Make search work again

This commit is contained in:
Julian Mutter 2024-02-02 22:51:11 +01:00
parent 5ee898c9c5
commit 0fe6bf1db2
3 changed files with 62 additions and 18 deletions

View File

@ -121,7 +121,11 @@ impl SimpleComponent for AppModel {
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
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));
}
}
}
}

View File

@ -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<SheetModel>,
}
#[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() -> &gtk::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>) {
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);
}
}
}
}

View File

@ -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 = &gtk::Label {
@ -41,10 +52,25 @@ impl FactoryComponent for SheetModel {
fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> 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<Self>) {
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));
}
}