making presentation_editor only need one page at a time
Some checks are pending
/ test (push) Waiting to run

This commit is contained in:
Chris Cochrun 2025-09-30 05:51:33 -05:00
parent ee45a11a0f
commit 19e8fbcc35
2 changed files with 100 additions and 42 deletions

View file

@ -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<Presentation>,
slides: Option<Vec<Slide>>,
document: Option<Document>,
current_slide: Option<Handle>,
page_count: Option<i32>,
current_slide_index: Option<i32>,
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<Message> {
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))
};