From 74bb9cd27242b9320f99ff4d2b50c3051576cc14 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Feb 2022 16:53:51 +0800 Subject: ... --- include/cru/platform/gui/Base.h | 37 ++++++ include/cru/platform/gui/Base.hpp | 37 ------ include/cru/platform/gui/Clipboard.h | 9 ++ include/cru/platform/gui/Clipboard.hpp | 9 -- include/cru/platform/gui/Cursor.h | 16 +++ include/cru/platform/gui/Cursor.hpp | 16 --- include/cru/platform/gui/DebugFlags.h | 8 ++ include/cru/platform/gui/DebugFlags.hpp | 8 -- include/cru/platform/gui/InputMethod.h | 55 ++++++++ include/cru/platform/gui/InputMethod.hpp | 55 -------- include/cru/platform/gui/Keyboard.h | 140 +++++++++++++++++++++ include/cru/platform/gui/Keyboard.hpp | 140 --------------------- include/cru/platform/gui/Menu.h | 32 +++++ include/cru/platform/gui/Menu.hpp | 32 ----- include/cru/platform/gui/SaveOpenDialogOptions.h | 81 ++++++++++++ include/cru/platform/gui/SaveOpenDialogOptions.hpp | 81 ------------ include/cru/platform/gui/TimerHelper.h | 69 ++++++++++ include/cru/platform/gui/TimerHelper.hpp | 69 ---------- include/cru/platform/gui/UiApplication.h | 70 +++++++++++ include/cru/platform/gui/UiApplication.hpp | 70 ----------- include/cru/platform/gui/Window.h | 112 +++++++++++++++++ include/cru/platform/gui/Window.hpp | 112 ----------------- 22 files changed, 629 insertions(+), 629 deletions(-) create mode 100644 include/cru/platform/gui/Base.h delete mode 100644 include/cru/platform/gui/Base.hpp create mode 100644 include/cru/platform/gui/Clipboard.h delete mode 100644 include/cru/platform/gui/Clipboard.hpp create mode 100644 include/cru/platform/gui/Cursor.h delete mode 100644 include/cru/platform/gui/Cursor.hpp create mode 100644 include/cru/platform/gui/DebugFlags.h delete mode 100644 include/cru/platform/gui/DebugFlags.hpp create mode 100644 include/cru/platform/gui/InputMethod.h delete mode 100644 include/cru/platform/gui/InputMethod.hpp create mode 100644 include/cru/platform/gui/Keyboard.h delete mode 100644 include/cru/platform/gui/Keyboard.hpp create mode 100644 include/cru/platform/gui/Menu.h delete mode 100644 include/cru/platform/gui/Menu.hpp create mode 100644 include/cru/platform/gui/SaveOpenDialogOptions.h delete mode 100644 include/cru/platform/gui/SaveOpenDialogOptions.hpp create mode 100644 include/cru/platform/gui/TimerHelper.h delete mode 100644 include/cru/platform/gui/TimerHelper.hpp create mode 100644 include/cru/platform/gui/UiApplication.h delete mode 100644 include/cru/platform/gui/UiApplication.hpp create mode 100644 include/cru/platform/gui/Window.h delete mode 100644 include/cru/platform/gui/Window.hpp (limited to 'include/cru/platform/gui') diff --git a/include/cru/platform/gui/Base.h b/include/cru/platform/gui/Base.h new file mode 100644 index 00000000..789ab308 --- /dev/null +++ b/include/cru/platform/gui/Base.h @@ -0,0 +1,37 @@ +#pragma once +#include "cru/common/Base.h" +#include "cru/common/Bitmask.h" +#include "cru/platform/graphics/Base.h" + +#include "../Resource.h" + +#ifdef CRU_PLATFORM_WINDOWS +#ifdef CRU_PLATFORM_GUI_EXPORT_API +#define CRU_PLATFORM_GUI_API __declspec(dllexport) +#else +#define CRU_PLATFORM_GUI_API __declspec(dllimport) +#endif +#else +#define CRU_PLATFORM_GUI_API +#endif + +namespace cru::platform::gui { +struct ICursor; +struct ICursorManager; +struct IUiApplication; +struct INativeWindow; +struct IInputMethodContext; +struct IClipboard; + +namespace details { +struct TagMouseButton {}; +} // namespace details + +using MouseButton = Bitmask; + +namespace mouse_buttons { +constexpr MouseButton left{0b1}; +constexpr MouseButton middle{0b10}; +constexpr MouseButton right{0b100}; +} // namespace mouse_buttons +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Base.hpp b/include/cru/platform/gui/Base.hpp deleted file mode 100644 index b432a2e0..00000000 --- a/include/cru/platform/gui/Base.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once -#include "cru/common/Base.hpp" -#include "cru/common/Bitmask.hpp" -#include "cru/platform/graphics/Base.hpp" - -#include "../Resource.hpp" - -#ifdef CRU_PLATFORM_WINDOWS -#ifdef CRU_PLATFORM_GUI_EXPORT_API -#define CRU_PLATFORM_GUI_API __declspec(dllexport) -#else -#define CRU_PLATFORM_GUI_API __declspec(dllimport) -#endif -#else -#define CRU_PLATFORM_GUI_API -#endif - -namespace cru::platform::gui { -struct ICursor; -struct ICursorManager; -struct IUiApplication; -struct INativeWindow; -struct IInputMethodContext; -struct IClipboard; - -namespace details { -struct TagMouseButton {}; -} // namespace details - -using MouseButton = Bitmask; - -namespace mouse_buttons { -constexpr MouseButton left{0b1}; -constexpr MouseButton middle{0b10}; -constexpr MouseButton right{0b100}; -} // namespace mouse_buttons -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Clipboard.h b/include/cru/platform/gui/Clipboard.h new file mode 100644 index 00000000..47267895 --- /dev/null +++ b/include/cru/platform/gui/Clipboard.h @@ -0,0 +1,9 @@ +#pragma once +#include "Base.h" + +namespace cru::platform::gui { +struct IClipboard : virtual IPlatformResource { + virtual String GetText() = 0; + virtual void SetText(String text) = 0; +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Clipboard.hpp b/include/cru/platform/gui/Clipboard.hpp deleted file mode 100644 index c3467de5..00000000 --- a/include/cru/platform/gui/Clipboard.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Base.hpp" - -namespace cru::platform::gui { -struct IClipboard : virtual IPlatformResource { - virtual String GetText() = 0; - virtual void SetText(String text) = 0; -}; -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Cursor.h b/include/cru/platform/gui/Cursor.h new file mode 100644 index 00000000..99168656 --- /dev/null +++ b/include/cru/platform/gui/Cursor.h @@ -0,0 +1,16 @@ +#pragma once +#include "Base.h" + +#include + +namespace cru::platform::gui { +enum class SystemCursorType { Arrow, Hand, IBeam }; + +struct ICursor : virtual IPlatformResource {}; + +struct ICursorManager : virtual IPlatformResource { + virtual std::shared_ptr GetSystemCursor(SystemCursorType type) = 0; + + // TODO: Add method to create cursor. +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Cursor.hpp b/include/cru/platform/gui/Cursor.hpp deleted file mode 100644 index abc8b064..00000000 --- a/include/cru/platform/gui/Cursor.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include - -namespace cru::platform::gui { -enum class SystemCursorType { Arrow, Hand, IBeam }; - -struct ICursor : virtual IPlatformResource {}; - -struct ICursorManager : virtual IPlatformResource { - virtual std::shared_ptr GetSystemCursor(SystemCursorType type) = 0; - - // TODO: Add method to create cursor. -}; -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/DebugFlags.h b/include/cru/platform/gui/DebugFlags.h new file mode 100644 index 00000000..2b7c7c19 --- /dev/null +++ b/include/cru/platform/gui/DebugFlags.h @@ -0,0 +1,8 @@ +#pragma once + +namespace cru::platform::gui { +struct DebugFlags { + static constexpr int paint = 0; + static constexpr int input_method = 0; +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/DebugFlags.hpp b/include/cru/platform/gui/DebugFlags.hpp deleted file mode 100644 index 2b7c7c19..00000000 --- a/include/cru/platform/gui/DebugFlags.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -namespace cru::platform::gui { -struct DebugFlags { - static constexpr int paint = 0; - static constexpr int input_method = 0; -}; -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/InputMethod.h b/include/cru/platform/gui/InputMethod.h new file mode 100644 index 00000000..45e11c06 --- /dev/null +++ b/include/cru/platform/gui/InputMethod.h @@ -0,0 +1,55 @@ +#pragma once +#include "Base.h" + +#include "cru/common/Event.h" + +#include +#include + +namespace cru::platform::gui { +struct CompositionClause { + int start; + int end; + bool target; +}; + +using CompositionClauses = std::vector; + +struct CompositionText { + String text; + CompositionClauses clauses; + TextRange selection; +}; + +struct IInputMethodContext : virtual IPlatformResource { + // Return true if you should draw composition text manually. Return false if + // system will take care of that for you. + virtual bool ShouldManuallyDrawCompositionText() = 0; + + virtual void EnableIME() = 0; + + virtual void DisableIME() = 0; + + virtual void CompleteComposition() = 0; + + virtual void CancelComposition() = 0; + + virtual CompositionText GetCompositionText() = 0; + + // Set the candidate window lefttop. Relative to window lefttop. Use this + // method to prepare typing. + virtual void SetCandidateWindowPosition(const Point& point) = 0; + + // Triggered when user starts composition. + virtual IEvent* CompositionStartEvent() = 0; + + // Triggered when user stops composition. + virtual IEvent* CompositionEndEvent() = 0; + + // Triggered every time composition text changes. + virtual IEvent* CompositionEvent() = 0; + + virtual IEvent* TextEvent() = 0; +}; +} // namespace cru::platform::gui + diff --git a/include/cru/platform/gui/InputMethod.hpp b/include/cru/platform/gui/InputMethod.hpp deleted file mode 100644 index a34aba74..00000000 --- a/include/cru/platform/gui/InputMethod.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/Event.hpp" - -#include -#include - -namespace cru::platform::gui { -struct CompositionClause { - int start; - int end; - bool target; -}; - -using CompositionClauses = std::vector; - -struct CompositionText { - String text; - CompositionClauses clauses; - TextRange selection; -}; - -struct IInputMethodContext : virtual IPlatformResource { - // Return true if you should draw composition text manually. Return false if - // system will take care of that for you. - virtual bool ShouldManuallyDrawCompositionText() = 0; - - virtual void EnableIME() = 0; - - virtual void DisableIME() = 0; - - virtual void CompleteComposition() = 0; - - virtual void CancelComposition() = 0; - - virtual CompositionText GetCompositionText() = 0; - - // Set the candidate window lefttop. Relative to window lefttop. Use this - // method to prepare typing. - virtual void SetCandidateWindowPosition(const Point& point) = 0; - - // Triggered when user starts composition. - virtual IEvent* CompositionStartEvent() = 0; - - // Triggered when user stops composition. - virtual IEvent* CompositionEndEvent() = 0; - - // Triggered every time composition text changes. - virtual IEvent* CompositionEvent() = 0; - - virtual IEvent* TextEvent() = 0; -}; -} // namespace cru::platform::gui - diff --git a/include/cru/platform/gui/Keyboard.h b/include/cru/platform/gui/Keyboard.h new file mode 100644 index 00000000..f25b25fa --- /dev/null +++ b/include/cru/platform/gui/Keyboard.h @@ -0,0 +1,140 @@ +#pragma once +#include "cru/common/Bitmask.h" +#include "cru/platform/gui/Base.h" + +#include +#include + +namespace cru::platform::gui { +// Because of the complexity of keyboard layout, I only add code in US keyboard +// layout, the most widely used layout in China. We should try to make it easy +// to add new keyboard layout. +enum class KeyCode { + Unknown, + LeftButton, + MiddleButton, + RightButton, + Escape, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + N0, + N1, + N2, + N3, + N4, + N5, + N6, + N7, + N8, + N9, + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + GraveAccent, + Tab, + CapsLock, + LeftShift, + LeftCtrl, + LeftSuper, + LeftAlt, + Minus, + Equal, + Backspace, + LeftSquareBracket, + RightSquareBracket, + BackSlash, + Semicolon, + Quote, + Comma, + Period, + Slash, + RightShift, + RightCtrl, + RightSuper, + RightAlt, + Insert, + Delete, + Home, + End, + PageUp, + PageDown, + Up, + Left, + Down, + Right, + PrintScreen, + ScrollLock, + Pause, + NumPad0, + NumPad1, + NumPad2, + NumPad3, + NumPad4, + NumPad5, + NumPad6, + NumPad7, + NumPad8, + NumPad9, + LeftCommand, + RightCommand, + Return, + Space +}; + +namespace details { +struct TagKeyModifier {}; +} // namespace details + +using KeyModifier = Bitmask; + +struct KeyModifiers { + static constexpr KeyModifier none{0}; + static constexpr KeyModifier shift{0b1}; + static constexpr KeyModifier ctrl{0b10}; + static constexpr KeyModifier alt{0b100}; + static constexpr KeyModifier command{0b1000}; +}; + +#ifdef CRU_PLATFORM_OSX +constexpr KeyModifier kKeyModifierCommand = KeyModifiers::command; +#else +constexpr KeyModifier kKeyModifierCommand = KeyModifiers::ctrl; +#endif + +CRU_PLATFORM_GUI_API String ToString(KeyCode key_code); +CRU_PLATFORM_GUI_API String ToString(KeyModifier key_modifier, + StringView separator = u"+"); +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp deleted file mode 100644 index 347b57bc..00000000 --- a/include/cru/platform/gui/Keyboard.hpp +++ /dev/null @@ -1,140 +0,0 @@ -#pragma once -#include "cru/common/Bitmask.hpp" -#include "cru/platform/gui/Base.hpp" - -#include -#include - -namespace cru::platform::gui { -// Because of the complexity of keyboard layout, I only add code in US keyboard -// layout, the most widely used layout in China. We should try to make it easy -// to add new keyboard layout. -enum class KeyCode { - Unknown, - LeftButton, - MiddleButton, - RightButton, - Escape, - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - N0, - N1, - N2, - N3, - N4, - N5, - N6, - N7, - N8, - N9, - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - GraveAccent, - Tab, - CapsLock, - LeftShift, - LeftCtrl, - LeftSuper, - LeftAlt, - Minus, - Equal, - Backspace, - LeftSquareBracket, - RightSquareBracket, - BackSlash, - Semicolon, - Quote, - Comma, - Period, - Slash, - RightShift, - RightCtrl, - RightSuper, - RightAlt, - Insert, - Delete, - Home, - End, - PageUp, - PageDown, - Up, - Left, - Down, - Right, - PrintScreen, - ScrollLock, - Pause, - NumPad0, - NumPad1, - NumPad2, - NumPad3, - NumPad4, - NumPad5, - NumPad6, - NumPad7, - NumPad8, - NumPad9, - LeftCommand, - RightCommand, - Return, - Space -}; - -namespace details { -struct TagKeyModifier {}; -} // namespace details - -using KeyModifier = Bitmask; - -struct KeyModifiers { - static constexpr KeyModifier none{0}; - static constexpr KeyModifier shift{0b1}; - static constexpr KeyModifier ctrl{0b10}; - static constexpr KeyModifier alt{0b100}; - static constexpr KeyModifier command{0b1000}; -}; - -#ifdef CRU_PLATFORM_OSX -constexpr KeyModifier kKeyModifierCommand = KeyModifiers::command; -#else -constexpr KeyModifier kKeyModifierCommand = KeyModifiers::ctrl; -#endif - -CRU_PLATFORM_GUI_API String ToString(KeyCode key_code); -CRU_PLATFORM_GUI_API String ToString(KeyModifier key_modifier, - StringView separator = u"+"); -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Menu.h b/include/cru/platform/gui/Menu.h new file mode 100644 index 00000000..6e2a8902 --- /dev/null +++ b/include/cru/platform/gui/Menu.h @@ -0,0 +1,32 @@ +#pragma once +#include "Base.h" + +#include "Keyboard.h" + +#include + +namespace cru::platform::gui { +struct IMenu; + +struct CRU_PLATFORM_GUI_API IMenuItem : virtual IPlatformResource { + virtual String GetTitle() = 0; + virtual void SetTitle(String title) = 0; + virtual bool IsEnabled() = 0; + virtual void SetEnabled(bool enabled) = 0; + virtual IMenu* GetParentMenu() = 0; + virtual IMenu* GetSubmenu() = 0; + virtual void SetKeyboardShortcut(KeyCode key, KeyModifier modifiers) = 0; + virtual void DeleteKeyboardShortcut() = 0; + virtual void SetOnClickHandler(std::function handler) = 0; +}; + +struct CRU_PLATFORM_GUI_API IMenu : virtual IPlatformResource { + virtual IMenuItem* GetItemAt(int index) = 0; + virtual int GetItemCount() = 0; + virtual IMenuItem* CreateItemAt(int index) = 0; + virtual void RemoveItemAt(int index) = 0; + + virtual std::vector GetItems(); + bool Empty() { return GetItemCount() == 0; } +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Menu.hpp b/include/cru/platform/gui/Menu.hpp deleted file mode 100644 index fc2e844f..00000000 --- a/include/cru/platform/gui/Menu.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "Keyboard.hpp" - -#include - -namespace cru::platform::gui { -struct IMenu; - -struct CRU_PLATFORM_GUI_API IMenuItem : virtual IPlatformResource { - virtual String GetTitle() = 0; - virtual void SetTitle(String title) = 0; - virtual bool IsEnabled() = 0; - virtual void SetEnabled(bool enabled) = 0; - virtual IMenu* GetParentMenu() = 0; - virtual IMenu* GetSubmenu() = 0; - virtual void SetKeyboardShortcut(KeyCode key, KeyModifier modifiers) = 0; - virtual void DeleteKeyboardShortcut() = 0; - virtual void SetOnClickHandler(std::function handler) = 0; -}; - -struct CRU_PLATFORM_GUI_API IMenu : virtual IPlatformResource { - virtual IMenuItem* GetItemAt(int index) = 0; - virtual int GetItemCount() = 0; - virtual IMenuItem* CreateItemAt(int index) = 0; - virtual void RemoveItemAt(int index) = 0; - - virtual std::vector GetItems(); - bool Empty() { return GetItemCount() == 0; } -}; -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/SaveOpenDialogOptions.h b/include/cru/platform/gui/SaveOpenDialogOptions.h new file mode 100644 index 00000000..cff4a44f --- /dev/null +++ b/include/cru/platform/gui/SaveOpenDialogOptions.h @@ -0,0 +1,81 @@ +#pragma once +#include "Base.h" + +namespace cru::platform::gui { +struct CRU_PLATFORM_GUI_API SaveDialogOptions { + String title; + String prompt; + String message; + std::vector allowed_file_types; + bool allow_all_file_types = false; +}; + +struct CRU_PLATFORM_GUI_API OpenDialogOptions : SaveDialogOptions { + bool can_choose_files = true; + bool can_choose_directories = false; + bool allow_mulitple_selection = false; +}; + +template +struct CRU_PLATFORM_GUI_API SaveDialogOptionsBuilderTemplate { + T options; + + SaveDialogOptionsBuilderTemplate& SetTitle(String title) { + options.title = std::move(title); + return *this; + } + + SaveDialogOptionsBuilderTemplate& SetPrompt(String prompt) { + options.prompt = std::move(prompt); + return *this; + } + + SaveDialogOptionsBuilderTemplate& SetMessage(String message) { + options.message = std::move(message); + return *this; + } + + SaveDialogOptionsBuilderTemplate& SetAllowedFileTypes( + std::vector allowed_file_types) { + options.allowed_file_types = std::move(allowed_file_types); + return *this; + } + + SaveDialogOptionsBuilderTemplate& AddAllowedFileType( + String allowed_file_type) { + options.allowed_file_types.push_back(allowed_file_type); + return *this; + } + + SaveDialogOptionsBuilderTemplate& SetAllowAllFileTypes( + bool allow_all_file_types) { + options.allow_all_file_types = allow_all_file_types; + return *this; + } + + T Build() { return options; } +}; + +using SaveDialogOptionsBuilder = + SaveDialogOptionsBuilderTemplate; + +struct CRU_PLATFORM_GUI_API OpenDialogOptionsBuilder + : SaveDialogOptionsBuilderTemplate { + OpenDialogOptionsBuilder& SetCanChooseFiles(bool can_choose_files) { + options.can_choose_files = can_choose_files; + return *this; + } + + OpenDialogOptionsBuilder& SetCanChooseDirectories( + bool can_choose_directories) { + options.can_choose_directories = can_choose_directories; + return *this; + } + + OpenDialogOptionsBuilder& SetAllowMultipleSelection( + bool allow_mulitple_selection) { + options.allow_mulitple_selection = allow_mulitple_selection; + return *this; + } +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/SaveOpenDialogOptions.hpp b/include/cru/platform/gui/SaveOpenDialogOptions.hpp deleted file mode 100644 index 907ec808..00000000 --- a/include/cru/platform/gui/SaveOpenDialogOptions.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once -#include "Base.hpp" - -namespace cru::platform::gui { -struct CRU_PLATFORM_GUI_API SaveDialogOptions { - String title; - String prompt; - String message; - std::vector allowed_file_types; - bool allow_all_file_types = false; -}; - -struct CRU_PLATFORM_GUI_API OpenDialogOptions : SaveDialogOptions { - bool can_choose_files = true; - bool can_choose_directories = false; - bool allow_mulitple_selection = false; -}; - -template -struct CRU_PLATFORM_GUI_API SaveDialogOptionsBuilderTemplate { - T options; - - SaveDialogOptionsBuilderTemplate& SetTitle(String title) { - options.title = std::move(title); - return *this; - } - - SaveDialogOptionsBuilderTemplate& SetPrompt(String prompt) { - options.prompt = std::move(prompt); - return *this; - } - - SaveDialogOptionsBuilderTemplate& SetMessage(String message) { - options.message = std::move(message); - return *this; - } - - SaveDialogOptionsBuilderTemplate& SetAllowedFileTypes( - std::vector allowed_file_types) { - options.allowed_file_types = std::move(allowed_file_types); - return *this; - } - - SaveDialogOptionsBuilderTemplate& AddAllowedFileType( - String allowed_file_type) { - options.allowed_file_types.push_back(allowed_file_type); - return *this; - } - - SaveDialogOptionsBuilderTemplate& SetAllowAllFileTypes( - bool allow_all_file_types) { - options.allow_all_file_types = allow_all_file_types; - return *this; - } - - T Build() { return options; } -}; - -using SaveDialogOptionsBuilder = - SaveDialogOptionsBuilderTemplate; - -struct CRU_PLATFORM_GUI_API OpenDialogOptionsBuilder - : SaveDialogOptionsBuilderTemplate { - OpenDialogOptionsBuilder& SetCanChooseFiles(bool can_choose_files) { - options.can_choose_files = can_choose_files; - return *this; - } - - OpenDialogOptionsBuilder& SetCanChooseDirectories( - bool can_choose_directories) { - options.can_choose_directories = can_choose_directories; - return *this; - } - - OpenDialogOptionsBuilder& SetAllowMultipleSelection( - bool allow_mulitple_selection) { - options.allow_mulitple_selection = allow_mulitple_selection; - return *this; - } -}; -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/TimerHelper.h b/include/cru/platform/gui/TimerHelper.h new file mode 100644 index 00000000..91d436a4 --- /dev/null +++ b/include/cru/platform/gui/TimerHelper.h @@ -0,0 +1,69 @@ +#pragma once +#include "UiApplication.h" + +namespace cru::platform::gui { + +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 list_; +}; + +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/TimerHelper.hpp b/include/cru/platform/gui/TimerHelper.hpp deleted file mode 100644 index a61d2f04..00000000 --- a/include/cru/platform/gui/TimerHelper.hpp +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once -#include "UiApplication.hpp" - -namespace cru::platform::gui { - -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 list_; -}; - -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/UiApplication.h b/include/cru/platform/gui/UiApplication.h new file mode 100644 index 00000000..b39e2d36 --- /dev/null +++ b/include/cru/platform/gui/UiApplication.h @@ -0,0 +1,70 @@ +#pragma once +#include "Base.h" + +#include "cru/common/Bitmask.h" +#include "cru/platform/gui/Menu.h" + +#include "SaveOpenDialogOptions.h" + +#include +#include +#include +#include + +namespace cru::platform::gui { +// The entry point of a ui application. +struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { + 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 handler) = 0; + + virtual bool IsQuitOnAllWindowClosed() = 0; + virtual void SetQuitOnAllWindowClosed(bool quit_on_all_window_closed) = 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 action) = 0; + virtual long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function action) = 0; + virtual long long SetInterval(std::chrono::milliseconds milliseconds, + std::function 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 GetAllWindow() = 0; + + virtual INativeWindow* CreateWindow() = 0; + + virtual cru::platform::graphics::IGraphicsFactory* GetGraphicsFactory() = 0; + + virtual ICursorManager* GetCursorManager() = 0; + + virtual IClipboard* GetClipboard() = 0; + + // If return nullptr, it means the menu is not supported. + virtual IMenu* GetApplicationMenu(); + + virtual std::optional ShowSaveDialog(SaveDialogOptions options); + virtual std::optional> ShowOpenDialog( + OpenDialogOptions options); +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/UiApplication.hpp b/include/cru/platform/gui/UiApplication.hpp deleted file mode 100644 index c917aa01..00000000 --- a/include/cru/platform/gui/UiApplication.hpp +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/Bitmask.hpp" -#include "cru/platform/gui/Menu.hpp" - -#include "SaveOpenDialogOptions.hpp" - -#include -#include -#include -#include - -namespace cru::platform::gui { -// The entry point of a ui application. -struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { - 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 handler) = 0; - - virtual bool IsQuitOnAllWindowClosed() = 0; - virtual void SetQuitOnAllWindowClosed(bool quit_on_all_window_closed) = 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 action) = 0; - virtual long long SetTimeout(std::chrono::milliseconds milliseconds, - std::function action) = 0; - virtual long long SetInterval(std::chrono::milliseconds milliseconds, - std::function 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 GetAllWindow() = 0; - - virtual INativeWindow* CreateWindow() = 0; - - virtual cru::platform::graphics::IGraphicsFactory* GetGraphicsFactory() = 0; - - virtual ICursorManager* GetCursorManager() = 0; - - virtual IClipboard* GetClipboard() = 0; - - // If return nullptr, it means the menu is not supported. - virtual IMenu* GetApplicationMenu(); - - virtual std::optional ShowSaveDialog(SaveDialogOptions options); - virtual std::optional> ShowOpenDialog( - OpenDialogOptions options); -}; -} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Window.h b/include/cru/platform/gui/Window.h new file mode 100644 index 00000000..d3f0daad --- /dev/null +++ b/include/cru/platform/gui/Window.h @@ -0,0 +1,112 @@ +#pragma once +#include "Base.h" + +#include "Keyboard.h" + +#include "cru/common/Event.h" + +#include + +namespace cru::platform::gui { +namespace details { +struct WindowStyleFlagTag; +} + +using WindowStyleFlag = Bitmask; + +struct WindowStyleFlags { + static constexpr WindowStyleFlag NoCaptionAndBorder{0b1}; +}; + +enum class WindowVisibilityType { Show, Hide, Minimize }; + +enum class FocusChangeType { Gain, Lose }; + +enum class MouseEnterLeaveType { Enter, Leave }; + +struct NativeMouseButtonEventArgs { + MouseButton button; + Point point; + KeyModifier modifier; +}; + +struct NativeMouseWheelEventArgs { + // Positive means down. Negative means up. + float delta; + Point point; + KeyModifier modifier; + bool horizontal; // true if horizontal wheel. +}; + +struct NativeKeyEventArgs { + KeyCode key; + KeyModifier modifier; +}; + +// Represents a native window, which exposes some low-level events and +// operations. +struct INativeWindow : virtual IPlatformResource { + virtual void Close() = 0; + + virtual INativeWindow* GetParent() = 0; + virtual void SetParent(INativeWindow* parent) = 0; + + virtual WindowStyleFlag GetStyleFlag() = 0; + virtual void SetStyleFlag(WindowStyleFlag flag) = 0; + + virtual String GetTitle() = 0; + virtual void SetTitle(String title) = 0; + + virtual WindowVisibilityType GetVisibility() = 0; + virtual void SetVisibility(WindowVisibilityType visibility) = 0; + + virtual Size GetClientSize() = 0; + virtual void SetClientSize(const Size& size) = 0; + + virtual Rect GetClientRect() = 0; + virtual void SetClientRect(const Rect& rect) = 0; + + // Get the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + virtual Rect GetWindowRect() = 0; + + // Set the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + virtual void SetWindowRect(const Rect& rect) = 0; + + virtual bool RequestFocus() = 0; + + // Relative to client lefttop. + virtual Point GetMousePosition() = 0; + + virtual bool CaptureMouse() = 0; + virtual bool ReleaseMouse() = 0; + + virtual void SetCursor(std::shared_ptr cursor) = 0; + + virtual void SetToForeground() = 0; + + virtual void RequestRepaint() = 0; + + // Remember to call EndDraw on return value and destroy it. + virtual std::unique_ptr BeginPaint() = 0; + + virtual IEvent* CreateEvent() = 0; + virtual IEvent* DestroyEvent() = 0; + virtual IEvent* PaintEvent() = 0; + + virtual IEvent* VisibilityChangeEvent() = 0; + virtual IEvent* ResizeEvent() = 0; + virtual IEvent* FocusEvent() = 0; + + virtual IEvent* MouseEnterLeaveEvent() = 0; + virtual IEvent* MouseMoveEvent() = 0; + virtual IEvent* MouseDownEvent() = 0; + virtual IEvent* MouseUpEvent() = 0; + virtual IEvent* MouseWheelEvent() = 0; + virtual IEvent* KeyDownEvent() = 0; + virtual IEvent* KeyUpEvent() = 0; + + virtual IInputMethodContext* GetInputMethodContext() = 0; +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Window.hpp b/include/cru/platform/gui/Window.hpp deleted file mode 100644 index a8fe3ca6..00000000 --- a/include/cru/platform/gui/Window.hpp +++ /dev/null @@ -1,112 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "Keyboard.hpp" - -#include "cru/common/Event.hpp" - -#include - -namespace cru::platform::gui { -namespace details { -struct WindowStyleFlagTag; -} - -using WindowStyleFlag = Bitmask; - -struct WindowStyleFlags { - static constexpr WindowStyleFlag NoCaptionAndBorder{0b1}; -}; - -enum class WindowVisibilityType { Show, Hide, Minimize }; - -enum class FocusChangeType { Gain, Lose }; - -enum class MouseEnterLeaveType { Enter, Leave }; - -struct NativeMouseButtonEventArgs { - MouseButton button; - Point point; - KeyModifier modifier; -}; - -struct NativeMouseWheelEventArgs { - // Positive means down. Negative means up. - float delta; - Point point; - KeyModifier modifier; - bool horizontal; // true if horizontal wheel. -}; - -struct NativeKeyEventArgs { - KeyCode key; - KeyModifier modifier; -}; - -// Represents a native window, which exposes some low-level events and -// operations. -struct INativeWindow : virtual IPlatformResource { - virtual void Close() = 0; - - virtual INativeWindow* GetParent() = 0; - virtual void SetParent(INativeWindow* parent) = 0; - - virtual WindowStyleFlag GetStyleFlag() = 0; - virtual void SetStyleFlag(WindowStyleFlag flag) = 0; - - virtual String GetTitle() = 0; - virtual void SetTitle(String title) = 0; - - virtual WindowVisibilityType GetVisibility() = 0; - virtual void SetVisibility(WindowVisibilityType visibility) = 0; - - virtual Size GetClientSize() = 0; - virtual void SetClientSize(const Size& size) = 0; - - virtual Rect GetClientRect() = 0; - virtual void SetClientRect(const Rect& rect) = 0; - - // Get the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - virtual Rect GetWindowRect() = 0; - - // Set the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - virtual void SetWindowRect(const Rect& rect) = 0; - - virtual bool RequestFocus() = 0; - - // Relative to client lefttop. - virtual Point GetMousePosition() = 0; - - virtual bool CaptureMouse() = 0; - virtual bool ReleaseMouse() = 0; - - virtual void SetCursor(std::shared_ptr cursor) = 0; - - virtual void SetToForeground() = 0; - - virtual void RequestRepaint() = 0; - - // Remember to call EndDraw on return value and destroy it. - virtual std::unique_ptr BeginPaint() = 0; - - virtual IEvent* CreateEvent() = 0; - virtual IEvent* DestroyEvent() = 0; - virtual IEvent* PaintEvent() = 0; - - virtual IEvent* VisibilityChangeEvent() = 0; - virtual IEvent* ResizeEvent() = 0; - virtual IEvent* FocusEvent() = 0; - - virtual IEvent* MouseEnterLeaveEvent() = 0; - virtual IEvent* MouseMoveEvent() = 0; - virtual IEvent* MouseDownEvent() = 0; - virtual IEvent* MouseUpEvent() = 0; - virtual IEvent* MouseWheelEvent() = 0; - virtual IEvent* KeyDownEvent() = 0; - virtual IEvent* KeyUpEvent() = 0; - - virtual IInputMethodContext* GetInputMethodContext() = 0; -}; -} // namespace cru::platform::gui -- cgit v1.2.3