#pragma once #include "system_headers.hpp" #include #include #include #include #include #include "base.hpp" #include "any_map.h" namespace cru { class Application; namespace ui { class WindowClass; class WindowManager; namespace animations::details { class AnimationManager; } } namespace graph { class GraphManager; } class TimerManager; struct CaretInfo { std::chrono::milliseconds caret_blink_duration; float half_caret_width; }; class GodWindow : public Object { public: explicit GodWindow(Application* application); GodWindow(const GodWindow& other) = delete; GodWindow(GodWindow&& other) = delete; GodWindow& operator=(const GodWindow& other) = delete; GodWindow& operator=(GodWindow&& other) = delete; ~GodWindow() override; HWND GetHandle() const { return hwnd_; } std::optional HandleGodWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param); private: Application* application_; std::unique_ptr god_window_class_; HWND hwnd_; }; class Application : public Object { public: static Application* GetInstance(); private: static Application* instance_; public: explicit Application(HINSTANCE h_instance); Application(const Application&) = delete; Application(Application&&) = delete; Application& operator = (const Application&) = delete; Application& operator = (Application&&) = delete; ~Application() override; public: int Run(); void Quit(int quit_code); HINSTANCE GetInstanceHandle() const { return h_instance_; } GodWindow* GetGodWindow() const { return god_window_.get(); } // Resolve a singleton. // All singletons will be delete in reverse order of resolve. template>> T* ResolveSingleton(const std::function& creator) { const auto& index = std::type_index{typeid(T)}; const auto find_result = singleton_map_.find(index); if (find_result != singleton_map_.cend()) return static_cast(find_result->second); auto singleton = creator(this); singleton_map_.emplace(index, static_cast(singleton)); singleton_list_.push_back(singleton); return singleton; } CaretInfo GetCaretInfo() const { return caret_info_; } const AnyMap* GetPredefineResourceMap() const { return &predefine_resource_map_; } private: HINSTANCE h_instance_; std::unique_ptr god_window_; std::unordered_map singleton_map_; std::list singleton_list_; // used for reverse destroy. CaretInfo caret_info_; AnyMap predefine_resource_map_{}; }; void InvokeLater(const std::function& action); }