diff options
author | crupest <crupest@outlook.com> | 2022-02-16 23:04:27 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-16 23:04:27 +0800 |
commit | 9307bd1af7c52aaf9c6d25b598bf3069a63c32a1 (patch) | |
tree | a3e485ad60a0114f42dfe3c8d4c64bee87c91ba5 /src/ui/controls/Control.cpp | |
parent | f75ab0bd662c73d15057d746347d09bf94a992a6 (diff) | |
download | cru-9307bd1af7c52aaf9c6d25b598bf3069a63c32a1.tar.gz cru-9307bd1af7c52aaf9c6d25b598bf3069a63c32a1.tar.bz2 cru-9307bd1af7c52aaf9c6d25b598bf3069a63c32a1.zip |
...
Diffstat (limited to 'src/ui/controls/Control.cpp')
-rw-r--r-- | src/ui/controls/Control.cpp | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index a66b605f..43faba69 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -27,21 +27,13 @@ Control::Control() { }); } -Control::~Control() { ReleaseMouse(); } - -host::WindowHost* Control::GetWindowHost() const { - auto parent = GetParent(); - if (parent) { - return parent->GetWindowHost(); - } - return nullptr; -} +Control::~Control() { RemoveFromParent(); } void Control::SetParent(Control* parent) { if (parent_ == parent) return; auto old_parent = parent_; parent_ = parent; - OnParentChanged(old_parent, parent); + OnParentChangedCore(old_parent, parent); } void Control::RemoveFromParent() { @@ -109,4 +101,34 @@ void Control::SetCursor(std::shared_ptr<ICursor> cursor) { std::shared_ptr<style::StyleRuleSet> Control::GetStyleRuleSet() { return style_rule_set_; } + +void Control::OnParentChangedCore(Control* old_parent, Control* new_parent) { + auto new_window_host = + new_parent == nullptr ? nullptr : new_parent->GetWindowHost(); + if (window_host_ != new_window_host) { + auto old_host = window_host_; + window_host_ = new_window_host; + OnWindowHostChangedCore(old_host, new_window_host); + } + + OnParentChanged(old_parent, new_parent); +} + +void Control::OnWindowHostChangedCore(host::WindowHost* old_host, + host::WindowHost* new_host) { + if (old_host != nullptr) { + if (old_host->GetMouseCaptureControl() == this) { + old_host->CaptureMouseFor(nullptr); + } + if (old_host->GetMouseHoverControl() == this) { + old_host->mouse_hover_control_ = nullptr; + } + } + + ForEachChild([old_host, new_host](Control* child) { + child->window_host_ = new_host; + child->OnWindowHostChangedCore(old_host, new_host); + }); + OnWindowHostChanged(old_host, new_host); +} } // namespace cru::ui::controls |