From bfe8847d72cd0893230f2e654242658214943e61 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Sat, 2 Oct 2021 09:24:32 +0200 Subject: chore: convert various files from JS to TS (#2010) --- src/lib/DBus.js | 45 -------------------------------------- src/lib/DBus.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib/Form.js | 32 --------------------------- src/lib/Form.ts | 32 +++++++++++++++++++++++++++ src/lib/TouchBar.js | 49 ------------------------------------------ src/lib/TouchBar.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 126 deletions(-) delete mode 100644 src/lib/DBus.js create mode 100644 src/lib/DBus.ts delete mode 100644 src/lib/Form.js create mode 100644 src/lib/Form.ts delete mode 100644 src/lib/TouchBar.js create mode 100644 src/lib/TouchBar.ts (limited to 'src/lib') diff --git a/src/lib/DBus.js b/src/lib/DBus.js deleted file mode 100644 index 9baaea014..000000000 --- a/src/lib/DBus.js +++ /dev/null @@ -1,45 +0,0 @@ -import { sessionBus } from 'dbus-next'; -import { isLinux } from '../environment'; - -export default class DBus { - bus = null; - - constructor(trayIcon) { - this.trayIcon = trayIcon; - } - - start() { - if (!isLinux || this.bus) return; - - try { - this.bus = sessionBus(); - } catch { - // Error connecting to the bus. - return; - } - - // HACK Hook onto the MessageBus to track StatusNotifierWatchers - this.bus._addMatch("type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',path='/org/freedesktop/DBus',member='NameOwnerChanged'"); - const mangled = JSON.stringify({ - path: '/org/freedesktop/DBus', - interface: 'org.freedesktop.DBus', - member: 'NameOwnerChanged', - }); - this.bus._signals.on(mangled, (msg) => { - const [name, oldOwner, newOwner] = msg.body; - if (name === 'org.kde.StatusNotifierWatcher' && oldOwner !== newOwner && newOwner !== '') { - // Leave ample time for the StatusNotifierWatcher to be initialized - setTimeout(() => { - this.trayIcon.recreateIfVisible(); - }, 400); - } - }); - } - - stop() { - if (!this.bus) return; - - this.bus.disconnect(); - this.bus = null; - } -} diff --git a/src/lib/DBus.ts b/src/lib/DBus.ts new file mode 100644 index 000000000..b1febc2d1 --- /dev/null +++ b/src/lib/DBus.ts @@ -0,0 +1,55 @@ +import { MessageBus, sessionBus } from 'dbus-next'; +import { isLinux } from '../environment'; + +export default class DBus { + bus: MessageBus | null = null; + + trayIcon: any; + + constructor(trayIcon: any) { + this.trayIcon = trayIcon; + } + + start() { + if (!isLinux || this.bus) return; + + try { + this.bus = sessionBus(); + } catch { + // Error connecting to the bus. + return; + } + + // HACK Hook onto the MessageBus to track StatusNotifierWatchers + // @ts-expect-error Property '_addMatch' does not exist on type 'MessageBus'. + this.bus._addMatch( + "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',path='/org/freedesktop/DBus',member='NameOwnerChanged'", + ); + const mangled = JSON.stringify({ + path: '/org/freedesktop/DBus', + interface: 'org.freedesktop.DBus', + member: 'NameOwnerChanged', + }); + // @ts-expect-error Property '_signals' does not exist on type 'MessageBus'. + this.bus._signals.on(mangled, (msg: { body: [any, any, any] }) => { + const [name, oldOwner, newOwner] = msg.body; + if ( + name === 'org.kde.StatusNotifierWatcher' && + oldOwner !== newOwner && + newOwner !== '' + ) { + // Leave ample time for the StatusNotifierWatcher to be initialized + setTimeout(() => { + this.trayIcon.recreateIfVisible(); + }, 400); + } + }); + } + + stop() { + if (!this.bus) return; + + this.bus.disconnect(); + this.bus = null; + } +} diff --git a/src/lib/Form.js b/src/lib/Form.js deleted file mode 100644 index 9b8321948..000000000 --- a/src/lib/Form.js +++ /dev/null @@ -1,32 +0,0 @@ -import Form from 'mobx-react-form'; - -export default class DefaultForm extends Form { - bindings() { - return { - default: { - id: 'id', - name: 'name', - type: 'type', - value: 'value', - label: 'label', - placeholder: 'placeholder', - disabled: 'disabled', - onChange: 'onChange', - onFocus: 'onFocus', - onBlur: 'onBlur', - error: 'error', - }, - }; - } - - options() { - return { - validateOnInit: false, // default: true - // validateOnBlur: true, // default: true - // validateOnChange: true // default: false - // // validationDebounceWait: { - // // trailing: true, - // // }, - }; - } -} diff --git a/src/lib/Form.ts b/src/lib/Form.ts new file mode 100644 index 000000000..9b8321948 --- /dev/null +++ b/src/lib/Form.ts @@ -0,0 +1,32 @@ +import Form from 'mobx-react-form'; + +export default class DefaultForm extends Form { + bindings() { + return { + default: { + id: 'id', + name: 'name', + type: 'type', + value: 'value', + label: 'label', + placeholder: 'placeholder', + disabled: 'disabled', + onChange: 'onChange', + onFocus: 'onFocus', + onBlur: 'onBlur', + error: 'error', + }, + }; + } + + options() { + return { + validateOnInit: false, // default: true + // validateOnBlur: true, // default: true + // validateOnChange: true // default: false + // // validationDebounceWait: { + // // trailing: true, + // // }, + }; + } +} diff --git a/src/lib/TouchBar.js b/src/lib/TouchBar.js deleted file mode 100644 index c80931200..000000000 --- a/src/lib/TouchBar.js +++ /dev/null @@ -1,49 +0,0 @@ -import semver from 'semver'; -import { TouchBar, getCurrentWindow } from '@electron/remote'; -import { autorun } from 'mobx'; - -import { isMac, osRelease } from '../environment'; - -export default class FranzTouchBar { - constructor(stores, actions) { - this.stores = stores; - this.actions = actions; - - // Temporary fix for https://github.com/electron/electron/issues/10442 - // TODO: remove when we upgrade to electron 1.8.2 or later - try { - if (isMac && semver.gt(osRelease, '16.6.0')) { - this.build = autorun(this._build.bind(this)); - } - } catch (error) { - console.error(error); - } - } - - _build() { - const currentWindow = getCurrentWindow(); - - if (this.stores.user.isLoggedIn) { - const { TouchBarButton, TouchBarSpacer } = TouchBar; - - const buttons = []; - for (const service of this.stores.services.allDisplayed) { - buttons.push(new TouchBarButton({ - label: `${service.name}${service.unreadDirectMessageCount > 0 - ? ' 🔴' : ''} ${service.unreadDirectMessageCount === 0 - && service.unreadIndirectMessageCount > 0 - ? ' ⚪️' : ''}`, - backgroundColor: service.isActive ? '#3498DB' : null, - click: () => { - this.actions.service.setActive({ serviceId: service.id }); - }, - }), new TouchBarSpacer({ size: 'small' })); - } - - const touchBar = new TouchBar({ items: buttons }); - currentWindow.setTouchBar(touchBar); - } else { - currentWindow.setTouchBar(null); - } - } -} diff --git a/src/lib/TouchBar.ts b/src/lib/TouchBar.ts new file mode 100644 index 000000000..417e20411 --- /dev/null +++ b/src/lib/TouchBar.ts @@ -0,0 +1,62 @@ +import semver from 'semver'; +import { TouchBar, getCurrentWindow } from '@electron/remote'; +import { autorun } from 'mobx'; + +import { isMac, osRelease } from '../environment'; + +export default class FranzTouchBar { + stores: any; + + actions: any; + + build: any; + + constructor(stores: any, actions: any) { + this.stores = stores; + this.actions = actions; + + // Temporary fix for https://github.com/electron/electron/issues/10442 + // TODO: remove when we upgrade to electron 1.8.2 or later + try { + if (isMac && semver.gt(osRelease, '16.6.0')) { + this.build = autorun(this._build.bind(this)); + } + } catch (error) { + console.error(error); + } + } + + _build() { + const currentWindow = getCurrentWindow(); + + if (this.stores.user.isLoggedIn) { + const { TouchBarButton, TouchBarSpacer } = TouchBar; + + const buttons: any[] = []; + for (const service of this.stores.services.allDisplayed) { + buttons.push( + new TouchBarButton({ + label: `${service.name}${ + service.unreadDirectMessageCount > 0 ? ' 🔴' : '' + } ${ + service.unreadDirectMessageCount === 0 && + service.unreadIndirectMessageCount > 0 + ? ' ⚪️' + : '' + }`, + backgroundColor: service.isActive && '#3498DB', + click: () => { + this.actions.service.setActive({ serviceId: service.id }); + }, + }), + new TouchBarSpacer({ size: 'small' }), + ); + } + + const touchBar = new TouchBar({ items: buttons }); + currentWindow.setTouchBar(touchBar); + } else { + currentWindow.setTouchBar(null); + } + } +} -- cgit v1.2.3-54-g00ecf