Skip to content

Commit

Permalink
feat: use menu register shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
EastSun5566 committed Nov 8, 2023
1 parent adfcada commit ee705b7
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 76 deletions.
80 changes: 76 additions & 4 deletions src-tauri/src/app/menu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use tauri::utils::assets::EmbeddedAssets;
use tauri::{AboutMetadata, Context, CustomMenuItem, Menu, MenuItem, Submenu};
use tauri::{
utils::assets::EmbeddedAssets, AboutMetadata, Context, CustomMenuItem, Manager, Menu, MenuItem,
Submenu, WindowMenuEvent, Wry,
};

use crate::cmd;

pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
let name = &context.package_info().name;
Expand All @@ -10,7 +14,7 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
.add_native_item(MenuItem::Separator)
.add_item(
CustomMenuItem::new("settings".to_string(), "Settings")
.accelerator("CmdOrCtrl+,".to_string()), // Should read from settings
.accelerator("CmdOrCtrl+,".to_string()),
)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Hide)
Expand All @@ -20,6 +24,16 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
.add_native_item(MenuItem::Quit),
);

let file_menu = Submenu::new(
"File",
Menu::new()
.add_item(
CustomMenuItem::new("new_note".to_string(), "New Note").accelerator("CmdOrCtrl+N"),
)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::CloseWindow),
);

let edit_menu = Submenu::new(
"Edit",
Menu::new()
Expand All @@ -32,7 +46,65 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
.add_native_item(MenuItem::SelectAll),
);

let view_menu = Submenu::new(
"View",
Menu::new()
.add_item(
CustomMenuItem::new("command_palette".to_string(), "Command Palette")
.accelerator("CmdOrCtrl+K"),
)
.add_native_item(MenuItem::Separator)
.add_item(
CustomMenuItem::new("reload".to_string(), "Reload This Page")
.accelerator("CmdOrCtrl+R"),
),
);

let help_menu = Submenu::new(
"Help",
Menu::new()
.add_item(CustomMenuItem::new("docs".to_string(), "Documentation"))
.add_item(CustomMenuItem::new("source".to_string(), "View on GitHub"))
.add_item(CustomMenuItem::new("issues".to_string(), "Report Issue"))
.add_native_item(MenuItem::Separator),
);

Menu::new()
.add_submenu(app_menu)
.add_submenu(file_menu)
.add_submenu(edit_menu)
}
.add_submenu(view_menu)
.add_submenu(help_menu)
}

pub fn handler(event: WindowMenuEvent<Wry>) {
let win = Some(event.window()).unwrap();
let app = win.app_handle();
match event.menu_item_id() {
"new_note" => {
cmd::run_script(app, "window.location.href = '/new'");
}
"settings" => {
cmd::open_settings_window(app);
}
"command_palette" => {
cmd::open_command_palette_window(app);
}
"reload" => {
cmd::run_script(app, "window.location.reload()");
}
"docs" => {
cmd::open_link(app, "https://hackdesk.vercel.app".to_string());
}
"source" => {
cmd::open_link(app, "https://github.com/EastSun5566/hackdesk".to_string());
}
"issues" => {
cmd::open_link(
app,
"https://github.com/EastSun5566/hackdesk/issues/new".to_string(),
);
}
_ => (),
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod cmd;
pub mod conf;
// pub mod menu;
pub mod menu;
pub mod setup;

#[cfg(target_os = "macos")]
Expand Down
4 changes: 1 addition & 3 deletions src-tauri/src/app/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"title": "HackDesk",
"shortcut.command-palette": "CmdOrCtrl+K",
"shortcut.settings": "CmdOrCtrl+,"
"title": "HackDesk"
}
9 changes: 3 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
mod app;
mod utils;

use app::{
cmd,
setup,
// menu
};
use app::{cmd, menu, setup};

#[tokio::main]
async fn main() {
Expand All @@ -25,7 +21,8 @@ async fn main() {
cmd::open_link
])
.setup(setup::init)
// .menu(menu::init(&context))
.menu(menu::init(&context))
.on_menu_event(menu::handler)
.system_tray(tauri::SystemTray::default())
.run(context)
.expect("error while running HackDesk application");
Expand Down
60 changes: 2 additions & 58 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ use std::path::{Path, PathBuf};

use anyhow::Result;
use serde_json::{self, json};
use tauri::{api::path, AppHandle, GlobalShortcutManager, Manager};
use tauri::{api::path, AppHandle, Manager};

use crate::app::{
cmd::{open_command_palette_window, open_settings_window},
conf::{DEFAULT_SETTINGS, DEFAULT_TITLE, MAIN_WINDOW_LABEL, ROOT, SETTINGS_NAME},
};
use crate::app::conf::{DEFAULT_SETTINGS, DEFAULT_TITLE, MAIN_WINDOW_LABEL, ROOT, SETTINGS_NAME};

pub fn exists(path: &Path) -> bool {
Path::new(path).exists()
Expand Down Expand Up @@ -53,30 +50,6 @@ pub fn init_settings(app: AppHandle) -> Result<()> {
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
main_window.set_title(title)?;

// TODO: should has better way to register shortcuts
// set shortcuts
let mut shortcut_manager = app.global_shortcut_manager();

let command_palette_shortcut = settings_json["shortcut.command-palette"].as_str().unwrap();
if !shortcut_manager.is_registered(command_palette_shortcut)? {
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
shortcut_manager.register(command_palette_shortcut, move || {
if main_window.is_focused().unwrap() {
open_command_palette_window(main_window.app_handle());
}
})?;
}

let settings_shortcut = settings_json["shortcut.settings"].as_str().unwrap();
if !shortcut_manager.is_registered(settings_shortcut)? {
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
shortcut_manager.register(settings_shortcut, move || {
if main_window.is_focused().unwrap() {
open_settings_window(main_window.app_handle());
}
})?;
}

Ok(())
}

Expand All @@ -90,34 +63,5 @@ pub fn apply_settings(app: AppHandle) -> Result<()> {
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
main_window.set_title(title)?;

// TODO: should has better way to register shortcuts
// set shortcut
let mut shortcut_manager = app.global_shortcut_manager();
shortcut_manager.unregister_all()?;

let command_palette_shortcut = settings_json["shortcut.command-palette"].as_str();
if let Some(shortcut_key) = command_palette_shortcut {
if !shortcut_manager.is_registered(shortcut_key)? {
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
shortcut_manager.register(shortcut_key, move || {
if main_window.is_focused().unwrap() {
open_command_palette_window(main_window.app_handle());
}
})?;
}
}

let settings_shortcut = settings_json["shortcut.settings"].as_str();
if let Some(shortcut_key) = settings_shortcut {
if !shortcut_manager.is_registered(shortcut_key)? {
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
shortcut_manager.register(shortcut_key, move || {
if main_window.is_focused().unwrap() {
open_settings_window(main_window.app_handle());
}
})?;
}
}

Ok(())
}
8 changes: 4 additions & 4 deletions src/pages/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const DEFAULT_COMMANDS: Command[] = [
value: '/new',
label: 'New Note',
Icon: <Cross className="mr-2 h-4 w-4" />,
// shortcut: '⌘ N',
shortcut: '⌘ N',
},
{
value: '/',
Expand Down Expand Up @@ -108,12 +108,12 @@ function reload() {
invoke(Cmd.RUN_SCRIPT, { script: 'window.location.reload()' });
}

const commandPalletteWindow = WebviewWindow.getByLabel('command-palette');
const commandPaletteWindow = WebviewWindow.getByLabel('command-palette');

const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
event.preventDefault();
commandPalletteWindow?.close();
commandPaletteWindow?.close();
}
};

Expand All @@ -132,7 +132,7 @@ const handleSelect = async (value: string) => {
redirect(value);
}

commandPalletteWindow?.close();
commandPaletteWindow?.close();
};

export function CommandPalette() {
Expand Down

0 comments on commit ee705b7

Please sign in to comment.