1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:37:35 +00:00

LibGUI: Rewrite layout system in terms of min and max sizes

This patch removes size policies and preferred sizes, and replaces them
with min-size and max-size for each widget.

Box layout now works in 3 passes:

    1) Set all items (widgets/spacers) to their min-size
    2) Distribute remaining space evenly, respecting max-size
    3) Place widgets one after the other, adding spacing in between

I've also added convenience helpers for setting a fixed size (which is
the same as setting min-size and max-size to the same value.)

This significantly reduces the verbosity of widget layout and makes GML
a bit more pleasant to write, too. :^)
This commit is contained in:
Andreas Kling 2020-12-30 01:23:32 +01:00
parent b2bba5ce5c
commit 7dc5a3ead8
83 changed files with 444 additions and 957 deletions

View file

@ -48,11 +48,9 @@ DisassemblyWidget::DisassemblyWidget()
m_top_container = add<GUI::Widget>();
m_top_container->set_layout<GUI::HorizontalBoxLayout>();
m_top_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_top_container->set_preferred_size(0, 20);
m_top_container->set_fixed_height(20);
m_function_name_label = m_top_container->add<GUI::Label>("");
m_function_name_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
m_disassembly_view = add<GUI::TableView>();

View file

@ -40,21 +40,18 @@ EditorWrapper::EditorWrapper()
set_layout<GUI::VerticalBoxLayout>();
auto& label_wrapper = add<GUI::Widget>();
label_wrapper.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
label_wrapper.set_preferred_size(0, 14);
label_wrapper.set_fixed_height(14);
label_wrapper.set_fill_with_background_color(true);
label_wrapper.set_layout<GUI::HorizontalBoxLayout>();
label_wrapper.layout()->set_margins({ 2, 0, 2, 0 });
m_filename_label = label_wrapper.add<GUI::Label>("(Untitled)");
m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
m_filename_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_filename_label->set_preferred_size(0, 14);
m_filename_label->set_fixed_height(14);
m_cursor_label = label_wrapper.add<GUI::Label>("(Cursor)");
m_cursor_label->set_text_alignment(Gfx::TextAlignment::CenterRight);
m_cursor_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_cursor_label->set_preferred_size(0, 14);
m_cursor_label->set_fixed_height(14);
m_editor = add<Editor>();
m_editor->set_ruler_visible(true);

View file

@ -138,15 +138,12 @@ FindInFilesWidget::FindInFilesWidget()
auto& top_container = add<Widget>();
top_container.set_layout<GUI::HorizontalBoxLayout>();
top_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
top_container.set_preferred_size(0, 20);
top_container.set_fixed_height(20);
m_textbox = top_container.add<GUI::TextBox>();
m_textbox->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
m_button = top_container.add<GUI::Button>("Find in files");
m_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
m_button->set_preferred_size(100, 0);
m_button->set_fixed_width(100);
m_result_view = add<GUI::TableView>();

View file

@ -54,16 +54,14 @@ GitWidget::GitWidget(const LexicalPath& repo_root)
auto& refresh_button = unstaged_header.add<GUI::Button>();
refresh_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"));
refresh_button.set_preferred_size({ 16, 16 });
refresh_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
refresh_button.set_fixed_size(16, 16);
refresh_button.set_tooltip("refresh");
refresh_button.on_click = [this](int) { refresh(); };
auto& unstaged_label = unstaged_header.add<GUI::Label>();
unstaged_label.set_text("Unstaged");
unstaged_header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
unstaged_header.set_preferred_size(0, 20);
unstaged_header.set_fixed_height(20);
m_unstaged_files = unstaged.add<GitFilesView>(
[this](const auto& file) { stage_file(file); },
Gfx::Bitmap::load_from_file("/res/icons/16x16/plus.png").release_nonnull());
@ -80,16 +78,14 @@ GitWidget::GitWidget(const LexicalPath& repo_root)
auto& commit_button = staged_header.add<GUI::Button>();
commit_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/commit.png"));
commit_button.set_preferred_size({ 16, 16 });
commit_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
commit_button.set_fixed_size(16, 16);
commit_button.set_tooltip("commit");
commit_button.on_click = [this](int) { commit(); };
auto& staged_label = staged_header.add<GUI::Label>();
staged_label.set_text("Staged");
staged_header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
staged_header.set_preferred_size(0, 20);
staged_header.set_fixed_height(20);
m_staged_files = staged.add<GitFilesView>(
[this](const auto& file) { unstage_file(file); },
Gfx::Bitmap::load_from_file("/res/icons/16x16/minus.png").release_nonnull());

View file

@ -100,8 +100,7 @@ HackStudioWidget::HackStudioWidget(const String& path_to_project)
auto& outer_splitter = add<GUI::HorizontalSplitter>();
auto& left_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
left_hand_splitter.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
left_hand_splitter.set_preferred_size(150, 0);
left_hand_splitter.set_fixed_width(150);
create_project_tree_view(left_hand_splitter);
m_project_tree_view_context_menu = create_project_tree_view_context_menu();
@ -494,8 +493,8 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
{
if (m_action_tab_widget->preferred_size().height() < 200)
m_action_tab_widget->set_preferred_size(0, 200);
if (m_action_tab_widget->min_height() < 200)
m_action_tab_widget->set_fixed_height(200);
m_action_tab_widget->set_active_widget(&widget);
}
@ -617,7 +616,7 @@ void HackStudioWidget::run(TerminalWrapper& wrapper)
void HackStudioWidget::hide_action_tabs()
{
m_action_tab_widget->set_preferred_size(0, 24);
m_action_tab_widget->set_fixed_height(24);
};
Project& HackStudioWidget::project()
@ -673,7 +672,7 @@ void HackStudioWidget::create_form_editor(GUI::Widget& parent)
m_form_inner_container = parent.add<GUI::Widget>();
m_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
auto& form_widgets_toolbar = m_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
form_widgets_toolbar.set_preferred_size(38, 0);
form_widgets_toolbar.set_fixed_width(38);
GUI::ActionGroup tool_actions;
tool_actions.set_exclusive(true);
@ -710,8 +709,7 @@ void HackStudioWidget::create_form_editor(GUI::Widget& parent)
m_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
form_editing_pane_container.set_preferred_size(190, 0);
form_editing_pane_container.set_fixed_width(190);
form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
auto add_properties_pane = [&](auto& text, auto& pane_widget) {
@ -721,8 +719,7 @@ void HackStudioWidget::create_form_editor(GUI::Widget& parent)
label.set_fill_with_background_color(true);
label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
label.set_font(Gfx::Font::default_bold_font());
label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
label.set_preferred_size(0, 16);
label.set_fixed_height(16);
wrapper.add_child(pane_widget);
};
@ -803,14 +800,13 @@ void HackStudioWidget::create_action_tab(GUI::Widget& parent)
{
m_action_tab_widget = parent.add<GUI::TabWidget>();
m_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_action_tab_widget->set_preferred_size(0, 24);
m_action_tab_widget->set_fixed_height(24);
m_action_tab_widget->on_change = [this](auto&) {
on_action_tab_change();
static bool first_time = true;
if (!first_time)
m_action_tab_widget->set_preferred_size(0, 200);
m_action_tab_widget->set_fixed_height(200);
first_time = false;
};

View file

@ -69,8 +69,7 @@ private:
Locator::Locator()
{
set_layout<GUI::VerticalBoxLayout>();
set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
set_preferred_size(0, 20);
set_fixed_height(20);
m_textbox = add<GUI::TextBox>();
m_textbox->on_change = [this] {
update_suggestions();