Skip to content

Commit

Permalink
LibGUI: Add fixed_size/fixed_width/fixed_height helpers to Widget
Browse files Browse the repository at this point in the history
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
    }
  • Loading branch information
awesomekling committed Dec 30, 2020
1 parent c9331a9 commit ee85713
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Libraries/LibGUI/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions Libraries/LibGUI/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ class Widget : public Core::Object {
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 }); }
Expand Down Expand Up @@ -369,6 +389,11 @@ class Widget : public Core::Object {

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<Layout> m_layout;

Expand Down

0 comments on commit ee85713

Please sign in to comment.