2025-03-08 11:39:04 +02:00
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import Component from "../components/component.js";
|
2025-03-08 12:34:06 +02:00
|
|
|
import appContext from "../components/app_context.js";
|
2025-03-08 11:39:04 +02:00
|
|
|
|
|
|
|
export default class TouchBarWidget extends Component {
|
|
|
|
|
2025-03-08 11:51:55 +02:00
|
|
|
nativeImage: typeof import("electron").nativeImage;
|
2025-03-08 11:39:04 +02:00
|
|
|
remote: typeof import("@electron/remote");
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2025-03-08 11:51:55 +02:00
|
|
|
this.nativeImage = utils.dynamicRequire("electron").nativeImage;
|
2025-03-08 11:39:04 +02:00
|
|
|
this.remote = utils.dynamicRequire("@electron/remote") as typeof import("@electron/remote");
|
|
|
|
this.#setTouchBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
#setTouchBar() {
|
|
|
|
const touchBarData = this.#buildTouchBar();
|
|
|
|
this.remote.getCurrentWindow().setTouchBar(touchBarData);
|
|
|
|
}
|
|
|
|
|
2025-03-08 11:55:09 +02:00
|
|
|
#buildIcon(name: string) {
|
2025-03-08 12:01:32 +02:00
|
|
|
const sourceImage = this.nativeImage.createFromNamedImage(name, [-1, 0, 1]);
|
2025-03-08 12:34:06 +02:00
|
|
|
const { width, height } = sourceImage.getSize();
|
|
|
|
const newImage = this.nativeImage.createEmpty();
|
2025-03-08 12:01:32 +02:00
|
|
|
newImage.addRepresentation({
|
|
|
|
scaleFactor: 1,
|
2025-03-08 12:34:06 +02:00
|
|
|
width: width / 2,
|
|
|
|
height: height / 2,
|
|
|
|
buffer: sourceImage.resize({ height: height / 2 }).toBitmap()
|
2025-03-08 12:01:32 +02:00
|
|
|
});
|
|
|
|
newImage.addRepresentation({
|
|
|
|
scaleFactor: 2,
|
2025-03-08 12:34:06 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
buffer: sourceImage.toBitmap()
|
2025-03-08 12:01:32 +02:00
|
|
|
});
|
|
|
|
return newImage;
|
2025-03-08 11:55:09 +02:00
|
|
|
}
|
|
|
|
|
2025-03-08 12:34:06 +02:00
|
|
|
async #triggerTextEditorCommand(command: string) {
|
|
|
|
const editor = await appContext.tabManager.getActiveContext().getTextEditor();
|
|
|
|
if (!editor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fix type of editor.
|
|
|
|
(editor as any).execute(command);
|
|
|
|
}
|
|
|
|
|
2025-03-08 11:39:04 +02:00
|
|
|
#buildTouchBar() {
|
2025-03-08 11:51:55 +02:00
|
|
|
const { TouchBar } = this.remote;
|
2025-03-08 12:34:06 +02:00
|
|
|
const { TouchBarButton, TouchBarSpacer, TouchBarGroup } = this.remote.TouchBar;
|
2025-03-08 11:39:04 +02:00
|
|
|
|
|
|
|
const items = [
|
|
|
|
new TouchBarButton({
|
2025-03-08 12:06:35 +02:00
|
|
|
icon: this.#buildIcon("NSTouchBarComposeTemplate"),
|
|
|
|
click: () => this.triggerCommand("createNoteIntoInbox")
|
2025-03-08 12:34:06 +02:00
|
|
|
}),
|
|
|
|
new TouchBarSpacer({ }),
|
|
|
|
new TouchBarGroup({
|
|
|
|
items: new TouchBar({
|
|
|
|
items: [
|
|
|
|
new TouchBarButton({
|
|
|
|
icon: this.#buildIcon("NSTouchBarTextBoldTemplate"),
|
|
|
|
click: () => this.#triggerTextEditorCommand("bold")
|
|
|
|
}),
|
|
|
|
new TouchBarButton({
|
|
|
|
icon: this.#buildIcon("NSTouchBarTextItalicTemplate"),
|
|
|
|
click: () => this.#triggerTextEditorCommand("italic")
|
|
|
|
}),
|
|
|
|
new TouchBarButton({
|
|
|
|
icon: this.#buildIcon("NSTouchBarTextUnderlineTemplate"),
|
|
|
|
click: () => this.#triggerTextEditorCommand("underline")
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
2025-03-08 11:39:04 +02:00
|
|
|
})
|
|
|
|
];
|
|
|
|
|
2025-03-08 11:51:55 +02:00
|
|
|
console.log("Update ", items);
|
|
|
|
return new TouchBar({
|
2025-03-08 11:39:04 +02:00
|
|
|
items
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|