Implement sheet listings with relm4 factory

This commit is contained in:
2024-02-02 21:11:21 +01:00
parent 6a6e1d03df
commit 9069138255
7 changed files with 126 additions and 117 deletions

View File

@@ -1,83 +1,18 @@
use std::path::PathBuf;
use gtk::prelude::*;
use relm4::factory::FactoryVecDeque;
use relm4::prelude::*;
use relm4::{
gtk, Component, ComponentController, ComponentParts, ComponentSender, SimpleComponent,
};
use walkdir::WalkDir;
pub struct SheetModel {
path: PathBuf,
visible: bool,
}
#[derive(Debug)]
pub enum SheetModelInput {
OnClicked,
SearchChanged(String),
}
#[derive(Debug)]
pub enum SheetPressedMessage {
SheetPressed(PathBuf),
}
#[relm4::component(pub)]
impl SimpleComponent for SheetModel {
type Init = PathBuf;
type Input = SheetModelInput;
type Output = SheetPressedMessage;
view! {
#[root]
gtk::Box {
#[watch]
set_visible: model.visible,
set_orientation: gtk::Orientation::Vertical,
append = &gtk::Button {
set_label: &format!("{}", model.path.file_name().unwrap().to_string_lossy()),
set_halign: gtk::Align::Start,
set_margin_all: 10,
connect_clicked[sender] => move |_| sender.input(SheetModelInput::OnClicked),
}
}
}
fn init(
path: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = SheetModel {
path,
visible: true,
};
let widgets = view_output!();
ComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
match message {
SheetModelInput::OnClicked => sender
.output(SheetPressedMessage::SheetPressed(self.path.clone()))
.unwrap(),
SheetModelInput::SearchChanged(query) => {
self.visible = self
.path
.file_name()
.unwrap()
.to_string_lossy()
.to_lowercase()
.contains(&query.to_lowercase());
}
}
}
}
use super::sheet_model::{SheetModel, SheetModelType};
pub struct SheetListingModel {
query: String,
sheet_models: Vec<Controller<SheetModel>>,
sheets: FactoryVecDeque<SheetModel>,
}
#[derive(Debug)]
@@ -87,45 +22,35 @@ pub struct SheetListingInput {
#[relm4::component(pub)]
impl SimpleComponent for SheetListingModel {
type Init = PathBuf;
type Init = Vec<SheetModelType>;
type Input = SheetListingInput;
type Output = SheetPressedMessage;
type Output = ();
view! {
#[root]
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
// set_orientation: gtk::Orientation::Vertical,
model.sheets.widget() -> &gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 5,
},
}
}
fn init(
dir: Self::Init,
init: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let mut sheet_models = Vec::new();
for entry in WalkDir::new(dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|file| file.file_type().is_file())
.map(|file| file.into_path())
.filter(|path| {
path.extension()
.map(|s| s.to_string_lossy().to_ascii_lowercase() == "pdf")
.unwrap_or(false)
})
{
let sheet_model = SheetModel::builder()
.launch(entry)
.forward(sender.output_sender(), |m| m);
root.append(sheet_model.widget());
sheet_models.push(sheet_model);
let mut sheets = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
for sheet_model_type in init {
sheets.guard().push_back(sheet_model_type);
}
let model = SheetListingModel {
query: String::new(),
sheet_models,
sheets,
};
let widgets = view_output!();
ComponentParts { model, widgets }
@@ -133,8 +58,5 @@ impl SimpleComponent for SheetListingModel {
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
self.query = message.query;
for model in self.sheet_models.iter() {
model.emit(SheetModelInput::SearchChanged(self.query.clone()));
}
}
}