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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:00:23 +03:00
|
|
|
return (
|
|
|
|
|
<TouchBarContext.Provider value={api}>
|
|
|
|
|
{children}
|
|
|
|
|
</TouchBarContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TouchBarLabel({ label }: { label: string }) {
|
|
|
|
|
const api = useContext(TouchBarContext);
|
|
|
|
|
|
|
|
|
|
if (api) {
|
|
|
|
|
const item = new api.TouchBar.TouchBarLabel({
|
|
|
|
|
label
|
|
|
|
|
});
|
|
|
|
|
api.addItem(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|