From 54f0088e615acfccc6c94604afb10fdd4a57559b Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sat, 30 Sep 2023 19:10:32 +0200 Subject: [PATCH] Add query signal to search entry --- data/ui/search_entry.blp | 2 ++ src/home_page.rs | 4 ++++ src/search_entry.rs | 46 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/data/ui/search_entry.blp b/data/ui/search_entry.blp index 1da87f8..e4e34ae 100644 --- a/data/ui/search_entry.blp +++ b/data/ui/search_entry.blp @@ -22,9 +22,11 @@ template $MusicusSearchEntry : Gtk.Box { hexpand: true; activate => $activate() swapped; backspace => $backspace() swapped; + changed => $text_changed() swapped; } Gtk.Image clear_icon { + visible: false; icon-name: "edit-clear-symbolic"; tooltip-text: _("Clear entry"); } diff --git a/src/home_page.rs b/src/home_page.rs index 57bf114..4d30ef6 100644 --- a/src/home_page.rs +++ b/src/home_page.rs @@ -53,6 +53,10 @@ mod imp { self.search_entry.set_key_capture_widget(&*self.obj()); + self.search_entry.connect_query_changed(|entry| { + log::info!("{}", entry.query()); + }); + self.player .borrow() .bind_property("active", &self.play_button.get(), "visible") diff --git a/src/search_entry.rs b/src/search_entry.rs index 88cab90..60f1b9e 100644 --- a/src/search_entry.rs +++ b/src/search_entry.rs @@ -1,7 +1,7 @@ use crate::search_tag::MusicusSearchTag; -use adw::{gdk, glib, glib::clone, glib::subclass::Signal, prelude::*, subclass::prelude::*}; +use adw::{gdk, gio, glib, glib::clone, glib::subclass::Signal, prelude::*, subclass::prelude::*}; use once_cell::sync::Lazy; -use std::cell::RefCell; +use std::{cell::RefCell, time::Duration}; mod imp { use super::*; @@ -17,6 +17,7 @@ mod imp { pub clear_icon: TemplateChild, pub tags: RefCell>, + pub query_changed: RefCell>, } #[glib::object_subclass] @@ -70,8 +71,12 @@ mod imp { } fn signals() -> &'static [Signal] { - static SIGNALS: Lazy> = - Lazy::new(|| vec![Signal::builder("activate").build()]); + static SIGNALS: Lazy> = Lazy::new(|| { + vec![ + Signal::builder("activate").build(), + Signal::builder("query-changed").build(), + ] + }); SIGNALS.as_ref() } @@ -97,6 +102,14 @@ impl MusicusSearchEntry { glib::Object::new() } + pub fn connect_query_changed(&self, f: F) -> glib::SignalHandlerId { + self.connect_local("query-changed", true, move |values| { + let obj = values[0].get::().unwrap(); + f(&obj); + None + }) + } + pub fn set_key_capture_widget(&self, widget: &impl IsA) { let controller = gtk::EventControllerKey::new(); @@ -134,6 +147,10 @@ impl MusicusSearchEntry { self.imp().tags.borrow_mut().push(tag); } + pub fn query(&self) -> String { + self.imp().text.text().to_string() + } + #[template_callback] fn activate(&self, _: >k::Text) { self.emit_by_name::<()>("activate", &[]); @@ -147,4 +164,25 @@ impl MusicusSearchEntry { } } } + + #[template_callback] + async fn text_changed(&self, text: >k::Text) { + self.imp().clear_icon.set_visible(!text.text().is_empty()); + + if let Some(cancellable) = self.imp().query_changed.borrow_mut().take() { + cancellable.cancel(); + } + + let cancellable = gio::Cancellable::new(); + self.imp().query_changed.replace(Some(cancellable.clone())); + + let _ = gio::CancellableFuture::new( + async { + glib::timeout_future(Duration::from_millis(150)).await; + self.emit_by_name::<()>("query-changed", &[]); + }, + cancellable, + ) + .await; + } }