diff options
author | crupest <crupest@outlook.com> | 2020-03-19 19:45:26 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-03-19 19:45:26 +0800 |
commit | 507de8c6a931d0784c8f740d41db610d3ed8db68 (patch) | |
tree | 9f77d8e2b1b0955ff669677b4d5913a08075946a | |
parent | 5da4f511e85de9e79bee40e3c5e04f899a48723c (diff) | |
download | cru-507de8c6a931d0784c8f740d41db610d3ed8db68.tar.gz cru-507de8c6a931d0784c8f740d41db610d3ed8db68.tar.bz2 cru-507de8c6a931d0784c8f740d41db610d3ed8db68.zip |
...
-rw-r--r-- | include/cru/platform/native/window.hpp | 3 | ||||
-rw-r--r-- | include/cru/ui/control.hpp | 8 | ||||
-rw-r--r-- | include/cru/ui/controls/text_box.hpp | 6 | ||||
-rw-r--r-- | include/cru/ui/render/border_render_object.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/ui_event.hpp | 11 | ||||
-rw-r--r-- | include/cru/ui/window.hpp | 1 | ||||
-rw-r--r-- | include/cru/win/native/window.hpp | 2 | ||||
-rw-r--r-- | src/ui/control.cpp | 14 | ||||
-rw-r--r-- | src/ui/controls/text_box.cpp | 22 | ||||
-rw-r--r-- | src/ui/render/border_render_object.cpp | 9 | ||||
-rw-r--r-- | src/ui/window.cpp | 9 | ||||
-rw-r--r-- | src/win/native/window.cpp | 6 |
12 files changed, 76 insertions, 17 deletions
diff --git a/include/cru/platform/native/window.hpp b/include/cru/platform/native/window.hpp index 85f809e5..0d40702f 100644 --- a/include/cru/platform/native/window.hpp +++ b/include/cru/platform/native/window.hpp @@ -3,6 +3,8 @@ #include "base.hpp" #include "cru/common/event.hpp" +#include <string> + namespace cru::platform::native { // Represents a native window, which exposes some low-level events and // operations. @@ -55,6 +57,7 @@ struct INativeWindow : virtual INativeResource { virtual IEvent<NativeMouseButtonEventArgs>* MouseUpEvent() = 0; virtual IEvent<int>* KeyDownEvent() = 0; virtual IEvent<int>* KeyUpEvent() = 0; + virtual IEvent<std::string>* CharEvent() = 0; }; // See INativeWindow for more info. diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp index 95e2cf52..f0475dea 100644 --- a/include/cru/ui/control.hpp +++ b/include/cru/ui/control.hpp @@ -111,9 +111,7 @@ class Control : public Object { event::RoutedEvent<event::KeyEventArgs>* KeyUpEvent() { return &key_up_event_; } - // event::RoutedEvent<event::CharEventArgs>* CharEvent() { - // return &char_event_; - // } + event::RoutedEvent<event::CharEventArgs>* CharEvent() { return &char_event_; } event::RoutedEvent<event::FocusChangeEventArgs>* GainFocusEvent() { return &gain_focus_event_; } @@ -131,7 +129,7 @@ class Control : public Object { event::RoutedEvent<event::KeyEventArgs> key_down_event_; event::RoutedEvent<event::KeyEventArgs> key_up_event_; - // event::RoutedEvent<event::CharEventArgs> char_event_; + event::RoutedEvent<event::CharEventArgs> char_event_; event::RoutedEvent<event::FocusChangeEventArgs> gain_focus_event_; event::RoutedEvent<event::FocusChangeEventArgs> lose_focus_event_; @@ -142,6 +140,8 @@ class Control : public Object { virtual void OnAttachToWindow(Window* window); virtual void OnDetachToWindow(Window* window); + virtual void OnMouseHoverChange(bool newHover) { CRU_UNUSED(newHover) } + private: Window* window_ = nullptr; Control* parent_ = nullptr; diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp index 4a4ed6e7..f5347430 100644 --- a/include/cru/ui/controls/text_box.hpp +++ b/include/cru/ui/controls/text_box.hpp @@ -28,6 +28,12 @@ class TextBox : public NoChildControl { const TextBoxBorderStyle& GetBorderStyle(); void SetBorderStyle(TextBoxBorderStyle border_style); + protected: + void OnMouseHoverChange(bool newHover) override; + + private: + void UpdateBorderStyle(); + private: std::unique_ptr<render::BorderRenderObject> border_render_object_; std::unique_ptr<render::StackLayoutRenderObject> stack_layout_render_object_; diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp index 259c1530..02672309 100644 --- a/include/cru/ui/render/border_render_object.hpp +++ b/include/cru/ui/render/border_render_object.hpp @@ -60,6 +60,8 @@ class BorderRenderObject : public RenderObject { InvalidatePaint(); } + void SetBorderStyle(const BorderStyle& style); + void Draw(platform::graph::IPainter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/ui_event.hpp b/include/cru/ui/ui_event.hpp index c5af2b61..d7ab4543 100644 --- a/include/cru/ui/ui_event.hpp +++ b/include/cru/ui/ui_event.hpp @@ -5,6 +5,7 @@ #include <memory> #include <optional> +#include <string> #include <type_traits> namespace cru::platform::graph { @@ -195,21 +196,19 @@ class KeyEventArgs : public UiEventArgs { int virtual_code_; }; -/* class CharEventArgs : public UiEventArgs { public: - CharEventArgs(Object* sender, Object* original_sender, wchar_t c) - : UiEventArgs(sender, original_sender), c_(c) {} + CharEventArgs(Object* sender, Object* original_sender, std::string c) + : UiEventArgs(sender, original_sender), c_(std::move(c)) {} CharEventArgs(const CharEventArgs& other) = default; CharEventArgs(CharEventArgs&& other) = default; CharEventArgs& operator=(const CharEventArgs& other) = default; CharEventArgs& operator=(CharEventArgs&& other) = default; ~CharEventArgs() override = default; - wchar_t GetChar() const { return c_; } + std::string GetChar() const { return c_; } private: - wchar_t c_; + std::string c_; }; -*/ } // namespace cru::ui::event diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 251712cf..ae54d006 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -96,6 +96,7 @@ class Window final : public ContentControl, public SelfResolvable<Window> { void OnNativeKeyDown(platform::native::INativeWindow* window, int virtual_code); void OnNativeKeyUp(platform::native::INativeWindow* window, int virtual_code); + void OnNativeChar(platform::native::INativeWindow* window, std::string c); //*************** region: event dispatcher helper *************** diff --git a/include/cru/win/native/window.hpp b/include/cru/win/native/window.hpp index 3c883338..d5f41e47 100644 --- a/include/cru/win/native/window.hpp +++ b/include/cru/win/native/window.hpp @@ -75,6 +75,7 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { } IEvent<int>* KeyDownEvent() override { return &key_down_event_; } IEvent<int>* KeyUpEvent() override { return &key_up_event_; } + IEvent<std::string>* CharEvent() override { return &char_event_; }; IEvent<WindowNativeMessageEventArgs&>* NativeMessageEvent() { return &native_message_event_; @@ -148,6 +149,7 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { Event<platform::native::NativeMouseButtonEventArgs> mouse_up_event_; Event<int> key_down_event_; Event<int> key_up_event_; + Event<std::string> char_event_; Event<WindowNativeMessageEventArgs&> native_message_event_; }; diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 04d89b5f..e3b7b967 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -16,11 +16,15 @@ using platform::native::IUiApplication; using platform::native::SystemCursorType; Control::Control() { - MouseEnterEvent()->Direct()->AddHandler( - [this](event::MouseEventArgs&) { this->is_mouse_over_ = true; }); - - MouseLeaveEvent()->Direct()->AddHandler( - [this](event::MouseEventArgs&) { this->is_mouse_over_ = false; }); + MouseEnterEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) { + this->is_mouse_over_ = true; + this->OnMouseHoverChange(true); + }); + + MouseLeaveEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) { + this->is_mouse_over_ = false; + this->OnMouseHoverChange(true); + }); } void Control::_SetParent(Control* parent) { diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp index 1e0890b8..99164b9b 100644 --- a/src/ui/controls/text_box.cpp +++ b/src/ui/controls/text_box.cpp @@ -36,6 +36,16 @@ TextBox::TextBox() stack_layout_render_object_->SetAttachedControl(this); text_render_object_->SetAttachedControl(this); caret_render_object_->SetAttachedControl(this); + + GainFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) { + this->service_->SetEnabled(true); + this->UpdateBorderStyle(); + }); + + LoseFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) { + this->service_->SetEnabled(false); + this->UpdateBorderStyle(); + }); } TextBox::~TextBox() {} @@ -55,6 +65,16 @@ std::shared_ptr<platform::graph::IBrush> TextBox::GetCaretBrush() { const TextBoxBorderStyle& TextBox::GetBorderStyle() { return border_style_; } void TextBox::SetBorderStyle(TextBoxBorderStyle border_style) { - border_style_ = std::move(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)); } } // namespace cru::ui::controls diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 16f2828a..5b6cdb3c 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -39,6 +39,15 @@ void BorderRenderObject::Draw(platform::graph::IPainter* painter) { foreground_brush_.get()); } +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(); +} + RenderObject* BorderRenderObject::HitTest(const Point& point) { if (const auto child = GetChild()) { auto offset = child->GetOffset(); diff --git a/src/ui/window.cpp b/src/ui/window.cpp index bf2e24ca..03c33cf3 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -32,6 +32,7 @@ CRU_DEFINE_EVENT_NAME(MouseDown) CRU_DEFINE_EVENT_NAME(MouseUp) CRU_DEFINE_EVENT_NAME(KeyDown) CRU_DEFINE_EVENT_NAME(KeyUp) +CRU_DEFINE_EVENT_NAME(Char) #undef CRU_DEFINE_EVENT_NAME } // namespace event_names @@ -316,6 +317,14 @@ void Window::OnNativeKeyUp(INativeWindow* window, int virtual_code) { nullptr, virtual_code); } +void Window::OnNativeChar(platform::native::INativeWindow* window, + std::string c) { + CRU_UNUSED(window) + + DispatchEvent(event_names::Char, focus_control_, &Control::CharEvent, nullptr, + std::move(c)); +} + void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, Control* new_control, const Point& point, diff --git a/src/win/native/window.cpp b/src/win/native/window.cpp index 2e99a5cb..c7bb1b9d 100644 --- a/src/win/native/window.cpp +++ b/src/win/native/window.cpp @@ -8,6 +8,7 @@ #include "cru/win/native/ui_application.hpp" #include "cru/win/native/window_class.hpp" #include "cru/win/native/window_render_target.hpp" +#include "cru/win/string.hpp" #include "dpi_util.hpp" #include "window_d2d_painter.hpp" #include "window_manager.hpp" @@ -404,7 +405,10 @@ void WinNativeWindow::OnKeyUpInternal(int virtual_code) { key_up_event_.Raise(virtual_code); } -void WinNativeWindow::OnCharInternal(wchar_t c) { CRU_UNUSED(c) } +void WinNativeWindow::OnCharInternal(wchar_t c) { + wchar_t s[2] = {c, 0}; + char_event_.Raise(platform::win::ToUtf8String(s)); +} void WinNativeWindow::OnActivatedInternal() {} |