feat(touch_bar): run button for scripts

This commit is contained in:
Elian Doran 2025-03-09 15:31:03 +02:00
parent 615a5f7222
commit 975e641286
No known key found for this signature in database
2 changed files with 27 additions and 4 deletions

View File

@ -4,6 +4,8 @@ import appContext from "../components/app_context.js";
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import type { TouchBarButton, TouchBarGroup, TouchBarSegmentedControl, TouchBarSpacer } from "@electron/remote";
export type TouchBarItem = (TouchBarButton | TouchBarSpacer | TouchBarGroup | TouchBarSegmentedControl);
export function buildSelectedBackgroundColor(isSelected: boolean) {
return isSelected ? "#757575" : undefined;
}
@ -58,13 +60,13 @@ export default class TouchBarWidget extends NoteContextAwareWidget {
let result = parentComponent.triggerCommand("buildTouchBar", {
TouchBar,
buildIcon: this.buildIcon.bind(this)
});
}) as unknown as TouchBarItem[];
const touchBar = this.#buildTouchBar(result);
this.remote.getCurrentWindow().setTouchBar(touchBar);
}
#buildTouchBar(componentSpecificItems?: (TouchBarButton | TouchBarSpacer | TouchBarGroup | TouchBarSegmentedControl)[]) {
#buildTouchBar(componentSpecificItems?: TouchBarItem[]) {
const { TouchBar } = this.remote;
const { TouchBarButton, TouchBarSpacer, TouchBarGroup, TouchBarSegmentedControl, TouchBarOtherItemsProxy } = this.remote.TouchBar;
@ -81,7 +83,6 @@ export default class TouchBarWidget extends NoteContextAwareWidget {
new TouchBarSpacer({ size: "large" }),
...componentSpecificItems,
new TouchBarOtherItemsProxy(),
new TouchBarSpacer({ size: "flexible" }),
new TouchBarButton({
icon: this.buildIcon("NSTouchBarAddDetailTemplate"),
click: () => this.triggerCommand("jumpToNote")

View File

@ -1,9 +1,12 @@
import type { EventData } from "../../components/app_context.js";
import { app } from "electron";
import type { CommandListenerData, EventData } from "../../components/app_context.js";
import type FNote from "../../entities/fnote.js";
import { t } from "../../services/i18n.js";
import keyboardActionService from "../../services/keyboard_actions.js";
import options from "../../services/options.js";
import AbstractCodeTypeWidget from "./abstract_code_type_widget.js";
import appContext from "../../components/app_context.js";
import type { TouchBarItem } from "../touch_bar.js";
const TPL = `
<div class="note-detail-code note-detail-printable">
@ -63,6 +66,8 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
});
this.show();
this.triggerCommand("refreshTouchBar");
}
getData() {
@ -80,4 +85,21 @@ export default class EditableCodeTypeWidget extends AbstractCodeTypeWidget {
resolve(this.codeEditor);
}
buildTouchBarCommand({ TouchBar, buildIcon }: CommandListenerData<"buildTouchBar">) {
const items: TouchBarItem[] = [
new TouchBar.TouchBarSpacer({ size: "flexible" }),
];
const note = this.note;
if (note?.mime.startsWith("application/javascript") || note?.mime === "text/x-sqlite;schema=trilium") {
items.push(new TouchBar.TouchBarButton({
icon: buildIcon("NSImageNameTouchBarPlayTemplate"),
click: () => appContext.triggerCommand("runActiveNote")
}));
}
return items;
}
}