From 0fe6bf1db2065087a634a34f2b1b6a63a8944641 Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Fri, 2 Feb 2024 22:51:11 +0100 Subject: [PATCH] Make search work again --- src/ui/app.rs | 6 +++++- src/ui/sheet_listing.rs | 36 +++++++++++++++++++++++++----------- src/ui/sheet_model.rs | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/ui/app.rs b/src/ui/app.rs index a137148..d2eb4b6 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -121,7 +121,11 @@ impl SimpleComponent for AppModel { fn update(&mut self, message: Self::Input, _sender: ComponentSender) { match message { AppInput::SheetPressed(sheet) => opener::open(sheet).unwrap(), - AppInput::SearchStarted(query) => {} //self.sheet_listing.emit(SheetListingInput { query }), + AppInput::SearchStarted(query) => { + self.sheets_and_files_listing + .emit(SheetListingInput::Query(query.clone())); + self.new_files_listing.emit(SheetListingInput::Query(query)); + } } } } diff --git a/src/ui/sheet_listing.rs b/src/ui/sheet_listing.rs index b1578c5..6f3eec2 100644 --- a/src/ui/sheet_listing.rs +++ b/src/ui/sheet_listing.rs @@ -1,23 +1,28 @@ use std::path::PathBuf; use gtk::prelude::*; +use log::debug; use relm4::factory::FactoryVecDeque; -use relm4::prelude::*; use relm4::{ gtk, Component, ComponentController, ComponentParts, ComponentSender, SimpleComponent, }; +use relm4::{prelude::*, RelmListBoxExt}; use walkdir::WalkDir; -use super::sheet_model::{SheetModel, SheetModelType}; +use super::sheet_model::{OnQueryUpdate, SheetModel, SheetModelType}; pub struct SheetListingModel { - query: String, sheets: FactoryVecDeque, } #[derive(Debug)] -pub struct SheetListingInput { - pub query: String, +pub enum SheetListingInput { + Query(String), + ListBoxRowClicked(i32), +} + +pub struct SheetModelSelected { + sheet_model_type: SheetModelType, } #[relm4::component(pub)] @@ -31,10 +36,14 @@ impl SimpleComponent for SheetListingModel { gtk::Box { // set_orientation: gtk::Orientation::Vertical, - model.sheets.widget() -> >k::ListBox { + model.sheets.widget() -> &relm4::gtk::ListBox { set_hexpand: true, // set_orientation: gtk::Orientation::Vertical, // set_spacing: 5, + connect_row_activated[sender] => move |list_box, row| { + let index = list_box.index_of_child(row).unwrap(); + sender.input(SheetListingInput::ListBoxRowClicked(index)); + }, }, } } @@ -49,15 +58,20 @@ impl SimpleComponent for SheetListingModel { sheets.guard().push_back(sheet_model_type); } - let model = SheetListingModel { - query: String::new(), - sheets, - }; + let model = SheetListingModel { sheets }; let widgets = view_output!(); ComponentParts { model, widgets } } fn update(&mut self, message: Self::Input, _sender: ComponentSender) { - self.query = message.query; + match message { + SheetListingInput::Query(query) => { + self.sheets.broadcast(OnQueryUpdate { query }); + } + SheetListingInput::ListBoxRowClicked(index) => { + let x = self.sheets.get(index as usize).unwrap(); + debug!("clicked: {}!!!!!", x.label); + } + } } } diff --git a/src/ui/sheet_model.rs b/src/ui/sheet_model.rs index 6100624..5728045 100644 --- a/src/ui/sheet_model.rs +++ b/src/ui/sheet_model.rs @@ -13,7 +13,16 @@ pub enum SheetModelType { } pub struct SheetModel { - label: String, + pub label: String, + visible: bool, +} + +#[derive(Debug)] +pub struct RowActivated; + +#[derive(Debug, Clone)] +pub struct OnQueryUpdate { + pub query: String, } #[relm4::factory(pub)] @@ -22,12 +31,14 @@ impl FactoryComponent for SheetModel { type ParentWidget = gtk::ListBox; type CommandOutput = (); type ParentInput = SheetListingInput; - type Input = (); - type Output = (); + type Input = OnQueryUpdate; + type Output = RowActivated; view! { #[root] gtk::ListBoxRow { + #[watch] + set_visible: self.visible, gtk::Box { set_orientation: gtk::Orientation::Vertical, append = >k::Label { @@ -41,10 +52,25 @@ impl FactoryComponent for SheetModel { fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender) -> Self { let label = match value { - SheetModelType::Sheet { sheet } => sheet.name, - SheetModelType::Pdf { path } => path.file_name().unwrap().to_str().unwrap().to_string(), + SheetModelType::Sheet { sheet } => format!("{}", sheet.name), + SheetModelType::Pdf { path } => { + format!("{}", path.file_name().unwrap().to_str().unwrap()) + } }; - SheetModel { label } + SheetModel { + label, + visible: true, + } + } + + fn update(&mut self, msg: Self::Input, _sender: FactorySender) { + let query = msg.query.to_ascii_lowercase(); + let split = query.split_ascii_whitespace(); + let label = self.label.to_ascii_lowercase(); + + self.visible = split + .into_iter() + .all(|query_part| label.contains(query_part)); } }