From d9a689bd9a76f8bfc88d6d4aad9a61287bb1f26a Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 8 Mar 2025 12:34:06 +0200 Subject: [PATCH] feat(touch_bar): functional bold, italic, underline --- src/public/app/widgets/touch_bar.ts | 47 ++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/public/app/widgets/touch_bar.ts b/src/public/app/widgets/touch_bar.ts index 92b1ab060..7103c1988 100644 --- a/src/public/app/widgets/touch_bar.ts +++ b/src/public/app/widgets/touch_bar.ts @@ -1,5 +1,6 @@ import utils from "../services/utils.js"; import Component from "../components/component.js"; +import appContext from "../components/app_context.js"; export default class TouchBarWidget extends Component { @@ -20,30 +21,60 @@ export default class TouchBarWidget extends Component { #buildIcon(name: string) { const sourceImage = this.nativeImage.createFromNamedImage(name, [-1, 0, 1]); - const newImage = this.nativeImage.createEmpty() + const { width, height } = sourceImage.getSize(); + const newImage = this.nativeImage.createEmpty(); newImage.addRepresentation({ scaleFactor: 1, - width: 22, - height: 22, - buffer: sourceImage.resize({ height: 22 }).toBitmap() + width: width / 2, + height: height / 2, + buffer: sourceImage.resize({ height: height / 2 }).toBitmap() }); newImage.addRepresentation({ scaleFactor: 2, - width: 44, - height: 44, - buffer: sourceImage.resize({ height: 44 }).toBitmap() + width: width, + height: height, + buffer: sourceImage.toBitmap() }); 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); + } + #buildTouchBar() { const { TouchBar } = this.remote; - const { TouchBarButton } = this.remote.TouchBar; + const { TouchBarButton, TouchBarSpacer, TouchBarGroup } = this.remote.TouchBar; const items = [ new TouchBarButton({ icon: this.#buildIcon("NSTouchBarComposeTemplate"), click: () => this.triggerCommand("createNoteIntoInbox") + }), + 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") + }) + ] + }) }) ];