aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-11-20 21:25:29 +0800
committercrupest <crupest@outlook.com>2021-11-20 21:25:29 +0800
commit1a53ed0791d9793ed8030d3a44e833e5e7c4542b (patch)
tree28aedaabee675b192e414489122349ad4985a31b /src
parenta9da4c10459e3c45115c8a42e771b00cb1caeab7 (diff)
downloadcru-1a53ed0791d9793ed8030d3a44e833e5e7c4542b.tar.gz
cru-1a53ed0791d9793ed8030d3a44e833e5e7c4542b.tar.bz2
cru-1a53ed0791d9793ed8030d3a44e833e5e7c4542b.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/osx/graphics/quartz/TextLayout.cpp2
-rw-r--r--src/osx/gui/Window.mm17
-rw-r--r--src/ui/components/Menu.cpp37
-rw-r--r--src/ui/host/WindowHost.cpp14
4 files changed, 61 insertions, 9 deletions
diff --git a/src/osx/graphics/quartz/TextLayout.cpp b/src/osx/graphics/quartz/TextLayout.cpp
index a5607854..c3a138f2 100644
--- a/src/osx/graphics/quartz/TextLayout.cpp
+++ b/src/osx/graphics/quartz/TextLayout.cpp
@@ -115,6 +115,8 @@ void OsxCTTextLayout::SetEditMode(bool enable) {
}
Rect OsxCTTextLayout::GetTextBounds(bool includingTrailingSpace) {
+ if (text_.empty() && edit_mode_) return Rect(0, 0, 0, font_->GetFontSize());
+
auto result = DoGetTextBoundsIncludingEmptyLines(includingTrailingSpace);
return Rect(0, 0, result.size.width, result.size.height);
}
diff --git a/src/osx/gui/Window.mm b/src/osx/gui/Window.mm
index 0112fd43..9f0d9742 100644
--- a/src/osx/gui/Window.mm
+++ b/src/osx/gui/Window.mm
@@ -71,7 +71,13 @@ void OsxWindowPrivate::OnWindowDidResize() {
[view addTrackingArea:tracking_area];
CGLayerRelease(draw_layer_);
- draw_layer_ = CGLayerCreateWithContext(nullptr, rect.size, nullptr);
+
+ // If size is 0 then create layer will fail.
+ auto s = rect.size;
+ if (s.width == 0) s.width = 1;
+ if (s.height == 0) s.height = 1;
+
+ draw_layer_ = CGLayerCreateWithContext(nullptr, s, nullptr);
Ensures(draw_layer_);
resize_event_.Raise(osx_window_->GetClientSize());
@@ -252,8 +258,13 @@ void OsxWindow::CreateWindow() {
[p_->window_ setContentView:content_view];
- p_->draw_layer_ = CGLayerCreateWithContext(
- nullptr, cru::platform::graphics::osx::quartz::Convert(p_->content_rect_.GetSize()), nullptr);
+ // If size is 0 then create layer will fail.
+ auto s = p_->content_rect_.GetSize();
+ if (s.width == 0) s.width = 1;
+ if (s.height == 0) s.height = 1;
+
+ p_->draw_layer_ =
+ CGLayerCreateWithContext(nullptr, cru::platform::graphics::osx::quartz::Convert(s), nullptr);
Ensures(p_->draw_layer_);
RequestRepaint();
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{});