feat: basic app menu (#1207)

This commit is contained in:
lin onetwo 2023-03-01 12:37:53 +08:00 committed by GitHub
parent c096fa156b
commit b575094f4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 154 additions and 9 deletions

View File

@ -2115,6 +2115,30 @@ dependencies = [
"thiserror",
]
[[package]]
name = "libappindicator"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db2d3cb96d092b4824cb306c9e544c856a4cb6210c1081945187f7f1924b47e8"
dependencies = [
"glib",
"gtk",
"gtk-sys",
"libappindicator-sys",
"log",
]
[[package]]
name = "libappindicator-sys"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1b3b6681973cea8cc3bce7391e6d7d5502720b80a581c9a95c9cbaf592826aa"
dependencies = [
"gtk-sys",
"libloading",
"once_cell",
]
[[package]]
name = "libc"
version = "0.2.138"
@ -2130,6 +2154,16 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "libloading"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
dependencies = [
"cfg-if",
"winapi",
]
[[package]]
name = "libsqlite3-sys"
version = "0.24.2"
@ -4430,6 +4464,7 @@ dependencies = [
"core-foundation",
"core-graphics",
"crossbeam-channel",
"dirs-next",
"dispatch",
"gdk",
"gdk-pixbuf",
@ -4443,6 +4478,7 @@ dependencies = [
"instant",
"jni",
"lazy_static",
"libappindicator",
"libc",
"log",
"ndk",

View File

@ -35,7 +35,7 @@ schemars = "0.8.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dotenvy = "0.15.6"
tauri = { version = "1.2", features = ["api-all", "devtools"] }
tauri = { version = "1.2", features = ["api-all", "devtools", "system-tray"] }
tokio = { version = "1.23.0", features = ["rt", "macros"] }
lib0 = "0.12.0"
moka = { version = "0.9.6", features = ["future"] }

View File

@ -5,6 +5,7 @@
mod commands;
mod state;
mod menu;
use dotenvy::dotenv;
use state::AppState;
use std::env;
@ -47,6 +48,8 @@ async fn main() {
Ok(())
})
.invoke_handler(commands::invoke_handler())
.menu(menu::init())
.on_menu_event(menu::menu_handler)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@ -0,0 +1,108 @@
use tauri::{CustomMenuItem, Manager, Menu, MenuItem, Submenu, WindowMenuEvent};
#[cfg(target_os = "macos")]
use tauri::AboutMetadata;
// --- Menu
pub fn init() -> Menu {
let name = "AFFiNE";
let app_menu = Submenu::new(
name,
Menu::with_items([
#[cfg(target_os = "macos")]
MenuItem::About(name.into(), AboutMetadata::default()).into(),
MenuItem::Services.into(),
MenuItem::Hide.into(),
MenuItem::HideOthers.into(),
MenuItem::ShowAll.into(),
MenuItem::Separator.into(),
MenuItem::Quit.into(),
]),
);
let edit_menu = Submenu::new(
"Edit",
Menu::new()
.add_native_item(MenuItem::Undo)
.add_native_item(MenuItem::Redo)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Cut)
.add_native_item(MenuItem::Copy)
.add_native_item(MenuItem::Paste)
.add_native_item(MenuItem::SelectAll),
);
let view_menu = Submenu::new(
"View",
Menu::new()
.add_item(CustomMenuItem::new("go_back".to_string(), "Go Back").accelerator("CmdOrCtrl+["))
.add_item(
CustomMenuItem::new("go_forward".to_string(), "Go Forward").accelerator("CmdOrCtrl+]"),
)
.add_native_item(MenuItem::Separator)
.add_item(
CustomMenuItem::new("zoom_0".to_string(), "Zoom to Actual Size").accelerator("CmdOrCtrl+0"),
)
.add_item(CustomMenuItem::new("zoom_out".to_string(), "Zoom Out").accelerator("CmdOrCtrl+-"))
.add_item(CustomMenuItem::new("zoom_in".to_string(), "Zoom In").accelerator("CmdOrCtrl+Plus"))
.add_native_item(MenuItem::Separator)
.add_item(
CustomMenuItem::new("reload".to_string(), "Refresh the Screen").accelerator("CmdOrCtrl+R"),
),
);
let window_menu = Submenu::new(
"Window",
Menu::new()
.add_item(CustomMenuItem::new(
"official_website".to_string(),
"About AFFiNE",
))
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Minimize)
.add_native_item(MenuItem::Zoom),
);
let help_menu = Submenu::new(
"Help",
Menu::new()
.add_item(CustomMenuItem::new("update_log".to_string(), "Update Log"))
.add_item(CustomMenuItem::new("report_bug".to_string(), "Report Bug"))
.add_item(
CustomMenuItem::new("dev_tools".to_string(), "Toggle Developer Tools")
.accelerator("CmdOrCtrl+Shift+I"),
),
);
Menu::new()
.add_submenu(app_menu)
.add_submenu(window_menu)
.add_submenu(edit_menu)
.add_submenu(view_menu)
.add_submenu(help_menu)
}
// --- Menu Event
pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
let win = Some(event.window()).unwrap();
let app = win.app_handle();
let menu_id = event.menu_item_id();
match menu_id {
// App
"restart" => tauri::api::process::restart(&app.env()),
// Window
// View
"zoom_0" => win.eval("window.__zoom0 && window.__zoom0()").unwrap(),
"zoom_out" => win.eval("window.__zoomOut && window.__zoomOut()").unwrap(),
"zoom_in" => win.eval("window.__zoomIn && window.__zoomIn()").unwrap(),
"reload" => win.eval("window.location.reload()").unwrap(),
"go_back" => win.eval("window.history.go(-1)").unwrap(),
"go_forward" => win.eval("window.history.go(1)").unwrap(),
"dev_tools" => {
win.open_devtools();
win.close_devtools();
}
_ => {}
}
}

View File

@ -18,6 +18,11 @@
"scope": ["$RESOURCE", "$RESOURCE/*", "$APP/*"]
}
},
"systemTray": {
"iconPath": "icons/128x128@2x.png",
"iconAsTemplate": true,
"menuOnLeftClick": false
},
"bundle": {
"active": true,
"category": "DeveloperTool",

View File

@ -1,9 +1,3 @@
declare global {
interface Window {
__TAURI_ISOLATION_HOOK_: (payload: any) => any;
}
}
// tauri preload script can't have `export {}`
// @ts-ignore 'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.ts(1208)
window.__TAURI_ISOLATION_HOOK__ = payload => {
@ -20,5 +14,3 @@ function setEnvironmentVariables() {
}
setEnvironmentVariables();
export {};

View File

@ -4,6 +4,7 @@ declare global {
interface Window {
CLIENT_APP?: boolean;
__TAURI_ISOLATION_HOOK_: (payload: any) => any;
}
}