Skip to content

Commit

Permalink
开机托盘未显示
Browse files Browse the repository at this point in the history
  • Loading branch information
hanaTsuk1 committed Jun 24, 2024
1 parent 0be5bfc commit 03c2d17
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 33 deletions.
1 change: 1 addition & 0 deletions src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"resources:default",
"menu:default",
"tray:default",
"tray:allow-get-by-id",
"autostart:allow-disable",
"autostart:allow-enable",
"autostart:allow-is-enabled",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/gen/schemas/capabilities.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"main-capability":{"identifier":"main-capability","description":"Capability for the main window","local":true,"windows":["main"],"permissions":["path:default","event:default","window:default","window:allow-start-dragging","window:allow-hide","window:allow-minimize","app:default","resources:default","menu:default","tray:default","autostart:allow-disable","autostart:allow-enable","autostart:allow-is-enabled","clipboard-manager:allow-write-text","dialog:allow-open","fs:allow-copy-file","fs:allow-mkdir","fs:allow-write-file","fs:allow-write-text-file","fs:allow-read-text-file","fs:allow-exists","fs:allow-remove","fs:scope-appdata-recursive","log:allow-log","process:allow-restart","process:allow-exit","shell:allow-open","sql:allow-load","sql:allow-select","sql:allow-execute","sql:allow-close","store:allow-length","store:allow-entries","store:allow-set","store:allow-save","updater:default","shion-history:allow-get-config","shion-history:allow-set-config","shion-history:allow-read-history","shion-watcher:allow-resume","shion-watcher:allow-suspend","shion-watcher:allow-get-program-list","shion-watcher:allow-get-program-by-path"]}}
{"main-capability":{"identifier":"main-capability","description":"Capability for the main window","local":true,"windows":["main"],"permissions":["path:default","event:default","window:default","window:allow-start-dragging","window:allow-hide","window:allow-minimize","app:default","resources:default","menu:default","tray:default","tray:allow-get-by-id","autostart:allow-disable","autostart:allow-enable","autostart:allow-is-enabled","clipboard-manager:allow-write-text","dialog:allow-open","fs:allow-copy-file","fs:allow-mkdir","fs:allow-write-file","fs:allow-write-text-file","fs:allow-read-text-file","fs:allow-exists","fs:allow-remove","fs:scope-appdata-recursive","log:allow-log","process:allow-restart","process:allow-exit","shell:allow-open","sql:allow-load","sql:allow-select","sql:allow-execute","sql:allow-close","store:allow-length","store:allow-entries","store:allow-set","store:allow-save","updater:default","shion-history:allow-get-config","shion-history:allow-set-config","shion-history:allow-read-history","shion-watcher:allow-resume","shion-watcher:allow-suspend","shion-watcher:allow-get-program-list","shion-watcher:allow-get-program-by-path"]}}
71 changes: 39 additions & 32 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ pub use error::Result;
mod autostart;

use parse_changelog::Changelog;
use tauri::menu::{Menu, MenuItem};
use tauri::{
menu::{Menu, MenuItem},
tray::{ClickType, TrayIconBuilder},
};
use tauri_plugin_log::{Target, TargetKind, TimezoneStrategy};
use tauri_plugin_sql::{Migration, MigrationKind};

Expand Down Expand Up @@ -119,6 +122,37 @@ pub fn run() {
autostart::disable()
}

#[tauri::command]
fn create_tray(app: tauri::AppHandle) -> tauri::Result<()> {
let title = if tauri::dev() { "shion-dev" } else { "shion" };

let quit = MenuItem::with_id(&app, "quit", "Quit", true, None::<&str>)?;
let menu = Menu::with_items(&app, &[&quit])?;

TrayIconBuilder::with_id("tray")
.tooltip(title)
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu)
.menu_on_left_click(false)
.on_menu_event(move |app, event| match event.id().as_ref() {
"quit" => {
let window = app.get_webview_window("main").unwrap();
window.hide().unwrap();
app.emit_to("main", "quit", ()).unwrap();
}
_ => (),
})
.on_tray_icon_event(|tray, event| {
if event.click_type == ClickType::Double {
let app = tray.app_handle();
let window = app.get_webview_window("main").unwrap();
window.show().unwrap();
}
})
.build(&app)?;
Ok(())
}

builder = builder
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
Expand Down Expand Up @@ -148,46 +182,17 @@ pub fn run() {
parse_changelog_from_text,
enable_autostart,
disable_autostart,
create_tray
]);
}

builder
.setup(|app| {
#[cfg(desktop)]
{
use tauri::{
tray::{ClickType, TrayIconBuilder},
Manager, WebviewUrl, WebviewWindowBuilder, Wry,
};
use tauri::{Manager, WebviewUrl, WebviewWindowBuilder, Wry};
use tauri_plugin_store::{with_store, StoreCollection};

let title = if tauri::dev() { "shion-dev" } else { "shion" };

let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
let menu = Menu::with_items(app, &[&quit])?;

let _ = TrayIconBuilder::with_id("tray")
.tooltip(title)
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu)
.menu_on_left_click(false)
.on_menu_event(move |app, event| match event.id().as_ref() {
"quit" => {
let window = app.get_webview_window("main").unwrap();
window.hide().unwrap();
app.emit_to("main", "quit", ()).unwrap();
}
_ => (),
})
.on_tray_icon_event(|tray, event| {
if event.click_type == ClickType::Double {
let app = tray.app_handle();
let window = app.get_webview_window("main").unwrap();
window.show().unwrap();
}
})
.build(app);

let stores = app.app_handle().state::<StoreCollection<Wry>>();

let launch_visible =
Expand All @@ -200,6 +205,8 @@ pub fn run() {
Ok(true)
})?;

let title = if tauri::dev() { "shion-dev" } else { "shion" };

WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
.visible(launch_visible)
.center()
Expand Down
3 changes: 3 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script setup lang="ts">
import { ModalsContainer } from 'vue-final-modal'
import { checkTrayExists } from './modules/tray'
if (isDesktop)
useUpdateStore()
Expand All @@ -12,6 +14,7 @@ const configStore = useConfigStore()
const { config } = storeToRefs(configStore)
useTour()
checkTrayExists()
useHotkey('*', (keyboardEvent) => {
const disableKeys = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12']
Expand Down
32 changes: 32 additions & 0 deletions src/modules/tray/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { invoke } from '@tauri-apps/api/core'
import { TrayIcon } from '@tauri-apps/api/tray'
import { error } from '@tauri-apps/plugin-log'

import { i18n } from '@/locales'

export function checkTrayExists() {
const timer = new Timer(async () => {
const exists = await createTray()
if (exists)
timer.destroy()
}, 3000)
}

async function createTray() {
const icon = await TrayIcon.getById('tray')
if (!icon) {
try {
await invoke('create_tray')
await invoke('update_tray_menu', {
data: {
quit: i18n.global.t('tray.quit'),
},
})
}
catch (e) {
error(`create tray error: ${e}`)
return false
}
}
return !!icon
}

0 comments on commit 03c2d17

Please sign in to comment.