diff --git a/Userland/Applications/Browser/CookiesModel.cpp b/Userland/Applications/Browser/CookiesModel.cpp index 0345a116b45..facae3722ba 100644 --- a/Userland/Applications/Browser/CookiesModel.cpp +++ b/Userland/Applications/Browser/CookiesModel.cpp @@ -5,6 +5,7 @@ */ #include "CookiesModel.h" +#include namespace Browser { @@ -26,6 +27,13 @@ void CookiesModel::clear_items() did_update(DontInvalidateIndices); } +int CookiesModel::row_count(GUI::ModelIndex const& index) const +{ + if (!index.is_valid()) + return m_cookies.size(); + return 0; +} + String CookiesModel::column_name(int column) const { switch (column) { @@ -76,4 +84,17 @@ GUI::Variant CookiesModel::data(GUI::ModelIndex const& index, GUI::ModelRole rol VERIFY_NOT_REACHED(); } +TriState CookiesModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const +{ + auto needle = term.as_string(); + if (needle.is_empty()) + return TriState::True; + + auto const& cookie = m_cookies[index.row()]; + auto haystack = String::formatted("{} {} {} {}", cookie.domain, cookie.path, cookie.name, cookie.value); + if (fuzzy_match(needle, haystack).score > 0) + return TriState::True; + return TriState::False; +} + } diff --git a/Userland/Applications/Browser/CookiesModel.h b/Userland/Applications/Browser/CookiesModel.h index 1ddd5afe254..280a189c144 100644 --- a/Userland/Applications/Browser/CookiesModel.h +++ b/Userland/Applications/Browser/CookiesModel.h @@ -27,11 +27,12 @@ public: void set_items(AK::Vector items); void clear_items(); - virtual int row_count(GUI::ModelIndex const&) const override { return m_cookies.size(); } + virtual int row_count(GUI::ModelIndex const&) const override; virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; } virtual String column_name(int column) const override; virtual GUI::ModelIndex index(int row, int column = 0, GUI::ModelIndex const& = GUI::ModelIndex()) const override; virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override; + virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override; private: AK::Vector m_cookies; diff --git a/Userland/Applications/Browser/LocalStorageModel.cpp b/Userland/Applications/Browser/LocalStorageModel.cpp index f90da090c7d..8f199a07675 100644 --- a/Userland/Applications/Browser/LocalStorageModel.cpp +++ b/Userland/Applications/Browser/LocalStorageModel.cpp @@ -6,6 +6,8 @@ #include "LocalStorageModel.h" +#include + namespace Browser { void LocalStorageModel::set_items(OrderedHashMap map) @@ -26,6 +28,13 @@ void LocalStorageModel::clear_items() did_update(DontInvalidateIndices); } +int LocalStorageModel::row_count(GUI::ModelIndex const& index) const +{ + if (!index.is_valid()) + return m_local_storage_entries.size(); + return 0; +} + String LocalStorageModel::column_name(int column) const { switch (column) { @@ -66,4 +75,20 @@ GUI::Variant LocalStorageModel::data(GUI::ModelIndex const& index, GUI::ModelRol VERIFY_NOT_REACHED(); } +TriState LocalStorageModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const +{ + auto needle = term.as_string(); + if (needle.is_empty()) + return TriState::True; + + auto const& keys = m_local_storage_entries.keys(); + auto const& local_storage_key = keys[index.row()]; + auto const& local_storage_value = m_local_storage_entries.get(local_storage_key).value_or({}); + + auto haystack = String::formatted("{} {}", local_storage_key, local_storage_value); + if (fuzzy_match(needle, haystack).score > 0) + return TriState::True; + return TriState::False; +} + } diff --git a/Userland/Applications/Browser/LocalStorageModel.h b/Userland/Applications/Browser/LocalStorageModel.h index b210c931bfd..12870f4b288 100644 --- a/Userland/Applications/Browser/LocalStorageModel.h +++ b/Userland/Applications/Browser/LocalStorageModel.h @@ -20,11 +20,12 @@ public: void set_items(OrderedHashMap map); void clear_items(); - virtual int row_count(GUI::ModelIndex const&) const override { return m_local_storage_entries.size(); } + virtual int row_count(GUI::ModelIndex const&) const override; virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; } virtual String column_name(int column) const override; virtual GUI::ModelIndex index(int row, int column = 0, GUI::ModelIndex const& = GUI::ModelIndex()) const override; virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override; + virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override; private: OrderedHashMap m_local_storage_entries; diff --git a/Userland/Applications/Browser/StorageWidget.cpp b/Userland/Applications/Browser/StorageWidget.cpp index 0efcc19ff6f..4d1799de6a7 100644 --- a/Userland/Applications/Browser/StorageWidget.cpp +++ b/Userland/Applications/Browser/StorageWidget.cpp @@ -21,22 +21,36 @@ StorageWidget::StorageWidget() auto& tab_widget = *find_descendant_of_type_named("tab_widget"); m_cookies_table_view = tab_widget.find_descendant_of_type_named("cookies_tableview"); + m_cookies_textbox = tab_widget.find_descendant_of_type_named("cookies_filter_textbox"); m_cookies_model = adopt_ref(*new CookiesModel()); - m_cookie_sorting_model = MUST(GUI::SortingProxyModel::create(*m_cookies_model)); - m_cookie_sorting_model->set_sort_role(GUI::ModelRole::Display); + m_cookies_filtering_model = MUST(GUI::FilteringProxyModel::create(*m_cookies_model)); + m_cookies_filtering_model->set_filter_term(""); - m_cookies_table_view->set_model(m_cookie_sorting_model); + m_cookies_textbox->on_change = [this] { + m_cookies_filtering_model->set_filter_term(m_cookies_textbox->text()); + if (m_cookies_filtering_model->row_count() != 0) + m_cookies_table_view->set_cursor(m_cookies_filtering_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set); + }; + + m_cookies_table_view->set_model(m_cookies_filtering_model); m_cookies_table_view->set_column_headers_visible(true); m_cookies_table_view->set_alternating_row_colors(true); m_local_storage_table_view = tab_widget.find_descendant_of_type_named("local_storage_tableview"); + m_local_storage_textbox = tab_widget.find_descendant_of_type_named("local_storage_filter_textbox"); m_local_storage_model = adopt_ref(*new LocalStorageModel()); - m_local_storage_sorting_model = MUST(GUI::SortingProxyModel::create(*m_local_storage_model)); - m_local_storage_sorting_model->set_sort_role(GUI::ModelRole::Display); + m_local_storage_filtering_model = MUST(GUI::FilteringProxyModel::create(*m_local_storage_model)); + m_local_storage_filtering_model->set_filter_term(""); - m_local_storage_table_view->set_model(m_local_storage_sorting_model); + m_local_storage_textbox->on_change = [this] { + m_local_storage_filtering_model->set_filter_term(m_local_storage_textbox->text()); + if (m_local_storage_filtering_model->row_count() != 0) + m_local_storage_table_view->set_cursor(m_local_storage_filtering_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set); + }; + + m_local_storage_table_view->set_model(m_local_storage_filtering_model); m_local_storage_table_view->set_column_headers_visible(true); m_local_storage_table_view->set_alternating_row_colors(true); } diff --git a/Userland/Applications/Browser/StorageWidget.gml b/Userland/Applications/Browser/StorageWidget.gml index b1a44a6a35c..cb76eeb647e 100644 --- a/Userland/Applications/Browser/StorageWidget.gml +++ b/Userland/Applications/Browser/StorageWidget.gml @@ -13,6 +13,11 @@ margins: [4] } + @GUI::TextBox { + name: "cookies_filter_textbox" + placeholder: "Filter" + } + @GUI::GroupBox { layout: @GUI::VerticalBoxLayout { margins: [6] @@ -30,6 +35,11 @@ margins: [4] } + @GUI::TextBox { + name: "local_storage_filter_textbox" + placeholder: "Filter" + } + @GUI::GroupBox { layout: @GUI::VerticalBoxLayout { margins: [6] diff --git a/Userland/Applications/Browser/StorageWidget.h b/Userland/Applications/Browser/StorageWidget.h index 70eba0ca28c..69397aa0d3d 100644 --- a/Userland/Applications/Browser/StorageWidget.h +++ b/Userland/Applications/Browser/StorageWidget.h @@ -9,7 +9,8 @@ #include "CookiesModel.h" #include "LocalStorageModel.h" #include "Tab.h" -#include +#include +#include #include #include @@ -30,11 +31,14 @@ private: StorageWidget(); RefPtr m_cookies_table_view; + RefPtr m_cookies_textbox; RefPtr m_cookies_model; - RefPtr m_cookie_sorting_model; + RefPtr m_cookies_filtering_model; + RefPtr m_local_storage_table_view; + RefPtr m_local_storage_textbox; RefPtr m_local_storage_model; - RefPtr m_local_storage_sorting_model; + RefPtr m_local_storage_filtering_model; }; }