diff --git a/src/core/videos.rs b/src/core/videos.rs index 3215cd4..5304717 100644 --- a/src/core/videos.rs +++ b/src/core/videos.rs @@ -15,7 +15,7 @@ use sqlx::{ SqlitePool, }; use std::path::{Path, PathBuf}; -use tracing::error; +use tracing::{debug, error}; #[derive( Clone, Debug, Default, PartialEq, Serialize, Deserialize, diff --git a/src/main.rs b/src/main.rs index 17242f0..6fceeb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,7 @@ use ui::EditorMode; use crate::core::kinds::ServiceItemKind; use crate::ui::text_svg::{self}; +use crate::ui::video_editor::{self, VideoEditor}; use crate::ui::widgets::draggable; pub mod core; @@ -124,6 +125,7 @@ struct App { library_open: bool, editor_mode: Option, song_editor: SongEditor, + video_editor: VideoEditor, searching: bool, search_query: String, search_results: Vec, @@ -171,6 +173,7 @@ enum Message { Save(Option), SaveAs, OpenSettings, + VideoEditor(video_editor::Message), } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -324,6 +327,7 @@ impl cosmic::Application for App { library_open: true, editor_mode: None, song_editor, + video_editor: VideoEditor::new(), searching: false, search_results: vec![], search_query: String::new(), @@ -763,6 +767,27 @@ impl cosmic::Application for App { song_editor::Action::None => Task::none(), } } + Message::VideoEditor(message) => { + match self.video_editor.update(message) { + video_editor::Action::Task(task) => { + task.map(|m| { + cosmic::Action::App(Message::VideoEditor( + m, + )) + }) + } + video_editor::Action::UpdateVideo(video) => { + if let Some(_) = &mut self.library { + self.update(Message::Library( + library::Message::UpdateVideo(video), + )) + } else { + Task::none() + } + } + video_editor::Action::None => Task::none(), + } + } Message::Present(message) => { // debug!(?message); if self.presentation_open @@ -917,53 +942,69 @@ impl cosmic::Application for App { let mut song = Song::default(); if let Some(library) = &mut self.library { match library.update(message) { - library::Action::OpenItem(None) => { - return Task::none(); - } - library::Action::Task(task) => { - return task.map(|message| { - cosmic::Action::App(Message::Library( - message, - )) - }); - } - library::Action::None => return Task::none(), - library::Action::OpenItem(Some(( - kind, - index, - ))) => { - debug!( - "Should get song at index: {:?}", - index - ); - let Some(lib_song) = - library.get_song(index) - else { - return Task::none(); - }; - self.editor_mode = Some(kind.into()); - song = lib_song.to_owned(); - debug!( - "Should change songs to: {:?}", - song - ); - } - library::Action::DraggedItem( - service_item, - ) => { - debug!("hi"); - self.library_dragged_item = - Some(service_item); - // self.nav_model - // .insert() - // .text(service_item.title.clone()) - // .data(service_item); - } - } + library::Action::OpenItem(None) => { + return Task::none(); + } + library::Action::Task(task) => { + return task.map(|message| { + cosmic::Action::App(Message::Library( + message, + )) + }); + } + library::Action::None => return Task::none(), + library::Action::OpenItem(Some(( + kind, + index, + ))) => { + match kind { + core::model::LibraryKind::Song => { + debug!( + "Should get song at index: {:?}", + index + ); + let Some(lib_song) = + library.get_song(index) + else { + return Task::none(); + }; + self.editor_mode = Some(kind.into()); + song = lib_song.to_owned(); + debug!( + "Should change songs to: {:?}", + song + ); + + return self.update(Message::SongEditor( + song_editor::Message::ChangeSong(song), + )); + }, + core::model::LibraryKind::Video => { + let Some(lib_video) = library.get_video(index) else { + return Task::none(); + }; + self.editor_mode = Some(kind.into()); + let video = lib_video.to_owned(); + return self.update(Message::VideoEditor(video_editor::Message::ChangeVideo(video))); + }, + core::model::LibraryKind::Image => todo!(), + core::model::LibraryKind::Presentation => todo!(), + } + } + library::Action::DraggedItem( + service_item, + ) => { + debug!("hi"); + self.library_dragged_item = + Some(service_item); + // self.nav_model + // .insert() + // .text(service_item.title.clone()) + // .data(service_item); + } + } } - self.update(Message::SongEditor( - song_editor::Message::ChangeSong(song), - )) + Task::none() } Message::File(file) => { self.file = file; @@ -998,16 +1039,16 @@ impl cosmic::Application for App { Message::WindowOpened(id) => { debug!(?id, "Window opened"); if self.cli_mode - || id > self.core.main_window_id().expect("Cosmic core seems to be missing a main window, was this started in cli mode?") - { - self.presentation_open = true; - if let Some(video) = &mut self.presenter.video { - video.set_muted(false); - } - window::change_mode(id, Mode::Fullscreen) - } else { - Task::none() - } + || id > self.core.main_window_id().expect("Cosmic core seems to be missing a main window, was this started in cli mode?") + { + self.presentation_open = true; + if let Some(video) = &mut self.presenter.video { + video.set_muted(false); + } + window::change_mode(id, Mode::Fullscreen) + } else { + Task::none() + } } Message::WindowClosed(id) => { warn!("Closing window: {id}"); @@ -1283,8 +1324,20 @@ impl cosmic::Application for App { Container::new(horizontal_space().width(0)) }; - let song_editor = - self.song_editor.view().map(Message::SongEditor); + let editor = self.editor_mode.as_ref().map_or_else( + || Element::from(Space::new(0, 0)), + |mode| match mode { + EditorMode::Song => { + self.song_editor.view().map(Message::SongEditor) + } + EditorMode::Image => todo!(), + EditorMode::Video => { + self.video_editor.view().map(Message::VideoEditor) + } + EditorMode::Presentation => todo!(), + EditorMode::Slide => todo!(), + }, + ); let service_row = row![ service_list, @@ -1331,12 +1384,13 @@ impl cosmic::Application for App { Container::new(horizontal_space()) }; - let main_area = if let Some(editor) = &self.editor_mode { - container(song_editor) - .padding(cosmic::theme::spacing().space_xxl) - } else { - Container::new(service_row).center_y(Length::Fill) - }; + let main_area = self.editor_mode.as_ref().map_or_else( + || Container::new(service_row).center_y(Length::Fill), + |_| { + container(editor) + .padding(cosmic::theme::spacing().space_xxl) + }, + ); let column = column![ row![ diff --git a/src/ui/library.rs b/src/ui/library.rs index bbe75f2..1bcea18 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -399,7 +399,9 @@ impl<'a> Library { Ok(()) => { return Action::Task( Task::future(self.db.acquire()).and_then( - move |conn| update_in_db(&song, conn), + move |conn| { + song_db_update(&song, conn) + }, ), ); } @@ -463,13 +465,7 @@ impl<'a> Library { return Action::Task( Task::future(self.db.acquire()).and_then( move |conn| { - Task::perform( - update_video_in_db( - video.clone(), - conn, - ), - |_| Message::VideoChanged, - ) + video_db_update(&video, conn) }, ), ); @@ -477,7 +473,7 @@ impl<'a> Library { Err(_) => todo!(), } } - Message::VideoChanged => (), + Message::VideoChanged => debug!("vid shoulda changed"), Message::UpdatePresentation(presentation) => { let Some((kind, index)) = self.editing_item else { error!("Not editing an item"); @@ -938,37 +934,12 @@ impl<'a> Library { items.into_iter().map(|item| item.1).collect() } - // fn update_item(self, item: C) -> Task { - // let Some((kind, index)) = self.editing_item else { - // error!("Not editing an item"); - // return Task::none(); - // }; - - // match kind { - // LibraryKind::Song => todo!(), - // LibraryKind::Video => todo!(), - // LibraryKind::Image => { - // match self - // .image_library - // .update_item(item as Image, index) - // { - // Ok(_) => Task::future(self.db.acquire()) - // .and_then(|conn| { - // Task::perform( - // update_image_in_db(item, conn), - // |_| Message::ImageChanged, - // ) - // }), - // Err(_) => todo!(), - // } - // } - // LibraryKind::Presentation => todo!(), - // } - // } + pub fn get_video(&self, index: i32) -> Option<&Video> { + self.video_library.get_item(index) + } } async fn add_images() -> Option> { - debug!("here man"); let paths = Dialog::new().title("pick image").open_files().await.ok()?; Some( @@ -983,7 +954,6 @@ async fn add_images() -> Option> { } async fn add_videos() -> Option> { - debug!("here man"); let paths = Dialog::new().title("pick video").open_files().await.ok()?; Some( @@ -997,7 +967,31 @@ async fn add_videos() -> Option> { ) } -fn update_in_db( +fn video_db_update( + video: &Video, + conn: PoolConnection, +) -> Task { + let video_title = video.title.clone(); + warn!("Should have updated video: {:?}", video_title); + Task::perform( + update_video_in_db(video.to_owned(), conn).map(move |r| { + match r { + Ok(()) => { + warn!( + "Should have updated video: {:?}", + video_title + ); + } + Err(e) => { + error!(?e); + } + } + }), + |()| Message::VideoChanged, + ) +} + +fn song_db_update( song: &Song, conn: PoolConnection, ) -> Task { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 81eed17..d73fab9 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -8,6 +8,7 @@ pub mod slide_editor; pub mod song_editor; pub mod text_svg; pub mod video; +pub mod video_editor; pub mod widgets; pub enum EditorMode {