Skip to content

Commit d0e4499

Browse files
d1823alimpfard
authored andcommitted
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.
1 parent cc93736 commit d0e4499

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

Userland/Libraries/LibGUI/AbstractView.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,12 @@ void AbstractView::keydown_event(KeyEvent& event)
577577
n_code_points--;
578578
sb.append_code_point(*it);
579579
}
580-
do_search(sb.to_string());
581-
start_highlighted_search_timer();
580+
auto index = find_next_search_match(sb.string_view());
581+
if (index.is_valid()) {
582+
m_highlighted_search = sb.to_string();
583+
highlight_search(index);
584+
start_highlighted_search_timer();
585+
}
582586
} else {
583587
stop_highlighted_search_timer();
584588
}
@@ -597,8 +601,13 @@ void AbstractView::keydown_event(KeyEvent& event)
597601
StringBuilder sb;
598602
sb.append(m_highlighted_search);
599603
sb.append_code_point(event.code_point());
600-
do_search(sb.to_string());
601-
start_highlighted_search_timer();
604+
605+
auto index = find_next_search_match(sb.string_view());
606+
if (index.is_valid()) {
607+
m_highlighted_search = sb.to_string();
608+
highlight_search(index);
609+
start_highlighted_search_timer();
610+
}
602611

603612
event.accept();
604613
return;
@@ -632,22 +641,25 @@ void AbstractView::start_highlighted_search_timer()
632641
m_highlighted_search_timer->restart();
633642
}
634643

635-
void AbstractView::do_search(String&& searching)
644+
ModelIndex AbstractView::find_next_search_match(StringView const search)
636645
{
637-
if (searching.is_empty() || !model()) {
638-
stop_highlighted_search_timer();
639-
return;
640-
}
646+
if (search.is_empty())
647+
return {};
641648

642-
auto found_indices = model()->matches(searching, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
643-
if (!found_indices.is_empty() && found_indices[0].is_valid()) {
644-
auto& index = found_indices[0];
645-
m_highlighted_search_index = index;
646-
m_highlighted_search = move(searching);
647-
set_selection(index);
648-
scroll_into_view(index);
649-
update();
650-
}
649+
auto found_indices = model()->matches(search, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
650+
651+
if (found_indices.is_empty())
652+
return {};
653+
654+
return found_indices[0];
655+
}
656+
657+
void AbstractView::highlight_search(ModelIndex const index)
658+
{
659+
m_highlighted_search_index = index;
660+
set_selection(index);
661+
scroll_into_view(index);
662+
update();
651663
}
652664

653665
bool AbstractView::is_searchable() const

Userland/Libraries/LibGUI/AbstractView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ class AbstractView
164164

165165
void stop_highlighted_search_timer();
166166
void start_highlighted_search_timer();
167-
void do_search(String&&);
167+
ModelIndex find_next_search_match(StringView const);
168+
void highlight_search(ModelIndex const index);
168169

169170
ModelIndex drop_candidate_index() const { return m_drop_candidate_index; }
170171

0 commit comments

Comments
 (0)