From acafbac9d998af4cdc1228ad136a18f1e1fac095 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Wed, 21 Jun 2023 07:47:15 +0200 Subject: [PATCH] feat: move runtime switcher box (steam vs rex) to its own component --- src/ui/app.rs | 18 ------ src/ui/main_view.rs | 64 +++++-------------- src/ui/mod.rs | 1 + src/ui/runtime_switcher_box.rs | 110 +++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 67 deletions(-) create mode 100644 src/ui/runtime_switcher_box.rs diff --git a/src/ui/app.rs b/src/ui/app.rs index 23bdcf2..521746e 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -14,12 +14,6 @@ use crate::dependencies::basalt_deps::get_missing_basalt_deps; use crate::dependencies::libsurvive_deps::get_missing_libsurvive_deps; use crate::dependencies::monado_deps::get_missing_monado_deps; use crate::dependencies::wivrn_deps::get_missing_wivrn_deps; -use crate::file_builders::active_runtime_json::{ - set_current_active_runtime_to_profile, set_current_active_runtime_to_steam, -}; -use crate::file_builders::openvrpaths_vrpath::{ - set_current_openvrpaths_to_profile, set_current_openvrpaths_to_steam, -}; use crate::file_utils::setcap_cap_sys_nice_eip; use crate::profile::{Profile, XRServiceType}; use crate::profiles::system_valve_index::system_valve_index_profile; @@ -79,7 +73,6 @@ pub enum Msg { EnableDebugViewChanged(bool), DoStartStopXRService, ProfileSelected(String), - SetXRServiceRuntime(bool), RunSetCap, OpenLibsurviveSetup, } @@ -334,16 +327,6 @@ impl SimpleComponent for App { .sender() .emit(MainViewMsg::UpdateSelectedProfile(profile.clone())); } - Msg::SetXRServiceRuntime(is_rex) => { - if is_rex { - let profile = self.get_selected_profile(); - set_current_active_runtime_to_profile(profile.clone()); - set_current_openvrpaths_to_profile(profile.clone()); - } else { - set_current_active_runtime_to_steam(); - set_current_openvrpaths_to_steam(); - } - } Msg::OpenLibsurviveSetup => { self.libsurvive_setup_window .sender() @@ -408,7 +391,6 @@ impl SimpleComponent for App { MainViewOutMsg::EnableDebugViewChanged(val) => Msg::EnableDebugViewChanged(val), MainViewOutMsg::DoStartStopXRService => Msg::DoStartStopXRService, MainViewOutMsg::ProfileSelected(name) => Msg::ProfileSelected(name), - MainViewOutMsg::SetXRServiceRuntime(is_rex) => Msg::SetXRServiceRuntime(is_rex), }), debug_view: DebugView::builder() .launch(DebugViewInit { diff --git a/src/ui/main_view.rs b/src/ui/main_view.rs index f8a63dd..5b95163 100644 --- a/src/ui/main_view.rs +++ b/src/ui/main_view.rs @@ -1,8 +1,10 @@ use super::install_wivrn_box::{InstallWivrnBox, InstallWivrnBoxInit, InstallWivrnBoxMsg}; +use super::runtime_switcher_box::{ + RuntimeSwitcherBox, RuntimeSwitcherBoxInit, RuntimeSwitcherBoxMsg, +}; use super::steam_launch_options_box::{SteamLaunchOptionsBox, SteamLaunchOptionsBoxMsg}; use crate::config::Config; use crate::constants::APP_NAME; -use crate::file_builders::active_runtime_json::{self, get_current_active_runtime}; use crate::profile::{Profile, XRServiceType}; use crate::ui::app::{ AboutAction, BuildProfileAction, DebugViewToggleAction, LibsurviveSetupAction, @@ -24,6 +26,8 @@ pub struct MainView { install_wivrn_box: Controller, #[tracker::do_not_track] steam_launch_options_box: Controller, + #[tracker::do_not_track] + runtime_switcher_box: Controller, } #[derive(Debug)] @@ -43,7 +47,6 @@ pub enum MainViewOutMsg { EnableDebugViewChanged(bool), DoStartStopXRService, ProfileSelected(String), - SetXRServiceRuntime(bool), } pub struct MainViewInit { @@ -132,37 +135,7 @@ impl SimpleComponent for MainView { sender.input(MainViewMsg::StartStopClicked) }, }, - gtk::Separator { - set_orientation: gtk::Orientation::Horizontal, - set_hexpand: true, - }, - gtk::Label { - add_css_class: "heading", - set_hexpand: true, - set_xalign: 0.0, - set_margin_start: 12, - set_margin_end: 12, - set_label: "OpenXR/OpenVR Runtime", - set_wrap: true, - set_wrap_mode: gtk::pango::WrapMode::Word, - }, - gtk::Box { - set_orientation: gtk::Orientation::Horizontal, - set_hexpand: true, - set_halign: gtk::Align::Start, - set_margin_start: 12, - set_margin_end: 12, - set_spacing: 12, - gtk::Label { - set_label: "Steam", - }, - #[name(runtime_switch)] - gtk::Switch { - }, - gtk::Label { - set_label: APP_NAME, - }, - }, + model.runtime_switcher_box.widget(), model.steam_launch_options_box.widget(), model.install_wivrn_box.widget(), } @@ -204,6 +177,9 @@ impl SimpleComponent for MainView { self.install_wivrn_box .sender() .emit(InstallWivrnBoxMsg::UpdateSelectedProfile(prof.clone())); + self.runtime_switcher_box + .sender() + .emit(RuntimeSwitcherBoxMsg::UpdateSelectedProfile(prof.clone())); } Self::Input::UpdateProfileNames(names, config) => { self.set_profile_names(names); @@ -253,28 +229,18 @@ impl SimpleComponent for MainView { steam_launch_options_box: SteamLaunchOptionsBox::builder().launch(()).detach(), install_wivrn_box: InstallWivrnBox::builder() .launch(InstallWivrnBoxInit { - selected_profile: init.selected_profile, + selected_profile: init.selected_profile.clone(), + }) + .detach(), + runtime_switcher_box: RuntimeSwitcherBox::builder() + .launch(RuntimeSwitcherBoxInit { + selected_profile: init.selected_profile.clone(), }) .detach(), tracker: 0, }; let widgets = view_output!(); - { - match get_current_active_runtime() { - None => {} - Some(runtime) => { - widgets - .runtime_switch - .set_active(!active_runtime_json::is_steam(runtime)); - } - } - widgets.runtime_switch.connect_state_set(move |_, state| { - sender.output(MainViewOutMsg::SetXRServiceRuntime(state)); - gtk::Inhibit(false) - }); - } - model.profiles_dropdown = Some(widgets.profiles_dropdown.clone()); ComponentParts { model, widgets } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index dd4b340..6bdd3bb 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -6,3 +6,4 @@ pub mod build_window; pub mod libsurvive_setup_window; pub mod install_wivrn_box; pub mod steam_launch_options_box; +pub mod runtime_switcher_box; diff --git a/src/ui/runtime_switcher_box.rs b/src/ui/runtime_switcher_box.rs new file mode 100644 index 0000000..5df2117 --- /dev/null +++ b/src/ui/runtime_switcher_box.rs @@ -0,0 +1,110 @@ +use relm4::prelude::*; +use gtk::prelude::*; + +use crate::{constants::APP_NAME, file_builders::{active_runtime_json::{get_current_active_runtime, self, set_current_active_runtime_to_profile, set_current_active_runtime_to_steam}, openvrpaths_vrpath::{set_current_openvrpaths_to_profile, set_current_openvrpaths_to_steam}}, profile::Profile}; + +pub struct RuntimeSwitcherBox { + selected_profile: Profile, +} + +#[derive(Debug)] +pub enum RuntimeSwitcherBoxMsg { + UpdateSelectedProfile(Profile), + SetXRServiceRuntime(bool), +} + +pub struct RuntimeSwitcherBoxInit { + pub selected_profile: Profile, +} + +#[relm4::component(pub)] +impl SimpleComponent for RuntimeSwitcherBox { + type Init = RuntimeSwitcherBoxInit; + type Input = RuntimeSwitcherBoxMsg; + type Output = (); + + view! { + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_hexpand: true, + set_spacing: 12, + gtk::Separator { + set_orientation: gtk::Orientation::Horizontal, + set_hexpand: true, + }, + gtk::Label { + add_css_class: "heading", + set_hexpand: true, + set_xalign: 0.0, + set_margin_start: 12, + set_margin_end: 12, + set_label: "OpenXR/OpenVR Runtime", + set_wrap: true, + set_wrap_mode: gtk::pango::WrapMode::Word, + }, + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_hexpand: true, + set_halign: gtk::Align::Start, + set_margin_start: 12, + set_margin_end: 12, + set_spacing: 12, + gtk::Label { + set_label: "Steam", + }, + #[name(runtime_switch)] + gtk::Switch { + }, + gtk::Label { + set_label: APP_NAME, + }, + }, + } + } + + fn update(&mut self, message: Self::Input, sender: ComponentSender) { + match message { + Self::Input::UpdateSelectedProfile(prof) => { + self.selected_profile = prof; + } + Self::Input::SetXRServiceRuntime(is_rex) => { + if is_rex { + set_current_active_runtime_to_profile(self.selected_profile.clone()); + set_current_openvrpaths_to_profile(self.selected_profile.clone()); + } else { + set_current_active_runtime_to_steam(); + set_current_openvrpaths_to_steam(); + } + } + } + } + + fn init( + init: Self::Init, + root: &Self::Root, + sender: ComponentSender, + ) -> ComponentParts { + let model = Self { + selected_profile: init.selected_profile, + }; + + let widgets = view_output!(); + + { + match get_current_active_runtime() { + None => {} + Some(runtime) => { + widgets + .runtime_switch + .set_active(!active_runtime_json::is_steam(runtime)); + } + } + widgets.runtime_switch.connect_state_set(move |_, state| { + sender.input(Self::Input::SetXRServiceRuntime(state)); + gtk::Inhibit(false) + }); + } + + ComponentParts { model, widgets } + } +}