aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx')
-rwxr-xr-xusers/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx108
1 files changed, 108 insertions, 0 deletions
diff --git a/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx
new file mode 100755
index 0000000..d6e04f2
--- /dev/null
+++ b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx
@@ -0,0 +1,108 @@
1import * as Uebersicht from "uebersicht";
2import * as DataWidget from "./data-widget.jsx";
3import * as DataWidgetLoader from "./data-widget-loader.jsx";
4import * as Icons from "../icons/icons.jsx";
5import useWidgetRefresh from "../../hooks/use-widget-refresh";
6import useServerSocket from "../../hooks/use-server-socket";
7import { useSimpleBarContext } from "../simple-bar-context.jsx";
8import * as Utils from "../../utils";
9
10export { keyboardStyles as styles } from "../../styles/components/data/keyboard";
11
12const { React } = Uebersicht;
13
14const DEFAULT_REFRESH_FREQUENCY = 20000;
15
16/**
17 * Keyboard widget component.
18 * @returns {JSX.Element|null} The rendered widget or null if not visible.
19 */
20export const Widget = React.memo(() => {
21 const { displayIndex, settings } = useSimpleBarContext();
22 const { widgets, keyboardWidgetOptions } = settings;
23 const { keyboardWidget } = widgets;
24 const { refreshFrequency, showOnDisplay, showIcon, keyboardMaxLength } =
25 keyboardWidgetOptions;
26
27 // Determine the refresh frequency for the widget
28 const refresh = React.useMemo(
29 () =>
30 Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY),
31 [refreshFrequency],
32 );
33
34 // Determine if the widget should be visible based on display settings
35 const visible =
36 Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && keyboardWidget;
37
38 const [state, setState] = React.useState();
39 const [loading, setLoading] = React.useState(visible);
40
41 /**
42 * Resets the widget state.
43 */
44 const resetWidget = () => {
45 setState(undefined);
46 setLoading(false);
47 };
48
49 /**
50 * Fetches the current keyboard layout or input mode.
51 */
52 const getKeyboard = React.useCallback(async () => {
53 if (!visible) return;
54 const keyboard = await Utils.cachedRun(
55 `defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | awk '/KeyboardLayout Name/ {$1=$2=$3=""; print $0}'`,
56 refresh,
57 );
58 const layout = Utils.cleanupOutput(keyboard)
59 .replace(";", "")
60 .replaceAll('"', "");
61 if (layout.length) {
62 setState({ keyboard: layout });
63 setLoading(false);
64 return;
65 }
66
67 const inputMode = await Utils.cachedRun(
68 `defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | awk '/"Input Mode" =/ {$1=$2=$3=""; print $0}'`,
69 refresh,
70 );
71 const cleanedInputMode = Utils.cleanupOutput(inputMode)
72 .replace(/"com.apple.inputmethod.(.*)"/, "$1")
73 .replace(";", "");
74
75 if (!cleanedInputMode.length) return setLoading(false);
76
77 const splitedInputMode = cleanedInputMode.split(".");
78 const inputModeName = splitedInputMode[splitedInputMode.length - 1];
79 setState({ keyboard: inputModeName });
80 setLoading(false);
81 }, [visible, refresh]);
82
83 // Use server socket to listen for keyboard events
84 useServerSocket("keyboard", visible, getKeyboard, resetWidget, setLoading);
85 // Refresh the widget at the specified interval
86 useWidgetRefresh(visible, getKeyboard, refresh);
87
88 if (loading) return <DataWidgetLoader.Widget className="keyboard" />;
89 if (!state) return null;
90 const { keyboard } = state;
91
92 const maxLength = Number(keyboardMaxLength) || 0;
93 const displayKeyboard =
94 maxLength > 0 ? keyboard.slice(0, maxLength) : keyboard;
95
96 if (!displayKeyboard?.length) return null;
97
98 return (
99 <DataWidget.Widget
100 classes="keyboard"
101 Icon={showIcon ? Icons.Keyboard : null}
102 >
103 {displayKeyboard}
104 </DataWidget.Widget>
105 );
106});
107
108Widget.displayName = "Keyboard";