diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-09-22 00:17:53 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-22 00:25:25 +0200 |
commit | bc319d9e8873734bb8e8cea3d762d7fab2ded887 (patch) | |
tree | c8648b92fe32e6c48454a99356f7b3ca59b5e423 /Libraries/LibGUI | |
parent | 0c72e0c09f085a371b0abdba172325e56b1e6a06 (diff) | |
download | serenity-bc319d9e8873734bb8e8cea3d762d7fab2ded887.zip |
LibCore: Make CObject reference-counted
Okay, I've spent a whole day on this now, and it finally kinda works!
With this patch, CObject and all of its derived classes are reference
counted instead of tree-owned.
The previous, Qt-like model was nice and familiar, but ultimately also
outdated and difficult to reason about.
CObject-derived types should now be stored in RefPtr/NonnullRefPtr and
each class can be constructed using the forwarding construct() helper:
auto widget = GWidget::construct(parent_widget);
Note that construct() simply forwards all arguments to an existing
constructor. It is inserted into each class by the C_OBJECT macro,
see CObject.h to understand how that works.
CObject::delete_later() disappears in this patch, as there is no longer
a single logical owner of a CObject.
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r-- | Libraries/LibGUI/GAboutDialog.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGUI/GApplication.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibGUI/GFilePicker.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibGUI/GInputBox.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibGUI/GMessageBox.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibGUI/GWidget.cpp | 14 |
6 files changed, 20 insertions, 21 deletions
diff --git a/Libraries/LibGUI/GAboutDialog.cpp b/Libraries/LibGUI/GAboutDialog.cpp index 4fa3666d9e..5bc8c50ed6 100644 --- a/Libraries/LibGUI/GAboutDialog.cpp +++ b/Libraries/LibGUI/GAboutDialog.cpp @@ -18,7 +18,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Horizontal)); - auto left_container = GWidget::construct(widget); + auto left_container = GWidget::construct(widget.ptr()); left_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); left_container->set_preferred_size(48, 0); left_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); @@ -28,7 +28,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C icon_label->set_preferred_size(40, 40); left_container->layout()->add_spacer(); - auto right_container = GWidget::construct(widget); + auto right_container = GWidget::construct(widget.ptr()); right_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); right_container->layout()->set_margins({ 0, 4, 4, 4 }); @@ -46,7 +46,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C right_container->layout()->add_spacer(); - auto button_container = GWidget::construct(right_container); + auto button_container = GWidget::construct(right_container.ptr()); button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button_container->set_preferred_size(0, 20); button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); diff --git a/Libraries/LibGUI/GApplication.cpp b/Libraries/LibGUI/GApplication.cpp index a2172fe552..1c404225c9 100644 --- a/Libraries/LibGUI/GApplication.cpp +++ b/Libraries/LibGUI/GApplication.cpp @@ -110,7 +110,6 @@ void GApplication::hide_tooltip() { if (m_tooltip_window) { m_tooltip_window->hide(); - delete m_tooltip_window; m_tooltip_window = nullptr; } } diff --git a/Libraries/LibGUI/GFilePicker.cpp b/Libraries/LibGUI/GFilePicker.cpp index ab60cb58a4..efe1f6444a 100644 --- a/Libraries/LibGUI/GFilePicker.cpp +++ b/Libraries/LibGUI/GFilePicker.cpp @@ -57,11 +57,11 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie horizontal_container->set_fill_with_background_color(true); horizontal_container->set_background_color(Color::WarmGray); - auto vertical_container = GWidget::construct(horizontal_container); + auto vertical_container = GWidget::construct(horizontal_container.ptr()); vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); vertical_container->layout()->set_spacing(4); - auto upper_container = GWidget::construct(vertical_container); + auto upper_container = GWidget::construct(vertical_container.ptr()); upper_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); upper_container->layout()->set_spacing(4); upper_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); @@ -118,13 +118,13 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie }); toolbar->add_action(*mkdir_action); - auto lower_container = GWidget::construct(vertical_container); + auto lower_container = GWidget::construct(vertical_container.ptr()); lower_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); lower_container->layout()->set_spacing(4); lower_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); lower_container->set_preferred_size(0, 60); - auto filename_container = GWidget::construct(lower_container); + auto filename_container = GWidget::construct(lower_container.ptr()); filename_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); filename_container->set_preferred_size(0, 20); filename_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); @@ -132,7 +132,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie filename_label->set_text_alignment(TextAlignment::CenterLeft); filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); filename_label->set_preferred_size(60, 0); - m_filename_textbox = GTextBox::construct(filename_container); + m_filename_textbox = GTextBox::construct(filename_container.ptr()); if (m_mode == Mode::Save) { m_filename_textbox->set_text(file_name); m_filename_textbox->set_focus(true); @@ -155,7 +155,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie set_preview(path); }; - auto button_container = GWidget::construct(lower_container); + auto button_container = GWidget::construct(lower_container.ptr()); button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button_container->set_preferred_size(0, 20); button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); diff --git a/Libraries/LibGUI/GInputBox.cpp b/Libraries/LibGUI/GInputBox.cpp index 483cb12045..521e2c0911 100644 --- a/Libraries/LibGUI/GInputBox.cpp +++ b/Libraries/LibGUI/GInputBox.cpp @@ -42,12 +42,12 @@ void GInputBox::build() m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); m_text_editor->set_preferred_size(0, 19); - auto button_container_outer = GWidget::construct(widget); + auto button_container_outer = GWidget::construct(widget.ptr()); button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button_container_outer->set_preferred_size(0, 20); button_container_outer->set_layout(make<GBoxLayout>(Orientation::Vertical)); - auto button_container_inner = GWidget::construct(button_container_outer); + auto button_container_inner = GWidget::construct(button_container_outer.ptr()); button_container_inner->set_layout(make<GBoxLayout>(Orientation::Horizontal)); button_container_inner->layout()->set_spacing(8); diff --git a/Libraries/LibGUI/GMessageBox.cpp b/Libraries/LibGUI/GMessageBox.cpp index c074d65210..d59c03bd25 100644 --- a/Libraries/LibGUI/GMessageBox.cpp +++ b/Libraries/LibGUI/GMessageBox.cpp @@ -64,7 +64,7 @@ void GMessageBox::build() ObjectPtr<GWidget> message_container = widget; if (m_type != Type::None) { - message_container = GWidget::construct(widget); + message_container = GWidget::construct(widget.ptr()); message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); message_container->layout()->set_margins({ 8, 0, 8, 0 }); message_container->layout()->set_spacing(8); @@ -80,7 +80,7 @@ void GMessageBox::build() label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); label->set_preferred_size(text_width, 16); - auto button_container = GWidget::construct(widget); + auto button_container = GWidget::construct(widget.ptr()); button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); button_container->layout()->set_spacing(5); button_container->layout()->set_margins({ 15, 0, 15, 0 }); diff --git a/Libraries/LibGUI/GWidget.cpp b/Libraries/LibGUI/GWidget.cpp index 66ff1eed53..1aa678c846 100644 --- a/Libraries/LibGUI/GWidget.cpp +++ b/Libraries/LibGUI/GWidget.cpp @@ -325,13 +325,13 @@ Rect GWidget::screen_relative_rect() const GWidget* GWidget::child_at(const Point& point) const { for (int i = children().size() - 1; i >= 0; --i) { - if (!is<GWidget>(*children()[i])) + if (!is<GWidget>(children()[i])) continue; - auto& child = to<GWidget>(*children()[i]); + auto& child = to<GWidget>(children()[i]); if (!child.is_visible()) continue; if (child.relative_rect().contains(point)) - return &child; + return const_cast<GWidget*>(&child); } return nullptr; } @@ -492,7 +492,7 @@ void GWidget::move_to_front() parent->children().remove_first_matching([this](auto& entry) { return entry == this; }); - parent->children().append(this); + parent->children().append(*this); parent->update(); } @@ -506,7 +506,7 @@ void GWidget::move_to_back() parent->children().remove_first_matching([this](auto& entry) { return entry == this; }); - parent->children().prepend(this); + parent->children().prepend(*this); parent->update(); } @@ -515,7 +515,7 @@ bool GWidget::is_frontmost() const auto* parent = parent_widget(); if (!parent) return true; - return parent->children().last() == this; + return &parent->children().last() == this; } bool GWidget::is_backmost() const @@ -523,7 +523,7 @@ bool GWidget::is_backmost() const auto* parent = parent_widget(); if (!parent) return true; - return parent->children().first() == this; + return &parent->children().first() == this; } GAction* GWidget::action_for_key_event(const GKeyEvent& event) |