Getting closer to a working little system

This commit is contained in:
Chris Cochrun 2024-11-19 12:35:42 -06:00
parent d9459d6336
commit c60353f8c8
9 changed files with 338 additions and 132 deletions

View file

@ -1,2 +1 @@
pub mod presenter;
pub mod slide_preview;

View file

@ -1,40 +1,43 @@
use std::{path::PathBuf, rc::Rc};
use std::{path::PathBuf, rc::Rc, time::Duration};
use cosmic::{
dialog::ashpd::url::Url,
iced::{widget::text, ContentFit, Length},
iced_widget::{row, stack, Stack},
iced::{widget::text, Background, Color, ContentFit, Length},
iced_widget::stack,
prelude::*,
widget::{
button, container, icon::Named, image, Container, Row, Space,
},
widget::{container, image, Container, Row, Space},
Task,
};
use iced_video_player::{Video, VideoPlayer};
use iced_video_player::{Position, Video, VideoPlayer};
use miette::{Context, IntoDiagnostic, Result};
use tracing::{debug, error};
use tracing::{debug, error, info};
use crate::{core::slide::Slide, BackgroundKind};
// #[derive(Default, Clone, Debug)]
pub(crate) struct Presenter {
pub slides: Vec<Slide>,
pub current_slide: i16,
pub current_slide: Slide,
pub current_slide_index: u16,
pub video: Option<Video>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Message {
NextSlide,
PrevSlide,
SlideChange(u8),
SlideChange(u16),
EndVideo,
StartVideo,
VideoPos(f32),
}
impl Presenter {
pub fn with_app_slides(slides: Vec<Slide>) -> Self {
pub fn with_slides(slides: Vec<Slide>) -> Self {
Self {
slides: slides.clone(),
current_slide: 0,
current_slide: slides[0].clone(),
current_slide_index: 0,
video: {
if let Some(slide) = slides.get(0) {
let path = slide.background().path.clone();
@ -42,7 +45,10 @@ impl Presenter {
let url = Url::from_file_path(path).unwrap();
let result = Video::new(&url);
match result {
Ok(v) => Some(v),
Ok(mut v) => {
v.set_paused(true);
Some(v)
}
Err(e) => {
error!("Had an error creating the video object: {e}");
None
@ -57,29 +63,6 @@ impl Presenter {
},
}
}
// pub fn with_initial_slide(slide: Slide) -> Self {
// Self {
// slides: vec![slide.clone()],
// current_slide: 0,
// video: {
// let path = slide.background().path.clone();
// if path.exists() {
// let url = Url::from_file_path(path).unwrap();
// let result = Video::new(&url);
// match result {
// Ok(v) => Some(v),
// Err(e) => {
// error!("Had an error creating the video object: {e}");
// None
// }
// }
// } else {
// error!("File doesn't exist: ");
// None
// }
// },
// }
// }
pub fn update(
&mut self,
@ -88,24 +71,75 @@ impl Presenter {
match message {
Message::NextSlide => {
debug!("next slide");
if let Some(video) = &mut self.video {
let _ = video.restart_stream();
if self.slides.len() as u16 - 1
== self.current_slide_index
{
debug!("no more slides");
return Task::none();
}
self.current_slide += 1;
self.reset_video();
Task::none()
self.current_slide_index += 1;
self.update(Message::SlideChange(
self.current_slide_index,
))
}
Message::PrevSlide => {
debug!("prev slide");
if let Some(video) = &mut self.video {
let _ = video.restart_stream();
if 0 == self.current_slide_index {
debug!("beginning slides");
return Task::none();
}
self.current_slide -= 1;
self.reset_video();
Task::none()
self.current_slide_index -= 1;
self.update(Message::SlideChange(
self.current_slide_index,
))
}
Message::SlideChange(id) => {
debug!(id, "slide changed");
if let Some(slide) = self.slides.get(id as usize) {
self.current_slide = slide.clone();
}
if let Some(video) = &mut self.video {
let _ = video.restart_stream();
}
self.reset_video();
Task::none()
}
Message::EndVideo => {
// if self.current_slide.video_loop() {
// if let Some(video) = &mut self.video {
// match video.restart_stream() {
// Ok(_) => info!("Restarting video"),
// Err(e) => {
// error!("Couldn't restart video: {e}")
// }
// }
// }
// }
Task::none()
}
Message::StartVideo => {
if let Some(video) = &mut self.video {
video.set_paused(false);
video
.set_looping(self.current_slide.video_loop());
}
Task::none()
}
Message::VideoPos(position) => {
if let Some(video) = &mut self.video {
let position = Position::Time(
Duration::from_secs_f32(position),
);
match video.seek(position, false) {
Ok(_) => debug!(
"Video position changed: {:?}",
position
),
Err(e) => error!(
"Problem changing video position: {e}"
),
}
}
Task::none()
}
}
@ -114,25 +148,30 @@ impl Presenter {
pub fn view(&self) -> Element<Message> {
let text = text!("This is frodo").size(50);
let text = Container::new(text).center(Length::Fill);
let container = match self
.slides
.get(self.current_slide as usize)
.unwrap()
.background()
.kind
{
crate::BackgroundKind::Image => Container::new(
image("/home/chris/pics/frodo.jpg")
.content_fit(ContentFit::Cover)
.width(Length::Fill)
.height(Length::Fill),
),
let black = Container::new(Space::new(0, 0))
.style(|_| {
container::background(Background::Color(Color::BLACK))
})
.width(Length::Fill)
.height(Length::Fill);
let container = match self.current_slide.background().kind {
crate::BackgroundKind::Image => {
let path =
self.current_slide.background().path.clone();
Container::new(
image(path)
.content_fit(ContentFit::Contain)
.width(Length::Fill)
.height(Length::Fill),
)
}
crate::BackgroundKind::Video => {
if let Some(video) = &self.video {
Container::new(
VideoPlayer::new(video)
.width(Length::Fill)
.height(Length::Fill)
.on_end_of_stream(Message::EndVideo)
.content_fit(ContentFit::Cover),
)
} else {
@ -140,7 +179,7 @@ impl Presenter {
}
}
};
stack!(container, text)
stack!(black, container.center(Length::Fill), text)
.width(Length::Fill)
.height(Length::Fill)
.into()
@ -169,7 +208,7 @@ impl Presenter {
Container::new(Space::new(Length::Fill, Length::Fill))
}
};
stack!(container, text)
stack!(container.center(Length::Fill), text)
.width(Length::Fill)
.height(Length::Fill)
.into()
@ -177,7 +216,7 @@ impl Presenter {
fn reset_video(&mut self) {
if let Some(slide) =
self.slides.get(self.current_slide as usize)
self.slides.get(self.current_slide_index as usize)
{
match slide.background().kind {
BackgroundKind::Image => self.video = None,
@ -201,34 +240,3 @@ impl Presenter {
}
}
}
pub(crate) fn slide_view<'a>(
slide: &'a Slide,
video: &'a Option<Video>,
) -> Stack<'a, crate::Message, cosmic::Theme> {
let text = text!("{}", slide.text()).size(50);
let text = Container::new(text).center(Length::Fill);
let container = match slide.background().kind {
crate::BackgroundKind::Image => Container::new(
image("/home/chris/pics/frodo.jpg")
.content_fit(ContentFit::Cover)
.width(Length::Fill)
.height(Length::Fill),
),
crate::BackgroundKind::Video => {
if let Some(video) = video {
Container::new(
VideoPlayer::new(video)
.width(Length::Shrink)
.height(Length::Fill)
.content_fit(ContentFit::Cover),
)
} else {
Container::new(Space::new(Length::Fill, Length::Fill))
}
}
};
stack!(container, text)
// .width(Length::Fill)
// .height(Length::Fill)
}

View file

@ -1,5 +0,0 @@
use crate::Slide;
pub struct SlidePreview<'a> {
slides: Vec<&'a Slide>,
}