Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix(runtime): tidy the menu and tray items added since 3.21.0

Tw93390c78d0d4a0
diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs
index e9377b51..8e319d37 100644
--- a/src-tauri/src/app/menu.rs
+++ b/src-tauri/src/app/menu.rs
@@ -4,13 +4,17 @@ 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 tauri::menu::{AboutMetadata, Menu, MenuItem, PredefinedMenuItem, Submenu};
+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,
@@ -19,7 +23,6 @@ pub struct MenuLabels {
pub show_all: &'static str,
pub quit: String,
pub file: &'static str,
- pub new_tab: &'static str,
pub new_window: &'static str,
pub close_window: &'static str,
pub print: &'static str,
@@ -53,7 +56,7 @@ pub struct MenuLabels {
pub zoom_window: &'static str,
pub always_on_top: &'static str,
pub help: &'static str,
- pub pake_help: String,
+ pub built_with_pake: &'static str,
}
impl MenuLabels {
@@ -67,7 +70,6 @@ impl MenuLabels {
show_all: "Show All",
quit: format!("Quit {product_name}"),
file: "File",
- new_tab: "New Tab",
new_window: "New Window",
close_window: "Close Window",
print: "Print…",
@@ -101,7 +103,7 @@ impl MenuLabels {
zoom_window: "Zoom",
always_on_top: "Always on Top",
help: "Help",
- pake_help: format!("{product_name} Help"),
+ built_with_pake: "Built with Pake",
},
UiLanguage::SimplifiedChinese => Self {
about: format!("关于 {product_name}"),
@@ -111,7 +113,6 @@ impl MenuLabels {
show_all: "全部显示",
quit: format!("退出 {product_name}"),
file: "文件",
- new_tab: "新建标签页",
new_window: "新建窗口",
close_window: "关闭窗口",
print: "打印…",
@@ -145,7 +146,7 @@ impl MenuLabels {
zoom_window: "缩放",
always_on_top: "窗口置顶",
help: "帮助",
- pake_help: format!("{product_name} 帮助"),
+ built_with_pake: "使用 Pake 构建",
},
UiLanguage::TraditionalChinese => Self {
about: format!("關於 {product_name}"),
@@ -155,7 +156,6 @@ impl MenuLabels {
show_all: "顯示全部",
quit: format!("結束 {product_name}"),
file: "檔案",
- new_tab: "新增分頁",
new_window: "新增視窗",
close_window: "關閉視窗",
print: "列印…",
@@ -189,7 +189,7 @@ impl MenuLabels {
zoom_window: "縮放",
always_on_top: "視窗置頂",
help: "輔助說明",
- pake_help: format!("{product_name} 輔助說明"),
+ built_with_pake: "使用 Pake 建置",
},
UiLanguage::German => Self {
about: format!("Über {product_name}"),
@@ -199,7 +199,6 @@ impl MenuLabels {
show_all: "Alle einblenden",
quit: format!("{product_name} beenden"),
file: "Ablage",
- new_tab: "Neuer Tab",
new_window: "Neues Fenster",
close_window: "Fenster schließen",
print: "Drucken…",
@@ -233,7 +232,7 @@ impl MenuLabels {
zoom_window: "Zoomen",
always_on_top: "Immer im Vordergrund",
help: "Hilfe",
- pake_help: format!("{product_name}-Hilfe"),
+ built_with_pake: "Erstellt mit Pake",
},
UiLanguage::French => Self {
about: format!("À propos de {product_name}"),
@@ -243,7 +242,6 @@ impl MenuLabels {
show_all: "Tout afficher",
quit: format!("Quitter {product_name}"),
file: "Fichier",
- new_tab: "Nouvel onglet",
new_window: "Nouvelle fenêtre",
close_window: "Fermer la fenêtre",
print: "Imprimer…",
@@ -277,7 +275,7 @@ impl MenuLabels {
zoom_window: "Réduire/agrandir",
always_on_top: "Toujours au premier plan",
help: "Aide",
- pake_help: format!("Aide {product_name}"),
+ built_with_pake: "Créé avec Pake",
},
}
}
@@ -356,13 +354,6 @@ fn file_menu(
) -> 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_tab",
- labels.new_tab,
- true,
- Some("CmdOrCtrl+T"),
- )?)?;
file_menu.append(&MenuItem::with_id(
app,
"new_window",
@@ -556,13 +547,16 @@ fn window_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Subme
Some(labels.zoom_window),
)?)?;
window_menu.append(&PredefinedMenuItem::separator(app)?)?;
- window_menu.append(&MenuItem::with_id(
+ 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,
@@ -576,7 +570,7 @@ fn help_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Submenu
let github_item = MenuItem::with_id(
app,
"pake_github_link",
- &labels.pake_help,
+ labels.built_with_pake,
true,
None::<&str>,
)?;
@@ -584,6 +578,15 @@ fn help_menu(app: &AppHandle<Wry>, labels: &MenuLabels) -> tauri::Result<Submenu
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));
+ }
+}
+
fn focused_webview_window(app_handle: &AppHandle) -> Option<WebviewWindow> {
let windows = app_handle.webview_windows();
windows
@@ -611,7 +614,7 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {
return;
}
match id {
- "new_tab" | "new_window" => {
+ "new_window" => {
open_additional_window_safe(app_handle);
}
"print" => {
@@ -711,6 +714,7 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {
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);
}
}
_ => {}
@@ -730,7 +734,8 @@ mod tests {
assert!(labels.quit.contains("Linear"));
assert!(!labels.about.contains("Pake"));
assert!(!labels.print.is_empty());
- assert!(!labels.new_tab.is_empty());
+ assert!(labels.built_with_pake.contains("Pake"));
+ assert!(!labels.built_with_pake.contains("Linear"));
assert!(!labels.clear_cache_restart.is_empty());
}
diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs
index 09c4b56f..0fdd9648 100644
--- a/src-tauri/src/app/setup.rs
+++ b/src-tauri/src/app/setup.rs
@@ -26,7 +26,6 @@ const TRAY_EXTENSION_ITEM_PREFIX: &str = "tray_extension_settings:";
#[cfg_attr(target_os = "macos", allow(dead_code))]
struct TrayLabels {
new_window: &'static str,
- show_or_hide: &'static str,
hide: &'static str,
show: &'static str,
always_on_top: &'static str,
@@ -40,7 +39,6 @@ impl TrayLabels {
match language {
UiLanguage::English => TrayLabels {
new_window: "New Window",
- show_or_hide: "Show / Hide",
hide: "Hide",
show: "Show",
always_on_top: "Always on Top",
@@ -50,7 +48,6 @@ impl TrayLabels {
},
UiLanguage::SimplifiedChinese => TrayLabels {
new_window: "新建窗口",
- show_or_hide: "显示 / 隐藏",
hide: "隐藏",
show: "显示",
always_on_top: "窗口置顶",
@@ -60,7 +57,6 @@ impl TrayLabels {
},
UiLanguage::TraditionalChinese => TrayLabels {
new_window: "新增視窗",
- show_or_hide: "顯示 / 隱藏",
hide: "隱藏",
show: "顯示",
always_on_top: "視窗置頂",
@@ -70,7 +66,6 @@ impl TrayLabels {
},
UiLanguage::German => TrayLabels {
new_window: "Neues Fenster",
- show_or_hide: "Ein- / Ausblenden",
hide: "Ausblenden",
show: "Einblenden",
always_on_top: "Immer im Vordergrund",
@@ -80,7 +75,6 @@ impl TrayLabels {
},
UiLanguage::French => TrayLabels {
new_window: "Nouvelle fenêtre",
- show_or_hide: "Afficher / Masquer",
hide: "Masquer",
show: "Afficher",
always_on_top: "Toujours au premier plan",
@@ -237,8 +231,6 @@ pub fn set_system_tray(
})
.unwrap_or(false);
- let toggle_window =
- MenuItemBuilder::with_id("tray_toggle_window", labels.show_or_hide).build(app)?;
let always_on_top =
CheckMenuItemBuilder::with_id("tray_always_on_top", labels.always_on_top)
.checked(initial_on_top)
@@ -273,13 +265,7 @@ pub fn set_system_tray(
if allow_multi_window {
builder = builder.item(&new_window);
}
- builder = builder.items(&[
- &toggle_window,
- &hide_app,
- &show_app,
- &sep_first,
- &always_on_top,
- ]);
+ builder = builder.items(&[&hide_app, &show_app, &sep_first, &always_on_top]);
#[cfg(target_os = "windows")]
if let Some(submenu) = &extensions_submenu {
builder = builder.item(submenu);
@@ -309,10 +295,6 @@ pub fn set_system_tray(
"tray_new_window" => {
open_additional_window_safe(app);
}
- "tray_toggle_window" => {
- cancel_startup_reveal(&menu_revealed);
- toggle_all_app_windows(app, _init_fullscreen);
- }
"hide_app" => {
// Hide every webview (main + multi-window clones), not only "pake".
cancel_startup_reveal(&menu_revealed);
@@ -462,7 +444,6 @@ mod tests {
for language in UiLanguage::all() {
let labels = TrayLabels::for_language(language);
assert!(!labels.new_window.is_empty());
- assert!(!labels.show_or_hide.is_empty());
assert!(!labels.hide.is_empty());
assert!(!labels.show.is_empty());
assert!(!labels.always_on_top.is_empty());
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 30c2d994..35e2bebd 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -542,6 +542,12 @@ pub fn run_app() {
Ok(())
})
.on_window_event(move |_window, _event| {
+ #[cfg(target_os = "macos")]
+ if let tauri::WindowEvent::Focused(true) = _event {
+ if let Some(webview) = _window.app_handle().get_webview_window(_window.label()) {
+ app::menu::sync_always_on_top_check(&webview);
+ }
+ }
if let tauri::WindowEvent::CloseRequested { api, .. } = _event {
if remember_url && _window.label() == "pake" {
save_last_url(_window.app_handle());