From d1df365e094e60403e8c5d8419ef669b4a437e15 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 13 Apr 2025 21:18:43 +0300 Subject: [PATCH] feat(touchbar): calendar view --- src/public/app/widgets/note_list.ts | 9 ++- .../app/widgets/view_widgets/calendar_view.ts | 67 ++++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/public/app/widgets/note_list.ts b/src/public/app/widgets/note_list.ts index 9306b6ca9..73ebb358d 100644 --- a/src/public/app/widgets/note_list.ts +++ b/src/public/app/widgets/note_list.ts @@ -1,7 +1,7 @@ import NoteContextAwareWidget from "./note_context_aware_widget.js"; import NoteListRenderer from "../services/note_list_renderer.js"; import type FNote from "../entities/fnote.js"; -import type { EventData } from "../components/app_context.js"; +import type { CommandListener, CommandListenerData, EventData } from "../components/app_context.js"; import type ViewMode from "./view_widgets/view_mode.js"; const TPL = /*html*/` @@ -127,4 +127,11 @@ export default class NoteListWidget extends NoteContextAwareWidget { this.checkRenderStatus(); } } + + buildTouchBarCommand(data: CommandListenerData<"buildTouchBar">) { + if (this.viewMode && "buildTouchBarCommand" in this.viewMode) { + return (this.viewMode as CommandListener<"buildTouchBar">).buildTouchBarCommand(data); + } + } + } diff --git a/src/public/app/widgets/view_widgets/calendar_view.ts b/src/public/app/widgets/view_widgets/calendar_view.ts index 22689af8c..83c12cc24 100644 --- a/src/public/app/widgets/view_widgets/calendar_view.ts +++ b/src/public/app/widgets/view_widgets/calendar_view.ts @@ -7,12 +7,14 @@ import { t } from "../../services/i18n.js"; import options from "../../services/options.js"; import dialogService from "../../services/dialog.js"; import attributes from "../../services/attributes.js"; -import type { EventData } from "../../components/app_context.js"; +import type { CommandListenerData, EventData } from "../../components/app_context.js"; import utils from "../../services/utils.js"; import date_notes from "../../services/date_notes.js"; import appContext from "../../components/app_context.js"; import type { EventImpl } from "@fullcalendar/core/internal"; import debounce, { type DebouncedFunction } from "debounce"; +import type { TouchBarItem } from "../touch_bar.js"; +import type { SegmentedControlSegment } from "electron"; const TPL = /*html*/`
@@ -69,7 +71,7 @@ const TPL = /*html*/` } -
+
`; @@ -595,4 +597,65 @@ export default class CalendarView extends ViewMode { return newDate; } + buildTouchBarCommand({ TouchBar, buildIcon }: CommandListenerData<"buildTouchBar">) { + if (!this.calendar) { + return; + } + + const items: TouchBarItem[] = []; + const $toolbarItems = this.$calendarContainer.find(".fc-toolbar-chunk .fc-button-group, .fc-toolbar-chunk > button"); + + for (const item of $toolbarItems) { + // Button groups. + if (item.classList.contains("fc-button-group")) { + let mode: "single" | "buttons" = "single"; + const segments: SegmentedControlSegment[] = []; + const subItems = item.childNodes as NodeListOf; + for (const subItem of subItems) { + // Text button. + if (subItem.innerText) { + segments.push({ label: subItem.innerText }); + continue; + } + + // Icon button. + const iconEl = subItem.querySelector("span.fc-icon"); + let icon = null; + if (iconEl?.classList.contains("fc-icon-chevron-left")) { + icon = "NSImageNameTouchBarGoBackTemplate"; + mode = "buttons"; + } else if (iconEl?.classList.contains("fc-icon-chevron-right")) { + icon = "NSImageNameTouchBarGoForwardTemplate"; + mode = "buttons"; + } + + if (icon) { + segments.push({ + icon: buildIcon(icon) + }); + } + } + + items.push(new TouchBar.TouchBarSegmentedControl({ + mode, + segments, + change(selectedIndex, isSelected) { + subItems[selectedIndex].click(); + } + })); + continue; + } + + // Standalone item. + if (item.innerText) { + items.push(new TouchBar.TouchBarButton({ + label: item.innerText, + click: () => item.click() + })); + } + } + + return items; + } + }