Skip to content

Commit 3b08d4f

Browse files
authored
Show recent room breadcrumbs on touchbar (#183)
1 parent 775b035 commit 3b08d4f

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

Diff for: src/ipc.ts

+51-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { app, autoUpdater, desktopCapturer, ipcMain, powerSaveBlocker } from "electron";
17+
import { app, autoUpdater, desktopCapturer, ipcMain, powerSaveBlocker, TouchBar, nativeImage } from "electron";
1818
import { relaunchApp } from "electron-clear-data";
1919

2020
import IpcMainEvent = Electron.IpcMainEvent;
@@ -194,6 +194,56 @@ ipcMain.on("ipcCall", async function (_ev: IpcMainEvent, payload) {
194194
relaunchApp();
195195
break;
196196

197+
case "breadcrumbs": {
198+
if (process.platform === "darwin") {
199+
const { TouchBarPopover, TouchBarButton } = TouchBar;
200+
201+
const recentsBar = new TouchBar({
202+
items: args[0].map((r: { roomId: string; avatarUrl: string | null; initial: string }) => {
203+
const defaultColors = ["#0DBD8B", "#368bd6", "#ac3ba8"];
204+
let total = 0;
205+
for (let i = 0; i < r.roomId.length; ++i) {
206+
total += r.roomId.charCodeAt(i);
207+
}
208+
209+
const button = new TouchBarButton({
210+
label: r.initial,
211+
backgroundColor: defaultColors[total % defaultColors.length],
212+
click: (): void => {
213+
global.mainWindow?.loadURL(`vector://vector/webapp/#/room/${r.roomId}`);
214+
},
215+
});
216+
if (r.avatarUrl) {
217+
fetch(r.avatarUrl)
218+
.then((resp) => {
219+
if (!resp.ok) return;
220+
return resp.arrayBuffer();
221+
})
222+
.then((arrayBuffer) => {
223+
const buffer = Buffer.from(arrayBuffer!);
224+
button.icon = nativeImage.createFromBuffer(buffer);
225+
button.label = "";
226+
button.backgroundColor = "";
227+
});
228+
}
229+
return button;
230+
}),
231+
});
232+
233+
const touchBar = new TouchBar({
234+
items: [
235+
new TouchBarPopover({
236+
label: "Recents",
237+
showCloseButton: true,
238+
items: recentsBar,
239+
}),
240+
],
241+
});
242+
global.mainWindow.setTouchBar(touchBar);
243+
}
244+
break;
245+
}
246+
197247
default:
198248
global.mainWindow.webContents.send("ipcReply", {
199249
id: payload.id,

0 commit comments

Comments
 (0)