From bca785d05ae0a6b2dfddbb1dce95e26d32b98aa8 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Fri, 8 Dec 2023 09:52:42 +0000 Subject: [PATCH] feat: stardust config, wire up settings in stardust view --- src/main.rs | 1 + src/stardust_config.rs | 68 ++++++++++++++++++++ src/ui/stardust/stardust_view.rs | 103 ++++++++++++++++++++++++------- 3 files changed, 148 insertions(+), 24 deletions(-) create mode 100644 src/stardust_config.rs diff --git a/src/main.rs b/src/main.rs index 5ac7bc1..a02f595 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,7 @@ pub mod profile; pub mod profiles; pub mod runner; pub mod runner_pipeline; +pub mod stardust_config; pub mod steamvr_utils; pub mod ui; pub mod xr_devices; diff --git a/src/stardust_config.rs b/src/stardust_config.rs new file mode 100644 index 0000000..224fa3b --- /dev/null +++ b/src/stardust_config.rs @@ -0,0 +1,68 @@ +use std::{fs::File, io::BufReader}; + +use serde::{Deserialize, Serialize}; + +use crate::{constants::CMD_NAME, file_utils::get_writer, paths::get_config_dir}; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct StardustExternalClient { + pub executable: String, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct StardustConfig { + pub autostart: bool, + pub start_as_overlay: bool, + pub overlay_priority: u32, + pub disable_controller: bool, + pub repo: String, + pub branch: String, + pub debug: bool, + + pub flatland_enabled: bool, + pub flatland_repo: String, + pub flatland_branch: String, + + pub external_clients: Vec, +} + +impl Default for StardustConfig { + fn default() -> Self { + Self { + autostart: false, + start_as_overlay: false, + overlay_priority: 0, + disable_controller: false, + repo: "https://github.com/StardustXR/server".into(), + branch: "main".into(), + debug: false, + + flatland_enabled: true, + flatland_repo: "https://github.com/StardustXR/flatland".into(), + flatland_branch: "main".into(), + + external_clients: vec![], + } + } +} + +impl StardustConfig { + fn file_path() -> String { + format!("{}/{}.stardust.json", get_config_dir(), CMD_NAME) + } + + pub fn get_config() -> Self { + if let Ok(file) = File::open(Self::file_path()) { + let reader = BufReader::new(file); + if let Ok(conf) = serde_json::from_reader(reader) { + return conf; + } + } + Self::default() + } + + pub fn save(&self) -> Result<(), serde_json::Error> { + let writer = get_writer(&Self::file_path()); + serde_json::to_writer_pretty(writer, self) + } +} diff --git a/src/ui/stardust/stardust_view.rs b/src/ui/stardust/stardust_view.rs index 3455139..38e53c6 100644 --- a/src/ui/stardust/stardust_view.rs +++ b/src/ui/stardust/stardust_view.rs @@ -1,4 +1,6 @@ use crate::{ + builders::build_stardust::{get_build_stardust_jobs, StardustServerSpec}, + stardust_config::StardustConfig, stateless_action, ui::build_window::{BuildWindow, BuildWindowMsg, BuildWindowOutMsg}, withclones, @@ -24,6 +26,8 @@ pub enum StardustViewMsg { OverlayPriorityChanged(u32), DisableControllerChanged(bool), RepoChanged(String), + DebugChanged(bool), + FlatlandEnabledChanged(bool), FlatlandRepoChanged(String), @@ -45,6 +49,8 @@ pub struct StardustView { stardust_service_active: bool, #[tracker::do_not_track] build_window: Controller, + + stardust_config: StardustConfig, } #[relm4::component(pub)] @@ -124,16 +130,14 @@ impl SimpleComponent for StardustView { set_selection_mode: gtk::SelectionMode::None, adw::SwitchRow { set_title: "Autostart with XR Service", - // TODO: settings - // set_active: model.settings.autostart, + set_active: model.stardust_config.autostart, connect_active_notify[sender] => move |row| { sender.input(Self::Input::AutostartChanged(row.is_active())); } }, adw::SwitchRow { set_title: "Start as an overlay", - // TODO: settings - // set_active: model.settings.overlay, + set_active: model.stardust_config.start_as_overlay, connect_active_notify[sender] => move |row| { sender.input(Self::Input::StartAsOvelayChanged(row.is_active())); } @@ -142,8 +146,6 @@ impl SimpleComponent for StardustView { set_title: "Overlay priority", set_digits: 0, set_snap_to_ticks: true, - // TODO: settings -- here or in adj? - // set_value: model.settings.priority, #[wrap(Some)] set_adjustment: overlay_priority_adj = >k::Adjustment { set_lower: 0.0, @@ -151,8 +153,7 @@ impl SimpleComponent for StardustView { set_value: 0.0, set_page_increment: 10.0, set_step_increment: 1.0, - // TODO: settings - // set_value: model.settings.priority, + set_value: model.stardust_config.overlay_priority.into(), }, set_range: (0.0, f64::from(u32::MAX)), connect_value_notify[sender] => move |row| { @@ -164,17 +165,21 @@ impl SimpleComponent for StardustView { set_title: "Disable controller", set_subtitle_lines: 0, set_subtitle: "Necessary if you plan on using hand tracking", - // TODO: settings - // set_active: model.settings.autostart, + set_active: model.stardust_config.disable_controller, connect_active_notify[sender] => move |row| { sender.input(Self::Input::DisableControllerChanged(row.is_active())); } }, + adw::SwitchRow { + set_title: "Debug symbols", + set_active: model.stardust_config.debug, + connect_active_notify[sender] => move |row| { + sender.input(Self::Input::DebugChanged(row.is_active())); + } + }, adw::EntryRow { set_title: "Repository#git ref", - // TODO: settings - // set_text: model.settings.repo, - set_text: "https://github.com/StardustXR/server#main", + set_text: &format!("{}#{}", model.stardust_config.repo, model.stardust_config.branch), connect_changed[sender] => move |row| { sender.input(Self::Input::RepoChanged(row.text().into())); } @@ -199,6 +204,7 @@ impl SimpleComponent for StardustView { set_valign: gtk::Align::Center, set_hexpand: false, set_vexpand: false, + set_active: model.stardust_config.flatland_enabled, connect_activate[sender] => move |sw| { sender.input(Self::Input::FlatlandEnabledChanged(sw.is_active())); }, @@ -207,9 +213,7 @@ impl SimpleComponent for StardustView { bind_property: ("enable-expansion", &flatland_switch, "active"), add_row: flatland_repo_entryrow = &adw::EntryRow { set_title: "Repository#git ref", - // TODO: settings - // set_text: model.settings.clients.builtin.flatland.repo, - set_text: "https://github.com/StardustXR/flatland#main", + set_text: &format!("{}#{}", model.stardust_config.flatland_repo, model.stardust_config.flatland_branch), connect_changed[sender] => move |row| { sender.input(Self::Input::FlatlandRepoChanged(row.text().into())); }, @@ -241,6 +245,15 @@ impl SimpleComponent for StardustView { match message { Self::Input::BuildStardust => { println!("Build"); + get_build_stardust_jobs( + StardustServerSpec { + repo: self.stardust_config.repo.clone(), + branch: self.stardust_config.branch.clone(), + debug: self.stardust_config.debug, + wayland_support: true, + }, + &vec![], + ); self.build_window.sender().emit(BuildWindowMsg::Present); } Self::Input::UpdateStardust => { @@ -260,25 +273,66 @@ impl SimpleComponent for StardustView { println!("Restart"); } Self::Input::AutostartChanged(val) => { - println!("Autostart: {}", val); + self.stardust_config.autostart = val; + self.stardust_config.save(); } Self::Input::StartAsOvelayChanged(val) => { - println!("StartAsOvelayChanged: {}", val); + self.stardust_config.start_as_overlay = val; + self.stardust_config.save(); } Self::Input::OverlayPriorityChanged(val) => { - println!("OverlayPriorityChanged: {}", val); + self.stardust_config.overlay_priority = val; + self.stardust_config.save(); } Self::Input::DisableControllerChanged(val) => { - println!("DisableControllerChanged: {}", val); + self.stardust_config.disable_controller = val; + self.stardust_config.save(); } Self::Input::RepoChanged(val) => { - println!("RepoChanged: {}", val); + let def = StardustConfig::default(); + let split = val + .split('#') + .map(str::trim) + .map(String::from) + .collect::>(); + let repo = split.get(0).unwrap_or_else(|| &def.repo).clone(); + let branch = split.get(1).unwrap_or_else(|| &def.branch).clone(); + self.stardust_config.repo = if repo.is_empty() { def.repo } else { repo }; + self.stardust_config.branch = if branch.is_empty() { + def.branch + } else { + branch + }; + self.stardust_config.save(); + } + Self::Input::DebugChanged(val) => { + self.stardust_config.debug = val; + self.stardust_config.save(); } Self::Input::FlatlandRepoChanged(val) => { - println!("FlatlandRepoChanged: {}", val); + let def = StardustConfig::default(); + let split = val + .split('#') + .map(str::trim) + .map(String::from) + .collect::>(); + let repo = split.get(0).unwrap_or_else(|| &def.flatland_repo).clone(); + let branch = split.get(1).unwrap_or_else(|| &def.flatland_branch).clone(); + self.stardust_config.flatland_repo = if repo.is_empty() { + def.flatland_repo + } else { + repo + }; + self.stardust_config.flatland_branch = if branch.is_empty() { + def.flatland_branch + } else { + branch + }; + self.stardust_config.save(); } Self::Input::FlatlandEnabledChanged(val) => { - println!("FlatlandEnabledChanged: {}", val); + self.stardust_config.flatland_enabled = val; + self.stardust_config.save(); } Self::Input::AddClient => { println!("AddClient"); @@ -292,6 +346,7 @@ impl SimpleComponent for StardustView { sender: ComponentSender, ) -> ComponentParts { let model = Self { + tracker: 0, stardust_service_active: false, build_window: BuildWindow::builder() .transient_for(init.root_win) @@ -299,7 +354,7 @@ impl SimpleComponent for StardustView { .forward(sender.input_sender(), |msg| match msg { BuildWindowOutMsg::CancelBuild => Self::Input::CancelBuild, }), - tracker: 0, + stardust_config: StardustConfig::get_config(), }; let widgets = view_output!();