From 47b63390253ba39b01a841ad29ae434d8e5c436e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 24 Nov 2021 13:13:30 +0100 Subject: LibGUI: Add GUI::TabWidget::try_add_tab(...) This is a fallible variant of add_tab(...) that returns ErrorOr. --- Userland/Libraries/LibGUI/TabWidget.cpp | 8 +++++++- Userland/Libraries/LibGUI/TabWidget.h | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGUI/TabWidget.cpp b/Userland/Libraries/LibGUI/TabWidget.cpp index 474b805a9d..d2c4136183 100644 --- a/Userland/Libraries/LibGUI/TabWidget.cpp +++ b/Userland/Libraries/LibGUI/TabWidget.cpp @@ -44,13 +44,19 @@ TabWidget::~TabWidget() { } -void TabWidget::add_widget(StringView title, Widget& widget) +ErrorOr TabWidget::try_add_widget(StringView title, Widget& widget) { m_tabs.append({ title, nullptr, &widget }); add_child(widget); update_focus_policy(); if (on_tab_count_change) on_tab_count_change(m_tabs.size()); + return {}; +} + +void TabWidget::add_widget(StringView title, Widget& widget) +{ + MUST(try_add_widget(title, widget)); } void TabWidget::remove_widget(Widget& widget) diff --git a/Userland/Libraries/LibGUI/TabWidget.h b/Userland/Libraries/LibGUI/TabWidget.h index bd232857e1..ac0d5402d0 100644 --- a/Userland/Libraries/LibGUI/TabWidget.h +++ b/Userland/Libraries/LibGUI/TabWidget.h @@ -36,9 +36,19 @@ public: GUI::Margins const& container_margins() const { return m_container_margins; } void set_container_margins(GUI::Margins const&); + ErrorOr try_add_widget(StringView, Widget&); + void add_widget(StringView, Widget&); void remove_widget(Widget&); + template + ErrorOr> try_add_tab(StringView title, Args&&... args) + { + auto t = TRY(T::try_create(forward(args)...)); + TRY(try_add_widget(title, *t)); + return *t; + } + template T& add_tab(StringView title, Args&&... args) { -- cgit v1.2.3