1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:47:34 +00:00

Everywhere: Replace Model::update() with Model::invalidate()

Most of the models were just calling did_update anyway, which is
pointless since it can be unified to the base Model class. Instead, code
calling update() will now call invalidate(), which functions identically
and is more obvious in what it does.

Additionally, a default implementation is provided, which removes the
need to add empty implementations of update() for each model subclass.

Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
This commit is contained in:
sin-ack 2021-05-25 14:13:19 +00:00 committed by Andreas Kling
parent 5cd2e0f3a2
commit ca2c81251a
100 changed files with 116 additions and 261 deletions

View file

@ -173,7 +173,6 @@ protected:
bool m_editable { false };
bool m_searchable { true };
ModelIndex m_edit_index;
RefPtr<Widget> m_edit_widget;
Gfx::IntRect m_edit_widget_content_rect;
OwnPtr<ModelEditingDelegate> m_editing_delegate;
@ -181,20 +180,22 @@ protected:
Gfx::IntPoint m_left_mousedown_position;
bool m_might_drag { false };
ModelIndex m_hovered_index;
ModelIndex m_highlighted_search_index;
int m_key_column { -1 };
SortOrder m_sort_order;
ModelIndex m_edit_index;
ModelIndex m_hovered_index;
ModelIndex m_highlighted_search_index;
private:
RefPtr<Model> m_model;
ModelSelection m_selection;
ModelIndex m_selection_start_index;
String m_searching;
RefPtr<Core::Timer> m_searching_timer;
ModelIndex m_cursor_index;
ModelIndex m_drop_candidate_index;
RefPtr<Model> m_model;
ModelSelection m_selection;
String m_searching;
RefPtr<Core::Timer> m_searching_timer;
SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
SelectionMode m_selection_mode { SelectionMode::SingleSelection };
unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };

View file

@ -69,10 +69,6 @@ public:
return {};
}
virtual void update() override
{
did_update();
};
void set_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions) { m_suggestions = move(suggestions); }
@ -110,7 +106,7 @@ void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& s
m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
}
m_suggestion_view->model()->update();
m_suggestion_view->model()->invalidate();
m_suggestion_view->update();
if (!has_suggestions)
close();

View file

@ -135,7 +135,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, const StringView& filen
if (rc < 0) {
MessageBox::show(this, String::formatted("mkdir(\"{}\") failed: {}", new_dir_path, strerror(errno)), "Error", MessageBox::Type::Error);
} else {
m_model->update();
m_model->invalidate();
}
}
},
@ -178,7 +178,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, const StringView& filen
m_context_menu->add_action(GUI::Action::create_checkable(
"Show dotfiles", { Mod_Ctrl, Key_H }, [&](auto& action) {
m_model->set_should_show_dotfiles(action.is_checked());
m_model->update();
m_model->invalidate();
},
this));

View file

@ -286,7 +286,7 @@ FileSystemModel::FileSystemModel(String root_path, Mode mode)
did_update();
};
update();
invalidate();
}
FileSystemModel::~FileSystemModel()
@ -364,7 +364,7 @@ void FileSystemModel::set_root_path(String root_path)
m_root_path = {};
else
m_root_path = LexicalPath::canonicalized_path(move(root_path));
update();
invalidate();
if (m_root->has_error()) {
if (on_directory_change_error)
@ -374,7 +374,7 @@ void FileSystemModel::set_root_path(String root_path)
}
}
void FileSystemModel::update()
void FileSystemModel::invalidate()
{
m_root = adopt_own(*new Node(*this));
@ -383,7 +383,7 @@ void FileSystemModel::update()
m_root->reify_if_needed();
did_update();
Model::invalidate();
}
int FileSystemModel::row_count(const ModelIndex& index) const
@ -665,7 +665,9 @@ void FileSystemModel::set_should_show_dotfiles(bool show)
if (m_should_show_dotfiles == show)
return;
m_should_show_dotfiles = show;
update();
// FIXME: add a way to granularly update in this case.
invalidate();
}
bool FileSystemModel::is_editable(const ModelIndex& index) const

View file

@ -122,7 +122,6 @@ public:
virtual int column_count(const ModelIndex& = ModelIndex()) const override;
virtual String column_name(int column) const override;
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override;
virtual void update() override;
virtual ModelIndex parent_index(const ModelIndex&) const override;
virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const override;
virtual StringView drag_data_type() const override { return "text/uri-list"; }
@ -132,6 +131,7 @@ public:
virtual bool is_searchable() const override { return true; }
virtual void set_data(const ModelIndex&, const Variant&) override;
virtual Vector<ModelIndex, 1> matches(const StringView&, unsigned = MatchesFlag::AllMatching, const ModelIndex& = ModelIndex()) override;
virtual void invalidate() override;
static String timestamp_string(time_t timestamp)
{

View file

@ -44,9 +44,9 @@ Variant FilteringProxyModel::data(const ModelIndex& index, ModelRole role) const
return m_matching_indices[index.row()].data(role);
}
void FilteringProxyModel::update()
void FilteringProxyModel::invalidate()
{
m_model.update();
m_model.invalidate();
filter();
did_update();
}
@ -84,7 +84,7 @@ void FilteringProxyModel::set_filter_term(const StringView& term)
if (m_filter_term == term)
return;
m_filter_term = term;
update();
invalidate();
}
ModelIndex FilteringProxyModel::map(const ModelIndex& index) const

View file

@ -26,7 +26,7 @@ public:
virtual int row_count(const ModelIndex& = ModelIndex()) const override;
virtual int column_count(const ModelIndex& = ModelIndex()) const override;
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override;
virtual void update() override;
virtual void invalidate() override;
virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const override;
virtual bool is_searchable() const override;
virtual Vector<ModelIndex, 1> matches(const StringView&, unsigned = MatchesFlag::AllMatching, const ModelIndex& = ModelIndex()) override;

View file

@ -71,7 +71,7 @@ FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, boo
if (m_weight.has_value())
index_of_old_weight_in_new_list = m_weights.find_first_index(m_weight.value());
m_weight_list_view->model()->update();
m_weight_list_view->model()->invalidate();
m_weight_list_view->set_cursor(m_weight_list_view->model()->index(index_of_old_weight_in_new_list.value_or(0)), GUI::AbstractView::SelectionUpdate::Set);
update_font();
};
@ -110,7 +110,7 @@ FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, boo
}
});
quick_sort(m_sizes);
m_size_list_view->model()->update();
m_size_list_view->model()->invalidate();
m_size_list_view->set_selection_mode(GUI::AbstractView::SelectionMode::SingleSelection);
if (m_size.has_value()) {
@ -188,8 +188,8 @@ void FontPicker::set_font(const Gfx::Font* font)
m_size = {};
m_weights.clear();
m_sizes.clear();
m_weight_list_view->model()->update();
m_size_list_view->model()->update();
m_weight_list_view->model()->invalidate();
m_size_list_view->model()->invalidate();
return;
}

View file

@ -77,11 +77,6 @@ public:
return {};
}
virtual void update() override
{
did_update();
}
protected:
explicit ItemListModel(const Container& data, Optional<size_t> row_count = {}) requires(!IsTwoDimensional)
: m_data(data)

View file

@ -10,7 +10,7 @@
namespace GUI {
void JsonArrayModel::update()
void JsonArrayModel::invalidate()
{
auto file = Core::File::construct(m_json_path);
if (!file->open(Core::OpenMode::ReadOnly)) {
@ -131,7 +131,7 @@ void JsonArrayModel::set_json_path(const String& json_path)
return;
m_json_path = json_path;
update();
invalidate();
}
}

View file

@ -50,7 +50,7 @@ public:
virtual int column_count(const ModelIndex& = ModelIndex()) const override { return m_fields.size(); }
virtual String column_name(int column) const override { return m_fields[column].column_name; }
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override;
virtual void update() override;
virtual void invalidate() override;
const String& json_path() const { return m_json_path; }
void set_json_path(const String& json_path);

View file

@ -29,6 +29,11 @@ void Model::unregister_view(Badge<AbstractView>, AbstractView& view)
m_clients.remove(&view);
}
void Model::invalidate()
{
did_update();
}
void Model::for_each_view(Function<void(AbstractView&)> callback)
{
for (auto* view : m_views)

View file

@ -56,7 +56,7 @@ public:
virtual String column_name(int) const { return {}; }
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const = 0;
virtual TriState data_matches(const ModelIndex&, const Variant&) const { return TriState::Unknown; }
virtual void update() = 0;
virtual void invalidate();
virtual ModelIndex parent_index(const ModelIndex&) const { return {}; }
virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const;
virtual bool is_editable(const ModelIndex&) const { return false; }

View file

@ -65,7 +65,7 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView&
done(ExecCancel);
};
m_table_view->model()->update();
m_table_view->model()->invalidate();
m_refresh_timer = add<Core::Timer>();
@ -77,7 +77,7 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView&
previous_selected_pid = pid_as_variant.as_i32();
}
m_table_view->model()->update();
m_table_view->model()->invalidate();
if (previous_selected_pid == -1) {
return;

View file

@ -28,7 +28,8 @@ public:
virtual int column_count(const GUI::ModelIndex&) const override;
virtual String column_name(int column_index) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
void update();
private:
RunningProcessesModel();

View file

@ -14,7 +14,7 @@ SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model> source)
: m_source(move(source))
{
m_source->register_client(*this);
invalidate();
update_sort();
}
SortingProxyModel::~SortingProxyModel()
@ -22,7 +22,13 @@ SortingProxyModel::~SortingProxyModel()
m_source->unregister_client(*this);
}
void SortingProxyModel::invalidate(unsigned int flags)
void SortingProxyModel::invalidate()
{
source().invalidate();
Model::invalidate();
}
void SortingProxyModel::update_sort(unsigned flags)
{
if (flags == UpdateFlag::DontInvalidateIndices) {
sort(m_last_key_column, m_last_sort_order);
@ -40,7 +46,7 @@ void SortingProxyModel::invalidate(unsigned int flags)
void SortingProxyModel::model_did_update(unsigned flags)
{
invalidate(flags);
update_sort(flags);
}
bool SortingProxyModel::accepts_drag(const ModelIndex& proxy_index, const Vector<String>& mime_types) const
@ -110,11 +116,6 @@ Variant SortingProxyModel::data(const ModelIndex& proxy_index, ModelRole role) c
return source().data(map_to_source(proxy_index), role);
}
void SortingProxyModel::update()
{
source().update();
}
StringView SortingProxyModel::drag_data_type() const
{
return source().drag_data_type();

View file

@ -21,7 +21,7 @@ public:
virtual int column_count(const ModelIndex& = ModelIndex()) const override;
virtual String column_name(int) const override;
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override;
virtual void update() override;
virtual void invalidate() override;
virtual StringView drag_data_type() const override;
virtual ModelIndex parent_index(const ModelIndex&) const override;
virtual ModelIndex index(int row, int column, const ModelIndex& parent) const override;
@ -63,7 +63,7 @@ private:
Model& source() { return *m_source; }
const Model& source() const { return *m_source; }
void invalidate(unsigned flags = Model::UpdateFlag::DontInvalidateIndices);
void update_sort(unsigned = UpdateFlag::DontInvalidateIndices);
InternalMapIterator build_mapping(const ModelIndex& proxy_index);
NonnullRefPtr<Model> m_source;