Skip to content

Commit

Permalink
Added MacOS support; added support for overriding copy keys
Browse files Browse the repository at this point in the history
  • Loading branch information
hsource committed Dec 3, 2023
1 parent 18d079b commit f162e6f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 19 deletions.
4 changes: 3 additions & 1 deletion ipc/KeyToCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ export const KeyToElectron = {
Backslash: '\\',
BracketRight: ']',
Quote: "'",
Ctrl: 'CmdOrCtrl',
// Do not change Ctrl to CmdOrCtrl. It causes registered shortcuts to
// often not work on Mac for unknown reasons.
Ctrl: 'Ctrl',
Alt: 'Alt',
Shift: 'Shift'
}
Expand Down
18 changes: 9 additions & 9 deletions ipc/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export interface HostConfig {
shortcuts: ShortcutAction[]
restoreClipboard: boolean
clientLog: string | null
gameConfig: string | null
stashScroll: boolean
overlayKey: string
logKeys: boolean
windowTitle: string
language: string
shortcuts: ShortcutAction[]
restoreClipboard: boolean
clientLog: string | null
gameConfig: string | null
stashScroll: boolean
overlayKey: string
logKeys: boolean
windowTitle: string
language: string
}

export interface ShortcutAction {
Expand Down
16 changes: 14 additions & 2 deletions main/src/AppTray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ export class AppTray {
serverPort = 0

constructor (server: ServerEvents) {
this.tray = new Tray(
nativeImage.createFromPath(path.join(__dirname, process.env.STATIC!, process.platform === 'win32' ? 'icon.ico' : 'icon.png'))
let trayImage = nativeImage.createFromPath(
path.join(
__dirname,
process.env.STATIC!,
process.platform === "win32" ? "icon.ico" : "icon.png"
)
)

if (process.platform === 'darwin') {
// Mac image size needs to be smaller, or else it looks huge. Size
// guideline is from https://iconhandbook.co.uk/reference/chart/osx/
trayImage = trayImage.resize({ width: 22, height: 22 })
}

this.tray = new Tray(trayImage)
this.tray.setToolTip(`Awakened PoE Trade v${app.getVersion()}`)
this.rebuildMenu()

Expand Down
8 changes: 7 additions & 1 deletion main/src/host-files/GameConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs/promises'
import path from 'path'
import ini from 'ini'
import { app } from 'electron'
import process from 'process';
import { hotkeyToString, CodeToKey } from '../../../ipc/KeyToCode'
import type { Logger } from '../RemoteLogger'
import type { ServerEvents } from '../server'
Expand All @@ -21,7 +22,12 @@ export class GameConfig {
if (this.filePath === filePath) return

if (!filePath) {
filePath = path.join(app.getPath('documents'), 'My Games', 'Path of Exile', 'production_Config.ini')
if (process.platform === 'darwin') {
filePath = path.join(app.getPath('appData'), 'Path of Exile', 'Preferences', 'production_Config.ini')
} else {
filePath = path.join(app.getPath('documents'), 'My Games', 'Path of Exile', 'production_Config.ini')
}

try {
await fs.access(filePath)
// this.server.sendEventTo('any', {
Expand Down
12 changes: 11 additions & 1 deletion main/src/shortcuts/Shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,18 @@ function pressKeysToCopyItemText (pressedModKeys: string[] = [], showModsKey: st
uIOhook.keyToggle(UiohookKey[key as UiohookKeyT], 'down')
}

const allowedModifierKeys: Set<UiohookKeyT> = new Set(["Ctrl", "Alt", "Shift"]);
const modifierKeys: number[] = keys
.filter((key) => allowedModifierKeys.has(key as UiohookKeyT))
.map((key) => UiohookKey[key as UiohookKeyT])

// finally press `C` to copy text
uIOhook.keyTap(UiohookKey.C)
uIOhook.keyTap(
UiohookKey.C,
// On Mac, robotjs requires the modifiers to be specified in this way to
// register. See https://github.com/octalmage/robotjs/issues/208#issuecomment-223828356
process.platform === 'darwin' ? modifierKeys : undefined
)

keys.reverse()
for (const key of keys) {
Expand Down
11 changes: 7 additions & 4 deletions main/src/shortcuts/text-box.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { uIOhook, UiohookKey as Key } from 'uiohook-napi'
import process from 'process';
import type { HostClipboard } from './HostClipboard'
import type { OverlayWindow } from '../windowing/OverlayWindow'

Expand All @@ -14,14 +15,16 @@ const AUTO_CLEAR = [

export function typeInChat (text: string, send: boolean, clipboard: HostClipboard) {
clipboard.restoreShortly((clipboard) => {
const modifiers = process.platform === 'darwin' ? [Key.Meta] : [Key.Ctrl]

if (text.startsWith(PLACEHOLDER_LAST)) {
text = text.slice(`${PLACEHOLDER_LAST} `.length)
clipboard.writeText(text)
uIOhook.keyTap(Key.Enter, [Key.Ctrl])
uIOhook.keyTap(Key.Enter, modifiers)
} else if (text.endsWith(PLACEHOLDER_LAST)) {
text = text.slice(0, -PLACEHOLDER_LAST.length)
clipboard.writeText(text)
uIOhook.keyTap(Key.Enter, [Key.Ctrl])
uIOhook.keyTap(Key.Enter, modifiers)
uIOhook.keyTap(Key.Home)
// press twice to focus input when using controller
uIOhook.keyTap(Key.Home)
Expand All @@ -30,11 +33,11 @@ export function typeInChat (text: string, send: boolean, clipboard: HostClipboar
clipboard.writeText(text)
uIOhook.keyTap(Key.Enter)
if (!AUTO_CLEAR.includes(text[0])) {
uIOhook.keyTap(Key.A, [Key.Ctrl])
uIOhook.keyTap(Key.A, modifiers)
}
}

uIOhook.keyTap(Key.V, [Key.Ctrl])
uIOhook.keyTap(Key.V, modifiers)

if (send) {
uIOhook.keyTap(Key.Enter)
Expand Down
2 changes: 1 addition & 1 deletion main/src/windowing/GameWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class GameWindow extends EventEmitter {
if (!this._isTracking) {
OverlayController.events.on('focus', () => { this.isActive = true })
OverlayController.events.on('blur', () => { this.isActive = false })
OverlayController.attachByTitle(window, title)
OverlayController.attachByTitle(window, title, { hasTitleBarOnMac: true })
this._isTracking = true
}
}
Expand Down

0 comments on commit f162e6f

Please sign in to comment.