From 42f9f6c0bce5b3bea05edf4e371539fe21ea1c5d Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 13 Sep 2018 23:11:52 +0800 Subject: ... --- CruUI/ui/control.cpp | 9 +++++++-- CruUI/ui/control.h | 7 ++++++- CruUI/ui/controls/text_block.cpp | 30 +++++++++--------------------- CruUI/ui/controls/text_block.h | 3 +-- CruUI/ui/window.cpp | 11 +++++++---- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp index 41693ed8..4d9f0593 100644 --- a/CruUI/ui/control.cpp +++ b/CruUI/ui/control.cpp @@ -28,6 +28,11 @@ namespace cru { } + Control::Control(WindowConstructorTag, Window* window) : Control(true) + { + window_ = window; + } + Control::~Control() { ForeachChild([](auto control) @@ -275,7 +280,7 @@ namespace cru { void Control::OnAddChild(Control* child) { - if (auto window = dynamic_cast(GetAncestor())) + if (auto window = GetWindow()) { child->TraverseDescendants([window](Control* control) { control->OnAttachToWindow(window); @@ -287,7 +292,7 @@ namespace cru { void Control::OnRemoveChild(Control* child) { - if (auto window = dynamic_cast(GetAncestor())) + if (auto window = GetWindow()) { child->TraverseDescendants([window](Control* control) { control->OnDetachToWindow(window); diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index 41401008..0c19f305 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -29,7 +29,10 @@ namespace cru friend class Window; friend class WindowLayoutManager; protected: + struct WindowConstructorTag {}; + explicit Control(bool container = false); + Control(WindowConstructorTag, Window* window); public: Control(const Control& other) = delete; @@ -251,8 +254,10 @@ namespace cru private: bool is_container_; - Window * window_; + protected: + Window * window_; // protected for Window class to write it as itself in constructor. + private: Control * parent_; Vector children_; diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index 04166f32..00850b0b 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -26,12 +26,9 @@ namespace cru TextBlock::TextBlock(const Microsoft::WRL::ComPtr& init_text_format, const Microsoft::WRL::ComPtr& init_brush) : Control(false), - window_deactivated_handler_(new events::UiEvent::EventHandler([this](events::UiEvent::ArgsType& args) + window_deactivated_handler_(new events::UiEvent::EventHandler([this](events::UiEvent::ArgsType& args) { - if (is_selecting_) - { - is_selecting_ = false; - } + is_selecting_ = false; })) { text_format_ = init_text_format; @@ -68,15 +65,14 @@ namespace cru void TextBlock::OnAttachToWindow(Window* window) { - window->deactivated_event.AddHandler([](auto args) - { - - }); + Control::OnAttachToWindow(window); + window->deactivated_event.AddHandler(window_deactivated_handler_); } void TextBlock::OnDetachToWindow(Window* window) { - + Control::OnDetachToWindow(window); + window->deactivated_event.RemoveHandler(window_deactivated_handler_); } void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args) @@ -132,6 +128,7 @@ namespace cru if (args.GetMouseButton() == MouseButton::Left) { RequestFocus(); + selected_range_ = std::nullopt; const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), true); if (hit_test_result.has_value()) { @@ -139,19 +136,16 @@ namespace cru is_selecting_ = true; GetWindow()->CaptureMouseFor(this); } - else - { - selected_range_ = std::nullopt; - } + Repaint(); } Control::OnMouseDownCore(args); } void TextBlock::OnMouseMoveCore(events::MouseEventArgs& args) { + OutputDebugStringW(L"Mouse move!"); if (is_selecting_) { - is_mouse_moved_ = true; const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value(); if (hit_test_result > mouse_down_position_) selected_range_ = TextRange(mouse_down_position_, hit_test_result - mouse_down_position_); @@ -170,12 +164,6 @@ namespace cru { if (is_selecting_) { - if (!is_mouse_moved_) - { - selected_range_ = std::nullopt; - Repaint(); - } - is_mouse_moved_ = false; is_selecting_ = false; GetWindow()->ReleaseCurrentMouseCapture(); } diff --git a/CruUI/ui/controls/text_block.h b/CruUI/ui/controls/text_block.h index 4c1f1fb1..689607ac 100644 --- a/CruUI/ui/controls/text_block.h +++ b/CruUI/ui/controls/text_block.h @@ -128,11 +128,10 @@ namespace cru Vector> text_layout_handlers_; + bool is_selecting_ = false; unsigned mouse_down_position_ = 0; - bool is_mouse_moved_ = false; std::optional selected_range_ = std::nullopt; - bool is_selecting_ = false; events::UiEvent::EventHandlerPtr window_deactivated_handler_; }; diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp index fe37fc46..d43f2e77 100644 --- a/CruUI/ui/window.cpp +++ b/CruUI/ui/window.cpp @@ -91,7 +91,7 @@ namespace cru else if (result != nullptr) return; // find a ancestor of "control", just return } - + cache_invalid_controls_.insert(control); if (cache_invalid_controls_.size() == 1) // when insert just now and not repeat to "InvokeLater". @@ -105,7 +105,7 @@ namespace cru control->CheckAndNotifyPositionChanged(); }); cache_invalid_controls_.clear(); // after update and notify, clear the set. - + }); } } @@ -149,8 +149,8 @@ namespace cru ); } - Window::Window() : Control(true), layout_manager_(new WindowLayoutManager()), control_list_({ this }) { - auto app = Application::GetInstance(); + Window::Window() : Control(WindowConstructorTag{}, this), layout_manager_(new WindowLayoutManager()), control_list_({ this }) { + const auto app = Application::GetInstance(); hwnd_ = CreateWindowEx(0, app->GetWindowManager()->GetGeneralWindowClass()->GetName(), L"", WS_OVERLAPPEDWINDOW, @@ -168,6 +168,9 @@ namespace cru Window::~Window() { Close(); + TraverseDescendants([this](Control* control) { + control->OnDetachToWindow(this); + }); } void Window::Close() { -- cgit v1.2.3