Repository navigation
Expand file tree
/
Copy pathNavigationManager.cpp
More file actions
179 lines (148 loc) · 5.25 KB
/
Copy pathNavigationManager.cpp
File metadata and controls
179 lines (148 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) Explorer++ Project
// SPDX-License-Identifier: GPL-3.0-only
// See LICENSE in the top level directory
#include "stdafx.h"
#include "NavigationManager.h"
#include "NavigationEvents.h"
#include "NavigationRequest.h"
#include "ShellEnumerator.h"
#include "../Helper/ScopedStopSource.h"
#include "../Helper/ShellHelper.h"
#include <algorithm>
#include <ranges>
NavigationManager::NavigationManager(const ShellBrowser *shellBrowser,
NavigationEvents *navigationEvents, std::shared_ptr<const ShellEnumerator> shellEnumerator,
std::shared_ptr<concurrencpp::executor> enumerationExecutor,
std::shared_ptr<concurrencpp::executor> originalExecutor) :
m_shellBrowser(shellBrowser),
m_navigationEvents(navigationEvents),
m_shellEnumerator(shellEnumerator),
m_enumerationExecutor(enumerationExecutor),
m_originalExecutor(originalExecutor),
m_scopedStopSource(std::make_unique<ScopedStopSource>())
{
}
NavigationManager::~NavigationManager() = default;
void NavigationManager::StartNavigation(const NavigateParams &navigateParams)
{
auto navigationRequest = std::make_unique<NavigationRequest>(m_shellBrowser, m_navigationEvents,
static_cast<NavigationRequestDelegate *>(this), m_shellEnumerator, m_enumerationExecutor,
m_originalExecutor, navigateParams, m_scopedStopSource->GetToken());
auto *rawNavigationRequest = navigationRequest.get();
m_pendingNavigations.push_back(std::move(navigationRequest));
rawNavigationRequest->Start();
}
void NavigationManager::OnEnumerationCompleted(NavigationRequest *request)
{
CommitNavigation(request);
}
void NavigationManager::OnEnumerationFailed(NavigationRequest *request)
{
if (!m_anyNavigationsCommitted)
{
// Typically, when a navigation fails, nothing will happen. The original folder will
// continue to be shown. However, when the initial navigation fails, there is no original
// folder, so the only reasonable choice is to commit the failed navigation, regardless.
CommitNavigation(request);
return;
}
request->Fail();
}
void NavigationManager::OnEnumerationStopped(NavigationRequest *request)
{
if (!m_anyNavigationsCommitted)
{
CommitNavigation(request);
return;
}
request->Cancel();
}
void NavigationManager::OnFinished(NavigationRequest *request)
{
RemoveNavigationRequest(request);
}
void NavigationManager::CommitNavigation(NavigationRequest *request)
{
UNREFERENCED_PARAMETER(request);
// The specified navigation will be committed, so all other navigations should be cancelled.
m_scopedStopSource = std::make_unique<ScopedStopSource>();
m_anyNavigationsCommitted = true;
request->Commit();
}
void NavigationManager::RemoveNavigationRequest(NavigationRequest *request)
{
auto itr = std::ranges::find_if(m_pendingNavigations,
[request](const auto &navigationRequest) { return navigationRequest.get() == request; });
CHECK(itr != m_pendingNavigations.end());
m_pendingNavigations.erase(itr);
}
void NavigationManager::StopLoading()
{
m_scopedStopSource = std::make_unique<ScopedStopSource>();
m_navigationEvents->NotifyStopped(m_shellBrowser);
}
// TODO: This should use std::generator once C++23 support is available.
concurrencpp::generator<const NavigationRequest *> NavigationManager::GetPendingNavigations() const
{
for (const auto &pendingNavigation : m_pendingNavigations)
{
co_yield pendingNavigation.get();
}
}
const NavigationRequest *NavigationManager::MaybeGetLatestPendingNavigation() const
{
if (m_pendingNavigations.empty())
{
return nullptr;
}
const auto &lastItem = *m_pendingNavigations.rbegin();
return lastItem.get();
}
size_t NavigationManager::GetNumPendingNavigations() const
{
return m_pendingNavigations.size();
}
bool NavigationManager::HasAnyPendingNavigations() const
{
return !m_pendingNavigations.empty();
}
// TODO: This should use std::generator once C++23 support is available.
concurrencpp::generator<const NavigationRequest *> NavigationManager::GetActiveNavigations() const
{
for (const auto &pendingNavigation : m_pendingNavigations
| std::views::filter(std::bind_front(&NavigationManager::ActiveNavigationFilter, this)))
{
co_yield pendingNavigation.get();
}
}
const NavigationRequest *NavigationManager::MaybeGetLatestActiveNavigation() const
{
auto itr = std::ranges::find_if(m_pendingNavigations.rbegin(), m_pendingNavigations.rend(),
std::bind_front(&NavigationManager::ActiveNavigationFilter, this));
if (itr == m_pendingNavigations.rend())
{
return nullptr;
}
return itr->get();
}
size_t NavigationManager::GetNumActiveNavigations() const
{
return std::ranges::count_if(m_pendingNavigations,
std::bind_front(&NavigationManager::ActiveNavigationFilter, this));
}
bool NavigationManager::ActiveNavigationFilter(
const std::unique_ptr<NavigationRequest> &pendingNavigation) const
{
if (!m_anyNavigationsCommitted)
{
// In this case, any pending navigation has the ability to commit, so every pending
// navigation is considered active.
return true;
}
// Typically, only navigations that haven't been stopped are considered active.
return !pendingNavigation->Stopped();
}
bool NavigationManager::HasAnyActiveNavigations() const
{
return GetNumActiveNavigations() > 0;
}