From ee85713e520d5f4295dea6831cd00f74be8f4a61 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 29 Dec 2020 18:46:42 +0100 Subject: [PATCH] LibGUI: Add fixed_size/fixed_width/fixed_height helpers to Widget Fixed sizes are really just shorthands for setting min and max size to the same value. This makes it much nicer to express fixed sizes in GML: Before: @GUI::Widget { horizontal_size_policy: "Fixed" preferred_width: 20 } After: @GUI::Widget { fixed_width: 20 } --- Libraries/LibGUI/Widget.cpp | 4 ++++ Libraries/LibGUI/Widget.h | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Libraries/LibGUI/Widget.cpp b/Libraries/LibGUI/Widget.cpp index 5c8f72c9ff..fba0fb25fa 100644 --- a/Libraries/LibGUI/Widget.cpp +++ b/Libraries/LibGUI/Widget.cpp @@ -145,6 +145,10 @@ Widget::Widget() REGISTER_INT_PROPERTY("min_height", min_height, set_min_height); REGISTER_INT_PROPERTY("max_height", max_height, set_max_height); + REGISTER_INT_PROPERTY("fixed_width", dummy_fixed_width, set_fixed_width); + REGISTER_INT_PROPERTY("fixed_height", dummy_fixed_height, set_fixed_height); + REGISTER_SIZE_PROPERTY("fixed_size", dummy_fixed_size, set_fixed_size); + register_property( "focus_policy", [this]() -> JsonValue { auto policy = focus_policy(); diff --git a/Libraries/LibGUI/Widget.h b/Libraries/LibGUI/Widget.h index 987f8d1d49..d8c0abc0f9 100644 --- a/Libraries/LibGUI/Widget.h +++ b/Libraries/LibGUI/Widget.h @@ -138,6 +138,26 @@ public: void set_max_width(int width) { set_max_size(width, max_height()); } void set_max_height(int height) { set_max_size(max_width(), height); } + void set_fixed_size(const Gfx::IntSize& size) + { + set_min_size(size); + set_max_size(size); + } + + void set_fixed_size(int width, int height) { set_fixed_size({ width, height }); } + + void set_fixed_width(int width) + { + set_min_width(width); + set_max_width(width); + } + + void set_fixed_height(int height) + { + set_min_height(height); + set_max_height(height); + } + Gfx::IntSize preferred_size() const { return m_preferred_size; } void set_preferred_size(const Gfx::IntSize&); void set_preferred_size(int width, int height) { set_preferred_size({ width, height }); } @@ -369,6 +389,11 @@ private: bool load_from_json(const JsonObject&); + // HACK: These are used as property getters for the fixed_* size property aliases. + int dummy_fixed_width() { return 0; } + int dummy_fixed_height() { return 0; } + Gfx::IntSize dummy_fixed_size() { return {}; } + Window* m_window { nullptr }; RefPtr m_layout;