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.

feat: Ctrl+Shift+H goes to the home page on Windows and Linux (#1440)

Hirun Thishakyaeb320dc515ab
diff --git a/docs/shortcuts.md b/docs/shortcuts.md
index f1530ace..c5b89f55 100644
--- a/docs/shortcuts.md
+++ b/docs/shortcuts.md
@@ -22,7 +22,7 @@ Every app Pake builds has these shortcuts, on top of the ones the website itself
| `⌘ 0` | `Ctrl 0` | Reset zoom |
| `⌘ L` | `Ctrl L` | Copy the current page URL |
| `⌘ ⇧ ⌥ V` | Mac only | Paste and match style |
-| `⌘ ⇧ H` | Mac only | Go to the home page |
+| `⌘ ⇧ H` | `Ctrl Shift H` | Go to the home page |
| `⌘ ⇧ ⌫` | Mac only | Clear the cache and restart |
| `⌃ ⌘ F` | `F11` | Toggle fullscreen |
| `⌘ ⌥ I` | `Ctrl Shift I` | Toggle developer tools (debug only) |
diff --git a/docs/shortcuts_CN.md b/docs/shortcuts_CN.md
index 6ed2170c..4c675a2b 100644
--- a/docs/shortcuts_CN.md
+++ b/docs/shortcuts_CN.md
@@ -22,7 +22,7 @@ Pake 打包出来的每个应用都带下面这些快捷键,网站自己定义
| `⌘ 0` | `Ctrl 0` | 重置缩放 |
| `⌘ L` | `Ctrl L` | 复制当前页面网址 |
| `⌘ ⇧ ⌥ V` | 仅 Mac | 粘贴并匹配样式 |
-| `⌘ ⇧ H` | 仅 Mac | 回到首页 |
+| `⌘ ⇧ H` | `Ctrl Shift H` | 回到首页 |
| `⌘ ⇧ ⌫` | 仅 Mac | 清除缓存并重启 |
| `⌃ ⌘ F` | `F11` | 切换全屏 |
| `⌘ ⌥ I` | `Ctrl Shift I` | 开关开发者工具(仅调试版) |
diff --git a/src-tauri/src/app/invoke.rs b/src-tauri/src/app/invoke.rs
index 6eb87763..2d0db724 100644
--- a/src-tauri/src/app/invoke.rs
+++ b/src-tauri/src/app/invoke.rs
@@ -1,4 +1,4 @@
-use crate::app::navigation::{history_step, reload_window};
+use crate::app::navigation::{history_step, navigate_home, reload_window};
use crate::app::notification::{self, NotificationOutcome, NotificationParams};
use crate::util::{
check_file_or_append, get_download_dir, get_download_message_with_lang,
@@ -547,8 +547,12 @@ pub fn webview_navigate(window: WebviewWindow, action: String) -> Result<(), Str
history_step(&window, false);
Ok(())
}
+ "home" => {
+ navigate_home(&window);
+ Ok(())
+ }
other => Err(format!(
- "Unknown webview_navigate action '{other}' (expected reload|back|forward)"
+ "Unknown webview_navigate action '{other}' (expected reload|back|forward|home)"
)),
}
}
diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs
index 8086f922..ecb842fd 100644
--- a/src-tauri/src/app/menu.rs
+++ b/src-tauri/src/app/menu.rs
@@ -1,5 +1,5 @@
// Menu functionality is only used on macOS; the module is gated in app/mod.rs.
-use crate::app::navigation::{history_step, reload_window};
+use crate::app::navigation::{history_step, navigate_home, reload_window};
use crate::app::window::{open_additional_window_safe, MultiWindowState};
use std::io::Write;
use std::process::{Command, Stdio};
@@ -256,27 +256,6 @@ fn help_menu(app: &AppHandle<Wry>, title: &str) -> tauri::Result<Submenu<Wry>> {
Ok(help_menu)
}
-// Resolve the app's real home URL from its window config. Split out from
-// `home_url` so the mapping can be unit-tested without an AppHandle.
-fn resolve_home_url(url_type: &str, url: &str) -> Option<tauri::Url> {
- match url_type {
- // A web app's configured url is already absolute.
- "web" => tauri::Url::parse(url).ok(),
- // A local-file app's url is only a basename; Tauri serves bundled assets
- // from tauri://localhost on macOS (this menu is macOS-only). Resolving it
- // against the currently loaded remote origin (as the old eval path did)
- // would point at the wrong server.
- "local" => tauri::Url::parse(&format!("tauri://localhost/{url}")).ok(),
- _ => None,
- }
-}
-
-fn home_url(app: &AppHandle) -> Option<tauri::Url> {
- let state = app.try_state::<MultiWindowState>()?;
- let window_config = state.pake_config.windows.first()?;
- resolve_home_url(&window_config.url_type, &window_config.url)
-}
-
fn focused_webview_window(app_handle: &AppHandle) -> Option<WebviewWindow> {
let windows = app_handle.webview_windows();
windows
@@ -358,14 +337,7 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {
// 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.
- match home_url(app_handle) {
- Some(url) => {
- let _ = window.navigate(url);
- }
- None => {
- let _ = window.eval("window.location.href = window.pakeConfig.url");
- }
- }
+ navigate_home(&window);
}
}
"copy_url" => {
@@ -413,31 +385,3 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {
_ => {}
}
}
-
-#[cfg(test)]
-mod tests {
- use super::resolve_home_url;
-
- #[test]
- fn web_url_passes_through_unchanged() {
- assert_eq!(
- resolve_home_url("web", "https://github.com").map(|u| u.to_string()),
- Some("https://github.com/".to_string())
- );
- }
-
- #[test]
- fn local_basename_resolves_to_bundled_asset_url() {
- // The fix: a local app's basename must become the bundled asset URL,
- // not a path resolved against whatever origin is currently loaded.
- assert_eq!(
- resolve_home_url("local", "launcher.html").map(|u| u.to_string()),
- Some("tauri://localhost/launcher.html".to_string())
- );
- }
-
- #[test]
- fn unknown_url_type_is_none() {
- assert!(resolve_home_url("bogus", "whatever").is_none());
- }
-}
diff --git a/src-tauri/src/app/navigation.rs b/src-tauri/src/app/navigation.rs
index 2c4ab75e..2cc50b0b 100644
--- a/src-tauri/src/app/navigation.rs
+++ b/src-tauri/src/app/navigation.rs
@@ -2,7 +2,8 @@
//! Blank error shells have no JS context, so `eval("history.back()")` and
//! `location.reload()` are dead exactly when the user is stuck.
-use tauri::WebviewWindow;
+use crate::app::window::MultiWindowState;
+use tauri::{Manager, WebviewWindow};
/// Reload the current document via the native webview API.
pub fn reload_window(window: &WebviewWindow) {
@@ -11,6 +12,49 @@ pub fn reload_window(window: &WebviewWindow) {
}
}
+/// Resolve the app's real home URL from its window config. Split out from
+/// `home_url` so the mapping can be unit-tested without an AppHandle.
+pub fn resolve_home_url(url_type: &str, url: &str) -> Option<tauri::Url> {
+ match url_type {
+ // A web app's configured url is already absolute.
+ "web" => tauri::Url::parse(url).ok(),
+ // A local-file app's url is only a basename; Tauri serves bundled assets
+ // from tauri://localhost on macOS/Linux and http://tauri.localhost on Windows.
+ // Resolving it against the currently loaded remote origin (as the old eval path did)
+ // would point at the wrong server.
+ "local" => {
+ #[cfg(windows)]
+ let base = "http://tauri.localhost";
+ #[cfg(not(windows))]
+ let base = "tauri://localhost";
+ tauri::Url::parse(&format!("{base}/{url}")).ok()
+ }
+ _ => None,
+ }
+}
+
+pub fn home_url(app: &tauri::AppHandle) -> Option<tauri::Url> {
+ let state = app.try_state::<MultiWindowState>()?;
+ let window_config = state.pake_config.windows.first()?;
+ resolve_home_url(&window_config.url_type, &window_config.url)
+}
+
+/// Navigate the webview to the packaged home URL. Native navigation works
+/// from a blank error page, where eval cannot run, and sends a local-file app
+/// to its bundled asset instead of a path on the current origin.
+pub fn navigate_home(window: &WebviewWindow) {
+ match home_url(window.app_handle()) {
+ Some(url) => {
+ if let Err(error) = window.navigate(url) {
+ eprintln!("[Pake] Failed to navigate to home URL: {error}");
+ }
+ }
+ None => {
+ let _ = window.eval("window.location.href = window.pakeConfig.url");
+ }
+ }
+}
+
/// Step the webview history without requiring a live page JS context.
pub fn history_step(window: &WebviewWindow, back: bool) {
let stepped = window.with_webview(move |webview| {
@@ -121,3 +165,35 @@ fn history_step_platform(webview: &tauri::webview::PlatformWebview, back: bool)
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
fn history_step_platform(_webview: &tauri::webview::PlatformWebview, _back: bool) {}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn home_url_resolution() {
+ assert_eq!(
+ resolve_home_url("web", "https://github.com").map(|u| u.to_string()),
+ Some("https://github.com/".to_string())
+ );
+ #[cfg(not(windows))]
+ assert_eq!(
+ resolve_home_url("local", "launcher.html").map(|u| u.to_string()),
+ Some("tauri://localhost/launcher.html".to_string())
+ );
+ #[cfg(windows)]
+ assert_eq!(
+ resolve_home_url("local", "launcher.html").map(|u| u.to_string()),
+ Some("http://tauri.localhost/launcher.html".to_string())
+ );
+ assert!(resolve_home_url("bogus", "whatever").is_none());
+ }
+
+ #[test]
+ fn a_home_url_with_a_path_keeps_it() {
+ assert_eq!(
+ resolve_home_url("web", "https://example.com/app/").map(|u| u.to_string()),
+ Some("https://example.com/app/".to_string())
+ );
+ }
+}
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index 9aa70c24..dbf2af71 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -19,6 +19,11 @@ function fallbackNavigate(action) {
window.history.back();
} else if (action === "forward") {
window.history.forward();
+ } else if (action === "home") {
+ const homeUrl = window.pakeConfig?.url;
+ if (homeUrl) {
+ window.location.href = homeUrl;
+ }
}
}
@@ -123,6 +128,10 @@ const windowShortcuts = {
l: copyPageAddress,
};
+const shiftShortcuts = {
+ h: () => nativeNavigate("home"),
+};
+
const altShortcuts = {
ArrowLeft: () => nativeNavigate("back"),
ArrowRight: () => nativeNavigate("forward"),
@@ -140,6 +149,13 @@ function handleWindowShortcut(event) {
let action;
if (event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
action = windowShortcuts[event.key?.toLowerCase?.()];
+ } else if (
+ event.ctrlKey &&
+ event.shiftKey &&
+ !event.altKey &&
+ !event.metaKey
+ ) {
+ action = shiftShortcuts[event.key?.toLowerCase?.()];
} else if (
event.altKey &&
!event.ctrlKey &&
diff --git a/tests/unit/event-fullscreen-shortcuts.test.js b/tests/unit/event-fullscreen-shortcuts.test.js
index 1861873c..65e3b8b4 100644
--- a/tests/unit/event-fullscreen-shortcuts.test.js
+++ b/tests/unit/event-fullscreen-shortcuts.test.js
@@ -278,6 +278,72 @@ describe("event fullscreen shortcuts", () => {
expect(context.navigateCalls).toEqual(["back", "forward"]);
});
+ it("navigates home on Ctrl+Shift+H on Windows and Linux", () => {
+ const context = loadEventHelpers();
+ const handler = getWindowShortcutHandler(context);
+ const event = createKeyEvent("h", { ctrlKey: true, shiftKey: true });
+
+ handler(event);
+
+ expect(context.navigateCalls).toEqual(["home"]);
+ expect(event.preventDefault).toHaveBeenCalled();
+ });
+
+ it("navigates home on Ctrl+Shift+H even when uppercase H is reported", () => {
+ const context = loadEventHelpers();
+ const handler = getWindowShortcutHandler(context);
+ const event = createKeyEvent("H", { ctrlKey: true, shiftKey: true });
+
+ handler(event);
+
+ expect(context.navigateCalls).toEqual(["home"]);
+ expect(event.preventDefault).toHaveBeenCalled();
+ });
+
+ it("navigates home on Ctrl+Shift+H when an input element has focus", () => {
+ const context = loadEventHelpers();
+ const input = createElement("input");
+ context.document.activeElement = input;
+ const handler = getWindowShortcutHandler(context);
+ const event = createKeyEvent("h", {
+ ctrlKey: true,
+ shiftKey: true,
+ target: input,
+ });
+
+ handler(event);
+
+ expect(context.navigateCalls).toEqual(["home"]);
+ expect(event.preventDefault).toHaveBeenCalled();
+ });
+
+ it("leaves Ctrl+Shift+H to a page that took it", () => {
+ const context = loadEventHelpers();
+ const handler = getWindowShortcutHandler(context);
+ const event = createKeyEvent("h", {
+ ctrlKey: true,
+ shiftKey: true,
+ defaultPrevented: true,
+ });
+
+ handler(event);
+
+ expect(context.navigateCalls).toEqual([]);
+ expect(event.preventDefault).not.toHaveBeenCalled();
+ });
+
+ it("does not go home on Ctrl+H or Ctrl+Shift+Alt+H", () => {
+ const context = loadEventHelpers();
+ const handler = getWindowShortcutHandler(context);
+
+ handler(createKeyEvent("h", { ctrlKey: true }));
+ handler(
+ createKeyEvent("h", { ctrlKey: true, shiftKey: true, altKey: true }),
+ );
+
+ expect(context.navigateCalls).toEqual([]);
+ });
+
it("leaves the window shortcuts to the macOS menu", () => {
const context = loadEventHelpers({
userAgent:
diff --git a/tests/unit/multi-window-visibility.test.ts b/tests/unit/multi-window-visibility.test.ts
index 5744bc67..b4a936a9 100644
--- a/tests/unit/multi-window-visibility.test.ts
+++ b/tests/unit/multi-window-visibility.test.ts
@@ -52,20 +52,22 @@ describe('multi-window visibility control', () => {
});
describe('native webview navigation IPC', () => {
- it('registers webview_navigate for reload/back/forward', () => {
+ it('registers webview_navigate for reload/back/forward/home', () => {
expect(invokeSource).toContain('pub fn webview_navigate');
expect(invokeSource).toContain('reload_window(&window)');
expect(invokeSource).toContain('history_step(&window, true)');
expect(invokeSource).toContain('history_step(&window, false)');
+ expect(invokeSource).toContain('navigate_home(&window)');
expect(libSource).toContain('webview_navigate');
});
- it('routes Ctrl shortcut reload/back/forward through native navigate', () => {
+ it('routes Ctrl shortcut reload/back/forward/home through native navigate', () => {
expect(eventSource).toContain('function nativeNavigate(action)');
expect(eventSource).toContain('invoke("webview_navigate", { action })');
expect(eventSource).toContain('nativeNavigate("reload")');
expect(eventSource).toContain('nativeNavigate("back")');
expect(eventSource).toContain('nativeNavigate("forward")');
+ expect(eventSource).toContain('nativeNavigate("home")');
// Top-level shortcuts must not call page APIs directly.
expect(eventSource).not.toMatch(
/const shortcuts = \{[\s\S]*?window\.location\.reload\(\)/,