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

[WIP]ウィンドウ情報の取得をポーリング方式からイベント駆動方式にする #44

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ directories:
files:
- '!**/.vscode/*'
- '!src/*'
- '!nativeSrc/*'
- '!electron.vite.config.{js,ts,mjs,cjs}'
- '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
- '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'
Expand Down
107 changes: 0 additions & 107 deletions nativeSrc/helper.swift

This file was deleted.

43 changes: 38 additions & 5 deletions nativeSrc/taskbar.helper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,43 @@ func getWindowInfoListData() -> Data? {
}


class WindowObserver {
static let shared = WindowObserver()

private init() {}

// ウィンドウの変更を監視
func observeWindowChanges() {
let notificationCenter = NSWorkspace.shared.notificationCenter

// アクティブになるイベントの監視
notificationCenter.addObserver(
self,
selector: #selector(windowDidChange(notification:)),
name: NSWorkspace.didActivateApplicationNotification,
object: nil
)

// アプリケーションが起動されたイベントを監視
// これがないと新規で起動したアプリケーションのウィンドウ情報が取得できない
notificationCenter.addObserver(
self,
selector: #selector(windowDidChange(notification:)),
name: NSWorkspace.didLaunchApplicationNotification,
object: nil
)
}

@objc func windowDidChange(notification: NSNotification) {
// 0.1秒待機
// アプリを起動した直後にウィンドウ情報を取得すると、ウィンドウ情報が取得できないため
usleep(100000)
mesichicken marked this conversation as resolved.
Show resolved Hide resolved
if let data = getWindowInfoListData() {
let stdOut = FileHandle.standardOutput
stdOut.write(data)
}
}
}


let arguments = CommandLine.arguments
Expand All @@ -103,11 +138,9 @@ case "grant":
CGRequestScreenCaptureAccess()
case "list":

if let data = getWindowInfoListData() {
let stdOut = FileHandle.standardOutput
stdOut.write(data)
}
// ウィンドウの変更を監視
WindowObserver.shared.observeWindowChanges()
RunLoop.main.run()
default:
print("default")
}

Binary file modified resources/TaskbarHelper
Binary file not shown.
28 changes: 19 additions & 9 deletions src/main/funcs/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@ export function getAndSubmitProcesses(): void {
let rawData = ''
try {
const taskbarHelper = spawn(binaryPath, ['list'])
taskbarHelper.stdout.on('data', (raw) => {
rawData += raw
})
taskbarHelper.stderr.on('data', (raw) => {
console.error(raw)
})
taskbarHelper.on('close', async (code) => {
if ((await code) === 0) {
const jsoned = JSON.parse(Buffer.from(rawData).toString('utf-8'))
taskbarHelper.stdout.on('data', (data) => {
// 画面の変更情報を受け取る
// ここでElectronのウィンドウや他のコンポーネントにデータを渡す
// dataの最後が]で終わっていないとJSON.parseでエラーになる
// ]で終わっていない場合は、次のデータを受け取るまで待つ
if (data.toString().endsWith(']')) {
rawData += data
// console.log(rawData)
const jsoned = JSON.parse(rawData.toString())
applyProcessChange(jsoned)
rawData = ''
} else {
rawData += data
}
})

taskbarHelper.stderr.on('data', (data) => {
console.error(`Error: ${data}`)
})

taskbarHelper.on('close', (code) => {
console.log(`Swift process exited with code ${code}`)
})
} catch (e) {
console.log(e)
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ app.whenReady().then(() => {
setEventHandlers()
})

// 1秒ごとにプロセスを取得する
setInterval(() => {
getAndSubmitProcesses()
}, 1000)
// プロセスを取得するプロセスを起動
getAndSubmitProcesses()

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
Expand Down