From 06fcb0077c617ca56c30dda60610ef8b63cf7b93 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Mon, 5 Feb 2024 13:37:11 +0100 Subject: [PATCH] feat: command line options support; option to start the xr service on startup --- src/main.rs | 19 +++++++++++++++---- src/ui/app.rs | 7 +++++++ src/ui/cmdline_opts.rs | 29 +++++++++++++++++++++++++++++ src/ui/mod.rs | 1 + 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/ui/cmdline_opts.rs diff --git a/src/main.rs b/src/main.rs index 8d5d642..78ab24d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,13 @@ use file_builders::{ use gettextrs::LocaleCategory; use relm4::{ adw, - gtk::{self, gdk, gio, glib}, - RelmApp, + gtk::{self, gdk, gio, glib, prelude::*}, + MessageBroker, RelmApp, }; use ui::app::{App, AppInit}; +use crate::ui::{app::Msg, cmdline_opts::CmdLineOpts}; + pub mod adb; pub mod build_tools; pub mod builders; @@ -90,10 +92,19 @@ fn main() -> Result<()> { let main_app = adw::Application::builder() .application_id(APP_ID) - .flags(gio::ApplicationFlags::FLAGS_NONE) + .flags(gio::ApplicationFlags::HANDLES_COMMAND_LINE) .resource_base_path(format!("/{}", APP_ID.replace(".", "/"))) .build(); - let app = RelmApp::from_app(main_app.clone()); + + static BROKER: MessageBroker = MessageBroker::new(); + CmdLineOpts::init(&main_app); + let sender = BROKER.sender(); + main_app.connect_command_line(move |this, cmdline| { + this.activate(); + sender.emit(Msg::HandleCommandLine(CmdLineOpts::from_cmdline(cmdline))); + 0 + }); + let app = RelmApp::from_app(main_app.clone()).with_broker(&BROKER); app.run::(AppInit { application: main_app, }); diff --git a/src/ui/app.rs b/src/ui/app.rs index e1d8199..5077c98 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -1,6 +1,7 @@ use super::about_dialog::AboutDialog; use super::alert::{alert, alert_w_widget}; use super::build_window::{BuildStatus, BuildWindow}; +use super::cmdline_opts::CmdLineOpts; use super::debug_view::{DebugView, DebugViewMsg}; use super::fbt_config_editor::{FbtConfigEditor, FbtConfigEditorInit, FbtConfigEditorMsg}; use super::job_worker::internal_worker::JobWorkerOut; @@ -136,6 +137,7 @@ pub enum Msg { DebugOpenPrefix, DebugOpenData, OpenWivrnConfig, + HandleCommandLine(CmdLineOpts), } impl App { @@ -736,6 +738,11 @@ impl SimpleComponent for App { editor.emit(WivrnConfEditorMsg::Present); self.wivrn_conf_editor = Some(editor); } + Msg::HandleCommandLine(opts) => { + if opts.start { + sender.input(Msg::DoStartStopXRService) + } + } } } diff --git a/src/ui/cmdline_opts.rs b/src/ui/cmdline_opts.rs new file mode 100644 index 0000000..c253fed --- /dev/null +++ b/src/ui/cmdline_opts.rs @@ -0,0 +1,29 @@ +use gtk4::{ + gio::{prelude::ApplicationExt, Application, ApplicationCommandLine}, + glib::{self, IsA}, +}; +use zoha_vte4::ApplicationCommandLineExt; + +#[derive(Debug, Clone)] +pub struct CmdLineOpts { + pub start: bool, +} + +impl CmdLineOpts { + pub fn init(app: &impl IsA) { + app.add_main_option( + "start", + glib::Char::try_from('S').unwrap(), + glib::OptionFlags::IN_MAIN, + glib::OptionArg::None, + "Start the XR Service right away", + None, + ); + } + + pub fn from_cmdline(cmdline: &ApplicationCommandLine) -> Self { + Self { + start: cmdline.options_dict().contains("start"), + } + } +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 99b0464..00baec8 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -2,6 +2,7 @@ mod about_dialog; mod alert; pub mod app; mod build_window; +pub mod cmdline_opts; mod debug_view; mod devices_box; mod factories;