summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-03 21:42:48 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-03 22:37:48 +0100
commit03e0ddce5237b6ad7b84724ef03039e413944f00 (patch)
treeee45767b85f54584ce6c3f08dd34b7f8e84ef1cd
parent0cafbbf09c303a51a48b67ef951fa828979cde6e (diff)
downloadserenity-03e0ddce5237b6ad7b84724ef03039e413944f00.zip
LibGUI: Some more convenience functions for constructing widgets
This patch adds two new API's: - WidgetType& GUI::Window::set_main_widget<WidgetType>(); This creates a new main widget for a window, assigns it, and returns it to you as a WidgetType&. - LayoutType& GUI::Widget::set_layout<LayoutType>(); Same basic idea, creates a new layout, assigns it, and returns it to you as a LayoutType&.
-rw-r--r--Applications/About/main.cpp23
-rw-r--r--Applications/Browser/main.cpp18
-rw-r--r--Applications/Calculator/main.cpp3
-rw-r--r--Applications/ChanViewer/main.cpp13
-rw-r--r--Applications/FileManager/PropertiesDialog.cpp15
-rw-r--r--Applications/FileManager/main.cpp15
-rw-r--r--Applications/Help/main.cpp11
-rw-r--r--Demos/HelloWorld/main.cpp17
-rw-r--r--Libraries/LibGUI/Widget.h7
-rw-r--r--Libraries/LibGUI/Window.h8
10 files changed, 68 insertions, 62 deletions
diff --git a/Applications/About/main.cpp b/Applications/About/main.cpp
index c1467cf39b..d8f5c33b07 100644
--- a/Applications/About/main.cpp
+++ b/Applications/About/main.cpp
@@ -63,21 +63,20 @@ int main(int argc, char** argv)
window->set_resizable(false);
window->set_rect(window_rect);
- auto outer_widget = GUI::Widget::construct();
- window->set_main_widget(outer_widget);
- outer_widget->set_fill_with_background_color(true);
- outer_widget->set_layout(make<GUI::VerticalBoxLayout>());
- outer_widget->layout()->set_margins({ 8, 8, 8, 8 });
-
- auto inner_widget = outer_widget->add<GUI::Widget>();
- inner_widget->set_layout(make<GUI::HorizontalBoxLayout>());
+ auto& outer_widget = window->set_main_widget<GUI::Widget>();
+ outer_widget.set_fill_with_background_color(true);
+ outer_widget.set_layout<GUI::VerticalBoxLayout>();
+ outer_widget.layout()->set_margins({ 8, 8, 8, 8 });
+
+ auto inner_widget = outer_widget.add<GUI::Widget>();
+ inner_widget->set_layout<GUI::HorizontalBoxLayout>();
inner_widget->layout()->set_spacing(8);
auto left_outer_container = inner_widget->add<GUI::Widget>();
- left_outer_container->set_layout(make<GUI::HorizontalBoxLayout>());
+ left_outer_container->set_layout<GUI::HorizontalBoxLayout>();
auto left_inner_container = left_outer_container->add<GUI::Widget>();
- left_inner_container->set_layout(make<GUI::VerticalBoxLayout>());
+ left_inner_container->set_layout<GUI::VerticalBoxLayout>();
left_inner_container->layout()->set_spacing(8);
left_inner_container->set_preferred_size(0, 50);
left_inner_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
@@ -106,7 +105,7 @@ int main(int argc, char** argv)
git_info_label->set_preferred_size(0, 11);
auto right_container = inner_widget->add<GUI::Widget>();
- right_container->set_layout(make<GUI::VerticalBoxLayout>());
+ right_container->set_layout<GUI::VerticalBoxLayout>();
auto icon_label = right_container->add<GUI::Label>();
icon_label->set_icon(Gfx::Bitmap::load_from_file("/res/icons/buggie.png"));
@@ -114,7 +113,7 @@ int main(int argc, char** argv)
icon_label->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
icon_label->set_preferred_size(icon_label->icon()->size());
- auto quit_button = outer_widget->add<GUI::Button>();
+ auto quit_button = outer_widget.add<GUI::Button>();
quit_button->set_text("Okay");
quit_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
quit_button->set_preferred_size(100, 20);
diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp
index e38824896c..61e9c3da69 100644
--- a/Applications/Browser/main.cpp
+++ b/Applications/Browser/main.cpp
@@ -75,13 +75,13 @@ int main(int argc, char** argv)
auto window = GUI::Window::construct();
window->set_rect(100, 100, 640, 480);
- auto widget = GUI::Widget::construct();
- widget->set_fill_with_background_color(true);
- widget->set_layout(make<GUI::VerticalBoxLayout>());
- widget->layout()->set_spacing(0);
+ auto& widget = window->set_main_widget<GUI::Widget>();
+ widget.set_fill_with_background_color(true);
+ widget.set_layout<GUI::VerticalBoxLayout>();
+ widget.layout()->set_spacing(0);
- auto toolbar = widget->add<GUI::ToolBar>();
- auto html_widget = widget->add<HtmlView>();
+ auto toolbar = widget.add<GUI::ToolBar>();
+ auto html_widget = widget.add<HtmlView>();
History<URL> history;
@@ -151,7 +151,7 @@ int main(int argc, char** argv)
location_box->set_focus(true);
});
- auto statusbar = widget->add<GUI::StatusBar>();
+ auto statusbar = widget.add<GUI::StatusBar>();
html_widget->on_link_hover = [&](auto& href) {
statusbar->set_text(href);
@@ -200,8 +200,7 @@ int main(int argc, char** argv)
dom_inspector_window = GUI::Window::construct();
dom_inspector_window->set_rect(100, 100, 300, 500);
dom_inspector_window->set_title("DOM inspector");
- auto dom_inspector_widget = InspectorWidget::construct();
- dom_inspector_window->set_main_widget(dom_inspector_widget);
+ dom_inspector_window->set_main_widget<InspectorWidget>();
}
auto* inspector_widget = static_cast<InspectorWidget*>(dom_inspector_window->main_widget());
inspector_widget->set_document(html_widget->document());
@@ -244,7 +243,6 @@ int main(int argc, char** argv)
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
window->set_title("Browser");
- window->set_main_widget(widget);
window->show();
URL url_to_load = home_url;
diff --git a/Applications/Calculator/main.cpp b/Applications/Calculator/main.cpp
index 6b166c5e25..772564f4a4 100644
--- a/Applications/Calculator/main.cpp
+++ b/Applications/Calculator/main.cpp
@@ -60,8 +60,7 @@ int main(int argc, char** argv)
window->set_resizable(false);
window->set_rect({ 300, 200, 254, 213 });
- auto calc_widget = CalculatorWidget::construct();
- window->set_main_widget(calc_widget);
+ window->set_main_widget<CalculatorWidget>();
window->show();
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-calculator.png"));
diff --git a/Applications/ChanViewer/main.cpp b/Applications/ChanViewer/main.cpp
index 1f874fc2b7..0cf00598b9 100644
--- a/Applications/ChanViewer/main.cpp
+++ b/Applications/ChanViewer/main.cpp
@@ -57,22 +57,21 @@ int main(int argc, char** argv)
window->set_rect(100, 100, 800, 500);
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-chanviewer.png"));
- auto widget = GUI::Widget::construct();
- window->set_main_widget(widget);
- widget->set_fill_with_background_color(true);
- widget->set_layout(make<GUI::VerticalBoxLayout>());
+ auto& widget = window->set_main_widget<GUI::Widget>();
+ widget.set_fill_with_background_color(true);
+ widget.set_layout<GUI::VerticalBoxLayout>();
- auto board_combo = widget->add<GUI::ComboBox>();
+ auto board_combo = widget.add<GUI::ComboBox>();
board_combo->set_only_allow_values_from_model(true);
board_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
board_combo->set_preferred_size(0, 20);
board_combo->set_model(BoardListModel::create());
- auto catalog_view = widget->add<GUI::TableView>();
+ auto catalog_view = widget.add<GUI::TableView>();
catalog_view->set_model(ThreadCatalogModel::create());
auto& catalog_model = *static_cast<ThreadCatalogModel*>(catalog_view->model());
- auto statusbar = widget->add<GUI::StatusBar>();
+ auto statusbar = widget.add<GUI::StatusBar>();
board_combo->on_change = [&] (auto&, const GUI::ModelIndex& index) {
auto selected_board = board_combo->model()->data(index, GUI::Model::Role::Custom);
diff --git a/Applications/FileManager/PropertiesDialog.cpp b/Applications/FileManager/PropertiesDialog.cpp
index 4c43f069ca..3561de5e08 100644
--- a/Applications/FileManager/PropertiesDialog.cpp
+++ b/Applications/FileManager/PropertiesDialog.cpp
@@ -43,16 +43,15 @@ PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, boo
auto file_path = FileSystemPath(path);
ASSERT(file_path.is_valid());
- auto main_widget = GUI::Widget::construct();
- main_widget->set_layout(make<GUI::VerticalBoxLayout>());
- main_widget->layout()->set_margins({ 4, 4, 4, 4 });
- main_widget->set_fill_with_background_color(true);
+ auto& main_widget = set_main_widget<GUI::Widget>();
+ main_widget.set_layout(make<GUI::VerticalBoxLayout>());
+ main_widget.layout()->set_margins({ 4, 4, 4, 4 });
+ main_widget.set_fill_with_background_color(true);
- set_main_widget(main_widget);
set_rect({ 0, 0, 360, 420 });
set_resizable(false);
- auto tab_widget = main_widget->add<GUI::TabWidget>();
+ auto tab_widget = main_widget.add<GUI::TabWidget>();
auto general_tab = tab_widget->add_tab<GUI::Widget>("General");
general_tab->set_layout(make<GUI::VerticalBoxLayout>());
@@ -131,8 +130,8 @@ PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, boo
general_tab->layout()->add_spacer();
- auto button_widget = main_widget->add<GUI::Widget>();
- button_widget->set_layout(make<GUI::HorizontalBoxLayout>());
+ auto button_widget = main_widget.add<GUI::Widget>();
+ button_widget->set_layout<GUI::HorizontalBoxLayout>();
button_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
button_widget->set_preferred_size(0, 24);
button_widget->layout()->set_spacing(5);
diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp
index f47c33150d..15ea2d6a6e 100644
--- a/Applications/FileManager/main.cpp
+++ b/Applications/FileManager/main.cpp
@@ -92,12 +92,12 @@ int main(int argc, char** argv)
auto heigth = config->read_num_entry("Window", "Heigth", 480);
window->set_rect({ left, top, width, heigth });
- auto widget = GUI::Widget::construct();
- widget->set_layout(make<GUI::VerticalBoxLayout>());
- widget->layout()->set_spacing(0);
+ auto& widget = window->set_main_widget<GUI::Widget>();
+ widget.set_layout<GUI::VerticalBoxLayout>();
+ widget.layout()->set_spacing(0);
- auto main_toolbar = widget->add<GUI::ToolBar>();
- auto location_toolbar = widget->add<GUI::ToolBar>();
+ auto main_toolbar = widget.add<GUI::ToolBar>();
+ auto location_toolbar = widget.add<GUI::ToolBar>();
location_toolbar->layout()->set_margins({ 6, 3, 6, 3 });
location_toolbar->set_preferred_size(0, 25);
@@ -106,7 +106,7 @@ int main(int argc, char** argv)
auto location_textbox = location_toolbar->add<GUI::TextBox>();
- auto splitter = widget->add<GUI::HorizontalSplitter>();
+ auto splitter = widget.add<GUI::HorizontalSplitter>();
auto tree_view = splitter->add<GUI::TreeView>();
auto directories_model = GUI::FileSystemModel::create("/", GUI::FileSystemModel::Mode::DirectoriesOnly);
tree_view->set_model(directories_model);
@@ -122,7 +122,7 @@ int main(int argc, char** argv)
tree_view->set_preferred_size(150, 0);
auto directory_view = splitter->add<DirectoryView>();
- auto statusbar = widget->add<GUI::StatusBar>();
+ auto statusbar = widget.add<GUI::StatusBar>();
auto progressbar = statusbar->add<GUI::ProgressBar>();
progressbar->set_caption("Generating thumbnails: ");
@@ -641,7 +641,6 @@ int main(int argc, char** argv)
directory_view->open(initial_location);
directory_view->set_focus(true);
- window->set_main_widget(widget);
window->show();
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp
index 010a87f0e5..423bc5ef64 100644
--- a/Applications/Help/main.cpp
+++ b/Applications/Help/main.cpp
@@ -78,13 +78,13 @@ int main(int argc, char* argv[])
window->set_title("Help");
window->set_rect(300, 200, 570, 500);
- auto widget = GUI::Widget::construct();
- widget->set_layout(make<GUI::VerticalBoxLayout>());
- widget->layout()->set_spacing(0);
+ auto& widget = window->set_main_widget<GUI::Widget>();
+ widget.set_layout<GUI::VerticalBoxLayout>();
+ widget.layout()->set_spacing(0);
- auto toolbar = widget->add<GUI::ToolBar>();
+ auto toolbar = widget.add<GUI::ToolBar>();
- auto splitter = widget->add<GUI::HorizontalSplitter>();
+ auto splitter = widget.add<GUI::HorizontalSplitter>();
auto model = ManualModel::create();
@@ -195,7 +195,6 @@ int main(int argc, char* argv[])
app.set_menubar(move(menubar));
- window->set_main_widget(widget);
window->set_focused_widget(tree_view);
window->show();
diff --git a/Demos/HelloWorld/main.cpp b/Demos/HelloWorld/main.cpp
index 73ce17a3a0..14614c00ce 100644
--- a/Demos/HelloWorld/main.cpp
+++ b/Demos/HelloWorld/main.cpp
@@ -39,17 +39,16 @@ int main(int argc, char** argv)
window->set_rect(100, 100, 240, 160);
window->set_title("Hello World!");
- auto main_widget = GUI::Widget::construct();
- window->set_main_widget(main_widget);
- main_widget->set_fill_with_background_color(true);
- main_widget->set_background_color(Color::White);
- main_widget->set_layout(make<GUI::VerticalBoxLayout>());
- main_widget->layout()->set_margins({ 4, 4, 4, 4 });
-
- auto label = main_widget->add<GUI::Label>();
+ auto& main_widget = window->set_main_widget<GUI::Widget>();
+ main_widget.set_fill_with_background_color(true);
+ main_widget.set_background_color(Color::White);
+ auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
+ layout.set_margins({ 4, 4, 4, 4 });
+
+ auto label = main_widget.add<GUI::Label>();
label->set_text("Hello\nWorld!");
- auto button = main_widget->add<GUI::Button>();
+ auto button = main_widget.add<GUI::Button>();
button->set_text("Good-bye");
button->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
button->set_preferred_size(0, 20);
diff --git a/Libraries/LibGUI/Widget.h b/Libraries/LibGUI/Widget.h
index 3de5ac95da..96795a94c4 100644
--- a/Libraries/LibGUI/Widget.h
+++ b/Libraries/LibGUI/Widget.h
@@ -98,6 +98,13 @@ public:
const Layout* layout() const { return m_layout.ptr(); }
void set_layout(OwnPtr<Layout>&&);
+ template<typename T>
+ T& set_layout()
+ {
+ set_layout(make<T>());
+ return static_cast<T&>(*layout());
+ }
+
SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; }
SizePolicy vertical_size_policy() const { return m_vertical_size_policy; }
SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
diff --git a/Libraries/LibGUI/Window.h b/Libraries/LibGUI/Window.h
index 65db147a96..c056637c50 100644
--- a/Libraries/LibGUI/Window.h
+++ b/Libraries/LibGUI/Window.h
@@ -125,6 +125,14 @@ public:
const Widget* main_widget() const { return m_main_widget; }
void set_main_widget(Widget*);
+ template<class T, class... Args>
+ inline T& set_main_widget(Args&&... args)
+ {
+ auto widget = T::construct(forward<Args>(args)...);
+ set_main_widget(widget.ptr());
+ return *widget;
+ }
+
Widget* focused_widget() { return m_focused_widget; }
const Widget* focused_widget() const { return m_focused_widget; }
void set_focused_widget(Widget*);