mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:37:34 +00:00
LibGUI: Refactor AbstractView::do_search() into two standalone steps
This change splits the do_search() into find_next_search_match() and highlight_search() to allow the given index be independently highlighted when needed.
This commit is contained in:
parent
cc93736f21
commit
d0e44993a1
2 changed files with 32 additions and 19 deletions
|
@ -577,8 +577,12 @@ void AbstractView::keydown_event(KeyEvent& event)
|
||||||
n_code_points--;
|
n_code_points--;
|
||||||
sb.append_code_point(*it);
|
sb.append_code_point(*it);
|
||||||
}
|
}
|
||||||
do_search(sb.to_string());
|
auto index = find_next_search_match(sb.string_view());
|
||||||
start_highlighted_search_timer();
|
if (index.is_valid()) {
|
||||||
|
m_highlighted_search = sb.to_string();
|
||||||
|
highlight_search(index);
|
||||||
|
start_highlighted_search_timer();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stop_highlighted_search_timer();
|
stop_highlighted_search_timer();
|
||||||
}
|
}
|
||||||
|
@ -597,8 +601,13 @@ void AbstractView::keydown_event(KeyEvent& event)
|
||||||
StringBuilder sb;
|
StringBuilder sb;
|
||||||
sb.append(m_highlighted_search);
|
sb.append(m_highlighted_search);
|
||||||
sb.append_code_point(event.code_point());
|
sb.append_code_point(event.code_point());
|
||||||
do_search(sb.to_string());
|
|
||||||
start_highlighted_search_timer();
|
auto index = find_next_search_match(sb.string_view());
|
||||||
|
if (index.is_valid()) {
|
||||||
|
m_highlighted_search = sb.to_string();
|
||||||
|
highlight_search(index);
|
||||||
|
start_highlighted_search_timer();
|
||||||
|
}
|
||||||
|
|
||||||
event.accept();
|
event.accept();
|
||||||
return;
|
return;
|
||||||
|
@ -632,22 +641,25 @@ void AbstractView::start_highlighted_search_timer()
|
||||||
m_highlighted_search_timer->restart();
|
m_highlighted_search_timer->restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractView::do_search(String&& searching)
|
ModelIndex AbstractView::find_next_search_match(StringView const search)
|
||||||
{
|
{
|
||||||
if (searching.is_empty() || !model()) {
|
if (search.is_empty())
|
||||||
stop_highlighted_search_timer();
|
return {};
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto found_indices = model()->matches(searching, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
|
auto found_indices = model()->matches(search, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
|
||||||
if (!found_indices.is_empty() && found_indices[0].is_valid()) {
|
|
||||||
auto& index = found_indices[0];
|
if (found_indices.is_empty())
|
||||||
m_highlighted_search_index = index;
|
return {};
|
||||||
m_highlighted_search = move(searching);
|
|
||||||
set_selection(index);
|
return found_indices[0];
|
||||||
scroll_into_view(index);
|
}
|
||||||
update();
|
|
||||||
}
|
void AbstractView::highlight_search(ModelIndex const index)
|
||||||
|
{
|
||||||
|
m_highlighted_search_index = index;
|
||||||
|
set_selection(index);
|
||||||
|
scroll_into_view(index);
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AbstractView::is_searchable() const
|
bool AbstractView::is_searchable() const
|
||||||
|
|
|
@ -164,7 +164,8 @@ protected:
|
||||||
|
|
||||||
void stop_highlighted_search_timer();
|
void stop_highlighted_search_timer();
|
||||||
void start_highlighted_search_timer();
|
void start_highlighted_search_timer();
|
||||||
void do_search(String&&);
|
ModelIndex find_next_search_match(StringView const);
|
||||||
|
void highlight_search(ModelIndex const index);
|
||||||
|
|
||||||
ModelIndex drop_candidate_index() const { return m_drop_candidate_index; }
|
ModelIndex drop_candidate_index() const { return m_drop_candidate_index; }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue