mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-13 12:32:28 +08:00
feat(touchbar): basic implementation for modal buttons
This commit is contained in:
parent
ce86a2b077
commit
e6e2bde274
@ -15,6 +15,7 @@ export default class TouchBarWidget extends NoteContextAwareWidget {
|
|||||||
nativeImage: typeof import("electron").nativeImage;
|
nativeImage: typeof import("electron").nativeImage;
|
||||||
remote: typeof import("@electron/remote");
|
remote: typeof import("@electron/remote");
|
||||||
lastFocusedComponent?: Component;
|
lastFocusedComponent?: Component;
|
||||||
|
private $activeModal: JQuery<HTMLElement>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
@ -23,8 +24,10 @@ export default class TouchBarWidget extends NoteContextAwareWidget {
|
|||||||
this.$widget = $("<div>");
|
this.$widget = $("<div>");
|
||||||
|
|
||||||
$(window).on("focusin", async (e) => {
|
$(window).on("focusin", async (e) => {
|
||||||
const target = e.target;
|
const $target = $(e.target);
|
||||||
const parentComponentEl = $(target).closest(".component");
|
|
||||||
|
this.$activeModal = $target.closest(".modal-dialog");
|
||||||
|
const parentComponentEl = $target.closest(".component");
|
||||||
this.lastFocusedComponent = appContext.getComponentByEl(parentComponentEl[0]);
|
this.lastFocusedComponent = appContext.getComponentByEl(parentComponentEl[0]);
|
||||||
this.#refreshTouchBar();
|
this.#refreshTouchBar();
|
||||||
});
|
});
|
||||||
@ -52,18 +55,43 @@ export default class TouchBarWidget extends NoteContextAwareWidget {
|
|||||||
#refreshTouchBar() {
|
#refreshTouchBar() {
|
||||||
const { TouchBar } = this.remote;
|
const { TouchBar } = this.remote;
|
||||||
const parentComponent = this.lastFocusedComponent;
|
const parentComponent = this.lastFocusedComponent;
|
||||||
if (!parentComponent) {
|
let touchBar = null;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = parentComponent.triggerCommand("buildTouchBar", {
|
if (this.$activeModal.length > 0) {
|
||||||
|
touchBar = this.#buildModalTouchBar();
|
||||||
|
} else if (parentComponent) {
|
||||||
|
const items = parentComponent.triggerCommand("buildTouchBar", {
|
||||||
TouchBar,
|
TouchBar,
|
||||||
buildIcon: this.buildIcon.bind(this)
|
buildIcon: this.buildIcon.bind(this)
|
||||||
}) as unknown as TouchBarItem[];
|
}) as unknown as TouchBarItem[];
|
||||||
|
touchBar = this.#buildTouchBar(items);
|
||||||
|
}
|
||||||
|
|
||||||
const touchBar = this.#buildTouchBar(result);
|
if (touchBar) {
|
||||||
this.remote.getCurrentWindow().setTouchBar(touchBar);
|
this.remote.getCurrentWindow().setTouchBar(touchBar);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#buildModalTouchBar() {
|
||||||
|
const { TouchBar } = this.remote;
|
||||||
|
const { TouchBarButton, TouchBarSpacer } = this.remote.TouchBar;
|
||||||
|
const items: TouchBarItem[] = [];
|
||||||
|
|
||||||
|
items.push(new TouchBarSpacer({ size: "flexible" }));
|
||||||
|
|
||||||
|
// Look for buttons in the modal.
|
||||||
|
const $buttons = this.$activeModal.find(".modal-footer button");
|
||||||
|
for (const button of $buttons) {
|
||||||
|
items.push(new TouchBarButton({
|
||||||
|
label: button.innerText,
|
||||||
|
click: () => button.click(),
|
||||||
|
enabled: !button.hasAttribute("disabled")
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
items.push(new TouchBarSpacer({ size: "flexible" }));
|
||||||
|
return new TouchBar({ items });
|
||||||
|
}
|
||||||
|
|
||||||
#buildTouchBar(componentSpecificItems?: TouchBarItem[]) {
|
#buildTouchBar(componentSpecificItems?: TouchBarItem[]) {
|
||||||
const { TouchBar } = this.remote;
|
const { TouchBar } = this.remote;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user