diff options
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r-- | Userland/Libraries/LibGUI/Button.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Button.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/CheckBox.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/ColorInput.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/ComboBox.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Label.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Label.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/MessageBox.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/RadioButton.cpp | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/RadioButton.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/SpinBox.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextBox.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Toolbar.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Toolbar.h | 2 |
16 files changed, 84 insertions, 17 deletions
diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp index d1fc40ab87..c17791f7f4 100644 --- a/Userland/Libraries/LibGUI/Button.cpp +++ b/Userland/Libraries/LibGUI/Button.cpp @@ -23,8 +23,8 @@ namespace GUI { Button::Button(String text) : AbstractButton(move(text)) { - set_min_width(32); - set_fixed_height(22); + set_min_size({ 40, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); set_focus_policy(GUI::FocusPolicy::StrongFocus); on_focus_change = [this](bool has_focus, auto) { @@ -257,4 +257,26 @@ void Button::timer_event(Core::TimerEvent&) } } +Optional<UISize> Button::calculated_min_size() const +{ + int horizontal = 0, vertical = 0; + + if (!text().is_empty()) { + auto& font = this->font(); + horizontal = font.width(text()) + 2; + vertical = font.glyph_height() + 4; // FIXME: Use actual maximum total height + } + + if (m_icon) { + vertical = max(vertical, m_icon->height()); + + horizontal += m_icon->width() + icon_spacing(); + } + + horizontal += 8; + vertical += 4; + + return UISize(horizontal, vertical); +} + } diff --git a/Userland/Libraries/LibGUI/Button.h b/Userland/Libraries/LibGUI/Button.h index f34de7a193..926ea575de 100644 --- a/Userland/Libraries/LibGUI/Button.h +++ b/Userland/Libraries/LibGUI/Button.h @@ -57,6 +57,8 @@ public: void set_mimic_pressed(bool mimic_pressed); bool is_mimic_pressed() const { return m_mimic_pressed; }; + virtual Optional<UISize> calculated_min_size() const override; + protected: explicit Button(String text = {}); virtual void mousedown_event(MouseEvent&) override; diff --git a/Userland/Libraries/LibGUI/CheckBox.cpp b/Userland/Libraries/LibGUI/CheckBox.cpp index a35b746b2d..34611c33fb 100644 --- a/Userland/Libraries/LibGUI/CheckBox.cpp +++ b/Userland/Libraries/LibGUI/CheckBox.cpp @@ -30,8 +30,8 @@ CheckBox::CheckBox(String text) { CheckBoxPosition::Left, "Left" }, { CheckBoxPosition::Right, "Right" }); - set_min_width(32); - set_fixed_height(22); + set_min_size({ 22, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); } void CheckBox::paint_event(PaintEvent& event) diff --git a/Userland/Libraries/LibGUI/ColorInput.cpp b/Userland/Libraries/LibGUI/ColorInput.cpp index 2a0d8ee23c..3af27a0d8d 100644 --- a/Userland/Libraries/LibGUI/ColorInput.cpp +++ b/Userland/Libraries/LibGUI/ColorInput.cpp @@ -18,8 +18,8 @@ namespace GUI { ColorInput::ColorInput() : TextEditor(TextEditor::SingleLine) { - set_min_width(32); - set_fixed_height(22); + set_min_size({ 40, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); TextEditor::on_change = [this] { auto parsed_color = Color::from_string(text()); if (parsed_color.has_value()) diff --git a/Userland/Libraries/LibGUI/ComboBox.cpp b/Userland/Libraries/LibGUI/ComboBox.cpp index ef4ff469ec..af61cca42d 100644 --- a/Userland/Libraries/LibGUI/ComboBox.cpp +++ b/Userland/Libraries/LibGUI/ComboBox.cpp @@ -56,8 +56,8 @@ ComboBox::ComboBox() REGISTER_STRING_PROPERTY("placeholder", editor_placeholder, set_editor_placeholder); REGISTER_BOOL_PROPERTY("model_only", only_allow_values_from_model, set_only_allow_values_from_model); - set_min_width(32); - set_fixed_height(22); + set_min_size({ 40, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); m_editor = add<ComboBoxEditor>(); m_editor->set_frame_thickness(0); diff --git a/Userland/Libraries/LibGUI/Label.cpp b/Userland/Libraries/LibGUI/Label.cpp index 17045c5e04..eca706597c 100644 --- a/Userland/Libraries/LibGUI/Label.cpp +++ b/Userland/Libraries/LibGUI/Label.cpp @@ -23,6 +23,8 @@ Label::Label(String text) REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment); REGISTER_TEXT_WRAPPING_PROPERTY("text_wrapping", text_wrapping, set_text_wrapping); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); + set_frame_thickness(0); set_frame_shadow(Gfx::FrameShadow::Plain); set_frame_shape(Gfx::FrameShape::NoFrame); @@ -112,10 +114,15 @@ void Label::size_to_fit() set_fixed_width(font().width(m_text) + m_autosize_padding * 2); } -int Label::preferred_height() const +int Label::text_calculated_preferred_height() const { // FIXME: The 4 is taken from Gfx::Painter and should be available as // a constant instead. return Gfx::TextLayout(&font(), Utf8View { m_text }, text_rect()).bounding_rect(Gfx::TextWrapping::Wrap, 4).height(); } + +Optional<UISize> Label::calculated_preferred_size() const +{ + return GUI::UISize(SpecialDimension::Grow, text_calculated_preferred_height()); +} } diff --git a/Userland/Libraries/LibGUI/Label.h b/Userland/Libraries/LibGUI/Label.h index d899231911..c9ea25a2ba 100644 --- a/Userland/Libraries/LibGUI/Label.h +++ b/Userland/Libraries/LibGUI/Label.h @@ -39,7 +39,8 @@ public: bool is_autosize() const { return m_autosize; } void set_autosize(bool, size_t padding = 0); - int preferred_height() const; + virtual Optional<UISize> calculated_preferred_size() const override; + int text_calculated_preferred_height() const; Gfx::IntRect text_rect() const; diff --git a/Userland/Libraries/LibGUI/MessageBox.cpp b/Userland/Libraries/LibGUI/MessageBox.cpp index 660e6c49bf..362b07ef39 100644 --- a/Userland/Libraries/LibGUI/MessageBox.cpp +++ b/Userland/Libraries/LibGUI/MessageBox.cpp @@ -177,7 +177,7 @@ void MessageBox::build() width = max(width, text_width + icon_width + 56); // FIXME: Use shrink from new layout system - set_rect(x(), y(), width, 80 + label.preferred_height()); + set_rect(x(), y(), width, 80 + label.text_calculated_preferred_height()); set_resizable(false); } diff --git a/Userland/Libraries/LibGUI/RadioButton.cpp b/Userland/Libraries/LibGUI/RadioButton.cpp index 25c01bf3f9..d4e6e30a2c 100644 --- a/Userland/Libraries/LibGUI/RadioButton.cpp +++ b/Userland/Libraries/LibGUI/RadioButton.cpp @@ -21,8 +21,8 @@ RadioButton::RadioButton(String text) { set_exclusive(true); set_checkable(true); - set_min_width(32); - set_fixed_height(22); + set_min_size({ 22, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); } Gfx::IntSize RadioButton::circle_size() @@ -61,4 +61,13 @@ void RadioButton::click(unsigned) set_checked(true); } +Optional<UISize> RadioButton::calculated_min_size() const +{ + int horizontal = 2 + 7, vertical = 0; + auto& font = this->font(); + vertical = max(font.glyph_height(), circle_size().height()); + horizontal += font.width(text()); + return UISize(horizontal, vertical); +} + } diff --git a/Userland/Libraries/LibGUI/RadioButton.h b/Userland/Libraries/LibGUI/RadioButton.h index e2ef712bfd..d07da91a0a 100644 --- a/Userland/Libraries/LibGUI/RadioButton.h +++ b/Userland/Libraries/LibGUI/RadioButton.h @@ -19,6 +19,8 @@ public: virtual void click(unsigned modifiers = 0) override; + virtual Optional<UISize> calculated_min_size() const override; + protected: explicit RadioButton(String text = {}); virtual void paint_event(PaintEvent&) override; diff --git a/Userland/Libraries/LibGUI/SpinBox.cpp b/Userland/Libraries/LibGUI/SpinBox.cpp index 1c858f9bcf..ab98c62030 100644 --- a/Userland/Libraries/LibGUI/SpinBox.cpp +++ b/Userland/Libraries/LibGUI/SpinBox.cpp @@ -15,8 +15,8 @@ namespace GUI { SpinBox::SpinBox() { - set_min_width(32); - set_fixed_height(22); + set_min_size({ 40, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); m_editor = add<TextBox>(); m_editor->set_text("0"); m_editor->on_change = [this, weak_this = make_weak_ptr()] { diff --git a/Userland/Libraries/LibGUI/TextBox.cpp b/Userland/Libraries/LibGUI/TextBox.cpp index b2a107524e..d781adfa63 100644 --- a/Userland/Libraries/LibGUI/TextBox.cpp +++ b/Userland/Libraries/LibGUI/TextBox.cpp @@ -18,8 +18,8 @@ namespace GUI { TextBox::TextBox() : TextEditor(TextEditor::SingleLine) { - set_min_width(32); - set_fixed_height(22); + set_min_size({ 40, 22 }); + set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); } void TextBox::keydown_event(GUI::KeyEvent& event) diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 53441febec..aa91b8dfa9 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -743,6 +743,17 @@ void TextEditor::paint_event(PaintEvent& event) painter.fill_rect(cursor_content_rect(), palette().text_cursor()); } +Optional<UISize> TextEditor::calculated_min_size() const +{ + auto margins = content_margins(); + int horizontal = margins.left() + margins.right(), + vertical = margins.top() + margins.bottom(); + int vertical_content_size = font().glyph_height() + 4; + if (!is_multi_line() && m_icon) + vertical_content_size = max(vertical_content_size, icon_size() + 2); + return UISize(horizontal, vertical); +} + void TextEditor::select_all() { TextPosition start_of_document { 0, 0 }; diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 6f8e4ac2f1..dfcbbcc52b 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -224,6 +224,8 @@ public: Optional<size_t> search_result_index() const { return m_search_result_index; } Vector<TextRange> const& search_results() const { return m_search_results; } + virtual Optional<UISize> calculated_min_size() const override; + protected: explicit TextEditor(Type = Type::MultiLine); diff --git a/Userland/Libraries/LibGUI/Toolbar.cpp b/Userland/Libraries/LibGUI/Toolbar.cpp index fee9db5daa..a52ec010e4 100644 --- a/Userland/Libraries/LibGUI/Toolbar.cpp +++ b/Userland/Libraries/LibGUI/Toolbar.cpp @@ -133,4 +133,13 @@ void Toolbar::paint_event(PaintEvent& event) painter.fill_rect(event.rect(), palette().button()); } +Optional<UISize> Toolbar::calculated_preferred_size() const +{ + if (m_orientation == Gfx::Orientation::Horizontal) + return { { SpecialDimension::Grow, SpecialDimension::Fit } }; + else + return { { SpecialDimension::Fit, SpecialDimension::Grow } }; + VERIFY_NOT_REACHED(); +} + } diff --git a/Userland/Libraries/LibGUI/Toolbar.h b/Userland/Libraries/LibGUI/Toolbar.h index 0c51a82c6b..5526a5fd98 100644 --- a/Userland/Libraries/LibGUI/Toolbar.h +++ b/Userland/Libraries/LibGUI/Toolbar.h @@ -27,6 +27,8 @@ public: bool has_frame() const { return m_has_frame; } void set_has_frame(bool has_frame) { m_has_frame = has_frame; } + virtual Optional<UISize> calculated_preferred_size() const override; + protected: explicit Toolbar(Gfx::Orientation = Gfx::Orientation::Horizontal, int button_size = 16); |