feat: Implement hide on escape and focus loss

This commit adds two ways that hides the main window. First, when the user presses escape in the command palette, the window hides; second, when the focus is lost (i.e. the user's cursor goes outside the window), it also hides itself.
This commit is contained in:
ByteAtATime 2025-06-25 14:31:21 -07:00
parent 9d73ac0a23
commit 58e48213bd
No known key found for this signature in database
3 changed files with 28 additions and 11 deletions

View file

@ -7,6 +7,7 @@
], ],
"permissions": [ "permissions": [
"core:default", "core:default",
"core:window:allow-hide",
"opener:default", "opener:default",
"clipboard-manager:default", "clipboard-manager:default",
"clipboard-manager:allow-write-text", "clipboard-manager:allow-write-text",

View file

@ -274,16 +274,23 @@ pub fn run() {
.build(tauri::generate_context!()).unwrap(); .build(tauri::generate_context!()).unwrap();
app.run(|app, event| { app.run(|app, event| {
if let tauri::RunEvent::WindowEvent { if let tauri::RunEvent::WindowEvent { label, event, .. } = event {
event: tauri::WindowEvent::CloseRequested { api, .. }, if label == "main" {
.. match event {
} = event tauri::WindowEvent::CloseRequested { api, .. } => {
{ api.prevent_close();
api.prevent_close(); if let Some(window) = app.get_webview_window("main") {
app.get_webview_window("main") let _ = window.hide();
.unwrap() }
.hide() }
.expect("To hide the window"); tauri::WindowEvent::Focused(false) => {
} if let Some(window) = app.get_webview_window("main") {
let _ = window.hide();
}
}
_ => {}
}
}
}
}); });
} }

View file

@ -16,6 +16,7 @@
import SnippetForm from '$lib/components/SnippetForm.svelte'; import SnippetForm from '$lib/components/SnippetForm.svelte';
import ImportSnippets from '$lib/components/ImportSnippets.svelte'; import ImportSnippets from '$lib/components/ImportSnippets.svelte';
import SearchSnippets from '$lib/components/SearchSnippets.svelte'; import SearchSnippets from '$lib/components/SearchSnippets.svelte';
import { getCurrentWindow } from '@tauri-apps/api/window';
const storePlugin: PluginInfo = { const storePlugin: PluginInfo = {
title: 'Discover Extensions', title: 'Discover Extensions',
@ -138,6 +139,14 @@
viewManager.showSettings(); viewManager.showSettings();
return; return;
} }
if (event.key === 'Escape') {
if (currentView === 'command-palette') {
event.preventDefault();
getCurrentWindow().hide();
console.log('hide');
}
}
} }
function handleSavePreferences(pluginName: string, values: Record<string, unknown>) { function handleSavePreferences(pluginName: string, values: Record<string, unknown>) {