aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/gui/UiApplication.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-03-24 19:14:19 +0800
committercrupest <crupest@outlook.com>2021-03-24 19:14:19 +0800
commit7f15a1ff9a2007e119798053083a0a87d042990a (patch)
treecb35c01a7eaee867376d959b96c9bbd15df939e5 /include/cru/platform/gui/UiApplication.hpp
parent74956951ee663012df0c3fe4ebe29799cb2f7732 (diff)
parent7703063a5816b089483e78ccd74bb9902ccfbea8 (diff)
downloadcru-7f15a1ff9a2007e119798053083a0a87d042990a.tar.gz
cru-7f15a1ff9a2007e119798053083a0a87d042990a.tar.bz2
cru-7f15a1ff9a2007e119798053083a0a87d042990a.zip
Merge branch 'master' of https://github.com/crupest/CruUI
Diffstat (limited to 'include/cru/platform/gui/UiApplication.hpp')
-rw-r--r--include/cru/platform/gui/UiApplication.hpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/include/cru/platform/gui/UiApplication.hpp b/include/cru/platform/gui/UiApplication.hpp
new file mode 100644
index 00000000..5a5b0b13
--- /dev/null
+++ b/include/cru/platform/gui/UiApplication.hpp
@@ -0,0 +1,135 @@
+#pragma once
+#include "Base.hpp"
+
+#include "cru/common/Bitmask.hpp"
+
+#include <chrono>
+#include <functional>
+#include <memory>
+#include <vector>
+
+namespace cru::platform::gui {
+namespace details {
+struct CreateWindowFlagTag;
+}
+
+using CreateWindowFlag = Bitmask<details::CreateWindowFlagTag>;
+
+struct CreateWindowFlags {
+ static constexpr CreateWindowFlag NoCaptionAndBorder{0b1};
+};
+
+// The entry point of a ui application.
+struct IUiApplication : public virtual INativeResource {
+ public:
+ static IUiApplication* GetInstance() { return instance; }
+
+ private:
+ static IUiApplication* instance;
+
+ protected:
+ IUiApplication();
+
+ public:
+ ~IUiApplication() override;
+
+ // Block current thread and run the message loop. Return the exit code when
+ // message loop gets a quit message (possibly posted by method RequestQuit).
+ virtual int Run() = 0;
+
+ // Post a quit message with given quit code.
+ virtual void RequestQuit(int quit_code) = 0;
+
+ virtual void AddOnQuitHandler(std::function<void()> handler) = 0;
+
+ // Timer id should always be positive (not 0) and never the same. So it's ok
+ // to use negative value (or 0) to represent no timer.
+ virtual long long SetImmediate(std::function<void()> action) = 0;
+ virtual long long SetTimeout(std::chrono::milliseconds milliseconds,
+ std::function<void()> action) = 0;
+ virtual long long SetInterval(std::chrono::milliseconds milliseconds,
+ std::function<void()> action) = 0;
+ // Implementation should guarantee calls on timer id already canceled have no
+ // effects and do not crash. Also canceling negative id or 0 should always
+ // result in no-op.
+ virtual void CancelTimer(long long id) = 0;
+
+ virtual std::vector<INativeWindow*> GetAllWindow() = 0;
+
+ INativeWindow* CreateWindow(INativeWindow* parent) {
+ return this->CreateWindow(parent, CreateWindowFlag(0));
+ };
+ virtual INativeWindow* CreateWindow(INativeWindow* parent,
+ CreateWindowFlag flags) = 0;
+
+ virtual cru::platform::graphics::IGraphFactory* GetGraphFactory() = 0;
+
+ virtual ICursorManager* GetCursorManager() = 0;
+};
+
+class TimerAutoCanceler {
+ public:
+ TimerAutoCanceler() : id_(0) {}
+ explicit TimerAutoCanceler(long long id) : id_(id) {}
+
+ CRU_DELETE_COPY(TimerAutoCanceler)
+
+ TimerAutoCanceler(TimerAutoCanceler&& other) : id_(other.id_) {
+ other.id_ = 0;
+ }
+
+ TimerAutoCanceler& operator=(TimerAutoCanceler&& other) {
+ if (&other == this) {
+ return *this;
+ }
+ Reset(other.id_);
+ other.id_ = 0;
+ return *this;
+ }
+
+ TimerAutoCanceler& operator=(long long other) {
+ return this->operator=(TimerAutoCanceler(other));
+ }
+
+ ~TimerAutoCanceler() { Reset(); }
+
+ long long Release() {
+ auto temp = id_;
+ id_ = 0;
+ return temp;
+ }
+
+ void Reset(long long id = 0) {
+ if (id_ > 0) IUiApplication::GetInstance()->CancelTimer(id_);
+ id_ = id;
+ }
+
+ explicit operator bool() const { return id_; }
+
+ private:
+ long long id_;
+};
+
+class TimerListAutoCanceler {
+ public:
+ TimerListAutoCanceler() = default;
+ CRU_DELETE_COPY(TimerListAutoCanceler)
+ CRU_DEFAULT_MOVE(TimerListAutoCanceler)
+ ~TimerListAutoCanceler() = default;
+
+ TimerListAutoCanceler& operator+=(long long id) {
+ list_.push_back(TimerAutoCanceler(id));
+ return *this;
+ }
+
+ void Clear() { list_.clear(); }
+
+ bool IsEmpty() const { return list_.empty(); }
+
+ private:
+ std::vector<TimerAutoCanceler> list_;
+};
+
+// Bootstrap from this.
+std::unique_ptr<IUiApplication> CreateUiApplication();
+} // namespace cru::platform::gui