From 5729a5aa1b443e3e25f3e14dee29636d3b31a6f8 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 18 Oct 2020 21:09:21 +0800 Subject: ... --- src/ui/UiManager.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/ui/UiManager.cpp') diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index 4cd38efa..8a2029b9 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -30,7 +30,10 @@ UiManager* UiManager::GetInstance() { UiManager::UiManager() { const auto factory = GetGraphFactory(); - theme_resource_.default_font = factory->CreateFont(u"等线", 24.0f); + theme_resource_.default_font_family = u"等线"; + + theme_resource_.default_font = + factory->CreateFont(theme_resource_.default_font_family, 24.0f); const auto black_brush = std::shared_ptr( CreateSolidColorBrush(factory, colors::black)); -- cgit v1.2.3 From 6aa2201797a9ed64ce0178215ae941d0c5f09579 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Oct 2020 00:07:57 +0800 Subject: ... --- demos/CMakeLists.txt | 2 +- demos/input_method/main.cpp | 16 +- demos/main/main.cpp | 6 +- include/cru/platform/graph/Base.hpp | 24 -- include/cru/platform/graph/Brush.hpp | 11 - include/cru/platform/graph/Factory.hpp | 25 -- include/cru/platform/graph/Font.hpp | 8 - include/cru/platform/graph/Geometry.hpp | 20 - include/cru/platform/graph/Painter.hpp | 29 -- include/cru/platform/graph/Resource.hpp | 10 - include/cru/platform/graph/TextLayout.hpp | 24 -- include/cru/platform/graph/util/Painter.hpp | 17 - include/cru/platform/graphics/Base.hpp | 24 ++ include/cru/platform/graphics/Brush.hpp | 11 + include/cru/platform/graphics/Factory.hpp | 25 ++ include/cru/platform/graphics/Font.hpp | 8 + include/cru/platform/graphics/Geometry.hpp | 20 + include/cru/platform/graphics/Painter.hpp | 29 ++ include/cru/platform/graphics/Resource.hpp | 10 + include/cru/platform/graphics/TextLayout.hpp | 24 ++ include/cru/platform/graphics/util/Painter.hpp | 17 + include/cru/platform/gui/Base.hpp | 47 +++ include/cru/platform/gui/Cursor.hpp | 14 + include/cru/platform/gui/InputMethod.hpp | 80 ++++ include/cru/platform/gui/Keyboard.hpp | 127 ++++++ include/cru/platform/gui/UiApplication.hpp | 109 +++++ include/cru/platform/gui/Window.hpp | 57 +++ include/cru/platform/native/Base.hpp | 47 --- include/cru/platform/native/Cursor.hpp | 14 - include/cru/platform/native/InputMethod.hpp | 80 ---- include/cru/platform/native/Keyboard.hpp | 127 ------ include/cru/platform/native/UiApplication.hpp | 109 ----- include/cru/platform/native/Window.hpp | 57 --- include/cru/ui/Base.hpp | 20 +- include/cru/ui/Control.hpp | 8 +- include/cru/ui/ShortcutHub.hpp | 22 +- include/cru/ui/UiEvent.hpp | 30 +- include/cru/ui/UiManager.hpp | 8 +- include/cru/ui/WindowHost.hpp | 42 +- include/cru/ui/render/BorderRenderObject.hpp | 26 +- include/cru/ui/render/CanvasRenderObject.hpp | 2 +- include/cru/ui/render/LayoutRenderObject.hpp | 2 +- include/cru/ui/render/RenderObject.hpp | 12 +- include/cru/ui/render/ScrollRenderObject.hpp | 4 +- include/cru/ui/render/TextRenderObject.hpp | 38 +- include/cru/win/graph/direct/Brush.hpp | 39 -- include/cru/win/graph/direct/ComResource.hpp | 11 - include/cru/win/graph/direct/ConvertUtil.hpp | 107 ----- include/cru/win/graph/direct/Exception.hpp | 7 - include/cru/win/graph/direct/Factory.hpp | 58 --- include/cru/win/graph/direct/Font.hpp | 33 -- include/cru/win/graph/direct/Geometry.hpp | 57 --- include/cru/win/graph/direct/Painter.hpp | 60 --- include/cru/win/graph/direct/Resource.hpp | 49 --- include/cru/win/graph/direct/TextLayout.hpp | 55 --- include/cru/win/graph/direct/WindowPainter.hpp | 21 - .../cru/win/graph/direct/WindowRenderTarget.hpp | 42 -- include/cru/win/graphics/direct/Brush.hpp | 39 ++ include/cru/win/graphics/direct/ComResource.hpp | 11 + include/cru/win/graphics/direct/ConvertUtil.hpp | 107 +++++ include/cru/win/graphics/direct/Exception.hpp | 7 + include/cru/win/graphics/direct/Factory.hpp | 58 +++ include/cru/win/graphics/direct/Font.hpp | 33 ++ include/cru/win/graphics/direct/Geometry.hpp | 57 +++ include/cru/win/graphics/direct/Painter.hpp | 60 +++ include/cru/win/graphics/direct/Resource.hpp | 49 +++ include/cru/win/graphics/direct/TextLayout.hpp | 55 +++ include/cru/win/graphics/direct/WindowPainter.hpp | 21 + .../cru/win/graphics/direct/WindowRenderTarget.hpp | 42 ++ include/cru/win/gui/Base.hpp | 16 + include/cru/win/gui/Cursor.hpp | 49 +++ include/cru/win/gui/Exception.hpp | 7 + include/cru/win/gui/GodWindow.hpp | 38 ++ include/cru/win/gui/InputMethod.hpp | 87 ++++ include/cru/win/gui/Keyboard.hpp | 9 + include/cru/win/gui/Resource.hpp | 23 ++ include/cru/win/gui/UiApplication.hpp | 74 ++++ include/cru/win/gui/Window.hpp | 178 ++++++++ include/cru/win/gui/WindowClass.hpp | 24 ++ .../cru/win/gui/WindowNativeMessageEventArgs.hpp | 40 ++ include/cru/win/native/Base.hpp | 16 - include/cru/win/native/Cursor.hpp | 49 --- include/cru/win/native/Exception.hpp | 7 - include/cru/win/native/GodWindow.hpp | 38 -- include/cru/win/native/InputMethod.hpp | 87 ---- include/cru/win/native/Keyboard.hpp | 9 - include/cru/win/native/Resource.hpp | 23 -- include/cru/win/native/UiApplication.hpp | 74 ---- include/cru/win/native/Window.hpp | 178 -------- include/cru/win/native/WindowClass.hpp | 24 -- .../win/native/WindowNativeMessageEventArgs.hpp | 40 -- src/platform/CMakeLists.txt | 4 +- src/platform/graph/CMakeLists.txt | 14 - src/platform/graphics/CMakeLists.txt | 14 + src/platform/gui/CMakeLists.txt | 14 + src/platform/gui/Keyboard.cpp | 142 +++++++ src/platform/gui/UiApplication.cpp | 15 + src/platform/native/CMakeLists.txt | 14 - src/platform/native/Keyboard.cpp | 142 ------- src/platform/native/UiApplication.cpp | 15 - src/ui/CMakeLists.txt | 2 +- src/ui/Control.cpp | 10 +- src/ui/Helper.cpp | 8 +- src/ui/Helper.hpp | 4 +- src/ui/UiManager.cpp | 12 +- src/ui/WindowHost.cpp | 30 +- src/ui/controls/Button.cpp | 10 +- src/ui/controls/Container.cpp | 2 +- src/ui/controls/TextControlService.hpp | 22 +- src/ui/render/BorderRenderObject.cpp | 12 +- src/ui/render/CanvasRenderObject.cpp | 2 +- src/ui/render/FlexLayoutRenderObject.cpp | 2 +- src/ui/render/RenderObject.cpp | 16 +- src/ui/render/ScrollRenderObject.cpp | 10 +- src/ui/render/TextRenderObject.cpp | 28 +- src/win/CMakeLists.txt | 4 +- src/win/graph/CMakeLists.txt | 1 - src/win/graph/direct/Brush.cpp | 17 - src/win/graph/direct/CMakeLists.txt | 29 -- src/win/graph/direct/Factory.cpp | 107 ----- src/win/graph/direct/Font.cpp | 31 -- src/win/graph/direct/Geometry.cpp | 62 --- src/win/graph/direct/Painter.cpp | 104 ----- src/win/graph/direct/Resource.cpp | 12 - src/win/graph/direct/TextLayout.cpp | 124 ------ src/win/graph/direct/WindowPainter.cpp | 20 - src/win/graph/direct/WindowRenderTarget.cpp | 81 ---- src/win/graphics/CMakeLists.txt | 1 + src/win/graphics/direct/Brush.cpp | 17 + src/win/graphics/direct/CMakeLists.txt | 29 ++ src/win/graphics/direct/Factory.cpp | 107 +++++ src/win/graphics/direct/Font.cpp | 31 ++ src/win/graphics/direct/Geometry.cpp | 62 +++ src/win/graphics/direct/Painter.cpp | 104 +++++ src/win/graphics/direct/Resource.cpp | 12 + src/win/graphics/direct/TextLayout.cpp | 124 ++++++ src/win/graphics/direct/WindowPainter.cpp | 20 + src/win/graphics/direct/WindowRenderTarget.cpp | 81 ++++ src/win/gui/CMakeLists.txt | 31 ++ src/win/gui/Cursor.cpp | 51 +++ src/win/gui/GodWindow.cpp | 63 +++ src/win/gui/InputMethod.cpp | 278 +++++++++++++ src/win/gui/Keyboard.cpp | 74 ++++ src/win/gui/TimerManager.cpp | 100 +++++ src/win/gui/TimerManager.hpp | 61 +++ src/win/gui/UiApplication.cpp | 118 ++++++ src/win/gui/Window.cpp | 453 +++++++++++++++++++++ src/win/gui/WindowClass.cpp | 28 ++ src/win/gui/WindowManager.cpp | 56 +++ src/win/gui/WindowManager.hpp | 51 +++ src/win/native/CMakeLists.txt | 31 -- src/win/native/Cursor.cpp | 51 --- src/win/native/GodWindow.cpp | 63 --- src/win/native/InputMethod.cpp | 278 ------------- src/win/native/Keyboard.cpp | 74 ---- src/win/native/TimerManager.cpp | 100 ----- src/win/native/TimerManager.hpp | 61 --- src/win/native/UiApplication.cpp | 118 ------ src/win/native/Window.cpp | 453 --------------------- src/win/native/WindowClass.cpp | 28 -- src/win/native/WindowManager.cpp | 56 --- src/win/native/WindowManager.hpp | 51 --- 162 files changed, 4031 insertions(+), 4031 deletions(-) delete mode 100644 include/cru/platform/graph/Base.hpp delete mode 100644 include/cru/platform/graph/Brush.hpp delete mode 100644 include/cru/platform/graph/Factory.hpp delete mode 100644 include/cru/platform/graph/Font.hpp delete mode 100644 include/cru/platform/graph/Geometry.hpp delete mode 100644 include/cru/platform/graph/Painter.hpp delete mode 100644 include/cru/platform/graph/Resource.hpp delete mode 100644 include/cru/platform/graph/TextLayout.hpp delete mode 100644 include/cru/platform/graph/util/Painter.hpp create mode 100644 include/cru/platform/graphics/Base.hpp create mode 100644 include/cru/platform/graphics/Brush.hpp create mode 100644 include/cru/platform/graphics/Factory.hpp create mode 100644 include/cru/platform/graphics/Font.hpp create mode 100644 include/cru/platform/graphics/Geometry.hpp create mode 100644 include/cru/platform/graphics/Painter.hpp create mode 100644 include/cru/platform/graphics/Resource.hpp create mode 100644 include/cru/platform/graphics/TextLayout.hpp create mode 100644 include/cru/platform/graphics/util/Painter.hpp create mode 100644 include/cru/platform/gui/Base.hpp create mode 100644 include/cru/platform/gui/Cursor.hpp create mode 100644 include/cru/platform/gui/InputMethod.hpp create mode 100644 include/cru/platform/gui/Keyboard.hpp create mode 100644 include/cru/platform/gui/UiApplication.hpp create mode 100644 include/cru/platform/gui/Window.hpp delete mode 100644 include/cru/platform/native/Base.hpp delete mode 100644 include/cru/platform/native/Cursor.hpp delete mode 100644 include/cru/platform/native/InputMethod.hpp delete mode 100644 include/cru/platform/native/Keyboard.hpp delete mode 100644 include/cru/platform/native/UiApplication.hpp delete mode 100644 include/cru/platform/native/Window.hpp delete mode 100644 include/cru/win/graph/direct/Brush.hpp delete mode 100644 include/cru/win/graph/direct/ComResource.hpp delete mode 100644 include/cru/win/graph/direct/ConvertUtil.hpp delete mode 100644 include/cru/win/graph/direct/Exception.hpp delete mode 100644 include/cru/win/graph/direct/Factory.hpp delete mode 100644 include/cru/win/graph/direct/Font.hpp delete mode 100644 include/cru/win/graph/direct/Geometry.hpp delete mode 100644 include/cru/win/graph/direct/Painter.hpp delete mode 100644 include/cru/win/graph/direct/Resource.hpp delete mode 100644 include/cru/win/graph/direct/TextLayout.hpp delete mode 100644 include/cru/win/graph/direct/WindowPainter.hpp delete mode 100644 include/cru/win/graph/direct/WindowRenderTarget.hpp create mode 100644 include/cru/win/graphics/direct/Brush.hpp create mode 100644 include/cru/win/graphics/direct/ComResource.hpp create mode 100644 include/cru/win/graphics/direct/ConvertUtil.hpp create mode 100644 include/cru/win/graphics/direct/Exception.hpp create mode 100644 include/cru/win/graphics/direct/Factory.hpp create mode 100644 include/cru/win/graphics/direct/Font.hpp create mode 100644 include/cru/win/graphics/direct/Geometry.hpp create mode 100644 include/cru/win/graphics/direct/Painter.hpp create mode 100644 include/cru/win/graphics/direct/Resource.hpp create mode 100644 include/cru/win/graphics/direct/TextLayout.hpp create mode 100644 include/cru/win/graphics/direct/WindowPainter.hpp create mode 100644 include/cru/win/graphics/direct/WindowRenderTarget.hpp create mode 100644 include/cru/win/gui/Base.hpp create mode 100644 include/cru/win/gui/Cursor.hpp create mode 100644 include/cru/win/gui/Exception.hpp create mode 100644 include/cru/win/gui/GodWindow.hpp create mode 100644 include/cru/win/gui/InputMethod.hpp create mode 100644 include/cru/win/gui/Keyboard.hpp create mode 100644 include/cru/win/gui/Resource.hpp create mode 100644 include/cru/win/gui/UiApplication.hpp create mode 100644 include/cru/win/gui/Window.hpp create mode 100644 include/cru/win/gui/WindowClass.hpp create mode 100644 include/cru/win/gui/WindowNativeMessageEventArgs.hpp delete mode 100644 include/cru/win/native/Base.hpp delete mode 100644 include/cru/win/native/Cursor.hpp delete mode 100644 include/cru/win/native/Exception.hpp delete mode 100644 include/cru/win/native/GodWindow.hpp delete mode 100644 include/cru/win/native/InputMethod.hpp delete mode 100644 include/cru/win/native/Keyboard.hpp delete mode 100644 include/cru/win/native/Resource.hpp delete mode 100644 include/cru/win/native/UiApplication.hpp delete mode 100644 include/cru/win/native/Window.hpp delete mode 100644 include/cru/win/native/WindowClass.hpp delete mode 100644 include/cru/win/native/WindowNativeMessageEventArgs.hpp delete mode 100644 src/platform/graph/CMakeLists.txt create mode 100644 src/platform/graphics/CMakeLists.txt create mode 100644 src/platform/gui/CMakeLists.txt create mode 100644 src/platform/gui/Keyboard.cpp create mode 100644 src/platform/gui/UiApplication.cpp delete mode 100644 src/platform/native/CMakeLists.txt delete mode 100644 src/platform/native/Keyboard.cpp delete mode 100644 src/platform/native/UiApplication.cpp delete mode 100644 src/win/graph/CMakeLists.txt delete mode 100644 src/win/graph/direct/Brush.cpp delete mode 100644 src/win/graph/direct/CMakeLists.txt delete mode 100644 src/win/graph/direct/Factory.cpp delete mode 100644 src/win/graph/direct/Font.cpp delete mode 100644 src/win/graph/direct/Geometry.cpp delete mode 100644 src/win/graph/direct/Painter.cpp delete mode 100644 src/win/graph/direct/Resource.cpp delete mode 100644 src/win/graph/direct/TextLayout.cpp delete mode 100644 src/win/graph/direct/WindowPainter.cpp delete mode 100644 src/win/graph/direct/WindowRenderTarget.cpp create mode 100644 src/win/graphics/CMakeLists.txt create mode 100644 src/win/graphics/direct/Brush.cpp create mode 100644 src/win/graphics/direct/CMakeLists.txt create mode 100644 src/win/graphics/direct/Factory.cpp create mode 100644 src/win/graphics/direct/Font.cpp create mode 100644 src/win/graphics/direct/Geometry.cpp create mode 100644 src/win/graphics/direct/Painter.cpp create mode 100644 src/win/graphics/direct/Resource.cpp create mode 100644 src/win/graphics/direct/TextLayout.cpp create mode 100644 src/win/graphics/direct/WindowPainter.cpp create mode 100644 src/win/graphics/direct/WindowRenderTarget.cpp create mode 100644 src/win/gui/CMakeLists.txt create mode 100644 src/win/gui/Cursor.cpp create mode 100644 src/win/gui/GodWindow.cpp create mode 100644 src/win/gui/InputMethod.cpp create mode 100644 src/win/gui/Keyboard.cpp create mode 100644 src/win/gui/TimerManager.cpp create mode 100644 src/win/gui/TimerManager.hpp create mode 100644 src/win/gui/UiApplication.cpp create mode 100644 src/win/gui/Window.cpp create mode 100644 src/win/gui/WindowClass.cpp create mode 100644 src/win/gui/WindowManager.cpp create mode 100644 src/win/gui/WindowManager.hpp delete mode 100644 src/win/native/CMakeLists.txt delete mode 100644 src/win/native/Cursor.cpp delete mode 100644 src/win/native/GodWindow.cpp delete mode 100644 src/win/native/InputMethod.cpp delete mode 100644 src/win/native/Keyboard.cpp delete mode 100644 src/win/native/TimerManager.cpp delete mode 100644 src/win/native/TimerManager.hpp delete mode 100644 src/win/native/UiApplication.cpp delete mode 100644 src/win/native/Window.cpp delete mode 100644 src/win/native/WindowClass.cpp delete mode 100644 src/win/native/WindowManager.cpp delete mode 100644 src/win/native/WindowManager.hpp (limited to 'src/ui/UiManager.cpp') diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt index 64612bf9..16159d08 100644 --- a/demos/CMakeLists.txt +++ b/demos/CMakeLists.txt @@ -1,7 +1,7 @@ add_library(cru_demo_base INTERFACE) if(WIN32) - target_link_libraries(cru_demo_base INTERFACE cru_win_native) + target_link_libraries(cru_demo_base INTERFACE cru_win_gui) endif() add_subdirectory(main) diff --git a/demos/input_method/main.cpp b/demos/input_method/main.cpp index 06098253..5a6dc942 100644 --- a/demos/input_method/main.cpp +++ b/demos/input_method/main.cpp @@ -1,14 +1,14 @@ -#include "cru/platform/graph/Factory.hpp" -#include "cru/platform/graph/Font.hpp" -#include "cru/platform/graph/Painter.hpp" -#include "cru/platform/native/InputMethod.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/platform/native/Window.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/graphics/Font.hpp" +#include "cru/platform/graphics/Painter.hpp" +#include "cru/platform/gui/InputMethod.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" int main() { using namespace cru::platform; - using namespace cru::platform::graph; - using namespace cru::platform::native; + using namespace cru::platform::graphics; + using namespace cru::platform::gui; auto application = CreateUiApplication(); diff --git a/demos/main/main.cpp b/demos/main/main.cpp index e973682e..6bd7fb4e 100644 --- a/demos/main/main.cpp +++ b/demos/main/main.cpp @@ -1,6 +1,6 @@ #include "cru/platform/HeapDebug.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/platform/native/Window.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" #include "cru/ui/Base.hpp" #include "cru/ui/Window.hpp" #include "cru/ui/WindowHost.hpp" @@ -9,7 +9,7 @@ #include "cru/ui/controls/TextBlock.hpp" #include "cru/ui/controls/TextBox.hpp" -using cru::platform::native::CreateUiApplication; +using cru::platform::gui::CreateUiApplication; using cru::ui::Window; using cru::ui::controls::Button; using cru::ui::controls::FlexLayout; diff --git a/include/cru/platform/graph/Base.hpp b/include/cru/platform/graph/Base.hpp deleted file mode 100644 index 61cfc5ef..00000000 --- a/include/cru/platform/graph/Base.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "../GraphBase.hpp" -#include "../Matrix.hpp" -#include "../Resource.hpp" - -#include - -namespace cru::platform::graph { -// forward declarations -struct IGraphFactory; -struct IBrush; -struct ISolidColorBrush; -struct IFont; -struct IGeometry; -struct IGeometryBuilder; -struct IPainter; -struct ITextLayout; - -struct TextHitTestResult { - int position; - bool trailing; - bool insideText; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Brush.hpp b/include/cru/platform/graph/Brush.hpp deleted file mode 100644 index e67384de..00000000 --- a/include/cru/platform/graph/Brush.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { -struct IBrush : virtual IGraphResource {}; - -struct ISolidColorBrush : virtual IBrush { - virtual Color GetColor() = 0; - virtual void SetColor(const Color& color) = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Factory.hpp b/include/cru/platform/graph/Factory.hpp deleted file mode 100644 index b4e68f12..00000000 --- a/include/cru/platform/graph/Factory.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include "Brush.hpp" -#include "Font.hpp" -#include "Geometry.hpp" -#include "TextLayout.hpp" - -#include -#include - -namespace cru::platform::graph { -// Entry point of the graph module. -struct IGraphFactory : virtual INativeResource { - virtual std::unique_ptr CreateSolidColorBrush() = 0; - - virtual std::unique_ptr CreateGeometryBuilder() = 0; - - virtual std::unique_ptr CreateFont(std::u16string font_family, - float font_size) = 0; - - virtual std::unique_ptr CreateTextLayout( - std::shared_ptr font, std::u16string text) = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Font.hpp b/include/cru/platform/graph/Font.hpp deleted file mode 100644 index 182cc15b..00000000 --- a/include/cru/platform/graph/Font.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { -struct IFont : virtual IGraphResource { - virtual float GetFontSize() = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Geometry.hpp b/include/cru/platform/graph/Geometry.hpp deleted file mode 100644 index 354efd97..00000000 --- a/include/cru/platform/graph/Geometry.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { -struct IGeometry : virtual IGraphResource { - virtual bool FillContains(const Point& point) = 0; -}; - -// After called Build, calling every method will throw a - -struct IGeometryBuilder : virtual IGraphResource { - virtual void BeginFigure(const Point& point) = 0; - virtual void LineTo(const Point& point) = 0; - virtual void QuadraticBezierTo(const Point& control_point, - const Point& end_point) = 0; - virtual void CloseFigure(bool close) = 0; - - virtual std::unique_ptr Build() = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Painter.hpp b/include/cru/platform/graph/Painter.hpp deleted file mode 100644 index 27ae420b..00000000 --- a/include/cru/platform/graph/Painter.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { - -struct IPainter : virtual INativeResource { - virtual Matrix GetTransform() = 0; - virtual void SetTransform(const Matrix& matrix) = 0; - - virtual void Clear(const Color& color) = 0; - - virtual void StrokeRectangle(const Rect& rectangle, IBrush* brush, - float width) = 0; - virtual void FillRectangle(const Rect& rectangle, IBrush* brush) = 0; - - virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush, - float width) = 0; - virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0; - - virtual void DrawText(const Point& offset, ITextLayout* text_layout, - IBrush* brush) = 0; - - virtual void PushLayer(const Rect& bounds) = 0; - - virtual void PopLayer() = 0; - - virtual void EndDraw() = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Resource.hpp b/include/cru/platform/graph/Resource.hpp deleted file mode 100644 index 8859360c..00000000 --- a/include/cru/platform/graph/Resource.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include "Base.hpp" - -namespace cru::platform::graph { -struct IGraphFactory; - -struct IGraphResource : virtual INativeResource { - virtual IGraphFactory* GetGraphFactory() = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/TextLayout.hpp b/include/cru/platform/graph/TextLayout.hpp deleted file mode 100644 index a101983f..00000000 --- a/include/cru/platform/graph/TextLayout.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include -#include - -namespace cru::platform::graph { -struct ITextLayout : virtual IGraphResource { - virtual std::u16string GetText() = 0; - virtual std::u16string_view GetTextView() = 0; - virtual void SetText(std::u16string new_text) = 0; - - virtual std::shared_ptr GetFont() = 0; - virtual void SetFont(std::shared_ptr font) = 0; - - virtual void SetMaxWidth(float max_width) = 0; - virtual void SetMaxHeight(float max_height) = 0; - - virtual Rect GetTextBounds() = 0; - virtual std::vector TextRangeRect(const TextRange& text_range) = 0; - virtual Point TextSinglePoint(Index position, bool trailing) = 0; - virtual TextHitTestResult HitTest(const Point& point) = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/util/Painter.hpp b/include/cru/platform/graph/util/Painter.hpp deleted file mode 100644 index f9aec027..00000000 --- a/include/cru/platform/graph/util/Painter.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "../Painter.hpp" - -#include -#include - -namespace cru::platform::graph::util { -template -void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) { - static_assert(std::is_invocable_v, - "Action must can be be invoked with painter."); - const auto old = painter->GetTransform(); - painter->SetTransform(old * matrix); - action(painter); - painter->SetTransform(old); -} -} // namespace cru::platform::graph::util diff --git a/include/cru/platform/graphics/Base.hpp b/include/cru/platform/graphics/Base.hpp new file mode 100644 index 00000000..e751ebdb --- /dev/null +++ b/include/cru/platform/graphics/Base.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "../GraphBase.hpp" +#include "../Matrix.hpp" +#include "../Resource.hpp" + +#include + +namespace cru::platform::graphics { +// forward declarations +struct IGraphFactory; +struct IBrush; +struct ISolidColorBrush; +struct IFont; +struct IGeometry; +struct IGeometryBuilder; +struct IPainter; +struct ITextLayout; + +struct TextHitTestResult { + int position; + bool trailing; + bool insideText; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Brush.hpp b/include/cru/platform/graphics/Brush.hpp new file mode 100644 index 00000000..10c666b5 --- /dev/null +++ b/include/cru/platform/graphics/Brush.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { +struct IBrush : virtual IGraphResource {}; + +struct ISolidColorBrush : virtual IBrush { + virtual Color GetColor() = 0; + virtual void SetColor(const Color& color) = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Factory.hpp b/include/cru/platform/graphics/Factory.hpp new file mode 100644 index 00000000..d1b37783 --- /dev/null +++ b/include/cru/platform/graphics/Factory.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "Resource.hpp" + +#include "Brush.hpp" +#include "Font.hpp" +#include "Geometry.hpp" +#include "TextLayout.hpp" + +#include +#include + +namespace cru::platform::graphics { +// Entry point of the graph module. +struct IGraphFactory : virtual INativeResource { + virtual std::unique_ptr CreateSolidColorBrush() = 0; + + virtual std::unique_ptr CreateGeometryBuilder() = 0; + + virtual std::unique_ptr CreateFont(std::u16string font_family, + float font_size) = 0; + + virtual std::unique_ptr CreateTextLayout( + std::shared_ptr font, std::u16string text) = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Font.hpp b/include/cru/platform/graphics/Font.hpp new file mode 100644 index 00000000..70392a69 --- /dev/null +++ b/include/cru/platform/graphics/Font.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { +struct IFont : virtual IGraphResource { + virtual float GetFontSize() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Geometry.hpp b/include/cru/platform/graphics/Geometry.hpp new file mode 100644 index 00000000..b0ce6ad9 --- /dev/null +++ b/include/cru/platform/graphics/Geometry.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { +struct IGeometry : virtual IGraphResource { + virtual bool FillContains(const Point& point) = 0; +}; + +// After called Build, calling every method will throw a + +struct IGeometryBuilder : virtual IGraphResource { + virtual void BeginFigure(const Point& point) = 0; + virtual void LineTo(const Point& point) = 0; + virtual void QuadraticBezierTo(const Point& control_point, + const Point& end_point) = 0; + virtual void CloseFigure(bool close) = 0; + + virtual std::unique_ptr Build() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Painter.hpp b/include/cru/platform/graphics/Painter.hpp new file mode 100644 index 00000000..76140c32 --- /dev/null +++ b/include/cru/platform/graphics/Painter.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { + +struct IPainter : virtual INativeResource { + virtual Matrix GetTransform() = 0; + virtual void SetTransform(const Matrix& matrix) = 0; + + virtual void Clear(const Color& color) = 0; + + virtual void StrokeRectangle(const Rect& rectangle, IBrush* brush, + float width) = 0; + virtual void FillRectangle(const Rect& rectangle, IBrush* brush) = 0; + + virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush, + float width) = 0; + virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0; + + virtual void DrawText(const Point& offset, ITextLayout* text_layout, + IBrush* brush) = 0; + + virtual void PushLayer(const Rect& bounds) = 0; + + virtual void PopLayer() = 0; + + virtual void EndDraw() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Resource.hpp b/include/cru/platform/graphics/Resource.hpp new file mode 100644 index 00000000..a1625ce4 --- /dev/null +++ b/include/cru/platform/graphics/Resource.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "Base.hpp" + +namespace cru::platform::graphics { +struct IGraphFactory; + +struct IGraphResource : virtual INativeResource { + virtual IGraphFactory* GetGraphFactory() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/TextLayout.hpp b/include/cru/platform/graphics/TextLayout.hpp new file mode 100644 index 00000000..efd017d6 --- /dev/null +++ b/include/cru/platform/graphics/TextLayout.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Resource.hpp" + +#include +#include + +namespace cru::platform::graphics { +struct ITextLayout : virtual IGraphResource { + virtual std::u16string GetText() = 0; + virtual std::u16string_view GetTextView() = 0; + virtual void SetText(std::u16string new_text) = 0; + + virtual std::shared_ptr GetFont() = 0; + virtual void SetFont(std::shared_ptr font) = 0; + + virtual void SetMaxWidth(float max_width) = 0; + virtual void SetMaxHeight(float max_height) = 0; + + virtual Rect GetTextBounds() = 0; + virtual std::vector TextRangeRect(const TextRange& text_range) = 0; + virtual Point TextSinglePoint(Index position, bool trailing) = 0; + virtual TextHitTestResult HitTest(const Point& point) = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/util/Painter.hpp b/include/cru/platform/graphics/util/Painter.hpp new file mode 100644 index 00000000..af3a1997 --- /dev/null +++ b/include/cru/platform/graphics/util/Painter.hpp @@ -0,0 +1,17 @@ +#pragma once +#include "../Painter.hpp" + +#include +#include + +namespace cru::platform::graphics::util { +template +void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) { + static_assert(std::is_invocable_v, + "Action must can be be invoked with painter."); + const auto old = painter->GetTransform(); + painter->SetTransform(old * matrix); + action(painter); + painter->SetTransform(old); +} +} // namespace cru::platform::graphics::util diff --git a/include/cru/platform/gui/Base.hpp b/include/cru/platform/gui/Base.hpp new file mode 100644 index 00000000..fb196f02 --- /dev/null +++ b/include/cru/platform/gui/Base.hpp @@ -0,0 +1,47 @@ +#pragma once +#include "Keyboard.hpp" +#include "cru/common/Base.hpp" +#include "cru/common/Bitmask.hpp" +#include "cru/platform/graphics/Base.hpp" + +#include "../Resource.hpp" + +namespace cru::platform::gui { +struct ICursor; +struct ICursorManager; +struct IUiApplication; +struct INativeWindow; +struct IInputMethodContext; + +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 + +enum class SystemCursorType { + Arrow, + Hand, +}; + +struct NativeMouseButtonEventArgs { + MouseButton button; + Point point; + KeyModifier modifier; +}; + +struct NativeKeyEventArgs { + KeyCode key; + KeyModifier modifier; +}; + +enum class FocusChangeType { Gain, Lost }; + +enum class MouseEnterLeaveType { Enter, Leave }; +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Cursor.hpp b/include/cru/platform/gui/Cursor.hpp new file mode 100644 index 00000000..3f1679e4 --- /dev/null +++ b/include/cru/platform/gui/Cursor.hpp @@ -0,0 +1,14 @@ +#pragma once +#include "Base.hpp" + +#include + +namespace cru::platform::gui { +struct ICursor : virtual INativeResource {}; + +struct ICursorManager : virtual INativeResource { + 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/InputMethod.hpp b/include/cru/platform/gui/InputMethod.hpp new file mode 100644 index 00000000..53a8d671 --- /dev/null +++ b/include/cru/platform/gui/InputMethod.hpp @@ -0,0 +1,80 @@ +#pragma once +#include "Base.hpp" + +#include "cru/common/Event.hpp" + +#include +#include +#include + +namespace cru::platform::gui { +struct CompositionClause { + int start; + int end; + bool target; +}; + +using CompositionClauses = std::vector; + +struct CompositionText { + std::u16string text; + CompositionClauses clauses; + TextRange selection; +}; + +struct IInputMethodContext : virtual INativeResource { + // 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. 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 + +template <> +struct fmt::formatter + : fmt::formatter { + auto parse(fmt::basic_format_parse_context& ctx) { + return fmt::formatter::parse(ctx); + } + + template + auto format(const cru::platform::gui::CompositionText& ct, + FormatContext& ctx) { + auto output = ctx.out(); + output = format_to(output, u"text: {}\n", ct.text); + output = format_to(output, u"clauses:\n"); + for (gsl::index i = 0; i < static_cast(ct.clauses.size()); + i++) { + const auto& clause = ct.clauses[i]; + output = + format_to(output, u"\t{}. start: {} end: {}{}\n", i, clause.start, + clause.end, clause.target ? u" target" : u""); + } + output = format_to(output, u"selection: position: {} count: {}", + ct.selection.position, ct.selection.count); + return output; + } +}; diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp new file mode 100644 index 00000000..e12cccda --- /dev/null +++ b/include/cru/platform/gui/Keyboard.hpp @@ -0,0 +1,127 @@ +#pragma once +#include "cru/common/Bitmask.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 +}; + +namespace details { +struct TagKeyModifier {}; +} // namespace details + +using KeyModifier = Bitmask; + +struct KeyModifiers { + static constexpr KeyModifier shift{0b1}; + static constexpr KeyModifier ctrl{0b10}; + static constexpr KeyModifier alt{0b100}; +}; + +std::u16string_view ToString(KeyCode key_code); +std::u16string ToString(KeyModifier key_modifier, + std::u16string_view separator = u"+"); +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/UiApplication.hpp b/include/cru/platform/gui/UiApplication.hpp new file mode 100644 index 00000000..6a2eb067 --- /dev/null +++ b/include/cru/platform/gui/UiApplication.hpp @@ -0,0 +1,109 @@ +#pragma once +#include "Base.hpp" + +#include +#include +#include +#include + +namespace cru::platform::gui { +// 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 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 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(INativeWindow* parent) = 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) { + Reset(other.id_); + other.id_ = 0; + return *this; + } + + ~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; + } + + 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_; +}; + +// Bootstrap from this. +std::unique_ptr CreateUiApplication(); +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/Window.hpp b/include/cru/platform/gui/Window.hpp new file mode 100644 index 00000000..26d1a476 --- /dev/null +++ b/include/cru/platform/gui/Window.hpp @@ -0,0 +1,57 @@ +#pragma once +#include "Base.hpp" + +#include "cru/common/Event.hpp" + +#include + +namespace cru::platform::gui { +// Represents a native window, which exposes some low-level events and +// operations. +struct INativeWindow : virtual INativeResource { + virtual void Close() = 0; + + virtual INativeWindow* GetParent() = 0; + + virtual bool IsVisible() = 0; + virtual void SetVisible(bool is_visible) = 0; + + virtual Size GetClientSize() = 0; + virtual void SetClientSize(const Size& size) = 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; + + // 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 RequestRepaint() = 0; + + // Remember to call EndDraw on return value and destroy it. + virtual std::unique_ptr BeginPaint() = 0; + + // Don't use this instance after receive this event. + virtual IEvent* DestroyEvent() = 0; + virtual IEvent* PaintEvent() = 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* KeyDownEvent() = 0; + virtual IEvent* KeyUpEvent() = 0; + + virtual IInputMethodContext* GetInputMethodContext() = 0; +}; +} // namespace cru::platform::gui diff --git a/include/cru/platform/native/Base.hpp b/include/cru/platform/native/Base.hpp deleted file mode 100644 index c3e87439..00000000 --- a/include/cru/platform/native/Base.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include "Keyboard.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/Bitmask.hpp" -#include "cru/platform/graph/Base.hpp" - -#include "../Resource.hpp" - -namespace cru::platform::native { -struct ICursor; -struct ICursorManager; -struct IUiApplication; -struct INativeWindow; -struct IInputMethodContext; - -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 - -enum class SystemCursorType { - Arrow, - Hand, -}; - -struct NativeMouseButtonEventArgs { - MouseButton button; - Point point; - KeyModifier modifier; -}; - -struct NativeKeyEventArgs { - KeyCode key; - KeyModifier modifier; -}; - -enum class FocusChangeType { Gain, Lost }; - -enum class MouseEnterLeaveType { Enter, Leave }; -} // namespace cru::platform::native diff --git a/include/cru/platform/native/Cursor.hpp b/include/cru/platform/native/Cursor.hpp deleted file mode 100644 index 447cd694..00000000 --- a/include/cru/platform/native/Cursor.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include - -namespace cru::platform::native { -struct ICursor : virtual INativeResource {}; - -struct ICursorManager : virtual INativeResource { - virtual std::shared_ptr GetSystemCursor(SystemCursorType type) = 0; - - // TODO: Add method to create cursor. -}; -} // namespace cru::platform::native diff --git a/include/cru/platform/native/InputMethod.hpp b/include/cru/platform/native/InputMethod.hpp deleted file mode 100644 index de752417..00000000 --- a/include/cru/platform/native/InputMethod.hpp +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/Event.hpp" - -#include -#include -#include - -namespace cru::platform::native { -struct CompositionClause { - int start; - int end; - bool target; -}; - -using CompositionClauses = std::vector; - -struct CompositionText { - std::u16string text; - CompositionClauses clauses; - TextRange selection; -}; - -struct IInputMethodContext : virtual INativeResource { - // 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. 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::native - -template <> -struct fmt::formatter - : fmt::formatter { - auto parse(fmt::basic_format_parse_context& ctx) { - return fmt::formatter::parse(ctx); - } - - template - auto format(const cru::platform::native::CompositionText& ct, - FormatContext& ctx) { - auto output = ctx.out(); - output = format_to(output, u"text: {}\n", ct.text); - output = format_to(output, u"clauses:\n"); - for (gsl::index i = 0; i < static_cast(ct.clauses.size()); - i++) { - const auto& clause = ct.clauses[i]; - output = - format_to(output, u"\t{}. start: {} end: {}{}\n", i, clause.start, - clause.end, clause.target ? u" target" : u""); - } - output = format_to(output, u"selection: position: {} count: {}", - ct.selection.position, ct.selection.count); - return output; - } -}; diff --git a/include/cru/platform/native/Keyboard.hpp b/include/cru/platform/native/Keyboard.hpp deleted file mode 100644 index 67a35c8a..00000000 --- a/include/cru/platform/native/Keyboard.hpp +++ /dev/null @@ -1,127 +0,0 @@ -#pragma once -#include "cru/common/Bitmask.hpp" - -#include -#include - -namespace cru::platform::native { -// 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 -}; - -namespace details { -struct TagKeyModifier {}; -} // namespace details - -using KeyModifier = Bitmask; - -struct KeyModifiers { - static constexpr KeyModifier shift{0b1}; - static constexpr KeyModifier ctrl{0b10}; - static constexpr KeyModifier alt{0b100}; -}; - -std::u16string_view ToString(KeyCode key_code); -std::u16string ToString(KeyModifier key_modifier, - std::u16string_view separator = u"+"); -} // namespace cru::platform::native diff --git a/include/cru/platform/native/UiApplication.hpp b/include/cru/platform/native/UiApplication.hpp deleted file mode 100644 index 2b1b047a..00000000 --- a/include/cru/platform/native/UiApplication.hpp +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include -#include -#include -#include - -namespace cru::platform::native { -// 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 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 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(INativeWindow* parent) = 0; - - virtual cru::platform::graph::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) { - Reset(other.id_); - other.id_ = 0; - return *this; - } - - ~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; - } - - 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_; -}; - -// Bootstrap from this. -std::unique_ptr CreateUiApplication(); -} // namespace cru::platform::native diff --git a/include/cru/platform/native/Window.hpp b/include/cru/platform/native/Window.hpp deleted file mode 100644 index c8abdeac..00000000 --- a/include/cru/platform/native/Window.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/Event.hpp" - -#include - -namespace cru::platform::native { -// Represents a native window, which exposes some low-level events and -// operations. -struct INativeWindow : virtual INativeResource { - virtual void Close() = 0; - - virtual INativeWindow* GetParent() = 0; - - virtual bool IsVisible() = 0; - virtual void SetVisible(bool is_visible) = 0; - - virtual Size GetClientSize() = 0; - virtual void SetClientSize(const Size& size) = 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; - - // 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 RequestRepaint() = 0; - - // Remember to call EndDraw on return value and destroy it. - virtual std::unique_ptr BeginPaint() = 0; - - // Don't use this instance after receive this event. - virtual IEvent* DestroyEvent() = 0; - virtual IEvent* PaintEvent() = 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* KeyDownEvent() = 0; - virtual IEvent* KeyUpEvent() = 0; - - virtual IInputMethodContext* GetInputMethodContext() = 0; -}; -} // namespace cru::platform::native diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index dd93f187..36d0eb78 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -1,7 +1,7 @@ #pragma once #include "cru/common/Base.hpp" -#include "cru/platform/graph/Base.hpp" -#include "cru/platform/native/Base.hpp" +#include "cru/platform/graphics/Base.hpp" +#include "cru/platform/gui/Base.hpp" #include #include @@ -19,9 +19,9 @@ using cru::platform::RoundedRect; using cru::platform::Size; using cru::platform::TextRange; using cru::platform::Thickness; -using cru::platform::native::MouseButton; +using cru::platform::gui::MouseButton; -namespace mouse_buttons = cru::platform::native::mouse_buttons; +namespace mouse_buttons = cru::platform::gui::mouse_buttons; namespace colors = cru::platform::colors; @@ -83,27 +83,27 @@ inline bool operator!=(const CornerRadius& left, const CornerRadius& right) { } struct BorderStyle { - std::shared_ptr border_brush; + std::shared_ptr border_brush; Thickness border_thickness; CornerRadius border_radius; - std::shared_ptr foreground_brush; - std::shared_ptr background_brush; + std::shared_ptr foreground_brush; + std::shared_ptr background_brush; }; class CanvasPaintEventArgs { public: - CanvasPaintEventArgs(platform::graph::IPainter* painter, + CanvasPaintEventArgs(platform::graphics::IPainter* painter, const Size& paint_size) : painter_(painter), paint_size_(paint_size) {} CRU_DEFAULT_COPY(CanvasPaintEventArgs) CRU_DEFAULT_MOVE(CanvasPaintEventArgs) ~CanvasPaintEventArgs() = default; - platform::graph::IPainter* GetPainter() const { return painter_; } + platform::graphics::IPainter* GetPainter() const { return painter_; } Size GetPaintSize() const { return paint_size_; } private: - platform::graph::IPainter* painter_; + platform::graphics::IPainter* painter_; Size paint_size_; }; diff --git a/include/cru/ui/Control.hpp b/include/cru/ui/Control.hpp index 0021ad62..5f381965 100644 --- a/include/cru/ui/Control.hpp +++ b/include/cru/ui/Control.hpp @@ -58,13 +58,13 @@ class Control : public Object { // Cursor is inherited from parent recursively if not set. public: // null for not set - std::shared_ptr GetCursor(); + std::shared_ptr GetCursor(); // will not return nullptr - std::shared_ptr GetInheritedCursor(); + std::shared_ptr GetInheritedCursor(); // null to unset - void SetCursor(std::shared_ptr cursor); + void SetCursor(std::shared_ptr cursor); //*************** region: events *************** public: @@ -146,6 +146,6 @@ class Control : public Object { private: bool is_mouse_over_ = false; - std::shared_ptr cursor_ = nullptr; + std::shared_ptr cursor_ = nullptr; }; } // namespace cru::ui diff --git a/include/cru/ui/ShortcutHub.hpp b/include/cru/ui/ShortcutHub.hpp index 5382f63e..1145c661 100644 --- a/include/cru/ui/ShortcutHub.hpp +++ b/include/cru/ui/ShortcutHub.hpp @@ -3,7 +3,7 @@ #include "cru/common/Base.hpp" #include "cru/common/Event.hpp" -#include "cru/platform/native/Keyboard.hpp" +#include "cru/platform/gui/Keyboard.hpp" #include "cru/ui/UiEvent.hpp" #include @@ -20,8 +20,8 @@ namespace cru::ui { class ShortcutKeyBind { public: - ShortcutKeyBind(platform::native::KeyCode key, - platform::native::KeyModifier modifier) + ShortcutKeyBind(platform::gui::KeyCode key, + platform::gui::KeyModifier modifier) : key_(key), modifier_(modifier) {} CRU_DEFAULT_COPY(ShortcutKeyBind) @@ -29,11 +29,11 @@ class ShortcutKeyBind { ~ShortcutKeyBind() = default; - platform::native::KeyCode GetKey() const { return key_; } - platform::native::KeyModifier GetModifier() const { return modifier_; } + platform::gui::KeyCode GetKey() const { return key_; } + platform::gui::KeyModifier GetModifier() const { return modifier_; } - bool Is(platform::native::KeyCode key, - platform::native::KeyModifier modifier) const { + bool Is(platform::gui::KeyCode key, + platform::gui::KeyModifier modifier) const { return key == key_ && modifier == modifier_; } @@ -47,15 +47,15 @@ class ShortcutKeyBind { std::u16string ToString() { std::u16string result = u"("; - result += platform::native::ToString(modifier_); + result += platform::gui::ToString(modifier_); result += u")"; - result += platform::native::ToString(key_); + result += platform::gui::ToString(key_); return result; } private: - platform::native::KeyCode key_; - platform::native::KeyModifier modifier_; + platform::gui::KeyCode key_; + platform::gui::KeyModifier modifier_; }; } // namespace cru::ui diff --git a/include/cru/ui/UiEvent.hpp b/include/cru/ui/UiEvent.hpp index 5adace8a..c0b2a902 100644 --- a/include/cru/ui/UiEvent.hpp +++ b/include/cru/ui/UiEvent.hpp @@ -2,14 +2,14 @@ #include "Base.hpp" #include "cru/common/Event.hpp" -#include "cru/platform/native/Keyboard.hpp" +#include "cru/platform/gui/Keyboard.hpp" #include #include #include #include -namespace cru::platform::graph { +namespace cru::platform::graphics { struct IPainter; } @@ -94,13 +94,13 @@ class MouseButtonEventArgs : public MouseEventArgs { public: MouseButtonEventArgs(Object* sender, Object* original_sender, const Point& point, const MouseButton button, - platform::native::KeyModifier key_modifier) + platform::gui::KeyModifier key_modifier) : MouseEventArgs(sender, original_sender, point), button_(button), key_modifier_(key_modifier) {} MouseButtonEventArgs(Object* sender, Object* original_sender, const MouseButton button, - platform::native::KeyModifier key_modifier) + platform::gui::KeyModifier key_modifier) : MouseEventArgs(sender, original_sender), button_(button), key_modifier_(key_modifier) {} @@ -111,11 +111,11 @@ class MouseButtonEventArgs : public MouseEventArgs { ~MouseButtonEventArgs() override = default; MouseButton GetButton() const { return button_; } - platform::native::KeyModifier GetKeyModifier() const { return key_modifier_; } + platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } private: MouseButton button_; - platform::native::KeyModifier key_modifier_; + platform::gui::KeyModifier key_modifier_; }; class MouseWheelEventArgs : public MouseEventArgs { @@ -138,7 +138,7 @@ class MouseWheelEventArgs : public MouseEventArgs { class PaintEventArgs : public UiEventArgs { public: PaintEventArgs(Object* sender, Object* original_sender, - platform::graph::IPainter* painter) + platform::graphics::IPainter* painter) : UiEventArgs(sender, original_sender), painter_(painter) {} PaintEventArgs(const PaintEventArgs& other) = default; PaintEventArgs(PaintEventArgs&& other) = default; @@ -146,10 +146,10 @@ class PaintEventArgs : public UiEventArgs { PaintEventArgs& operator=(PaintEventArgs&& other) = default; ~PaintEventArgs() = default; - platform::graph::IPainter* GetPainter() const { return painter_; } + platform::graphics::IPainter* GetPainter() const { return painter_; } private: - platform::graph::IPainter* painter_; + platform::graphics::IPainter* painter_; }; class FocusChangeEventArgs : public UiEventArgs { @@ -191,8 +191,8 @@ class ToggleEventArgs : public UiEventArgs { class KeyEventArgs : public UiEventArgs { public: KeyEventArgs(Object* sender, Object* original_sender, - platform::native::KeyCode key_code, - platform::native::KeyModifier key_modifier) + platform::gui::KeyCode key_code, + platform::gui::KeyModifier key_modifier) : UiEventArgs(sender, original_sender), key_code_(key_code), key_modifier_(key_modifier) {} @@ -202,12 +202,12 @@ class KeyEventArgs : public UiEventArgs { KeyEventArgs& operator=(KeyEventArgs&& other) = default; ~KeyEventArgs() override = default; - platform::native::KeyCode GetKeyCode() const { return key_code_; } - platform::native::KeyModifier GetKeyModifier() const { return key_modifier_; } + platform::gui::KeyCode GetKeyCode() const { return key_code_; } + platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } private: - platform::native::KeyCode key_code_; - platform::native::KeyModifier key_modifier_; + platform::gui::KeyCode key_code_; + platform::gui::KeyModifier key_modifier_; }; class CharEventArgs : public UiEventArgs { diff --git a/include/cru/ui/UiManager.hpp b/include/cru/ui/UiManager.hpp index 46f06ac2..64599d99 100644 --- a/include/cru/ui/UiManager.hpp +++ b/include/cru/ui/UiManager.hpp @@ -9,10 +9,10 @@ namespace cru::ui { struct ThemeResources { std::u16string default_font_family; - std::shared_ptr default_font; - std::shared_ptr text_brush; - std::shared_ptr text_selection_brush; - std::shared_ptr caret_brush; + std::shared_ptr default_font; + std::shared_ptr text_brush; + std::shared_ptr text_selection_brush; + std::shared_ptr caret_brush; controls::ButtonStyle button_style; controls::TextBoxBorderStyle text_box_border_style; }; diff --git a/include/cru/ui/WindowHost.hpp b/include/cru/ui/WindowHost.hpp index c3221dcf..97acf72e 100644 --- a/include/cru/ui/WindowHost.hpp +++ b/include/cru/ui/WindowHost.hpp @@ -2,8 +2,8 @@ #include "Base.hpp" #include "cru/common/Event.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/platform/native/Window.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" #include "render/Base.hpp" #include @@ -24,7 +24,7 @@ class WindowHost : public Object { ~WindowHost() override; public: - platform::native::INativeWindow* GetNativeWindow() { return native_window_; } + platform::gui::INativeWindow* GetNativeWindow() { return native_window_; } // Mark the layout as invalid, and arrange a re-layout later. // This method could be called more than one times in a message cycle. But @@ -87,30 +87,30 @@ class WindowHost : public Object { private: //*************** region: native messages *************** - void OnNativeDestroy(platform::native::INativeWindow* window, std::nullptr_t); - void OnNativePaint(platform::native::INativeWindow* window, std::nullptr_t); - void OnNativeResize(platform::native::INativeWindow* window, + void OnNativeDestroy(platform::gui::INativeWindow* window, std::nullptr_t); + void OnNativePaint(platform::gui::INativeWindow* window, std::nullptr_t); + void OnNativeResize(platform::gui::INativeWindow* window, const Size& size); - void OnNativeFocus(platform::native::INativeWindow* window, - cru::platform::native::FocusChangeType focus); + void OnNativeFocus(platform::gui::INativeWindow* window, + cru::platform::gui::FocusChangeType focus); void OnNativeMouseEnterLeave( - platform::native::INativeWindow* window, - cru::platform::native::MouseEnterLeaveType enter); - void OnNativeMouseMove(platform::native::INativeWindow* window, + platform::gui::INativeWindow* window, + cru::platform::gui::MouseEnterLeaveType enter); + void OnNativeMouseMove(platform::gui::INativeWindow* window, const Point& point); void OnNativeMouseDown( - platform::native::INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args); + platform::gui::INativeWindow* window, + const platform::gui::NativeMouseButtonEventArgs& args); void OnNativeMouseUp( - platform::native::INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args); + platform::gui::INativeWindow* window, + const platform::gui::NativeMouseButtonEventArgs& args); - void OnNativeKeyDown(platform::native::INativeWindow* window, - const platform::native::NativeKeyEventArgs& args); - void OnNativeKeyUp(platform::native::INativeWindow* window, - const platform::native::NativeKeyEventArgs& args); + void OnNativeKeyDown(platform::gui::INativeWindow* window, + const platform::gui::NativeKeyEventArgs& args); + void OnNativeKeyUp(platform::gui::INativeWindow* window, + const platform::gui::NativeKeyEventArgs& args); //*************** region: event dispatcher helper *************** @@ -123,10 +123,10 @@ class WindowHost : public Object { Control* root_control_ = nullptr; render::RenderObject* root_render_object_ = nullptr; - platform::native::INativeWindow* native_window_ = nullptr; + platform::gui::INativeWindow* native_window_ = nullptr; bool need_layout_ = false; - platform::native::TimerAutoCanceler relayout_timer_canceler_; + platform::gui::TimerAutoCanceler relayout_timer_canceler_; Event after_layout_event_; std::vector > after_layout_stable_action_; diff --git a/include/cru/ui/render/BorderRenderObject.hpp b/include/cru/ui/render/BorderRenderObject.hpp index 587f051a..f1b957cf 100644 --- a/include/cru/ui/render/BorderRenderObject.hpp +++ b/include/cru/ui/render/BorderRenderObject.hpp @@ -16,11 +16,11 @@ class BorderRenderObject : public RenderObject { bool IsBorderEnabled() const { return is_border_enabled_; } void SetBorderEnabled(bool enabled) { is_border_enabled_ = enabled; } - std::shared_ptr GetBorderBrush() { + std::shared_ptr GetBorderBrush() { return border_brush_; } - void SetBorderBrush(std::shared_ptr brush) { + void SetBorderBrush(std::shared_ptr brush) { if (brush == border_brush_) return; border_brush_ = std::move(brush); InvalidatePaint(); @@ -42,21 +42,21 @@ class BorderRenderObject : public RenderObject { RecreateGeometry(); } - std::shared_ptr GetForegroundBrush() { + std::shared_ptr GetForegroundBrush() { return foreground_brush_; } - void SetForegroundBrush(std::shared_ptr brush) { + void SetForegroundBrush(std::shared_ptr brush) { if (brush == foreground_brush_) return; foreground_brush_ = std::move(brush); InvalidatePaint(); } - std::shared_ptr GetBackgroundBrush() { + std::shared_ptr GetBackgroundBrush() { return background_brush_; } - void SetBackgroundBrush(std::shared_ptr brush) { + void SetBackgroundBrush(std::shared_ptr brush) { if (brush == background_brush_) return; background_brush_ = std::move(brush); InvalidatePaint(); @@ -67,7 +67,7 @@ class BorderRenderObject : public RenderObject { RenderObject* HitTest(const Point& point) override; protected: - void OnDrawCore(platform::graph::IPainter* painter) override; + void OnDrawCore(platform::graphics::IPainter* painter) override; Size OnMeasureCore(const MeasureRequirement& requirement, const MeasureSize& preferred_size) override; @@ -87,19 +87,19 @@ class BorderRenderObject : public RenderObject { private: bool is_border_enabled_ = false; - std::shared_ptr border_brush_; + std::shared_ptr border_brush_; Thickness border_thickness_; CornerRadius border_radius_; - std::shared_ptr foreground_brush_; - std::shared_ptr background_brush_; + std::shared_ptr foreground_brush_; + std::shared_ptr background_brush_; // The ring. Used for painting. - std::unique_ptr geometry_; + std::unique_ptr geometry_; // Area including inner area of the border. Used for painting foreground and // background. - std::unique_ptr border_inner_geometry_; + std::unique_ptr border_inner_geometry_; // Area including border ring and inner area. Used for hit test. - std::unique_ptr border_outer_geometry_; + std::unique_ptr border_outer_geometry_; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/CanvasRenderObject.hpp b/include/cru/ui/render/CanvasRenderObject.hpp index 3216f08c..58fee59c 100644 --- a/include/cru/ui/render/CanvasRenderObject.hpp +++ b/include/cru/ui/render/CanvasRenderObject.hpp @@ -22,7 +22,7 @@ class CanvasRenderObject : public RenderObject { IEvent* PaintEvent() { return &paint_event_; } protected: - void OnDrawContent(platform::graph::IPainter* painter) override; + void OnDrawContent(platform::graphics::IPainter* painter) override; Size OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) override; diff --git a/include/cru/ui/render/LayoutRenderObject.hpp b/include/cru/ui/render/LayoutRenderObject.hpp index b46ba0d0..732031a1 100644 --- a/include/cru/ui/render/LayoutRenderObject.hpp +++ b/include/cru/ui/render/LayoutRenderObject.hpp @@ -1,7 +1,7 @@ #pragma once #include "RenderObject.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/util/Painter.hpp" namespace cru::ui::render { template diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp index 20e095fa..436cf6b2 100644 --- a/include/cru/ui/render/RenderObject.hpp +++ b/include/cru/ui/render/RenderObject.hpp @@ -33,7 +33,7 @@ namespace cru::ui::render { // // To write a custom RenderObject, override following methods: // public: -// void Draw(platform::graph::IPainter* painter) override; +// void Draw(platform::graphics::IPainter* painter) override; // RenderObject* HitTest(const Point& point) override; // protected: // Size OnMeasureContent(const MeasureRequirement& requirement) override; @@ -129,7 +129,7 @@ class RenderObject : public Object { // This will set offset of this render object and call OnLayoutCore. void Layout(const Point& offset); - void Draw(platform::graph::IPainter* painter); + void Draw(platform::graphics::IPainter* painter); // Param point must be relative the lefttop of render object including margin. // Add offset before pass point to children. @@ -163,15 +163,15 @@ class RenderObject : public Object { virtual void OnRemoveChild(RenderObject* removed_child, Index position); // Draw all children with offset. - void DefaultDrawChildren(platform::graph::IPainter* painter); + void DefaultDrawChildren(platform::graphics::IPainter* painter); // Draw all children with translation of content rect lefttop. - void DefaultDrawContent(platform::graph::IPainter* painter); + void DefaultDrawContent(platform::graphics::IPainter* painter); // Call DefaultDrawContent. Then call DefaultDrawChildren. - virtual void OnDrawCore(platform::graph::IPainter* painter); + virtual void OnDrawCore(platform::graphics::IPainter* painter); - virtual void OnDrawContent(platform::graph::IPainter* painter); + virtual void OnDrawContent(platform::graphics::IPainter* painter); // Size measure including margin and padding. Please reduce margin and padding // or other custom things and pass the result content measure requirement and diff --git a/include/cru/ui/render/ScrollRenderObject.hpp b/include/cru/ui/render/ScrollRenderObject.hpp index 9b0cbf9a..3cc0e4c4 100644 --- a/include/cru/ui/render/ScrollRenderObject.hpp +++ b/include/cru/ui/render/ScrollRenderObject.hpp @@ -1,7 +1,7 @@ #pragma once #include "RenderObject.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/util/Painter.hpp" #include @@ -44,7 +44,7 @@ class ScrollRenderObject : public RenderObject { void ScrollToContain(const Rect& rect, const Thickness& margin = Thickness{}); protected: - void OnDrawCore(platform::graph::IPainter* painter) override; + void OnDrawCore(platform::graphics::IPainter* painter) override; // Logic: // If available size is bigger than child's preferred size, then child's diff --git a/include/cru/ui/render/TextRenderObject.hpp b/include/cru/ui/render/TextRenderObject.hpp index 3be42bbb..fa569c8c 100644 --- a/include/cru/ui/render/TextRenderObject.hpp +++ b/include/cru/ui/render/TextRenderObject.hpp @@ -24,10 +24,10 @@ class TextRenderObject : public RenderObject { constexpr static float default_caret_width = 2; public: - TextRenderObject(std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush, - std::shared_ptr caret_brush); + TextRenderObject(std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr selection_brush, + std::shared_ptr caret_brush); TextRenderObject(const TextRenderObject& other) = delete; TextRenderObject(TextRenderObject&& other) = delete; TextRenderObject& operator=(const TextRenderObject& other) = delete; @@ -38,25 +38,25 @@ class TextRenderObject : public RenderObject { std::u16string_view GetTextView() const; void SetText(std::u16string new_text); - std::shared_ptr GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr new_brush); + std::shared_ptr GetBrush() const { return brush_; } + void SetBrush(std::shared_ptr new_brush); - std::shared_ptr GetFont() const; - void SetFont(std::shared_ptr font); + std::shared_ptr GetFont() const; + void SetFont(std::shared_ptr font); std::vector TextRangeRect(const TextRange& text_range); Point TextSinglePoint(gsl::index position, bool trailing); - platform::graph::TextHitTestResult TextHitTest(const Point& point); + platform::graphics::TextHitTestResult TextHitTest(const Point& point); std::optional GetSelectionRange() const { return selection_range_; } void SetSelectionRange(std::optional new_range); - std::shared_ptr GetSelectionBrush() const { + std::shared_ptr GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(std::shared_ptr new_brush); + void SetSelectionBrush(std::shared_ptr new_brush); bool IsDrawCaret() const { return draw_caret_; } void SetDrawCaret(bool draw_caret); @@ -72,10 +72,10 @@ class TextRenderObject : public RenderObject { // Lefttop relative to render object lefttop. Rect GetCaretRect(); - std::shared_ptr GetCaretBrush() const { + std::shared_ptr GetCaretBrush() const { return caret_brush_; } - void GetCaretBrush(std::shared_ptr brush); + void GetCaretBrush(std::shared_ptr brush); float GetCaretWidth() const { return caret_width_; } void SetCaretWidth(float width); @@ -83,7 +83,7 @@ class TextRenderObject : public RenderObject { RenderObject* HitTest(const Point& point) override; protected: - void OnDrawContent(platform::graph::IPainter* painter) override; + void OnDrawContent(platform::graphics::IPainter* painter) override; // See remarks of this class. Size OnMeasureContent(const MeasureRequirement& requirement, @@ -93,16 +93,16 @@ class TextRenderObject : public RenderObject { void OnAfterLayout() override; private: - std::shared_ptr brush_; - std::shared_ptr font_; - std::unique_ptr text_layout_; + std::shared_ptr brush_; + std::shared_ptr font_; + std::unique_ptr text_layout_; std::optional selection_range_ = std::nullopt; - std::shared_ptr selection_brush_; + std::shared_ptr selection_brush_; bool draw_caret_ = false; gsl::index caret_position_ = 0; - std::shared_ptr caret_brush_; + std::shared_ptr caret_brush_; float caret_width_ = default_caret_width; }; } // namespace cru::ui::render diff --git a/include/cru/win/graph/direct/Brush.hpp b/include/cru/win/graph/direct/Brush.hpp deleted file mode 100644 index df1debe3..00000000 --- a/include/cru/win/graph/direct/Brush.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "ComResource.hpp" -#include "Resource.hpp" - -#include "cru/platform/graph/Brush.hpp" - -namespace cru::platform::graph::win::direct { -struct ID2DBrush : virtual IBrush { - virtual ID2D1Brush* GetD2DBrushInterface() const = 0; -}; - -class D2DSolidColorBrush : public DirectGraphResource, - public virtual ISolidColorBrush, - public virtual ID2DBrush, - public virtual IComResource { - public: - explicit D2DSolidColorBrush(DirectGraphFactory* factory); - - CRU_DELETE_COPY(D2DSolidColorBrush) - CRU_DELETE_MOVE(D2DSolidColorBrush) - - ~D2DSolidColorBrush() override = default; - - public: - Color GetColor() override { return color_; } - void SetColor(const Color& color) override; - - ID2D1Brush* GetD2DBrushInterface() const override { return brush_.Get(); } - - ID2D1SolidColorBrush* GetComInterface() const override { - return brush_.Get(); - } - - private: - Color color_ = colors::black; - - Microsoft::WRL::ComPtr brush_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/ComResource.hpp b/include/cru/win/graph/direct/ComResource.hpp deleted file mode 100644 index 2ac332cd..00000000 --- a/include/cru/win/graph/direct/ComResource.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "../../WinPreConfig.hpp" - -#include "cru/common/Base.hpp" - -namespace cru::platform::graph::win::direct { -template -struct IComResource : virtual Interface { - virtual TInterface* GetComInterface() const = 0; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/ConvertUtil.hpp b/include/cru/win/graph/direct/ConvertUtil.hpp deleted file mode 100644 index 12a04c7b..00000000 --- a/include/cru/win/graph/direct/ConvertUtil.hpp +++ /dev/null @@ -1,107 +0,0 @@ -#pragma once -#include "../../WinPreConfig.hpp" - -#include "cru/platform/graph/Base.hpp" - -namespace cru::platform::graph::win::direct { -inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { - D2D1_MATRIX_3X2_F m; - m._11 = matrix.m11; - m._12 = matrix.m12; - m._21 = matrix.m21; - m._22 = matrix.m22; - m._31 = matrix.m31; - m._32 = matrix.m32; - return m; -} - -inline D2D1_COLOR_F Convert(const Color& color) { - return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, - color.blue / 255.0f, color.alpha / 255.0f); -} - -inline D2D1_POINT_2F Convert(const Point& point) { - return D2D1::Point2F(point.x, point.y); -} - -inline D2D1_RECT_F Convert(const Rect& rect) { - return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, - rect.top + rect.height); -} - -inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { - return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, - rounded_rect.radius_y); -} - -inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { - return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, - ellipse.radius_y); -} - -inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { - return platform::Matrix{matrix._11, matrix._12, matrix._21, - matrix._22, matrix._31, matrix._32}; -} - -inline Color Convert(const D2D1_COLOR_F& color) { - auto floor = [](float n) { return static_cast(n + 0.5f); }; - return Color{floor(color.r * 255.0f), floor(color.g * 255.0f), - floor(color.b * 255.0f), floor(color.a * 255.0f)}; -} - -inline Point Convert(const D2D1_POINT_2F& point) { - return Point(point.x, point.y); -} - -inline Rect Convert(const D2D1_RECT_F& rect) { - return Rect(rect.left, rect.top, rect.right - rect.left, - rect.bottom - rect.top); -} - -inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { - return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, - rounded_rect.radiusY); -} - -inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { - return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); -} - -inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { - return left.x == right.x && left.y == right.y; -} - -inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { - return left.left == right.left && left.top == right.top && - left.right == right.right && left.bottom == right.bottom; -} - -inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_ROUNDED_RECT& left, - const D2D1_ROUNDED_RECT& right) { - return left.rect == right.rect && left.radiusX == right.radiusX && - left.radiusY == right.radiusY; -} - -inline bool operator!=(const D2D1_ROUNDED_RECT& left, - const D2D1_ROUNDED_RECT& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { - return left.point == right.point && left.radiusX == right.radiusX && - left.radiusY == right.radiusY; -} - -inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { - return !(left == right); -} -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/Exception.hpp b/include/cru/win/graph/direct/Exception.hpp deleted file mode 100644 index 8b62e8fa..00000000 --- a/include/cru/win/graph/direct/Exception.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include "../../Exception.hpp" - -namespace cru::platform::graph::win::direct { -using platform::win::HResultError; -using platform::win::ThrowIfFailed; -} // namespace cru::platform::graph::win::direct \ No newline at end of file diff --git a/include/cru/win/graph/direct/Factory.hpp b/include/cru/win/graph/direct/Factory.hpp deleted file mode 100644 index e70454f5..00000000 --- a/include/cru/win/graph/direct/Factory.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include "cru/platform/graph/Factory.hpp" - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory : public DirectResource, public virtual IGraphFactory { - public: - DirectGraphFactory(); - - CRU_DELETE_COPY(DirectGraphFactory) - CRU_DELETE_MOVE(DirectGraphFactory) - - ~DirectGraphFactory() override; - - public: - ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); } - ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } - ID2D1Device* GetD2D1Device() const { return d2d1_device_.Get(); } - IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); } - IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); } - IDWriteFontCollection* GetSystemFontCollection() const { - return dwrite_system_font_collection_.Get(); - } - - public: - Microsoft::WRL::ComPtr CreateD2D1DeviceContext(); - - // This context should only be used to create graphic resources like brush. - // Because graphic resources can be shared if they are created in the same - // device. - ID2D1DeviceContext* GetDefaultD2D1DeviceContext() { - return d2d1_device_context_.Get(); - } - - public: - std::unique_ptr CreateSolidColorBrush() override; - - std::unique_ptr CreateGeometryBuilder() override; - - std::unique_ptr CreateFont(std::u16string font_family, - float font_size) override; - - std::unique_ptr CreateTextLayout(std::shared_ptr font, - std::u16string text) override; - - private: - Microsoft::WRL::ComPtr d3d11_device_; - // ID2D1Factory1 is a interface only available in Windows 8 and Windows 7 with - // update. It is d2d v1.1. - Microsoft::WRL::ComPtr d2d1_factory_; - Microsoft::WRL::ComPtr d2d1_device_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_factory_; - Microsoft::WRL::ComPtr dwrite_factory_; - Microsoft::WRL::ComPtr dwrite_system_font_collection_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/Font.hpp b/include/cru/win/graph/direct/Font.hpp deleted file mode 100644 index 2195f3e4..00000000 --- a/include/cru/win/graph/direct/Font.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once -#include "ComResource.hpp" -#include "Resource.hpp" - -#include "cru/platform/graph/Font.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class DWriteFont : public DirectGraphResource, - public virtual IFont, - public virtual IComResource { - public: - DWriteFont(DirectGraphFactory* factory, std::u16string font_family, - float font_size); - - CRU_DELETE_COPY(DWriteFont) - CRU_DELETE_MOVE(DWriteFont) - - ~DWriteFont() override = default; - - public: - IDWriteTextFormat* GetComInterface() const override { - return text_format_.Get(); - } - - float GetFontSize() override; - - private: - std::u16string font_family_; - Microsoft::WRL::ComPtr text_format_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/Geometry.hpp b/include/cru/win/graph/direct/Geometry.hpp deleted file mode 100644 index 87987d3e..00000000 --- a/include/cru/win/graph/direct/Geometry.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once -#include "ComResource.hpp" -#include "Resource.hpp" - -#include "cru/platform/graph/Geometry.hpp" - -namespace cru::platform::graph::win::direct { -class D2DGeometryBuilder : public DirectGraphResource, - public virtual IGeometryBuilder { - public: - explicit D2DGeometryBuilder(DirectGraphFactory* factory); - - CRU_DELETE_COPY(D2DGeometryBuilder) - CRU_DELETE_MOVE(D2DGeometryBuilder) - - ~D2DGeometryBuilder() override = default; - - public: - void BeginFigure(const Point& point) override; - void LineTo(const Point& point) override; - void QuadraticBezierTo(const Point& control_point, - const Point& end_point) override; - void CloseFigure(bool close) override; - - std::unique_ptr Build() override; - - private: - bool IsValid() { return geometry_ != nullptr; } - void CheckValidation(); - - private: - Microsoft::WRL::ComPtr geometry_; - Microsoft::WRL::ComPtr geometry_sink_; -}; - -class D2DGeometry : public DirectGraphResource, - public virtual IGeometry, - public IComResource { - public: - D2DGeometry(DirectGraphFactory* factory, - Microsoft::WRL::ComPtr geometry); - - CRU_DELETE_COPY(D2DGeometry) - CRU_DELETE_MOVE(D2DGeometry) - - ~D2DGeometry() override = default; - - public: - ID2D1Geometry* GetComInterface() const override { return geometry_.Get(); } - - public: - bool FillContains(const Point& point) override; - - private: - Microsoft::WRL::ComPtr geometry_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/Painter.hpp b/include/cru/win/graph/direct/Painter.hpp deleted file mode 100644 index a50f962d..00000000 --- a/include/cru/win/graph/direct/Painter.hpp +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once -#include "ComResource.hpp" -#include "Resource.hpp" - -#include "cru/platform/graph/Painter.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class D2DPainter : public DirectResource, - public virtual IPainter, - public virtual IComResource { - public: - explicit D2DPainter(ID2D1RenderTarget* render_target); - - CRU_DELETE_COPY(D2DPainter) - CRU_DELETE_MOVE(D2DPainter) - - ~D2DPainter() override = default; - - public: - ID2D1RenderTarget* GetComInterface() const override { return render_target_; } - - public: - Matrix GetTransform() override; - void SetTransform(const platform::Matrix& matrix) override; - - void Clear(const Color& color) override; - - void StrokeRectangle(const Rect& rectangle, IBrush* brush, - float width) override; - void FillRectangle(const Rect& rectangle, IBrush* brush) override; - - void StrokeGeometry(IGeometry* geometry, IBrush* brush, float width) override; - void FillGeometry(IGeometry* geometry, IBrush* brush) override; - - void DrawText(const Point& offset, ITextLayout* text_layout, - IBrush* brush) override; - - void PushLayer(const Rect& bounds) override; - - void PopLayer() override; - - void EndDraw() override final; - - protected: - virtual void DoEndDraw() = 0; - - private: - bool IsValid() { return is_drawing_; } - void CheckValidation(); - - private: - ID2D1RenderTarget* render_target_; - - std::vector> layers_; - - bool is_drawing_ = true; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/Resource.hpp b/include/cru/win/graph/direct/Resource.hpp deleted file mode 100644 index 6162ebd8..00000000 --- a/include/cru/win/graph/direct/Resource.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "../../WinPreConfig.hpp" - -#include "cru/platform/graph/Resource.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory; - -class DirectResource : public Object, public virtual INativeResource { - public: - static constexpr std::u16string_view k_platform_id = u"Windows Direct"; - - protected: - DirectResource() = default; - - public: - CRU_DELETE_COPY(DirectResource) - CRU_DELETE_MOVE(DirectResource) - - ~DirectResource() override = default; - - public: - std::u16string_view GetPlatformId() const final { return k_platform_id; } -}; - -class DirectGraphResource : public DirectResource, - public virtual IGraphResource { - protected: - // Param factory can't be null. - explicit DirectGraphResource(DirectGraphFactory* factory); - - public: - CRU_DELETE_COPY(DirectGraphResource) - CRU_DELETE_MOVE(DirectGraphResource) - - ~DirectGraphResource() override = default; - - public: - IGraphFactory* GetGraphFactory() final; - - public: - DirectGraphFactory* GetDirectFactory() const { return factory_; } - - private: - DirectGraphFactory* factory_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/TextLayout.hpp b/include/cru/win/graph/direct/TextLayout.hpp deleted file mode 100644 index 016009ab..00000000 --- a/include/cru/win/graph/direct/TextLayout.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once -#include "ComResource.hpp" -#include "Resource.hpp" - -#include "cru/platform/graph/TextLayout.hpp" - -#include -#include - -namespace cru::platform::graph::win::direct { -class DWriteFont; - -class DWriteTextLayout : public DirectGraphResource, - public virtual ITextLayout, - public virtual IComResource { - public: - DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr font, - std::u16string text); - - CRU_DELETE_COPY(DWriteTextLayout) - CRU_DELETE_MOVE(DWriteTextLayout) - - ~DWriteTextLayout() override; - - public: - IDWriteTextLayout* GetComInterface() const override { - return text_layout_.Get(); - } - - public: - std::u16string GetText() override; - std::u16string_view GetTextView() override; - void SetText(std::u16string new_text) override; - - std::shared_ptr GetFont() override; - void SetFont(std::shared_ptr font) override; - - void SetMaxWidth(float max_width) override; - void SetMaxHeight(float max_height) override; - - Rect GetTextBounds() override; - // Return empty vector if text_range.count is 0. Text range could be in - // reverse direction, it should be normalized first in implementation. - std::vector TextRangeRect(const TextRange& text_range) override; - Point TextSinglePoint(Index position, bool trailing) override; - TextHitTestResult HitTest(const Point& point) override; - - private: - std::u16string text_; - std::shared_ptr font_; - float max_width_ = std::numeric_limits::max(); - float max_height_ = std::numeric_limits::max(); - Microsoft::WRL::ComPtr text_layout_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/WindowPainter.hpp b/include/cru/win/graph/direct/WindowPainter.hpp deleted file mode 100644 index 53961586..00000000 --- a/include/cru/win/graph/direct/WindowPainter.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "Painter.hpp" -#include "WindowRenderTarget.hpp" - -namespace cru::platform::graph::win::direct { -class D2DWindowPainter : public graph::win::direct::D2DPainter { - public: - explicit D2DWindowPainter(D2DWindowRenderTarget* window); - - CRU_DELETE_COPY(D2DWindowPainter) - CRU_DELETE_MOVE(D2DWindowPainter) - - ~D2DWindowPainter() override; - - protected: - void DoEndDraw() override; - - private: - D2DWindowRenderTarget* render_target_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/WindowRenderTarget.hpp b/include/cru/win/graph/direct/WindowRenderTarget.hpp deleted file mode 100644 index c9ee098f..00000000 --- a/include/cru/win/graph/direct/WindowRenderTarget.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "Factory.hpp" - -namespace cru::platform::graph::win::direct { -// Represents a window render target. -class D2DWindowRenderTarget : public Object { - public: - D2DWindowRenderTarget(gsl::not_null factory, HWND hwnd); - - CRU_DELETE_COPY(D2DWindowRenderTarget) - CRU_DELETE_MOVE(D2DWindowRenderTarget) - - ~D2DWindowRenderTarget() override = default; - - public: - graph::win::direct::DirectGraphFactory* GetDirectFactory() const { - return factory_; - } - - ID2D1DeviceContext* GetD2D1DeviceContext() { - return d2d1_device_context_.Get(); - } - - void SetDpi(float x, float y); - - // Resize the underlying buffer. - void ResizeBuffer(int width, int height); - - // Present the data of the underlying buffer to the window. - void Present(); - - private: - void CreateTargetBitmap(); - - private: - DirectGraphFactory* factory_; - HWND hwnd_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_swap_chain_; - Microsoft::WRL::ComPtr target_bitmap_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graphics/direct/Brush.hpp b/include/cru/win/graphics/direct/Brush.hpp new file mode 100644 index 00000000..fbff83b5 --- /dev/null +++ b/include/cru/win/graphics/direct/Brush.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Brush.hpp" + +namespace cru::platform::graphics::win::direct { +struct ID2DBrush : virtual IBrush { + virtual ID2D1Brush* GetD2DBrushInterface() const = 0; +}; + +class D2DSolidColorBrush : public DirectGraphResource, + public virtual ISolidColorBrush, + public virtual ID2DBrush, + public virtual IComResource { + public: + explicit D2DSolidColorBrush(DirectGraphFactory* factory); + + CRU_DELETE_COPY(D2DSolidColorBrush) + CRU_DELETE_MOVE(D2DSolidColorBrush) + + ~D2DSolidColorBrush() override = default; + + public: + Color GetColor() override { return color_; } + void SetColor(const Color& color) override; + + ID2D1Brush* GetD2DBrushInterface() const override { return brush_.Get(); } + + ID2D1SolidColorBrush* GetComInterface() const override { + return brush_.Get(); + } + + private: + Color color_ = colors::black; + + Microsoft::WRL::ComPtr brush_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/ComResource.hpp b/include/cru/win/graphics/direct/ComResource.hpp new file mode 100644 index 00000000..34ea39ed --- /dev/null +++ b/include/cru/win/graphics/direct/ComResource.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::graphics::win::direct { +template +struct IComResource : virtual Interface { + virtual TInterface* GetComInterface() const = 0; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/ConvertUtil.hpp b/include/cru/win/graphics/direct/ConvertUtil.hpp new file mode 100644 index 00000000..0d8da8a1 --- /dev/null +++ b/include/cru/win/graphics/direct/ConvertUtil.hpp @@ -0,0 +1,107 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/platform/graphics/Base.hpp" + +namespace cru::platform::graphics::win::direct { +inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { + D2D1_MATRIX_3X2_F m; + m._11 = matrix.m11; + m._12 = matrix.m12; + m._21 = matrix.m21; + m._22 = matrix.m22; + m._31 = matrix.m31; + m._32 = matrix.m32; + return m; +} + +inline D2D1_COLOR_F Convert(const Color& color) { + return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, + color.blue / 255.0f, color.alpha / 255.0f); +} + +inline D2D1_POINT_2F Convert(const Point& point) { + return D2D1::Point2F(point.x, point.y); +} + +inline D2D1_RECT_F Convert(const Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { + return platform::Matrix{matrix._11, matrix._12, matrix._21, + matrix._22, matrix._31, matrix._32}; +} + +inline Color Convert(const D2D1_COLOR_F& color) { + auto floor = [](float n) { return static_cast(n + 0.5f); }; + return Color{floor(color.r * 255.0f), floor(color.g * 255.0f), + floor(color.b * 255.0f), floor(color.a * 255.0f)}; +} + +inline Point Convert(const D2D1_POINT_2F& point) { + return Point(point.x, point.y); +} + +inline Rect Convert(const D2D1_RECT_F& rect) { + return Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); +} + +inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return left.x == right.x && left.y == right.y; +} + +inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return left.rect == right.rect && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return left.point == right.point && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return !(left == right); +} +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Exception.hpp b/include/cru/win/graphics/direct/Exception.hpp new file mode 100644 index 00000000..72493f2f --- /dev/null +++ b/include/cru/win/graphics/direct/Exception.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "../../Exception.hpp" + +namespace cru::platform::graphics::win::direct { +using platform::win::HResultError; +using platform::win::ThrowIfFailed; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Factory.hpp b/include/cru/win/graphics/direct/Factory.hpp new file mode 100644 index 00000000..70f3ede1 --- /dev/null +++ b/include/cru/win/graphics/direct/Factory.hpp @@ -0,0 +1,58 @@ +#pragma once +#include "Resource.hpp" + +#include "cru/platform/graphics/Factory.hpp" + +namespace cru::platform::graphics::win::direct { +class DirectGraphFactory : public DirectResource, public virtual IGraphFactory { + public: + DirectGraphFactory(); + + CRU_DELETE_COPY(DirectGraphFactory) + CRU_DELETE_MOVE(DirectGraphFactory) + + ~DirectGraphFactory() override; + + public: + ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); } + ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } + ID2D1Device* GetD2D1Device() const { return d2d1_device_.Get(); } + IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); } + IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); } + IDWriteFontCollection* GetSystemFontCollection() const { + return dwrite_system_font_collection_.Get(); + } + + public: + Microsoft::WRL::ComPtr CreateD2D1DeviceContext(); + + // This context should only be used to create graphic resources like brush. + // Because graphic resources can be shared if they are created in the same + // device. + ID2D1DeviceContext* GetDefaultD2D1DeviceContext() { + return d2d1_device_context_.Get(); + } + + public: + std::unique_ptr CreateSolidColorBrush() override; + + std::unique_ptr CreateGeometryBuilder() override; + + std::unique_ptr CreateFont(std::u16string font_family, + float font_size) override; + + std::unique_ptr CreateTextLayout(std::shared_ptr font, + std::u16string text) override; + + private: + Microsoft::WRL::ComPtr d3d11_device_; + // ID2D1Factory1 is a interface only available in Windows 8 and Windows 7 with + // update. It is d2d v1.1. + Microsoft::WRL::ComPtr d2d1_factory_; + Microsoft::WRL::ComPtr d2d1_device_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_factory_; + Microsoft::WRL::ComPtr dwrite_factory_; + Microsoft::WRL::ComPtr dwrite_system_font_collection_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Font.hpp b/include/cru/win/graphics/direct/Font.hpp new file mode 100644 index 00000000..fd3921a3 --- /dev/null +++ b/include/cru/win/graphics/direct/Font.hpp @@ -0,0 +1,33 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Font.hpp" + +#include + +namespace cru::platform::graphics::win::direct { +class DWriteFont : public DirectGraphResource, + public virtual IFont, + public virtual IComResource { + public: + DWriteFont(DirectGraphFactory* factory, std::u16string font_family, + float font_size); + + CRU_DELETE_COPY(DWriteFont) + CRU_DELETE_MOVE(DWriteFont) + + ~DWriteFont() override = default; + + public: + IDWriteTextFormat* GetComInterface() const override { + return text_format_.Get(); + } + + float GetFontSize() override; + + private: + std::u16string font_family_; + Microsoft::WRL::ComPtr text_format_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Geometry.hpp b/include/cru/win/graphics/direct/Geometry.hpp new file mode 100644 index 00000000..edfec590 --- /dev/null +++ b/include/cru/win/graphics/direct/Geometry.hpp @@ -0,0 +1,57 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Geometry.hpp" + +namespace cru::platform::graphics::win::direct { +class D2DGeometryBuilder : public DirectGraphResource, + public virtual IGeometryBuilder { + public: + explicit D2DGeometryBuilder(DirectGraphFactory* factory); + + CRU_DELETE_COPY(D2DGeometryBuilder) + CRU_DELETE_MOVE(D2DGeometryBuilder) + + ~D2DGeometryBuilder() override = default; + + public: + void BeginFigure(const Point& point) override; + void LineTo(const Point& point) override; + void QuadraticBezierTo(const Point& control_point, + const Point& end_point) override; + void CloseFigure(bool close) override; + + std::unique_ptr Build() override; + + private: + bool IsValid() { return geometry_ != nullptr; } + void CheckValidation(); + + private: + Microsoft::WRL::ComPtr geometry_; + Microsoft::WRL::ComPtr geometry_sink_; +}; + +class D2DGeometry : public DirectGraphResource, + public virtual IGeometry, + public IComResource { + public: + D2DGeometry(DirectGraphFactory* factory, + Microsoft::WRL::ComPtr geometry); + + CRU_DELETE_COPY(D2DGeometry) + CRU_DELETE_MOVE(D2DGeometry) + + ~D2DGeometry() override = default; + + public: + ID2D1Geometry* GetComInterface() const override { return geometry_.Get(); } + + public: + bool FillContains(const Point& point) override; + + private: + Microsoft::WRL::ComPtr geometry_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Painter.hpp b/include/cru/win/graphics/direct/Painter.hpp new file mode 100644 index 00000000..93c768e7 --- /dev/null +++ b/include/cru/win/graphics/direct/Painter.hpp @@ -0,0 +1,60 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Painter.hpp" + +#include + +namespace cru::platform::graphics::win::direct { +class D2DPainter : public DirectResource, + public virtual IPainter, + public virtual IComResource { + public: + explicit D2DPainter(ID2D1RenderTarget* render_target); + + CRU_DELETE_COPY(D2DPainter) + CRU_DELETE_MOVE(D2DPainter) + + ~D2DPainter() override = default; + + public: + ID2D1RenderTarget* GetComInterface() const override { return render_target_; } + + public: + Matrix GetTransform() override; + void SetTransform(const platform::Matrix& matrix) override; + + void Clear(const Color& color) override; + + void StrokeRectangle(const Rect& rectangle, IBrush* brush, + float width) override; + void FillRectangle(const Rect& rectangle, IBrush* brush) override; + + void StrokeGeometry(IGeometry* geometry, IBrush* brush, float width) override; + void FillGeometry(IGeometry* geometry, IBrush* brush) override; + + void DrawText(const Point& offset, ITextLayout* text_layout, + IBrush* brush) override; + + void PushLayer(const Rect& bounds) override; + + void PopLayer() override; + + void EndDraw() override final; + + protected: + virtual void DoEndDraw() = 0; + + private: + bool IsValid() { return is_drawing_; } + void CheckValidation(); + + private: + ID2D1RenderTarget* render_target_; + + std::vector> layers_; + + bool is_drawing_ = true; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Resource.hpp b/include/cru/win/graphics/direct/Resource.hpp new file mode 100644 index 00000000..f60f373e --- /dev/null +++ b/include/cru/win/graphics/direct/Resource.hpp @@ -0,0 +1,49 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/platform/graphics/Resource.hpp" + +#include + +namespace cru::platform::graphics::win::direct { +class DirectGraphFactory; + +class DirectResource : public Object, public virtual INativeResource { + public: + static constexpr std::u16string_view k_platform_id = u"Windows Direct"; + + protected: + DirectResource() = default; + + public: + CRU_DELETE_COPY(DirectResource) + CRU_DELETE_MOVE(DirectResource) + + ~DirectResource() override = default; + + public: + std::u16string_view GetPlatformId() const final { return k_platform_id; } +}; + +class DirectGraphResource : public DirectResource, + public virtual IGraphResource { + protected: + // Param factory can't be null. + explicit DirectGraphResource(DirectGraphFactory* factory); + + public: + CRU_DELETE_COPY(DirectGraphResource) + CRU_DELETE_MOVE(DirectGraphResource) + + ~DirectGraphResource() override = default; + + public: + IGraphFactory* GetGraphFactory() final; + + public: + DirectGraphFactory* GetDirectFactory() const { return factory_; } + + private: + DirectGraphFactory* factory_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/TextLayout.hpp b/include/cru/win/graphics/direct/TextLayout.hpp new file mode 100644 index 00000000..3320431f --- /dev/null +++ b/include/cru/win/graphics/direct/TextLayout.hpp @@ -0,0 +1,55 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/TextLayout.hpp" + +#include +#include + +namespace cru::platform::graphics::win::direct { +class DWriteFont; + +class DWriteTextLayout : public DirectGraphResource, + public virtual ITextLayout, + public virtual IComResource { + public: + DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr font, + std::u16string text); + + CRU_DELETE_COPY(DWriteTextLayout) + CRU_DELETE_MOVE(DWriteTextLayout) + + ~DWriteTextLayout() override; + + public: + IDWriteTextLayout* GetComInterface() const override { + return text_layout_.Get(); + } + + public: + std::u16string GetText() override; + std::u16string_view GetTextView() override; + void SetText(std::u16string new_text) override; + + std::shared_ptr GetFont() override; + void SetFont(std::shared_ptr font) override; + + void SetMaxWidth(float max_width) override; + void SetMaxHeight(float max_height) override; + + Rect GetTextBounds() override; + // Return empty vector if text_range.count is 0. Text range could be in + // reverse direction, it should be normalized first in implementation. + std::vector TextRangeRect(const TextRange& text_range) override; + Point TextSinglePoint(Index position, bool trailing) override; + TextHitTestResult HitTest(const Point& point) override; + + private: + std::u16string text_; + std::shared_ptr font_; + float max_width_ = std::numeric_limits::max(); + float max_height_ = std::numeric_limits::max(); + Microsoft::WRL::ComPtr text_layout_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/WindowPainter.hpp b/include/cru/win/graphics/direct/WindowPainter.hpp new file mode 100644 index 00000000..b5faf7b5 --- /dev/null +++ b/include/cru/win/graphics/direct/WindowPainter.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "Painter.hpp" +#include "WindowRenderTarget.hpp" + +namespace cru::platform::graphics::win::direct { +class D2DWindowPainter : public graphics::win::direct::D2DPainter { + public: + explicit D2DWindowPainter(D2DWindowRenderTarget* window); + + CRU_DELETE_COPY(D2DWindowPainter) + CRU_DELETE_MOVE(D2DWindowPainter) + + ~D2DWindowPainter() override; + + protected: + void DoEndDraw() override; + + private: + D2DWindowRenderTarget* render_target_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/WindowRenderTarget.hpp b/include/cru/win/graphics/direct/WindowRenderTarget.hpp new file mode 100644 index 00000000..75b1bf20 --- /dev/null +++ b/include/cru/win/graphics/direct/WindowRenderTarget.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "Factory.hpp" + +namespace cru::platform::graphics::win::direct { +// Represents a window render target. +class D2DWindowRenderTarget : public Object { + public: + D2DWindowRenderTarget(gsl::not_null factory, HWND hwnd); + + CRU_DELETE_COPY(D2DWindowRenderTarget) + CRU_DELETE_MOVE(D2DWindowRenderTarget) + + ~D2DWindowRenderTarget() override = default; + + public: + graphics::win::direct::DirectGraphFactory* GetDirectFactory() const { + return factory_; + } + + ID2D1DeviceContext* GetD2D1DeviceContext() { + return d2d1_device_context_.Get(); + } + + void SetDpi(float x, float y); + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + DirectGraphFactory* factory_; + HWND hwnd_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_swap_chain_; + Microsoft::WRL::ComPtr target_bitmap_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/gui/Base.hpp b/include/cru/win/gui/Base.hpp new file mode 100644 index 00000000..00782663 --- /dev/null +++ b/include/cru/win/gui/Base.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::gui::win { +class GodWindow; +class TimerManager; +class WinCursor; +class WinCursorManager; +class WindowClass; +class WindowManager; +class WinNativeWindow; +class WinUiApplication; +class WinInputMethodContext; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/Cursor.hpp b/include/cru/win/gui/Cursor.hpp new file mode 100644 index 00000000..cd13ded7 --- /dev/null +++ b/include/cru/win/gui/Cursor.hpp @@ -0,0 +1,49 @@ +#pragma once +#include "Resource.hpp" + +#include "cru/platform/gui/Cursor.hpp" + +#include + +namespace cru::platform::gui::win { +class WinCursor : public WinNativeResource, public virtual ICursor { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::gui::win::WinCursor") + + public: + WinCursor(HCURSOR handle, bool auto_destroy); + + CRU_DELETE_COPY(WinCursor) + CRU_DELETE_MOVE(WinCursor) + + ~WinCursor() override; + + public: + HCURSOR GetHandle() const { return handle_; } + + private: + HCURSOR handle_; + bool auto_destroy_; +}; + +class WinCursorManager : public WinNativeResource, + public virtual ICursorManager { + public: + WinCursorManager(); + + CRU_DELETE_COPY(WinCursorManager) + CRU_DELETE_MOVE(WinCursorManager) + + ~WinCursorManager() override = default; + + public: + std::shared_ptr GetSystemWinCursor(SystemCursorType type); + + std::shared_ptr GetSystemCursor(SystemCursorType type) override { + return std::static_pointer_cast(GetSystemWinCursor(type)); + } + + private: + std::shared_ptr sys_arrow_; + std::shared_ptr sys_hand_; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/Exception.hpp b/include/cru/win/gui/Exception.hpp new file mode 100644 index 00000000..895e6c14 --- /dev/null +++ b/include/cru/win/gui/Exception.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "../Exception.hpp" + +namespace cru::platform::gui::win { +using platform::win::Win32Error; +using platform::win::HResultError; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/GodWindow.hpp b/include/cru/win/gui/GodWindow.hpp new file mode 100644 index 00000000..0343b159 --- /dev/null +++ b/include/cru/win/gui/GodWindow.hpp @@ -0,0 +1,38 @@ +#pragma once +#include "Base.hpp" + +#include "WindowNativeMessageEventArgs.hpp" +#include "cru/common/Event.hpp" + +#include + +namespace cru::platform::gui::win { +class GodWindow : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::gui::win::GodWindow") + + public: + explicit GodWindow(WinUiApplication* application); + + CRU_DELETE_COPY(GodWindow) + CRU_DELETE_MOVE(GodWindow) + + ~GodWindow() override; + + HWND GetHandle() const { return hwnd_; } + + bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + IEvent* MessageEvent() { + return &message_event_; + } + + private: + WinUiApplication* application_; + + std::unique_ptr god_window_class_; + HWND hwnd_; + + Event message_event_; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/InputMethod.hpp b/include/cru/win/gui/InputMethod.hpp new file mode 100644 index 00000000..51a007d8 --- /dev/null +++ b/include/cru/win/gui/InputMethod.hpp @@ -0,0 +1,87 @@ +// Some useful information can be found from chromium code: +// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.h +// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.cc + +#pragma once +#include "Resource.hpp" + +#include "WindowNativeMessageEventArgs.hpp" +#include "cru/platform/gui/InputMethod.hpp" + +#include + +namespace cru::platform::gui::win { +class AutoHIMC : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::gui::win::AutoHIMC") + + public: + explicit AutoHIMC(HWND hwnd); + + CRU_DELETE_COPY(AutoHIMC) + + AutoHIMC(AutoHIMC&& other); + AutoHIMC& operator=(AutoHIMC&& other); + + ~AutoHIMC() override; + + HWND GetHwnd() const { return hwnd_; } + + HIMC Get() const { return handle_; } + + private: + HWND hwnd_; + HIMC handle_; +}; + +class WinInputMethodContext : public WinNativeResource, + public virtual IInputMethodContext { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::gui::win::WinInputMethodContext") + + public: + WinInputMethodContext(gsl::not_null window); + + CRU_DELETE_COPY(WinInputMethodContext) + CRU_DELETE_MOVE(WinInputMethodContext) + + ~WinInputMethodContext() override; + + bool ShouldManuallyDrawCompositionText() override { return true; } + + void EnableIME() override; + + void DisableIME() override; + + void CompleteComposition() override; + + void CancelComposition() override; + + CompositionText GetCompositionText() override; + + void SetCandidateWindowPosition(const Point& point) override; + + IEvent* CompositionStartEvent() override; + + IEvent* CompositionEndEvent() override; + + IEvent* CompositionEvent() override; + + IEvent* TextEvent() override; + + private: + void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); + + std::u16string GetResultString(); + + AutoHIMC GetHIMC(); + + private: + WinNativeWindow* native_window_; + + EventRevokerListGuard event_guard_; + + Event composition_start_event_; + Event composition_end_event_; + Event composition_event_; + Event text_event_; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/Keyboard.hpp b/include/cru/win/gui/Keyboard.hpp new file mode 100644 index 00000000..5b98833c --- /dev/null +++ b/include/cru/win/gui/Keyboard.hpp @@ -0,0 +1,9 @@ +#pragma once +#include "Base.hpp" + +#include "cru/platform/gui/Keyboard.hpp" + +namespace cru::platform::gui::win { +KeyCode VirtualKeyToKeyCode(int virtual_key); +KeyModifier RetrieveKeyMofifier(); +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/Resource.hpp b/include/cru/win/gui/Resource.hpp new file mode 100644 index 00000000..1f6f0a4a --- /dev/null +++ b/include/cru/win/gui/Resource.hpp @@ -0,0 +1,23 @@ +#pragma once +#include "Base.hpp" + +#include "cru/platform/Resource.hpp" + +namespace cru::platform::gui::win { +class WinNativeResource : public Object, public virtual INativeResource { + public: + static constexpr std::u16string_view k_platform_id = u"Windows"; + + protected: + WinNativeResource() = default; + + public: + CRU_DELETE_COPY(WinNativeResource) + CRU_DELETE_MOVE(WinNativeResource) + + ~WinNativeResource() override = default; + + public: + std::u16string_view GetPlatformId() const final { return k_platform_id; } +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/UiApplication.hpp b/include/cru/win/gui/UiApplication.hpp new file mode 100644 index 00000000..0f733cd4 --- /dev/null +++ b/include/cru/win/gui/UiApplication.hpp @@ -0,0 +1,74 @@ +#pragma once +#include "Resource.hpp" + +#include "cru/platform/gui/Base.hpp" +#include "cru/platform/gui/UiApplication.hpp" + +#include + +namespace cru::platform::graphics::win::direct { +class DirectGraphFactory; +} + +namespace cru::platform::gui::win { +class WinUiApplication : public WinNativeResource, + public virtual IUiApplication { + public: + static WinUiApplication* GetInstance() { return instance; } + + private: + static WinUiApplication* instance; + + public: + WinUiApplication(); + + CRU_DELETE_COPY(WinUiApplication) + CRU_DELETE_MOVE(WinUiApplication) + + ~WinUiApplication() override; + + public: + int Run() override; + void RequestQuit(int quit_code) override; + + void AddOnQuitHandler(std::function handler) override; + + long long SetImmediate(std::function action) override; + long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function action) override; + long long SetInterval(std::chrono::milliseconds milliseconds, + std::function action) override; + void CancelTimer(long long id) override; + + std::vector GetAllWindow() override; + INativeWindow* CreateWindow(INativeWindow* parent) override; + + cru::platform::graphics::IGraphFactory* GetGraphFactory() override; + + cru::platform::graphics::win::direct::DirectGraphFactory* GetDirectFactory() { + return graph_factory_.get(); + } + + ICursorManager* GetCursorManager() override; + + HINSTANCE GetInstanceHandle() const { return instance_handle_; } + + GodWindow* GetGodWindow() const { return god_window_.get(); } + TimerManager* GetTimerManager() const { return timer_manager_.get(); } + WindowManager* GetWindowManager() const { return window_manager_.get(); } + + private: + HINSTANCE instance_handle_; + + std::unique_ptr + graph_factory_; + + std::unique_ptr god_window_; + std::unique_ptr timer_manager_; + std::unique_ptr window_manager_; + + std::unique_ptr cursor_manager_; + + std::vector> quit_handlers_; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/Window.hpp b/include/cru/win/gui/Window.hpp new file mode 100644 index 00000000..3ba9ef68 --- /dev/null +++ b/include/cru/win/gui/Window.hpp @@ -0,0 +1,178 @@ +#pragma once +#include "Resource.hpp" + +#include "WindowNativeMessageEventArgs.hpp" +#include "cru/platform/GraphBase.hpp" +#include "cru/platform/gui/Base.hpp" +#include "cru/platform/gui/Window.hpp" +#include "cru/win/graphics/direct/WindowRenderTarget.hpp" + +#include + +namespace cru::platform::gui::win { +class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::gui::win::WinNativeWindow") + + public: + WinNativeWindow(WinUiApplication* application, WindowClass* window_class, + DWORD window_style, WinNativeWindow* parent); + + CRU_DELETE_COPY(WinNativeWindow) + CRU_DELETE_MOVE(WinNativeWindow) + + ~WinNativeWindow() override; + + public: + void Close() override; + + WinNativeWindow* GetParent() override { return parent_window_; } + + bool IsVisible() override; + void SetVisible(bool is_visible) override; + + Size GetClientSize() override; + void SetClientSize(const Size& size) override; + + // Get the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + Rect GetWindowRect() override; + + // Set the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + void SetWindowRect(const Rect& rect) override; + + Point GetMousePosition() override; + + bool CaptureMouse() override; + bool ReleaseMouse() override; + + void RequestRepaint() override; + std::unique_ptr BeginPaint() override; + + void SetCursor(std::shared_ptr cursor) override; + + IEvent* DestroyEvent() override { return &destroy_event_; } + IEvent* PaintEvent() override { return &paint_event_; } + IEvent* ResizeEvent() override { return &resize_event_; } + IEvent* FocusEvent() override { return &focus_event_; } + IEvent* MouseEnterLeaveEvent() override { + return &mouse_enter_leave_event_; + } + IEvent* MouseMoveEvent() override { return &mouse_move_event_; } + IEvent* MouseDownEvent() + override { + return &mouse_down_event_; + } + IEvent* MouseUpEvent() + override { + return &mouse_up_event_; + } + IEvent* KeyDownEvent() override { + return &key_down_event_; + } + IEvent* KeyUpEvent() override { + return &key_up_event_; + } + + IEvent* NativeMessageEvent() { + return &native_message_event_; + } + + IInputMethodContext* GetInputMethodContext() override; + + // Get the handle of the window. Return null if window is invalid. + HWND GetWindowHandle() const { return hwnd_; } + + bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + graphics::win::direct::D2DWindowRenderTarget* GetWindowRenderTarget() const { + return window_render_target_.get(); + } + + //*************** region: dpi *************** + float GetDpi() const { return dpi_; } + + inline int DipToPixel(const float dip) { + return static_cast(dip * GetDpi() / 96.0f); + } + + inline POINT DipToPixel(const Point& dip_point) { + POINT result; + result.x = DipToPixel(dip_point.x); + result.y = DipToPixel(dip_point.y); + return result; + } + + inline float PixelToDip(const int pixel) { + return static_cast(pixel) * 96.0f / GetDpi(); + } + + inline Point PixelToDip(const POINT& pi_point) { + return Point(PixelToDip(pi_point.x), PixelToDip(pi_point.y)); + } + + private: + // Get the client rect in pixel. + RECT GetClientRectPixel(); + + //*************** region: native messages *************** + + void OnDestroyInternal(); + void OnPaintInternal(); + void OnResizeInternal(int new_width, int new_height); + + void OnSetFocusInternal(); + void OnKillFocusInternal(); + + void OnMouseMoveInternal(POINT point); + void OnMouseLeaveInternal(); + void OnMouseDownInternal(platform::gui::MouseButton button, POINT point); + void OnMouseUpInternal(platform::gui::MouseButton button, POINT point); + + void OnMouseWheelInternal(short delta, POINT point); + void OnKeyDownInternal(int virtual_code); + void OnKeyUpInternal(int virtual_code); + + void OnActivatedInternal(); + void OnDeactivatedInternal(); + + private: + WinUiApplication* application_; + + // when delete is called first, it set this to true to indicate + // destroy message handler not to double delete this instance; + // when destroy handler is called first (by user action or method + // Close), it set this to true to indicate delete not call Close + // again. + bool sync_flag_ = false; + + HWND hwnd_; + WinNativeWindow* parent_window_; + + float dpi_; + + bool has_focus_ = false; + bool is_mouse_in_ = false; + + std::unique_ptr + window_render_target_; + + std::shared_ptr cursor_; + + std::unique_ptr input_method_context_; + + Event destroy_event_; + Event paint_event_; + Event resize_event_; + Event focus_event_; + Event mouse_enter_leave_event_; + Event mouse_move_event_; + Event mouse_down_event_; + Event mouse_up_event_; + Event key_down_event_; + Event key_up_event_; + + Event native_message_event_; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/WindowClass.hpp b/include/cru/win/gui/WindowClass.hpp new file mode 100644 index 00000000..2c07b68f --- /dev/null +++ b/include/cru/win/gui/WindowClass.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Base.hpp" + +#include + +namespace cru::platform::gui::win { +class WindowClass : public Object { + public: + WindowClass(std::wstring name, WNDPROC window_proc, HINSTANCE h_instance); + + CRU_DELETE_COPY(WindowClass) + CRU_DELETE_MOVE(WindowClass) + + ~WindowClass() override = default; + + const wchar_t* GetName() const { return name_.c_str(); } + + ATOM GetAtom() const { return atom_; } + + private: + std::wstring name_; + ATOM atom_; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/gui/WindowNativeMessageEventArgs.hpp b/include/cru/win/gui/WindowNativeMessageEventArgs.hpp new file mode 100644 index 00000000..834ba3c2 --- /dev/null +++ b/include/cru/win/gui/WindowNativeMessageEventArgs.hpp @@ -0,0 +1,40 @@ +#pragma once +#include "../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::gui::win { +struct WindowNativeMessage { + HWND hwnd; + UINT msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public Object { + public: + WindowNativeMessageEventArgs(const WindowNativeMessage& message) + : message_(message) {} + CRU_DEFAULT_COPY(WindowNativeMessageEventArgs) + CRU_DEFAULT_MOVE(WindowNativeMessageEventArgs) + ~WindowNativeMessageEventArgs() override = default; + + const WindowNativeMessage& GetWindowMessage() const { return message_; } + + LRESULT GetResult() const { return result_; } + void SetResult(LRESULT result) { result_ = result; } + + bool IsHandled() const { return handled_; } + void SetHandled(bool handled) { handled_ = handled; } + + void HandleWithResult(LRESULT result) { + handled_ = true; + result_ = result; + } + + private: + WindowNativeMessage message_; + LRESULT result_; + bool handled_ = false; +}; +} // namespace cru::platform::gui::win diff --git a/include/cru/win/native/Base.hpp b/include/cru/win/native/Base.hpp deleted file mode 100644 index 881dd8b1..00000000 --- a/include/cru/win/native/Base.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "../WinPreConfig.hpp" - -#include "cru/common/Base.hpp" - -namespace cru::platform::native::win { -class GodWindow; -class TimerManager; -class WinCursor; -class WinCursorManager; -class WindowClass; -class WindowManager; -class WinNativeWindow; -class WinUiApplication; -class WinInputMethodContext; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/Cursor.hpp b/include/cru/win/native/Cursor.hpp deleted file mode 100644 index 373b9170..00000000 --- a/include/cru/win/native/Cursor.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include "cru/platform/native/Cursor.hpp" - -#include - -namespace cru::platform::native::win { -class WinCursor : public WinNativeResource, public virtual ICursor { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::WinCursor") - - public: - WinCursor(HCURSOR handle, bool auto_destroy); - - CRU_DELETE_COPY(WinCursor) - CRU_DELETE_MOVE(WinCursor) - - ~WinCursor() override; - - public: - HCURSOR GetHandle() const { return handle_; } - - private: - HCURSOR handle_; - bool auto_destroy_; -}; - -class WinCursorManager : public WinNativeResource, - public virtual ICursorManager { - public: - WinCursorManager(); - - CRU_DELETE_COPY(WinCursorManager) - CRU_DELETE_MOVE(WinCursorManager) - - ~WinCursorManager() override = default; - - public: - std::shared_ptr GetSystemWinCursor(SystemCursorType type); - - std::shared_ptr GetSystemCursor(SystemCursorType type) override { - return std::static_pointer_cast(GetSystemWinCursor(type)); - } - - private: - std::shared_ptr sys_arrow_; - std::shared_ptr sys_hand_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/Exception.hpp b/include/cru/win/native/Exception.hpp deleted file mode 100644 index 6a5265c1..00000000 --- a/include/cru/win/native/Exception.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include "../Exception.hpp" - -namespace cru::platform::native::win { -using platform::win::Win32Error; -using platform::win::HResultError; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/GodWindow.hpp b/include/cru/win/native/GodWindow.hpp deleted file mode 100644 index 93d1acad..00000000 --- a/include/cru/win/native/GodWindow.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "WindowNativeMessageEventArgs.hpp" -#include "cru/common/Event.hpp" - -#include - -namespace cru::platform::native::win { -class GodWindow : public Object { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::GodWindow") - - public: - explicit GodWindow(WinUiApplication* application); - - CRU_DELETE_COPY(GodWindow) - CRU_DELETE_MOVE(GodWindow) - - ~GodWindow() override; - - HWND GetHandle() const { return hwnd_; } - - bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, - LPARAM l_param, LRESULT* result); - - IEvent* MessageEvent() { - return &message_event_; - } - - private: - WinUiApplication* application_; - - std::unique_ptr god_window_class_; - HWND hwnd_; - - Event message_event_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/InputMethod.hpp b/include/cru/win/native/InputMethod.hpp deleted file mode 100644 index f3dc15c0..00000000 --- a/include/cru/win/native/InputMethod.hpp +++ /dev/null @@ -1,87 +0,0 @@ -// Some useful information can be found from chromium code: -// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.h -// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.cc - -#pragma once -#include "Resource.hpp" - -#include "WindowNativeMessageEventArgs.hpp" -#include "cru/platform/native/InputMethod.hpp" - -#include - -namespace cru::platform::native::win { -class AutoHIMC : public Object { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::AutoHIMC") - - public: - explicit AutoHIMC(HWND hwnd); - - CRU_DELETE_COPY(AutoHIMC) - - AutoHIMC(AutoHIMC&& other); - AutoHIMC& operator=(AutoHIMC&& other); - - ~AutoHIMC() override; - - HWND GetHwnd() const { return hwnd_; } - - HIMC Get() const { return handle_; } - - private: - HWND hwnd_; - HIMC handle_; -}; - -class WinInputMethodContext : public WinNativeResource, - public virtual IInputMethodContext { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::WinInputMethodContext") - - public: - WinInputMethodContext(gsl::not_null window); - - CRU_DELETE_COPY(WinInputMethodContext) - CRU_DELETE_MOVE(WinInputMethodContext) - - ~WinInputMethodContext() override; - - bool ShouldManuallyDrawCompositionText() override { return true; } - - void EnableIME() override; - - void DisableIME() override; - - void CompleteComposition() override; - - void CancelComposition() override; - - CompositionText GetCompositionText() override; - - void SetCandidateWindowPosition(const Point& point) override; - - IEvent* CompositionStartEvent() override; - - IEvent* CompositionEndEvent() override; - - IEvent* CompositionEvent() override; - - IEvent* TextEvent() override; - - private: - void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); - - std::u16string GetResultString(); - - AutoHIMC GetHIMC(); - - private: - WinNativeWindow* native_window_; - - EventRevokerListGuard event_guard_; - - Event composition_start_event_; - Event composition_end_event_; - Event composition_event_; - Event text_event_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/Keyboard.hpp b/include/cru/win/native/Keyboard.hpp deleted file mode 100644 index 790e0015..00000000 --- a/include/cru/win/native/Keyboard.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/platform/native/Keyboard.hpp" - -namespace cru::platform::native::win { -KeyCode VirtualKeyToKeyCode(int virtual_key); -KeyModifier RetrieveKeyMofifier(); -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/Resource.hpp b/include/cru/win/native/Resource.hpp deleted file mode 100644 index 0de0e1a8..00000000 --- a/include/cru/win/native/Resource.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/platform/Resource.hpp" - -namespace cru::platform::native::win { -class WinNativeResource : public Object, public virtual INativeResource { - public: - static constexpr std::u16string_view k_platform_id = u"Windows"; - - protected: - WinNativeResource() = default; - - public: - CRU_DELETE_COPY(WinNativeResource) - CRU_DELETE_MOVE(WinNativeResource) - - ~WinNativeResource() override = default; - - public: - std::u16string_view GetPlatformId() const final { return k_platform_id; } -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/UiApplication.hpp b/include/cru/win/native/UiApplication.hpp deleted file mode 100644 index 170be532..00000000 --- a/include/cru/win/native/UiApplication.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include "cru/platform/native/Base.hpp" -#include "cru/platform/native/UiApplication.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory; -} - -namespace cru::platform::native::win { -class WinUiApplication : public WinNativeResource, - public virtual IUiApplication { - public: - static WinUiApplication* GetInstance() { return instance; } - - private: - static WinUiApplication* instance; - - public: - WinUiApplication(); - - CRU_DELETE_COPY(WinUiApplication) - CRU_DELETE_MOVE(WinUiApplication) - - ~WinUiApplication() override; - - public: - int Run() override; - void RequestQuit(int quit_code) override; - - void AddOnQuitHandler(std::function handler) override; - - long long SetImmediate(std::function action) override; - long long SetTimeout(std::chrono::milliseconds milliseconds, - std::function action) override; - long long SetInterval(std::chrono::milliseconds milliseconds, - std::function action) override; - void CancelTimer(long long id) override; - - std::vector GetAllWindow() override; - INativeWindow* CreateWindow(INativeWindow* parent) override; - - cru::platform::graph::IGraphFactory* GetGraphFactory() override; - - cru::platform::graph::win::direct::DirectGraphFactory* GetDirectFactory() { - return graph_factory_.get(); - } - - ICursorManager* GetCursorManager() override; - - HINSTANCE GetInstanceHandle() const { return instance_handle_; } - - GodWindow* GetGodWindow() const { return god_window_.get(); } - TimerManager* GetTimerManager() const { return timer_manager_.get(); } - WindowManager* GetWindowManager() const { return window_manager_.get(); } - - private: - HINSTANCE instance_handle_; - - std::unique_ptr - graph_factory_; - - std::unique_ptr god_window_; - std::unique_ptr timer_manager_; - std::unique_ptr window_manager_; - - std::unique_ptr cursor_manager_; - - std::vector> quit_handlers_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/Window.hpp b/include/cru/win/native/Window.hpp deleted file mode 100644 index 6bf71601..00000000 --- a/include/cru/win/native/Window.hpp +++ /dev/null @@ -1,178 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include "WindowNativeMessageEventArgs.hpp" -#include "cru/platform/GraphBase.hpp" -#include "cru/platform/native/Base.hpp" -#include "cru/platform/native/Window.hpp" -#include "cru/win/graph/direct/WindowRenderTarget.hpp" - -#include - -namespace cru::platform::native::win { -class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::WinNativeWindow") - - public: - WinNativeWindow(WinUiApplication* application, WindowClass* window_class, - DWORD window_style, WinNativeWindow* parent); - - CRU_DELETE_COPY(WinNativeWindow) - CRU_DELETE_MOVE(WinNativeWindow) - - ~WinNativeWindow() override; - - public: - void Close() override; - - WinNativeWindow* GetParent() override { return parent_window_; } - - bool IsVisible() override; - void SetVisible(bool is_visible) override; - - Size GetClientSize() override; - void SetClientSize(const Size& size) override; - - // Get the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - Rect GetWindowRect() override; - - // Set the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - void SetWindowRect(const Rect& rect) override; - - Point GetMousePosition() override; - - bool CaptureMouse() override; - bool ReleaseMouse() override; - - void RequestRepaint() override; - std::unique_ptr BeginPaint() override; - - void SetCursor(std::shared_ptr cursor) override; - - IEvent* DestroyEvent() override { return &destroy_event_; } - IEvent* PaintEvent() override { return &paint_event_; } - IEvent* ResizeEvent() override { return &resize_event_; } - IEvent* FocusEvent() override { return &focus_event_; } - IEvent* MouseEnterLeaveEvent() override { - return &mouse_enter_leave_event_; - } - IEvent* MouseMoveEvent() override { return &mouse_move_event_; } - IEvent* MouseDownEvent() - override { - return &mouse_down_event_; - } - IEvent* MouseUpEvent() - override { - return &mouse_up_event_; - } - IEvent* KeyDownEvent() override { - return &key_down_event_; - } - IEvent* KeyUpEvent() override { - return &key_up_event_; - } - - IEvent* NativeMessageEvent() { - return &native_message_event_; - } - - IInputMethodContext* GetInputMethodContext() override; - - // Get the handle of the window. Return null if window is invalid. - HWND GetWindowHandle() const { return hwnd_; } - - bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, - LPARAM l_param, LRESULT* result); - - graph::win::direct::D2DWindowRenderTarget* GetWindowRenderTarget() const { - return window_render_target_.get(); - } - - //*************** region: dpi *************** - float GetDpi() const { return dpi_; } - - inline int DipToPixel(const float dip) { - return static_cast(dip * GetDpi() / 96.0f); - } - - inline POINT DipToPixel(const Point& dip_point) { - POINT result; - result.x = DipToPixel(dip_point.x); - result.y = DipToPixel(dip_point.y); - return result; - } - - inline float PixelToDip(const int pixel) { - return static_cast(pixel) * 96.0f / GetDpi(); - } - - inline Point PixelToDip(const POINT& pi_point) { - return Point(PixelToDip(pi_point.x), PixelToDip(pi_point.y)); - } - - private: - // Get the client rect in pixel. - RECT GetClientRectPixel(); - - //*************** region: native messages *************** - - void OnDestroyInternal(); - void OnPaintInternal(); - void OnResizeInternal(int new_width, int new_height); - - void OnSetFocusInternal(); - void OnKillFocusInternal(); - - void OnMouseMoveInternal(POINT point); - void OnMouseLeaveInternal(); - void OnMouseDownInternal(platform::native::MouseButton button, POINT point); - void OnMouseUpInternal(platform::native::MouseButton button, POINT point); - - void OnMouseWheelInternal(short delta, POINT point); - void OnKeyDownInternal(int virtual_code); - void OnKeyUpInternal(int virtual_code); - - void OnActivatedInternal(); - void OnDeactivatedInternal(); - - private: - WinUiApplication* application_; - - // when delete is called first, it set this to true to indicate - // destroy message handler not to double delete this instance; - // when destroy handler is called first (by user action or method - // Close), it set this to true to indicate delete not call Close - // again. - bool sync_flag_ = false; - - HWND hwnd_; - WinNativeWindow* parent_window_; - - float dpi_; - - bool has_focus_ = false; - bool is_mouse_in_ = false; - - std::unique_ptr - window_render_target_; - - std::shared_ptr cursor_; - - std::unique_ptr input_method_context_; - - Event destroy_event_; - Event paint_event_; - Event resize_event_; - Event focus_event_; - Event mouse_enter_leave_event_; - Event mouse_move_event_; - Event mouse_down_event_; - Event mouse_up_event_; - Event key_down_event_; - Event key_up_event_; - - Event native_message_event_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowClass.hpp b/include/cru/win/native/WindowClass.hpp deleted file mode 100644 index fdd55065..00000000 --- a/include/cru/win/native/WindowClass.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include - -namespace cru::platform::native::win { -class WindowClass : public Object { - public: - WindowClass(std::wstring name, WNDPROC window_proc, HINSTANCE h_instance); - - CRU_DELETE_COPY(WindowClass) - CRU_DELETE_MOVE(WindowClass) - - ~WindowClass() override = default; - - const wchar_t* GetName() const { return name_.c_str(); } - - ATOM GetAtom() const { return atom_; } - - private: - std::wstring name_; - ATOM atom_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowNativeMessageEventArgs.hpp b/include/cru/win/native/WindowNativeMessageEventArgs.hpp deleted file mode 100644 index 84a7a123..00000000 --- a/include/cru/win/native/WindowNativeMessageEventArgs.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "../WinPreConfig.hpp" - -#include "cru/common/Base.hpp" - -namespace cru::platform::native::win { -struct WindowNativeMessage { - HWND hwnd; - UINT msg; - WPARAM w_param; - LPARAM l_param; -}; - -class WindowNativeMessageEventArgs : public Object { - public: - WindowNativeMessageEventArgs(const WindowNativeMessage& message) - : message_(message) {} - CRU_DEFAULT_COPY(WindowNativeMessageEventArgs) - CRU_DEFAULT_MOVE(WindowNativeMessageEventArgs) - ~WindowNativeMessageEventArgs() override = default; - - const WindowNativeMessage& GetWindowMessage() const { return message_; } - - LRESULT GetResult() const { return result_; } - void SetResult(LRESULT result) { result_ = result; } - - bool IsHandled() const { return handled_; } - void SetHandled(bool handled) { handled_ = handled; } - - void HandleWithResult(LRESULT result) { - handled_ = true; - result_ = result; - } - - private: - WindowNativeMessage message_; - LRESULT result_; - bool handled_ = false; -}; -} // namespace cru::platform::native::win diff --git a/src/platform/CMakeLists.txt b/src/platform/CMakeLists.txt index 51253b56..623ec08f 100644 --- a/src/platform/CMakeLists.txt +++ b/src/platform/CMakeLists.txt @@ -10,5 +10,5 @@ target_sources(cru_platform_base INTERFACE ) target_link_libraries(cru_platform_base INTERFACE cru_base) -add_subdirectory(graph) -add_subdirectory(native) +add_subdirectory(graphics) +add_subdirectory(gui) diff --git a/src/platform/graph/CMakeLists.txt b/src/platform/graph/CMakeLists.txt deleted file mode 100644 index 3bf11e8d..00000000 --- a/src/platform/graph/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -set(CRU_PLATFORM_GRAPH_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/platform/graph) -add_library(cru_platform_graph INTERFACE) -target_sources(cru_platform_graph INTERFACE - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Base.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Brush.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Font.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Geometry.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Factory.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Resource.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/Painter.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/TextLayout.hpp - ${CRU_PLATFORM_GRAPH_INCLUDE_DIR}/util/Painter.hpp -) -target_link_libraries(cru_platform_graph INTERFACE cru_platform_base) diff --git a/src/platform/graphics/CMakeLists.txt b/src/platform/graphics/CMakeLists.txt new file mode 100644 index 00000000..5f841267 --- /dev/null +++ b/src/platform/graphics/CMakeLists.txt @@ -0,0 +1,14 @@ +set(CRU_PLATFORM_GRAPHICS_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/platform/graphics) +add_library(cru_platform_graphics INTERFACE) +target_sources(cru_platform_graphics INTERFACE + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Base.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Brush.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Font.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Geometry.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Factory.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Resource.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/Painter.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/TextLayout.hpp + ${CRU_PLATFORM_GRAPHICS_INCLUDE_DIR}/util/Painter.hpp +) +target_link_libraries(cru_platform_graphics INTERFACE cru_platform_base) diff --git a/src/platform/gui/CMakeLists.txt b/src/platform/gui/CMakeLists.txt new file mode 100644 index 00000000..aca7620c --- /dev/null +++ b/src/platform/gui/CMakeLists.txt @@ -0,0 +1,14 @@ +set(CRU_PLATFORM_GUI_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/platform/gui) +add_library(cru_platform_gui STATIC + Keyboard.cpp + UiApplication.cpp +) +target_sources(cru_platform_gui PUBLIC + ${CRU_PLATFORM_GUI_INCLUDE_DIR}/Base.hpp + ${CRU_PLATFORM_GUI_INCLUDE_DIR}/Cursor.hpp + ${CRU_PLATFORM_GUI_INCLUDE_DIR}/InputMethod.hpp + ${CRU_PLATFORM_GUI_INCLUDE_DIR}/Keyboard.hpp + ${CRU_PLATFORM_GUI_INCLUDE_DIR}/Window.hpp + ${CRU_PLATFORM_GUI_INCLUDE_DIR}/UiApplication.hpp +) +target_link_libraries(cru_platform_gui PUBLIC cru_platform_graphics) diff --git a/src/platform/gui/Keyboard.cpp b/src/platform/gui/Keyboard.cpp new file mode 100644 index 00000000..24880e00 --- /dev/null +++ b/src/platform/gui/Keyboard.cpp @@ -0,0 +1,142 @@ +#include "cru/platform/gui/Keyboard.hpp" + +#include +#include +#include + +namespace cru::platform::gui { +constexpr std::array(KeyCode::NumPad9) + 1> + key_code_string_list{u"Unknown", + u"LeftButton", + u"MiddleButton", + u"RightButton", + u"Escape", + u"F1", + u"F2", + u"F3", + u"F4", + u"F5", + u"F6", + u"F7", + u"F8", + u"F9", + u"F10", + u"F11", + u"F12", + u"N0", + u"N1", + u"N2", + u"N3", + u"N4", + u"N5", + u"N6", + u"N7", + u"N8", + u"N9", + u"A", + u"B", + u"C", + u"D", + u"E", + u"F", + u"G", + u"H", + u"I", + u"J", + u"K", + u"L", + u"M", + u"N", + u"O", + u"P", + u"Q", + u"R", + u"S", + u"T", + u"U", + u"V", + u"W", + u"X", + u"Y", + u"Z", + u"GraveAccent", + u"Tab", + u"CapsLock", + u"LeftShift", + u"LeftCtrl", + u"LeftSuper", + u"LeftAlt", + u"Minus", + u"Equal", + u"Backspace", + u"LeftSquareBracket", + u"RightSquareBracket", + u"BackSlash", + u"Semicolon", + u"Quote", + u"Comma", + u"Period", + u"Slash", + u"RightShift", + u"RightCtrl", + u"RightSuper", + u"RightAlt", + u"Insert", + u"Delete", + u"Home", + u"End", + u"PageUp", + u"PageDown", + u"Up", + u"Left", + u"Down", + u"Right", + u"PrintScreen", + u"ScrollLock", + u"Pause", + u"NumPad0", + u"NumPad1", + u"NumPad2", + u"NumPad3", + u"NumPad4", + u"NumPad5", + u"NumPad6", + u"NumPad7", + u"NumPad8", + u"NumPad9"}; + +std::u16string_view ToString(KeyCode key_code) { + if (static_cast(key_code) < 0 || + static_cast(key_code) >= + static_cast(key_code_string_list.size())) + return u"UNKNOWN_KEYCODENAME"; + + return key_code_string_list[static_cast(key_code)]; +} + +std::u16string ToString(KeyModifier key_modifier, + std::u16string_view separator) { + std::vector list; + if (key_modifier & KeyModifiers::shift) { + list.push_back(u"Shift"); + } + + if (key_modifier & KeyModifiers::ctrl) { + list.push_back(u"Ctrl"); + } + + if (key_modifier & KeyModifiers::alt) { + list.push_back(u"Shift"); + } + + if (list.empty()) return u""; + std::u16string result = list.front(); + for (auto iter = list.cbegin() + 1; iter != list.cend(); ++iter) { + result += separator; + result += *iter; + } + + return result; +} +} // namespace cru::platform::gui diff --git a/src/platform/gui/UiApplication.cpp b/src/platform/gui/UiApplication.cpp new file mode 100644 index 00000000..f095361e --- /dev/null +++ b/src/platform/gui/UiApplication.cpp @@ -0,0 +1,15 @@ +#include "cru/platform/gui/UiApplication.hpp" + +namespace cru::platform::gui { +IUiApplication* IUiApplication::instance = nullptr; + +IUiApplication::IUiApplication() { + if (instance) { + throw std::runtime_error("An ui application has already been created."); + } + + instance = this; +} + +IUiApplication::~IUiApplication() { instance = nullptr; } +} // namespace cru::platform::gui diff --git a/src/platform/native/CMakeLists.txt b/src/platform/native/CMakeLists.txt deleted file mode 100644 index 3fe4370a..00000000 --- a/src/platform/native/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -set(CRU_PLATFORM_NATIVE_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/platform/native) -add_library(cru_platform_native STATIC - Keyboard.cpp - UiApplication.cpp -) -target_sources(cru_platform_native PUBLIC - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/Base.hpp - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/Cursor.hpp - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/InputMethod.hpp - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/Keyboard.hpp - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/Window.hpp - ${CRU_PLATFORM_NATIVE_INCLUDE_DIR}/UiApplication.hpp -) -target_link_libraries(cru_platform_native PUBLIC cru_platform_graph) diff --git a/src/platform/native/Keyboard.cpp b/src/platform/native/Keyboard.cpp deleted file mode 100644 index dd0e8f23..00000000 --- a/src/platform/native/Keyboard.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include "cru/platform/native/Keyboard.hpp" - -#include -#include -#include - -namespace cru::platform::native { -constexpr std::array(KeyCode::NumPad9) + 1> - key_code_string_list{u"Unknown", - u"LeftButton", - u"MiddleButton", - u"RightButton", - u"Escape", - u"F1", - u"F2", - u"F3", - u"F4", - u"F5", - u"F6", - u"F7", - u"F8", - u"F9", - u"F10", - u"F11", - u"F12", - u"N0", - u"N1", - u"N2", - u"N3", - u"N4", - u"N5", - u"N6", - u"N7", - u"N8", - u"N9", - u"A", - u"B", - u"C", - u"D", - u"E", - u"F", - u"G", - u"H", - u"I", - u"J", - u"K", - u"L", - u"M", - u"N", - u"O", - u"P", - u"Q", - u"R", - u"S", - u"T", - u"U", - u"V", - u"W", - u"X", - u"Y", - u"Z", - u"GraveAccent", - u"Tab", - u"CapsLock", - u"LeftShift", - u"LeftCtrl", - u"LeftSuper", - u"LeftAlt", - u"Minus", - u"Equal", - u"Backspace", - u"LeftSquareBracket", - u"RightSquareBracket", - u"BackSlash", - u"Semicolon", - u"Quote", - u"Comma", - u"Period", - u"Slash", - u"RightShift", - u"RightCtrl", - u"RightSuper", - u"RightAlt", - u"Insert", - u"Delete", - u"Home", - u"End", - u"PageUp", - u"PageDown", - u"Up", - u"Left", - u"Down", - u"Right", - u"PrintScreen", - u"ScrollLock", - u"Pause", - u"NumPad0", - u"NumPad1", - u"NumPad2", - u"NumPad3", - u"NumPad4", - u"NumPad5", - u"NumPad6", - u"NumPad7", - u"NumPad8", - u"NumPad9"}; - -std::u16string_view ToString(KeyCode key_code) { - if (static_cast(key_code) < 0 || - static_cast(key_code) >= - static_cast(key_code_string_list.size())) - return u"UNKNOWN_KEYCODENAME"; - - return key_code_string_list[static_cast(key_code)]; -} - -std::u16string ToString(KeyModifier key_modifier, - std::u16string_view separator) { - std::vector list; - if (key_modifier & KeyModifiers::shift) { - list.push_back(u"Shift"); - } - - if (key_modifier & KeyModifiers::ctrl) { - list.push_back(u"Ctrl"); - } - - if (key_modifier & KeyModifiers::alt) { - list.push_back(u"Shift"); - } - - if (list.empty()) return u""; - std::u16string result = list.front(); - for (auto iter = list.cbegin() + 1; iter != list.cend(); ++iter) { - result += separator; - result += *iter; - } - - return result; -} -} // namespace cru::platform::native diff --git a/src/platform/native/UiApplication.cpp b/src/platform/native/UiApplication.cpp deleted file mode 100644 index 200b10e0..00000000 --- a/src/platform/native/UiApplication.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "cru/platform/native/UiApplication.hpp" - -namespace cru::platform::native { -IUiApplication* IUiApplication::instance = nullptr; - -IUiApplication::IUiApplication() { - if (instance) { - throw std::runtime_error("An ui application has already been created."); - } - - instance = this; -} - -IUiApplication::~IUiApplication() { instance = nullptr; } -} // namespace cru::platform::native diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 045fea24..45f64c88 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -63,4 +63,4 @@ target_sources(cru_ui PUBLIC ${CRU_UI_INCLUDE_DIR}/render/StackLayoutRenderObject.hpp ${CRU_UI_INCLUDE_DIR}/render/TextRenderObject.hpp ) -target_link_libraries(cru_ui PUBLIC cru_platform_native) +target_link_libraries(cru_ui PUBLIC cru_platform_gui) diff --git a/src/ui/Control.cpp b/src/ui/Control.cpp index 13b1c780..cae48c66 100644 --- a/src/ui/Control.cpp +++ b/src/ui/Control.cpp @@ -2,8 +2,8 @@ #include "RoutedEventDispatch.hpp" #include "cru/common/Base.hpp" -#include "cru/platform/native/Cursor.hpp" -#include "cru/platform/native/UiApplication.hpp" +#include "cru/platform/gui/Cursor.hpp" +#include "cru/platform/gui/UiApplication.hpp" #include "cru/ui/Base.hpp" #include "cru/ui/WindowHost.hpp" #include "cru/ui/render/RenderObject.hpp" @@ -11,9 +11,9 @@ #include namespace cru::ui { -using platform::native::ICursor; -using platform::native::IUiApplication; -using platform::native::SystemCursorType; +using platform::gui::ICursor; +using platform::gui::IUiApplication; +using platform::gui::SystemCursorType; Control::Control() { MouseEnterEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) { diff --git a/src/ui/Helper.cpp b/src/ui/Helper.cpp index 6f67e701..88ead993 100644 --- a/src/ui/Helper.cpp +++ b/src/ui/Helper.cpp @@ -1,11 +1,11 @@ #include "Helper.hpp" -#include "cru/platform/graph/Factory.hpp" -#include "cru/platform/native/UiApplication.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/gui/UiApplication.hpp" namespace cru::ui { -using cru::platform::graph::IGraphFactory; -using cru::platform::native::IUiApplication; +using cru::platform::graphics::IGraphFactory; +using cru::platform::gui::IUiApplication; IGraphFactory* GetGraphFactory() { return IUiApplication::GetInstance()->GetGraphFactory(); diff --git a/src/ui/Helper.hpp b/src/ui/Helper.hpp index 6923852f..327f91ff 100644 --- a/src/ui/Helper.hpp +++ b/src/ui/Helper.hpp @@ -12,6 +12,6 @@ struct IUiApplication; } // namespace cru::platform namespace cru::ui { -cru::platform::graph::IGraphFactory* GetGraphFactory(); -cru::platform::native::IUiApplication* GetUiApplication(); +cru::platform::graphics::IGraphFactory* GetGraphFactory(); +cru::platform::gui::IUiApplication* GetUiApplication(); } // namespace cru::ui diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index 8a2029b9..62995f86 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -1,13 +1,13 @@ #include "cru/ui/UiManager.hpp" #include "Helper.hpp" -#include "cru/platform/graph/Brush.hpp" -#include "cru/platform/graph/Factory.hpp" -#include "cru/platform/graph/Font.hpp" -#include "cru/platform/native/UiApplication.hpp" +#include "cru/platform/graphics/Brush.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/graphics/Font.hpp" +#include "cru/platform/gui/UiApplication.hpp" namespace cru::ui { -using namespace cru::platform::graph; +using namespace cru::platform::graphics; namespace { std::unique_ptr CreateSolidColorBrush(IGraphFactory* factory, @@ -35,7 +35,7 @@ UiManager::UiManager() { theme_resource_.default_font = factory->CreateFont(theme_resource_.default_font_family, 24.0f); - const auto black_brush = std::shared_ptr( + const auto black_brush = std::shared_ptr( CreateSolidColorBrush(factory, colors::black)); theme_resource_.text_brush = black_brush; theme_resource_.text_selection_brush = diff --git a/src/ui/WindowHost.cpp b/src/ui/WindowHost.cpp index 8c61d27d..4b32467b 100644 --- a/src/ui/WindowHost.cpp +++ b/src/ui/WindowHost.cpp @@ -2,10 +2,10 @@ #include "RoutedEventDispatch.hpp" #include "cru/common/Logger.hpp" -#include "cru/platform/graph/Painter.hpp" -#include "cru/platform/native/InputMethod.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/platform/native/Window.hpp" +#include "cru/platform/graphics/Painter.hpp" +#include "cru/platform/gui/InputMethod.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" #include "cru/ui/DebugFlags.hpp" #include "cru/ui/Window.hpp" #include "cru/ui/render/MeasureRequirement.hpp" @@ -14,8 +14,8 @@ #include namespace cru::ui { -using platform::native::INativeWindow; -using platform::native::IUiApplication; +using platform::gui::INativeWindow; +using platform::gui::IUiApplication; namespace event_names { #ifdef CRU_DEBUG @@ -149,7 +149,7 @@ void WindowHost::InvalidateLayout() { if constexpr (debug_flags::layout) log::TagDebug(log_tag, u"A relayout is requested."); if (!need_layout_) { - platform::native::IUiApplication::GetInstance()->SetImmediate([this] { + platform::gui::IUiApplication::GetInstance()->SetImmediate([this] { Relayout(); need_layout_ = false; InvalidatePaint(); @@ -272,10 +272,10 @@ void WindowHost::OnNativeResize(INativeWindow* window, const Size& size) { } void WindowHost::OnNativeFocus(INativeWindow* window, - platform::native::FocusChangeType focus) { + platform::gui::FocusChangeType focus) { CRU_UNUSED(window) - focus == platform::native::FocusChangeType::Gain + focus == platform::gui::FocusChangeType::Gain ? DispatchEvent(event_names::GainFocus, focus_control_, &Control::GainFocusEvent, nullptr, true) : DispatchEvent(event_names::LoseFocus, focus_control_, @@ -283,10 +283,10 @@ void WindowHost::OnNativeFocus(INativeWindow* window, } void WindowHost::OnNativeMouseEnterLeave( - INativeWindow* window, platform::native::MouseEnterLeaveType type) { + INativeWindow* window, platform::gui::MouseEnterLeaveType type) { CRU_UNUSED(window) - if (type == platform::native::MouseEnterLeaveType::Leave) { + if (type == platform::gui::MouseEnterLeaveType::Leave) { DispatchEvent(event_names::MouseLeave, mouse_hover_control_, &Control::MouseLeaveEvent, nullptr); mouse_hover_control_ = nullptr; @@ -328,7 +328,7 @@ void WindowHost::OnNativeMouseMove(INativeWindow* window, const Point& point) { void WindowHost::OnNativeMouseDown( INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args) { + const platform::gui::NativeMouseButtonEventArgs& args) { CRU_UNUSED(window) Control* control = @@ -339,7 +339,7 @@ void WindowHost::OnNativeMouseDown( void WindowHost::OnNativeMouseUp( INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args) { + const platform::gui::NativeMouseButtonEventArgs& args) { CRU_UNUSED(window) Control* control = @@ -349,7 +349,7 @@ void WindowHost::OnNativeMouseUp( } void WindowHost::OnNativeKeyDown( - INativeWindow* window, const platform::native::NativeKeyEventArgs& args) { + INativeWindow* window, const platform::gui::NativeKeyEventArgs& args) { CRU_UNUSED(window) DispatchEvent(event_names::KeyDown, focus_control_, &Control::KeyDownEvent, @@ -357,7 +357,7 @@ void WindowHost::OnNativeKeyDown( } void WindowHost::OnNativeKeyUp( - INativeWindow* window, const platform::native::NativeKeyEventArgs& args) { + INativeWindow* window, const platform::gui::NativeKeyEventArgs& args) { CRU_UNUSED(window) DispatchEvent(event_names::KeyUp, focus_control_, &Control::KeyUpEvent, diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp index 6f6af878..5f7ed143 100644 --- a/src/ui/controls/Button.cpp +++ b/src/ui/controls/Button.cpp @@ -2,15 +2,15 @@ #include #include "../Helper.hpp" -#include "cru/platform/graph/Brush.hpp" -#include "cru/platform/native/Cursor.hpp" -#include "cru/platform/native/UiApplication.hpp" +#include "cru/platform/graphics/Brush.hpp" +#include "cru/platform/gui/Cursor.hpp" +#include "cru/platform/gui/UiApplication.hpp" #include "cru/ui/render/BorderRenderObject.hpp" #include "cru/ui/UiManager.hpp" #include "cru/ui/Window.hpp" namespace cru::ui::controls { -using cru::platform::native::SystemCursorType; +using cru::platform::gui::SystemCursorType; namespace { void Set(render::BorderRenderObject* o, const ButtonStateStyle& s) { @@ -21,7 +21,7 @@ void Set(render::BorderRenderObject* o, const ButtonStateStyle& s) { o->SetBackgroundBrush(s.background_brush); } -std::shared_ptr GetSystemCursor( +std::shared_ptr GetSystemCursor( SystemCursorType type) { return GetUiApplication()->GetCursorManager()->GetSystemCursor(type); } diff --git a/src/ui/controls/Container.cpp b/src/ui/controls/Container.cpp index de58ee64..8b15c566 100644 --- a/src/ui/controls/Container.cpp +++ b/src/ui/controls/Container.cpp @@ -1,6 +1,6 @@ #include "cru/ui/controls/Container.hpp" -#include "cru/platform/graph/Factory.hpp" +#include "cru/platform/graphics/Factory.hpp" #include "cru/ui/render/BorderRenderObject.hpp" namespace cru::ui::controls { diff --git a/src/ui/controls/TextControlService.hpp b/src/ui/controls/TextControlService.hpp index 3c082bad..8ad87416 100644 --- a/src/ui/controls/TextControlService.hpp +++ b/src/ui/controls/TextControlService.hpp @@ -2,11 +2,11 @@ #include "../Helper.hpp" #include "cru/common/Logger.hpp" #include "cru/common/StringUtil.hpp" -#include "cru/platform/graph/Font.hpp" -#include "cru/platform/graph/Painter.hpp" -#include "cru/platform/native/InputMethod.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/platform/native/Window.hpp" +#include "cru/platform/graphics/Font.hpp" +#include "cru/platform/graphics/Painter.hpp" +#include "cru/platform/gui/InputMethod.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" #include "cru/ui/Base.hpp" #include "cru/ui/Control.hpp" #include "cru/ui/DebugFlags.hpp" @@ -135,10 +135,10 @@ class TextControlService : public Object { this->SyncTextRenderObject(); } - platform::native::IInputMethodContext* GetInputMethodContext() { + platform::gui::IInputMethodContext* GetInputMethodContext() { WindowHost* host = this->control_->GetWindowHost(); if (!host) return nullptr; - platform::native::INativeWindow* native_window = host->GetNativeWindow(); + platform::gui::INativeWindow* native_window = host->GetNativeWindow(); if (!native_window) return nullptr; return native_window->GetInputMethodContext(); } @@ -149,7 +149,7 @@ class TextControlService : public Object { input_method_context->CancelComposition(); } - std::optional GetCompositionInfo() { + std::optional GetCompositionInfo() { auto input_method_context = GetInputMethodContext(); if (input_method_context == nullptr) return std::nullopt; auto composition_info = input_method_context->GetCompositionText(); @@ -360,8 +360,8 @@ class TextControlService : public Object { void KeyDownHandler(event::KeyEventArgs& args) { const auto key_code = args.GetKeyCode(); - using cru::platform::native::KeyCode; - using cru::platform::native::KeyModifiers; + using cru::platform::gui::KeyCode; + using cru::platform::gui::KeyModifiers; switch (key_code) { case KeyCode::Backspace: { @@ -469,7 +469,7 @@ class TextControlService : public Object { bool editable_ = false; bool caret_visible_ = false; - platform::native::TimerAutoCanceler caret_timer_canceler_; + platform::gui::TimerAutoCanceler caret_timer_canceler_; int caret_blink_duration_ = k_default_caret_blink_duration; ShortcutHub shortcut_hub_; diff --git a/src/ui/render/BorderRenderObject.cpp b/src/ui/render/BorderRenderObject.cpp index b7e1e709..8e16d8cb 100644 --- a/src/ui/render/BorderRenderObject.cpp +++ b/src/ui/render/BorderRenderObject.cpp @@ -2,9 +2,9 @@ #include "../Helper.hpp" #include "cru/common/Logger.hpp" -#include "cru/platform/graph/Factory.hpp" -#include "cru/platform/graph/Geometry.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/graphics/Geometry.hpp" +#include "cru/platform/graphics/util/Painter.hpp" #include @@ -51,7 +51,7 @@ RenderObject* BorderRenderObject::HitTest(const Point& point) { } } -void BorderRenderObject::OnDrawCore(platform::graph::IPainter* painter) { +void BorderRenderObject::OnDrawCore(platform::graphics::IPainter* painter) { if (background_brush_ != nullptr) painter->FillGeometry(border_inner_geometry_.get(), background_brush_.get()); @@ -235,7 +235,7 @@ void BorderRenderObject::RecreateGeometry() { r.left_bottom - Point{t.left, t.bottom}, r.right_bottom - Point{t.right, t.bottom}); - auto f = [](platform::graph::IGeometryBuilder* builder, const Rect& rect, + auto f = [](platform::graphics::IGeometryBuilder* builder, const Rect& rect, const CornerRadius& corner) { builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top)); builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top)); @@ -263,7 +263,7 @@ void BorderRenderObject::RecreateGeometry() { size.width - margin.GetHorizontalTotal(), size.height - margin.GetVerticalTotal()}; const auto graph_factory = GetGraphFactory(); - std::unique_ptr builder{ + std::unique_ptr builder{ graph_factory->CreateGeometryBuilder()}; f(builder.get(), outer_rect, outer_radius); border_outer_geometry_ = builder->Build(); diff --git a/src/ui/render/CanvasRenderObject.cpp b/src/ui/render/CanvasRenderObject.cpp index 967fdcec..bf1155e1 100644 --- a/src/ui/render/CanvasRenderObject.cpp +++ b/src/ui/render/CanvasRenderObject.cpp @@ -10,7 +10,7 @@ RenderObject* CanvasRenderObject::HitTest(const Point& point) { return padding_rect.IsPointInside(point) ? this : nullptr; } -void CanvasRenderObject::OnDrawContent(platform::graph::IPainter* painter) { +void CanvasRenderObject::OnDrawContent(platform::graphics::IPainter* painter) { const auto rect = GetContentRect(); CanvasPaintEventArgs args{painter, rect.GetSize()}; paint_event_.Raise(args); diff --git a/src/ui/render/FlexLayoutRenderObject.cpp b/src/ui/render/FlexLayoutRenderObject.cpp index 36a2dcea..b1ef69ee 100644 --- a/src/ui/render/FlexLayoutRenderObject.cpp +++ b/src/ui/render/FlexLayoutRenderObject.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/FlexLayoutRenderObject.hpp" #include "cru/common/Logger.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/util/Painter.hpp" #include "cru/ui/render/LayoutHelper.hpp" #include diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp index 57929a21..09410113 100644 --- a/src/ui/render/RenderObject.cpp +++ b/src/ui/render/RenderObject.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/RenderObject.hpp" #include "cru/common/Logger.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/util/Painter.hpp" #include "cru/ui/DebugFlags.hpp" #include "cru/ui/WindowHost.hpp" @@ -103,7 +103,7 @@ void RenderObject::Layout(const Point& offset) { OnLayoutCore(); } -void RenderObject::Draw(platform::graph::IPainter* painter) { +void RenderObject::Draw(platform::graphics::IPainter* painter) { OnDrawCore(painter); } @@ -138,29 +138,29 @@ void RenderObject::OnRemoveChild(RenderObject* removed_child, Index position) { InvalidatePaint(); } -void RenderObject::DefaultDrawChildren(platform::graph::IPainter* painter) { +void RenderObject::DefaultDrawChildren(platform::graphics::IPainter* painter) { for (const auto child : GetChildren()) { auto offset = child->GetOffset(); - platform::graph::util::WithTransform( + platform::graphics::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto p) { child->Draw(p); }); } } -void RenderObject::DefaultDrawContent(platform::graph::IPainter* painter) { +void RenderObject::DefaultDrawContent(platform::graphics::IPainter* painter) { const auto content_rect = GetContentRect(); - platform::graph::util::WithTransform( + platform::graphics::util::WithTransform( painter, Matrix::Translation(content_rect.left, content_rect.top), [this](auto p) { this->OnDrawContent(p); }); } -void RenderObject::OnDrawCore(platform::graph::IPainter* painter) { +void RenderObject::OnDrawCore(platform::graphics::IPainter* painter) { DefaultDrawContent(painter); DefaultDrawChildren(painter); } -void RenderObject::OnDrawContent(platform::graph::IPainter* painter) { +void RenderObject::OnDrawContent(platform::graphics::IPainter* painter) { CRU_UNUSED(painter); } diff --git a/src/ui/render/ScrollRenderObject.cpp b/src/ui/render/ScrollRenderObject.cpp index 24ea351a..e621cd0c 100644 --- a/src/ui/render/ScrollRenderObject.cpp +++ b/src/ui/render/ScrollRenderObject.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/ScrollRenderObject.hpp" -#include "cru/platform/graph/Painter.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/Painter.hpp" +#include "cru/platform/graphics/util/Painter.hpp" #include @@ -42,14 +42,14 @@ RenderObject* ScrollRenderObject::HitTest(const Point& point) { return rect.IsPointInside(point) ? this : nullptr; } // namespace cru::ui::render -void ScrollRenderObject::OnDrawCore(platform::graph::IPainter* painter) { +void ScrollRenderObject::OnDrawCore(platform::graphics::IPainter* painter) { DefaultDrawContent(painter); if (const auto child = GetSingleChild()) { painter->PushLayer(this->GetPaddingRect()); const auto offset = child->GetOffset(); - platform::graph::util::WithTransform( + platform::graphics::util::WithTransform( painter, Matrix::Translation(offset.x, offset.y), - [child](platform::graph::IPainter* p) { child->Draw(p); }); + [child](platform::graphics::IPainter* p) { child->Draw(p); }); painter->PopLayer(); } } diff --git a/src/ui/render/TextRenderObject.cpp b/src/ui/render/TextRenderObject.cpp index 466c5084..9faab622 100644 --- a/src/ui/render/TextRenderObject.cpp +++ b/src/ui/render/TextRenderObject.cpp @@ -2,19 +2,19 @@ #include "../Helper.hpp" #include "cru/common/Logger.hpp" -#include "cru/platform/graph/Factory.hpp" -#include "cru/platform/graph/TextLayout.hpp" -#include "cru/platform/graph/util/Painter.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/graphics/TextLayout.hpp" +#include "cru/platform/graphics/util/Painter.hpp" #include #include namespace cru::ui::render { TextRenderObject::TextRenderObject( - std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush, - std::shared_ptr caret_brush) { + std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr selection_brush, + std::shared_ptr caret_brush) { Expects(brush); Expects(font); Expects(selection_brush); @@ -47,17 +47,17 @@ void TextRenderObject::SetText(std::u16string new_text) { } void TextRenderObject::SetBrush( - std::shared_ptr new_brush) { + std::shared_ptr new_brush) { Expects(new_brush); new_brush.swap(brush_); InvalidatePaint(); } -std::shared_ptr TextRenderObject::GetFont() const { +std::shared_ptr TextRenderObject::GetFont() const { return text_layout_->GetFont(); } -void TextRenderObject::SetFont(std::shared_ptr font) { +void TextRenderObject::SetFont(std::shared_ptr font) { Expects(font); text_layout_->SetFont(std::move(font)); } @@ -70,7 +70,7 @@ Point TextRenderObject::TextSinglePoint(gsl::index position, bool trailing) { return text_layout_->TextSinglePoint(position, trailing); } -platform::graph::TextHitTestResult TextRenderObject::TextHitTest( +platform::graphics::TextHitTestResult TextRenderObject::TextHitTest( const Point& point) { return text_layout_->HitTest(point); } @@ -81,7 +81,7 @@ void TextRenderObject::SetSelectionRange(std::optional new_range) { } void TextRenderObject::SetSelectionBrush( - std::shared_ptr new_brush) { + std::shared_ptr new_brush) { Expects(new_brush); new_brush.swap(selection_brush_); if (selection_range_ && selection_range_->count) { @@ -106,7 +106,7 @@ void TextRenderObject::SetCaretPosition(gsl::index position) { } void TextRenderObject::GetCaretBrush( - std::shared_ptr brush) { + std::shared_ptr brush) { Expects(brush); brush.swap(caret_brush_); if (draw_caret_) { @@ -159,7 +159,7 @@ RenderObject* TextRenderObject::HitTest(const Point& point) { return padding_rect.IsPointInside(point) ? this : nullptr; } -void TextRenderObject::OnDrawContent(platform::graph::IPainter* painter) { +void TextRenderObject::OnDrawContent(platform::graphics::IPainter* painter) { if (this->selection_range_.has_value()) { const auto&& rects = text_layout_->TextRangeRect(this->selection_range_.value()); diff --git a/src/win/CMakeLists.txt b/src/win/CMakeLists.txt index 06f947a1..bf8de863 100644 --- a/src/win/CMakeLists.txt +++ b/src/win/CMakeLists.txt @@ -14,5 +14,5 @@ target_sources(cru_win_base PUBLIC target_compile_definitions(cru_win_base PUBLIC UNICODE _UNICODE) # use unicode target_link_libraries(cru_win_base PUBLIC cru_base) -add_subdirectory(graph) -add_subdirectory(native) +add_subdirectory(graphics) +add_subdirectory(gui) diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt deleted file mode 100644 index c90537ac..00000000 --- a/src/win/graph/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(direct) diff --git a/src/win/graph/direct/Brush.cpp b/src/win/graph/direct/Brush.cpp deleted file mode 100644 index 2b9d4ac3..00000000 --- a/src/win/graph/direct/Brush.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "cru/win/graph/direct/Brush.hpp" - -#include "cru/win/graph/direct/ConvertUtil.hpp" -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Factory.hpp" - -namespace cru::platform::graph::win::direct { -D2DSolidColorBrush::D2DSolidColorBrush(DirectGraphFactory* factory) - : DirectGraphResource(factory) { - ThrowIfFailed(factory->GetDefaultD2D1DeviceContext()->CreateSolidColorBrush( - Convert(color_), &brush_)); -} - -void D2DSolidColorBrush::SetColor(const Color& color) { - brush_->SetColor(Convert(color)); -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt deleted file mode 100644 index 070b7b51..00000000 --- a/src/win/graph/direct/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ -set(CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graph/direct) - -add_library(cru_win_graph_direct STATIC - Brush.cpp - Font.cpp - Geometry.cpp - Factory.cpp - Painter.cpp - Resource.cpp - TextLayout.cpp - WindowPainter.cpp - WindowRenderTarget.cpp -) -target_sources(cru_win_graph_direct PUBLIC - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Brush.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/ComResource.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/ConvertUtil.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Exception.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Font.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Geometry.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Factory.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Painter.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Resource.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/TextLayout.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/WindowPainter.hpp - ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/WindowRenderTarget.hpp -) -target_link_libraries(cru_win_graph_direct PUBLIC D3D11 D2d1 DWrite) -target_link_libraries(cru_win_graph_direct PUBLIC cru_win_base cru_platform_graph) diff --git a/src/win/graph/direct/Factory.cpp b/src/win/graph/direct/Factory.cpp deleted file mode 100644 index 03e64e13..00000000 --- a/src/win/graph/direct/Factory.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include "cru/win/graph/direct/Factory.hpp" - -#include "cru/common/Logger.hpp" -#include "cru/win/graph/direct/Brush.hpp" -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Font.hpp" -#include "cru/win/graph/direct/Geometry.hpp" -#include "cru/win/graph/direct/TextLayout.hpp" - -#include -#include - -namespace cru::platform::graph::win::direct { -namespace { -void InitializeCom() { - const auto hresult = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - if (FAILED(hresult)) { - throw HResultError(hresult, "Failed to call CoInitializeEx."); - } - if (hresult == S_FALSE) { - log::Debug( - u"Try to call CoInitializeEx, but it seems COM is already " - u"initialized."); - } -} - -void UninitializeCom() { ::CoUninitialize(); } -} // namespace - -DirectGraphFactory::DirectGraphFactory() { - // TODO! Detect repeated creation. Because I don't think we can create two d2d - // and dwrite factory so we need to prevent the "probably dangerous" behavior. - - InitializeCom(); - - UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; - -#ifdef CRU_DEBUG - creation_flags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - const D3D_FEATURE_LEVEL feature_levels[] = { - D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1}; - - Microsoft::WRL::ComPtr d3d11_device_context; - - ThrowIfFailed(D3D11CreateDevice( - nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, - feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, - &d3d11_device_, nullptr, &d3d11_device_context)); - - Microsoft::WRL::ComPtr dxgi_device; - ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); - - ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, - IID_PPV_ARGS(&d2d1_factory_))); - - ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device_)); - - d2d1_device_context_ = CreateD2D1DeviceContext(); - - // Identify the physical adapter (GPU or card) this device is runs on. - Microsoft::WRL::ComPtr dxgi_adapter; - ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); - - // Get the factory object that created the DXGI device. - ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); - - ThrowIfFailed(DWriteCreateFactory( - DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(dwrite_factory_.GetAddressOf()))); - - ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( - &dwrite_system_font_collection_)); -} - -DirectGraphFactory::~DirectGraphFactory() { UninitializeCom(); } - -Microsoft::WRL::ComPtr -DirectGraphFactory::CreateD2D1DeviceContext() { - Microsoft::WRL::ComPtr d2d1_device_context; - ThrowIfFailed(d2d1_device_->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context)); - return d2d1_device_context; -} - -std::unique_ptr DirectGraphFactory::CreateSolidColorBrush() { - return std::make_unique(this); -} - -std::unique_ptr DirectGraphFactory::CreateGeometryBuilder() { - return std::make_unique(this); -} - -std::unique_ptr DirectGraphFactory::CreateFont( - std::u16string font_family, float font_size) { - return std::make_unique(this, std::move(font_family), font_size); -} - -std::unique_ptr DirectGraphFactory::CreateTextLayout( - std::shared_ptr font, std::u16string text) { - return std::make_unique(this, std::move(font), - std::move(text)); -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/Font.cpp b/src/win/graph/direct/Font.cpp deleted file mode 100644 index 34de5b71..00000000 --- a/src/win/graph/direct/Font.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "cru/win/graph/direct/Font.hpp" - -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Factory.hpp" - -#include -#include - -namespace cru::platform::graph::win::direct { -DWriteFont::DWriteFont(DirectGraphFactory* factory, std::u16string font_family, - float font_size) - : DirectGraphResource(factory), font_family_(std::move(font_family)) { - // Get locale - std::array buffer; - if (!::GetUserDefaultLocaleName(buffer.data(), - static_cast(buffer.size()))) - throw platform::win::Win32Error( - ::GetLastError(), "Failed to get locale when create dwrite font."); - - ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat( - reinterpret_cast(font_family_.c_str()), nullptr, - DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, - DWRITE_FONT_STRETCH_NORMAL, font_size, buffer.data(), &text_format_)); - - ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING)); - ThrowIfFailed( - text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR)); -} - -float DWriteFont::GetFontSize() { return text_format_->GetFontSize(); } -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/Geometry.cpp b/src/win/graph/direct/Geometry.cpp deleted file mode 100644 index e77b4749..00000000 --- a/src/win/graph/direct/Geometry.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "cru/win/graph/direct/Geometry.hpp" - -#include "cru/win/graph/direct/ConvertUtil.hpp" -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Factory.hpp" - -namespace cru::platform::graph::win::direct { -D2DGeometryBuilder::D2DGeometryBuilder(DirectGraphFactory* factory) - : DirectGraphResource(factory) { - ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_)); - ThrowIfFailed(geometry_->Open(&geometry_sink_)); -} - -void D2DGeometryBuilder::CheckValidation() { - if (!IsValid()) - throw ReuseException("The geometry builder is already disposed."); -} - -void D2DGeometryBuilder::BeginFigure(const Point& point) { - CheckValidation(); - geometry_sink_->BeginFigure(Convert(point), D2D1_FIGURE_BEGIN_FILLED); -} - -void D2DGeometryBuilder::LineTo(const Point& point) { - CheckValidation(); - geometry_sink_->AddLine(Convert(point)); -} - -void D2DGeometryBuilder::QuadraticBezierTo(const Point& control_point, - const Point& end_point) { - CheckValidation(); - geometry_sink_->AddQuadraticBezier( - D2D1::QuadraticBezierSegment(Convert(control_point), Convert(end_point))); -} - -void D2DGeometryBuilder::CloseFigure(bool close) { - CheckValidation(); - geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED - : D2D1_FIGURE_END_OPEN); -} - -std::unique_ptr D2DGeometryBuilder::Build() { - CheckValidation(); - ThrowIfFailed(geometry_sink_->Close()); - geometry_sink_ = nullptr; - auto geometry = - std::make_unique(GetDirectFactory(), std::move(geometry_)); - geometry_ = nullptr; - return geometry; -} - -D2DGeometry::D2DGeometry(DirectGraphFactory* factory, - Microsoft::WRL::ComPtr geometry) - : DirectGraphResource(factory), geometry_(std::move(geometry)) {} - -bool D2DGeometry::FillContains(const Point& point) { - BOOL result; - ThrowIfFailed(geometry_->FillContainsPoint( - Convert(point), D2D1::Matrix3x2F::Identity(), &result)); - return result != 0; -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/Painter.cpp b/src/win/graph/direct/Painter.cpp deleted file mode 100644 index 3ffb5208..00000000 --- a/src/win/graph/direct/Painter.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "cru/win/graph/direct/Painter.hpp" - -#include "cru/platform/Check.hpp" -#include "cru/win/graph/direct/Brush.hpp" -#include "cru/win/graph/direct/ConvertUtil.hpp" -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Geometry.hpp" -#include "cru/win/graph/direct/TextLayout.hpp" - -#include - -namespace cru::platform::graph::win::direct { -D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) { - Expects(render_target); - render_target_ = render_target; -} - -platform::Matrix D2DPainter::GetTransform() { - CheckValidation(); - D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); - return Convert(m); -} - -void D2DPainter::SetTransform(const platform::Matrix& matrix) { - CheckValidation(); - render_target_->SetTransform(Convert(matrix)); -} - -void D2DPainter::Clear(const Color& color) { - CheckValidation(); - render_target_->Clear(Convert(color)); -} - -void D2DPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush, - float width) { - CheckValidation(); - const auto b = CheckPlatform(brush, GetPlatformId()); - render_target_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(), - width); -} - -void D2DPainter::FillRectangle(const Rect& rectangle, IBrush* brush) { - CheckValidation(); - const auto b = CheckPlatform(brush, GetPlatformId()); - render_target_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface()); -} - -void D2DPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush, - float width) { - CheckValidation(); - const auto g = CheckPlatform(geometry, GetPlatformId()); - const auto b = CheckPlatform(brush, GetPlatformId()); - render_target_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(), - width); -} - -void D2DPainter::FillGeometry(IGeometry* geometry, IBrush* brush) { - CheckValidation(); - const auto g = CheckPlatform(geometry, GetPlatformId()); - const auto b = CheckPlatform(brush, GetPlatformId()); - render_target_->FillGeometry(g->GetComInterface(), b->GetD2DBrushInterface()); -} - -void D2DPainter::DrawText(const Point& offset, ITextLayout* text_layout, - IBrush* brush) { - CheckValidation(); - const auto t = CheckPlatform(text_layout, GetPlatformId()); - const auto b = CheckPlatform(brush, GetPlatformId()); - render_target_->DrawTextLayout(Convert(offset), t->GetComInterface(), - b->GetD2DBrushInterface()); -} - -void D2DPainter::PushLayer(const Rect& bounds) { - CheckValidation(); - - Microsoft::WRL::ComPtr layer; - ThrowIfFailed(render_target_->CreateLayer(&layer)); - - render_target_->PushLayer(D2D1::LayerParameters(Convert(bounds)), - layer.Get()); - - layers_.push_back(std::move(layer)); -} - -void D2DPainter::PopLayer() { - render_target_->PopLayer(); - layers_.pop_back(); -} - -void D2DPainter::EndDraw() { - if (is_drawing_) { - is_drawing_ = false; - DoEndDraw(); - } -} - -void D2DPainter::CheckValidation() { - if (!is_drawing_) { - throw cru::platform::ReuseException( - "Can't do that on painter after end drawing."); - } -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/Resource.cpp b/src/win/graph/direct/Resource.cpp deleted file mode 100644 index 772bb74b..00000000 --- a/src/win/graph/direct/Resource.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "cru/win/graph/direct/Resource.hpp" - -#include "cru/win/graph/direct/Factory.hpp" - -namespace cru::platform::graph::win::direct { -DirectGraphResource::DirectGraphResource(DirectGraphFactory* factory) - : factory_(factory) { - Expects(factory); -} - -IGraphFactory* DirectGraphResource::GetGraphFactory() { return factory_; } -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/TextLayout.cpp b/src/win/graph/direct/TextLayout.cpp deleted file mode 100644 index 0d4a6392..00000000 --- a/src/win/graph/direct/TextLayout.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "cru/win/graph/direct/TextLayout.hpp" - -#include "cru/common/Logger.hpp" -#include "cru/platform/Check.hpp" -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Factory.hpp" -#include "cru/win/graph/direct/Font.hpp" - -#include - -namespace cru::platform::graph::win::direct { -DWriteTextLayout::DWriteTextLayout(DirectGraphFactory* factory, - std::shared_ptr font, - std::u16string text) - : DirectGraphResource(factory), text_(std::move(text)) { - Expects(font); - font_ = CheckPlatform(font, GetPlatformId()); - - ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout( - reinterpret_cast(text_.c_str()), - static_cast(text_.size()), font_->GetComInterface(), max_width_, - max_height_, &text_layout_)); -} - -DWriteTextLayout::~DWriteTextLayout() = default; - -std::u16string DWriteTextLayout::GetText() { return text_; } - -std::u16string_view DWriteTextLayout::GetTextView() { return text_; } - -void DWriteTextLayout::SetText(std::u16string new_text) { - text_.swap(new_text); - ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout( - reinterpret_cast(text_.c_str()), - static_cast(text_.size()), font_->GetComInterface(), max_width_, - max_height_, &text_layout_)); -} - -std::shared_ptr DWriteTextLayout::GetFont() { - return std::dynamic_pointer_cast(font_); -} - -void DWriteTextLayout::SetFont(std::shared_ptr font) { - font_ = CheckPlatform(font, GetPlatformId()); - ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout( - reinterpret_cast(text_.c_str()), - static_cast(text_.size()), font_->GetComInterface(), max_width_, - max_height_, &text_layout_)); -} - -void DWriteTextLayout::SetMaxWidth(float max_width) { - max_width_ = max_width; - ThrowIfFailed(text_layout_->SetMaxWidth(max_width_)); -} - -void DWriteTextLayout::SetMaxHeight(float max_height) { - max_height_ = max_height; - ThrowIfFailed(text_layout_->SetMaxHeight(max_height_)); -} - -Rect DWriteTextLayout::GetTextBounds() { - DWRITE_TEXT_METRICS metrics; - ThrowIfFailed(text_layout_->GetMetrics(&metrics)); - return Rect{metrics.left, metrics.top, metrics.width, metrics.height}; -} - -std::vector DWriteTextLayout::TextRangeRect( - const TextRange& text_range_arg) { - if (text_range_arg.count == 0) { - return {}; - } - const auto text_range = text_range_arg.Normalize(); - - DWRITE_TEXT_METRICS text_metrics; - ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); - const auto metrics_count = - text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; - - std::vector hit_test_metrics(metrics_count); - UINT32 actual_count; - ThrowIfFailed(text_layout_->HitTestTextRange( - static_cast(text_range.position), - static_cast(text_range.count), 0, 0, hit_test_metrics.data(), - metrics_count, &actual_count)); - - hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, - hit_test_metrics.cend()); - - std::vector result; - result.reserve(actual_count); - - for (const auto& metrics : hit_test_metrics) { - result.push_back( - Rect{metrics.left, metrics.top, metrics.width, metrics.height}); - } - - return result; -} - -Point DWriteTextLayout::TextSinglePoint(Index position, bool trailing) { - DWRITE_HIT_TEST_METRICS metrics; - FLOAT left; - FLOAT top; - ThrowIfFailed(text_layout_->HitTestTextPosition(static_cast(position), - static_cast(trailing), - &left, &top, &metrics)); - return Point{left, top}; -} - -TextHitTestResult DWriteTextLayout::HitTest(const Point& point) { - BOOL trailing; - BOOL inside; - DWRITE_HIT_TEST_METRICS metrics; - - ThrowIfFailed(text_layout_->HitTestPoint(point.x, point.y, &trailing, &inside, - &metrics)); - - TextHitTestResult result; - result.position = metrics.textPosition; - result.trailing = trailing != 0; - result.insideText = inside != 0; - return result; -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/WindowPainter.cpp b/src/win/graph/direct/WindowPainter.cpp deleted file mode 100644 index 74f7da7a..00000000 --- a/src/win/graph/direct/WindowPainter.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "cru/win/graph/direct/WindowPainter.hpp" - -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Factory.hpp" -#include "cru/win/graph/direct/WindowRenderTarget.hpp" - -namespace cru::platform::graph::win::direct { -D2DWindowPainter::D2DWindowPainter(D2DWindowRenderTarget* render_target) - : D2DPainter(render_target->GetD2D1DeviceContext()), - render_target_(render_target) { - render_target_->GetD2D1DeviceContext()->BeginDraw(); -} - -D2DWindowPainter::~D2DWindowPainter() { EndDraw(); } - -void D2DWindowPainter::DoEndDraw() { - ThrowIfFailed(render_target_->GetD2D1DeviceContext()->EndDraw()); - render_target_->Present(); -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/WindowRenderTarget.cpp b/src/win/graph/direct/WindowRenderTarget.cpp deleted file mode 100644 index d26fccf6..00000000 --- a/src/win/graph/direct/WindowRenderTarget.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include "cru/win/graph/direct/WindowRenderTarget.hpp" - -#include "cru/win/graph/direct/Exception.hpp" -#include "cru/win/graph/direct/Factory.hpp" - -namespace cru::platform::graph::win::direct { -D2DWindowRenderTarget::D2DWindowRenderTarget( - gsl::not_null factory, HWND hwnd) - : factory_(factory), hwnd_(hwnd) { - const auto d3d11_device = factory->GetD3D11Device(); - const auto dxgi_factory = factory->GetDxgiFactory(); - - d2d1_device_context_ = factory->CreateD2D1DeviceContext(); - d2d1_device_context_->SetUnitMode(D2D1_UNIT_MODE_DIPS); - - // Allocate a descriptor. - DXGI_SWAP_CHAIN_DESC1 swap_chain_desc; - swap_chain_desc.Width = 0; // use automatic sizing - swap_chain_desc.Height = 0; - swap_chain_desc.Format = - DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format - swap_chain_desc.Stereo = false; - swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling - swap_chain_desc.SampleDesc.Quality = 0; - swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swap_chain_desc.BufferCount = 2; // use double buffering to enable flip - swap_chain_desc.Scaling = DXGI_SCALING_NONE; - swap_chain_desc.SwapEffect = - DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect - swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED; - swap_chain_desc.Flags = 0; - - // Get the final swap chain for this window from the DXGI factory. - ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd( - d3d11_device, hwnd, &swap_chain_desc, nullptr, nullptr, - &dxgi_swap_chain_)); - - CreateTargetBitmap(); -} - -void D2DWindowRenderTarget::SetDpi(float x, float y) { - d2d1_device_context_->SetDpi(x, y); -} - -void D2DWindowRenderTarget::ResizeBuffer(const int width, const int height) { - // In order to resize buffer, we need to untarget the buffer first. - d2d1_device_context_->SetTarget(nullptr); - target_bitmap_ = nullptr; - ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, - DXGI_FORMAT_UNKNOWN, 0)); - CreateTargetBitmap(); -} - -void D2DWindowRenderTarget::Present() { - ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); -} - -void D2DWindowRenderTarget::CreateTargetBitmap() { - Expects(target_bitmap_ == nullptr); // target bitmap must not exist. - - // Direct2D needs the dxgi version of the backbuffer surface pointer. - Microsoft::WRL::ComPtr dxgi_back_buffer; - ThrowIfFailed( - dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); - - float dpi_x, dpi_y; - d2d1_device_context_->GetDpi(&dpi_x, &dpi_y); - - auto bitmap_properties = D2D1::BitmapProperties1( - D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, - D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), - dpi_x, dpi_y); - - // Get a D2D surface from the DXGI back buffer to use as the D2D render - // target. - ThrowIfFailed(d2d1_device_context_->CreateBitmapFromDxgiSurface( - dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); - - d2d1_device_context_->SetTarget(target_bitmap_.Get()); -} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/graphics/CMakeLists.txt b/src/win/graphics/CMakeLists.txt new file mode 100644 index 00000000..c90537ac --- /dev/null +++ b/src/win/graphics/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(direct) diff --git a/src/win/graphics/direct/Brush.cpp b/src/win/graphics/direct/Brush.cpp new file mode 100644 index 00000000..b7842b97 --- /dev/null +++ b/src/win/graphics/direct/Brush.cpp @@ -0,0 +1,17 @@ +#include "cru/win/graphics/direct/Brush.hpp" + +#include "cru/win/graphics/direct/ConvertUtil.hpp" +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" + +namespace cru::platform::graphics::win::direct { +D2DSolidColorBrush::D2DSolidColorBrush(DirectGraphFactory* factory) + : DirectGraphResource(factory) { + ThrowIfFailed(factory->GetDefaultD2D1DeviceContext()->CreateSolidColorBrush( + Convert(color_), &brush_)); +} + +void D2DSolidColorBrush::SetColor(const Color& color) { + brush_->SetColor(Convert(color)); +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/CMakeLists.txt b/src/win/graphics/direct/CMakeLists.txt new file mode 100644 index 00000000..d4c96a65 --- /dev/null +++ b/src/win/graphics/direct/CMakeLists.txt @@ -0,0 +1,29 @@ +set(CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graphics/direct) + +add_library(cru_win_graphics_direct STATIC + Brush.cpp + Font.cpp + Geometry.cpp + Factory.cpp + Painter.cpp + Resource.cpp + TextLayout.cpp + WindowPainter.cpp + WindowRenderTarget.cpp +) +target_sources(cru_win_graphics_direct PUBLIC + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Brush.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/ComResource.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/ConvertUtil.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Exception.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Font.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Geometry.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Factory.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Painter.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/Resource.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/TextLayout.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/WindowPainter.hpp + ${CRU_WIN_GRAPHICS_DIRECT_INCLUDE_DIR}/WindowRenderTarget.hpp +) +target_link_libraries(cru_win_graphics_direct PUBLIC D3D11 D2d1 DWrite) +target_link_libraries(cru_win_graphics_direct PUBLIC cru_win_base cru_platform_graphics) diff --git a/src/win/graphics/direct/Factory.cpp b/src/win/graphics/direct/Factory.cpp new file mode 100644 index 00000000..6694801f --- /dev/null +++ b/src/win/graphics/direct/Factory.cpp @@ -0,0 +1,107 @@ +#include "cru/win/graphics/direct/Factory.hpp" + +#include "cru/common/Logger.hpp" +#include "cru/win/graphics/direct/Brush.hpp" +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Font.hpp" +#include "cru/win/graphics/direct/Geometry.hpp" +#include "cru/win/graphics/direct/TextLayout.hpp" + +#include +#include + +namespace cru::platform::graphics::win::direct { +namespace { +void InitializeCom() { + const auto hresult = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); + if (FAILED(hresult)) { + throw HResultError(hresult, "Failed to call CoInitializeEx."); + } + if (hresult == S_FALSE) { + log::Debug( + u"Try to call CoInitializeEx, but it seems COM is already " + u"initialized."); + } +} + +void UninitializeCom() { ::CoUninitialize(); } +} // namespace + +DirectGraphFactory::DirectGraphFactory() { + // TODO! Detect repeated creation. Because I don't think we can create two d2d + // and dwrite factory so we need to prevent the "probably dangerous" behavior. + + InitializeCom(); + + UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; + +#ifdef CRU_DEBUG + creation_flags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + const D3D_FEATURE_LEVEL feature_levels[] = { + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1}; + + Microsoft::WRL::ComPtr d3d11_device_context; + + ThrowIfFailed(D3D11CreateDevice( + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, + feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, + &d3d11_device_, nullptr, &d3d11_device_context)); + + Microsoft::WRL::ComPtr dxgi_device; + ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); + + ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, + IID_PPV_ARGS(&d2d1_factory_))); + + ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device_)); + + d2d1_device_context_ = CreateD2D1DeviceContext(); + + // Identify the physical adapter (GPU or card) this device is runs on. + Microsoft::WRL::ComPtr dxgi_adapter; + ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); + + // Get the factory object that created the DXGI device. + ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); + + ThrowIfFailed(DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(dwrite_factory_.GetAddressOf()))); + + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( + &dwrite_system_font_collection_)); +} + +DirectGraphFactory::~DirectGraphFactory() { UninitializeCom(); } + +Microsoft::WRL::ComPtr +DirectGraphFactory::CreateD2D1DeviceContext() { + Microsoft::WRL::ComPtr d2d1_device_context; + ThrowIfFailed(d2d1_device_->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context)); + return d2d1_device_context; +} + +std::unique_ptr DirectGraphFactory::CreateSolidColorBrush() { + return std::make_unique(this); +} + +std::unique_ptr DirectGraphFactory::CreateGeometryBuilder() { + return std::make_unique(this); +} + +std::unique_ptr DirectGraphFactory::CreateFont( + std::u16string font_family, float font_size) { + return std::make_unique(this, std::move(font_family), font_size); +} + +std::unique_ptr DirectGraphFactory::CreateTextLayout( + std::shared_ptr font, std::u16string text) { + return std::make_unique(this, std::move(font), + std::move(text)); +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Font.cpp b/src/win/graphics/direct/Font.cpp new file mode 100644 index 00000000..1d6a5c88 --- /dev/null +++ b/src/win/graphics/direct/Font.cpp @@ -0,0 +1,31 @@ +#include "cru/win/graphics/direct/Font.hpp" + +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" + +#include +#include + +namespace cru::platform::graphics::win::direct { +DWriteFont::DWriteFont(DirectGraphFactory* factory, std::u16string font_family, + float font_size) + : DirectGraphResource(factory), font_family_(std::move(font_family)) { + // Get locale + std::array buffer; + if (!::GetUserDefaultLocaleName(buffer.data(), + static_cast(buffer.size()))) + throw platform::win::Win32Error( + ::GetLastError(), "Failed to get locale when create dwrite font."); + + ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat( + reinterpret_cast(font_family_.c_str()), nullptr, + DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, + DWRITE_FONT_STRETCH_NORMAL, font_size, buffer.data(), &text_format_)); + + ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING)); + ThrowIfFailed( + text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR)); +} + +float DWriteFont::GetFontSize() { return text_format_->GetFontSize(); } +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Geometry.cpp b/src/win/graphics/direct/Geometry.cpp new file mode 100644 index 00000000..8aa961b2 --- /dev/null +++ b/src/win/graphics/direct/Geometry.cpp @@ -0,0 +1,62 @@ +#include "cru/win/graphics/direct/Geometry.hpp" + +#include "cru/win/graphics/direct/ConvertUtil.hpp" +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" + +namespace cru::platform::graphics::win::direct { +D2DGeometryBuilder::D2DGeometryBuilder(DirectGraphFactory* factory) + : DirectGraphResource(factory) { + ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_)); + ThrowIfFailed(geometry_->Open(&geometry_sink_)); +} + +void D2DGeometryBuilder::CheckValidation() { + if (!IsValid()) + throw ReuseException("The geometry builder is already disposed."); +} + +void D2DGeometryBuilder::BeginFigure(const Point& point) { + CheckValidation(); + geometry_sink_->BeginFigure(Convert(point), D2D1_FIGURE_BEGIN_FILLED); +} + +void D2DGeometryBuilder::LineTo(const Point& point) { + CheckValidation(); + geometry_sink_->AddLine(Convert(point)); +} + +void D2DGeometryBuilder::QuadraticBezierTo(const Point& control_point, + const Point& end_point) { + CheckValidation(); + geometry_sink_->AddQuadraticBezier( + D2D1::QuadraticBezierSegment(Convert(control_point), Convert(end_point))); +} + +void D2DGeometryBuilder::CloseFigure(bool close) { + CheckValidation(); + geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED + : D2D1_FIGURE_END_OPEN); +} + +std::unique_ptr D2DGeometryBuilder::Build() { + CheckValidation(); + ThrowIfFailed(geometry_sink_->Close()); + geometry_sink_ = nullptr; + auto geometry = + std::make_unique(GetDirectFactory(), std::move(geometry_)); + geometry_ = nullptr; + return geometry; +} + +D2DGeometry::D2DGeometry(DirectGraphFactory* factory, + Microsoft::WRL::ComPtr geometry) + : DirectGraphResource(factory), geometry_(std::move(geometry)) {} + +bool D2DGeometry::FillContains(const Point& point) { + BOOL result; + ThrowIfFailed(geometry_->FillContainsPoint( + Convert(point), D2D1::Matrix3x2F::Identity(), &result)); + return result != 0; +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Painter.cpp b/src/win/graphics/direct/Painter.cpp new file mode 100644 index 00000000..91392ba7 --- /dev/null +++ b/src/win/graphics/direct/Painter.cpp @@ -0,0 +1,104 @@ +#include "cru/win/graphics/direct/Painter.hpp" + +#include "cru/platform/Check.hpp" +#include "cru/win/graphics/direct/Brush.hpp" +#include "cru/win/graphics/direct/ConvertUtil.hpp" +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Geometry.hpp" +#include "cru/win/graphics/direct/TextLayout.hpp" + +#include + +namespace cru::platform::graphics::win::direct { +D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) { + Expects(render_target); + render_target_ = render_target; +} + +platform::Matrix D2DPainter::GetTransform() { + CheckValidation(); + D2D1_MATRIX_3X2_F m; + render_target_->GetTransform(&m); + return Convert(m); +} + +void D2DPainter::SetTransform(const platform::Matrix& matrix) { + CheckValidation(); + render_target_->SetTransform(Convert(matrix)); +} + +void D2DPainter::Clear(const Color& color) { + CheckValidation(); + render_target_->Clear(Convert(color)); +} + +void D2DPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush, + float width) { + CheckValidation(); + const auto b = CheckPlatform(brush, GetPlatformId()); + render_target_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(), + width); +} + +void D2DPainter::FillRectangle(const Rect& rectangle, IBrush* brush) { + CheckValidation(); + const auto b = CheckPlatform(brush, GetPlatformId()); + render_target_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface()); +} + +void D2DPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush, + float width) { + CheckValidation(); + const auto g = CheckPlatform(geometry, GetPlatformId()); + const auto b = CheckPlatform(brush, GetPlatformId()); + render_target_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(), + width); +} + +void D2DPainter::FillGeometry(IGeometry* geometry, IBrush* brush) { + CheckValidation(); + const auto g = CheckPlatform(geometry, GetPlatformId()); + const auto b = CheckPlatform(brush, GetPlatformId()); + render_target_->FillGeometry(g->GetComInterface(), b->GetD2DBrushInterface()); +} + +void D2DPainter::DrawText(const Point& offset, ITextLayout* text_layout, + IBrush* brush) { + CheckValidation(); + const auto t = CheckPlatform(text_layout, GetPlatformId()); + const auto b = CheckPlatform(brush, GetPlatformId()); + render_target_->DrawTextLayout(Convert(offset), t->GetComInterface(), + b->GetD2DBrushInterface()); +} + +void D2DPainter::PushLayer(const Rect& bounds) { + CheckValidation(); + + Microsoft::WRL::ComPtr layer; + ThrowIfFailed(render_target_->CreateLayer(&layer)); + + render_target_->PushLayer(D2D1::LayerParameters(Convert(bounds)), + layer.Get()); + + layers_.push_back(std::move(layer)); +} + +void D2DPainter::PopLayer() { + render_target_->PopLayer(); + layers_.pop_back(); +} + +void D2DPainter::EndDraw() { + if (is_drawing_) { + is_drawing_ = false; + DoEndDraw(); + } +} + +void D2DPainter::CheckValidation() { + if (!is_drawing_) { + throw cru::platform::ReuseException( + "Can't do that on painter after end drawing."); + } +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Resource.cpp b/src/win/graphics/direct/Resource.cpp new file mode 100644 index 00000000..2b4a0772 --- /dev/null +++ b/src/win/graphics/direct/Resource.cpp @@ -0,0 +1,12 @@ +#include "cru/win/graphics/direct/Resource.hpp" + +#include "cru/win/graphics/direct/Factory.hpp" + +namespace cru::platform::graphics::win::direct { +DirectGraphResource::DirectGraphResource(DirectGraphFactory* factory) + : factory_(factory) { + Expects(factory); +} + +IGraphFactory* DirectGraphResource::GetGraphFactory() { return factory_; } +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/TextLayout.cpp b/src/win/graphics/direct/TextLayout.cpp new file mode 100644 index 00000000..0c6e797f --- /dev/null +++ b/src/win/graphics/direct/TextLayout.cpp @@ -0,0 +1,124 @@ +#include "cru/win/graphics/direct/TextLayout.hpp" + +#include "cru/common/Logger.hpp" +#include "cru/platform/Check.hpp" +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" +#include "cru/win/graphics/direct/Font.hpp" + +#include + +namespace cru::platform::graphics::win::direct { +DWriteTextLayout::DWriteTextLayout(DirectGraphFactory* factory, + std::shared_ptr font, + std::u16string text) + : DirectGraphResource(factory), text_(std::move(text)) { + Expects(font); + font_ = CheckPlatform(font, GetPlatformId()); + + ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout( + reinterpret_cast(text_.c_str()), + static_cast(text_.size()), font_->GetComInterface(), max_width_, + max_height_, &text_layout_)); +} + +DWriteTextLayout::~DWriteTextLayout() = default; + +std::u16string DWriteTextLayout::GetText() { return text_; } + +std::u16string_view DWriteTextLayout::GetTextView() { return text_; } + +void DWriteTextLayout::SetText(std::u16string new_text) { + text_.swap(new_text); + ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout( + reinterpret_cast(text_.c_str()), + static_cast(text_.size()), font_->GetComInterface(), max_width_, + max_height_, &text_layout_)); +} + +std::shared_ptr DWriteTextLayout::GetFont() { + return std::dynamic_pointer_cast(font_); +} + +void DWriteTextLayout::SetFont(std::shared_ptr font) { + font_ = CheckPlatform(font, GetPlatformId()); + ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout( + reinterpret_cast(text_.c_str()), + static_cast(text_.size()), font_->GetComInterface(), max_width_, + max_height_, &text_layout_)); +} + +void DWriteTextLayout::SetMaxWidth(float max_width) { + max_width_ = max_width; + ThrowIfFailed(text_layout_->SetMaxWidth(max_width_)); +} + +void DWriteTextLayout::SetMaxHeight(float max_height) { + max_height_ = max_height; + ThrowIfFailed(text_layout_->SetMaxHeight(max_height_)); +} + +Rect DWriteTextLayout::GetTextBounds() { + DWRITE_TEXT_METRICS metrics; + ThrowIfFailed(text_layout_->GetMetrics(&metrics)); + return Rect{metrics.left, metrics.top, metrics.width, metrics.height}; +} + +std::vector DWriteTextLayout::TextRangeRect( + const TextRange& text_range_arg) { + if (text_range_arg.count == 0) { + return {}; + } + const auto text_range = text_range_arg.Normalize(); + + DWRITE_TEXT_METRICS text_metrics; + ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); + const auto metrics_count = + text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; + + std::vector hit_test_metrics(metrics_count); + UINT32 actual_count; + ThrowIfFailed(text_layout_->HitTestTextRange( + static_cast(text_range.position), + static_cast(text_range.count), 0, 0, hit_test_metrics.data(), + metrics_count, &actual_count)); + + hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, + hit_test_metrics.cend()); + + std::vector result; + result.reserve(actual_count); + + for (const auto& metrics : hit_test_metrics) { + result.push_back( + Rect{metrics.left, metrics.top, metrics.width, metrics.height}); + } + + return result; +} + +Point DWriteTextLayout::TextSinglePoint(Index position, bool trailing) { + DWRITE_HIT_TEST_METRICS metrics; + FLOAT left; + FLOAT top; + ThrowIfFailed(text_layout_->HitTestTextPosition(static_cast(position), + static_cast(trailing), + &left, &top, &metrics)); + return Point{left, top}; +} + +TextHitTestResult DWriteTextLayout::HitTest(const Point& point) { + BOOL trailing; + BOOL inside; + DWRITE_HIT_TEST_METRICS metrics; + + ThrowIfFailed(text_layout_->HitTestPoint(point.x, point.y, &trailing, &inside, + &metrics)); + + TextHitTestResult result; + result.position = metrics.textPosition; + result.trailing = trailing != 0; + result.insideText = inside != 0; + return result; +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/WindowPainter.cpp b/src/win/graphics/direct/WindowPainter.cpp new file mode 100644 index 00000000..c88667b6 --- /dev/null +++ b/src/win/graphics/direct/WindowPainter.cpp @@ -0,0 +1,20 @@ +#include "cru/win/graphics/direct/WindowPainter.hpp" + +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" +#include "cru/win/graphics/direct/WindowRenderTarget.hpp" + +namespace cru::platform::graphics::win::direct { +D2DWindowPainter::D2DWindowPainter(D2DWindowRenderTarget* render_target) + : D2DPainter(render_target->GetD2D1DeviceContext()), + render_target_(render_target) { + render_target_->GetD2D1DeviceContext()->BeginDraw(); +} + +D2DWindowPainter::~D2DWindowPainter() { EndDraw(); } + +void D2DWindowPainter::DoEndDraw() { + ThrowIfFailed(render_target_->GetD2D1DeviceContext()->EndDraw()); + render_target_->Present(); +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/WindowRenderTarget.cpp b/src/win/graphics/direct/WindowRenderTarget.cpp new file mode 100644 index 00000000..7479ae24 --- /dev/null +++ b/src/win/graphics/direct/WindowRenderTarget.cpp @@ -0,0 +1,81 @@ +#include "cru/win/graphics/direct/WindowRenderTarget.hpp" + +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" + +namespace cru::platform::graphics::win::direct { +D2DWindowRenderTarget::D2DWindowRenderTarget( + gsl::not_null factory, HWND hwnd) + : factory_(factory), hwnd_(hwnd) { + const auto d3d11_device = factory->GetD3D11Device(); + const auto dxgi_factory = factory->GetDxgiFactory(); + + d2d1_device_context_ = factory->CreateD2D1DeviceContext(); + d2d1_device_context_->SetUnitMode(D2D1_UNIT_MODE_DIPS); + + // Allocate a descriptor. + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc; + swap_chain_desc.Width = 0; // use automatic sizing + swap_chain_desc.Height = 0; + swap_chain_desc.Format = + DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format + swap_chain_desc.Stereo = false; + swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling + swap_chain_desc.SampleDesc.Quality = 0; + swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + swap_chain_desc.BufferCount = 2; // use double buffering to enable flip + swap_chain_desc.Scaling = DXGI_SCALING_NONE; + swap_chain_desc.SwapEffect = + DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect + swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED; + swap_chain_desc.Flags = 0; + + // Get the final swap chain for this window from the DXGI factory. + ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd( + d3d11_device, hwnd, &swap_chain_desc, nullptr, nullptr, + &dxgi_swap_chain_)); + + CreateTargetBitmap(); +} + +void D2DWindowRenderTarget::SetDpi(float x, float y) { + d2d1_device_context_->SetDpi(x, y); +} + +void D2DWindowRenderTarget::ResizeBuffer(const int width, const int height) { + // In order to resize buffer, we need to untarget the buffer first. + d2d1_device_context_->SetTarget(nullptr); + target_bitmap_ = nullptr; + ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, + DXGI_FORMAT_UNKNOWN, 0)); + CreateTargetBitmap(); +} + +void D2DWindowRenderTarget::Present() { + ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); +} + +void D2DWindowRenderTarget::CreateTargetBitmap() { + Expects(target_bitmap_ == nullptr); // target bitmap must not exist. + + // Direct2D needs the dxgi version of the backbuffer surface pointer. + Microsoft::WRL::ComPtr dxgi_back_buffer; + ThrowIfFailed( + dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); + + float dpi_x, dpi_y; + d2d1_device_context_->GetDpi(&dpi_x, &dpi_y); + + auto bitmap_properties = D2D1::BitmapProperties1( + D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), + dpi_x, dpi_y); + + // Get a D2D surface from the DXGI back buffer to use as the D2D render + // target. + ThrowIfFailed(d2d1_device_context_->CreateBitmapFromDxgiSurface( + dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); + + d2d1_device_context_->SetTarget(target_bitmap_.Get()); +} +} // namespace cru::platform::graphics::win::direct diff --git a/src/win/gui/CMakeLists.txt b/src/win/gui/CMakeLists.txt new file mode 100644 index 00000000..48bed00d --- /dev/null +++ b/src/win/gui/CMakeLists.txt @@ -0,0 +1,31 @@ +set(CRU_WIN_GUI_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/gui) + +add_library(cru_win_gui STATIC + TimerManager.hpp + WindowManager.hpp + + Cursor.cpp + GodWindow.cpp + InputMethod.cpp + Keyboard.cpp + TimerManager.cpp + UiApplication.cpp + Window.cpp + WindowClass.cpp + WindowManager.cpp +) +target_sources(cru_win_gui PUBLIC + ${CRU_WIN_GUI_INCLUDE_DIR}/Cursor.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/Exception.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/Base.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/GodWindow.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/InputMethod.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/Keyboard.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/Resource.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/UiApplication.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/Window.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/WindowClass.hpp + ${CRU_WIN_GUI_INCLUDE_DIR}/WindowNativeMessageEventArgs.hpp +) +target_link_libraries(cru_win_gui PUBLIC imm32) +target_link_libraries(cru_win_gui PUBLIC cru_win_graphics_direct cru_platform_gui) diff --git a/src/win/gui/Cursor.cpp b/src/win/gui/Cursor.cpp new file mode 100644 index 00000000..5f3086fa --- /dev/null +++ b/src/win/gui/Cursor.cpp @@ -0,0 +1,51 @@ +#include "cru/win/gui/Cursor.hpp" + +#include "cru/common/Logger.hpp" +#include "cru/win/gui/Exception.hpp" + +#include + +namespace cru::platform::gui::win { +WinCursor::WinCursor(HCURSOR handle, bool auto_destroy) { + handle_ = handle; + auto_destroy_ = auto_destroy; +} + +WinCursor::~WinCursor() { + if (auto_destroy_) { + if (!::DestroyCursor(handle_)) { + // This is not a fetal error but might still need notice because it may + // cause leak. + log::TagWarn(log_tag, u"Failed to destroy a cursor. Last error code: {}", + ::GetLastError()); + } + } +} + +namespace { +WinCursor* LoadWinCursor(const wchar_t* name) { + const auto handle = static_cast(::LoadImageW( + NULL, name, IMAGE_CURSOR, SM_CYCURSOR, SM_CYCURSOR, LR_SHARED)); + if (handle == NULL) { + throw Win32Error(::GetLastError(), "Failed to load system cursor."); + } + return new WinCursor(handle, false); +} +} // namespace + +WinCursorManager::WinCursorManager() + : sys_arrow_(LoadWinCursor(IDC_ARROW)), + sys_hand_(LoadWinCursor(IDC_HAND)) {} + +std::shared_ptr WinCursorManager::GetSystemWinCursor( + SystemCursorType type) { + switch (type) { + case SystemCursorType::Arrow: + return sys_arrow_; + case SystemCursorType::Hand: + return sys_hand_; + default: + throw std::runtime_error("Unknown system cursor value."); + } +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/GodWindow.cpp b/src/win/gui/GodWindow.cpp new file mode 100644 index 00000000..7bce83a3 --- /dev/null +++ b/src/win/gui/GodWindow.cpp @@ -0,0 +1,63 @@ +#include "cru/win/gui/GodWindow.hpp" + +#include "cru/common/Logger.hpp" +#include "cru/win/gui/Exception.hpp" +#include "cru/win/gui/UiApplication.hpp" +#include "cru/win/gui/WindowClass.hpp" + +namespace cru::platform::gui::win { +constexpr auto god_window_class_name = L"GodWindowClass"; + +LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, + LPARAM lParam) { + const auto app = WinUiApplication::GetInstance(); + + if (app) { + LRESULT result; + auto god_window = app->GetGodWindow(); + if (god_window != nullptr) { + const auto handled = god_window->HandleGodWindowMessage( + hWnd, uMsg, wParam, lParam, &result); + if (handled) return result; + } + } + return DefWindowProcW(hWnd, uMsg, wParam, lParam); +} + +GodWindow::GodWindow(WinUiApplication* application) { + application_ = application; + + const auto h_instance = application->GetInstanceHandle(); + + god_window_class_ = std::make_unique(god_window_class_name, + GodWndProc, h_instance); + + hwnd_ = CreateWindowEx(0, god_window_class_name, L"", 0, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + HWND_MESSAGE, nullptr, h_instance, nullptr); + + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create god window."); +} + +GodWindow::~GodWindow() { + if (!::DestroyWindow(hwnd_)) { + // Although this could be "safely" ignore. + log::TagWarn(log_tag, u"Failed to destroy god window."); + } +} + +bool GodWindow::HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result) { + WindowNativeMessageEventArgs args( + WindowNativeMessage{hwnd, msg, w_param, l_param}); + message_event_.Raise(args); + + if (args.IsHandled()) { + *result = args.GetResult(); + return true; + } + + return false; +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/InputMethod.cpp b/src/win/gui/InputMethod.cpp new file mode 100644 index 00000000..d6f2146d --- /dev/null +++ b/src/win/gui/InputMethod.cpp @@ -0,0 +1,278 @@ +#include "cru/win/gui/InputMethod.hpp" + +#include "cru/common/Logger.hpp" +#include "cru/common/StringUtil.hpp" +#include "cru/platform/Check.hpp" +#include "cru/win/Exception.hpp" +#include "cru/win/gui/Window.hpp" + +#include + +namespace cru::platform::gui::win { +AutoHIMC::AutoHIMC(HWND hwnd) : hwnd_(hwnd) { + Expects(hwnd); + handle_ = ::ImmGetContext(hwnd); +} + +AutoHIMC::AutoHIMC(AutoHIMC&& other) + : hwnd_(other.hwnd_), handle_(other.handle_) { + other.hwnd_ = nullptr; + other.handle_ = nullptr; +} + +AutoHIMC& AutoHIMC::operator=(AutoHIMC&& other) { + if (this != &other) { + Object::operator=(std::move(other)); + this->hwnd_ = other.hwnd_; + this->handle_ = other.handle_; + other.hwnd_ = nullptr; + other.handle_ = nullptr; + } + return *this; +} + +AutoHIMC::~AutoHIMC() { + if (handle_) { + if (!::ImmReleaseContext(hwnd_, handle_)) + log::TagWarn(log_tag, u"Failed to release HIMC."); + } +} + +// copied from chromium +namespace { +// Determines whether or not the given attribute represents a target +// (a.k.a. a selection). +bool IsTargetAttribute(char attribute) { + return (attribute == ATTR_TARGET_CONVERTED || + attribute == ATTR_TARGET_NOTCONVERTED); +} +// Helper function for ImeInput::GetCompositionInfo() method, to get the target +// range that's selected by the user in the current composition string. +void GetCompositionTargetRange(HIMC imm_context, int* target_start, + int* target_end) { + int attribute_size = + ::ImmGetCompositionString(imm_context, GCS_COMPATTR, NULL, 0); + if (attribute_size > 0) { + int start = 0; + int end = 0; + std::vector attribute_data(attribute_size); + ::ImmGetCompositionString(imm_context, GCS_COMPATTR, attribute_data.data(), + attribute_size); + for (start = 0; start < attribute_size; ++start) { + if (IsTargetAttribute(attribute_data[start])) break; + } + for (end = start; end < attribute_size; ++end) { + if (!IsTargetAttribute(attribute_data[end])) break; + } + if (start == attribute_size) { + // This composition clause does not contain any target clauses, + // i.e. this clauses is an input clause. + // We treat the whole composition as a target clause. + start = 0; + end = attribute_size; + } + *target_start = start; + *target_end = end; + } +} +// Helper function for ImeInput::GetCompositionInfo() method, to get underlines +// information of the current composition string. +CompositionClauses GetCompositionClauses(HIMC imm_context, int target_start, + int target_end) { + CompositionClauses result; + int clause_size = + ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE, NULL, 0); + int clause_length = clause_size / sizeof(std::uint32_t); + if (clause_length) { + result.reserve(clause_length - 1); + std::vector clause_data(clause_length); + ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE, clause_data.data(), + clause_size); + for (int i = 0; i < clause_length - 1; ++i) { + CompositionClause clause; + clause.start = clause_data[i]; + clause.end = clause_data[i + 1]; + clause.target = false; + // Use thick underline for the target clause. + if (clause.start >= target_start && clause.end <= target_end) { + clause.target = true; + } + result.push_back(clause); + } + } + return result; +} + +std::u16string GetString(HIMC imm_context) { + LONG string_size = + ::ImmGetCompositionString(imm_context, GCS_COMPSTR, NULL, 0); + std::u16string result((string_size / sizeof(char16_t)), 0); + ::ImmGetCompositionString(imm_context, GCS_COMPSTR, result.data(), + string_size); + return result; +} + +std::u16string GetResultString(HIMC imm_context) { + LONG string_size = + ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, NULL, 0); + std::u16string result((string_size / sizeof(char16_t)), 0); + ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, result.data(), + string_size); + return result; +} + +CompositionText GetCompositionInfo(HIMC imm_context) { + // We only care about GCS_COMPATTR, GCS_COMPCLAUSE and GCS_CURSORPOS, and + // convert them into underlines and selection range respectively. + + auto text = GetString(imm_context); + + int length = static_cast(text.length()); + // Find out the range selected by the user. + int target_start = length; + int target_end = length; + GetCompositionTargetRange(imm_context, &target_start, &target_end); + + auto clauses = GetCompositionClauses(imm_context, target_start, target_end); + + int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0); + + return CompositionText{std::move(text), std::move(clauses), + TextRange{cursor}}; +} + +} // namespace + +WinInputMethodContext::WinInputMethodContext( + gsl::not_null window) + : native_window_(window) { + event_guard_ += window->NativeMessageEvent()->AddHandler( + std::bind(&WinInputMethodContext::OnWindowNativeMessage, this, + std::placeholders::_1)); +} + +WinInputMethodContext::~WinInputMethodContext() {} + +void WinInputMethodContext::EnableIME() { + const auto hwnd = native_window_->GetWindowHandle(); + if (::ImmAssociateContextEx(hwnd, nullptr, IACE_DEFAULT) == FALSE) { + log::TagWarn(log_tag, u"Failed to enable ime."); + } +} + +void WinInputMethodContext::DisableIME() { + const auto hwnd = native_window_->GetWindowHandle(); + AutoHIMC himc{hwnd}; + + if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { + log::TagWarn(log_tag, + u"Failed to complete composition before disable ime."); + } + + if (::ImmAssociateContextEx(hwnd, nullptr, 0) == FALSE) { + log::TagWarn(log_tag, u"Failed to disable ime."); + } +} + +void WinInputMethodContext::CompleteComposition() { + auto himc = GetHIMC(); + if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { + log::TagWarn(log_tag, u"Failed to complete composition."); + } +} + +void WinInputMethodContext::CancelComposition() { + auto himc = GetHIMC(); + if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0)) { + log::TagWarn(log_tag, u"Failed to complete composition."); + } +} + +CompositionText WinInputMethodContext::GetCompositionText() { + auto himc = GetHIMC(); + return GetCompositionInfo(himc.Get()); +} + +void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) { + auto himc = GetHIMC(); + + ::CANDIDATEFORM form; + form.dwIndex = 1; + form.dwStyle = CFS_CANDIDATEPOS; + + form.ptCurrentPos = native_window_->DipToPixel(point); + + if (!::ImmSetCandidateWindow(himc.Get(), &form)) + log::TagDebug(log_tag, + u"Failed to set input method candidate window position."); +} + +IEvent* WinInputMethodContext::CompositionStartEvent() { + return &composition_start_event_; +} + +IEvent* WinInputMethodContext::CompositionEndEvent() { + return &composition_end_event_; +}; + +IEvent* WinInputMethodContext::CompositionEvent() { + return &composition_event_; +} + +IEvent* WinInputMethodContext::TextEvent() { + return &text_event_; +} + +void WinInputMethodContext::OnWindowNativeMessage( + WindowNativeMessageEventArgs& args) { + const auto& message = args.GetWindowMessage(); + switch (message.msg) { + case WM_CHAR: { + const auto c = static_cast(message.w_param); + if (IsUtf16SurrogatePairCodeUnit(c)) { + // I don't think this will happen because normal key strike without ime + // should only trigger ascci character. If it is a charater from + // supplementary planes, it should be handled with ime messages. + log::TagWarn(log_tag, + u"A WM_CHAR message for character from supplementary " + u"planes is ignored."); + } else { + char16_t s[1] = {c}; + text_event_.Raise({s, 1}); + } + args.HandleWithResult(0); + break; + } + case WM_IME_COMPOSITION: { + composition_event_.Raise(nullptr); + auto composition_text = GetCompositionText(); + log::TagDebug(log_tag, u"WM_IME_COMPOSITION composition text:\n{}", + composition_text); + if (message.l_param & GCS_RESULTSTR) { + auto result_string = GetResultString(); + text_event_.Raise(result_string); + } + break; + } + case WM_IME_STARTCOMPOSITION: { + composition_start_event_.Raise(nullptr); + break; + } + case WM_IME_ENDCOMPOSITION: { + composition_end_event_.Raise(nullptr); + break; + } + } +} + +std::u16string WinInputMethodContext::GetResultString() { + auto himc = GetHIMC(); + auto result = win::GetResultString(himc.Get()); + return result; +} + +AutoHIMC WinInputMethodContext::GetHIMC() { + const auto hwnd = native_window_->GetWindowHandle(); + return AutoHIMC{hwnd}; +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/Keyboard.cpp b/src/win/gui/Keyboard.cpp new file mode 100644 index 00000000..b706b240 --- /dev/null +++ b/src/win/gui/Keyboard.cpp @@ -0,0 +1,74 @@ +#include "cru/win/gui/Keyboard.hpp" + +namespace cru::platform::gui::win { +KeyCode VirtualKeyToKeyCode(int virtual_key) { + if (virtual_key >= 0x30 && virtual_key <= 0x39) { + return KeyCode{static_cast(KeyCode::N0) + (virtual_key - 0x30)}; + } else if (virtual_key >= 0x41 && virtual_key <= 0x5a) { + return KeyCode{static_cast(KeyCode::A) + (virtual_key - 0x41)}; + } else if (virtual_key >= VK_NUMPAD0 && virtual_key <= VK_NUMPAD9) { + return KeyCode{static_cast(KeyCode::NumPad0) + + (virtual_key - VK_NUMPAD0)}; + } else if (virtual_key >= VK_F1 && virtual_key <= VK_F12) { + return KeyCode{static_cast(KeyCode::F1) + (virtual_key - VK_F1)}; + } else { + switch (virtual_key) { +#define CRU_MAP_KEY(virtual_key, keycode) \ + case virtual_key: \ + return KeyCode::keycode; + + CRU_MAP_KEY(VK_LBUTTON, LeftButton) + CRU_MAP_KEY(VK_MBUTTON, MiddleButton) + CRU_MAP_KEY(VK_RBUTTON, RightButton) + CRU_MAP_KEY(VK_ESCAPE, Escape) + CRU_MAP_KEY(VK_OEM_3, GraveAccent) + CRU_MAP_KEY(VK_TAB, Tab) + CRU_MAP_KEY(VK_CAPITAL, CapsLock) + CRU_MAP_KEY(VK_LSHIFT, LeftShift) + CRU_MAP_KEY(VK_LCONTROL, LeftCtrl) + CRU_MAP_KEY(VK_LWIN, LeftSuper) + CRU_MAP_KEY(VK_LMENU, LeftAlt) + CRU_MAP_KEY(VK_OEM_MINUS, Minus) + CRU_MAP_KEY(VK_OEM_PLUS, Equal) + CRU_MAP_KEY(VK_BACK, Backspace) + CRU_MAP_KEY(VK_OEM_4, LeftSquareBracket) + CRU_MAP_KEY(VK_OEM_6, RightSquareBracket) + CRU_MAP_KEY(VK_OEM_5, BackSlash) + CRU_MAP_KEY(VK_OEM_1, Semicolon) + CRU_MAP_KEY(VK_OEM_7, Quote) + CRU_MAP_KEY(VK_OEM_COMMA, Comma) + CRU_MAP_KEY(VK_OEM_PERIOD, Period) + CRU_MAP_KEY(VK_OEM_2, Slash) + CRU_MAP_KEY(VK_RSHIFT, RightShift) + CRU_MAP_KEY(VK_RCONTROL, RightCtrl) + CRU_MAP_KEY(VK_RWIN, RightSuper) + CRU_MAP_KEY(VK_RMENU, RightAlt) + CRU_MAP_KEY(VK_INSERT, Insert) + CRU_MAP_KEY(VK_DELETE, Delete) + CRU_MAP_KEY(VK_HOME, Home) + CRU_MAP_KEY(VK_END, End) + CRU_MAP_KEY(VK_PRIOR, PageUp) + CRU_MAP_KEY(VK_NEXT, PageDown) + CRU_MAP_KEY(VK_UP, Up) + CRU_MAP_KEY(VK_LEFT, Left) + CRU_MAP_KEY(VK_DOWN, Down) + CRU_MAP_KEY(VK_RIGHT, Right) + CRU_MAP_KEY(VK_SNAPSHOT, PrintScreen) + CRU_MAP_KEY(VK_PAUSE, Pause) + +#undef CRU_MAP_KEY + + default: + return KeyCode::Unknown; + } + } +} + +KeyModifier RetrieveKeyMofifier() { + KeyModifier result{0}; + if (::GetKeyState(VK_SHIFT) < 0) result |= KeyModifiers::shift; + if (::GetKeyState(VK_CONTROL) < 0) result |= KeyModifiers::ctrl; + if (::GetKeyState(VK_MENU) < 0) result |= KeyModifiers::alt; + return result; +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/TimerManager.cpp b/src/win/gui/TimerManager.cpp new file mode 100644 index 00000000..fc26b6c4 --- /dev/null +++ b/src/win/gui/TimerManager.cpp @@ -0,0 +1,100 @@ +#include "TimerManager.hpp" + +#include "cru/win/gui/Base.hpp" +#include "cru/win/gui/Exception.hpp" +#include "gsl/gsl_util" + +#include +#include + +namespace cru::platform::gui::win { +constexpr int kSetImmediateWindowMessageId = WM_USER + 2000; + +TimerManager::TimerManager(GodWindow* god_window) { + god_window_ = god_window; + event_guard_ += god_window->MessageEvent()->AddHandler(std::bind( + &TimerManager::HandleGodWindowMessage, this, std::placeholders::_1)); +} + +long long TimerManager::SetTimer(TimerType type, int period, + std::function action) { + auto id = next_id_++; + TimerInfo timer_info{id, type, type == TimerType::Immediate ? 0 : period, + std::move(action)}; + if (type == TimerType::Immediate) { + if (!::PostMessageW(god_window_->GetHandle(), kSetImmediateWindowMessageId, + gsl::narrow(id), 0)) { + throw Win32Error( + ::GetLastError(), + "Failed to post window message to god window for set immediate."); + } + } else { + CreateNativeTimer(&timer_info); + } + + info_map_.emplace(id, std::move(timer_info)); + return id; +} + +void TimerManager::CancelTimer(long long id) { + if (id <= 0) return; + auto find_result = this->info_map_.find(id); + if (find_result != info_map_.cend()) { + auto& info = find_result->second; + KillNativeTimer(&info); + this->info_map_.erase(find_result); + } +} + +void TimerManager::CreateNativeTimer(TimerInfo* info) { + info->native_timer_id = gsl::narrow(info->id); + ::SetTimer(god_window_->GetHandle(), info->native_timer_id, info->period, + nullptr); +} + +void TimerManager::KillNativeTimer(TimerInfo* info) { + if (info->native_timer_id == 0) return; + ::KillTimer(god_window_->GetHandle(), info->native_timer_id); + info->native_timer_id = 0; +} + +void TimerManager::HandleGodWindowMessage(WindowNativeMessageEventArgs& args) { + const auto& message = args.GetWindowMessage(); + + switch (message.msg) { + case kSetImmediateWindowMessageId: { + auto find_result = + this->info_map_.find(static_cast(message.w_param)); + if (find_result != info_map_.cend()) { + auto& info = find_result->second; + info.action(); + info_map_.erase(find_result); + } + args.SetResult(0); + args.SetHandled(true); + return; + } + case WM_TIMER: { + auto find_result = + this->info_map_.find(static_cast(message.w_param)); + if (find_result != info_map_.cend()) { + auto& info = find_result->second; + if (info.type == TimerType::Interval) { + info.action(); + args.SetResult(0); + args.SetHandled(true); + } else if (info.type == TimerType::Timeout) { + info.action(); + KillNativeTimer(&info); + info_map_.erase(find_result); + args.SetResult(0); + args.SetHandled(true); + } + } + return; + } + default: + return; + } +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/TimerManager.hpp b/src/win/gui/TimerManager.hpp new file mode 100644 index 00000000..a8db1075 --- /dev/null +++ b/src/win/gui/TimerManager.hpp @@ -0,0 +1,61 @@ +#pragma once +#include "cru/common/Event.hpp" +#include "cru/win/WinPreConfig.hpp" + +#include "cru/common/Base.hpp" +#include "cru/win/gui/GodWindow.hpp" +#include "cru/win/gui/WindowNativeMessageEventArgs.hpp" + +#include +#include +#include +#include + +namespace cru::platform::gui::win { +enum class TimerType { Immediate, Timeout, Interval }; + +struct TimerInfo { + TimerInfo(long long id, TimerType type, int period, + std::function action, UINT_PTR native_timer_id = 0) + : id(id), + type(type), + period(period), + action(std::move(action)), + native_timer_id(native_timer_id) {} + + long long id; + TimerType type; + int period; // in milliseconds + std::function action; + UINT_PTR native_timer_id; +}; + +class TimerManager : public Object { + public: + TimerManager(GodWindow* god_window); + + CRU_DELETE_COPY(TimerManager) + CRU_DELETE_MOVE(TimerManager) + + ~TimerManager() override = default; + + // Period is in milliseconds. When type is immediate, it is not checked and + // used. + long long SetTimer(TimerType type, int period, std::function action); + void CancelTimer(long long id); + + private: + void HandleGodWindowMessage(WindowNativeMessageEventArgs& args); + + void CreateNativeTimer(TimerInfo* info); + void KillNativeTimer(TimerInfo* info); + + private: + GodWindow* god_window_; + + EventRevokerListGuard event_guard_; + + long long next_id_ = 1; + std::unordered_map info_map_; +}; +} // namespace cru::platform::gui::win diff --git a/src/win/gui/UiApplication.cpp b/src/win/gui/UiApplication.cpp new file mode 100644 index 00000000..5041a6c0 --- /dev/null +++ b/src/win/gui/UiApplication.cpp @@ -0,0 +1,118 @@ +#include "cru/win/gui/UiApplication.hpp" + +#include "../DebugLogger.hpp" +#include "../StdOutLogger.hpp" +#include "TimerManager.hpp" +#include "WindowManager.hpp" +#include "cru/common/Logger.hpp" +#include "cru/platform/Check.hpp" +#include "cru/win/graphics/direct/Factory.hpp" +#include "cru/win/gui/Cursor.hpp" +#include "cru/win/gui/Exception.hpp" +#include "cru/win/gui/GodWindow.hpp" +#include "cru/win/gui/InputMethod.hpp" +#include "cru/win/gui/Window.hpp" + +namespace cru::platform::gui { +std::unique_ptr CreateUiApplication() { + return std::make_unique(); +} +} // namespace cru::platform::gui + +namespace cru::platform::gui::win { +WinUiApplication* WinUiApplication::instance = nullptr; + +WinUiApplication::WinUiApplication() { + instance = this; + + instance_handle_ = ::GetModuleHandleW(nullptr); + if (!instance_handle_) + throw Win32Error("Failed to get module(instance) handle."); + + ::SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE); + + log::Logger::GetInstance()->AddSource( + std::make_unique<::cru::platform::win::WinDebugLoggerSource>()); + log::Logger::GetInstance()->AddSource( + std::make_unique<::cru::platform::win::WinStdOutLoggerSource>()); + + graph_factory_ = + std::make_unique(); + + god_window_ = std::make_unique(this); + timer_manager_ = std::make_unique(god_window_.get()); + window_manager_ = std::make_unique(this); + cursor_manager_ = std::make_unique(); +} + +WinUiApplication::~WinUiApplication() { instance = nullptr; } + +int WinUiApplication::Run() { + MSG msg; + while (GetMessageW(&msg, nullptr, 0, 0)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + + for (const auto& handler : quit_handlers_) handler(); + + return static_cast(msg.wParam); +} + +void WinUiApplication::RequestQuit(const int quit_code) { + ::PostQuitMessage(quit_code); +} + +void WinUiApplication::AddOnQuitHandler(std::function handler) { + quit_handlers_.push_back(std::move(handler)); +} + +long long WinUiApplication::SetImmediate(std::function action) { + return this->timer_manager_->SetTimer(TimerType::Immediate, 0, + std::move(action)); +} + +long long WinUiApplication::SetTimeout(std::chrono::milliseconds milliseconds, + std::function action) { + return this->timer_manager_->SetTimer(TimerType::Timeout, + gsl::narrow(milliseconds.count()), + std::move(action)); +} + +long long WinUiApplication::SetInterval(std::chrono::milliseconds milliseconds, + std::function action) { + return this->timer_manager_->SetTimer(TimerType::Interval, + gsl::narrow(milliseconds.count()), + std::move(action)); +} + +void WinUiApplication::CancelTimer(long long id) { + timer_manager_->CancelTimer(id); +} + +std::vector WinUiApplication::GetAllWindow() { + const auto&& windows = window_manager_->GetAllWindows(); + std::vector result; + for (const auto w : windows) { + result.push_back(static_cast(w)); + } + return result; +} + +INativeWindow* WinUiApplication::CreateWindow(INativeWindow* parent) { + WinNativeWindow* p = nullptr; + if (parent != nullptr) { + p = CheckPlatform(parent, GetPlatformId()); + } + return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), + WS_OVERLAPPEDWINDOW, p); +} + +cru::platform::graphics::IGraphFactory* WinUiApplication::GetGraphFactory() { + return graph_factory_.get(); +} + +ICursorManager* WinUiApplication::GetCursorManager() { + return cursor_manager_.get(); +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/Window.cpp b/src/win/gui/Window.cpp new file mode 100644 index 00000000..dda8a36a --- /dev/null +++ b/src/win/gui/Window.cpp @@ -0,0 +1,453 @@ +#include "cru/win/gui/Window.hpp" + +#include "WindowManager.hpp" +#include "cru/common/Logger.hpp" +#include "cru/platform/Check.hpp" +#include "cru/platform/gui/Base.hpp" +#include "cru/win/graphics/direct/WindowPainter.hpp" +#include "cru/win/gui/Cursor.hpp" +#include "cru/win/gui/Exception.hpp" +#include "cru/win/gui/InputMethod.hpp" +#include "cru/win/gui/Keyboard.hpp" +#include "cru/win/gui/UiApplication.hpp" +#include "cru/win/gui/WindowClass.hpp" + +#include +#include +#include + +namespace cru::platform::gui::win { +WinNativeWindow::WinNativeWindow(WinUiApplication* application, + WindowClass* window_class, DWORD window_style, + WinNativeWindow* parent) + : application_(application), parent_window_(parent) { + Expects(application); // application can't be null. + + if (parent != nullptr) { + throw new std::runtime_error("Can't use a invalid window as parent."); + } + + const auto window_manager = application->GetWindowManager(); + + hwnd_ = CreateWindowExW( + 0, window_class->GetName(), L"", window_style, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, + application->GetInstanceHandle(), nullptr); + + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create window."); + + auto dpi = ::GetDpiForWindow(hwnd_); + if (dpi == 0) + throw Win32Error(::GetLastError(), "Failed to get dpi of window."); + dpi_ = static_cast(dpi); + log::Debug(u"Dpi of window is {}.", dpi_); + + window_manager->RegisterWindow(hwnd_, this); + + SetCursor(application->GetCursorManager()->GetSystemCursor( + cru::platform::gui::SystemCursorType::Arrow)); + + window_render_target_ = + std::make_unique( + application->GetDirectFactory(), hwnd_); + window_render_target_->SetDpi(dpi_, dpi_); + + input_method_context_ = std::make_unique(this); + input_method_context_->DisableIME(); +} + +WinNativeWindow::~WinNativeWindow() { + if (!sync_flag_) { + sync_flag_ = true; + Close(); + } +} + +void WinNativeWindow::Close() { ::DestroyWindow(hwnd_); } + +bool WinNativeWindow::IsVisible() { return ::IsWindowVisible(hwnd_); } + +void WinNativeWindow::SetVisible(bool is_visible) { + is_visible ? ShowWindow(hwnd_, SW_SHOWNORMAL) : ShowWindow(hwnd_, SW_HIDE); +} +Size WinNativeWindow::GetClientSize() { + const auto pixel_rect = GetClientRectPixel(); + return Size(PixelToDip(pixel_rect.right), PixelToDip(pixel_rect.bottom)); +} + +void WinNativeWindow::SetClientSize(const Size& size) { + const auto window_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); + const auto window_ex_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); + + RECT rect; + rect.left = 0; + rect.top = 0; + rect.right = DipToPixel(size.width); + rect.bottom = DipToPixel(size.height); + if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style)) + throw Win32Error(::GetLastError(), "Failed to invoke AdjustWindowRectEx."); + + if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, + rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE)) + throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); +} + +Rect WinNativeWindow::GetWindowRect() { + RECT rect; + if (!::GetWindowRect(hwnd_, &rect)) + throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect."); + + return Rect::FromVertices(PixelToDip(rect.left), PixelToDip(rect.top), + PixelToDip(rect.right), PixelToDip(rect.bottom)); +} + +void WinNativeWindow::SetWindowRect(const Rect& rect) { + if (!SetWindowPos(hwnd_, nullptr, DipToPixel(rect.left), DipToPixel(rect.top), + DipToPixel(rect.GetRight()), DipToPixel(rect.GetBottom()), + SWP_NOZORDER)) + throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); +} + +Point WinNativeWindow::GetMousePosition() { + POINT p; + if (!::GetCursorPos(&p)) + throw Win32Error(::GetLastError(), "Failed to get cursor position."); + if (!::ScreenToClient(hwnd_, &p)) + throw Win32Error(::GetLastError(), "Failed to call ScreenToClient."); + return PixelToDip(p); +} + +bool WinNativeWindow::CaptureMouse() { + ::SetCapture(hwnd_); + return true; +} + +bool WinNativeWindow::ReleaseMouse() { + const auto result = ::ReleaseCapture(); + return result != 0; +} + +void WinNativeWindow::RequestRepaint() { + log::TagDebug(log_tag, u"A repaint is requested."); + if (!::InvalidateRect(hwnd_, nullptr, FALSE)) + throw Win32Error(::GetLastError(), "Failed to invalidate window."); + if (!::UpdateWindow(hwnd_)) + throw Win32Error(::GetLastError(), "Failed to update window."); +} + +std::unique_ptr WinNativeWindow::BeginPaint() { + return std::make_unique( + window_render_target_.get()); +} + +void WinNativeWindow::SetCursor(std::shared_ptr cursor) { + if (cursor == nullptr) { + throw std::runtime_error("Can't use a nullptr as cursor."); + } + + cursor_ = CheckPlatform(cursor, GetPlatformId()); + + if (!::SetClassLongPtrW(hwnd_, GCLP_HCURSOR, + reinterpret_cast(cursor_->GetHandle()))) { + log::TagWarn(log_tag, + u"Failed to set cursor because failed to set class long. Last " + u"error code: {}.", + ::GetLastError()); + return; + } + + if (!IsVisible()) return; + + auto lg = [](const std::u16string_view& reason) { + log::TagWarn( + log_tag, + u"Failed to set cursor because {} when window is visible. (We need to " + u"update cursor if it is inside the window.) Last error code: {}.", + reason, ::GetLastError()); + }; + + ::POINT point; + if (!::GetCursorPos(&point)) { + lg(u"failed to get cursor pos"); + return; + } + + ::RECT rect; + if (!::GetClientRect(hwnd_, &rect)) { + lg(u"failed to get window's client rect"); + return; + } + + ::POINT lefttop{rect.left, rect.top}; + ::POINT rightbottom{rect.right, rect.bottom}; + if (!::ClientToScreen(hwnd_, &lefttop)) { + lg(u"failed to call ClientToScreen on lefttop"); + return; + } + + if (!::ClientToScreen(hwnd_, &rightbottom)) { + lg(u"failed to call ClientToScreen on rightbottom"); + return; + } + + if (point.x >= lefttop.x && point.y >= lefttop.y && + point.x <= rightbottom.x && point.y <= rightbottom.y) { + ::SetCursor(cursor_->GetHandle()); + } +} + +IInputMethodContext* WinNativeWindow::GetInputMethodContext() { + return static_cast(input_method_context_.get()); +} + +bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, + WPARAM w_param, LPARAM l_param, + LRESULT* result) { + WindowNativeMessageEventArgs args{ + WindowNativeMessage{hwnd, msg, w_param, l_param}}; + native_message_event_.Raise(args); + if (args.IsHandled()) { + *result = args.GetResult(); + return true; + } + + switch (msg) { + case WM_PAINT: + OnPaintInternal(); + *result = 0; + return true; + case WM_ERASEBKGND: + *result = 1; + return true; + case WM_SETFOCUS: + OnSetFocusInternal(); + *result = 0; + return true; + case WM_KILLFOCUS: + OnKillFocusInternal(); + *result = 0; + return true; + case WM_MOUSEMOVE: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseMoveInternal(point); + *result = 0; + return true; + } + case WM_LBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(platform::gui::mouse_buttons::left, point); + *result = 0; + return true; + } + case WM_LBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(platform::gui::mouse_buttons::left, point); + *result = 0; + return true; + } + case WM_RBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(platform::gui::mouse_buttons::right, point); + *result = 0; + return true; + } + case WM_RBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(platform::gui::mouse_buttons::right, point); + *result = 0; + return true; + } + case WM_MBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(platform::gui::mouse_buttons::middle, point); + *result = 0; + return true; + } + case WM_MBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(platform::gui::mouse_buttons::middle, point); + *result = 0; + return true; + } + case WM_MOUSEWHEEL: + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + ScreenToClient(hwnd, &point); + OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); + *result = 0; + return true; + case WM_KEYDOWN: + OnKeyDownInternal(static_cast(w_param)); + *result = 0; + return true; + case WM_KEYUP: + OnKeyUpInternal(static_cast(w_param)); + *result = 0; + return true; + case WM_SYSKEYDOWN: + if (l_param & (1 << 29)) { + OnKeyDownInternal(static_cast(w_param)); + *result = 0; + return true; + } + return false; + case WM_SYSKEYUP: + if (l_param & (1 << 29)) { + OnKeyUpInternal(static_cast(w_param)); + *result = 0; + return true; + } + return false; + case WM_SIZE: + OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); + *result = 0; + return true; + case WM_ACTIVATE: + if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) + OnActivatedInternal(); + else if (w_param == WA_INACTIVE) + OnDeactivatedInternal(); + *result = 0; + return true; + case WM_DESTROY: + OnDestroyInternal(); + *result = 0; + return true; + case WM_IME_SETCONTEXT: + l_param &= ~ISC_SHOWUICOMPOSITIONWINDOW; + *result = ::DefWindowProcW(hwnd, msg, w_param, l_param); + return true; + // We must block these message from DefWindowProc or it will create + // an ugly composition window. + case WM_IME_STARTCOMPOSITION: + case WM_IME_COMPOSITION: + *result = 0; + return true; + case WM_DPICHANGED: { + dpi_ = static_cast(LOWORD(w_param)); + const RECT* suggest_rect = reinterpret_cast(l_param); + window_render_target_->SetDpi(dpi_, dpi_); + SetWindowPos(hwnd_, NULL, suggest_rect->left, suggest_rect->top, + suggest_rect->right - suggest_rect->left, + suggest_rect->bottom - suggest_rect->top, + SWP_NOZORDER | SWP_NOACTIVATE); + } + default: + return false; + } +} + +RECT WinNativeWindow::GetClientRectPixel() { + RECT rect; + if (!GetClientRect(hwnd_, &rect)) + throw Win32Error(::GetLastError(), "Failed to invoke GetClientRect."); + return rect; +} + +void WinNativeWindow::OnDestroyInternal() { + application_->GetWindowManager()->UnregisterWindow(hwnd_); + hwnd_ = nullptr; + destroy_event_.Raise(nullptr); + if (!sync_flag_) { + sync_flag_ = true; + delete this; + } +} + +void WinNativeWindow::OnPaintInternal() { + paint_event_.Raise(nullptr); + ValidateRect(hwnd_, nullptr); + log::TagDebug(log_tag, u"A repaint is finished."); +} + +void WinNativeWindow::OnResizeInternal(const int new_width, + const int new_height) { + if (!(new_width == 0 && new_height == 0)) { + window_render_target_->ResizeBuffer(new_width, new_height); + resize_event_.Raise(Size{PixelToDip(new_width), PixelToDip(new_height)}); + } +} + +void WinNativeWindow::OnSetFocusInternal() { + has_focus_ = true; + focus_event_.Raise(FocusChangeType::Gain); +} + +void WinNativeWindow::OnKillFocusInternal() { + has_focus_ = false; + focus_event_.Raise(FocusChangeType::Lost); +} + +void WinNativeWindow::OnMouseMoveInternal(const POINT point) { + // when mouse was previous outside the window + if (!is_mouse_in_) { + // invoke TrackMouseEvent to have WM_MOUSELEAVE sent. + TRACKMOUSEEVENT tme; + tme.cbSize = sizeof tme; + tme.dwFlags = TME_LEAVE; + tme.hwndTrack = hwnd_; + + TrackMouseEvent(&tme); + + is_mouse_in_ = true; + mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Enter); + } + + mouse_move_event_.Raise(PixelToDip(point)); +} + +void WinNativeWindow::OnMouseLeaveInternal() { + is_mouse_in_ = false; + mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Leave); +} + +void WinNativeWindow::OnMouseDownInternal(platform::gui::MouseButton button, + POINT point) { + const auto dip_point = PixelToDip(point); + mouse_down_event_.Raise({button, dip_point, RetrieveKeyMofifier()}); +} + +void WinNativeWindow::OnMouseUpInternal(platform::gui::MouseButton button, + POINT point) { + const auto dip_point = PixelToDip(point); + mouse_up_event_.Raise({button, dip_point, RetrieveKeyMofifier()}); +} + +void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) { + CRU_UNUSED(delta) + CRU_UNUSED(point) +} + +void WinNativeWindow::OnKeyDownInternal(int virtual_code) { + key_down_event_.Raise( + {VirtualKeyToKeyCode(virtual_code), RetrieveKeyMofifier()}); +} + +void WinNativeWindow::OnKeyUpInternal(int virtual_code) { + key_up_event_.Raise( + {VirtualKeyToKeyCode(virtual_code), RetrieveKeyMofifier()}); +} + +void WinNativeWindow::OnActivatedInternal() {} + +void WinNativeWindow::OnDeactivatedInternal() {} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/WindowClass.cpp b/src/win/gui/WindowClass.cpp new file mode 100644 index 00000000..a033d091 --- /dev/null +++ b/src/win/gui/WindowClass.cpp @@ -0,0 +1,28 @@ +#include "cru/win/gui/WindowClass.hpp" + +#include "cru/win/gui/Exception.hpp" + +namespace cru::platform::gui::win { +WindowClass::WindowClass(std::wstring name, WNDPROC window_proc, + HINSTANCE h_instance) + : name_(std::move(name)) { + WNDCLASSEXW window_class; + window_class.cbSize = sizeof(WNDCLASSEXW); + + window_class.style = CS_HREDRAW | CS_VREDRAW; + window_class.lpfnWndProc = window_proc; + window_class.cbClsExtra = 0; + window_class.cbWndExtra = 0; + window_class.hInstance = h_instance; + window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); + window_class.hCursor = LoadCursor(NULL, IDC_ARROW); + window_class.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); + window_class.lpszMenuName = NULL; + window_class.lpszClassName = name_.c_str(); + window_class.hIconSm = NULL; + + atom_ = ::RegisterClassExW(&window_class); + if (atom_ == 0) + throw Win32Error(::GetLastError(), "Failed to create window class."); +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/WindowManager.cpp b/src/win/gui/WindowManager.cpp new file mode 100644 index 00000000..4e84e967 --- /dev/null +++ b/src/win/gui/WindowManager.cpp @@ -0,0 +1,56 @@ +#include "WindowManager.hpp" + +#include "cru/win/gui/UiApplication.hpp" +#include "cru/win/gui/Window.hpp" +#include "cru/win/gui/WindowClass.hpp" + +namespace cru::platform::gui::win { +LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, + LPARAM lParam) { + auto window = + WinUiApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd); + + LRESULT result; + if (window != nullptr && + window->HandleNativeWindowMessage(hWnd, Msg, wParam, lParam, &result)) + return result; + + return DefWindowProc(hWnd, Msg, wParam, lParam); +} + +WindowManager::WindowManager(WinUiApplication* application) { + application_ = application; + general_window_class_ = std::make_unique( + L"CruUIWindowClass", GeneralWndProc, application->GetInstanceHandle()); +} + +WindowManager::~WindowManager() { + for (const auto& [key, window] : window_map_) delete window; +} + +void WindowManager::RegisterWindow(HWND hwnd, WinNativeWindow* window) { + Expects(window_map_.count(hwnd) == 0); // The hwnd is already in the map. + window_map_.emplace(hwnd, window); +} + +void WindowManager::UnregisterWindow(HWND hwnd) { + const auto find_result = window_map_.find(hwnd); + Expects(find_result != window_map_.end()); // The hwnd is not in the map. + window_map_.erase(find_result); + if (window_map_.empty()) application_->RequestQuit(0); +} + +WinNativeWindow* WindowManager::FromHandle(HWND hwnd) { + const auto find_result = window_map_.find(hwnd); + if (find_result == window_map_.end()) + return nullptr; + else + return find_result->second; +} + +std::vector WindowManager::GetAllWindows() const { + std::vector windows; + for (const auto& [key, value] : window_map_) windows.push_back(value); + return windows; +} +} // namespace cru::platform::gui::win diff --git a/src/win/gui/WindowManager.hpp b/src/win/gui/WindowManager.hpp new file mode 100644 index 00000000..3b037f89 --- /dev/null +++ b/src/win/gui/WindowManager.hpp @@ -0,0 +1,51 @@ +#pragma once +#include "cru/win/WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +#include +#include +#include + +namespace cru::platform::gui::win { +class WinUiApplication; +class WinNativeWindow; +class WindowClass; + +class WindowManager : public Object { + public: + WindowManager(WinUiApplication* application); + + CRU_DELETE_COPY(WindowManager) + CRU_DELETE_MOVE(WindowManager) + + ~WindowManager() override; + + // Get the general window class for creating ordinary window. + WindowClass* GetGeneralWindowClass() const { + return general_window_class_.get(); + } + + // Register a window newly created. + // This function adds the hwnd to hwnd-window map. + // It should be called immediately after a window was created. + void RegisterWindow(HWND hwnd, WinNativeWindow* window); + + // Unregister a window that is going to be destroyed. + // This function removes the hwnd from the hwnd-window map. + // It should be called immediately before a window is going to be destroyed, + void UnregisterWindow(HWND hwnd); + + // Return a pointer to the Window object related to the HWND or nullptr if the + // hwnd is not in the map. + WinNativeWindow* FromHandle(HWND hwnd); + + std::vector GetAllWindows() const; + + private: + WinUiApplication* application_; + + std::unique_ptr general_window_class_; + std::map window_map_; +}; +} // namespace cru::platform::gui::win diff --git a/src/win/native/CMakeLists.txt b/src/win/native/CMakeLists.txt deleted file mode 100644 index 4b84600b..00000000 --- a/src/win/native/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -set(CRU_WIN_NATIVE_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/native) - -add_library(cru_win_native STATIC - TimerManager.hpp - WindowManager.hpp - - Cursor.cpp - GodWindow.cpp - InputMethod.cpp - Keyboard.cpp - TimerManager.cpp - UiApplication.cpp - Window.cpp - WindowClass.cpp - WindowManager.cpp -) -target_sources(cru_win_native PUBLIC - ${CRU_WIN_NATIVE_INCLUDE_DIR}/Cursor.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/Exception.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/Base.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/GodWindow.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/InputMethod.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/Keyboard.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/Resource.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/UiApplication.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/Window.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/WindowClass.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/WindowNativeMessageEventArgs.hpp -) -target_link_libraries(cru_win_native PUBLIC imm32) -target_link_libraries(cru_win_native PUBLIC cru_win_graph_direct cru_platform_native) diff --git a/src/win/native/Cursor.cpp b/src/win/native/Cursor.cpp deleted file mode 100644 index 429f6e7c..00000000 --- a/src/win/native/Cursor.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "cru/win/native/Cursor.hpp" - -#include "cru/common/Logger.hpp" -#include "cru/win/native/Exception.hpp" - -#include - -namespace cru::platform::native::win { -WinCursor::WinCursor(HCURSOR handle, bool auto_destroy) { - handle_ = handle; - auto_destroy_ = auto_destroy; -} - -WinCursor::~WinCursor() { - if (auto_destroy_) { - if (!::DestroyCursor(handle_)) { - // This is not a fetal error but might still need notice because it may - // cause leak. - log::TagWarn(log_tag, u"Failed to destroy a cursor. Last error code: {}", - ::GetLastError()); - } - } -} - -namespace { -WinCursor* LoadWinCursor(const wchar_t* name) { - const auto handle = static_cast(::LoadImageW( - NULL, name, IMAGE_CURSOR, SM_CYCURSOR, SM_CYCURSOR, LR_SHARED)); - if (handle == NULL) { - throw Win32Error(::GetLastError(), "Failed to load system cursor."); - } - return new WinCursor(handle, false); -} -} // namespace - -WinCursorManager::WinCursorManager() - : sys_arrow_(LoadWinCursor(IDC_ARROW)), - sys_hand_(LoadWinCursor(IDC_HAND)) {} - -std::shared_ptr WinCursorManager::GetSystemWinCursor( - SystemCursorType type) { - switch (type) { - case SystemCursorType::Arrow: - return sys_arrow_; - case SystemCursorType::Hand: - return sys_hand_; - default: - throw std::runtime_error("Unknown system cursor value."); - } -} -} // namespace cru::platform::native::win diff --git a/src/win/native/GodWindow.cpp b/src/win/native/GodWindow.cpp deleted file mode 100644 index 799a3cc6..00000000 --- a/src/win/native/GodWindow.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "cru/win/native/GodWindow.hpp" - -#include "cru/common/Logger.hpp" -#include "cru/win/native/Exception.hpp" -#include "cru/win/native/UiApplication.hpp" -#include "cru/win/native/WindowClass.hpp" - -namespace cru::platform::native::win { -constexpr auto god_window_class_name = L"GodWindowClass"; - -LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, - LPARAM lParam) { - const auto app = WinUiApplication::GetInstance(); - - if (app) { - LRESULT result; - auto god_window = app->GetGodWindow(); - if (god_window != nullptr) { - const auto handled = god_window->HandleGodWindowMessage( - hWnd, uMsg, wParam, lParam, &result); - if (handled) return result; - } - } - return DefWindowProcW(hWnd, uMsg, wParam, lParam); -} - -GodWindow::GodWindow(WinUiApplication* application) { - application_ = application; - - const auto h_instance = application->GetInstanceHandle(); - - god_window_class_ = std::make_unique(god_window_class_name, - GodWndProc, h_instance); - - hwnd_ = CreateWindowEx(0, god_window_class_name, L"", 0, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - HWND_MESSAGE, nullptr, h_instance, nullptr); - - if (hwnd_ == nullptr) - throw Win32Error(::GetLastError(), "Failed to create god window."); -} - -GodWindow::~GodWindow() { - if (!::DestroyWindow(hwnd_)) { - // Although this could be "safely" ignore. - log::TagWarn(log_tag, u"Failed to destroy god window."); - } -} - -bool GodWindow::HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, - LPARAM l_param, LRESULT* result) { - WindowNativeMessageEventArgs args( - WindowNativeMessage{hwnd, msg, w_param, l_param}); - message_event_.Raise(args); - - if (args.IsHandled()) { - *result = args.GetResult(); - return true; - } - - return false; -} -} // namespace cru::platform::native::win diff --git a/src/win/native/InputMethod.cpp b/src/win/native/InputMethod.cpp deleted file mode 100644 index 45c5f8da..00000000 --- a/src/win/native/InputMethod.cpp +++ /dev/null @@ -1,278 +0,0 @@ -#include "cru/win/native/InputMethod.hpp" - -#include "cru/common/Logger.hpp" -#include "cru/common/StringUtil.hpp" -#include "cru/platform/Check.hpp" -#include "cru/win/Exception.hpp" -#include "cru/win/native/Window.hpp" - -#include - -namespace cru::platform::native::win { -AutoHIMC::AutoHIMC(HWND hwnd) : hwnd_(hwnd) { - Expects(hwnd); - handle_ = ::ImmGetContext(hwnd); -} - -AutoHIMC::AutoHIMC(AutoHIMC&& other) - : hwnd_(other.hwnd_), handle_(other.handle_) { - other.hwnd_ = nullptr; - other.handle_ = nullptr; -} - -AutoHIMC& AutoHIMC::operator=(AutoHIMC&& other) { - if (this != &other) { - Object::operator=(std::move(other)); - this->hwnd_ = other.hwnd_; - this->handle_ = other.handle_; - other.hwnd_ = nullptr; - other.handle_ = nullptr; - } - return *this; -} - -AutoHIMC::~AutoHIMC() { - if (handle_) { - if (!::ImmReleaseContext(hwnd_, handle_)) - log::TagWarn(log_tag, u"Failed to release HIMC."); - } -} - -// copied from chromium -namespace { -// Determines whether or not the given attribute represents a target -// (a.k.a. a selection). -bool IsTargetAttribute(char attribute) { - return (attribute == ATTR_TARGET_CONVERTED || - attribute == ATTR_TARGET_NOTCONVERTED); -} -// Helper function for ImeInput::GetCompositionInfo() method, to get the target -// range that's selected by the user in the current composition string. -void GetCompositionTargetRange(HIMC imm_context, int* target_start, - int* target_end) { - int attribute_size = - ::ImmGetCompositionString(imm_context, GCS_COMPATTR, NULL, 0); - if (attribute_size > 0) { - int start = 0; - int end = 0; - std::vector attribute_data(attribute_size); - ::ImmGetCompositionString(imm_context, GCS_COMPATTR, attribute_data.data(), - attribute_size); - for (start = 0; start < attribute_size; ++start) { - if (IsTargetAttribute(attribute_data[start])) break; - } - for (end = start; end < attribute_size; ++end) { - if (!IsTargetAttribute(attribute_data[end])) break; - } - if (start == attribute_size) { - // This composition clause does not contain any target clauses, - // i.e. this clauses is an input clause. - // We treat the whole composition as a target clause. - start = 0; - end = attribute_size; - } - *target_start = start; - *target_end = end; - } -} -// Helper function for ImeInput::GetCompositionInfo() method, to get underlines -// information of the current composition string. -CompositionClauses GetCompositionClauses(HIMC imm_context, int target_start, - int target_end) { - CompositionClauses result; - int clause_size = - ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE, NULL, 0); - int clause_length = clause_size / sizeof(std::uint32_t); - if (clause_length) { - result.reserve(clause_length - 1); - std::vector clause_data(clause_length); - ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE, clause_data.data(), - clause_size); - for (int i = 0; i < clause_length - 1; ++i) { - CompositionClause clause; - clause.start = clause_data[i]; - clause.end = clause_data[i + 1]; - clause.target = false; - // Use thick underline for the target clause. - if (clause.start >= target_start && clause.end <= target_end) { - clause.target = true; - } - result.push_back(clause); - } - } - return result; -} - -std::u16string GetString(HIMC imm_context) { - LONG string_size = - ::ImmGetCompositionString(imm_context, GCS_COMPSTR, NULL, 0); - std::u16string result((string_size / sizeof(char16_t)), 0); - ::ImmGetCompositionString(imm_context, GCS_COMPSTR, result.data(), - string_size); - return result; -} - -std::u16string GetResultString(HIMC imm_context) { - LONG string_size = - ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, NULL, 0); - std::u16string result((string_size / sizeof(char16_t)), 0); - ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, result.data(), - string_size); - return result; -} - -CompositionText GetCompositionInfo(HIMC imm_context) { - // We only care about GCS_COMPATTR, GCS_COMPCLAUSE and GCS_CURSORPOS, and - // convert them into underlines and selection range respectively. - - auto text = GetString(imm_context); - - int length = static_cast(text.length()); - // Find out the range selected by the user. - int target_start = length; - int target_end = length; - GetCompositionTargetRange(imm_context, &target_start, &target_end); - - auto clauses = GetCompositionClauses(imm_context, target_start, target_end); - - int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0); - - return CompositionText{std::move(text), std::move(clauses), - TextRange{cursor}}; -} - -} // namespace - -WinInputMethodContext::WinInputMethodContext( - gsl::not_null window) - : native_window_(window) { - event_guard_ += window->NativeMessageEvent()->AddHandler( - std::bind(&WinInputMethodContext::OnWindowNativeMessage, this, - std::placeholders::_1)); -} - -WinInputMethodContext::~WinInputMethodContext() {} - -void WinInputMethodContext::EnableIME() { - const auto hwnd = native_window_->GetWindowHandle(); - if (::ImmAssociateContextEx(hwnd, nullptr, IACE_DEFAULT) == FALSE) { - log::TagWarn(log_tag, u"Failed to enable ime."); - } -} - -void WinInputMethodContext::DisableIME() { - const auto hwnd = native_window_->GetWindowHandle(); - AutoHIMC himc{hwnd}; - - if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { - log::TagWarn(log_tag, - u"Failed to complete composition before disable ime."); - } - - if (::ImmAssociateContextEx(hwnd, nullptr, 0) == FALSE) { - log::TagWarn(log_tag, u"Failed to disable ime."); - } -} - -void WinInputMethodContext::CompleteComposition() { - auto himc = GetHIMC(); - if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { - log::TagWarn(log_tag, u"Failed to complete composition."); - } -} - -void WinInputMethodContext::CancelComposition() { - auto himc = GetHIMC(); - if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0)) { - log::TagWarn(log_tag, u"Failed to complete composition."); - } -} - -CompositionText WinInputMethodContext::GetCompositionText() { - auto himc = GetHIMC(); - return GetCompositionInfo(himc.Get()); -} - -void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) { - auto himc = GetHIMC(); - - ::CANDIDATEFORM form; - form.dwIndex = 1; - form.dwStyle = CFS_CANDIDATEPOS; - - form.ptCurrentPos = native_window_->DipToPixel(point); - - if (!::ImmSetCandidateWindow(himc.Get(), &form)) - log::TagDebug(log_tag, - u"Failed to set input method candidate window position."); -} - -IEvent* WinInputMethodContext::CompositionStartEvent() { - return &composition_start_event_; -} - -IEvent* WinInputMethodContext::CompositionEndEvent() { - return &composition_end_event_; -}; - -IEvent* WinInputMethodContext::CompositionEvent() { - return &composition_event_; -} - -IEvent* WinInputMethodContext::TextEvent() { - return &text_event_; -} - -void WinInputMethodContext::OnWindowNativeMessage( - WindowNativeMessageEventArgs& args) { - const auto& message = args.GetWindowMessage(); - switch (message.msg) { - case WM_CHAR: { - const auto c = static_cast(message.w_param); - if (IsUtf16SurrogatePairCodeUnit(c)) { - // I don't think this will happen because normal key strike without ime - // should only trigger ascci character. If it is a charater from - // supplementary planes, it should be handled with ime messages. - log::TagWarn(log_tag, - u"A WM_CHAR message for character from supplementary " - u"planes is ignored."); - } else { - char16_t s[1] = {c}; - text_event_.Raise({s, 1}); - } - args.HandleWithResult(0); - break; - } - case WM_IME_COMPOSITION: { - composition_event_.Raise(nullptr); - auto composition_text = GetCompositionText(); - log::TagDebug(log_tag, u"WM_IME_COMPOSITION composition text:\n{}", - composition_text); - if (message.l_param & GCS_RESULTSTR) { - auto result_string = GetResultString(); - text_event_.Raise(result_string); - } - break; - } - case WM_IME_STARTCOMPOSITION: { - composition_start_event_.Raise(nullptr); - break; - } - case WM_IME_ENDCOMPOSITION: { - composition_end_event_.Raise(nullptr); - break; - } - } -} - -std::u16string WinInputMethodContext::GetResultString() { - auto himc = GetHIMC(); - auto result = win::GetResultString(himc.Get()); - return result; -} - -AutoHIMC WinInputMethodContext::GetHIMC() { - const auto hwnd = native_window_->GetWindowHandle(); - return AutoHIMC{hwnd}; -} -} // namespace cru::platform::native::win diff --git a/src/win/native/Keyboard.cpp b/src/win/native/Keyboard.cpp deleted file mode 100644 index 929ca737..00000000 --- a/src/win/native/Keyboard.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "cru/win/native/Keyboard.hpp" - -namespace cru::platform::native::win { -KeyCode VirtualKeyToKeyCode(int virtual_key) { - if (virtual_key >= 0x30 && virtual_key <= 0x39) { - return KeyCode{static_cast(KeyCode::N0) + (virtual_key - 0x30)}; - } else if (virtual_key >= 0x41 && virtual_key <= 0x5a) { - return KeyCode{static_cast(KeyCode::A) + (virtual_key - 0x41)}; - } else if (virtual_key >= VK_NUMPAD0 && virtual_key <= VK_NUMPAD9) { - return KeyCode{static_cast(KeyCode::NumPad0) + - (virtual_key - VK_NUMPAD0)}; - } else if (virtual_key >= VK_F1 && virtual_key <= VK_F12) { - return KeyCode{static_cast(KeyCode::F1) + (virtual_key - VK_F1)}; - } else { - switch (virtual_key) { -#define CRU_MAP_KEY(virtual_key, keycode) \ - case virtual_key: \ - return KeyCode::keycode; - - CRU_MAP_KEY(VK_LBUTTON, LeftButton) - CRU_MAP_KEY(VK_MBUTTON, MiddleButton) - CRU_MAP_KEY(VK_RBUTTON, RightButton) - CRU_MAP_KEY(VK_ESCAPE, Escape) - CRU_MAP_KEY(VK_OEM_3, GraveAccent) - CRU_MAP_KEY(VK_TAB, Tab) - CRU_MAP_KEY(VK_CAPITAL, CapsLock) - CRU_MAP_KEY(VK_LSHIFT, LeftShift) - CRU_MAP_KEY(VK_LCONTROL, LeftCtrl) - CRU_MAP_KEY(VK_LWIN, LeftSuper) - CRU_MAP_KEY(VK_LMENU, LeftAlt) - CRU_MAP_KEY(VK_OEM_MINUS, Minus) - CRU_MAP_KEY(VK_OEM_PLUS, Equal) - CRU_MAP_KEY(VK_BACK, Backspace) - CRU_MAP_KEY(VK_OEM_4, LeftSquareBracket) - CRU_MAP_KEY(VK_OEM_6, RightSquareBracket) - CRU_MAP_KEY(VK_OEM_5, BackSlash) - CRU_MAP_KEY(VK_OEM_1, Semicolon) - CRU_MAP_KEY(VK_OEM_7, Quote) - CRU_MAP_KEY(VK_OEM_COMMA, Comma) - CRU_MAP_KEY(VK_OEM_PERIOD, Period) - CRU_MAP_KEY(VK_OEM_2, Slash) - CRU_MAP_KEY(VK_RSHIFT, RightShift) - CRU_MAP_KEY(VK_RCONTROL, RightCtrl) - CRU_MAP_KEY(VK_RWIN, RightSuper) - CRU_MAP_KEY(VK_RMENU, RightAlt) - CRU_MAP_KEY(VK_INSERT, Insert) - CRU_MAP_KEY(VK_DELETE, Delete) - CRU_MAP_KEY(VK_HOME, Home) - CRU_MAP_KEY(VK_END, End) - CRU_MAP_KEY(VK_PRIOR, PageUp) - CRU_MAP_KEY(VK_NEXT, PageDown) - CRU_MAP_KEY(VK_UP, Up) - CRU_MAP_KEY(VK_LEFT, Left) - CRU_MAP_KEY(VK_DOWN, Down) - CRU_MAP_KEY(VK_RIGHT, Right) - CRU_MAP_KEY(VK_SNAPSHOT, PrintScreen) - CRU_MAP_KEY(VK_PAUSE, Pause) - -#undef CRU_MAP_KEY - - default: - return KeyCode::Unknown; - } - } -} - -KeyModifier RetrieveKeyMofifier() { - KeyModifier result{0}; - if (::GetKeyState(VK_SHIFT) < 0) result |= KeyModifiers::shift; - if (::GetKeyState(VK_CONTROL) < 0) result |= KeyModifiers::ctrl; - if (::GetKeyState(VK_MENU) < 0) result |= KeyModifiers::alt; - return result; -} -} // namespace cru::platform::native::win diff --git a/src/win/native/TimerManager.cpp b/src/win/native/TimerManager.cpp deleted file mode 100644 index 206ae3e3..00000000 --- a/src/win/native/TimerManager.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "TimerManager.hpp" - -#include "cru/win/native/Base.hpp" -#include "cru/win/native/Exception.hpp" -#include "gsl/gsl_util" - -#include -#include - -namespace cru::platform::native::win { -constexpr int kSetImmediateWindowMessageId = WM_USER + 2000; - -TimerManager::TimerManager(GodWindow* god_window) { - god_window_ = god_window; - event_guard_ += god_window->MessageEvent()->AddHandler(std::bind( - &TimerManager::HandleGodWindowMessage, this, std::placeholders::_1)); -} - -long long TimerManager::SetTimer(TimerType type, int period, - std::function action) { - auto id = next_id_++; - TimerInfo timer_info{id, type, type == TimerType::Immediate ? 0 : period, - std::move(action)}; - if (type == TimerType::Immediate) { - if (!::PostMessageW(god_window_->GetHandle(), kSetImmediateWindowMessageId, - gsl::narrow(id), 0)) { - throw Win32Error( - ::GetLastError(), - "Failed to post window message to god window for set immediate."); - } - } else { - CreateNativeTimer(&timer_info); - } - - info_map_.emplace(id, std::move(timer_info)); - return id; -} - -void TimerManager::CancelTimer(long long id) { - if (id <= 0) return; - auto find_result = this->info_map_.find(id); - if (find_result != info_map_.cend()) { - auto& info = find_result->second; - KillNativeTimer(&info); - this->info_map_.erase(find_result); - } -} - -void TimerManager::CreateNativeTimer(TimerInfo* info) { - info->native_timer_id = gsl::narrow(info->id); - ::SetTimer(god_window_->GetHandle(), info->native_timer_id, info->period, - nullptr); -} - -void TimerManager::KillNativeTimer(TimerInfo* info) { - if (info->native_timer_id == 0) return; - ::KillTimer(god_window_->GetHandle(), info->native_timer_id); - info->native_timer_id = 0; -} - -void TimerManager::HandleGodWindowMessage(WindowNativeMessageEventArgs& args) { - const auto& message = args.GetWindowMessage(); - - switch (message.msg) { - case kSetImmediateWindowMessageId: { - auto find_result = - this->info_map_.find(static_cast(message.w_param)); - if (find_result != info_map_.cend()) { - auto& info = find_result->second; - info.action(); - info_map_.erase(find_result); - } - args.SetResult(0); - args.SetHandled(true); - return; - } - case WM_TIMER: { - auto find_result = - this->info_map_.find(static_cast(message.w_param)); - if (find_result != info_map_.cend()) { - auto& info = find_result->second; - if (info.type == TimerType::Interval) { - info.action(); - args.SetResult(0); - args.SetHandled(true); - } else if (info.type == TimerType::Timeout) { - info.action(); - KillNativeTimer(&info); - info_map_.erase(find_result); - args.SetResult(0); - args.SetHandled(true); - } - } - return; - } - default: - return; - } -} -} // namespace cru::platform::native::win diff --git a/src/win/native/TimerManager.hpp b/src/win/native/TimerManager.hpp deleted file mode 100644 index f2731f60..00000000 --- a/src/win/native/TimerManager.hpp +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once -#include "cru/common/Event.hpp" -#include "cru/win/WinPreConfig.hpp" - -#include "cru/common/Base.hpp" -#include "cru/win/native/GodWindow.hpp" -#include "cru/win/native/WindowNativeMessageEventArgs.hpp" - -#include -#include -#include -#include - -namespace cru::platform::native::win { -enum class TimerType { Immediate, Timeout, Interval }; - -struct TimerInfo { - TimerInfo(long long id, TimerType type, int period, - std::function action, UINT_PTR native_timer_id = 0) - : id(id), - type(type), - period(period), - action(std::move(action)), - native_timer_id(native_timer_id) {} - - long long id; - TimerType type; - int period; // in milliseconds - std::function action; - UINT_PTR native_timer_id; -}; - -class TimerManager : public Object { - public: - TimerManager(GodWindow* god_window); - - CRU_DELETE_COPY(TimerManager) - CRU_DELETE_MOVE(TimerManager) - - ~TimerManager() override = default; - - // Period is in milliseconds. When type is immediate, it is not checked and - // used. - long long SetTimer(TimerType type, int period, std::function action); - void CancelTimer(long long id); - - private: - void HandleGodWindowMessage(WindowNativeMessageEventArgs& args); - - void CreateNativeTimer(TimerInfo* info); - void KillNativeTimer(TimerInfo* info); - - private: - GodWindow* god_window_; - - EventRevokerListGuard event_guard_; - - long long next_id_ = 1; - std::unordered_map info_map_; -}; -} // namespace cru::platform::native::win diff --git a/src/win/native/UiApplication.cpp b/src/win/native/UiApplication.cpp deleted file mode 100644 index 87ef0b81..00000000 --- a/src/win/native/UiApplication.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include "cru/win/native/UiApplication.hpp" - -#include "../DebugLogger.hpp" -#include "../StdOutLogger.hpp" -#include "TimerManager.hpp" -#include "WindowManager.hpp" -#include "cru/common/Logger.hpp" -#include "cru/platform/Check.hpp" -#include "cru/win/graph/direct/Factory.hpp" -#include "cru/win/native/Cursor.hpp" -#include "cru/win/native/Exception.hpp" -#include "cru/win/native/GodWindow.hpp" -#include "cru/win/native/InputMethod.hpp" -#include "cru/win/native/Window.hpp" - -namespace cru::platform::native { -std::unique_ptr CreateUiApplication() { - return std::make_unique(); -} -} // namespace cru::platform::native - -namespace cru::platform::native::win { -WinUiApplication* WinUiApplication::instance = nullptr; - -WinUiApplication::WinUiApplication() { - instance = this; - - instance_handle_ = ::GetModuleHandleW(nullptr); - if (!instance_handle_) - throw Win32Error("Failed to get module(instance) handle."); - - ::SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE); - - log::Logger::GetInstance()->AddSource( - std::make_unique<::cru::platform::win::WinDebugLoggerSource>()); - log::Logger::GetInstance()->AddSource( - std::make_unique<::cru::platform::win::WinStdOutLoggerSource>()); - - graph_factory_ = - std::make_unique(); - - god_window_ = std::make_unique(this); - timer_manager_ = std::make_unique(god_window_.get()); - window_manager_ = std::make_unique(this); - cursor_manager_ = std::make_unique(); -} - -WinUiApplication::~WinUiApplication() { instance = nullptr; } - -int WinUiApplication::Run() { - MSG msg; - while (GetMessageW(&msg, nullptr, 0, 0)) { - TranslateMessage(&msg); - DispatchMessageW(&msg); - } - - for (const auto& handler : quit_handlers_) handler(); - - return static_cast(msg.wParam); -} - -void WinUiApplication::RequestQuit(const int quit_code) { - ::PostQuitMessage(quit_code); -} - -void WinUiApplication::AddOnQuitHandler(std::function handler) { - quit_handlers_.push_back(std::move(handler)); -} - -long long WinUiApplication::SetImmediate(std::function action) { - return this->timer_manager_->SetTimer(TimerType::Immediate, 0, - std::move(action)); -} - -long long WinUiApplication::SetTimeout(std::chrono::milliseconds milliseconds, - std::function action) { - return this->timer_manager_->SetTimer(TimerType::Timeout, - gsl::narrow(milliseconds.count()), - std::move(action)); -} - -long long WinUiApplication::SetInterval(std::chrono::milliseconds milliseconds, - std::function action) { - return this->timer_manager_->SetTimer(TimerType::Interval, - gsl::narrow(milliseconds.count()), - std::move(action)); -} - -void WinUiApplication::CancelTimer(long long id) { - timer_manager_->CancelTimer(id); -} - -std::vector WinUiApplication::GetAllWindow() { - const auto&& windows = window_manager_->GetAllWindows(); - std::vector result; - for (const auto w : windows) { - result.push_back(static_cast(w)); - } - return result; -} - -INativeWindow* WinUiApplication::CreateWindow(INativeWindow* parent) { - WinNativeWindow* p = nullptr; - if (parent != nullptr) { - p = CheckPlatform(parent, GetPlatformId()); - } - return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), - WS_OVERLAPPEDWINDOW, p); -} - -cru::platform::graph::IGraphFactory* WinUiApplication::GetGraphFactory() { - return graph_factory_.get(); -} - -ICursorManager* WinUiApplication::GetCursorManager() { - return cursor_manager_.get(); -} -} // namespace cru::platform::native::win diff --git a/src/win/native/Window.cpp b/src/win/native/Window.cpp deleted file mode 100644 index 1a6fcb07..00000000 --- a/src/win/native/Window.cpp +++ /dev/null @@ -1,453 +0,0 @@ -#include "cru/win/native/Window.hpp" - -#include "WindowManager.hpp" -#include "cru/common/Logger.hpp" -#include "cru/platform/Check.hpp" -#include "cru/platform/native/Base.hpp" -#include "cru/win/graph/direct/WindowPainter.hpp" -#include "cru/win/native/Cursor.hpp" -#include "cru/win/native/Exception.hpp" -#include "cru/win/native/InputMethod.hpp" -#include "cru/win/native/Keyboard.hpp" -#include "cru/win/native/UiApplication.hpp" -#include "cru/win/native/WindowClass.hpp" - -#include -#include -#include - -namespace cru::platform::native::win { -WinNativeWindow::WinNativeWindow(WinUiApplication* application, - WindowClass* window_class, DWORD window_style, - WinNativeWindow* parent) - : application_(application), parent_window_(parent) { - Expects(application); // application can't be null. - - if (parent != nullptr) { - throw new std::runtime_error("Can't use a invalid window as parent."); - } - - const auto window_manager = application->GetWindowManager(); - - hwnd_ = CreateWindowExW( - 0, window_class->GetName(), L"", window_style, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, - application->GetInstanceHandle(), nullptr); - - if (hwnd_ == nullptr) - throw Win32Error(::GetLastError(), "Failed to create window."); - - auto dpi = ::GetDpiForWindow(hwnd_); - if (dpi == 0) - throw Win32Error(::GetLastError(), "Failed to get dpi of window."); - dpi_ = static_cast(dpi); - log::Debug(u"Dpi of window is {}.", dpi_); - - window_manager->RegisterWindow(hwnd_, this); - - SetCursor(application->GetCursorManager()->GetSystemCursor( - cru::platform::native::SystemCursorType::Arrow)); - - window_render_target_ = - std::make_unique( - application->GetDirectFactory(), hwnd_); - window_render_target_->SetDpi(dpi_, dpi_); - - input_method_context_ = std::make_unique(this); - input_method_context_->DisableIME(); -} - -WinNativeWindow::~WinNativeWindow() { - if (!sync_flag_) { - sync_flag_ = true; - Close(); - } -} - -void WinNativeWindow::Close() { ::DestroyWindow(hwnd_); } - -bool WinNativeWindow::IsVisible() { return ::IsWindowVisible(hwnd_); } - -void WinNativeWindow::SetVisible(bool is_visible) { - is_visible ? ShowWindow(hwnd_, SW_SHOWNORMAL) : ShowWindow(hwnd_, SW_HIDE); -} -Size WinNativeWindow::GetClientSize() { - const auto pixel_rect = GetClientRectPixel(); - return Size(PixelToDip(pixel_rect.right), PixelToDip(pixel_rect.bottom)); -} - -void WinNativeWindow::SetClientSize(const Size& size) { - const auto window_style = - static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); - const auto window_ex_style = - static_cast(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); - - RECT rect; - rect.left = 0; - rect.top = 0; - rect.right = DipToPixel(size.width); - rect.bottom = DipToPixel(size.height); - if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style)) - throw Win32Error(::GetLastError(), "Failed to invoke AdjustWindowRectEx."); - - if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, - rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE)) - throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); -} - -Rect WinNativeWindow::GetWindowRect() { - RECT rect; - if (!::GetWindowRect(hwnd_, &rect)) - throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect."); - - return Rect::FromVertices(PixelToDip(rect.left), PixelToDip(rect.top), - PixelToDip(rect.right), PixelToDip(rect.bottom)); -} - -void WinNativeWindow::SetWindowRect(const Rect& rect) { - if (!SetWindowPos(hwnd_, nullptr, DipToPixel(rect.left), DipToPixel(rect.top), - DipToPixel(rect.GetRight()), DipToPixel(rect.GetBottom()), - SWP_NOZORDER)) - throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); -} - -Point WinNativeWindow::GetMousePosition() { - POINT p; - if (!::GetCursorPos(&p)) - throw Win32Error(::GetLastError(), "Failed to get cursor position."); - if (!::ScreenToClient(hwnd_, &p)) - throw Win32Error(::GetLastError(), "Failed to call ScreenToClient."); - return PixelToDip(p); -} - -bool WinNativeWindow::CaptureMouse() { - ::SetCapture(hwnd_); - return true; -} - -bool WinNativeWindow::ReleaseMouse() { - const auto result = ::ReleaseCapture(); - return result != 0; -} - -void WinNativeWindow::RequestRepaint() { - log::TagDebug(log_tag, u"A repaint is requested."); - if (!::InvalidateRect(hwnd_, nullptr, FALSE)) - throw Win32Error(::GetLastError(), "Failed to invalidate window."); - if (!::UpdateWindow(hwnd_)) - throw Win32Error(::GetLastError(), "Failed to update window."); -} - -std::unique_ptr WinNativeWindow::BeginPaint() { - return std::make_unique( - window_render_target_.get()); -} - -void WinNativeWindow::SetCursor(std::shared_ptr cursor) { - if (cursor == nullptr) { - throw std::runtime_error("Can't use a nullptr as cursor."); - } - - cursor_ = CheckPlatform(cursor, GetPlatformId()); - - if (!::SetClassLongPtrW(hwnd_, GCLP_HCURSOR, - reinterpret_cast(cursor_->GetHandle()))) { - log::TagWarn(log_tag, - u"Failed to set cursor because failed to set class long. Last " - u"error code: {}.", - ::GetLastError()); - return; - } - - if (!IsVisible()) return; - - auto lg = [](const std::u16string_view& reason) { - log::TagWarn( - log_tag, - u"Failed to set cursor because {} when window is visible. (We need to " - u"update cursor if it is inside the window.) Last error code: {}.", - reason, ::GetLastError()); - }; - - ::POINT point; - if (!::GetCursorPos(&point)) { - lg(u"failed to get cursor pos"); - return; - } - - ::RECT rect; - if (!::GetClientRect(hwnd_, &rect)) { - lg(u"failed to get window's client rect"); - return; - } - - ::POINT lefttop{rect.left, rect.top}; - ::POINT rightbottom{rect.right, rect.bottom}; - if (!::ClientToScreen(hwnd_, &lefttop)) { - lg(u"failed to call ClientToScreen on lefttop"); - return; - } - - if (!::ClientToScreen(hwnd_, &rightbottom)) { - lg(u"failed to call ClientToScreen on rightbottom"); - return; - } - - if (point.x >= lefttop.x && point.y >= lefttop.y && - point.x <= rightbottom.x && point.y <= rightbottom.y) { - ::SetCursor(cursor_->GetHandle()); - } -} - -IInputMethodContext* WinNativeWindow::GetInputMethodContext() { - return static_cast(input_method_context_.get()); -} - -bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, - WPARAM w_param, LPARAM l_param, - LRESULT* result) { - WindowNativeMessageEventArgs args{ - WindowNativeMessage{hwnd, msg, w_param, l_param}}; - native_message_event_.Raise(args); - if (args.IsHandled()) { - *result = args.GetResult(); - return true; - } - - switch (msg) { - case WM_PAINT: - OnPaintInternal(); - *result = 0; - return true; - case WM_ERASEBKGND: - *result = 1; - return true; - case WM_SETFOCUS: - OnSetFocusInternal(); - *result = 0; - return true; - case WM_KILLFOCUS: - OnKillFocusInternal(); - *result = 0; - return true; - case WM_MOUSEMOVE: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseMoveInternal(point); - *result = 0; - return true; - } - case WM_LBUTTONDOWN: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(platform::native::mouse_buttons::left, point); - *result = 0; - return true; - } - case WM_LBUTTONUP: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(platform::native::mouse_buttons::left, point); - *result = 0; - return true; - } - case WM_RBUTTONDOWN: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(platform::native::mouse_buttons::right, point); - *result = 0; - return true; - } - case WM_RBUTTONUP: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(platform::native::mouse_buttons::right, point); - *result = 0; - return true; - } - case WM_MBUTTONDOWN: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(platform::native::mouse_buttons::middle, point); - *result = 0; - return true; - } - case WM_MBUTTONUP: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(platform::native::mouse_buttons::middle, point); - *result = 0; - return true; - } - case WM_MOUSEWHEEL: - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - ScreenToClient(hwnd, &point); - OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); - *result = 0; - return true; - case WM_KEYDOWN: - OnKeyDownInternal(static_cast(w_param)); - *result = 0; - return true; - case WM_KEYUP: - OnKeyUpInternal(static_cast(w_param)); - *result = 0; - return true; - case WM_SYSKEYDOWN: - if (l_param & (1 << 29)) { - OnKeyDownInternal(static_cast(w_param)); - *result = 0; - return true; - } - return false; - case WM_SYSKEYUP: - if (l_param & (1 << 29)) { - OnKeyUpInternal(static_cast(w_param)); - *result = 0; - return true; - } - return false; - case WM_SIZE: - OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); - *result = 0; - return true; - case WM_ACTIVATE: - if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) - OnActivatedInternal(); - else if (w_param == WA_INACTIVE) - OnDeactivatedInternal(); - *result = 0; - return true; - case WM_DESTROY: - OnDestroyInternal(); - *result = 0; - return true; - case WM_IME_SETCONTEXT: - l_param &= ~ISC_SHOWUICOMPOSITIONWINDOW; - *result = ::DefWindowProcW(hwnd, msg, w_param, l_param); - return true; - // We must block these message from DefWindowProc or it will create - // an ugly composition window. - case WM_IME_STARTCOMPOSITION: - case WM_IME_COMPOSITION: - *result = 0; - return true; - case WM_DPICHANGED: { - dpi_ = static_cast(LOWORD(w_param)); - const RECT* suggest_rect = reinterpret_cast(l_param); - window_render_target_->SetDpi(dpi_, dpi_); - SetWindowPos(hwnd_, NULL, suggest_rect->left, suggest_rect->top, - suggest_rect->right - suggest_rect->left, - suggest_rect->bottom - suggest_rect->top, - SWP_NOZORDER | SWP_NOACTIVATE); - } - default: - return false; - } -} - -RECT WinNativeWindow::GetClientRectPixel() { - RECT rect; - if (!GetClientRect(hwnd_, &rect)) - throw Win32Error(::GetLastError(), "Failed to invoke GetClientRect."); - return rect; -} - -void WinNativeWindow::OnDestroyInternal() { - application_->GetWindowManager()->UnregisterWindow(hwnd_); - hwnd_ = nullptr; - destroy_event_.Raise(nullptr); - if (!sync_flag_) { - sync_flag_ = true; - delete this; - } -} - -void WinNativeWindow::OnPaintInternal() { - paint_event_.Raise(nullptr); - ValidateRect(hwnd_, nullptr); - log::TagDebug(log_tag, u"A repaint is finished."); -} - -void WinNativeWindow::OnResizeInternal(const int new_width, - const int new_height) { - if (!(new_width == 0 && new_height == 0)) { - window_render_target_->ResizeBuffer(new_width, new_height); - resize_event_.Raise(Size{PixelToDip(new_width), PixelToDip(new_height)}); - } -} - -void WinNativeWindow::OnSetFocusInternal() { - has_focus_ = true; - focus_event_.Raise(FocusChangeType::Gain); -} - -void WinNativeWindow::OnKillFocusInternal() { - has_focus_ = false; - focus_event_.Raise(FocusChangeType::Lost); -} - -void WinNativeWindow::OnMouseMoveInternal(const POINT point) { - // when mouse was previous outside the window - if (!is_mouse_in_) { - // invoke TrackMouseEvent to have WM_MOUSELEAVE sent. - TRACKMOUSEEVENT tme; - tme.cbSize = sizeof tme; - tme.dwFlags = TME_LEAVE; - tme.hwndTrack = hwnd_; - - TrackMouseEvent(&tme); - - is_mouse_in_ = true; - mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Enter); - } - - mouse_move_event_.Raise(PixelToDip(point)); -} - -void WinNativeWindow::OnMouseLeaveInternal() { - is_mouse_in_ = false; - mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Leave); -} - -void WinNativeWindow::OnMouseDownInternal(platform::native::MouseButton button, - POINT point) { - const auto dip_point = PixelToDip(point); - mouse_down_event_.Raise({button, dip_point, RetrieveKeyMofifier()}); -} - -void WinNativeWindow::OnMouseUpInternal(platform::native::MouseButton button, - POINT point) { - const auto dip_point = PixelToDip(point); - mouse_up_event_.Raise({button, dip_point, RetrieveKeyMofifier()}); -} - -void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) { - CRU_UNUSED(delta) - CRU_UNUSED(point) -} - -void WinNativeWindow::OnKeyDownInternal(int virtual_code) { - key_down_event_.Raise( - {VirtualKeyToKeyCode(virtual_code), RetrieveKeyMofifier()}); -} - -void WinNativeWindow::OnKeyUpInternal(int virtual_code) { - key_up_event_.Raise( - {VirtualKeyToKeyCode(virtual_code), RetrieveKeyMofifier()}); -} - -void WinNativeWindow::OnActivatedInternal() {} - -void WinNativeWindow::OnDeactivatedInternal() {} -} // namespace cru::platform::native::win diff --git a/src/win/native/WindowClass.cpp b/src/win/native/WindowClass.cpp deleted file mode 100644 index 2e74606e..00000000 --- a/src/win/native/WindowClass.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "cru/win/native/WindowClass.hpp" - -#include "cru/win/native/Exception.hpp" - -namespace cru::platform::native::win { -WindowClass::WindowClass(std::wstring name, WNDPROC window_proc, - HINSTANCE h_instance) - : name_(std::move(name)) { - WNDCLASSEXW window_class; - window_class.cbSize = sizeof(WNDCLASSEXW); - - window_class.style = CS_HREDRAW | CS_VREDRAW; - window_class.lpfnWndProc = window_proc; - window_class.cbClsExtra = 0; - window_class.cbWndExtra = 0; - window_class.hInstance = h_instance; - window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); - window_class.hCursor = LoadCursor(NULL, IDC_ARROW); - window_class.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); - window_class.lpszMenuName = NULL; - window_class.lpszClassName = name_.c_str(); - window_class.hIconSm = NULL; - - atom_ = ::RegisterClassExW(&window_class); - if (atom_ == 0) - throw Win32Error(::GetLastError(), "Failed to create window class."); -} -} // namespace cru::platform::native::win diff --git a/src/win/native/WindowManager.cpp b/src/win/native/WindowManager.cpp deleted file mode 100644 index 56cc8981..00000000 --- a/src/win/native/WindowManager.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "WindowManager.hpp" - -#include "cru/win/native/UiApplication.hpp" -#include "cru/win/native/Window.hpp" -#include "cru/win/native/WindowClass.hpp" - -namespace cru::platform::native::win { -LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, - LPARAM lParam) { - auto window = - WinUiApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd); - - LRESULT result; - if (window != nullptr && - window->HandleNativeWindowMessage(hWnd, Msg, wParam, lParam, &result)) - return result; - - return DefWindowProc(hWnd, Msg, wParam, lParam); -} - -WindowManager::WindowManager(WinUiApplication* application) { - application_ = application; - general_window_class_ = std::make_unique( - L"CruUIWindowClass", GeneralWndProc, application->GetInstanceHandle()); -} - -WindowManager::~WindowManager() { - for (const auto& [key, window] : window_map_) delete window; -} - -void WindowManager::RegisterWindow(HWND hwnd, WinNativeWindow* window) { - Expects(window_map_.count(hwnd) == 0); // The hwnd is already in the map. - window_map_.emplace(hwnd, window); -} - -void WindowManager::UnregisterWindow(HWND hwnd) { - const auto find_result = window_map_.find(hwnd); - Expects(find_result != window_map_.end()); // The hwnd is not in the map. - window_map_.erase(find_result); - if (window_map_.empty()) application_->RequestQuit(0); -} - -WinNativeWindow* WindowManager::FromHandle(HWND hwnd) { - const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - return nullptr; - else - return find_result->second; -} - -std::vector WindowManager::GetAllWindows() const { - std::vector windows; - for (const auto& [key, value] : window_map_) windows.push_back(value); - return windows; -} -} // namespace cru::platform::native::win diff --git a/src/win/native/WindowManager.hpp b/src/win/native/WindowManager.hpp deleted file mode 100644 index 3f6387f7..00000000 --- a/src/win/native/WindowManager.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once -#include "cru/win/WinPreConfig.hpp" - -#include "cru/common/Base.hpp" - -#include -#include -#include - -namespace cru::platform::native::win { -class WinUiApplication; -class WinNativeWindow; -class WindowClass; - -class WindowManager : public Object { - public: - WindowManager(WinUiApplication* application); - - CRU_DELETE_COPY(WindowManager) - CRU_DELETE_MOVE(WindowManager) - - ~WindowManager() override; - - // Get the general window class for creating ordinary window. - WindowClass* GetGeneralWindowClass() const { - return general_window_class_.get(); - } - - // Register a window newly created. - // This function adds the hwnd to hwnd-window map. - // It should be called immediately after a window was created. - void RegisterWindow(HWND hwnd, WinNativeWindow* window); - - // Unregister a window that is going to be destroyed. - // This function removes the hwnd from the hwnd-window map. - // It should be called immediately before a window is going to be destroyed, - void UnregisterWindow(HWND hwnd); - - // Return a pointer to the Window object related to the HWND or nullptr if the - // hwnd is not in the map. - WinNativeWindow* FromHandle(HWND hwnd); - - std::vector GetAllWindows() const; - - private: - WinUiApplication* application_; - - std::unique_ptr general_window_class_; - std::map window_map_; -}; -} // namespace cru::platform::native::win -- cgit v1.2.3 From b29fb11be2f043a3438a50d8942b4ad7d2af0034 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 3 Dec 2020 22:44:57 +0800 Subject: ... --- include/cru/common/ClonablePtr.hpp | 7 ++- include/cru/common/Event.hpp | 4 +- include/cru/ui/Base.hpp | 12 ++-- include/cru/ui/UiManager.hpp | 5 +- include/cru/ui/controls/Base.hpp | 22 +------ include/cru/ui/controls/Button.hpp | 5 -- include/cru/ui/controls/Control.hpp | 5 ++ include/cru/ui/controls/TextBox.hpp | 14 +---- include/cru/ui/helper/ClickDetector.hpp | 2 +- include/cru/ui/render/BorderRenderObject.hpp | 2 - include/cru/ui/style/Condition.hpp | 42 +++++++++++++ include/cru/ui/style/StyleRuleSet.hpp | 55 +++++++++++++++++ include/cru/ui/style/Styler.hpp | 7 ++- src/ui/UiManager.cpp | 91 ++++++++++++++++------------ src/ui/controls/Button.cpp | 19 +----- src/ui/controls/Control.cpp | 7 +++ src/ui/controls/TextBox.cpp | 29 +-------- src/ui/render/BorderRenderObject.cpp | 9 --- src/ui/style/Condition.cpp | 10 +++ src/ui/style/StyleRuleSet.cpp | 58 ++++++++++++++++++ src/win/gui/Window.cpp | 2 +- 21 files changed, 262 insertions(+), 145 deletions(-) (limited to 'src/ui/UiManager.cpp') diff --git a/include/cru/common/ClonablePtr.hpp b/include/cru/common/ClonablePtr.hpp index 47a1d3bd..5e4b80c9 100644 --- a/include/cru/common/ClonablePtr.hpp +++ b/include/cru/common/ClonablePtr.hpp @@ -8,13 +8,16 @@ namespace cru { template class ClonablePtr { + template + friend class ClonablePtr; + public: using element_type = typename std::unique_ptr::element_type; using pointer = typename std::unique_ptr::pointer; ClonablePtr() = default; ClonablePtr(std::nullptr_t) noexcept : ptr_(nullptr) {} - ClonablePtr(pointer p) noexcept : ptr_(p) {} + explicit ClonablePtr(pointer p) noexcept : ptr_(p) {} ClonablePtr(std::unique_ptr&& p) noexcept : ptr_(std::move(p)) {} template (other.ptr->Clone()); + ptr_ = std::unique_ptr(other.ptr_->Clone()); } return *this; } diff --git a/include/cru/common/Event.hpp b/include/cru/common/Event.hpp index 59502527..7f7b4dd4 100644 --- a/include/cru/common/Event.hpp +++ b/include/cru/common/Event.hpp @@ -98,7 +98,7 @@ struct IBaseEvent { using SpyOnlyHandler = std::function; public: - virtual EventRevoker AddHandler(SpyOnlyHandler handler) = 0; + virtual EventRevoker AddSpyOnlyHandler(SpyOnlyHandler handler) = 0; }; // Provides an interface of event. @@ -147,7 +147,7 @@ class Event : public details::EventBase, public IEvent { CRU_DEFAULT_MOVE(Event) ~Event() = default; - EventRevoker AddHandler(SpyOnlyHandler handler) override { + EventRevoker AddSpyOnlyHandler(SpyOnlyHandler handler) override { const auto token = current_token_++; this->handler_data_list_.emplace_back(token, std::move(handler)); return CreateRevoker(token); diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index 8595258d..57beb723 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -40,6 +40,10 @@ namespace render { class RenderObject; } +namespace style { +class StyleRuleSet; +} + //-------------------- region: basic types -------------------- namespace internal { constexpr int align_start = 0; @@ -87,14 +91,6 @@ inline bool operator!=(const CornerRadius& left, const CornerRadius& right) { return !(left == right); } -struct BorderStyle { - std::shared_ptr border_brush; - Thickness border_thickness; - CornerRadius border_radius; - std::shared_ptr foreground_brush; - std::shared_ptr background_brush; -}; - class CanvasPaintEventArgs { public: CanvasPaintEventArgs(platform::graphics::IPainter* painter, diff --git a/include/cru/ui/UiManager.hpp b/include/cru/ui/UiManager.hpp index 64599d99..e747fcd2 100644 --- a/include/cru/ui/UiManager.hpp +++ b/include/cru/ui/UiManager.hpp @@ -2,6 +2,7 @@ #include "Base.hpp" #include "controls/Base.hpp" +#include "style/StyleRuleSet.hpp" #include #include @@ -13,8 +14,8 @@ struct ThemeResources { std::shared_ptr text_brush; std::shared_ptr text_selection_brush; std::shared_ptr caret_brush; - controls::ButtonStyle button_style; - controls::TextBoxBorderStyle text_box_border_style; + style::StyleRuleSet button_style; + style::StyleRuleSet text_box_style; }; class UiManager : public Object { diff --git a/include/cru/ui/controls/Base.hpp b/include/cru/ui/controls/Base.hpp index 82c31d1e..7c85cdb2 100644 --- a/include/cru/ui/controls/Base.hpp +++ b/include/cru/ui/controls/Base.hpp @@ -1,24 +1,4 @@ #pragma once #include "../Base.hpp" -namespace cru::ui::controls { -using ButtonStateStyle = ui::BorderStyle; - -struct ButtonStyle { - // corresponds to ClickState::None - ButtonStateStyle normal; - // corresponds to ClickState::Hover - ButtonStateStyle hover; - // corresponds to ClickState::Press - ButtonStateStyle press; - // corresponds to ClickState::PressInactive - ButtonStateStyle press_cancel; -}; - -struct TextBoxBorderStyle { - ui::BorderStyle normal; - ui::BorderStyle hover; - ui::BorderStyle focus; - ui::BorderStyle focus_hover; -}; -} // namespace cru::ui::controls +namespace cru::ui::controls {} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Button.hpp b/include/cru/ui/controls/Button.hpp index 7299c146..1c9b1216 100644 --- a/include/cru/ui/controls/Button.hpp +++ b/include/cru/ui/controls/Button.hpp @@ -41,14 +41,9 @@ class Button : public ContentControl, void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; - const ButtonStyle& GetStyle() const { return style_; } - void SetStyle(ButtonStyle style); - private: std::unique_ptr render_object_{}; - ButtonStyle style_; - helper::ClickDetector click_detector_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Control.hpp b/include/cru/ui/controls/Control.hpp index 96aad2bd..0d34bc63 100644 --- a/include/cru/ui/controls/Control.hpp +++ b/include/cru/ui/controls/Control.hpp @@ -66,6 +66,9 @@ class Control : public Object { // null to unset void SetCursor(std::shared_ptr cursor); + public: + style::StyleRuleSet* GetStyleRuleSet(); + //*************** region: events *************** public: // Raised when mouse enter the control. Even when the control itself captures @@ -147,5 +150,7 @@ class Control : public Object { bool is_mouse_over_ = false; std::shared_ptr cursor_ = nullptr; + + std::unique_ptr style_rule_set_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBox.hpp b/include/cru/ui/controls/TextBox.hpp index 91d38c61..75e7cb65 100644 --- a/include/cru/ui/controls/TextBox.hpp +++ b/include/cru/ui/controls/TextBox.hpp @@ -1,5 +1,6 @@ #pragma once #include "NoChildControl.hpp" +#include "IBorderControl.hpp" #include @@ -7,7 +8,7 @@ namespace cru::ui::controls { template class TextControlService; -class TextBox : public NoChildControl { +class TextBox : public NoChildControl, public IBorderControl { public: static constexpr std::u16string_view control_type = u"TextBox"; @@ -29,22 +30,13 @@ class TextBox : public NoChildControl { gsl::not_null GetTextRenderObject(); render::ScrollRenderObject* GetScrollRenderObject(); - const TextBoxBorderStyle& GetBorderStyle(); - void SetBorderStyle(TextBoxBorderStyle border_style); - - protected: - void OnMouseHoverChange(bool newHover) override; - - private: - void UpdateBorderStyle(); + void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; private: std::unique_ptr border_render_object_; std::unique_ptr scroll_render_object_; std::unique_ptr text_render_object_; - TextBoxBorderStyle border_style_; - std::unique_ptr> service_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/helper/ClickDetector.hpp b/include/cru/ui/helper/ClickDetector.hpp index 0df77c60..b58297b1 100644 --- a/include/cru/ui/helper/ClickDetector.hpp +++ b/include/cru/ui/helper/ClickDetector.hpp @@ -71,7 +71,7 @@ class ClickDetector : public Object { private: controls::Control* control_; - ClickState state_; + ClickState state_ = ClickState::None; bool enable_ = true; MouseButton trigger_button_ = mouse_buttons::left | mouse_buttons::right; diff --git a/include/cru/ui/render/BorderRenderObject.hpp b/include/cru/ui/render/BorderRenderObject.hpp index ec0bd52b..3d4f4dad 100644 --- a/include/cru/ui/render/BorderRenderObject.hpp +++ b/include/cru/ui/render/BorderRenderObject.hpp @@ -64,8 +64,6 @@ class BorderRenderObject : public RenderObject { InvalidatePaint(); } - void SetBorderStyle(const BorderStyle& style); - void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style); RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/style/Condition.hpp b/include/cru/ui/style/Condition.hpp index 13ab7764..d5cf16f2 100644 --- a/include/cru/ui/style/Condition.hpp +++ b/include/cru/ui/style/Condition.hpp @@ -21,6 +21,21 @@ class Condition : public Object { virtual Condition* Clone() const = 0; }; +class NoCondition : public Condition { + public: + static ClonablePtr Create() { + return ClonablePtr(new NoCondition); + }; + + std::vector ChangeOn(controls::Control*) const override { + return {}; + } + + bool Judge(controls::Control*) const override { return true; } + + NoCondition* Clone() const override { return new NoCondition; } +}; + class CompoundCondition : public Condition { public: explicit CompoundCondition(std::vector> conditions); @@ -51,6 +66,10 @@ class OrCondition : public CompoundCondition { class FocusCondition : public Condition { public: + static ClonablePtr Create(bool has_focus) { + return ClonablePtr(new FocusCondition(has_focus)); + } + explicit FocusCondition(bool has_focus); std::vector ChangeOn(controls::Control* control) const override; @@ -64,8 +83,31 @@ class FocusCondition : public Condition { bool has_focus_; }; +class HoverCondition : public Condition { + public: + static ClonablePtr Create(bool hover) { + return ClonablePtr(new HoverCondition(hover)); + } + + explicit HoverCondition(bool hover) : hover_(hover) {} + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + HoverCondition* Clone() const override { return new HoverCondition(hover_); } + + private: + bool hover_; +}; + class ClickStateCondition : public Condition { public: + static ClonablePtr Create( + helper::ClickState click_state) { + return ClonablePtr( + new ClickStateCondition(click_state)); + } + explicit ClickStateCondition(helper::ClickState click_state); std::vector ChangeOn(controls::Control* control) const override; diff --git a/include/cru/ui/style/StyleRuleSet.hpp b/include/cru/ui/style/StyleRuleSet.hpp index e69de29b..3ec71730 100644 --- a/include/cru/ui/style/StyleRuleSet.hpp +++ b/include/cru/ui/style/StyleRuleSet.hpp @@ -0,0 +1,55 @@ +#pragma once +#include "StyleRule.hpp" +#include "cru/common/Base.hpp" +#include "cru/common/Event.hpp" +#include "gsl/gsl_assert" + +namespace cru::ui::style { +class StyleRuleSet : public Object { + public: + StyleRuleSet() : control_(nullptr) {} + explicit StyleRuleSet(controls::Control* control) : control_(control) {} + + CRU_DELETE_COPY(StyleRuleSet) + CRU_DELETE_MOVE(StyleRuleSet) + + ~StyleRuleSet() override = default; + + public: + gsl::index GetSize() const { return static_cast(rules_.size()); } + const std::vector& GetRules() const { return rules_; } + + void AddStyleRule(StyleRule rule) { + AddStyleRule(std::move(rule), GetSize()); + } + + void AddStyleRule(StyleRule rule, gsl::index index); + + template + void AddStyleRuleRange(Iter start, Iter end, gsl::index index) { + Expects(index >= 0 && index <= GetSize()); + rules_.insert(rules_.cbegin() + index, std::move(start), std::move(end)); + UpdateChangeListener(); + UpdateStyle(); + } + + void RemoveStyleRule(gsl::index index, gsl::index count = 1); + + void Clear() { RemoveStyleRule(0, GetSize()); } + + void Set(const StyleRuleSet& other); + + const StyleRule& operator[](gsl::index index) const { return rules_[index]; } + + private: + void UpdateChangeListener(); + void UpdateStyle(); + + private: + controls::Control* control_; + + std::vector rules_; + + EventRevokerListGuard guard_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/Styler.hpp b/include/cru/ui/style/Styler.hpp index 2aece114..10b169b1 100644 --- a/include/cru/ui/style/Styler.hpp +++ b/include/cru/ui/style/Styler.hpp @@ -2,19 +2,24 @@ #include "../Base.hpp" #include "ApplyBorderStyleInfo.hpp" #include "cru/common/Base.hpp" +#include "cru/common/ClonablePtr.hpp" #include namespace cru::ui::style { class Styler : public Object { public: - virtual void Apply(controls::Control* control) const; + virtual void Apply(controls::Control* control) const = 0; virtual Styler* Clone() const = 0; }; class BorderStyler : public Styler { public: + static ClonablePtr Create(ApplyBorderStyleInfo style) { + return ClonablePtr(new BorderStyler(std::move(style))); + } + explicit BorderStyler(ApplyBorderStyleInfo style); void Apply(controls::Control* control) const override; diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index 62995f86..bb7f5841 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -5,9 +5,14 @@ #include "cru/platform/graphics/Factory.hpp" #include "cru/platform/graphics/Font.hpp" #include "cru/platform/gui/UiApplication.hpp" +#include "cru/ui/style/ApplyBorderStyleInfo.hpp" +#include "cru/ui/style/Condition.hpp" +#include "cru/ui/style/Styler.hpp" namespace cru::ui { using namespace cru::platform::graphics; +using namespace cru::ui::style; +using namespace cru::ui::helper; namespace { std::unique_ptr CreateSolidColorBrush(IGraphFactory* factory, @@ -35,49 +40,59 @@ UiManager::UiManager() { theme_resource_.default_font = factory->CreateFont(theme_resource_.default_font_family, 24.0f); - const auto black_brush = std::shared_ptr( - CreateSolidColorBrush(factory, colors::black)); + const auto black_brush = + std::shared_ptr( + CreateSolidColorBrush(factory, colors::black)); theme_resource_.text_brush = black_brush; theme_resource_.text_selection_brush = CreateSolidColorBrush(factory, colors::skyblue); theme_resource_.caret_brush = black_brush; - theme_resource_.button_style.normal.border_brush = - CreateSolidColorBrush(factory, Color::FromHex(0x00bfff)); - theme_resource_.button_style.hover.border_brush = - CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff)); - theme_resource_.button_style.press.border_brush = - CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff)); - theme_resource_.button_style.press_cancel.border_brush = - CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff)); - - theme_resource_.button_style.normal.border_thickness = - theme_resource_.button_style.hover.border_thickness = - theme_resource_.button_style.press.border_thickness = - theme_resource_.button_style.press_cancel.border_thickness = - Thickness(3); - - theme_resource_.button_style.normal.border_radius = - theme_resource_.button_style.hover.border_radius = - theme_resource_.button_style.press.border_radius = - theme_resource_.button_style.press_cancel.border_radius = - CornerRadius({5, 5}); - - theme_resource_.text_box_border_style.normal.border_brush = - CreateSolidColorBrush(factory, Color::FromHex(0xced4da)); - theme_resource_.text_box_border_style.normal.border_radius = CornerRadius(5); - theme_resource_.text_box_border_style.normal.border_thickness = Thickness(1); - - theme_resource_.text_box_border_style.hover = - theme_resource_.text_box_border_style.normal; - - theme_resource_.text_box_border_style.focus.border_brush = - CreateSolidColorBrush(factory, Color::FromHex(0x495057)); - theme_resource_.text_box_border_style.focus.border_radius = CornerRadius(5); - theme_resource_.text_box_border_style.focus.border_thickness = Thickness(1); - - theme_resource_.text_box_border_style.focus_hover = - theme_resource_.text_box_border_style.focus; + theme_resource_.button_style.AddStyleRule( + {ClickStateCondition::Create(ClickState::None), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x00bfff)), + Thickness(3), CornerRadius(5), nullptr, nullptr}), + u"DefaultButtonNormal"}); + theme_resource_.button_style.AddStyleRule( + {ClickStateCondition::Create(ClickState::Hover), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff)), + Thickness(3), CornerRadius(5), nullptr, nullptr}), + u"DefaultButtonHover"}); + theme_resource_.button_style.AddStyleRule( + {ClickStateCondition::Create(ClickState::Press), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff)), + Thickness(3), CornerRadius(5), nullptr, nullptr}), + u"DefaultButtonPress"}); + theme_resource_.button_style.AddStyleRule( + {ClickStateCondition::Create(ClickState::PressInactive), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff)), + Thickness(3), CornerRadius(5), nullptr, nullptr}), + u"DefaultButtonPressInactive"}); + + theme_resource_.text_box_style.AddStyleRule( + {HoverCondition::Create(false), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0xced4da)), + Thickness(1), CornerRadius(5), nullptr, nullptr}), + u"DefaultTextBoxNormal"}); + + theme_resource_.text_box_style.AddStyleRule( + {HoverCondition::Create(true), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0xced4da)), + Thickness(1), CornerRadius(5), nullptr, nullptr}), + u"DefaultTextBoxHover"}); + + theme_resource_.text_box_style.AddStyleRule( + {FocusCondition::Create(true), + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x495057)), + Thickness(1), CornerRadius(5), nullptr, nullptr}), + u"DefaultTextBoxHover"}); } UiManager::~UiManager() = default; diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp index 6f19e6b9..7858eadb 100644 --- a/src/ui/controls/Button.cpp +++ b/src/ui/controls/Button.cpp @@ -13,50 +13,37 @@ namespace cru::ui::controls { using cru::platform::gui::SystemCursorType; namespace { -void Set(render::BorderRenderObject* o, const ButtonStateStyle& s) { - o->SetBorderBrush(s.border_brush); - o->SetBorderThickness(s.border_thickness); - o->SetBorderRadius(s.border_radius); - o->SetForegroundBrush(s.foreground_brush); - o->SetBackgroundBrush(s.background_brush); -} - std::shared_ptr GetSystemCursor(SystemCursorType type) { return GetUiApplication()->GetCursorManager()->GetSystemCursor(type); } } // namespace Button::Button() : click_detector_(this) { - style_ = UiManager::GetInstance()->GetThemeResources()->button_style; - render_object_ = std::make_unique(); render_object_->SetAttachedControl(this); SetContainerRenderObject(render_object_.get()); - - Set(render_object_.get(), style_.normal); render_object_->SetBorderEnabled(true); click_detector_.StateChangeEvent()->AddHandler( [this](const helper::ClickState& state) { switch (state) { case helper::ClickState::None: - Set(render_object_.get(), style_.normal); SetCursor(GetSystemCursor(SystemCursorType::Arrow)); break; case helper::ClickState::Hover: - Set(render_object_.get(), style_.hover); SetCursor(GetSystemCursor(SystemCursorType::Hand)); break; case helper::ClickState::Press: - Set(render_object_.get(), style_.press); SetCursor(GetSystemCursor(SystemCursorType::Hand)); break; case helper::ClickState::PressInactive: - Set(render_object_.get(), style_.press_cancel); SetCursor(GetSystemCursor(SystemCursorType::Arrow)); break; } }); + + GetStyleRuleSet()->Set( + UiManager::GetInstance()->GetThemeResources()->button_style); } Button::~Button() = default; diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index c1316a62..1c4ffe51 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -6,6 +6,7 @@ #include "cru/ui/Base.hpp" #include "cru/ui/host/WindowHost.hpp" #include "cru/ui/render/RenderObject.hpp" +#include "cru/ui/style/StyleRuleSet.hpp" #include @@ -15,6 +16,8 @@ using platform::gui::IUiApplication; using platform::gui::SystemCursorType; Control::Control() { + style_rule_set_ = std::make_unique(this); + MouseEnterEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) { this->is_mouse_over_ = true; this->OnMouseHoverChange(true); @@ -94,6 +97,10 @@ void Control::SetCursor(std::shared_ptr cursor) { } } +style::StyleRuleSet* Control::GetStyleRuleSet() { + return style_rule_set_.get(); +} + void Control::AddChild(Control* control, const Index position) { Expects(control->GetParent() == nullptr); // The control already has a parent. diff --git a/src/ui/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp index 6ba6ecb2..031894c0 100644 --- a/src/ui/controls/TextBox.cpp +++ b/src/ui/controls/TextBox.cpp @@ -18,8 +18,6 @@ TextBox::TextBox() scroll_render_object_(new ScrollRenderObject()) { const auto theme_resources = UiManager::GetInstance()->GetThemeResources(); - border_style_ = theme_resources->text_box_border_style; - text_render_object_ = std::make_unique( theme_resources->text_brush, theme_resources->default_font, theme_resources->text_selection_brush, theme_resources->caret_brush); @@ -38,17 +36,8 @@ TextBox::TextBox() service_->SetEditable(true); border_render_object_->SetBorderEnabled(true); - border_render_object_->SetBorderStyle(border_style_.normal); - - GainFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) { - this->service_->SetCaretVisible(true); - this->UpdateBorderStyle(); - }); - LoseFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) { - this->service_->SetCaretVisible(false); - this->UpdateBorderStyle(); - }); + GetStyleRuleSet()->Set(theme_resources->text_box_style); } TextBox::~TextBox() {} @@ -65,19 +54,7 @@ render::ScrollRenderObject* TextBox::GetScrollRenderObject() { return scroll_render_object_.get(); } -const TextBoxBorderStyle& TextBox::GetBorderStyle() { return border_style_; } - -void TextBox::SetBorderStyle(TextBoxBorderStyle border_style) { - border_style_ = std::move(border_style); -} - -void TextBox::OnMouseHoverChange(bool) { UpdateBorderStyle(); } - -void TextBox::UpdateBorderStyle() { - const auto focus = HasFocus(); - const auto hover = IsMouseOver(); - border_render_object_->SetBorderStyle( - focus ? (hover ? border_style_.focus_hover : border_style_.focus) - : (hover ? border_style_.hover : border_style_.normal)); +void TextBox::ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) { + border_render_object_->ApplyBorderStyle(style); } } // namespace cru::ui::controls diff --git a/src/ui/render/BorderRenderObject.cpp b/src/ui/render/BorderRenderObject.cpp index 5abc7832..c176e760 100644 --- a/src/ui/render/BorderRenderObject.cpp +++ b/src/ui/render/BorderRenderObject.cpp @@ -17,15 +17,6 @@ BorderRenderObject::BorderRenderObject() { BorderRenderObject::~BorderRenderObject() {} -void BorderRenderObject::SetBorderStyle(const BorderStyle& style) { - border_brush_ = style.border_brush; - border_thickness_ = style.border_thickness; - border_radius_ = style.border_radius; - foreground_brush_ = style.foreground_brush; - background_brush_ = style.background_brush; - InvalidateLayout(); -} - void BorderRenderObject::ApplyBorderStyle( const style::ApplyBorderStyleInfo& style) { if (style.border_brush != nullptr) border_brush_ = style.border_brush; diff --git a/src/ui/style/Condition.cpp b/src/ui/style/Condition.cpp index 891eb062..f4866c04 100644 --- a/src/ui/style/Condition.cpp +++ b/src/ui/style/Condition.cpp @@ -51,6 +51,16 @@ bool FocusCondition::Judge(controls::Control* control) const { return control->HasFocus() == has_focus_; } +std::vector HoverCondition::ChangeOn( + controls::Control* control) const { + return {control->MouseEnterEvent()->Direct(), + control->MouseLeaveEvent()->Direct()}; +} + +bool HoverCondition::Judge(controls::Control* control) const { + return control->IsMouseOver() == hover_; +} + ClickStateCondition::ClickStateCondition(helper::ClickState click_state) : click_state_(click_state) {} diff --git a/src/ui/style/StyleRuleSet.cpp b/src/ui/style/StyleRuleSet.cpp index e69de29b..403fe114 100644 --- a/src/ui/style/StyleRuleSet.cpp +++ b/src/ui/style/StyleRuleSet.cpp @@ -0,0 +1,58 @@ +#include "cru/ui/style/StyleRuleSet.hpp" +#include "cru/common/Event.hpp" +#include "gsl/gsl_assert" + +#include + +namespace cru::ui::style { +void StyleRuleSet::AddStyleRule(StyleRule rule, gsl::index index) { + Expects(index >= 0 && index <= GetSize()); + + rules_.insert(rules_.cbegin() + index, std::move(rule)); + + UpdateChangeListener(); + UpdateStyle(); +} + +void StyleRuleSet::RemoveStyleRule(gsl::index index, gsl::index count) { + Expects(index >= 0); + Expects(count >= 0 && index + count <= GetSize()); + + rules_.erase(rules_.cbegin() + index, rules_.cbegin() + index + count); + + UpdateChangeListener(); + UpdateStyle(); +} + +void StyleRuleSet::Set(const StyleRuleSet& other) { + rules_ = other.rules_; + UpdateChangeListener(); + UpdateStyle(); +} + +void StyleRuleSet::UpdateChangeListener() { + if (control_ == nullptr) return; + + guard_.Clear(); + + std::unordered_set events; + for (const auto& rule : rules_) { + auto e = rule.GetCondition()->ChangeOn(control_); + events.insert(e.cbegin(), e.cend()); + } + + for (auto e : events) { + guard_ += e->AddSpyOnlyHandler([this] { this->UpdateStyle(); }); + } +} + +void StyleRuleSet::UpdateStyle() { + if (control_ == nullptr) return; + + for (const auto& rule : rules_) { + if (rule.GetCondition()->Judge(control_)) { + rule.GetStyler()->Apply(control_); + } + } +} +} // namespace cru::ui::style diff --git a/src/win/gui/Window.cpp b/src/win/gui/Window.cpp index 174b8931..efd3bfcc 100644 --- a/src/win/gui/Window.cpp +++ b/src/win/gui/Window.cpp @@ -367,9 +367,9 @@ RECT WinNativeWindow::GetClientRectPixel() { } void WinNativeWindow::OnDestroyInternal() { + destroy_event_.Raise(nullptr); application_->GetWindowManager()->UnregisterWindow(hwnd_); hwnd_ = nullptr; - destroy_event_.Raise(nullptr); if (!sync_flag_) { sync_flag_ = true; delete this; -- cgit v1.2.3 From c80808cf38b863f3bd84400eb7cf948d461238e0 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 24 Dec 2020 23:51:15 +0800 Subject: ... --- include/cru/ui/style/ApplyBorderStyleInfo.hpp | 22 ++++++++++++--- src/ui/UiManager.cpp | 39 +++++++++++++++------------ src/ui/render/BorderRenderObject.cpp | 7 ++--- 3 files changed, 45 insertions(+), 23 deletions(-) (limited to 'src/ui/UiManager.cpp') diff --git a/include/cru/ui/style/ApplyBorderStyleInfo.hpp b/include/cru/ui/style/ApplyBorderStyleInfo.hpp index e9c4ca44..5058b51f 100644 --- a/include/cru/ui/style/ApplyBorderStyleInfo.hpp +++ b/include/cru/ui/style/ApplyBorderStyleInfo.hpp @@ -1,12 +1,28 @@ #pragma once +#include #include "../Base.hpp" namespace cru::ui::style { struct ApplyBorderStyleInfo { - std::shared_ptr border_brush; + explicit ApplyBorderStyleInfo( + std::optional> border_brush = + std::nullopt, + std::optional border_thickness = std::nullopt, + std::optional border_radius = std::nullopt, + std::optional> + foreground_brush = std::nullopt, + std::optional> + background_brush = std::nullopt) + : border_brush(std::move(border_brush)), + border_thickness(std::move(border_thickness)), + border_radius(std::move(border_radius)), + foreground_brush(std::move(foreground_brush)), + background_brush(std::move(background_brush)) {} + + std::optional> border_brush; std::optional border_thickness; std::optional border_radius; - std::shared_ptr foreground_brush; - std::shared_ptr background_brush; + std::optional> foreground_brush; + std::optional> background_brush; }; } // namespace cru::ui::style diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index bb7f5841..07812a96 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -1,10 +1,13 @@ #include "cru/ui/UiManager.hpp" +#include #include "Helper.hpp" +#include "cru/platform/GraphBase.hpp" #include "cru/platform/graphics/Brush.hpp" #include "cru/platform/graphics/Factory.hpp" #include "cru/platform/graphics/Font.hpp" #include "cru/platform/gui/UiApplication.hpp" +#include "cru/ui/Base.hpp" #include "cru/ui/style/ApplyBorderStyleInfo.hpp" #include "cru/ui/style/Condition.hpp" #include "cru/ui/style/Styler.hpp" @@ -48,51 +51,53 @@ UiManager::UiManager() { CreateSolidColorBrush(factory, colors::skyblue); theme_resource_.caret_brush = black_brush; + theme_resource_.button_style.AddStyleRule( + {NoCondition::Create(), + BorderStyler::Create(ApplyBorderStyleInfo{std::nullopt, Thickness(3), + CornerRadius(5), std::nullopt, + std::nullopt}), + u"DefaultButton"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::None), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x00bfff)), - Thickness(3), CornerRadius(5), nullptr, nullptr}), + CreateSolidColorBrush(factory, Color::FromHex(0x00bfff))}), u"DefaultButtonNormal"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::Hover), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff)), - Thickness(3), CornerRadius(5), nullptr, nullptr}), + CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff))}), u"DefaultButtonHover"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::Press), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff)), - Thickness(3), CornerRadius(5), nullptr, nullptr}), + CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}), u"DefaultButtonPress"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::PressInactive), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff)), - Thickness(3), CornerRadius(5), nullptr, nullptr}), + CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}), u"DefaultButtonPressInactive"}); + theme_resource_.text_box_style.AddStyleRule( + {NoCondition::Create(), + BorderStyler::Create( + ApplyBorderStyleInfo{std::nullopt, Thickness{1}, CornerRadius{5}}), + u"DefaultTextBox"}); theme_resource_.text_box_style.AddStyleRule( {HoverCondition::Create(false), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0xced4da)), - Thickness(1), CornerRadius(5), nullptr, nullptr}), + CreateSolidColorBrush(factory, Color::FromHex(0xced4da))}), u"DefaultTextBoxNormal"}); - theme_resource_.text_box_style.AddStyleRule( {HoverCondition::Create(true), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0xced4da)), - Thickness(1), CornerRadius(5), nullptr, nullptr}), + CreateSolidColorBrush(factory, Color::FromHex(0xced4da))}), u"DefaultTextBoxHover"}); - theme_resource_.text_box_style.AddStyleRule( {FocusCondition::Create(true), BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x495057)), - Thickness(1), CornerRadius(5), nullptr, nullptr}), - u"DefaultTextBoxHover"}); + CreateSolidColorBrush(factory, Color::FromHex(0x495057))}), + u"DefaultTextBoxFocus"}); } UiManager::~UiManager() = default; diff --git a/src/ui/render/BorderRenderObject.cpp b/src/ui/render/BorderRenderObject.cpp index c176e760..e2c40f0c 100644 --- a/src/ui/render/BorderRenderObject.cpp +++ b/src/ui/render/BorderRenderObject.cpp @@ -6,6 +6,7 @@ #include "cru/platform/graphics/Geometry.hpp" #include "cru/platform/graphics/util/Painter.hpp" #include "cru/ui/style/ApplyBorderStyleInfo.hpp" +#include "gsl/gsl_assert" #include @@ -19,11 +20,11 @@ BorderRenderObject::~BorderRenderObject() {} void BorderRenderObject::ApplyBorderStyle( const style::ApplyBorderStyleInfo& style) { - if (style.border_brush != nullptr) border_brush_ = style.border_brush; + if (style.border_brush) border_brush_ = *style.border_brush; if (style.border_thickness) border_thickness_ = *style.border_thickness; if (style.border_radius) border_radius_ = *style.border_radius; - if (style.foreground_brush) foreground_brush_ = style.foreground_brush; - if (style.background_brush) background_brush_ = style.background_brush; + if (style.foreground_brush) foreground_brush_ = *style.foreground_brush; + if (style.background_brush) background_brush_ = *style.background_brush; InvalidateLayout(); } -- cgit v1.2.3 From d23cdd9c6f2fbec1329c704bde7e183b5ef07e2e Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 24 Dec 2020 23:57:51 +0800 Subject: ... --- src/ui/UiManager.cpp | 6 ++++++ src/ui/components/Menu.cpp | 4 ++++ 2 files changed, 10 insertions(+) (limited to 'src/ui/UiManager.cpp') diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index 07812a96..5b4d4931 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -98,6 +98,12 @@ UiManager::UiManager() { BorderStyler::Create(ApplyBorderStyleInfo{ CreateSolidColorBrush(factory, Color::FromHex(0x495057))}), u"DefaultTextBoxFocus"}); + + theme_resource_.menu_item_style.AddStyleRule( + {NoCondition::Create(), + BorderStyler::Create( + ApplyBorderStyleInfo{std::nullopt, Thickness{0}, CornerRadius{0}}), + u"DefaultMenuItem"}); } UiManager::~UiManager() = default; diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index ea9dcdb6..d45bc44f 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -1,7 +1,9 @@ #include "cru/ui/components/Menu.hpp" +#include "cru/ui/UiManager.hpp" #include "cru/ui/controls/Button.hpp" #include "cru/ui/controls/FlexLayout.hpp" #include "cru/ui/controls/TextBlock.hpp" +#include "cru/ui/style/StyleRuleSet.hpp" #include @@ -10,6 +12,8 @@ MenuItem::MenuItem() { container_ = controls::Button::Create(); text_ = controls::TextBlock::Create(); container_->SetChild(text_); + container_->GetStyleRuleSet()->SetParent( + &UiManager::GetInstance()->GetThemeResources()->menu_item_style); } MenuItem::MenuItem(std::u16string text) : MenuItem() { -- cgit v1.2.3 From a14704fbd9b9fb377b7009a9fbe641a9b8d0fdfb Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 25 Dec 2020 14:43:19 +0800 Subject: ... --- include/cru/ui/style/Styler.hpp | 48 +++++++++++++++++++++++++++++++++++++++++ src/ui/UiManager.cpp | 25 ++++++++++++++------- src/ui/controls/Button.cpp | 27 ----------------------- src/ui/style/Styler.cpp | 14 ++++++++++++ 4 files changed, 79 insertions(+), 35 deletions(-) (limited to 'src/ui/UiManager.cpp') diff --git a/include/cru/ui/style/Styler.hpp b/include/cru/ui/style/Styler.hpp index 10b169b1..865cbbaf 100644 --- a/include/cru/ui/style/Styler.hpp +++ b/include/cru/ui/style/Styler.hpp @@ -3,8 +3,11 @@ #include "ApplyBorderStyleInfo.hpp" #include "cru/common/Base.hpp" #include "cru/common/ClonablePtr.hpp" +#include "cru/platform/gui/Cursor.hpp" +#include "cru/ui/controls/Control.hpp" #include +#include namespace cru::ui::style { class Styler : public Object { @@ -14,6 +17,31 @@ class Styler : public Object { virtual Styler* Clone() const = 0; }; +class CompoundStyler : public Styler { + public: + template + static ClonablePtr Create(ClonablePtr... s) { + return ClonablePtr( + new CompoundStyler(std::vector>{std::move(s)...})); + } + + explicit CompoundStyler(std::vector> stylers) + : stylers_(std::move(stylers)) {} + + void Apply(controls::Control* control) const override { + for (const auto& styler : stylers_) { + styler->Apply(control); + } + } + + virtual CompoundStyler* Clone() const override { + return new CompoundStyler(stylers_); + } + + private: + std::vector> stylers_; +}; + class BorderStyler : public Styler { public: static ClonablePtr Create(ApplyBorderStyleInfo style) { @@ -29,4 +57,24 @@ class BorderStyler : public Styler { private: ApplyBorderStyleInfo style_; }; + +class CursorStyler : public Styler { + public: + static ClonablePtr Create( + std::shared_ptr cursor) { + return ClonablePtr(new CursorStyler(std::move(cursor))); + } + + static ClonablePtr Create(platform::gui::SystemCursorType type); + + explicit CursorStyler(std::shared_ptr cursor) + : cursor_(std::move(cursor)) {} + + void Apply(controls::Control* control) const override; + + CursorStyler* Clone() const override { return new CursorStyler(cursor_); } + + private: + std::shared_ptr cursor_; +}; } // namespace cru::ui::style diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index 5b4d4931..7981aa86 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -6,6 +6,7 @@ #include "cru/platform/graphics/Brush.hpp" #include "cru/platform/graphics/Factory.hpp" #include "cru/platform/graphics/Font.hpp" +#include "cru/platform/gui/Cursor.hpp" #include "cru/platform/gui/UiApplication.hpp" #include "cru/ui/Base.hpp" #include "cru/ui/style/ApplyBorderStyleInfo.hpp" @@ -59,23 +60,31 @@ UiManager::UiManager() { u"DefaultButton"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::None), - BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x00bfff))}), + CompoundStyler::Create( + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x00bfff))}), + CursorStyler::Create(platform::gui::SystemCursorType::Arrow)), u"DefaultButtonNormal"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::Hover), - BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff))}), + CompoundStyler::Create( + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff))}), + CursorStyler::Create(platform::gui::SystemCursorType::Hand)), u"DefaultButtonHover"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::Press), - BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}), + CompoundStyler::Create( + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}), + CursorStyler::Create(platform::gui::SystemCursorType::Hand)), u"DefaultButtonPress"}); theme_resource_.button_style.AddStyleRule( {ClickStateCondition::Create(ClickState::PressInactive), - BorderStyler::Create(ApplyBorderStyleInfo{ - CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}), + CompoundStyler::Create( + BorderStyler::Create(ApplyBorderStyleInfo{ + CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}), + CursorStyler::Create(platform::gui::SystemCursorType::Arrow)), u"DefaultButtonPressInactive"}); theme_resource_.text_box_style.AddStyleRule( diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp index 8bd9f93f..c6480b77 100644 --- a/src/ui/controls/Button.cpp +++ b/src/ui/controls/Button.cpp @@ -1,5 +1,4 @@ #include "cru/ui/controls/Button.hpp" -#include #include "../Helper.hpp" #include "cru/platform/graphics/Brush.hpp" @@ -10,38 +9,12 @@ #include "cru/ui/render/BorderRenderObject.hpp" namespace cru::ui::controls { -using cru::platform::gui::SystemCursorType; - -namespace { -std::shared_ptr GetSystemCursor(SystemCursorType type) { - return GetUiApplication()->GetCursorManager()->GetSystemCursor(type); -} -} // namespace - Button::Button() : click_detector_(this) { render_object_ = std::make_unique(); render_object_->SetAttachedControl(this); SetContainerRenderObject(render_object_.get()); render_object_->SetBorderEnabled(true); - click_detector_.StateChangeEvent()->AddHandler( - [this](const helper::ClickState& state) { - switch (state) { - case helper::ClickState::None: - SetCursor(GetSystemCursor(SystemCursorType::Arrow)); - break; - case helper::ClickState::Hover: - SetCursor(GetSystemCursor(SystemCursorType::Hand)); - break; - case helper::ClickState::Press: - SetCursor(GetSystemCursor(SystemCursorType::Hand)); - break; - case helper::ClickState::PressInactive: - SetCursor(GetSystemCursor(SystemCursorType::Arrow)); - break; - } - }); - GetStyleRuleSet()->SetParent( &UiManager::GetInstance()->GetThemeResources()->button_style); } diff --git a/src/ui/style/Styler.cpp b/src/ui/style/Styler.cpp index 823ac718..da3a2247 100644 --- a/src/ui/style/Styler.cpp +++ b/src/ui/style/Styler.cpp @@ -1,4 +1,9 @@ #include "cru/ui/style/Styler.hpp" + +#include "../Helper.hpp" +#include "cru/common/ClonablePtr.hpp" +#include "cru/platform/gui/Cursor.hpp" +#include "cru/platform/gui/UiApplication.hpp" #include "cru/ui/controls/Control.hpp" #include "cru/ui/controls/IBorderControl.hpp" #include "cru/ui/style/ApplyBorderStyleInfo.hpp" @@ -12,4 +17,13 @@ void BorderStyler::Apply(controls::Control *control) const { border_control->ApplyBorderStyle(style_); } } + +ClonablePtr CursorStyler::Create( + platform::gui::SystemCursorType type) { + return Create(GetUiApplication()->GetCursorManager()->GetSystemCursor(type)); +} + +void CursorStyler::Apply(controls::Control *control) const { + control->SetCursor(cursor_); +} } // namespace cru::ui::style -- cgit v1.2.3