From 21ad84ca945ef7d538ceb409535c2750b8deee76 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 20 Jan 2025 16:56:25 +0800 Subject: [PATCH 1/2] Allow creating verso without panel from controller --- verso/src/lib.rs | 23 ++++++++++++++++++----- verso/src/main.rs | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/verso/src/lib.rs b/verso/src/lib.rs index 6ab21f86..8f3688ff 100644 --- a/verso/src/lib.rs +++ b/verso/src/lib.rs @@ -7,19 +7,32 @@ pub struct VersoviewController(IpcSender); impl VersoviewController { /// Create a new verso instance and get the controller to it - pub fn new(verso_path: impl AsRef, initial_url: url::Url) -> Self { + fn create(verso_path: impl AsRef, initial_url: url::Url, with_panel: bool) -> Self { let path = verso_path.as_ref(); let (server, server_name) = IpcOneShotServer::>::new().unwrap(); - Command::new(path) + let mut command = Command::new(path); + command .arg(format!("--ipc-channel={server_name}")) - .arg(format!("--url={initial_url}")) - .spawn() - .unwrap(); + .arg(format!("--url={initial_url}")); + if !with_panel { + command.arg("--no-panel"); + } + command.spawn().unwrap(); let (_, sender) = server.accept().unwrap(); Self(sender) } + /// Create a new verso instance and get the controller to it + pub fn new(verso_path: impl AsRef, initial_url: url::Url) -> Self { + Self::create(verso_path, initial_url, false) + } + + /// Create a new verso instance with the default panel and get the controller to it + pub fn new_with_panel(verso_path: impl AsRef, initial_url: url::Url) -> Self { + Self::create(verso_path, initial_url, true) + } + /// Navigate to url pub fn navigate(&self, url: url::Url) -> Result<(), Box> { self.0.send(ControllerMessage::NavigateTo(url)) diff --git a/verso/src/main.rs b/verso/src/main.rs index 188191d5..8ee7e7e2 100644 --- a/verso/src/main.rs +++ b/verso/src/main.rs @@ -2,7 +2,7 @@ use std::{env::current_exe, thread::sleep, time::Duration}; fn main() { let versoview_path = current_exe().unwrap().parent().unwrap().join("versoview"); - let controller = verso::VersoviewController::new( + let controller = verso::VersoviewController::new_with_panel( versoview_path, url::Url::parse("https://example.com").unwrap(), ); From 0d3c219478a2ee27512e3bd0a69b6633fa9aa4f4 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 22 Jan 2025 18:54:30 +0800 Subject: [PATCH 2/2] Use settings for future expansions --- verso/src/lib.rs | 29 +++++++++++++++++++++-------- verso/src/main.rs | 8 +++++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/verso/src/lib.rs b/verso/src/lib.rs index 8f3688ff..c8b522e5 100644 --- a/verso/src/lib.rs +++ b/verso/src/lib.rs @@ -3,11 +3,20 @@ use versoview_messages::ControllerMessage; use ipc_channel::ipc::{IpcOneShotServer, IpcSender}; +#[derive(Debug, Default)] +pub struct VersoviewSettings { + pub with_panel: bool, +} + pub struct VersoviewController(IpcSender); impl VersoviewController { - /// Create a new verso instance and get the controller to it - fn create(verso_path: impl AsRef, initial_url: url::Url, with_panel: bool) -> Self { + /// Create a new verso instance with settings and get the controller to it + fn create( + verso_path: impl AsRef, + initial_url: url::Url, + settings: VersoviewSettings, + ) -> Self { let path = verso_path.as_ref(); let (server, server_name) = IpcOneShotServer::>::new().unwrap(); @@ -15,7 +24,7 @@ impl VersoviewController { command .arg(format!("--ipc-channel={server_name}")) .arg(format!("--url={initial_url}")); - if !with_panel { + if !settings.with_panel { command.arg("--no-panel"); } command.spawn().unwrap(); @@ -23,14 +32,18 @@ impl VersoviewController { Self(sender) } - /// Create a new verso instance and get the controller to it + /// Create a new verso instance with default settings and get the controller to it pub fn new(verso_path: impl AsRef, initial_url: url::Url) -> Self { - Self::create(verso_path, initial_url, false) + Self::create(verso_path, initial_url, VersoviewSettings::default()) } - /// Create a new verso instance with the default panel and get the controller to it - pub fn new_with_panel(verso_path: impl AsRef, initial_url: url::Url) -> Self { - Self::create(verso_path, initial_url, true) + /// Create a new verso instance with custom settings and get the controller to it + pub fn new_with_settings( + verso_path: impl AsRef, + initial_url: url::Url, + settings: VersoviewSettings, + ) -> Self { + Self::create(verso_path, initial_url, settings) } /// Navigate to url diff --git a/verso/src/main.rs b/verso/src/main.rs index 8ee7e7e2..4e7627b4 100644 --- a/verso/src/main.rs +++ b/verso/src/main.rs @@ -1,10 +1,16 @@ use std::{env::current_exe, thread::sleep, time::Duration}; +use verso::VersoviewSettings; + fn main() { let versoview_path = current_exe().unwrap().parent().unwrap().join("versoview"); - let controller = verso::VersoviewController::new_with_panel( + let controller = verso::VersoviewController::new_with_settings( versoview_path, url::Url::parse("https://example.com").unwrap(), + VersoviewSettings { + with_panel: true, + ..Default::default() + }, ); sleep(Duration::from_secs(10)); dbg!(controller