plusui-native-core 0.1.86 → 0.1.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Core/CMakeLists.txt
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#include <plusui/router.hpp>
|
|
2
|
+
|
|
3
|
+
namespace plusui {
|
|
4
|
+
|
|
5
|
+
struct Router::Impl {
|
|
6
|
+
std::map<std::string, std::string> routes;
|
|
7
|
+
std::string initialRoute = "/";
|
|
8
|
+
std::map<std::string, std::string> currentRoutes;
|
|
9
|
+
std::vector<RouteChangeCallback> callbacks;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
Router::Router() : pImpl(std::make_unique<Impl>()) {}
|
|
13
|
+
Router::~Router() = default;
|
|
14
|
+
|
|
15
|
+
Router& Router::instance() {
|
|
16
|
+
static Router router;
|
|
17
|
+
return router;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
void Router::setRoutes(const std::map<std::string, std::string>& routes) {
|
|
21
|
+
pImpl->routes = routes;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
std::map<std::string, std::string> Router::getRoutes() const {
|
|
25
|
+
return pImpl->routes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void Router::push(const std::string& route, const std::string& windowId) {
|
|
29
|
+
std::string wid = windowId.empty() ? "__main__" : windowId;
|
|
30
|
+
pImpl->currentRoutes[wid] = route;
|
|
31
|
+
for (auto& cb : pImpl->callbacks) {
|
|
32
|
+
cb(route, wid);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
void Router::replace(const std::string& route, const std::string& windowId) {
|
|
37
|
+
std::string wid = windowId.empty() ? "__main__" : windowId;
|
|
38
|
+
pImpl->currentRoutes[wid] = route;
|
|
39
|
+
for (auto& cb : pImpl->callbacks) {
|
|
40
|
+
cb(route, wid);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
std::string Router::getCurrentRoute(const std::string& windowId) const {
|
|
45
|
+
std::string wid = windowId.empty() ? "__main__" : windowId;
|
|
46
|
+
auto it = pImpl->currentRoutes.find(wid);
|
|
47
|
+
if (it != pImpl->currentRoutes.end()) {
|
|
48
|
+
return it->second;
|
|
49
|
+
}
|
|
50
|
+
return pImpl->initialRoute;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void Router::setInitialRoute(const std::string& route) {
|
|
54
|
+
pImpl->initialRoute = route;
|
|
55
|
+
pImpl->currentRoutes["__main__"] = route;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void Router::onRouteChange(RouteChangeCallback callback) {
|
|
59
|
+
pImpl->callbacks.push_back(std::move(callback));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
} // namespace plusui
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#ifndef PLUSUI_ROUTER_H
|
|
2
|
+
#define PLUSUI_ROUTER_H
|
|
3
|
+
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <map>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
namespace plusui {
|
|
11
|
+
|
|
12
|
+
class Router {
|
|
13
|
+
public:
|
|
14
|
+
Router();
|
|
15
|
+
~Router();
|
|
16
|
+
|
|
17
|
+
static Router& instance();
|
|
18
|
+
|
|
19
|
+
void setRoutes(const std::map<std::string, std::string>& routes);
|
|
20
|
+
std::map<std::string, std::string> getRoutes() const;
|
|
21
|
+
|
|
22
|
+
void push(const std::string& route, const std::string& windowId = "");
|
|
23
|
+
void replace(const std::string& route, const std::string& windowId = "");
|
|
24
|
+
|
|
25
|
+
std::string getCurrentRoute(const std::string& windowId = "") const;
|
|
26
|
+
void setInitialRoute(const std::string& route);
|
|
27
|
+
|
|
28
|
+
using RouteChangeCallback = std::function<void(const std::string& route, const std::string& windowId)>;
|
|
29
|
+
void onRouteChange(RouteChangeCallback callback);
|
|
30
|
+
|
|
31
|
+
private:
|
|
32
|
+
struct Impl;
|
|
33
|
+
std::unique_ptr<Impl> pImpl;
|
|
34
|
+
|
|
35
|
+
// Prevent copying
|
|
36
|
+
Router(const Router&) = delete;
|
|
37
|
+
Router& operator=(const Router&) = delete;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
} // namespace plusui
|
|
41
|
+
|
|
42
|
+
#endif // PLUSUI_ROUTER_H
|