From 07d059102d2fd147f13f67b56a9e0f761ce5260e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 13 Apr 2021 20:08:14 +0100 Subject: [PATCH 1/3] Initial TouchBar support containing breadcrumbs --- src/electron-main.js | 49 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/electron-main.js b/src/electron-main.js index 8ca13995d..eb64b24a6 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -28,7 +28,7 @@ const argv = require('minimist')(process.argv, { }); const { - app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog, + app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog, TouchBar, nativeImage, } = require('electron'); const AutoLaunch = require('auto-launch'); const path = require('path'); @@ -44,6 +44,7 @@ const Store = require('electron-store'); const fs = require('fs'); const afs = fs.promises; +const request = require("request"); const crypto = require('crypto'); let keytar; @@ -481,6 +482,52 @@ ipcMain.on('ipcCall', async function(ev, payload) { } catch (e) {} break; + case "breadcrumbs": { + const { TouchBarPopover, TouchBarButton } = TouchBar; + + const recentsBar = new TouchBar({ + items: args[0].map(r => { + const defaultColors = ['#0DBD8B', '#368bd6', '#ac3ba8']; + let total = 0; + for (let i = 0; i < r.roomId.length; ++i) { + total += r.roomId.charCodeAt(i); + } + + const button = new TouchBarButton({ + label: r.initial, + backgroundColor: defaultColors[total % defaultColors.length], + click: () => { + global.mainWindow.loadURL(`vector://vector/webapp/#/room/${r.roomId}`); + }, + }); + if (r.avatarUrl) { + request({ + url: r.avatarUrl, + encoding: null, + }, (err, resp, buffer) => { + if (err) return; + button.icon = nativeImage.createFromBuffer(buffer); + button.label = null; + button.backgroundColor = null; + }); + } + return button; + }), + }); + + const touchBar = new TouchBar({ + items: [ + new TouchBarPopover({ + label: "Recents", + showCloseButton: true, + items: recentsBar, + }), + ], + }); + global.mainWindow.setTouchBar(touchBar); + break; + } + default: mainWindow.webContents.send('ipcReply', { id: payload.id, From 26dabe5bd625997df8829ed831639054e570f030 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 16 Mar 2023 16:55:31 +0000 Subject: [PATCH 2/3] delint --- src/ipc.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ipc.ts b/src/ipc.ts index d1b610784..a2eed7081 100644 --- a/src/ipc.ts +++ b/src/ipc.ts @@ -200,7 +200,7 @@ ipcMain.on("ipcCall", async function (_ev: IpcMainEvent, payload) { const button = new TouchBarButton({ label: r.initial, backgroundColor: defaultColors[total % defaultColors.length], - click: () => { + click: (): void => { global.mainWindow?.loadURL(`vector://vector/webapp/#/room/${r.roomId}`); }, }); From cfb1602f05b9fa9565d9f8a46dee4ae25750cb96 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 22 Mar 2023 09:10:15 +0000 Subject: [PATCH 3/3] Iterate --- src/ipc.ts | 88 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/src/ipc.ts b/src/ipc.ts index a2eed7081..f0ed5cc51 100644 --- a/src/ipc.ts +++ b/src/ipc.ts @@ -187,50 +187,52 @@ ipcMain.on("ipcCall", async function (_ev: IpcMainEvent, payload) { break; case "breadcrumbs": { - const { TouchBarPopover, TouchBarButton } = TouchBar; - - const recentsBar = new TouchBar({ - items: args[0].map((r: { roomId: string; avatarUrl: string | null; initial: string }) => { - const defaultColors = ["#0DBD8B", "#368bd6", "#ac3ba8"]; - let total = 0; - for (let i = 0; i < r.roomId.length; ++i) { - total += r.roomId.charCodeAt(i); - } - - const button = new TouchBarButton({ - label: r.initial, - backgroundColor: defaultColors[total % defaultColors.length], - click: (): void => { - global.mainWindow?.loadURL(`vector://vector/webapp/#/room/${r.roomId}`); - }, - }); - if (r.avatarUrl) { - fetch(r.avatarUrl) - .then((resp) => { - if (!resp.ok) return; - return resp.arrayBuffer(); - }) - .then((arrayBuffer) => { - const buffer = Buffer.from(arrayBuffer!); - button.icon = nativeImage.createFromBuffer(buffer); - button.label = ""; - button.backgroundColor = ""; - }); - } - return button; - }), - }); - - const touchBar = new TouchBar({ - items: [ - new TouchBarPopover({ - label: "Recents", - showCloseButton: true, - items: recentsBar, + if (process.platform === "darwin") { + const { TouchBarPopover, TouchBarButton } = TouchBar; + + const recentsBar = new TouchBar({ + items: args[0].map((r: { roomId: string; avatarUrl: string | null; initial: string }) => { + const defaultColors = ["#0DBD8B", "#368bd6", "#ac3ba8"]; + let total = 0; + for (let i = 0; i < r.roomId.length; ++i) { + total += r.roomId.charCodeAt(i); + } + + const button = new TouchBarButton({ + label: r.initial, + backgroundColor: defaultColors[total % defaultColors.length], + click: (): void => { + global.mainWindow?.loadURL(`vector://vector/webapp/#/room/${r.roomId}`); + }, + }); + if (r.avatarUrl) { + fetch(r.avatarUrl) + .then((resp) => { + if (!resp.ok) return; + return resp.arrayBuffer(); + }) + .then((arrayBuffer) => { + const buffer = Buffer.from(arrayBuffer!); + button.icon = nativeImage.createFromBuffer(buffer); + button.label = ""; + button.backgroundColor = ""; + }); + } + return button; }), - ], - }); - global.mainWindow.setTouchBar(touchBar); + }); + + const touchBar = new TouchBar({ + items: [ + new TouchBarPopover({ + label: "Recents", + showCloseButton: true, + items: recentsBar, + }), + ], + }); + global.mainWindow.setTouchBar(touchBar); + } break; }