Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

feat(macos): start the menu-bar icon near system controls (#2267)

Joshua4fb3277a816c
diff --git a/src/helpers/tray.js b/src/helpers/tray.js
index 58cb1a798..76ca273b1 100644
--- a/src/helpers/tray.js
+++ b/src/helpers/tray.js
@@ -1,10 +1,16 @@
-const { Tray, Menu, nativeImage, app } = require("electron");
+const { Tray, Menu, nativeImage, app, systemPreferences } = require("electron");
const path = require("path");
const fs = require("fs");
const debugLogger = require("./debugLogger");
const dockManager = require("./dockManager");
const { i18nMain } = require("./i18nMain");
+// macOS saves the menu-bar position under this GUID, so changing it resets every
+// user's placement. Electron lowercases the GUID before handing it to macOS, so
+// it stays lowercase here or the position key below matches no item.
+const MACOS_TRAY_GUID = "eb809902-04b5-5b08-b12a-f81d6f27e185";
+const MACOS_TRAY_POSITION_KEY = `NSStatusItem Preferred Position ${MACOS_TRAY_GUID}`;
+
class TrayManager {
constructor() {
this.tray = null;
@@ -138,10 +144,16 @@ class TrayManager {
return;
}
- this.tray = new Tray(trayIcon);
-
if (process.platform === "darwin") {
+ // The position key is an undocumented AppKit default, so placement is best
+ // effort. Position 0 starts the icon as far right as macOS allows, beside
+ // the system icons. A registered default only fills in for a missing value,
+ // so once the user drags the icon, their saved position wins.
+ systemPreferences.registerDefaults({ [MACOS_TRAY_POSITION_KEY]: 0 });
+ this.tray = new Tray(trayIcon, MACOS_TRAY_GUID);
this.tray.setIgnoreDoubleClickEvents(true);
+ } else {
+ this.tray = new Tray(trayIcon);
}
this.updateTrayMenu();
diff --git a/test/helpers/trayPlacement.test.js b/test/helpers/trayPlacement.test.js
new file mode 100644
index 000000000..60c44228b
--- /dev/null
+++ b/test/helpers/trayPlacement.test.js
@@ -0,0 +1,75 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const path = require("node:path");
+const vm = require("node:vm");
+
+// Spelled out rather than imported: a new GUID resets every user's saved
+// menu-bar position, so changing it should fail here.
+const TRAY_GUID = "eb809902-04b5-5b08-b12a-f81d6f27e185";
+const trayPath = path.join(__dirname, "../../src/helpers/tray.js");
+const traySource = fs.readFileSync(trayPath, "utf8");
+
+// Runs tray.js in its own context so each test picks a platform without
+// touching the real process.platform.
+async function createTrayOn(platform) {
+ const calls = [];
+ const icon = { isEmpty: () => false };
+ const stubs = {
+ electron: {
+ Tray: class {
+ constructor(...args) {
+ calls.push(["construct", ...args]);
+ }
+ setIgnoreDoubleClickEvents(ignore) {
+ calls.push(["ignoreDoubleClick", ignore]);
+ }
+ setToolTip() {}
+ setContextMenu() {}
+ on() {}
+ },
+ Menu: { buildFromTemplate: () => ({}) },
+ systemPreferences: {
+ // Copied because the object comes from the VM's realm, whose prototype
+ // deepStrictEqual would reject.
+ registerDefaults: (defaults) => calls.push(["register", { ...defaults }]),
+ },
+ },
+ "./debugLogger": { error: (...args) => calls.push(["error", ...args]) },
+ "./dockManager": {},
+ "./i18nMain": { i18nMain: { t: (key) => key } },
+ };
+ const loadedModule = { exports: {} };
+ vm.runInNewContext(
+ traySource,
+ {
+ module: loadedModule,
+ process: { platform },
+ require: (specifier) => stubs[specifier] ?? require(specifier),
+ },
+ { filename: trayPath }
+ );
+
+ const trayManager = new loadedModule.exports();
+ trayManager.loadTrayIcon = async () => icon;
+ await trayManager.createTray();
+ return { calls, icon };
+}
+
+test("macOS registers the starting position before creating its tray under the fixed GUID", async () => {
+ const { calls, icon } = await createTrayOn("darwin");
+
+ assert.deepEqual(calls, [
+ ["register", { [`NSStatusItem Preferred Position ${TRAY_GUID}`]: 0 }],
+ ["construct", icon, TRAY_GUID],
+ ["ignoreDoubleClick", true],
+ ]);
+});
+
+for (const platform of ["win32", "linux"]) {
+ test(`${platform} creates its tray without a GUID or a starting position`, async () => {
+ const { calls, icon } = await createTrayOn(platform);
+
+ assert.deepEqual(calls, [["construct", icon]]);
+ });
+}