Add custom search bar with tags

This commit is contained in:
Elias Projahn 2023-09-30 00:22:33 +02:00
parent 00bd7027a6
commit 08be3cb613
9 changed files with 283 additions and 16 deletions

View file

@ -1,10 +1,9 @@
use crate::player::MusicusPlayer;
use crate::{player::MusicusPlayer, tile::MusicusTile, search_entry::MusicusSearchEntry};
use adw::subclass::{navigation_page::NavigationPageImpl, prelude::*};
use gtk::{glib, glib::Properties, prelude::*};
use std::cell::RefCell;
mod imp {
use crate::tile::MusicusTile;
use super::*;
@ -16,7 +15,7 @@ mod imp {
pub player: RefCell<MusicusPlayer>,
#[template_child]
pub search_entry: TemplateChild<gtk::SearchEntry>,
pub search_entry: TemplateChild<MusicusSearchEntry>,
#[template_child]
pub persons_flow_box: TemplateChild<gtk::FlowBox>,
#[template_child]
@ -47,8 +46,8 @@ mod imp {
impl ObjectImpl for MusicusHomePage {
fn constructed(&self) {
self.parent_constructed();
self.search_entry
.set_key_capture_widget(Some(self.obj().as_ref()));
self.search_entry.set_key_capture_widget(&*self.obj());
self.player
.borrow()
@ -87,7 +86,7 @@ impl MusicusHomePage {
}
#[template_callback]
fn search(&self, entry: &gtk::SearchEntry) {
log::info!("Search changed: \"{}\"", entry.text());
fn select(&self, search_entry: &MusicusSearchEntry) {
search_entry.add_tag("Tag");
}
}

View file

@ -3,6 +3,8 @@ mod config;
mod home_page;
mod player;
mod playlist_page;
mod search_entry;
mod search_tag;
mod tile;
mod welcome_page;
mod window;

150
src/search_entry.rs Normal file
View file

@ -0,0 +1,150 @@
use crate::search_tag::MusicusSearchTag;
use adw::{gdk, glib, glib::clone, glib::subclass::Signal, prelude::*, subclass::prelude::*};
use once_cell::sync::Lazy;
use std::cell::RefCell;
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>>,
}
#[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();
true
}
None => false,
}))
.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] {
static SIGNALS: Lazy<Vec<Signal>> =
Lazy::new(|| vec![Signal::builder("activate").build()]);
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()
}
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) {
let mut tags = self.imp().tags.borrow_mut();
while let Some(tag) = tags.pop() {
self.imp().tags_box.remove(&tag);
}
self.imp().text.set_text("");
}
pub fn add_tag(&self, name: &str) {
self.imp().text.set_text("");
let tag = MusicusSearchTag::new(name);
self.imp().tags_box.append(&tag);
self.imp().tags.borrow_mut().push(tag);
}
#[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 {
if let Some(tag) = self.imp().tags.borrow_mut().pop() {
self.imp().tags_box.remove(&tag);
}
}
}
}

60
src/search_tag.rs Normal file
View file

@ -0,0 +1,60 @@
use adw::{glib, glib::subclass::Signal, prelude::*, subclass::prelude::*};
use once_cell::sync::Lazy;
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "data/ui/search_tag.blp")]
pub struct MusicusSearchTag {
#[template_child]
pub label: TemplateChild<gtk::Label>,
}
#[glib::object_subclass]
impl ObjectSubclass for MusicusSearchTag {
const NAME: &'static str = "MusicusSearchTag";
type Type = super::MusicusSearchTag;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.bind_template_instance_callbacks();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for MusicusSearchTag {
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> =
Lazy::new(|| vec![Signal::builder("remove").build()]);
SIGNALS.as_ref()
}
}
impl WidgetImpl for MusicusSearchTag {}
impl BoxImpl for MusicusSearchTag {}
}
glib::wrapper! {
pub struct MusicusSearchTag(ObjectSubclass<imp::MusicusSearchTag>)
@extends gtk::Widget;
}
#[gtk::template_callbacks]
impl MusicusSearchTag {
pub fn new(label: &str) -> Self {
let tag: MusicusSearchTag = glib::Object::new();
tag.imp().label.set_label(label);
tag
}
#[template_callback]
fn remove(&self, _: &gtk::Button) {
self.emit_by_name::<()>("remove", &[]);
}
}