diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 34328d7..3d0bc41 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -2,6 +2,7 @@ pub mod ffmpeg; mod file_helper; pub mod image_model; pub mod models; +pub mod obs; pub mod presentation_model; pub mod reveal_js; pub mod schema; @@ -11,8 +12,7 @@ pub mod settings; pub mod slide_model; mod slide_obj; pub mod songs; +pub mod utils; pub mod video_model; pub mod ytdl; -pub mod utils; -pub mod obs; // mod video_thumbnail; diff --git a/src/rust/obs.rs b/src/rust/obs.rs index 00da796..797bf63 100644 --- a/src/rust/obs.rs +++ b/src/rust/obs.rs @@ -1,7 +1,8 @@ +use core::fmt; use std::error::Error; -use obws::Client; use obws::responses::scenes::Scenes; +use obws::Client; use tracing::debug; pub struct Obs { @@ -9,16 +10,43 @@ pub struct Obs { client: Client, } +impl fmt::Debug for Obs { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Client") + .field("host", &"localhost") + .field("port", &4455) + .finish() + } +} + +impl Clone for Obs { + fn clone(&self) -> Self { + Self { + scenes: self.scenes.clone(), + client: make_client(), + } + } +} + impl Obs { pub async fn new() -> Result> { - let client = Client::connect("localhost", 4455, Some("")).await?; + let client = + Client::connect("localhost", 4455, Some("")).await?; let scene_list = client.scenes().list().await?; debug!(?scene_list); - Ok(Self{scenes: scene_list, client}) + Ok(Self { + scenes: scene_list, + client, + }) } pub fn get_list(self) -> Result, Box> { - let scenes = self.scenes.scenes.iter().map(|x| x.name).collect::>(); + let scenes = self + .scenes + .scenes + .iter() + .map(|x| x.name.clone()) + .collect::>(); if scenes.len() > 0 { Ok(scenes) } else { @@ -26,12 +54,30 @@ impl Obs { } } - pub fn set_scene(self, scene: String) -> Result<(), Box> { - if let Some(scene) = self.scenes.scenes.iter().filter(|x| x.name == scene).next() { - self.client.scenes().set_current_program_scene(scene.name.as_str()); + pub fn set_scene( + self, + scene: String, + ) -> Result<(), Box> { + if let Some(scene) = self + .scenes + .scenes + .iter() + .filter(|x| x.name == scene) + .next() + { + self.client + .scenes() + .set_current_program_scene(scene.name.as_str()); Ok(()) } else { Err("Couldn't set the scene".to_owned())? } } } + +fn make_client() -> Client { + let runtime = tokio::runtime::Runtime::new().unwrap(); + let future = Client::connect("localhost", 4455, Some("")); + let client = runtime.block_on(future).unwrap(); + client +} diff --git a/src/rust/service_item_model.rs b/src/rust/service_item_model.rs index d36e69f..869bd7b 100644 --- a/src/rust/service_item_model.rs +++ b/src/rust/service_item_model.rs @@ -24,6 +24,7 @@ mod service_item_model { type QUrl = cxx_qt_lib::QUrl; } + use crate::obs::Obs; use serde::{Deserialize, Serialize}; #[cxx_qt::qobject] #[derive(Clone, Debug)] @@ -83,13 +84,26 @@ mod service_item_model { } #[cxx_qt::qobject(base = "QAbstractListModel")] - #[derive(Default, Debug)] + #[derive(Debug)] pub struct ServiceItemMod { id: i32, service_items: Vec, obs: Obs, } + impl Default for ServiceItemMod { + fn default() -> Self { + let obs = tokio::runtime::Runtime::new() + .unwrap() + .block_on(async { Obs::new().await.ok().unwrap() }); + Self { + id: 0, + service_items: Vec::new(), + obs, + } + } + } + #[cxx_qt::qsignals(ServiceItemMod)] pub enum Signals<'a> { #[inherit] @@ -152,7 +166,6 @@ mod service_item_model { use tracing::{debug, debug_span, error, info, instrument}; use zstd::{Decoder, Encoder}; - use crate::obs::Obs; impl qobject::ServiceItemMod { pub fn setup(mut self: Pin<&mut Self>) { todo!() @@ -349,6 +362,8 @@ mod service_item_model { let dest_id = dest_index as usize; let cnt = count as usize; let end_service_item = source_id + cnt - 1; + // This needs to point to the index above the intended position if moving + // up. Qt's begin_move_rows requires that knowledge for some reason. let qt_dest_index = if source_index < dest_index { dest_index + 1 } else { @@ -585,25 +600,19 @@ mod service_item_model { // println!("service_item is deactivating {:?}", i); service_item.active = false; } + let obs = self.as_mut().obs_mut().clone(); + if let Some(service_item) = self .as_mut() .service_items_mut() .get_mut(index as usize) { - println!("service_item is activating {:?}", index); - println!( - "service_item_title: {:?}", - service_item.name - ); - println!( - "service_item_background: {:?}", - service_item.background - ); - println!( - "service_item_background_type: {:?}", - service_item.background_type - ); + debug!(activating_item = index, + title = ?service_item.name, + background = ?service_item.background, + background_type = ?service_item.background_type); service_item.active = true; + obs.set_scene(service_item.obs_scene.to_string()); self.as_mut().emit_data_changed( tl, br, @@ -612,7 +621,6 @@ mod service_item_model { // We use this signal generated by our signals enum to tell QML that // the active service_item has changed which is used to reposition views. self.as_mut().emit_active_changed(); - Obs::set_scene(service_item.obs_scene.to_string()); true } else { false