mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 11:08:12 +00:00
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&.
This commit is contained in:
parent
0cafbbf09c
commit
03e0ddce52
10 changed files with 66 additions and 60 deletions
|
@ -63,21 +63,20 @@ int main(int argc, char** argv)
|
||||||
window->set_resizable(false);
|
window->set_resizable(false);
|
||||||
window->set_rect(window_rect);
|
window->set_rect(window_rect);
|
||||||
|
|
||||||
auto outer_widget = GUI::Widget::construct();
|
auto& outer_widget = window->set_main_widget<GUI::Widget>();
|
||||||
window->set_main_widget(outer_widget);
|
outer_widget.set_fill_with_background_color(true);
|
||||||
outer_widget->set_fill_with_background_color(true);
|
outer_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
outer_widget->set_layout(make<GUI::VerticalBoxLayout>());
|
outer_widget.layout()->set_margins({ 8, 8, 8, 8 });
|
||||||
outer_widget->layout()->set_margins({ 8, 8, 8, 8 });
|
|
||||||
|
|
||||||
auto inner_widget = outer_widget->add<GUI::Widget>();
|
auto inner_widget = outer_widget.add<GUI::Widget>();
|
||||||
inner_widget->set_layout(make<GUI::HorizontalBoxLayout>());
|
inner_widget->set_layout<GUI::HorizontalBoxLayout>();
|
||||||
inner_widget->layout()->set_spacing(8);
|
inner_widget->layout()->set_spacing(8);
|
||||||
|
|
||||||
auto left_outer_container = inner_widget->add<GUI::Widget>();
|
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>();
|
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->layout()->set_spacing(8);
|
||||||
left_inner_container->set_preferred_size(0, 50);
|
left_inner_container->set_preferred_size(0, 50);
|
||||||
left_inner_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
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);
|
git_info_label->set_preferred_size(0, 11);
|
||||||
|
|
||||||
auto right_container = inner_widget->add<GUI::Widget>();
|
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>();
|
auto icon_label = right_container->add<GUI::Label>();
|
||||||
icon_label->set_icon(Gfx::Bitmap::load_from_file("/res/icons/buggie.png"));
|
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_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||||
icon_label->set_preferred_size(icon_label->icon()->size());
|
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_text("Okay");
|
||||||
quit_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
quit_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||||
quit_button->set_preferred_size(100, 20);
|
quit_button->set_preferred_size(100, 20);
|
||||||
|
|
|
@ -75,13 +75,13 @@ int main(int argc, char** argv)
|
||||||
auto window = GUI::Window::construct();
|
auto window = GUI::Window::construct();
|
||||||
window->set_rect(100, 100, 640, 480);
|
window->set_rect(100, 100, 640, 480);
|
||||||
|
|
||||||
auto widget = GUI::Widget::construct();
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
||||||
widget->set_fill_with_background_color(true);
|
widget.set_fill_with_background_color(true);
|
||||||
widget->set_layout(make<GUI::VerticalBoxLayout>());
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
widget->layout()->set_spacing(0);
|
widget.layout()->set_spacing(0);
|
||||||
|
|
||||||
auto toolbar = widget->add<GUI::ToolBar>();
|
auto toolbar = widget.add<GUI::ToolBar>();
|
||||||
auto html_widget = widget->add<HtmlView>();
|
auto html_widget = widget.add<HtmlView>();
|
||||||
|
|
||||||
History<URL> history;
|
History<URL> history;
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ int main(int argc, char** argv)
|
||||||
location_box->set_focus(true);
|
location_box->set_focus(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
auto statusbar = widget->add<GUI::StatusBar>();
|
auto statusbar = widget.add<GUI::StatusBar>();
|
||||||
|
|
||||||
html_widget->on_link_hover = [&](auto& href) {
|
html_widget->on_link_hover = [&](auto& href) {
|
||||||
statusbar->set_text(href);
|
statusbar->set_text(href);
|
||||||
|
@ -200,8 +200,7 @@ int main(int argc, char** argv)
|
||||||
dom_inspector_window = GUI::Window::construct();
|
dom_inspector_window = GUI::Window::construct();
|
||||||
dom_inspector_window->set_rect(100, 100, 300, 500);
|
dom_inspector_window->set_rect(100, 100, 300, 500);
|
||||||
dom_inspector_window->set_title("DOM inspector");
|
dom_inspector_window->set_title("DOM inspector");
|
||||||
auto dom_inspector_widget = InspectorWidget::construct();
|
dom_inspector_window->set_main_widget<InspectorWidget>();
|
||||||
dom_inspector_window->set_main_widget(dom_inspector_widget);
|
|
||||||
}
|
}
|
||||||
auto* inspector_widget = static_cast<InspectorWidget*>(dom_inspector_window->main_widget());
|
auto* inspector_widget = static_cast<InspectorWidget*>(dom_inspector_window->main_widget());
|
||||||
inspector_widget->set_document(html_widget->document());
|
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_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
|
||||||
|
|
||||||
window->set_title("Browser");
|
window->set_title("Browser");
|
||||||
window->set_main_widget(widget);
|
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
URL url_to_load = home_url;
|
URL url_to_load = home_url;
|
||||||
|
|
|
@ -60,8 +60,7 @@ int main(int argc, char** argv)
|
||||||
window->set_resizable(false);
|
window->set_resizable(false);
|
||||||
window->set_rect({ 300, 200, 254, 213 });
|
window->set_rect({ 300, 200, 254, 213 });
|
||||||
|
|
||||||
auto calc_widget = CalculatorWidget::construct();
|
window->set_main_widget<CalculatorWidget>();
|
||||||
window->set_main_widget(calc_widget);
|
|
||||||
|
|
||||||
window->show();
|
window->show();
|
||||||
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-calculator.png"));
|
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-calculator.png"));
|
||||||
|
|
|
@ -57,22 +57,21 @@ int main(int argc, char** argv)
|
||||||
window->set_rect(100, 100, 800, 500);
|
window->set_rect(100, 100, 800, 500);
|
||||||
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-chanviewer.png"));
|
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-chanviewer.png"));
|
||||||
|
|
||||||
auto widget = GUI::Widget::construct();
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
||||||
window->set_main_widget(widget);
|
widget.set_fill_with_background_color(true);
|
||||||
widget->set_fill_with_background_color(true);
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
widget->set_layout(make<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_only_allow_values_from_model(true);
|
||||||
board_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
board_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||||
board_combo->set_preferred_size(0, 20);
|
board_combo->set_preferred_size(0, 20);
|
||||||
board_combo->set_model(BoardListModel::create());
|
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());
|
catalog_view->set_model(ThreadCatalogModel::create());
|
||||||
auto& catalog_model = *static_cast<ThreadCatalogModel*>(catalog_view->model());
|
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) {
|
board_combo->on_change = [&] (auto&, const GUI::ModelIndex& index) {
|
||||||
auto selected_board = board_combo->model()->data(index, GUI::Model::Role::Custom);
|
auto selected_board = board_combo->model()->data(index, GUI::Model::Role::Custom);
|
||||||
|
|
|
@ -43,16 +43,15 @@ PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, boo
|
||||||
auto file_path = FileSystemPath(path);
|
auto file_path = FileSystemPath(path);
|
||||||
ASSERT(file_path.is_valid());
|
ASSERT(file_path.is_valid());
|
||||||
|
|
||||||
auto main_widget = GUI::Widget::construct();
|
auto& main_widget = set_main_widget<GUI::Widget>();
|
||||||
main_widget->set_layout(make<GUI::VerticalBoxLayout>());
|
main_widget.set_layout(make<GUI::VerticalBoxLayout>());
|
||||||
main_widget->layout()->set_margins({ 4, 4, 4, 4 });
|
main_widget.layout()->set_margins({ 4, 4, 4, 4 });
|
||||||
main_widget->set_fill_with_background_color(true);
|
main_widget.set_fill_with_background_color(true);
|
||||||
|
|
||||||
set_main_widget(main_widget);
|
|
||||||
set_rect({ 0, 0, 360, 420 });
|
set_rect({ 0, 0, 360, 420 });
|
||||||
set_resizable(false);
|
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");
|
auto general_tab = tab_widget->add_tab<GUI::Widget>("General");
|
||||||
general_tab->set_layout(make<GUI::VerticalBoxLayout>());
|
general_tab->set_layout(make<GUI::VerticalBoxLayout>());
|
||||||
|
@ -131,8 +130,8 @@ PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, boo
|
||||||
|
|
||||||
general_tab->layout()->add_spacer();
|
general_tab->layout()->add_spacer();
|
||||||
|
|
||||||
auto button_widget = main_widget->add<GUI::Widget>();
|
auto button_widget = main_widget.add<GUI::Widget>();
|
||||||
button_widget->set_layout(make<GUI::HorizontalBoxLayout>());
|
button_widget->set_layout<GUI::HorizontalBoxLayout>();
|
||||||
button_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
button_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||||
button_widget->set_preferred_size(0, 24);
|
button_widget->set_preferred_size(0, 24);
|
||||||
button_widget->layout()->set_spacing(5);
|
button_widget->layout()->set_spacing(5);
|
||||||
|
|
|
@ -92,12 +92,12 @@ int main(int argc, char** argv)
|
||||||
auto heigth = config->read_num_entry("Window", "Heigth", 480);
|
auto heigth = config->read_num_entry("Window", "Heigth", 480);
|
||||||
window->set_rect({ left, top, width, heigth });
|
window->set_rect({ left, top, width, heigth });
|
||||||
|
|
||||||
auto widget = GUI::Widget::construct();
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
||||||
widget->set_layout(make<GUI::VerticalBoxLayout>());
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
widget->layout()->set_spacing(0);
|
widget.layout()->set_spacing(0);
|
||||||
|
|
||||||
auto main_toolbar = widget->add<GUI::ToolBar>();
|
auto main_toolbar = widget.add<GUI::ToolBar>();
|
||||||
auto location_toolbar = widget->add<GUI::ToolBar>();
|
auto location_toolbar = widget.add<GUI::ToolBar>();
|
||||||
location_toolbar->layout()->set_margins({ 6, 3, 6, 3 });
|
location_toolbar->layout()->set_margins({ 6, 3, 6, 3 });
|
||||||
location_toolbar->set_preferred_size(0, 25);
|
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 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 tree_view = splitter->add<GUI::TreeView>();
|
||||||
auto directories_model = GUI::FileSystemModel::create("/", GUI::FileSystemModel::Mode::DirectoriesOnly);
|
auto directories_model = GUI::FileSystemModel::create("/", GUI::FileSystemModel::Mode::DirectoriesOnly);
|
||||||
tree_view->set_model(directories_model);
|
tree_view->set_model(directories_model);
|
||||||
|
@ -122,7 +122,7 @@ int main(int argc, char** argv)
|
||||||
tree_view->set_preferred_size(150, 0);
|
tree_view->set_preferred_size(150, 0);
|
||||||
auto directory_view = splitter->add<DirectoryView>();
|
auto directory_view = splitter->add<DirectoryView>();
|
||||||
|
|
||||||
auto statusbar = widget->add<GUI::StatusBar>();
|
auto statusbar = widget.add<GUI::StatusBar>();
|
||||||
|
|
||||||
auto progressbar = statusbar->add<GUI::ProgressBar>();
|
auto progressbar = statusbar->add<GUI::ProgressBar>();
|
||||||
progressbar->set_caption("Generating thumbnails: ");
|
progressbar->set_caption("Generating thumbnails: ");
|
||||||
|
@ -641,7 +641,6 @@ int main(int argc, char** argv)
|
||||||
directory_view->open(initial_location);
|
directory_view->open(initial_location);
|
||||||
directory_view->set_focus(true);
|
directory_view->set_focus(true);
|
||||||
|
|
||||||
window->set_main_widget(widget);
|
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
|
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
|
||||||
|
|
|
@ -78,13 +78,13 @@ int main(int argc, char* argv[])
|
||||||
window->set_title("Help");
|
window->set_title("Help");
|
||||||
window->set_rect(300, 200, 570, 500);
|
window->set_rect(300, 200, 570, 500);
|
||||||
|
|
||||||
auto widget = GUI::Widget::construct();
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
||||||
widget->set_layout(make<GUI::VerticalBoxLayout>());
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
widget->layout()->set_spacing(0);
|
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();
|
auto model = ManualModel::create();
|
||||||
|
|
||||||
|
@ -195,7 +195,6 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
app.set_menubar(move(menubar));
|
app.set_menubar(move(menubar));
|
||||||
|
|
||||||
window->set_main_widget(widget);
|
|
||||||
window->set_focused_widget(tree_view);
|
window->set_focused_widget(tree_view);
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
|
|
|
@ -39,17 +39,16 @@ int main(int argc, char** argv)
|
||||||
window->set_rect(100, 100, 240, 160);
|
window->set_rect(100, 100, 240, 160);
|
||||||
window->set_title("Hello World!");
|
window->set_title("Hello World!");
|
||||||
|
|
||||||
auto main_widget = GUI::Widget::construct();
|
auto& main_widget = window->set_main_widget<GUI::Widget>();
|
||||||
window->set_main_widget(main_widget);
|
main_widget.set_fill_with_background_color(true);
|
||||||
main_widget->set_fill_with_background_color(true);
|
main_widget.set_background_color(Color::White);
|
||||||
main_widget->set_background_color(Color::White);
|
auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
main_widget->set_layout(make<GUI::VerticalBoxLayout>());
|
layout.set_margins({ 4, 4, 4, 4 });
|
||||||
main_widget->layout()->set_margins({ 4, 4, 4, 4 });
|
|
||||||
|
|
||||||
auto label = main_widget->add<GUI::Label>();
|
auto label = main_widget.add<GUI::Label>();
|
||||||
label->set_text("Hello\nWorld!");
|
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_text("Good-bye");
|
||||||
button->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
button->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||||
button->set_preferred_size(0, 20);
|
button->set_preferred_size(0, 20);
|
||||||
|
|
|
@ -98,6 +98,13 @@ public:
|
||||||
const Layout* layout() const { return m_layout.ptr(); }
|
const Layout* layout() const { return m_layout.ptr(); }
|
||||||
void set_layout(OwnPtr<Layout>&&);
|
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 horizontal_size_policy() const { return m_horizontal_size_policy; }
|
||||||
SizePolicy vertical_size_policy() const { return m_vertical_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; }
|
SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
|
||||||
|
|
|
@ -125,6 +125,14 @@ public:
|
||||||
const Widget* main_widget() const { return m_main_widget; }
|
const Widget* main_widget() const { return m_main_widget; }
|
||||||
void set_main_widget(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; }
|
Widget* focused_widget() { return m_focused_widget; }
|
||||||
const Widget* focused_widget() const { return m_focused_widget; }
|
const Widget* focused_widget() const { return m_focused_widget; }
|
||||||
void set_focused_widget(Widget*);
|
void set_focused_widget(Widget*);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue