Notes/src/public/app/widgets/touch_bar.ts

93 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-03-08 11:39:04 +02:00
import utils from "../services/utils.js";
import Component from "../components/component.js";
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);
}
#buildIcon(name: string) {
2025-03-08 12:01:32 +02:00
const sourceImage = this.nativeImage.createFromNamedImage(name, [-1, 0, 1]);
const { width, height } = sourceImage.getSize();
const newImage = this.nativeImage.createEmpty();
2025-03-08 12:01:32 +02:00
newImage.addRepresentation({
scaleFactor: 1,
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,
width: width,
height: height,
buffer: sourceImage.toBitmap()
2025-03-08 12:01:32 +02:00
});
return newImage;
}
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;
const { TouchBarButton, TouchBarSpacer, TouchBarGroup } = this.remote.TouchBar;
2025-03-08 11:39:04 +02:00
const items = [
new TouchBarButton({
icon: this.#buildIcon("NSTouchBarComposeTemplate"),
click: () => this.triggerCommand("createNoteIntoInbox")
}),
2025-03-08 13:13:21 +02:00
new TouchBarSpacer({ size: "flexible" }),
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 13:13:21 +02:00
}),
new TouchBarSpacer({ size: "flexible" }),
new TouchBarButton({
icon: this.#buildIcon("NSTouchBarAddDetailTemplate"),
click: () => this.triggerCommand("jumpToNote")
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
});
}
}