From d5c280476902d72627c863177a6adcbef4f94f3e Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 2 Jan 2025 15:58:12 +0100 Subject: Search: Build preview string based on chars not bytes --- main/src/ui/global_search.vala | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main/src/ui/global_search.vala b/main/src/ui/global_search.vala index a63aaed1..51e04af2 100644 --- a/main/src/ui/global_search.vala +++ b/main/src/ui/global_search.vala @@ -224,14 +224,15 @@ public class GlobalSearch { grid.margin_bottom = 3; string text = Util.unbreak_space_around_non_spacing_mark(item.message.body.replace("\n", "").replace("\r", "")); - if (text.length > 200) { + if (text.char_count() > 200) { int index = text.index_of(search); - if (index + search.length <= 100) { - text = text.substring(0, 150) + " … " + text.substring(text.length - 50, 50); - } else if (index >= text.length - 100) { - text = text.substring(0, 50) + " … " + text.substring(text.length - 150, 150); + int char_index = index < 0 ? 0 : text.char_count(index); + if (char_index + search.char_count() <= 100) { + text = text.substring(0, text.index_of_nth_char(150)) + " … " + text.substring(text.index_of_nth_char(text.char_count() - 50)); + } else if (char_index >= text.char_count() - 100) { + text = text.substring(0, text.index_of_nth_char(50)) + " … " + text.substring(text.index_of_nth_char(text.char_count() - 150)); } else { - text = text.substring(0, 25) + " … " + text.substring(index - 50, 50) + text.substring(index, 100) + " … " + text.substring(text.length - 25, 25); + text = text.substring(0, text.index_of_nth_char(25)) + " … " + text.substring(text.index_of_nth_char(char_index - 50), text.index_of_nth_char(char_index + 100)) + " … " + text.substring(text.index_of_nth_char(text.char_count() - 25)); } } Label label = new Label("") { use_markup=true, xalign=0, selectable=true, wrap=true, wrap_mode=Pango.WrapMode.WORD_CHAR, vexpand=true }; -- cgit v1.2.3-70-g09d2