mirror of
https://github.com/RGBCube/serenity
synced 2025-07-29 01:07:35 +00:00
LibGUI: Allow FilteringProxyModel to optionally sort results by score
When the `FilteringOptions::SortByScore` flag is set, filtered indices are sorted by match score in descending order, meaning the most relevant results should appear first. The default behavior of FilteringProxyModel is unchanged.
This commit is contained in:
parent
a042c4e93d
commit
556c4ac358
15 changed files with 89 additions and 51 deletions
|
@ -88,17 +88,18 @@ 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
|
||||
GUI::Model::MatchResult CookiesModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
|
||||
{
|
||||
auto needle = term.as_string();
|
||||
if (needle.is_empty())
|
||||
return TriState::True;
|
||||
return { TriState::True };
|
||||
|
||||
auto const& cookie = m_cookies[index.row()];
|
||||
auto haystack = DeprecatedString::formatted("{} {} {} {}", cookie.domain, cookie.path, cookie.name, cookie.value);
|
||||
if (fuzzy_match(needle, haystack).score > 0)
|
||||
return TriState::True;
|
||||
return TriState::False;
|
||||
auto match_result = fuzzy_match(needle, haystack);
|
||||
if (match_result.score > 0)
|
||||
return { TriState::True, match_result.score };
|
||||
return { TriState::False };
|
||||
}
|
||||
|
||||
Web::Cookie::Cookie CookiesModel::take_cookie(GUI::ModelIndex const& index)
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
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;
|
||||
virtual GUI::Model::MatchResult data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override;
|
||||
|
||||
Web::Cookie::Cookie take_cookie(GUI::ModelIndex const&);
|
||||
AK::Vector<Web::Cookie::Cookie> take_all_cookies();
|
||||
|
|
|
@ -72,17 +72,18 @@ GUI::Variant HistoryModel::data(GUI::ModelIndex const& index, GUI::ModelRole rol
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
TriState HistoryModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
|
||||
GUI::Model::MatchResult HistoryModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
|
||||
{
|
||||
auto needle = term.as_string();
|
||||
if (needle.is_empty())
|
||||
return TriState::True;
|
||||
return { TriState::True };
|
||||
|
||||
auto const& history_entry = m_entries[index.row()];
|
||||
auto haystack = DeprecatedString::formatted("{} {}", history_entry.title, history_entry.url.serialize());
|
||||
if (fuzzy_match(needle, haystack).score > 0)
|
||||
return TriState::True;
|
||||
return TriState::False;
|
||||
auto match_result = fuzzy_match(needle, haystack);
|
||||
if (match_result.score > 0)
|
||||
return { TriState::True, match_result.score };
|
||||
return { TriState::False };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public:
|
|||
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;
|
||||
virtual GUI::Model::MatchResult data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override;
|
||||
|
||||
private:
|
||||
AK::Vector<History::URLTitlePair> m_entries;
|
||||
|
|
|
@ -75,20 +75,21 @@ GUI::Variant StorageModel::data(GUI::ModelIndex const& index, GUI::ModelRole rol
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
TriState StorageModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
|
||||
GUI::Model::MatchResult StorageModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
|
||||
{
|
||||
auto needle = term.as_string();
|
||||
if (needle.is_empty())
|
||||
return TriState::True;
|
||||
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 = DeprecatedString::formatted("{} {}", local_storage_key, local_storage_value);
|
||||
if (fuzzy_match(needle, haystack).score > 0)
|
||||
return TriState::True;
|
||||
return TriState::False;
|
||||
auto match_result = fuzzy_match(needle, haystack);
|
||||
if (match_result.score > 0)
|
||||
return { TriState::True, match_result.score };
|
||||
return { TriState::False };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public:
|
|||
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;
|
||||
virtual GUI::Model::MatchResult data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override;
|
||||
|
||||
private:
|
||||
OrderedHashMap<DeprecatedString, DeprecatedString> m_local_storage_entries;
|
||||
|
|
|
@ -208,23 +208,26 @@ void ManualModel::update_section_node_on_toggle(const GUI::ModelIndex& index, bo
|
|||
static_cast<Manual::SectionNode*>(node)->set_open(open);
|
||||
}
|
||||
|
||||
TriState ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Variant& term) const
|
||||
GUI::Model::MatchResult ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Variant& term) const
|
||||
{
|
||||
auto name = page_name(index);
|
||||
if (!name.has_value())
|
||||
return TriState::False;
|
||||
return { TriState::False };
|
||||
|
||||
auto match_result = fuzzy_match(term.as_string(), name.value());
|
||||
if (match_result.score > 0)
|
||||
return TriState::True;
|
||||
return { TriState::True, match_result.score };
|
||||
|
||||
auto path = page_path(index);
|
||||
// NOTE: This is slightly inaccurate, as page_path can also fail due to OOM. We consider it acceptable to have a data mismatch in that case.
|
||||
if (!path.has_value())
|
||||
return TriState::False;
|
||||
return { TriState::False };
|
||||
auto view_result = page_view(path.release_value());
|
||||
if (view_result.is_error() || view_result.value().is_empty())
|
||||
return TriState::False;
|
||||
return { TriState::False };
|
||||
|
||||
return view_result.value().contains(term.as_string(), CaseSensitivity::CaseInsensitive) ? TriState::True : TriState::False;
|
||||
if (view_result.value().contains(term.as_string(), CaseSensitivity::CaseInsensitive))
|
||||
return { TriState::True, 0 };
|
||||
|
||||
return { TriState::False };
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ public:
|
|||
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
|
||||
virtual TriState data_matches(const GUI::ModelIndex&, const GUI::Variant&) const override;
|
||||
virtual GUI::Model::MatchResult data_matches(const GUI::ModelIndex&, const GUI::Variant&) const override;
|
||||
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
|
||||
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue