diff options
author | crupest <crupest@outlook.com> | 2020-04-24 00:03:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-04-24 00:03:16 +0800 |
commit | 75ff8a6a05afd02aaadf7e3049b0a0e305241182 (patch) | |
tree | 5444bbb3ef80036cc38a827b8ccf03f48b310728 /include | |
parent | 922d7f6c96f81a33538900f8a8992a5b6f640874 (diff) | |
download | cru-75ff8a6a05afd02aaadf7e3049b0a0e305241182.tar.gz cru-75ff8a6a05afd02aaadf7e3049b0a0e305241182.tar.bz2 cru-75ff8a6a05afd02aaadf7e3049b0a0e305241182.zip |
...
Diffstat (limited to 'include')
-rw-r--r-- | include/cru/platform/graph/text_layout.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/native/ui_application.hpp | 15 | ||||
-rw-r--r-- | include/cru/ui/controls/text_block.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/controls/text_box.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/render/text_render_object.hpp | 40 | ||||
-rw-r--r-- | include/cru/win/graph/direct/text_layout.hpp | 2 | ||||
-rw-r--r-- | include/cru/win/native/ui_application.hpp | 10 |
7 files changed, 47 insertions, 32 deletions
diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp index 0d104742..d91834c0 100644 --- a/include/cru/platform/graph/text_layout.hpp +++ b/include/cru/platform/graph/text_layout.hpp @@ -17,7 +17,7 @@ struct ITextLayout : virtual IGraphResource { virtual Rect GetTextBounds() = 0; virtual std::vector<Rect> TextRangeRect(const TextRange& text_range) = 0; - virtual Point TextSingleRect(gsl::index position, bool trailing) = 0; + virtual Point TextSinglePoint(gsl::index position, bool trailing) = 0; virtual TextHitTestResult HitTest(const Point& point) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/native/ui_application.hpp b/include/cru/platform/native/ui_application.hpp index 006255db..afcc7117 100644 --- a/include/cru/platform/native/ui_application.hpp +++ b/include/cru/platform/native/ui_application.hpp @@ -32,11 +32,16 @@ struct IUiApplication : public virtual INativeResource { virtual void AddOnQuitHandler(std::function<void()> handler) = 0; virtual void InvokeLater(std::function<void()> action) = 0; - virtual unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - std::function<void()> action) = 0; - virtual unsigned long SetInterval(std::chrono::milliseconds milliseconds, - std::function<void()> action) = 0; - virtual void CancelTimer(unsigned long id) = 0; + // Timer id should always be positive and never the same. So it's ok to use + // negative value to represent no timer. + virtual long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function<void()> action) = 0; + virtual long long SetInterval(std::chrono::milliseconds milliseconds, + std::function<void()> action) = 0; + // Implementation should guarantee calls on timer id already canceled have no + // effects and do not crash. Also canceling negative id should always result + // in no-op. + virtual void CancelTimer(long long id) = 0; virtual std::vector<INativeWindow*> GetAllWindow() = 0; virtual std::shared_ptr<INativeWindowResolver> CreateWindow( diff --git a/include/cru/ui/controls/text_block.hpp b/include/cru/ui/controls/text_block.hpp index ec61a08c..61f568c4 100644 --- a/include/cru/ui/controls/text_block.hpp +++ b/include/cru/ui/controls/text_block.hpp @@ -29,14 +29,9 @@ class TextBlock : public NoChildControl { void SetText(std::string text); render::TextRenderObject* GetTextRenderObject(); - render::CanvasRenderObject* GetCaretRenderObject(); - std::shared_ptr<platform::graph::IBrush> GetCaretBrush(); private: - std::unique_ptr<render::StackLayoutRenderObject> root_render_object_; std::unique_ptr<render::TextRenderObject> text_render_object_; - std::unique_ptr<render::CanvasRenderObject> caret_render_object_; - std::shared_ptr<platform::graph::IBrush> caret_brush_; std::unique_ptr<TextControlService<TextBlock>> service_; }; diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp index f5347430..c0160658 100644 --- a/include/cru/ui/controls/text_box.hpp +++ b/include/cru/ui/controls/text_box.hpp @@ -22,8 +22,6 @@ class TextBox : public NoChildControl { std::string_view GetControlType() const final { return control_type; } render::TextRenderObject* GetTextRenderObject(); - render::CanvasRenderObject* GetCaretRenderObject(); - std::shared_ptr<platform::graph::IBrush> GetCaretBrush(); const TextBoxBorderStyle& GetBorderStyle(); void SetBorderStyle(TextBoxBorderStyle border_style); @@ -36,11 +34,8 @@ class TextBox : public NoChildControl { private: std::unique_ptr<render::BorderRenderObject> border_render_object_; - std::unique_ptr<render::StackLayoutRenderObject> stack_layout_render_object_; std::unique_ptr<render::TextRenderObject> text_render_object_; - std::unique_ptr<render::CanvasRenderObject> caret_render_object_; - std::shared_ptr<platform::graph::IBrush> caret_brush_; TextBoxBorderStyle border_style_; std::unique_ptr<TextControlService<TextBox>> service_; diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp index c062c9f0..4b1e91e0 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -6,9 +6,13 @@ namespace cru::ui::render { class TextRenderObject : public RenderObject { public: + constexpr static float default_caret_width = 2; + + public: TextRenderObject(std::shared_ptr<platform::graph::IBrush> brush, std::shared_ptr<platform::graph::IFont> font, - std::shared_ptr<platform::graph::IBrush> selection_brush); + std::shared_ptr<platform::graph::IBrush> selection_brush, + std::shared_ptr<platform::graph::IBrush> caret_brush); TextRenderObject(const TextRenderObject& other) = delete; TextRenderObject(TextRenderObject&& other) = delete; TextRenderObject& operator=(const TextRenderObject& other) = delete; @@ -19,30 +23,41 @@ class TextRenderObject : public RenderObject { void SetText(std::string new_text); std::shared_ptr<platform::graph::IBrush> GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr<platform::graph::IBrush> new_brush) { - new_brush.swap(brush_); - } + void SetBrush(std::shared_ptr<platform::graph::IBrush> new_brush); std::shared_ptr<platform::graph::IFont> GetFont() const; void SetFont(std::shared_ptr<platform::graph::IFont> font); std::vector<Rect> TextRangeRect(const TextRange& text_range); - Point TextSingleRect(gsl::index position, bool trailing); + Point TextSinglePoint(gsl::index position, bool trailing); platform::graph::TextHitTestResult TextHitTest(const Point& point); std::optional<TextRange> GetSelectionRange() const { return selection_range_; } - void SetSelectionRange(std::optional<TextRange> new_range) { - selection_range_ = std::move(new_range); - } + void SetSelectionRange(std::optional<TextRange> new_range); std::shared_ptr<platform::graph::IBrush> GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(std::shared_ptr<platform::graph::IBrush> new_brush) { - new_brush.swap(selection_brush_); + void SetSelectionBrush(std::shared_ptr<platform::graph::IBrush> new_brush); + + bool IsDrawCaret() const { return draw_caret_; } + void SetDrawCaret(bool draw_caret); + void ToggleDrawCaret() { SetDrawCaret(!IsDrawCaret()); } + + // Caret position can be any value. When it is negative, 0 is used. When it + // exceeds the size of the string, the size of the string is used. + gsl::index GetCaretPosition() const { return caret_position_; } + void SetCaretPosition(gsl::index position); + + std::shared_ptr<platform::graph::IBrush> GetCaretBrush() const { + return caret_brush_; } + void GetCaretBrush(std::shared_ptr<platform::graph::IBrush> brush); + + float GetCaretWidth() const { return caret_width_; } + void SetCaretWidth(float width); void Draw(platform::graph::IPainter* painter) override; @@ -61,5 +76,10 @@ class TextRenderObject : public RenderObject { std::optional<TextRange> selection_range_ = std::nullopt; std::shared_ptr<platform::graph::IBrush> selection_brush_; + + bool draw_caret_ = false; + gsl::index caret_position_ = 0; + std::shared_ptr<platform::graph::IBrush> caret_brush_; + float caret_width_ = default_caret_width; }; } // namespace cru::ui::render diff --git a/include/cru/win/graph/direct/text_layout.hpp b/include/cru/win/graph/direct/text_layout.hpp index 90faadf9..2870db96 100644 --- a/include/cru/win/graph/direct/text_layout.hpp +++ b/include/cru/win/graph/direct/text_layout.hpp @@ -40,7 +40,7 @@ class DWriteTextLayout : public DirectGraphResource, // 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<Rect> TextRangeRect(const TextRange& text_range) override; - Point TextSingleRect(gsl::index position, bool trailing) override; + Point TextSinglePoint(gsl::index position, bool trailing) override; TextHitTestResult HitTest(const Point& point) override; private: diff --git a/include/cru/win/native/ui_application.hpp b/include/cru/win/native/ui_application.hpp index 1c54cc05..250e855c 100644 --- a/include/cru/win/native/ui_application.hpp +++ b/include/cru/win/native/ui_application.hpp @@ -33,11 +33,11 @@ class WinUiApplication : public WinNativeResource, void AddOnQuitHandler(std::function<void()> handler) override; void InvokeLater(std::function<void()> action) override; - unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - std::function<void()> action) override; - unsigned long SetInterval(std::chrono::milliseconds milliseconds, - std::function<void()> action) override; - void CancelTimer(unsigned long id) override; + long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function<void()> action) override; + long long SetInterval(std::chrono::milliseconds milliseconds, + std::function<void()> action) override; + void CancelTimer(long long id) override; std::vector<INativeWindow*> GetAllWindow() override; std::shared_ptr<INativeWindowResolver> CreateWindow( |