diff options
author | crupest <crupest@outlook.com> | 2021-11-20 21:25:29 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-11-20 21:25:29 +0800 |
commit | 1a53ed0791d9793ed8030d3a44e833e5e7c4542b (patch) | |
tree | 28aedaabee675b192e414489122349ad4985a31b /src/ui | |
parent | a9da4c10459e3c45115c8a42e771b00cb1caeab7 (diff) | |
download | cru-1a53ed0791d9793ed8030d3a44e833e5e7c4542b.tar.gz cru-1a53ed0791d9793ed8030d3a44e833e5e7c4542b.tar.bz2 cru-1a53ed0791d9793ed8030d3a44e833e5e7c4542b.zip |
...
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/components/Menu.cpp | 37 | ||||
-rw-r--r-- | src/ui/host/WindowHost.cpp | 14 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index afd1ab71..af54c46c 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -1,12 +1,12 @@ #include "cru/ui/components/Menu.hpp" #include "cru/ui/UiManager.hpp" #include "cru/ui/controls/Button.hpp" +#include "cru/ui/controls/Control.hpp" #include "cru/ui/controls/FlexLayout.hpp" #include "cru/ui/controls/TextBlock.hpp" +#include "cru/ui/host/WindowHost.hpp" #include "cru/ui/style/StyleRuleSet.hpp" -#include <string> - namespace cru::ui::components { MenuItem::MenuItem() { container_ = controls::Button::Create(); @@ -56,4 +56,37 @@ Component* Menu::RemoveItem(gsl::index index) { return item; } + +void Menu::AddTextItem(String text, gsl::index index) { + MenuItem* item = new MenuItem(std::move(text)); + AddItem(item, index); +} + +PopupMenu::PopupMenu(controls::Control* attached_control) + : attached_control_(attached_control) { + popup_ = controls::Popup::Create(attached_control); + + menu_ = new Menu(); + + popup_->AddChild(menu_->GetRootControl(), 0); +} + +PopupMenu::~PopupMenu() { + delete menu_; + + if (!popup_->GetWindowHost()) { + delete popup_; + } +} + +controls::Control* PopupMenu::GetRootControl() { return popup_; } + +void PopupMenu::SetPosition(const Point& position) { + popup_->SetRect(Rect{position, {}}); +} + +void PopupMenu::Show() { + popup_->GetWindowHost()->RelayoutWithSize(Size::Infinate(), true); + popup_->Show(); +} } // namespace cru::ui::components diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp index f60e2809..d26b43ab 100644 --- a/src/ui/host/WindowHost.cpp +++ b/src/ui/host/WindowHost.cpp @@ -180,18 +180,24 @@ void WindowHost::SetLayoutPreferToFillWindow(bool value) { void WindowHost::Relayout() { const auto available_size = - native_window_ ? native_window_->GetClientSize() - : Size{100, 100}; // a reasonable assumed size - Relayout(available_size); + native_window_ ? native_window_->GetClientSize() : Size::Infinate(); + RelayoutWithSize(available_size); } -void WindowHost::Relayout(const Size& available_size) { +void WindowHost::RelayoutWithSize(const Size& available_size, + bool set_window_size_to_fit_content) { root_render_object_->Measure( render::MeasureRequirement{available_size, IsLayoutPreferToFillWindow() ? render::MeasureSize(available_size) : render::MeasureSize::NotSpecified()}, render::MeasureSize::NotSpecified()); + + if (set_window_size_to_fit_content) { + auto rect = GetWindowRect(); + SetWindowRect({rect.GetLeftTop(), root_render_object_->GetSize()}); + } + root_render_object_->Layout(Point{}); for (auto& action : after_layout_stable_action_) action(); after_layout_event_.Raise(AfterLayoutEventArgs{}); |