mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:57:46 +00:00
LibGUI: Add ModelClient abstract class and allow registering clients
This solves a problem where the SortingProxyModel doesn't receive the on_update call because other code overwrote the handler later on.
This commit is contained in:
parent
0e10a92ebc
commit
b778804d20
13 changed files with 144 additions and 60 deletions
|
@ -88,16 +88,18 @@ BookmarksBarWidget::BookmarksBarWidget(const String& bookmarks_file, bool enable
|
|||
|
||||
BookmarksBarWidget::~BookmarksBarWidget()
|
||||
{
|
||||
if (m_model)
|
||||
m_model->unregister_client(*this);
|
||||
}
|
||||
|
||||
void BookmarksBarWidget::set_model(RefPtr<GUI::Model> model)
|
||||
{
|
||||
if (model == m_model)
|
||||
return;
|
||||
if (m_model)
|
||||
m_model->unregister_client(*this);
|
||||
m_model = move(model);
|
||||
m_model->on_update = [&]() {
|
||||
did_update_model();
|
||||
};
|
||||
m_model->register_client(*this);
|
||||
}
|
||||
|
||||
void BookmarksBarWidget::resize_event(GUI::ResizeEvent& event)
|
||||
|
@ -106,7 +108,7 @@ void BookmarksBarWidget::resize_event(GUI::ResizeEvent& event)
|
|||
update_content_size();
|
||||
}
|
||||
|
||||
void BookmarksBarWidget::did_update_model()
|
||||
void BookmarksBarWidget::on_model_update(unsigned)
|
||||
{
|
||||
for (auto* child : child_widgets()) {
|
||||
child->remove_from_parent();
|
||||
|
|
|
@ -27,11 +27,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibGUI/Forward.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
namespace Browser {
|
||||
|
||||
class BookmarksBarWidget final : public GUI::Widget {
|
||||
class BookmarksBarWidget final : public GUI::Widget
|
||||
, private GUI::ModelClient {
|
||||
C_OBJECT(BookmarksBarWidget)
|
||||
public:
|
||||
static BookmarksBarWidget& the();
|
||||
|
@ -52,7 +54,7 @@ public:
|
|||
private:
|
||||
BookmarksBarWidget(const String&, bool enabled);
|
||||
|
||||
virtual void did_update_model();
|
||||
virtual void on_model_update(unsigned) override;
|
||||
virtual void resize_event(GUI::ResizeEvent&) override;
|
||||
|
||||
void update_content_size();
|
||||
|
|
|
@ -95,16 +95,7 @@ DirectoryView::DirectoryView()
|
|||
on_path_change(model().root_path());
|
||||
};
|
||||
|
||||
// NOTE: We're using the on_update hook on the GUI::SortingProxyModel here instead of
|
||||
// the GUI::FileSystemModel's hook. This is because GUI::SortingProxyModel has already
|
||||
// installed an on_update hook on the GUI::FileSystemModel internally.
|
||||
// FIXME: This is an unfortunate design. We should come up with something better.
|
||||
m_table_view->model()->on_update = [this] {
|
||||
for_each_view_implementation([](auto& view) {
|
||||
view.selection().clear();
|
||||
});
|
||||
update_statusbar();
|
||||
};
|
||||
m_model->register_client(*this);
|
||||
|
||||
m_model->on_thumbnail_progress = [this](int done, int total) {
|
||||
if (on_thumbnail_progress)
|
||||
|
@ -169,6 +160,17 @@ DirectoryView::DirectoryView()
|
|||
|
||||
DirectoryView::~DirectoryView()
|
||||
{
|
||||
m_model->unregister_client(*this);
|
||||
}
|
||||
|
||||
void DirectoryView::on_model_update(unsigned flags)
|
||||
{
|
||||
if (flags & GUI::Model::UpdateFlag::InvalidateAllIndexes) {
|
||||
for_each_view_implementation([](auto& view) {
|
||||
view.selection().clear();
|
||||
});
|
||||
}
|
||||
update_statusbar();
|
||||
}
|
||||
|
||||
void DirectoryView::set_view_mode(ViewMode mode)
|
||||
|
|
|
@ -34,7 +34,8 @@
|
|||
#include <LibGUI/TableView.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
class DirectoryView final : public GUI::StackWidget {
|
||||
class DirectoryView final : public GUI::StackWidget
|
||||
, private GUI::ModelClient {
|
||||
C_OBJECT(DirectoryView)
|
||||
public:
|
||||
virtual ~DirectoryView() override;
|
||||
|
@ -94,6 +95,8 @@ private:
|
|||
DirectoryView();
|
||||
const GUI::FileSystemModel& model() const { return *m_model; }
|
||||
|
||||
virtual void on_model_update(unsigned) override;
|
||||
|
||||
void handle_activation(const GUI::ModelIndex&);
|
||||
|
||||
void set_status_message(const StringView&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue