屏蔽多余菜单按钮,更改托盘按钮顺序

This commit is contained in:
Tlntin
2022-12-28 17:07:55 +08:00
parent 2bc74e8e22
commit 506d3875f2
4 changed files with 98 additions and 167 deletions

101
src-tauri/Cargo.lock generated
View File

@@ -42,17 +42,14 @@ checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
name = "app"
version = "0.1.0"
dependencies = [
"cocoa 0.24.1 (git+https://github.com/servo/core-foundation-rs/)",
"home",
"image",
"objc",
"serde",
"serde_json",
"tauri",
"tauri-build",
"tauri-plugin-window-state",
"tauri-utils",
"webkit2gtk",
]
[[package]]
@@ -269,24 +266,9 @@ checksum = "f425db7937052c684daec3bd6375c8abe2d146dca4b8b143d6db777c39138f3a"
dependencies = [
"bitflags",
"block",
"cocoa-foundation 0.1.0",
"core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)",
"foreign-types",
"libc",
"objc",
]
[[package]]
name = "cocoa"
version = "0.24.1"
source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993"
dependencies = [
"bitflags",
"block",
"cocoa-foundation 0.1.1",
"core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)",
"core-graphics 0.22.3 (git+https://github.com/servo/core-foundation-rs/)",
"cocoa-foundation",
"core-foundation",
"core-graphics",
"foreign-types",
"libc",
"objc",
@@ -300,22 +282,8 @@ checksum = "7ade49b65d560ca58c403a479bb396592b155c0185eada742ee323d1d68d6318"
dependencies = [
"bitflags",
"block",
"core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics-types 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"foreign-types",
"libc",
"objc",
]
[[package]]
name = "cocoa-foundation"
version = "0.1.1"
source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993"
dependencies = [
"bitflags",
"block",
"core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)",
"core-graphics-types 0.1.1 (git+https://github.com/servo/core-foundation-rs/)",
"core-foundation",
"core-graphics-types",
"foreign-types",
"libc",
"objc",
@@ -349,16 +317,7 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
"core-foundation-sys 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993"
dependencies = [
"core-foundation-sys 0.8.3 (git+https://github.com/servo/core-foundation-rs/)",
"core-foundation-sys",
"libc",
]
@@ -368,11 +327,6 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993"
[[package]]
name = "core-graphics"
version = "0.22.3"
@@ -380,20 +334,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb"
dependencies = [
"bitflags",
"core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics-types 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"foreign-types",
"libc",
]
[[package]]
name = "core-graphics"
version = "0.22.3"
source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993"
dependencies = [
"bitflags",
"core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)",
"core-graphics-types 0.1.1 (git+https://github.com/servo/core-foundation-rs/)",
"core-foundation",
"core-graphics-types",
"foreign-types",
"libc",
]
@@ -405,18 +347,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68b68b3446082644c91ac778bf50cd4104bfb002b5a6a7c44cca5a2c70788b"
dependencies = [
"bitflags",
"core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"foreign-types",
"libc",
]
[[package]]
name = "core-graphics-types"
version = "0.1.1"
source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993"
dependencies = [
"bitflags",
"core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)",
"core-foundation",
"foreign-types",
"libc",
]
@@ -2589,9 +2520,9 @@ dependencies = [
"bitflags",
"cairo-rs",
"cc",
"cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)",
"core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)",
"cocoa",
"core-foundation",
"core-graphics",
"crossbeam-channel",
"dirs-next",
"dispatch",
@@ -2646,7 +2577,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b48820ee3bb6a5031a83b2b6e11f8630bdc5a2f68cb841ab8ebc7a15a916679"
dependencies = [
"anyhow",
"cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cocoa",
"dirs-next",
"embed_plist",
"encoding_rs",
@@ -2776,7 +2707,7 @@ version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36b1c5764a41a13176a4599b5b7bd0881bea7d94dfe45e1e755f789b98317e30"
dependencies = [
"cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cocoa",
"gtk",
"percent-encoding",
"rand 0.8.5",
@@ -3479,8 +3410,8 @@ checksum = "4c1ad8e2424f554cc5bdebe8aa374ef5b433feff817aebabca0389961fc7ef98"
dependencies = [
"base64",
"block",
"cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)",
"cocoa",
"core-graphics",
"crossbeam-channel",
"dunce",
"gdk",

View File

@@ -25,12 +25,12 @@ tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin-
# webbrowser = "0.8.2"
# wry = "0.23.4"
[target.'cfg(target_os = "linux")'.dependencies]
webkit2gtk = "0.18.0"
# [target.'cfg(target_os = "linux")'.dependencies]
# webkit2gtk = "0.18.0"
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2.7"
cocoa = {git = "https://github.com/servo/core-foundation-rs/"}
# [target.'cfg(target_os = "macos")'.dependencies]
# objc = "0.2.7"
# cocoa = {git = "https://github.com/servo/core-foundation-rs/"}
[features]
# by default Tauri runs in production mode

View File

@@ -17,7 +17,7 @@
},
"menu": {
"macos": true,
"linux": false,
"linux": true,
"windows": false
},
"system_tray": {

View File

@@ -1,13 +1,13 @@
#[cfg(target_os = "macos")]
use tauri::MenuItem;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
// #[cfg(target_os = "macos")]
// #[macro_use]
// extern crate objc;
use tauri::{
window::PlatformWebview, App, Config, CustomMenuItem, Manager, Menu, Submenu, SystemTray,
SystemTrayEvent, SystemTrayMenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl,
App, Config, CustomMenuItem, Manager, Menu, Submenu, SystemTray, SystemTrayEvent,
SystemTrayMenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl,
};
mod pake;
use pake::PakeConfig;
@@ -47,19 +47,19 @@ pub fn get_menu() -> Menu {
// let previous = CustomMenuItem::new("previous", "Previous (←)");
// let next = CustomMenuItem::new("next", "next (→)");
// let refresh = CustomMenuItem::new("refresh", "Refresh");
let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)");
let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)");
let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset");
let hot_key = Menu::new()
// .add_item(top)
// .add_item(buttom)
// .add_item(previous)
// .add_item(next)
// .add_item(refresh)
.add_item(zoom_in)
.add_item(zoom_out)
.add_item(zoom_reset);
let hot_key_menu = Submenu::new("Hot Key", hot_key);
// let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)");
// let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)");
// let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset");
// let hot_key = Menu::new()
// .add_item(top)
// .add_item(buttom)
// .add_item(previous)
// .add_item(next)
// .add_item(refresh)
// .add_item(zoom_in)
// .add_item(zoom_out)
// .add_item(zoom_reset);
// let hot_key_menu = Submenu::new("Hot Key", hot_key);
// Help
// let instructions = CustomMenuItem::new("instruction", "Instruction");
@@ -68,47 +68,46 @@ pub fn get_menu() -> Menu {
// .add_item(instructions)
// .add_item(about);
// let help_menu = Submenu::new("Help", help);
Menu::new()
.add_submenu(first_menu)
.add_submenu(hot_key_menu)
Menu::new().add_submenu(first_menu)
// .add_submenu(hot_key_menu)
}
pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) {
#[cfg(target_os = "linux")]
{
// see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html
// and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html
use webkit2gtk::traits::WebViewExt;
webview.inner().set_zoom_level(zoom_value);
}
// pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) {
// #[cfg(target_os = "linux")]
// {
// // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html
// // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html
// use webkit2gtk::traits::WebViewExt;
// webview.inner().set_zoom_level(zoom_value);
// }
#[cfg(windows)]
unsafe {
// see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
webview.controller().SetZoomFactor(zoom_value).unwrap();
}
// #[cfg(windows)]
// unsafe {
// // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
// webview.controller().SetZoomFactor(zoom_value).unwrap();
// }
#[cfg(target_os = "macos")]
unsafe {
let () = msg_send![webview.inner(), setPageZoom: zoom_value];
let () = msg_send![webview.controller(), removeAllUserScripts];
let bg_color: cocoa::base::id =
msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
}
}
// #[cfg(target_os = "macos")]
// unsafe {
// let () = msg_send![webview.inner(), setPageZoom: zoom_value];
// let () = msg_send![webview.controller(), removeAllUserScripts];
// let bg_color: cocoa::base::id =
// msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
// let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
// }
// }
pub fn set_zoom_out(webview: PlatformWebview) {
set_zoom(webview, 1.25);
}
// pub fn set_zoom_out(webview: PlatformWebview) {
// set_zoom(webview, 1.25);
// }
pub fn set_zoom_in(webview: PlatformWebview) {
set_zoom(webview, 0.75);
}
// pub fn set_zoom_in(webview: PlatformWebview) {
// set_zoom(webview, 0.75);
// }
pub fn zoom_reset(webview: PlatformWebview) {
set_zoom(webview, 1.0);
}
// pub fn zoom_reset(webview: PlatformWebview) {
// set_zoom(webview, 1.0);
// }
pub fn menu_event_handle(event: WindowMenuEvent) {
match event.menu_item_id() {
@@ -116,24 +115,24 @@ pub fn menu_event_handle(event: WindowMenuEvent) {
"show" => event.window().show().expect("can't show window"),
"close" => event.window().close().expect("can't close window"),
"quit" => std::process::exit(0),
"zoom_out" => {
event
.window()
.with_webview(set_zoom_out)
.expect("can't set zoom out");
}
"zoom_in" => {
event
.window()
.with_webview(set_zoom_in)
.expect("can't set zoom in");
}
"reset" => {
event
.window()
.with_webview(zoom_reset)
.expect("can't reset zoom");
}
// "zoom_out" => {
// event
// .window()
// .with_webview(set_zoom_out)
// .expect("can't set zoom out");
// }
// "zoom_in" => {
// event
// .window()
// .with_webview(set_zoom_in)
// .expect("can't set zoom in");
// }
// "reset" => {
// event
// .window()
// .with_webview(zoom_reset)
// .expect("can't reset zoom");
// }
_ => {}
}
}
@@ -154,17 +153,18 @@ pub fn get_system_tray(show_menu: bool) -> SystemTray {
let show_app = CustomMenuItem::new("show_app".to_string(), "Show App");
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let about = CustomMenuItem::new("about".to_string(), "About");
let tray_menu = SystemTrayMenu::new()
.add_item(hide_app)
.add_item(show_app)
.add_item(quit)
.add_item(about);
let tray_menu = SystemTrayMenu::new().add_item(hide_app).add_item(show_app);
if show_menu {
let hide_menu = CustomMenuItem::new("hide_menu".to_string(), "Hide Menu");
let show_menu = CustomMenuItem::new("show_menu".to_string(), "Show Menu");
let tray_menu = tray_menu.add_item(hide_menu).add_item(show_menu);
let tray_menu = tray_menu
.add_item(hide_menu)
.add_item(show_menu)
.add_item(quit)
.add_item(about);
SystemTray::new().with_menu(tray_menu)
} else {
let tray_menu = tray_menu.add_item(quit).add_item(about);
SystemTray::new().with_menu(tray_menu)
}
}