musicus/src/search_entry.rs

250 lines
7.2 KiB
Rust
Raw Normal View History

2023-10-08 00:16:41 +02:00
use crate::{
library::LibraryQuery,
search_tag::{MusicusSearchTag, Tag},
};
2024-04-01 18:47:44 +02:00
use adw::{prelude::*, subclass::prelude::*};
use gtk::{
gdk, gio,
glib::{self, clone, subclass::Signal, Propagation},
};
2023-09-30 00:22:33 +02:00
use once_cell::sync::Lazy;
2023-09-30 19:10:32 +02:00
use std::{cell::RefCell, time::Duration};
2023-09-30 00:22:33 +02:00
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "data/ui/search_entry.blp")]
pub struct MusicusSearchEntry {
#[template_child]
pub tags_box: TemplateChild<gtk::Box>,
#[template_child]
pub text: TemplateChild<gtk::Text>,
#[template_child]
pub clear_icon: TemplateChild<gtk::Image>,
pub tags: RefCell<Vec<MusicusSearchTag>>,
2023-09-30 19:10:32 +02:00
pub query_changed: RefCell<Option<gio::Cancellable>>,
2023-09-30 00:22:33 +02:00
}
#[glib::object_subclass]
impl ObjectSubclass for MusicusSearchEntry {
const NAME: &'static str = "MusicusSearchEntry";
type Type = super::MusicusSearchEntry;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.bind_template_instance_callbacks();
klass.set_css_name("entry");
klass.add_shortcut(
&gtk::Shortcut::builder()
.trigger(&gtk::KeyvalTrigger::new(
gdk::Key::Escape,
gdk::ModifierType::empty(),
))
.action(&gtk::CallbackAction::new(|widget, _| match widget
.downcast_ref::<super::MusicusSearchEntry>(
) {
Some(obj) => {
obj.reset();
2024-04-01 18:47:44 +02:00
Propagation::Stop
2023-09-30 00:22:33 +02:00
}
2024-04-01 18:47:44 +02:00
None => Propagation::Proceed,
2023-09-30 00:22:33 +02:00
}))
.build(),
);
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for MusicusSearchEntry {
fn constructed(&self) {
let controller = gtk::GestureClick::new();
controller.connect_pressed(|gesture, _, _, _| {
gesture.set_state(gtk::EventSequenceState::Claimed);
});
controller.connect_released(clone!(@weak self as _self => move |_, _, _, _| {
_self.obj().reset();
}));
self.clear_icon.add_controller(controller);
}
fn signals() -> &'static [Signal] {
2023-09-30 19:10:32 +02:00
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![
Signal::builder("activate").build(),
Signal::builder("query-changed").build(),
]
});
2023-09-30 00:22:33 +02:00
SIGNALS.as_ref()
}
}
impl WidgetImpl for MusicusSearchEntry {
fn grab_focus(&self) -> bool {
self.text.grab_focus_without_selecting()
}
}
impl BoxImpl for MusicusSearchEntry {}
}
glib::wrapper! {
pub struct MusicusSearchEntry(ObjectSubclass<imp::MusicusSearchEntry>)
@extends gtk::Widget;
}
#[gtk::template_callbacks]
impl MusicusSearchEntry {
pub fn new() -> Self {
glib::Object::new()
}
2023-09-30 19:10:32 +02:00
pub fn connect_query_changed<F: Fn(&Self) + 'static>(&self, f: F) -> glib::SignalHandlerId {
self.connect_local("query-changed", true, move |values| {
let obj = values[0].get::<Self>().unwrap();
f(&obj);
None
})
}
2023-09-30 00:22:33 +02:00
pub fn set_key_capture_widget(&self, widget: &impl IsA<gtk::Widget>) {
let controller = gtk::EventControllerKey::new();
controller.connect_key_pressed(clone!(@weak self as _self => @default-return glib::Propagation::Proceed, move |controller, _, _, _| {
match controller.forward(&_self.imp().text.get()) {
true => {
_self.grab_focus();
glib::Propagation::Stop
},
false => glib::Propagation::Proceed,
}
}));
controller.connect_key_released(clone!(@weak self as _self => move |controller, _, _, _| {
controller.forward(&_self.imp().text.get());
}));
widget.add_controller(controller);
}
pub fn reset(&self) {
2023-10-08 16:40:59 +02:00
{
let mut tags = self.imp().tags.borrow_mut();
while let Some(tag) = tags.pop() {
self.imp().tags_box.remove(&tag);
}
2023-09-30 00:22:33 +02:00
}
self.imp().text.set_text("");
2023-10-08 00:30:30 +02:00
self.emit_by_name::<()>("query-changed", &[]);
2023-09-30 00:22:33 +02:00
}
2023-10-08 00:16:41 +02:00
pub fn add_tag(&self, tag: Tag) {
2023-11-03 19:09:26 +01:00
let imp = self.imp();
imp.clear_icon.set_visible(true);
imp.text.set_text("");
2024-04-01 18:47:44 +02:00
2023-10-08 00:16:41 +02:00
let tag = MusicusSearchTag::new(tag);
2023-10-08 15:25:00 +02:00
tag.connect_remove(clone!(@weak self as self_ => move |tag| {
let imp = self_.imp();
imp.tags_box.remove(tag);
{
imp.tags.borrow_mut().retain(|t| t.tag() != tag.tag());
}
self_.emit_by_name::<()>("query-changed", &[]);
}));
2023-11-03 19:09:26 +01:00
imp.tags_box.append(&tag);
imp.tags.borrow_mut().push(tag);
2023-10-08 00:30:30 +02:00
self.emit_by_name::<()>("query-changed", &[]);
2023-09-30 00:22:33 +02:00
}
pub fn tags(&self) -> Vec<Tag> {
self.imp()
.tags
.borrow()
.iter()
.map(|t| t.tag().to_owned())
.collect()
}
2023-10-07 22:49:20 +02:00
pub fn query(&self) -> LibraryQuery {
2023-10-08 00:16:41 +02:00
let mut query = LibraryQuery {
2023-10-07 22:49:20 +02:00
search: self.imp().text.text().to_string(),
..Default::default()
2023-10-08 00:16:41 +02:00
};
for tag in &*self.imp().tags.borrow() {
match tag.tag().clone() {
2023-10-08 15:11:47 +02:00
Tag::Composer(person) => query.composer = Some(person),
Tag::Performer(person) => query.performer = Some(person),
2023-10-08 00:16:41 +02:00
Tag::Ensemble(ensemble) => query.ensemble = Some(ensemble),
Tag::Work(work) => query.work = Some(work),
}
2023-10-07 22:49:20 +02:00
}
2023-10-08 00:16:41 +02:00
query
2023-09-30 19:10:32 +02:00
}
2023-09-30 00:22:33 +02:00
#[template_callback]
fn activate(&self, _: &gtk::Text) {
self.emit_by_name::<()>("activate", &[]);
}
#[template_callback]
fn backspace(&self, text: &gtk::Text) {
if text.cursor_position() == 0 {
2023-10-08 00:16:41 +02:00
let changed = if let Some(tag) = self.imp().tags.borrow_mut().pop() {
2023-09-30 00:22:33 +02:00
self.imp().tags_box.remove(&tag);
2023-10-08 00:16:41 +02:00
true
} else {
false
};
if changed {
self.emit_by_name::<()>("query-changed", &[]);
2023-09-30 00:22:33 +02:00
}
}
}
2023-09-30 19:10:32 +02:00
#[template_callback]
async fn text_changed(&self, text: &gtk::Text) {
2023-11-03 19:09:26 +01:00
let imp = self.imp();
if imp.tags.borrow().is_empty() {
imp.clear_icon.set_visible(!text.text().is_empty());
}
2023-09-30 19:10:32 +02:00
2023-11-03 19:09:26 +01:00
if let Some(cancellable) = imp.query_changed.borrow_mut().take() {
2023-09-30 19:10:32 +02:00
cancellable.cancel();
}
let cancellable = gio::Cancellable::new();
2023-11-03 19:09:26 +01:00
imp.query_changed.replace(Some(cancellable.clone()));
2023-09-30 19:10:32 +02:00
let _ = gio::CancellableFuture::new(
async {
glib::timeout_future(Duration::from_millis(150)).await;
self.emit_by_name::<()>("query-changed", &[]);
},
cancellable,
)
.await;
}
2023-09-30 00:22:33 +02:00
}