users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/keyboard.jsx
2bead7efb473f0202dc6ef189fbc0e4a5401d41f
· 3.5 KB · 108 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import * as DataWidget from "./data-widget.jsx"; |
| 3 | import * as DataWidgetLoader from "./data-widget-loader.jsx"; |
| 4 | import * as Icons from "../icons/icons.jsx"; |
| 5 | import useWidgetRefresh from "../../hooks/use-widget-refresh"; |
| 6 | import useServerSocket from "../../hooks/use-server-socket"; |
| 7 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; |
| 8 | import * as Utils from "../../utils"; |
| 9 | |
| 10 | export { keyboardStyles as styles } from "../../styles/components/data/keyboard"; |
| 11 | |
| 12 | const { React } = Uebersicht; |
| 13 | |
| 14 | const DEFAULT_REFRESH_FREQUENCY = 20000; |
| 15 | |
| 16 | /** |
| 17 | * Keyboard widget component. |
| 18 | * @returns {JSX.Element|null} The rendered widget or null if not visible. |
| 19 | */ |
| 20 | export 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 | |
| 108 | Widget.displayName = "Keyboard"; |