Skip to content

Commit

Permalink
Add ctrl+q/ctrl+w keybinds
Browse files Browse the repository at this point in the history
Does what title says.

Lifts event handler logic from paste image support (cinnyapp#167).

Includes some commented out logic for compatibility with systray (cinnyapp#166) when/if merged. Requires TRAY_LABEL be exposed as public, however.
  • Loading branch information
hugeblank committed Sep 26, 2023
1 parent ba8d255 commit c2f854c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tauri-build = { version = "1.2.1", features = [] }
[dependencies]
serde_json = "1.0.91"
serde = { version = "1.0.152", features = ["derive"] }
tauri = { version = "1.2.3", features = ["api-all", "devtools", "updater"] }
tauri = { version = "1.2.3", features = ["api-all", "devtools", "global-shortcut", "updater"] }

[features]
# by default Tauri runs in production mode
Expand Down
19 changes: 15 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@

#[cfg(target_os = "macos")]
mod menu;
mod shortcuts;

fn main() {
let builder = tauri::Builder::default();

#[cfg(target_os = "macos")]
let builder = builder.menu(menu::menu());

builder
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
builder
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(run_event_handler)
}

fn run_event_handler<R: tauri::Runtime>(app: &tauri::AppHandle<R>, event: tauri::RunEvent) {
match event {
tauri::RunEvent::Ready => {
shortcuts::ready_event_handler(app);
}
_ => {}
}
}
25 changes: 25 additions & 0 deletions src-tauri/src/shortcuts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use tauri::{ GlobalShortcutManager, Manager };

//use crate::tray;

pub fn ready_event_handler<R: tauri::Runtime>(
app: &tauri::AppHandle<R>
) {
let manager = app.global_shortcut_manager();
manager.clone().register("CmdOrCtrl+w", hide_closure(app)).unwrap();
manager.clone().register("CmdOrCtrl+q", close_closure(app)).unwrap();
}

fn hide_closure<R: tauri::Runtime>(app: &tauri::AppHandle<R>) -> impl Fn() {
let window = app.get_window("main").unwrap();
//let app = app.clone();
return move || {
window.hide().unwrap();
//tray::toggle_window_state(window.clone(), app.tray_handle_by_id(tray::TRAY_LABEL).unwrap())
}
}

fn close_closure<R: tauri::Runtime>(app: &tauri::AppHandle<R>) -> impl Fn() {
let window = app.get_window("main").unwrap();
return move || window.close().unwrap();
}

0 comments on commit c2f854c

Please sign in to comment.