From 8623e3109a2e04d81479d24f5985c629a548ed93 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sun, 20 Apr 2025 13:11:59 +0200 Subject: [PATCH] fix search and select --- src/lib/config.rs | 5 +++-- src/lib/gui.rs | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/lib/config.rs b/src/lib/config.rs index 1a40367..c15d618 100644 --- a/src/lib/config.rs +++ b/src/lib/config.rs @@ -270,7 +270,8 @@ pub struct Config { pub show_animation_time: Option, /// Defines the animation when the window is hidden. - /// Defaults to Expand + /// Defaults to None, because it is a bit buggy with + /// gtk layer shell. works fine with normal window though #[serde(default = "default_hide_animation")] #[clap(long = "hide-animation")] pub hide_animation: Option, @@ -383,7 +384,7 @@ pub fn default_hide_animation_time() -> Option { #[allow(clippy::unnecessary_wraps)] #[must_use] pub fn default_hide_animation() -> Option { - Some(Animation::Expand) + Some(Animation::None) } // allowed because option is needed for serde macro diff --git a/src/lib/gui.rs b/src/lib/gui.rs index 629a5cd..984c635 100644 --- a/src/lib/gui.rs +++ b/src/lib/gui.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::ops::DerefMut; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -172,13 +173,16 @@ fn build_ui( ); } - let items_clone = Arc::>>>::clone(&list_items); - inner_box.set_sort_func(move |child1, child2| sort_menu_items(child1, child2, &items_clone)); + let items_sort = Arc::>>>::clone(&list_items); + inner_box.set_sort_func(move |child1, child2| sort_menu_items(child1, child2, &items_sort)); - // Set focus after everything is realized - inner_box.connect_map(|fb| { + let items_focus = Arc::>>>::clone(&list_items); + inner_box.connect_map(move |fb| { fb.grab_focus(); fb.invalidate_sort(); + + let mut item_lock = items_focus.lock().unwrap(); + select_first_visible_child(item_lock.deref_mut(), fb) }); let wrapper_box = gtk4::Box::new(Orientation::Vertical, 0); @@ -265,6 +269,13 @@ fn sort_menu_items( let m1 = lock.get(child1); let m2 = lock.get(child2); + if !child1.is_visible() { + return Ordering::Smaller; + } + if !child2.is_visible() { + return Ordering::Larger; + } + match (m1, m2) { (Some(menu1), Some(menu2)) => { if menu1.search_sort_score != 0.0 || menu2.search_sort_score != 0.0 { @@ -704,7 +715,6 @@ fn filter_widgets( } let query = query.to_owned().to_lowercase(); - let mut fb: Option<&FlowBoxChild> = None; for (flowbox_child, menu_item) in items.iter_mut() { let menu_item_search = format!( "{} {}", @@ -745,16 +755,23 @@ fn filter_widgets( }; menu_item.search_sort_score = search_sort_score; - if visible { - fb = Some(flowbox_child); - } - flowbox_child.set_visible(visible); } - if let Some(top_item) = fb { - inner_box.select_child(top_item); - top_item.grab_focus(); + select_first_visible_child(items, inner_box); +} + +fn select_first_visible_child( + items: &mut HashMap>, + inner_box: &FlowBox, +) { + for i in 0..items.len() { + if let Some(child) = inner_box.child_at_index(i as i32) { + if child.is_visible() { + inner_box.select_child(&child); + break; + } + } } }