const { BrowserWindow, screen, ipcMain } = require('electron'); const path = require('node:path'); const { wheelContent } = require('./wheel-options.cjs'); class Wheel { constructor(onChoose, validateFiles, fixInput) { this.onChoose = onChoose; this.validateFiles = validateFiles; this.fixInput = fixInput; this.visible = false; this.mode = 'quick'; this.files = []; this.options = []; const on = (suffix, fn) => ipcMain.on(`filebow-wheel:${suffix}`, (event, data) => { if (this.window && event.sender === this.window.webContents && this.visible) fn(data); }); on('cancel', () => this.hide()); on('choose', id => this.choose(id)); on('files', data => { const files = this.validateFiles(data?.files); if (files.length) { this.files = files; this.interactive = true; this.publish(); } }); on('drop', data => { const files = this.validateFiles(data?.files); if (files.length) this.files = files; this.publish(); this.choose(data?.optionId); }); } async ensure() { if (this.window && !this.window.isDestroyed()) return; this.window = new BrowserWindow({ title: 'FileBow Wheel', show: false, frame: false, transparent: true, backgroundColor: '#00000000', hasShadow: false, resizable: false, movable: false, skipTaskbar: true, alwaysOnTop: true, fullscreenable: false, webPreferences: { preload: path.join(__dirname, '../renderer/wheel/preload.js'), nodeIntegration: false, contextIsolation: true, sandbox: true, backgroundThrottling: false } }); this.window.setAlwaysOnTop(true, 'screen-saver'); this.window.setMenuBarVisibility(false); this.window.webContents.setWindowOpenHandler(() => ({ action: 'deny' })); this.window.webContents.on('will-navigate', event => event.preventDefault()); await this.window.loadFile(path.join(__dirname, '../renderer/wheel/index.html')); } async show(files, mode = 'quick', point = screen.getCursorScreenPoint(), interactive = true) { await this.ensure(); this.files = this.validateFiles(files); this.mode = mode; if (!this.files.length) return; this.interactive = interactive; const display = screen.getDisplayNearestPoint(point), area = display.workArea; this.window.setBounds(display.bounds); this.center = { x: Math.max(area.x + 250, Math.min(area.x + area.width - 250, point.x)), y: Math.max(area.y + 250, Math.min(area.y + area.height - 250, point.y)) }; this.visible = true; this.publish(); if (interactive) { this.window.show(); this.window.focus(); } else this.window.showInactive(); try { this.fixInput?.(this.window.getNativeWindowHandle()); } catch {} clearTimeout(this.timer); this.timer = setTimeout(() => this.hide(), 45000); } publish() { const content = wheelContent(this.files, this.mode); this.options = content.options; const bounds = this.window.getBounds(); this.window.webContents.send('filebow-wheel:state', { ...content, center: { x: this.center.x - bounds.x, y: this.center.y - bounds.y }, ready: true, interactive: this.interactive, subtitle: this.interactive ? 'Aktion wählen · Esc zum Schließen' : content.subtitle }); } choose(id) { const option = this.options.find(item => item.id === id); if (!option) return; const files = [...this.files]; this.hide(); this.onChoose(files, option); } dragEnded(point) { if (!this.visible) return; point = screen.screenToDipPoint(point); const dx = point.x - this.center.x, dy = point.y - this.center.y, radius = Math.hypot(dx, dy); if (radius < 96) { this.interactive = true; this.publish(); return; } if (radius > 250) { this.hide(); return; } const segment = 360 / this.options.length; const index = ((Math.floor((Math.atan2(dy, dx) * 180 / Math.PI + 90 + segment / 2) / segment) % this.options.length) + this.options.length) % this.options.length; const id = this.options[index]?.id; if (this.visible) this.choose(id); } hide() { clearTimeout(this.timer); this.visible = false; this.window?.hide(); } destroy() { this.hide(); this.window?.destroy(); } } module.exports = { Wheel };