Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Linux tray menu; Fix tray window position; Fix AppImage APP icon #695

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 84 additions & 23 deletions src/main/ui/tray/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,50 @@ const makeTray = async () => {

tray.setToolTip('SwitchHosts')

tray.on('click', () => {
if (!win) {
makeWindow()
return
}
let locale = await configGet('locale')
if (process.platform === 'linux') {
locale = global.system_locale // configGet() always get undefined on Linux
}
const i18n = new I18N(locale)
const { lang } = i18n

if (win.isVisible()) {
if (win.isFocused()) {
win.hide()
} else {
show()
win.focus()
}
} else {
show()
}
})
const ver = version.slice(0, 3).join('.') + ` (${version[3]})`

tray.on('double-click', () => broadcast(events.active_main_window))
if (process.platform === 'linux') {
const menu = Menu.buildFromTemplate([
{
label: lang.click_to_open,
click: () => window(),
},
{ type: 'separator' },
{
label: lang._app_name,
toolTip: lang.show_main_window,
click: () => {
broadcast(events.active_main_window)
},
},
{
label: `v${ver}`,
enabled: false,
},
{ type: 'separator' },
{
label: lang.quit,
role: 'quit',
},
])

tray.on('right-click', async () => {
let locale = await configGet('locale')
const i18n = new I18N(locale)
const { lang } = i18n
// Linux requires setContextMenu to be called in order for the context menu to populate correctly
tray.setContextMenu(menu)
return
}

const ver = version.slice(0, 3).join('.') + ` (${version[3]})`
tray.on('click', () => window())

tray.on('double-click', () => broadcast(events.active_main_window))

tray.on('right-click', async () => {
const menu = Menu.buildFromTemplate([
{
label: lang._app_name,
Expand Down Expand Up @@ -139,8 +156,52 @@ const getPosition = () => {
return { x, y }
}

const getLinuxPosition = () => {
const window_bounds = win.getBounds()
const point = screen.getCursorScreenPoint()
const screen_bounds0 = screen.getDisplayNearestPoint(point).bounds
const screen_bounds = screen.getDisplayNearestPoint(point).workAreaSize

let x: number
let y: number

if (point.x - screen_bounds0.x > screen_bounds.width / 2) { // display on the right of the active screen
x = screen_bounds0.x + screen_bounds0.width - window_bounds.width
} else {
x = 0
}
if (point.y < screen_bounds.height / 2) { // display on the top of the active screen
y = 0
} else {
y = screen_bounds.height - window_bounds.height
}

x = Math.round(x)
y = Math.round(y)

return {x, y}
}

const window = () => {
if (!win) {
makeWindow()
return
}

if (win.isVisible()) {
if (win.isFocused()) {
win.hide()
} else {
show()
win.focus()
}
} else {
show()
}
}

const show = () => {
const { x, y } = getPosition()
let {x, y} = process.platform === 'linux' ? getLinuxPosition(): getPosition()
win.setPosition(x, y, true)
win.show()
// win.focus()
Expand Down
8 changes: 8 additions & 0 deletions src/main/ui/tray/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import path from 'path'

const makeWindow = () => {
let win: BrowserWindow | null
// Linux AppImage APP can't automatically recognize dock icon, requires special configuration to display correctly
let linux_icon = {}
if (process.platform === 'linux') {
linux_icon = {
icon: path.join(__dirname, '/assets/icon.png'),
}
}
win = new BrowserWindow({
frame: false,
// titleBarStyle: 'hidden',
Expand All @@ -30,6 +37,7 @@ const makeWindow = () => {
preload: path.join(__dirname, 'preload.js'),
spellcheck: true,
},
...linux_icon,
})

win.setVisibleOnAllWorkspaces(true, {
Expand Down