mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 04:32:44 +00:00 
			
		
		
		
	 5e1499d104
			
		
	
	
		5e1499d104
		
	
	
	
	
		
			
			This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).
This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
		
	
			
		
			
				
	
	
		
			89 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			89 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2022, the SerenityOS developers.
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include "HistoryModel.h"
 | |
| #include <AK/FuzzyMatch.h>
 | |
| 
 | |
| namespace Browser {
 | |
| 
 | |
| void HistoryModel::set_items(AK::Vector<WebView::History::URLTitlePair> items)
 | |
| {
 | |
|     begin_insert_rows({}, m_entries.size(), m_entries.size());
 | |
|     m_entries = items;
 | |
|     end_insert_rows();
 | |
| 
 | |
|     did_update(DontInvalidateIndices);
 | |
| }
 | |
| 
 | |
| void HistoryModel::clear_items()
 | |
| {
 | |
|     begin_insert_rows({}, m_entries.size(), m_entries.size());
 | |
|     m_entries.clear();
 | |
|     end_insert_rows();
 | |
| 
 | |
|     did_update(DontInvalidateIndices);
 | |
| }
 | |
| 
 | |
| int HistoryModel::row_count(GUI::ModelIndex const& index) const
 | |
| {
 | |
|     if (!index.is_valid())
 | |
|         return m_entries.size();
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| ErrorOr<String> HistoryModel::column_name(int column) const
 | |
| {
 | |
|     switch (column) {
 | |
|     case Column::Title:
 | |
|         return "Title"_string;
 | |
|     case Column::URL:
 | |
|         return "URL"_string;
 | |
|     default:
 | |
|         VERIFY_NOT_REACHED();
 | |
|     }
 | |
| 
 | |
|     return String {};
 | |
| }
 | |
| 
 | |
| GUI::ModelIndex HistoryModel::index(int row, int column, GUI::ModelIndex const&) const
 | |
| {
 | |
|     if (static_cast<size_t>(row) < m_entries.size())
 | |
|         return create_index(row, column, &m_entries.at(row));
 | |
|     return {};
 | |
| }
 | |
| 
 | |
| GUI::Variant HistoryModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
 | |
| {
 | |
|     if (role != GUI::ModelRole::Display)
 | |
|         return {};
 | |
| 
 | |
|     auto const& history_entry = m_entries[index.row()];
 | |
| 
 | |
|     switch (index.column()) {
 | |
|     case Column::Title:
 | |
|         return history_entry.title;
 | |
|     case Column::URL:
 | |
|         return history_entry.url.serialize();
 | |
|     }
 | |
| 
 | |
|     VERIFY_NOT_REACHED();
 | |
| }
 | |
| 
 | |
| 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 };
 | |
| 
 | |
|     auto const& history_entry = m_entries[index.row()];
 | |
|     auto haystack = ByteString::formatted("{} {}", history_entry.title, history_entry.url.serialize());
 | |
|     auto match_result = fuzzy_match(needle, haystack);
 | |
|     if (match_result.score > 0)
 | |
|         return { TriState::True, match_result.score };
 | |
|     return { TriState::False };
 | |
| }
 | |
| 
 | |
| }
 |