1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:47:44 +00:00

LibGUI: Use new layout system for basic widgets

This commit is contained in:
FrHun 2022-02-22 20:25:30 +01:00 committed by Sam Atkins
parent bfbaad9f41
commit 19fac58e49
17 changed files with 85 additions and 18 deletions

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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)

View file

@ -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())

View file

@ -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);

View file

@ -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());
}
}

View file

@ -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;

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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()] {

View file

@ -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)

View file

@ -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 };

View file

@ -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);

View file

@ -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();
}
}

View file

@ -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);

View file

@ -107,7 +107,7 @@ RefPtr<NotificationWindow> NotificationWindow::get_window_by_id(i32 id)
void NotificationWindow::resize_to_fit_text()
{
auto line_height = m_text_label->font().glyph_height();
auto total_height = m_text_label->preferred_height();
auto total_height = m_text_label->text_calculated_preferred_height();
m_text_label->set_fixed_height(total_height);
set_height(40 - line_height + total_height);