Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating verso without panel from controller #268

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions verso/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,49 @@ use versoview_messages::ControllerMessage;

use ipc_channel::ipc::{IpcOneShotServer, IpcSender};

#[derive(Debug, Default)]
pub struct VersoviewSettings {
pub with_panel: bool,
}

pub struct VersoviewController(IpcSender<ControllerMessage>);

impl VersoviewController {
/// Create a new verso instance and get the controller to it
pub fn new(verso_path: impl AsRef<Path>, initial_url: url::Url) -> Self {
/// Create a new verso instance with settings and get the controller to it
fn create(
verso_path: impl AsRef<Path>,
initial_url: url::Url,
settings: VersoviewSettings,
) -> Self {
let path = verso_path.as_ref();
let (server, server_name) =
IpcOneShotServer::<IpcSender<ControllerMessage>>::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 !settings.with_panel {
command.arg("--no-panel");
}
command.spawn().unwrap();
let (_, sender) = server.accept().unwrap();
Self(sender)
}

/// Create a new verso instance with default settings and get the controller to it
pub fn new(verso_path: impl AsRef<Path>, initial_url: url::Url) -> Self {
Self::create(verso_path, initial_url, VersoviewSettings::default())
}

/// Create a new verso instance with custom settings and get the controller to it
pub fn new_with_settings(
verso_path: impl AsRef<Path>,
initial_url: url::Url,
settings: VersoviewSettings,
) -> Self {
Self::create(verso_path, initial_url, settings)
}

/// Navigate to url
pub fn navigate(&self, url: url::Url) -> Result<(), Box<ipc_channel::ErrorKind>> {
self.0.send(ControllerMessage::NavigateTo(url))
Expand Down
8 changes: 7 additions & 1 deletion verso/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(
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
Expand Down
Loading