2025-09-06 14:08:00 +03:00
|
|
|
import { useContext, useEffect, useState } from "preact/hooks";
|
2025-09-06 14:00:23 +03:00
|
|
|
import { ParentComponent } from "./react_utils";
|
|
|
|
|
import { ComponentChildren, createContext } from "preact";
|
|
|
|
|
import { TouchBarItem } from "../../components/touch_bar";
|
|
|
|
|
import { dynamicRequire } from "../../services/utils";
|
|
|
|
|
|
|
|
|
|
interface TouchBarProps {
|
|
|
|
|
children: ComponentChildren;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 14:31:41 +03:00
|
|
|
interface LabelProps {
|
|
|
|
|
label: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SliderProps {
|
|
|
|
|
label: string;
|
|
|
|
|
value: number;
|
|
|
|
|
minValue: number;
|
|
|
|
|
maxValue: number;
|
|
|
|
|
onChange: (newValue: number) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ButtonProps {
|
|
|
|
|
label: string;
|
|
|
|
|
click: () => void;
|
|
|
|
|
enabled?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 14:59:57 +03:00
|
|
|
interface SegmentedControlProps {
|
|
|
|
|
mode: "single" | "buttons";
|
|
|
|
|
segments: {
|
|
|
|
|
label: string;
|
|
|
|
|
}[];
|
|
|
|
|
selectedIndex?: number;
|
|
|
|
|
onChange?: (selectedIndex: number, isSelected: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 14:00:23 +03:00
|
|
|
interface TouchBarContextApi {
|
|
|
|
|
addItem(item: TouchBarItem): void;
|
|
|
|
|
TouchBar: typeof Electron.TouchBar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TouchBarContext = createContext<TouchBarContextApi | null>(null);
|
|
|
|
|
|
|
|
|
|
export default function TouchBar({ children }: TouchBarProps) {
|
2025-09-06 14:08:00 +03:00
|
|
|
const [ isFocused, setIsFocused ] = useState(false);
|
2025-09-06 14:00:23 +03:00
|
|
|
const parentComponent = useContext(ParentComponent);
|
|
|
|
|
const remote = dynamicRequire("@electron/remote") as typeof import("@electron/remote");
|
|
|
|
|
const items: TouchBarItem[] = [];
|
|
|
|
|
|
|
|
|
|
const api: TouchBarContextApi = {
|
|
|
|
|
TouchBar: remote.TouchBar,
|
|
|
|
|
addItem: (item) => {
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const el = parentComponent?.$widget[0];
|
|
|
|
|
if (!el) return;
|
|
|
|
|
|
|
|
|
|
function onFocusGained() {
|
2025-09-06 14:08:00 +03:00
|
|
|
setIsFocused(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onFocusLost() {
|
|
|
|
|
setIsFocused(false);
|
2025-09-06 14:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
el.addEventListener("focusin", onFocusGained);
|
2025-09-06 14:08:00 +03:00
|
|
|
el.addEventListener("focusout", onFocusLost);
|
|
|
|
|
return () => {
|
|
|
|
|
el.removeEventListener("focusin", onFocusGained);
|
|
|
|
|
el.removeEventListener("focusout", onFocusLost);
|
|
|
|
|
}
|
2025-09-06 14:00:23 +03:00
|
|
|
}, []);
|
|
|
|
|
|
2025-09-06 14:08:00 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (isFocused) {
|
|
|
|
|
remote.getCurrentWindow().setTouchBar(new remote.TouchBar({ items }));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-06 14:59:57 +03:00
|
|
|
console.log("Touch bar state", isFocused, items);
|
|
|
|
|
|
2025-09-06 14:00:23 +03:00
|
|
|
return (
|
|
|
|
|
<TouchBarContext.Provider value={api}>
|
|
|
|
|
{children}
|
|
|
|
|
</TouchBarContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 14:31:41 +03:00
|
|
|
export function TouchBarLabel({ label }: LabelProps) {
|
2025-09-06 14:00:23 +03:00
|
|
|
const api = useContext(TouchBarContext);
|
|
|
|
|
|
|
|
|
|
if (api) {
|
|
|
|
|
const item = new api.TouchBar.TouchBarLabel({
|
|
|
|
|
label
|
|
|
|
|
});
|
|
|
|
|
api.addItem(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|
2025-09-06 14:18:32 +03:00
|
|
|
|
|
|
|
|
export function TouchBarSlider({ label, value, minValue, maxValue, onChange }: SliderProps) {
|
|
|
|
|
const api = useContext(TouchBarContext);
|
|
|
|
|
|
|
|
|
|
if (api) {
|
|
|
|
|
const item = new api.TouchBar.TouchBarSlider({
|
|
|
|
|
label,
|
|
|
|
|
value, minValue, maxValue,
|
|
|
|
|
change: onChange
|
|
|
|
|
});
|
|
|
|
|
api.addItem(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|
2025-09-06 14:31:41 +03:00
|
|
|
|
|
|
|
|
export function TouchBarButton({ label, click, enabled }: ButtonProps) {
|
|
|
|
|
const api = useContext(TouchBarContext);
|
|
|
|
|
|
|
|
|
|
if (api) {
|
|
|
|
|
const item = new api.TouchBar.TouchBarButton({
|
|
|
|
|
label, click, enabled
|
|
|
|
|
});
|
|
|
|
|
api.addItem(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|
2025-09-06 14:59:57 +03:00
|
|
|
|
|
|
|
|
export function TouchBarSegmentedControl({ mode, segments, selectedIndex, onChange }: SegmentedControlProps) {
|
|
|
|
|
const api = useContext(TouchBarContext);
|
|
|
|
|
|
|
|
|
|
if (api) {
|
|
|
|
|
const item = new api.TouchBar.TouchBarSegmentedControl({
|
|
|
|
|
mode, segments, selectedIndex,
|
|
|
|
|
change: onChange
|
|
|
|
|
});
|
|
|
|
|
api.addItem(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|