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

57 lines
1.6 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";
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 newImage = this.nativeImage.createEmpty()
newImage.addRepresentation({
scaleFactor: 1,
width: 22,
height: 22,
buffer: sourceImage.resize({ height: 22 }).toBitmap()
});
newImage.addRepresentation({
scaleFactor: 2,
width: 44,
height: 44,
buffer: sourceImage.resize({ height: 44 }).toBitmap()
});
return newImage;
}
2025-03-08 11:39:04 +02:00
#buildTouchBar() {
2025-03-08 11:51:55 +02:00
const { TouchBar } = this.remote;
2025-03-08 11:39:04 +02:00
const { TouchBarButton } = this.remote.TouchBar;
const items = [
new TouchBarButton({
icon: this.#buildIcon("NSTouchBarComposeTemplate"),
click: () => this.triggerCommand("createNoteIntoInbox")
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
});
}
}