Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
// Menu functionality is only used on macOS; the module is gated in app/mod.rs.
use crate::app::navigation::{history_step, navigate_home, reload_window};
use crate::app::window::open_additional_window_safe;
use crate::util::{app_product_name, confirm_clear_cache_and_restart, UiLanguage};
use std::io::Write;
use std::process::{Command, Stdio};
use std::sync::OnceLock;
use tauri::menu::{AboutMetadata, CheckMenuItem, Menu, MenuItem, PredefinedMenuItem, Submenu};
use tauri::{AppHandle, Manager, WebviewWindow, Wry};
use tauri_plugin_opener::OpenerExt;
/// Menu ids of the Extensions menu start with this; the rest is the folder name.
const EXTENSION_ITEM_PREFIX: &str = "extension_settings:";
/// The Window menu's Always on Top item, whose check follows the focused window.
static ALWAYS_ON_TOP_ITEM: OnceLock<CheckMenuItem<Wry>> = OnceLock::new();
pub struct MenuLabels {
pub about: String,
pub services: &'static str,
pub hide: String,
pub hide_others: &'static str,
pub show_all: &'static str,
pub quit: String,
pub file: &'static str,
pub new_window: &'static str,
pub close_window: &'static str,
pub print: &'static str,
pub clear_cache_restart: &'static str,
pub edit: &'static str,
pub undo: &'static str,
pub redo: &'static str,
pub cut: &'static str,
pub copy: &'static str,
pub paste: &'static str,
pub paste_and_match_style: &'static str,
pub select_all: &'static str,
pub find: &'static str,
pub find_next: &'static str,
pub find_previous: &'static str,
pub copy_url: &'static str,
pub view: &'static str,
pub reload: &'static str,
pub zoom_in: &'static str,
pub zoom_out: &'static str,
pub zoom_reset: &'static str,
pub fullscreen: &'static str,
pub toggle_devtools: &'static str,
pub navigation: &'static str,
pub back: &'static str,
pub forward: &'static str,
pub go_home: &'static str,
pub extensions: &'static str,
pub window: &'static str,
pub minimize: &'static str,
pub zoom_window: &'static str,
pub always_on_top: &'static str,
pub help: &'static str,
pub built_with_pake: &'static str,
}
impl MenuLabels {
pub fn for_language(language: UiLanguage, product_name: &str) -> Self {
match language {
UiLanguage::English => Self {
about: format!("About {product_name}"),
services: "Services",
hide: format!("Hide {product_name}"),
hide_others: "Hide Others",
show_all: "Show All",
quit: format!("Quit {product_name}"),
file: "File",
new_window: "New Window",
close_window: "Close Window",
print: "Print…",
clear_cache_restart: "Clear Cache & Restart…",
edit: "Edit",
undo: "Undo",
redo: "Redo",
cut: "Cut",
copy: "Copy",
paste: "Paste",
paste_and_match_style: "Paste and Match Style",
select_all: "Select All",
find: "Find…",
find_next: "Find Next",
find_previous: "Find Previous",
copy_url: "Copy URL",
view: "View",
reload: "Reload",
zoom_in: "Zoom In",
zoom_out: "Zoom Out",
zoom_reset: "Actual Size",
fullscreen: "Toggle Full Screen",
toggle_devtools: "Toggle Developer Tools",
navigation: "Navigation",
back: "Back",
forward: "Forward",
go_home: "Go Home",
extensions: "Extensions",
window: "Window",
minimize: "Minimize",
zoom_window: "Zoom",
always_on_top: "Always on Top",
help: "Help",
built_with_pake: "Built with Pake",
},
UiLanguage::SimplifiedChinese => Self {
about: format!("关于 {product_name}"),
services: "服务",
hide: format!("隐藏 {product_name}"),
hide_others: "隐藏其他",
show_all: "全部显示",
quit: format!("退出 {product_name}"),
file: "文件",
new_window: "新建窗口",
close_window: "关闭窗口",
print: "打印…",
clear_cache_restart: "清除缓存并重启…",
edit: "编辑",
undo: "撤销",
redo: "重做",
cut: "剪切",
copy: "拷贝",
paste: "粘贴",
paste_and_match_style: "粘贴并匹配样式",
select_all: "全选",
find: "查找…",
find_next: "查找下一个",
find_previous: "查找上一个",
copy_url: "拷贝网址",
view: "显示",
reload: "重新加载",
zoom_in: "放大",
zoom_out: "缩小",
zoom_reset: "实际大小",
fullscreen: "切换全屏幕",
toggle_devtools: "切换开发者工具",
navigation: "导航",
back: "后退",
forward: "前进",
go_home: "返回主页",
extensions: "扩展",
window: "窗口",
minimize: "最小化",
zoom_window: "缩放",
always_on_top: "窗口置顶",
help: "帮助",
built_with_pake: "使用 Pake 构建",
},
UiLanguage::TraditionalChinese => Self {
about: format!("關於 {product_name}"),
services: "服務",
hide: format!("隱藏 {product_name}"),
hide_others: "隱藏其他",
show_all: "顯示全部",
quit: format!("結束 {product_name}"),
file: "檔案",
new_window: "新增視窗",
close_window: "關閉視窗",
print: "列印…",
clear_cache_restart: "清除快取並重新啟動…",
edit: "編輯",
undo: "還原",
redo: "重做",
cut: "剪下",
copy: "拷貝",
paste: "貼上",
paste_and_match_style: "貼上並符合樣式",
select_all: "全選",
find: "尋找…",
find_next: "尋找下一個",
find_previous: "尋找上一個",
copy_url: "拷貝網址",
view: "顯示方式",
reload: "重新載入",
zoom_in: "放大",
zoom_out: "縮小",
zoom_reset: "實際大小",
fullscreen: "切換全螢幕",
toggle_devtools: "切換開發者工具",
navigation: "導覽",
back: "上一頁",
forward: "下一頁",
go_home: "回到首頁",
extensions: "擴充功能",
window: "視窗",
minimize: "最小化",
zoom_window: "縮放",
always_on_top: "視窗置頂",
help: "輔助說明",
built_with_pake: "使用 Pake 建置",
},
UiLanguage::German => Self {
about: format!("Über {product_name}"),
services: "Dienste",
hide: format!("{product_name} ausblenden"),
hide_others: "Andere ausblenden",
show_all: "Alle einblenden",
quit: format!("{product_name} beenden"),
file: "Ablage",
new_window: "Neues Fenster",
close_window: "Fenster schließen",
print: "Drucken…",
clear_cache_restart: "Cache leeren und neu starten…",
edit: "Bearbeiten",
undo: "Widerrufen",
redo: "Wiederholen",
cut: "Ausschneiden",
copy: "Kopieren",
paste: "Einsetzen",
paste_and_match_style: "Einsetzen und Stil anpassen",
select_all: "Alles auswählen",
find: "Suchen…",
find_next: "Weitersuchen",
find_previous: "Rückwärts suchen",
copy_url: "Adresse kopieren",
view: "Darstellung",
reload: "Neu laden",
zoom_in: "Vergrößern",
zoom_out: "Verkleinern",
zoom_reset: "Originalgröße",
fullscreen: "Vollbild umschalten",
toggle_devtools: "Entwicklerwerkzeuge umschalten",
navigation: "Navigation",
back: "Zurück",
forward: "Vorwärts",
go_home: "Zur Startseite",
extensions: "Erweiterungen",
window: "Fenster",
minimize: "Im Dock ablegen",
zoom_window: "Zoomen",
always_on_top: "Immer im Vordergrund",
help: "Hilfe",
built_with_pake: "Erstellt mit Pake",
},
UiLanguage::French => Self {
about: format!("À propos de {product_name}"),
services: "Services",
hide: format!("Masquer {product_name}"),
hide_others: "Masquer les autres",
show_all: "Tout afficher",
quit: format!("Quitter {product_name}"),
file: "Fichier",
new_window: "Nouvelle fenêtre",
close_window: "Fermer la fenêtre",
print: "Imprimer…",
clear_cache_restart: "Vider le cache et redémarrer…",
edit: "Édition",
undo: "Annuler",
redo: "Rétablir",
cut: "Couper",
copy: "Copier",
paste: "Coller",
paste_and_match_style: "Coller et adapter le style",
select_all: "Tout sélectionner",
find: "Rechercher…",
find_next: "Rechercher le suivant",
find_previous: "Rechercher le précédent",
copy_url: "Copier l'adresse",
view: "Présentation",
reload: "Actualiser",
zoom_in: "Agrandir",
zoom_out: "Réduire",
zoom_reset: "Taille réelle",
fullscreen: "Basculer en plein écran",
toggle_devtools: "Afficher ou masquer les outils de développement",
navigation: "Navigation",
back: "Précédent",
forward: "Suivant",
go_home: "Page d'accueil",
extensions: "Extensions",
window: "Fenêtre",
minimize: "Placer dans le Dock",
zoom_window: "Réduire/agrandir",
always_on_top: "Toujours au premier plan",
help: "Aide",
built_with_pake: "Créé avec Pake",
},
}
}
}
pub fn set_app_menu(
app: &AppHandle<Wry>,
allow_multi_window: bool,
enable_find: bool,
extensions: &[crate::app::extensions::Installed],
) -> tauri::Result<()> {
let product_name = app_product_name(app);
let labels = MenuLabels::for_language(UiLanguage::system(), &product_name);
let window_submenu = window_menu(app, &labels)?;
let menu = Menu::with_items(
app,
&[
&app_menu(app, &product_name, &labels)?,
&file_menu(app, allow_multi_window, &labels)?,
&edit_menu(app, enable_find, &labels)?,
&view_menu(app, &labels)?,
&navigation_menu(app, &labels)?,
],
)?;
// Only an app with an extension that has settings to show gets the menu.
if extensions.iter().any(|extension| extension.page.is_some()) {
menu.append(&extensions_menu(app, extensions, &labels)?)?;
}
menu.append(&window_submenu)?;
menu.append(&help_menu(app, &labels)?)?;
app.set_menu(menu)?;
// AppKit injects Move & Resize, Fill, Center, Full Screen Tile, and
// window-cycling once the submenu is registered as the windows menu.
window_submenu.set_as_windows_menu_for_nsapp()?;
Ok(())
}
fn app_menu(
app: &AppHandle<Wry>,
product_name: &str,
labels: &MenuLabels,
) -> tauri::Result<Submenu<Wry>> {
let app_menu = Submenu::new(app, product_name, true)?;
let about_metadata = AboutMetadata {
name: Some(product_name.to_string()),
..AboutMetadata::default()
};
app_menu.append(&PredefinedMenuItem::about(
app,
Some(&labels.about),
Some(about_metadata),
)?)?;
app_menu.append(&PredefinedMenuItem::separator(app)?)?;
app_menu.append(&PredefinedMenuItem::services(app, Some(labels.services))?)?;
app_menu.append(&PredefinedMenuItem::separator(app)?)?;
app_menu.append(&PredefinedMenuItem::hide(app, Some(&labels.hide))?)?;
app_menu.append(&PredefinedMenuItem::hide_others(
app,
Some(labels.hide_others),
)?)?;
app_menu.append(&PredefinedMenuItem::show_all(app, Some(labels.show_all))?)?;
app_menu.append(&PredefinedMenuItem::separator(app)?)?;
app_menu.append(&PredefinedMenuItem::quit(app, Some(&labels.quit))?)?;
Ok(app_menu)
}
fn file_menu(
app: &AppHandle<Wry>,
allow_multi_window: bool,
labels: &MenuLabels,
) -> tauri::Result<Submenu<Wry>> {
let file_menu = Submenu::new(app, labels.file, true)?;
if allow_multi_window {
file_menu.append(&MenuItem::with_id(
app,
"new_window",
labels.new_window,
true,
Some("CmdOrCtrl+N"),
)?)?;
file_menu.append(&PredefinedMenuItem::separator(app)?)?;
}
file_menu.append(&PredefinedMenuItem::close_window(
app,
Some(labels.close_window),
)?)?;
file_menu.append(&PredefinedMenuItem::separator(app)?)?;
file_menu.append(&MenuItem::with_id(
app,
"print",
labels.print,
true,
Some("CmdOrCtrl+P"),
)?)?;
file_menu.append(&PredefinedMenuItem::separator(app)?)?;
file_menu.append(&MenuItem::with_id(
app,
"clear_cache_restart",
labels.clear_cache_restart,
true,
Some("CmdOrCtrl+Shift+Backspace"),
)?)?;
Ok(file_menu)
}
fn edit_menu(
app: &AppHandle<Wry>,
enable_find: bool,
labels: &MenuLabels,
) -> tauri::Result<Submenu<Wry>> {
let edit_menu = Submenu::new(app, labels.edit, true)?;
edit_menu.append(&PredefinedMenuItem::undo(app, Some(labels.undo))?)?;
edit_menu.append(&PredefinedMenuItem::redo(app, Some(labels.redo))?)?;
edit_menu.append(&PredefinedMenuItem::separator(app)?)?;
edit_menu.append(&PredefinedMenuItem::cut(app, Some(labels.cut))?)?;
edit_menu.append(&PredefinedMenuItem::copy(app, Some(labels.copy))?)?;
edit_menu.append(&PredefinedMenuItem::paste(app, Some(labels.paste))?)?;
edit_menu.append(&MenuItem::with_id(
app,
"paste_and_match_style",
labels.paste_and_match_style,
true,
Some("CmdOrCtrl+Shift+Option+V"),
)?)?;
edit_menu.append(&PredefinedMenuItem::select_all(
app,
Some(labels.select_all),
)?)?;
edit_menu.append(&PredefinedMenuItem::separator(app)?)?;
if enable_find {
edit_menu.append(&MenuItem::with_id(
app,
"find",
labels.find,
true,
Some("CmdOrCtrl+F"),
)?)?;
edit_menu.append(&MenuItem::with_id(
app,
"find_next",
labels.find_next,
true,
Some("CmdOrCtrl+G"),
)?)?;
edit_menu.append(&MenuItem::with_id(
app,
"find_previous",
labels.find_previous,
true,
Some("CmdOrCtrl+Shift+G"),
)?)?;
edit_menu.append(&PredefinedMenuItem::separator(app)?)?;
}
edit_menu.append(&MenuItem::with_id(
app,
"copy_url",
labels.copy_url,
true,
Some("CmdOrCtrl+L"),
)?)?;
Ok(edit_menu)
}
fn view_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Submenu<Wry>> {
let view_menu = Submenu::new(app, labels.view, true)?;
view_menu.append(&MenuItem::with_id(
app,
"reload",
labels.reload,
true,
Some("CmdOrCtrl+R"),
)?)?;
view_menu.append(&PredefinedMenuItem::separator(app)?)?;
view_menu.append(&MenuItem::with_id(
app,
"zoom_in",
labels.zoom_in,
true,
Some("CmdOrCtrl+="),
)?)?;
view_menu.append(&MenuItem::with_id(
app,
"zoom_out",
labels.zoom_out,
true,
Some("CmdOrCtrl+-"),
)?)?;
view_menu.append(&MenuItem::with_id(
app,
"zoom_reset",
labels.zoom_reset,
true,
Some("CmdOrCtrl+0"),
)?)?;
view_menu.append(&PredefinedMenuItem::separator(app)?)?;
view_menu.append(&PredefinedMenuItem::fullscreen(
app,
Some(labels.fullscreen),
)?)?;
view_menu.append(&PredefinedMenuItem::separator(app)?)?;
view_menu.append(&MenuItem::with_id(
app,
"toggle_devtools",
labels.toggle_devtools,
cfg!(debug_assertions),
Some("CmdOrCtrl+Option+I"),
)?)?;
Ok(view_menu)
}
fn navigation_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Submenu<Wry>> {
let navigation_menu = Submenu::new(app, labels.navigation, true)?;
navigation_menu.append(&MenuItem::with_id(
app,
"go_back",
labels.back,
true,
Some("CmdOrCtrl+["),
)?)?;
navigation_menu.append(&MenuItem::with_id(
app,
"go_forward",
labels.forward,
true,
Some("CmdOrCtrl+]"),
)?)?;
navigation_menu.append(&MenuItem::with_id(
app,
"go_home",
labels.go_home,
true,
Some("CmdOrCtrl+Shift+H"),
)?)?;
Ok(navigation_menu)
}
/// One item per extension, opening the page where its settings are.
fn extensions_menu(
app: &AppHandle<Wry>,
extensions: &[crate::app::extensions::Installed],
labels: &MenuLabels,
) -> tauri::Result<Submenu<Wry>> {
let menu = Submenu::new(app, labels.extensions, true)?;
for extension in extensions
.iter()
.filter(|extension| extension.page.is_some())
{
menu.append(&MenuItem::with_id(
app,
format!("{EXTENSION_ITEM_PREFIX}{}", extension.id),
&extension.name,
true,
None::<&str>,
)?)?;
}
Ok(menu)
}
fn window_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Submenu<Wry>> {
let window_menu = Submenu::new(app, labels.window, true)?;
window_menu.append(&PredefinedMenuItem::minimize(app, Some(labels.minimize))?)?;
window_menu.append(&PredefinedMenuItem::maximize(
app,
Some(labels.zoom_window),
)?)?;
window_menu.append(&PredefinedMenuItem::separator(app)?)?;
let always_on_top = CheckMenuItem::with_id(
app,
"always_on_top",
labels.always_on_top,
true,
false,
None::<&str>,
)?;
window_menu.append(&always_on_top)?;
let _ = ALWAYS_ON_TOP_ITEM.set(always_on_top);
window_menu.append(&PredefinedMenuItem::separator(app)?)?;
window_menu.append(&PredefinedMenuItem::close_window(
app,
Some(labels.close_window),
)?)?;
Ok(window_menu)
}
fn help_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Submenu<Wry>> {
let help_menu = Submenu::new(app, labels.help, true)?;
let github_item = MenuItem::with_id(
app,
"pake_github_link",
labels.built_with_pake,
true,
None::<&str>,
)?;
help_menu.append(&github_item)?;
Ok(help_menu)
}
/// Shows whether `window` stays on top in the Window menu. AppKit flips the
/// check itself on a click, so this also corrects it when the toggle failed,
/// and runs again whenever another window takes focus.
pub fn sync_always_on_top_check(window: &WebviewWindow) {
if let Some(item) = ALWAYS_ON_TOP_ITEM.get() {
let _ = item.set_checked(window.is_always_on_top().unwrap_or(false));
}
crate::app::setup::sync_tray_always_on_top_check(window);
}
fn focused_webview_window(app_handle: &AppHandle) -> Option<WebviewWindow> {
let windows = app_handle.webview_windows();
windows
.values()
.find(|window| window.is_focused().unwrap_or(false))
.cloned()
.or_else(|| windows.get("pake").cloned())
}
/// Copy text via pbcopy so it works when the page has no JS context (error
/// shells) and when the Clipboard API is blocked by the site CSP.
fn copy_text_to_pasteboard(text: &str) {
let Ok(mut child) = Command::new("pbcopy").stdin(Stdio::piped()).spawn() else {
return;
};
if let Some(stdin) = child.stdin.as_mut() {
let _ = stdin.write_all(text.as_bytes());
}
let _ = child.wait();
}
pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {
if let Some(extension) = id.strip_prefix(EXTENSION_ITEM_PREFIX) {
crate::app::extensions::open_page(extension);
return;
}
match id {
"new_window" => {
open_additional_window_safe(app_handle);
}
"print" => {
if let Some(window) = focused_webview_window(app_handle) {
if window.print().is_err() {
let _ = window.eval("window.print()");
}
}
}
"pake_github_link" => {
let _ = app_handle
.opener()
.open_url("https://github.com/tw93/Pake", None::<&str>);
}
"reload" => {
if let Some(window) = focused_webview_window(app_handle) {
// Native reload works on blank error pages where eval cannot.
reload_window(&window);
}
}
"toggle_devtools" => {
#[cfg(debug_assertions)] // Only allow in debug builds
if let Some(window) = focused_webview_window(app_handle) {
if window.is_devtools_open() {
window.close_devtools();
} else {
window.open_devtools();
}
}
}
"zoom_in" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("zoomIn()");
}
}
"zoom_out" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("zoomOut()");
}
}
"zoom_reset" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("setZoom('100%')");
}
}
"go_back" => {
if let Some(window) = focused_webview_window(app_handle) {
history_step(&window, true);
}
}
"go_forward" => {
if let Some(window) = focused_webview_window(app_handle) {
history_step(&window, false);
}
}
"go_home" => {
if let Some(window) = focused_webview_window(app_handle) {
// Native navigation works even from a blank error page (where
// eval cannot run) and resolves local-file apps to the correct
// bundled asset instead of a path on the current origin.
navigate_home(&window);
}
}
"copy_url" => {
if let Some(window) = focused_webview_window(app_handle) {
// Prefer the native webview URL so copy still works on error
// pages that have no document.location / clipboard API.
if let Some(url) = crate::app::window::page_url(&window) {
copy_text_to_pasteboard(url.as_str());
}
}
}
"paste_and_match_style" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("triggerPasteAsPlainText()");
}
}
"find" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("window.pakeFind?.open()");
}
}
"find_next" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("window.pakeFind?.next()");
}
}
"find_previous" => {
if let Some(window) = focused_webview_window(app_handle) {
let _ = window.eval("window.pakeFind?.previous()");
}
}
"clear_cache_restart" => {
confirm_clear_cache_and_restart(app_handle);
}
"always_on_top" => {
if let Some(window) = focused_webview_window(app_handle) {
let is_on_top = window.is_always_on_top().unwrap_or(false);
let _ = window.set_always_on_top(!is_on_top);
sync_always_on_top_check(&window);
}
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn macos_menu_labels_localize_all_five_languages_and_use_product_name() {
for language in UiLanguage::all() {
let labels = MenuLabels::for_language(language, "Linear");
assert!(labels.about.contains("Linear"));
assert!(labels.hide.contains("Linear"));
assert!(labels.quit.contains("Linear"));
assert!(!labels.about.contains("Pake"));
assert!(!labels.print.is_empty());
assert!(labels.built_with_pake.contains("Pake"));
assert!(!labels.built_with_pake.contains("Linear"));
assert!(!labels.clear_cache_restart.is_empty());
}
let tw = MenuLabels::for_language(UiLanguage::TraditionalChinese, "Notion");
assert_eq!(tw.print, "列印…");
assert_eq!(tw.window, "視窗");
assert_eq!(tw.clear_cache_restart, "清除快取並重新啟動…");
let de = MenuLabels::for_language(UiLanguage::German, "Notion");
assert_eq!(de.file, "Ablage");
assert_eq!(de.print, "Drucken…");
let fr = MenuLabels::for_language(UiLanguage::French, "Notion");
assert_eq!(fr.file, "Fichier");
assert_eq!(fr.print, "Imprimer…");
}
}