From 787f6550b824259438ac034c19f100be8bba99be Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 20 Dec 2022 23:43:55 -0700 Subject: [PATCH] macos: link to helpful resources from Help menu --- wezterm-gui/Cargo.toml | 2 +- wezterm-gui/src/commands.rs | 24 ++++++++++++++++++++++++ wezterm-gui/src/frontend.rs | 7 +++++++ wezterm-gui/src/termwindow/mod.rs | 8 ++++++++ window/examples/async.rs | 1 + window/src/connection.rs | 2 ++ window/src/lib.rs | 2 ++ window/src/os/macos/app.rs | 5 +++++ window/src/os/macos/menu.rs | 1 + window/src/os/macos/window.rs | 8 ++++++++ 10 files changed, 59 insertions(+), 1 deletion(-) diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.toml index fddae39c7..009b30c7a 100644 --- a/wezterm-gui/Cargo.toml +++ b/wezterm-gui/Cargo.toml @@ -65,7 +65,7 @@ metrics = { version="0.17", features=["std"]} mlua = {version="0.8.3", features=["send"]} mux = { path = "../mux" } mux-lua = { path = "../lua-api-crates/mux" } -open = "3.0" +open = "3.2" ordered-float = "3.0" parking_lot = "0.12" portable-pty = { path = "../pty", features = ["serde_support"]} diff --git a/wezterm-gui/src/commands.rs b/wezterm-gui/src/commands.rs index 8300b3415..59954eb26 100644 --- a/wezterm-gui/src/commands.rs +++ b/wezterm-gui/src/commands.rs @@ -235,6 +235,30 @@ impl CommandDef { main_menu.add_item(&help_item); help_item.set_sub_menu(&help_menu); help_menu.assign_as_help_menu(); + + for (caption, url) in [ + ("Documentation", "https://wezfurlong.org/wezterm/"), + /* Omitted because the underlying `open` utility rewrites the `#` to `%23` + * and matrix's URL parser doesn't like that, despite it being a core + * part of how URLs work. + ( + "Discuss in realtime on Matrix", + "https://matrix.to/#/#wezterm:matrix.org", + ), + */ + ( + "Discuss on GitHub", + "https://github.com/wez/wezterm/discussions", + ), + ( + "Search or report issue on GitHub", + "https://github.com/wez/wezterm/issues", + ), + ] { + let item = MenuItem::new_with(caption, Some(sel!(weztermPerformKeyAssignment:)), ""); + item.set_represented_item(RepresentedItem::OpenInBrowser(url.to_string())); + help_menu.add_item(&item); + } } } diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs index 68bf99df2..7272c9f76 100644 --- a/wezterm-gui/src/frontend.rs +++ b/wezterm-gui/src/frontend.rs @@ -213,6 +213,13 @@ impl GuiFrontEnd { // future. log::info!("perform: {action:?}"); } + ApplicationEvent::OpenInBrowser(url) => { + std::thread::spawn(move || { + if let Err(err) = open::that(&url) { + log::error!("Error opening {}: {:#}", url, err); + } + }); + } } } diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 32973c8aa..7492816a5 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -879,6 +879,14 @@ impl TermWindow { } Ok(true) } + WindowEvent::OpenInBrowser(url) => { + std::thread::spawn(move || { + if let Err(err) = open::that(&url) { + log::error!("Error opening {}: {:#}", url, err); + } + }); + Ok(true) + } WindowEvent::FocusChanged(focused) => { self.focus_changed(focused, window); Ok(true) diff --git a/window/examples/async.rs b/window/examples/async.rs index 50afe6784..24a218d38 100644 --- a/window/examples/async.rs +++ b/window/examples/async.rs @@ -85,6 +85,7 @@ impl MyWindow { | WindowEvent::DraggedFile(_) | WindowEvent::DroppedFile(_) | WindowEvent::PerformKeyAssignment(_) + | WindowEvent::OpenInBrowser(_) | WindowEvent::MouseLeave => {} } } diff --git a/window/src/connection.rs b/window/src/connection.rs index 4fdd10dbd..288e79e81 100644 --- a/window/src/connection.rs +++ b/window/src/connection.rs @@ -24,6 +24,8 @@ pub enum ApplicationEvent { /// The system wants to open a command in the terminal OpenCommandScript(String), PerformKeyAssignment(KeyAssignment), + /// A request to open a URL in the browser + OpenInBrowser(String), } pub trait ConnectionOps { diff --git a/window/src/lib.rs b/window/src/lib.rs index 24dd53992..718805c6a 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -189,6 +189,8 @@ pub enum WindowEvent { /// Called by menubar dispatching stuff on some systems PerformKeyAssignment(config::keyassignment::KeyAssignment), + /// Called by menubar dispatching stuff on some systems + OpenInBrowser(String), } pub struct WindowEventSender { diff --git a/window/src/os/macos/app.rs b/window/src/os/macos/app.rs index 8f5f0e085..c6e34b2c4 100644 --- a/window/src/os/macos/app.rs +++ b/window/src/os/macos/app.rs @@ -53,6 +53,11 @@ extern "C" fn wezterm_perform_key_assignment( conn.dispatch_app_event(ApplicationEvent::PerformKeyAssignment(action)); } } + Some(RepresentedItem::OpenInBrowser(url)) => { + if let Some(conn) = Connection::get() { + conn.dispatch_app_event(ApplicationEvent::OpenInBrowser(url)); + } + } None => {} } } diff --git a/window/src/os/macos/menu.rs b/window/src/os/macos/menu.rs index 064206871..8f1e550f7 100644 --- a/window/src/os/macos/menu.rs +++ b/window/src/os/macos/menu.rs @@ -111,6 +111,7 @@ pub struct MenuItem { #[derive(Clone, Debug)] pub enum RepresentedItem { KeyAssignment(KeyAssignment), + OpenInBrowser(String), } impl MenuItem { diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index ab6f5b99c..6c5cb1483 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -1922,6 +1922,14 @@ impl WindowView { .dispatch(WindowEvent::PerformKeyAssignment(action)); } } + Some(RepresentedItem::OpenInBrowser(url)) => { + if let Some(this) = Self::get_this(this) { + this.inner + .borrow_mut() + .events + .dispatch(WindowEvent::OpenInBrowser(url)); + } + } None => {} } }