From a21d8f27f4f931d40af2af86efe745968dafc2f9 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Mon, 28 Aug 2023 17:36:24 +0000 Subject: [PATCH] feat: button to auto add detected trackers to config --- src/file_builders/monado_config_v0.rs | 30 ++++++++++++++++++++ src/ui/devices_box.rs | 41 +++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/file_builders/monado_config_v0.rs b/src/file_builders/monado_config_v0.rs index 96441b7..7c61d37 100644 --- a/src/file_builders/monado_config_v0.rs +++ b/src/file_builders/monado_config_v0.rs @@ -163,6 +163,19 @@ pub struct MonadoConfigV0 { pub tracker_roles: Vec, } +impl MonadoConfigV0 { + pub fn has_tracker_serial(&self, serial: &str) -> bool { + self.tracker_roles.iter().any(|t| t.device_serial == serial) + } + + pub fn get_next_free_xrt_tracker_role(&self) -> XrtTrackerRole { + XrtTrackerRole::iter() + .find(|role| !self.tracker_roles.iter().any(|et| et.role == **role)) + .unwrap_or(&XrtTrackerRole::ViveTrackerHtcxHandheldObject) + .clone() + } +} + fn get_monado_config_v0_path() -> String { format!( "{config}/monado/config_v0.json", @@ -188,6 +201,23 @@ pub fn dump_monado_config_v0(config: &MonadoConfigV0) { dump_monado_config_v0_to_path(config, &get_monado_config_v0_path()); } +pub fn dump_generic_trackers(trackers: &[String]) -> usize { + let mut conf = get_monado_config_v0(); + let mut added: usize = 0; + trackers.iter().for_each(|serial| { + if conf.has_tracker_serial(serial) { + conf.tracker_roles.push(TrackerRole { + device_serial: serial.to_string(), + role: conf.get_next_free_xrt_tracker_role(), + ..TrackerRole::default() + }); + added += 1; + } + }); + + added +} + #[cfg(test)] mod tests { use super::get_monado_config_v0_from_path; diff --git a/src/ui/devices_box.rs b/src/ui/devices_box.rs index 4ba24c1..3b11447 100644 --- a/src/ui/devices_box.rs +++ b/src/ui/devices_box.rs @@ -1,7 +1,12 @@ -use crate::xr_devices::{XRDevice, XRDevices}; +use crate::{ + file_builders::monado_config_v0::dump_generic_trackers, + xr_devices::{XRDevice, XRDevices}, +}; use gtk::prelude::*; use relm4::prelude::*; +use super::alert::alert; + #[tracker::track] pub struct DevicesBox { devices: Option, @@ -10,6 +15,7 @@ pub struct DevicesBox { #[derive(Debug)] pub enum DevicesBoxMsg { UpdateDevices(Option), + DumpGenericTrackers, } impl DevicesBox { @@ -251,6 +257,16 @@ impl SimpleComponent for DevicesBox { None => "None".to_string(), }).as_str(), }, + gtk::Button { + set_halign: gtk::Align::Start, + set_hexpand: false, + set_icon_name: "document-save-symbolic", + set_tooltip_text: Some("Save current trackers"), + set_css_classes: &["circular", "flat"], + connect_clicked => move |_| { + sender.input(Self::Input::DumpGenericTrackers); + } + }, }, } } @@ -262,13 +278,34 @@ impl SimpleComponent for DevicesBox { Self::Input::UpdateDevices(devs) => { self.set_devices(devs); } + Self::Input::DumpGenericTrackers => { + if let Some(devs) = self.devices.as_ref() { + let added = dump_generic_trackers(&devs.generic_trackers); + let multi_title = format!("Added {} new trackers", added); + let (title, msg) = match added { + 0 => ( + "No new trackers found", + "All the currently connected trackers are already present in your configuration" + ), + 1 => ( + "Added 1 new tracker", + "Edit your configuration to make sure that all the trackers have the appropriate roles assigned" + ), + _ => ( + multi_title.as_str(), + "Edit your configuration to make sure that all the trackers have the appropriate roles assigned" + ), + }; + alert(title, Some(msg), None); + } + } } } fn init( _init: Self::Init, root: &Self::Root, - _sender: ComponentSender, + sender: ComponentSender, ) -> ComponentParts { let model = Self { tracker: 0,