Re-implement file opening

This commit is contained in:
Julian Mutter 2024-02-02 23:02:07 +01:00
parent 0fe6bf1db2
commit 60649fe633
4 changed files with 45 additions and 16 deletions

View File

@ -1,6 +1,6 @@
use std::{fs, path::Path}; use std::{fs, path::Path};
#[derive(sqlx::FromRow, Debug)] #[derive(sqlx::FromRow, Debug, Clone)]
pub struct Sheet { pub struct Sheet {
pub id: i32, pub id: i32,
pub name: String, pub name: String,

View File

@ -15,14 +15,15 @@ use super::{
pub struct AppModel { pub struct AppModel {
mcdu: Controller<McduModel>, mcdu: Controller<McduModel>,
sheets_and_files_listing: Connector<SheetListingModel>, sheets_and_files_listing: Controller<SheetListingModel>,
new_files_listing: Connector<SheetListingModel>, new_files_listing: Controller<SheetListingModel>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum AppInput { pub enum AppInput {
SearchStarted(String), SearchStarted(String),
SheetPressed(PathBuf), NewFilesSheetPressed(SheetModelType),
SheetsAndFilesSheetPressed(SheetModelType),
} }
#[relm4::component(pub)] #[relm4::component(pub)]
@ -100,12 +101,16 @@ impl SimpleComponent for AppModel {
.chain(new_files_clone_iter) .chain(new_files_clone_iter)
.collect(); .collect();
let sheets_and_files_listing = SheetListingModel::builder().launch(sheets_and_files); let sheets_and_files_listing = SheetListingModel::builder()
let new_files_listing = SheetListingModel::builder().launch(new_files); .launch(sheets_and_files)
// .forward(sender.input_sender(), |_| {}); .forward(sender.input_sender(), |response| {
// .forward(sender.input_sender(), |response| match response { AppInput::SheetsAndFilesSheetPressed(response.sheet_model_type)
// SheetPressedMessage::SheetPressed(path) => AppInput::SheetPressed(path), });
// }); let new_files_listing = SheetListingModel::builder()
.launch(new_files)
.forward(sender.input_sender(), |response| {
AppInput::NewFilesSheetPressed(response.sheet_model_type)
});
let model = AppModel { let model = AppModel {
mcdu, mcdu,
@ -119,13 +124,19 @@ impl SimpleComponent for AppModel {
} }
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) { fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
// AppInput::SheetPressed(sheet) => opener::open(sheet).unwrap(),
match message { match message {
AppInput::SheetPressed(sheet) => opener::open(sheet).unwrap(),
AppInput::SearchStarted(query) => { AppInput::SearchStarted(query) => {
self.sheets_and_files_listing self.sheets_and_files_listing
.emit(SheetListingInput::Query(query.clone())); .emit(SheetListingInput::Query(query.clone()));
self.new_files_listing.emit(SheetListingInput::Query(query)); self.new_files_listing.emit(SheetListingInput::Query(query));
} }
AppInput::NewFilesSheetPressed(_) => {
// TODO
}
AppInput::SheetsAndFilesSheetPressed(sheet_model_type) => {
opener::open(sheet_model_type.get_path()).unwrap()
}
} }
} }
} }

View File

@ -21,15 +21,16 @@ pub enum SheetListingInput {
ListBoxRowClicked(i32), ListBoxRowClicked(i32),
} }
#[derive(Debug)]
pub struct SheetModelSelected { pub struct SheetModelSelected {
sheet_model_type: SheetModelType, pub sheet_model_type: SheetModelType,
} }
#[relm4::component(pub)] #[relm4::component(pub)]
impl SimpleComponent for SheetListingModel { impl SimpleComponent for SheetListingModel {
type Init = Vec<SheetModelType>; type Init = Vec<SheetModelType>;
type Input = SheetListingInput; type Input = SheetListingInput;
type Output = (); type Output = SheetModelSelected;
view! { view! {
#[root] #[root]
@ -63,7 +64,7 @@ impl SimpleComponent for SheetListingModel {
ComponentParts { model, widgets } ComponentParts { model, widgets }
} }
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) { fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
match message { match message {
SheetListingInput::Query(query) => { SheetListingInput::Query(query) => {
self.sheets.broadcast(OnQueryUpdate { query }); self.sheets.broadcast(OnQueryUpdate { query });
@ -71,6 +72,11 @@ impl SimpleComponent for SheetListingModel {
SheetListingInput::ListBoxRowClicked(index) => { SheetListingInput::ListBoxRowClicked(index) => {
let x = self.sheets.get(index as usize).unwrap(); let x = self.sheets.get(index as usize).unwrap();
debug!("clicked: {}!!!!!", x.label); debug!("clicked: {}!!!!!", x.label);
sender
.output(SheetModelSelected {
sheet_model_type: x.sheet_model_type.clone(),
})
.unwrap();
} }
} }
} }

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::{Path, PathBuf};
use gtk::prelude::*; use gtk::prelude::*;
use relm4::prelude::*; use relm4::prelude::*;
@ -7,13 +7,24 @@ use crate::sheet::Sheet;
use super::sheet_listing::SheetListingInput; use super::sheet_listing::SheetListingInput;
#[derive(Debug, Clone)]
pub enum SheetModelType { pub enum SheetModelType {
Sheet { sheet: Sheet }, Sheet { sheet: Sheet },
Pdf { path: PathBuf }, Pdf { path: PathBuf },
} }
impl SheetModelType {
pub fn get_path(&self) -> &str {
match self {
SheetModelType::Sheet { sheet } => &sheet.path,
SheetModelType::Pdf { path } => path.to_str().unwrap(),
}
}
}
pub struct SheetModel { pub struct SheetModel {
pub label: String, pub label: String,
pub sheet_model_type: SheetModelType,
visible: bool, visible: bool,
} }
@ -51,7 +62,7 @@ impl FactoryComponent for SheetModel {
} }
fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self { fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self {
let label = match value { let label = match &value {
SheetModelType::Sheet { sheet } => format!("{}", sheet.name), SheetModelType::Sheet { sheet } => format!("{}", sheet.name),
SheetModelType::Pdf { path } => { SheetModelType::Pdf { path } => {
format!("{}", path.file_name().unwrap().to_str().unwrap()) format!("{}", path.file_name().unwrap().to_str().unwrap())
@ -60,6 +71,7 @@ impl FactoryComponent for SheetModel {
SheetModel { SheetModel {
label, label,
sheet_model_type: value,
visible: true, visible: true,
} }
} }