Skip to content

Commit

Permalink
fix: Adjust macos tray behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 22, 2022
1 parent 4494e9b commit 76ca968
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
58 changes: 40 additions & 18 deletions xmcl-electron-app/main/controllers/tray.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Controller from '@/Controller'
import { darkIco, darkIcon, darkTray, lightIcon, lightTray } from '@/utils/icons'
import { darkIcon, darkTray, lightIcon, lightTray } from '@/utils/icons'
import { BaseService } from '@xmcl/runtime'
import { app, Menu, shell, Tray, nativeTheme } from 'electron'
import { app, Menu, shell, Tray, nativeTheme, nativeImage, MenuItemConstructorOptions } from 'electron'
import { ControllerPlugin } from './plugin'

export const trayPlugin: ControllerPlugin = function (this: Controller) {
Expand Down Expand Up @@ -30,7 +30,7 @@ export const trayPlugin: ControllerPlugin = function (this: Controller) {
const showLogs = () => {
shell.openPath(this.app.logManager.getLogRoot())
}
return Menu.buildFromTemplate([
const options: MenuItemConstructorOptions[] = [
{
type: 'normal',
label: t('checkUpdate'),
Expand Down Expand Up @@ -61,25 +61,47 @@ export const trayPlugin: ControllerPlugin = function (this: Controller) {
app.quit()
},
},
])
]
if (app.platform.name === 'osx') {
const show = () => {
const window = this.mainWin
window?.show()
}
options.unshift({
label: t('showLauncher'),
type: 'normal',
click: show,
})
}
return Menu.buildFromTemplate(options)
}

const getTrayImage = (dark: string, light: string) => {
const path = nativeTheme.shouldUseDarkColors ? darkTray : lightTray
if (this.app.platform.name === 'osx') {
const icon = nativeImage.createFromPath(path)
return icon.resize({ width: 20, height: 20 })
}
return path
}

this.app.once('engine-ready', () => {
const tray = new Tray(nativeTheme.shouldUseDarkColors ? darkTray : lightTray)
tray.on('click', () => {
if (this.app.platform.name === 'windows') {
const tray = new Tray(getTrayImage(darkTray, lightTray))
if (this.app.platform.name === 'windows') {
tray.on('double-click', () => {
const window = this.mainWin
if (window && !window.isFocused()) {
window.focus()
if (window) {
if (window.isVisible()) {
if (!window.isFocused()) {
window.focus()
} else {
window.hide()
}
} else window.show()
}
}
}).on('double-click', () => {
const window = this.mainWin
if (window) {
if (window.isVisible()) window.hide()
else window.show()
}
})
})
}

tray.setToolTip(t('title'))
tray.setContextMenu(createMenu())
this.app.serviceStateManager.subscribe('config', () => {
Expand All @@ -103,6 +125,6 @@ export const trayPlugin: ControllerPlugin = function (this: Controller) {
if (app.dock) {
app.dock.setIcon(nativeTheme.shouldUseDarkColors ? man.iconSets.darkDockIcon : man.iconSets.dockIcon)
}
this.tray?.setImage(nativeTheme.shouldUseDarkColors ? man.iconSets.darkTrayIcon : man.iconSets.trayIcon)
this.tray?.setImage(getTrayImage(man.iconSets.darkTrayIcon, man.iconSets.trayIcon))
})
}
3 changes: 2 additions & 1 deletion xmcl-electron-app/main/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ quit: Quit
setup:
title: X Minecraft Launcher 主页
showDiagnosis: Show Diagnose
showLauncher: Show Launcher
showLogsFolder: Show Logs Folder
task:
continue: View in Launcher
fail: One Task Failed
success: One Task Success
title: X Minecraft Launcher
urlFailed: Unknown url {url}. Please check your url or contact developer!
urlSuccess: Succeed! Now you can close this window and return to the launcher!
title: X Minecraft Launcher
3 changes: 2 additions & 1 deletion xmcl-electron-app/main/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ quit: 退出
setup:
title: XMCL
showDiagnosis: 显示 Debug 信息
showLauncher: 显示启动器
showLogsFolder: 显示日志文件夹
task:
continue: 在启动器中查看
fail: 有一个任务失败了
success: 有一个任务成功完成了
title: X Minecraft Launcher
urlFailed: 未知 URL {url}. 请检查你复制的链接或联系作者!
urlSuccess: 验证成功!现在你可以关掉这个窗口回到启动器了
title: X Minecraft Launcher

0 comments on commit 76ca968

Please sign in to comment.