mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-28 02:31:38 +08:00
refactor(touch_bar): command-driven approach
This commit is contained in:
parent
214674cf73
commit
db8d47183d
@ -22,10 +22,9 @@ import type LoadResults from "../services/load_results.js";
|
|||||||
import type { Attribute } from "../services/attribute_parser.js";
|
import type { Attribute } from "../services/attribute_parser.js";
|
||||||
import type NoteTreeWidget from "../widgets/note_tree.js";
|
import type NoteTreeWidget from "../widgets/note_tree.js";
|
||||||
import type { default as NoteContext, GetTextEditorCallback } from "./note_context.js";
|
import type { default as NoteContext, GetTextEditorCallback } from "./note_context.js";
|
||||||
import type { ContextMenuEvent } from "../menus/context_menu.js";
|
|
||||||
import type TypeWidget from "../widgets/type_widgets/type_widget.js";
|
import type TypeWidget from "../widgets/type_widgets/type_widget.js";
|
||||||
import type EditableTextTypeWidget from "../widgets/type_widgets/editable_text.js";
|
import type EditableTextTypeWidget from "../widgets/type_widgets/editable_text.js";
|
||||||
import TouchBarWidget from "../widgets/touch_bar.js";
|
import type { NativeImage } from "electron";
|
||||||
|
|
||||||
interface Layout {
|
interface Layout {
|
||||||
getRootWidget: (appContext: AppContext) => RootWidget;
|
getRootWidget: (appContext: AppContext) => RootWidget;
|
||||||
@ -256,6 +255,11 @@ export type CommandMappings = {
|
|||||||
|
|
||||||
refreshResults: {};
|
refreshResults: {};
|
||||||
refreshSearchDefinition: {};
|
refreshSearchDefinition: {};
|
||||||
|
|
||||||
|
buildTouchBar: CommandData & {
|
||||||
|
TouchBar: typeof import("electron").TouchBar;
|
||||||
|
buildIcon(name: string): NativeImage;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
type EventMappings = {
|
type EventMappings = {
|
||||||
@ -448,10 +452,6 @@ class AppContext extends Component {
|
|||||||
|
|
||||||
this.components = [this.tabManager, new RootCommandExecutor(), new Entrypoints(), new MainTreeExecutors(), new ShortcutComponent()];
|
this.components = [this.tabManager, new RootCommandExecutor(), new Entrypoints(), new MainTreeExecutors(), new ShortcutComponent()];
|
||||||
|
|
||||||
if (utils.isElectron() && utils.isMac()) {
|
|
||||||
this.components.push(new TouchBarWidget());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (utils.isMobile()) {
|
if (utils.isMobile()) {
|
||||||
this.components.push(new MobileScreenSwitcherExecutor());
|
this.components.push(new MobileScreenSwitcherExecutor());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,6 +88,7 @@ import utils from "../services/utils.js";
|
|||||||
import GeoMapButtons from "../widgets/floating_buttons/geo_map_button.js";
|
import GeoMapButtons from "../widgets/floating_buttons/geo_map_button.js";
|
||||||
import ContextualHelpButton from "../widgets/floating_buttons/help_button.js";
|
import ContextualHelpButton from "../widgets/floating_buttons/help_button.js";
|
||||||
import CloseZenButton from "../widgets/close_zen_button.js";
|
import CloseZenButton from "../widgets/close_zen_button.js";
|
||||||
|
import TouchBarWidget from "../widgets/touch_bar.js";
|
||||||
|
|
||||||
export default class DesktopLayout {
|
export default class DesktopLayout {
|
||||||
constructor(customWidgets) {
|
constructor(customWidgets) {
|
||||||
@ -153,6 +154,7 @@ export default class DesktopLayout {
|
|||||||
.filling()
|
.filling()
|
||||||
.collapsible()
|
.collapsible()
|
||||||
.id("center-pane")
|
.id("center-pane")
|
||||||
|
.child(new TouchBarWidget())
|
||||||
.child(
|
.child(
|
||||||
new SplitNoteContainer(() =>
|
new SplitNoteContainer(() =>
|
||||||
new NoteWrapperWidget()
|
new NoteWrapperWidget()
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import utils from "../services/utils.js";
|
import utils from "../services/utils.js";
|
||||||
import Component from "../components/component.js";
|
import Component from "../components/component.js";
|
||||||
import appContext from "../components/app_context.js";
|
import appContext from "../components/app_context.js";
|
||||||
|
import NoteContextAwareWidget from "./note_context_aware_widget.js";
|
||||||
|
import type FNote from "../entities/fnote.js";
|
||||||
|
|
||||||
async function triggerTextEditorCommand(command: string, args?: object) {
|
async function triggerTextEditorCommand(command: string, args?: object) {
|
||||||
const editor = await appContext.tabManager.getActiveContext().getTextEditor();
|
const editor = await appContext.tabManager.getActiveContext().getTextEditor();
|
||||||
@ -12,7 +14,7 @@ async function triggerTextEditorCommand(command: string, args?: object) {
|
|||||||
(editor as any).execute(command, args);
|
(editor as any).execute(command, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class TouchBarWidget extends Component {
|
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");
|
||||||
@ -21,15 +23,30 @@ export default class TouchBarWidget extends Component {
|
|||||||
super();
|
super();
|
||||||
this.nativeImage = utils.dynamicRequire("electron").nativeImage;
|
this.nativeImage = utils.dynamicRequire("electron").nativeImage;
|
||||||
this.remote = utils.dynamicRequire("@electron/remote") as typeof import("@electron/remote");
|
this.remote = utils.dynamicRequire("@electron/remote") as typeof import("@electron/remote");
|
||||||
this.#setTouchBar();
|
this.$widget = $("<div>");
|
||||||
|
|
||||||
|
$(window).on("focusin", async (e) => {
|
||||||
|
const target = e.target;
|
||||||
|
const parentComponentEl = $(target).closest(".component");
|
||||||
|
// TODO: Remove typecast once it's no longer necessary.
|
||||||
|
const parentComponent = appContext.getComponentByEl(parentComponentEl[0]) as Component;
|
||||||
|
const { TouchBar } = this.remote;
|
||||||
|
if (!parentComponent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = parentComponent.triggerCommand("buildTouchBar", {
|
||||||
|
TouchBar,
|
||||||
|
buildIcon: this.buildIcon.bind(this)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
this.remote.getCurrentWindow().setTouchBar(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#setTouchBar() {
|
buildIcon(name: string) {
|
||||||
const touchBarData = this.#buildTouchBar();
|
|
||||||
this.remote.getCurrentWindow().setTouchBar(touchBarData);
|
|
||||||
}
|
|
||||||
|
|
||||||
#buildIcon(name: string) {
|
|
||||||
const sourceImage = this.nativeImage.createFromNamedImage(name, [-1, 0, 1]);
|
const sourceImage = this.nativeImage.createFromNamedImage(name, [-1, 0, 1]);
|
||||||
const { width, height } = sourceImage.getSize();
|
const { width, height } = sourceImage.getSize();
|
||||||
const newImage = this.nativeImage.createEmpty();
|
const newImage = this.nativeImage.createEmpty();
|
||||||
@ -48,58 +65,21 @@ export default class TouchBarWidget extends Component {
|
|||||||
return newImage;
|
return newImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
#buildTouchBar() {
|
#buildTextTouchBar() {
|
||||||
const { TouchBar } = this.remote;
|
const { TouchBar } = this.remote;
|
||||||
const { TouchBarButton, TouchBarSpacer, TouchBarGroup, TouchBarSegmentedControl, TouchBarOtherItemsProxy } = this.remote.TouchBar;
|
const { TouchBarButton, TouchBarSpacer, TouchBarGroup, TouchBarSegmentedControl, TouchBarOtherItemsProxy } = this.remote.TouchBar;
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
new TouchBarButton({
|
new TouchBarButton({
|
||||||
icon: this.#buildIcon("NSTouchBarComposeTemplate"),
|
icon: this.buildIcon("NSTouchBarComposeTemplate"),
|
||||||
click: () => this.triggerCommand("createNoteIntoInbox")
|
click: () => this.triggerCommand("createNoteIntoInbox")
|
||||||
}),
|
}),
|
||||||
new TouchBarSpacer({ size: "large" }),
|
new TouchBarSpacer({ size: "large" }),
|
||||||
new TouchBarSegmentedControl({
|
// data should go here
|
||||||
segments: [
|
|
||||||
{ label: "P" },
|
|
||||||
{ label: "H2" },
|
|
||||||
{ label: "H3" }
|
|
||||||
],
|
|
||||||
change(selectedIndex, isSelected) {
|
|
||||||
switch (selectedIndex) {
|
|
||||||
case 0:
|
|
||||||
triggerTextEditorCommand("paragraph")
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
triggerTextEditorCommand("heading", { value: "heading2" });
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
triggerTextEditorCommand("heading", { value: "heading3" });
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
new TouchBarGroup({
|
|
||||||
items: new TouchBar({
|
|
||||||
items: [
|
|
||||||
new TouchBarButton({
|
|
||||||
icon: this.#buildIcon("NSTouchBarTextBoldTemplate"),
|
|
||||||
click: () => triggerTextEditorCommand("bold")
|
|
||||||
}),
|
|
||||||
new TouchBarButton({
|
|
||||||
icon: this.#buildIcon("NSTouchBarTextItalicTemplate"),
|
|
||||||
click: () => triggerTextEditorCommand("italic")
|
|
||||||
}),
|
|
||||||
new TouchBarButton({
|
|
||||||
icon: this.#buildIcon("NSTouchBarTextUnderlineTemplate"),
|
|
||||||
click: () => triggerTextEditorCommand("underline")
|
|
||||||
})
|
|
||||||
]
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
new TouchBarOtherItemsProxy(),
|
new TouchBarOtherItemsProxy(),
|
||||||
new TouchBarSpacer({ size: "flexible" }),
|
new TouchBarSpacer({ size: "flexible" }),
|
||||||
new TouchBarButton({
|
new TouchBarButton({
|
||||||
icon: this.#buildIcon("NSTouchBarAddDetailTemplate"),
|
icon: this.buildIcon("NSTouchBarAddDetailTemplate"),
|
||||||
click: () => this.triggerCommand("jumpToNote")
|
click: () => this.triggerCommand("jumpToNote")
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
|||||||
@ -507,4 +507,49 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
await this.reinitialize(data);
|
await this.reinitialize(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildTouchBarCommand(data) {
|
||||||
|
const { TouchBar, buildIcon } = data;
|
||||||
|
const { TouchBarSegmentedControl, TouchBarGroup, TouchBarButton } = TouchBar;
|
||||||
|
return [
|
||||||
|
new TouchBarSegmentedControl({
|
||||||
|
segments: [
|
||||||
|
{ label: "P" },
|
||||||
|
{ label: "H2" },
|
||||||
|
{ label: "H3" }
|
||||||
|
],
|
||||||
|
change(selectedIndex, isSelected) {
|
||||||
|
switch (selectedIndex) {
|
||||||
|
case 0:
|
||||||
|
triggerTextEditorCommand("paragraph")
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
triggerTextEditorCommand("heading", { value: "heading2" });
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
triggerTextEditorCommand("heading", { value: "heading3" });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
new TouchBarGroup({
|
||||||
|
items: new TouchBar({
|
||||||
|
items: [
|
||||||
|
new TouchBarButton({
|
||||||
|
icon: buildIcon("NSTouchBarTextBoldTemplate"),
|
||||||
|
click: () => triggerTextEditorCommand("bold")
|
||||||
|
}),
|
||||||
|
new TouchBarButton({
|
||||||
|
icon: buildIcon("NSTouchBarTextItalicTemplate"),
|
||||||
|
click: () => triggerTextEditorCommand("italic")
|
||||||
|
}),
|
||||||
|
new TouchBarButton({
|
||||||
|
icon: buildIcon("NSTouchBarTextUnderlineTemplate"),
|
||||||
|
click: () => triggerTextEditorCommand("underline")
|
||||||
|
})
|
||||||
|
]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user