LibGUI: Add helper for constructing new TabWidget tabs

This patch adds the following convenience helper:

    auto tab_widget = GUI::TabWidget::construct();
    auto my_widget = tab_widget->add_tab<GUI::Widget>("My tab", ...);

The above is equivalent to:

    auto tab_widget = GUI::TabWidget::construct();
    auto my_widget = GUI::Widget::construct(...);
    tab_widget->add_widget("My tab", my_widget);
This commit is contained in:
Andreas Kling 2020-02-23 12:23:48 +01:00
parent bbc02af090
commit 6c5100b644
Notes: sideshowbarker 2024-07-19 09:08:28 +09:00
8 changed files with 42 additions and 53 deletions

View file

@ -54,15 +54,14 @@ InspectorWidget::InspectorWidget()
m_computed_style_table_view->set_model(nullptr);
}
};
m_style_table_view = GUI::TableView::construct();
auto tab_widget = splitter->add<GUI::TabWidget>();
m_style_table_view = tab_widget->add_tab<GUI::TableView>("Styles");
m_style_table_view->set_size_columns_to_fit_content(true);
m_computed_style_table_view = GUI::TableView::construct();
m_computed_style_table_view = tab_widget->add_tab<GUI::TableView>("Computed");
m_computed_style_table_view->set_size_columns_to_fit_content(true);
auto tabwidget = splitter->add<GUI::TabWidget>();
tabwidget->add_widget("Styles", m_style_table_view);
tabwidget->add_widget("Computed", m_computed_style_table_view);
}
InspectorWidget::~InspectorWidget()

View file

@ -124,17 +124,15 @@ void DisplayPropertiesWidget::create_frame()
{
auto tab_widget = m_root_widget->add<GUI::TabWidget>();
// First, let's create the "Background" tab
auto background_splitter = GUI::VerticalSplitter::construct();
tab_widget->add_widget("Wallpaper", background_splitter);
auto wallpaper_splitter = tab_widget->add_tab<GUI::VerticalSplitter>("Wallpaper");
auto background_content = background_splitter->add<GUI::Widget>();
background_content->set_layout(make<GUI::VerticalBoxLayout>());
background_content->layout()->set_margins({ 4, 4, 4, 4 });
auto wallpaper_content = wallpaper_splitter->add<GUI::Widget>();
wallpaper_content->set_layout(make<GUI::VerticalBoxLayout>());
wallpaper_content->layout()->set_margins({ 4, 4, 4, 4 });
m_wallpaper_preview = background_splitter->add<GUI::Label>();
m_wallpaper_preview = wallpaper_splitter->add<GUI::Label>();
auto wallpaper_list = background_content->add<GUI::ListView>();
auto wallpaper_list = wallpaper_content->add<GUI::ListView>();
wallpaper_list->set_background_color(Color::White);
wallpaper_list->set_model(*ItemListModel<AK::String>::create(m_wallpapers));
@ -154,9 +152,7 @@ void DisplayPropertiesWidget::create_frame()
m_wallpaper_preview->set_should_stretch_icon(true);
};
// Let's add the settings tab
auto settings_splitter = GUI::VerticalSplitter::construct();
tab_widget->add_widget("Settings", settings_splitter);
auto settings_splitter = tab_widget->add_tab<GUI::VerticalSplitter>("Settings");
auto settings_content = settings_splitter->add<GUI::Widget>();
settings_content->set_layout(make<GUI::VerticalBoxLayout>());

View file

@ -54,11 +54,10 @@ PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, boo
auto tab_widget = main_widget->add<GUI::TabWidget>();
auto general_tab = tab_widget->add<GUI::Widget>();
auto general_tab = tab_widget->add_tab<GUI::Widget>("General");
general_tab->set_layout(make<GUI::VerticalBoxLayout>());
general_tab->layout()->set_margins({ 12, 8, 12, 8 });
general_tab->layout()->set_spacing(10);
tab_widget->add_widget("General", general_tab);
general_tab->layout()->add_spacer();

View file

@ -47,15 +47,13 @@ MainWidget::MainWidget(AudioEngine& audio_engine)
m_wave_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_wave_widget->set_preferred_size(0, 100);
m_roll_widget = RollWidget::construct(audio_engine);
m_tab_widget = add<GUI::TabWidget>();
m_roll_widget = m_tab_widget->add_tab<RollWidget>("Piano Roll", audio_engine);
m_roll_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
m_roll_widget->set_preferred_size(0, 300);
m_sampler_widget = SamplerWidget::construct(audio_engine);
m_tab_widget = add<GUI::TabWidget>();
m_tab_widget->add_widget("Piano Roll", m_roll_widget);
m_tab_widget->add_widget("Sampler", m_sampler_widget);
m_tab_widget->add_tab<SamplerWidget>("Sampler", audio_engine);
m_keys_and_knobs_container = add<GUI::Widget>();
m_keys_and_knobs_container->set_layout(make<GUI::HorizontalBoxLayout>());

View file

@ -70,9 +70,9 @@ static String human_readable_size(u32 size)
return String::format("%u GB", size / GB);
}
static RefPtr<GUI::Widget> build_file_systems_tab();
static RefPtr<GUI::Widget> build_pci_devices_tab();
static RefPtr<GUI::Widget> build_devices_tab();
static NonnullRefPtr<GUI::Widget> build_file_systems_tab();
static NonnullRefPtr<GUI::Widget> build_pci_devices_tab();
static NonnullRefPtr<GUI::Widget> build_devices_tab();
static NonnullRefPtr<GUI::Widget> build_graphs_tab();
int main(int argc, char** argv)
@ -123,8 +123,7 @@ int main(int argc, char** argv)
auto tabwidget = keeper->add<GUI::TabWidget>();
auto process_container_splitter = GUI::VerticalSplitter::construct();
tabwidget->add_widget("Processes", process_container_splitter);
auto process_container_splitter = tabwidget->add_tab<GUI::VerticalSplitter>("Processes");
auto process_table_container = process_container_splitter->add<GUI::Widget>();
@ -232,17 +231,10 @@ int main(int argc, char** argv)
auto process_tab_widget = process_container_splitter->add<GUI::TabWidget>();
auto memory_map_widget = ProcessMemoryMapWidget::construct();
process_tab_widget->add_widget("Memory map", memory_map_widget);
auto open_files_widget = ProcessFileDescriptorMapWidget::construct();
process_tab_widget->add_widget("Open files", open_files_widget);
auto unveiled_paths_widget = ProcessUnveiledPathsWidget::construct();
process_tab_widget->add_widget("Unveiled paths", unveiled_paths_widget);
auto stacks_widget = ProcessStacksWidget::construct();
process_tab_widget->add_widget("Stacks", stacks_widget);
auto memory_map_widget = process_tab_widget->add_tab<ProcessMemoryMapWidget>("Memory map");
auto open_files_widget = process_tab_widget->add_tab<ProcessFileDescriptorMapWidget>("Open files");
auto unveiled_paths_widget = process_tab_widget->add_tab<ProcessUnveiledPathsWidget>("Unveiled paths");
auto stacks_widget = process_tab_widget->add_tab<ProcessStacksWidget>("Stacks");
process_table_view->on_process_selected = [&](pid_t pid) {
open_files_widget->set_pid(pid);
@ -276,7 +268,7 @@ public:
}
};
RefPtr<GUI::Widget> build_file_systems_tab()
NonnullRefPtr<GUI::Widget> build_file_systems_tab()
{
auto fs_widget = GUI::LazyWidget::construct();
@ -369,7 +361,7 @@ RefPtr<GUI::Widget> build_file_systems_tab()
return fs_widget;
}
RefPtr<GUI::Widget> build_pci_devices_tab()
NonnullRefPtr<GUI::Widget> build_pci_devices_tab()
{
auto pci_widget = GUI::LazyWidget::construct();
@ -427,7 +419,7 @@ RefPtr<GUI::Widget> build_pci_devices_tab()
return pci_widget;
}
RefPtr<GUI::Widget> build_devices_tab()
NonnullRefPtr<GUI::Widget> build_devices_tab()
{
auto devices_widget = GUI::LazyWidget::construct();

View file

@ -445,11 +445,8 @@ int main(int argc, char** argv)
update_actions();
});
auto find_in_files_widget = FindInFilesWidget::construct();
s_action_tab_widget->add_widget("Find in files", find_in_files_widget);
auto terminal_wrapper = TerminalWrapper::construct();
s_action_tab_widget->add_widget("Console", terminal_wrapper);
auto find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
auto terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Console");
auto locator = widget->add<Locator>();

View file

@ -41,10 +41,10 @@ TabWidget::~TabWidget()
{
}
void TabWidget::add_widget(const StringView& title, Widget* widget)
void TabWidget::add_widget(const StringView& title, Widget& widget)
{
m_tabs.append({ title, widget });
add_child(*widget);
m_tabs.append({ title, &widget });
add_child(widget);
}
void TabWidget::set_active_widget(Widget* widget)

View file

@ -52,7 +52,15 @@ public:
int bar_height() const { return 21; }
int container_padding() const { return 2; }
void add_widget(const StringView&, Widget*);
void add_widget(const StringView&, Widget&);
template<class T, class... Args>
inline NonnullRefPtr<T> add_tab(const StringView& title, Args&&... args)
{
auto t = T::construct(forward<Args>(args)...);
add_widget(title, *t);
return t;
}
protected:
TabWidget();