use shared highlight function

This commit is contained in:
yggverse 2025-03-11 12:40:00 +02:00
parent 48b70a263f
commit d26917c78d

View File

@ -10,7 +10,7 @@ use gtk::{
prelude::{Cast, CastNone}, prelude::{Cast, CastNone},
ListStore, ListStore,
}, },
glib::SignalHandlerId, glib::{GString, SignalHandlerId},
prelude::{EditableExt, EntryExt, ListItemExt, WidgetExt}, prelude::{EditableExt, EntryExt, ListItemExt, WidgetExt},
Entry, ListItem, ListView, Popover, SignalListItemFactory, SingleSelection, Entry, ListItem, ListView, Popover, SignalListItemFactory, SingleSelection,
INVALID_LIST_POSITION, INVALID_LIST_POSITION,
@ -149,7 +149,7 @@ impl Suggestion {
// Actions // Actions
pub fn update(&self, limit: Option<usize>) { pub fn update(&self, limit: Option<usize>) {
use gtk::{glib::GString, prelude::EditableExt}; use gtk::prelude::EditableExt;
use itertools::Itertools; use itertools::Itertools;
if self.request.text_length() > 0 { if self.request.text_length() > 0 {
self.list_store.remove_all(); self.list_store.remove_all();
@ -160,14 +160,11 @@ impl Suggestion {
.into_iter() .into_iter()
.sorted_by(|a, b| Ord::cmp(&b.opened.len(), &a.opened.len())) .sorted_by(|a, b| Ord::cmp(&b.opened.len(), &a.opened.len()))
{ {
let subtitle = let subtitle = highlight(&item.request, &query);
GString::from(item.request.replace(&*query, &format!("<b>{query}</b>")));
let title = match item.title { let title = match item.title {
Some(title) => title.replace(&*query, &format!("<b>{query}</b>")).into(), Some(title) => highlight(&title, &query),
None => subtitle.clone(), None => subtitle.clone(),
}; };
self.list_store.append(&Item::build( self.list_store.append(&Item::build(
title, title,
subtitle, subtitle,
@ -221,3 +218,9 @@ impl Suggestion {
true true
} }
} }
// Tools
fn highlight(subject: &str, key: &str) -> GString {
subject.replace(key, &format!("<b>{key}</b>")).into()
}