From 19e8fbcc35ba27ff8e4c6989c2632ca3be135e7e Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 30 Sep 2025 05:51:33 -0500 Subject: [PATCH] making presentation_editor only need one page at a time --- src/main.rs | 68 +++++++++++++++++++++++--------- src/ui/presentation_editor.rs | 74 +++++++++++++++++++++++------------ 2 files changed, 100 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4fa4b1f..8cdb977 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1182,10 +1182,43 @@ impl cosmic::Application for App { Message::AddServiceItem(index, item) => { let item_index = item.0 .1; let kind = item.0 .0; + let mut item; match kind { - core::model::LibraryKind::Song => todo!(), - core::model::LibraryKind::Video => todo!(), - core::model::LibraryKind::Image => todo!(), + core::model::LibraryKind::Song => { + let Some(library) = self.library.as_mut() + else { + return Task::none(); + }; + let Some(song) = library.get_song(item_index) + else { + return Task::none(); + }; + item = song.to_service_item(); + } + core::model::LibraryKind::Video => { + let Some(library) = self.library.as_mut() + else { + return Task::none(); + }; + let Some(video) = + library.get_video(item_index) + else { + return Task::none(); + }; + item = video.to_service_item(); + } + core::model::LibraryKind::Image => { + let Some(library) = self.library.as_mut() + else { + return Task::none(); + }; + let Some(image) = + library.get_image(item_index) + else { + return Task::none(); + }; + item = image.to_service_item(); + } core::model::LibraryKind::Presentation => { let Some(library) = self.library.as_mut() else { @@ -1196,23 +1229,22 @@ impl cosmic::Application for App { else { return Task::none(); }; - let mut item = presentation.to_service_item(); - item.slides = item - .slides - .into_par_iter() - .map(|mut slide| { - let fontdb = Arc::clone(&self.fontdb); - text_svg::text_svg_generator( - &mut slide, fontdb, - ); - slide - }) - .collect(); - self.service.insert(index, item); - self.presenter - .update_items(self.service.clone()); + item = presentation.to_service_item(); } } + item.slides = item + .slides + .into_par_iter() + .map(|mut slide| { + let fontdb = Arc::clone(&self.fontdb); + text_svg::text_svg_generator( + &mut slide, fontdb, + ); + slide + }) + .collect(); + self.service.insert(index, item); + self.presenter.update_items(self.service.clone()); Task::none() } Message::RemoveServiceItem(index) => { diff --git a/src/ui/presentation_editor.rs b/src/ui/presentation_editor.rs index b124665..38cb023 100644 --- a/src/ui/presentation_editor.rs +++ b/src/ui/presentation_editor.rs @@ -10,20 +10,25 @@ use cosmic::{ iced_widget::{column, row}, theme, widget::{ - button, container, horizontal_space, icon, image, text, - text_input, Space, + self, button, container, horizontal_space, icon, + image::{self, Handle}, + text, text_input, Space, }, Element, Task, }; +use miette::IntoDiagnostic; +use mupdf::{Colorspace, Document, Matrix}; use tracing::{debug, error, warn}; #[derive(Debug)] pub struct PresentationEditor { pub presentation: Option, - slides: Option>, + document: Option, + current_slide: Option, + page_count: Option, + current_slide_index: Option, title: String, editing: bool, - current_slide: i16, } pub enum Action { @@ -46,10 +51,12 @@ impl PresentationEditor { pub fn new() -> Self { Self { presentation: None, - slides: None, + document: None, title: "Death was Arrested".to_string(), editing: false, - current_slide: 0, + current_slide: None, + current_slide_index: None, + page_count: None, } } pub fn update(&mut self, message: Message) -> Action { @@ -57,12 +64,40 @@ impl PresentationEditor { Message::ChangePresentation(presentation) => { self.presentation = Some(presentation.clone()); self.title = presentation.title.clone(); + self.document = + Document::open(&presentation.path.as_path()).ok(); + self.page_count = self + .document + .as_ref() + .and_then(|doc| doc.page_count().ok()); warn!("changing presentation"); - let Ok(slides) = presentation.to_slides() else { - return Action::None; - }; - self.slides = Some(slides); - self.current_slide = 0; + self.current_slide = + self.document.as_ref().and_then(|doc| { + let page = doc.load_page(0).ok()?; + let matrix = Matrix::IDENTITY; + let colorspace = Colorspace::device_rgb(); + let Ok(pixmap) = page + .to_pixmap( + &matrix, + &colorspace, + true, + true, + ) + .into_diagnostic() + else { + error!( + "Can't turn this page into pixmap" + ); + return None; + }; + debug!(?pixmap); + Some(Handle::from_rgba( + pixmap.width(), + pixmap.height(), + pixmap.samples().to_vec(), + )) + }); + self.current_slide_index = Some(0); return self.update(Message::Update(presentation)); } Message::ChangeTitle(title) => { @@ -110,19 +145,10 @@ impl PresentationEditor { } pub fn view(&self) -> Element { - let container = if let Some(slides) = &self.slides { - if let Some(slide) = - slides.get(self.current_slide as usize) - { - container( - image(slide.pdf_page().unwrap_or( - image::Handle::from_path("res/chad.png"), - )) - .content_fit(ContentFit::Cover), - ) - } else { - container(Space::new(0, 0)) - } + let container = if let Some(slide) = &self.current_slide { + container( + widget::image(slide).content_fit(ContentFit::Cover), + ) } else { container(Space::new(0, 0)) };