From ea1b68052e43b7b759a845f12188fde2fb5a95a8 Mon Sep 17 00:00:00 2001 From: Jean Tinland Date: Thu, 4 Jan 2024 13:57:05 +0100 Subject: [PATCH] Allow simple-bar spaces & process refresh from simple-bar-server --- index.jsx | 11 +++++- lib/components/simple-bar-context.jsx | 9 ++++- lib/components/yabai-context.jsx | 53 ++++++++++++++++++++++++++- lib/hooks/use-server-socket.js | 11 +++--- lib/scripts/init.sh | 32 ++++++++-------- lib/settings.js | 10 ++++- 6 files changed, 100 insertions(+), 26 deletions(-) diff --git a/index.jsx b/index.jsx index 568bc6d3..130cea01 100644 --- a/index.jsx +++ b/index.jsx @@ -34,11 +34,18 @@ import YabaiContextProvider from "./lib/components/yabai-context.jsx"; const refreshFrequency = false; const settings = Settings.get(); -const { yabaiPath = "/usr/local/bin/yabai", shell } = settings.global; +const { + yabaiPath = "/usr/local/bin/yabai", + shell, + enableServer, + yabaiServerRefresh, +} = settings.global; const { hideWindowTitle, displayOnlyIcon, displaySkhdMode } = settings.process; +const disableSignals = enableServer && yabaiServerRefresh; const enableTitleChangedSignal = hideWindowTitle || displayOnlyIcon; -const args = `${yabaiPath} ${displaySkhdMode} ${enableTitleChangedSignal}`; + +const args = `${yabaiPath} ${displaySkhdMode} ${disableSignals} ${enableTitleChangedSignal}`; const command = `${shell} simple-bar/lib/scripts/init.sh ${args}`; Utils.injectStyles("simple-bar-index-styles", [ diff --git a/lib/components/simple-bar-context.jsx b/lib/components/simple-bar-context.jsx index 00fb6e40..d3f07037 100644 --- a/lib/components/simple-bar-context.jsx +++ b/lib/components/simple-bar-context.jsx @@ -18,10 +18,16 @@ export default function SimpleBarContextProvider({ children, }) { const [settings, setSettings] = React.useState(initialSettings); + const [yabaiDisplays, setYabaiDisplays] = React.useState(displays); + + const { enableServer, yabaiServerRefresh } = settings.global; + const serverEnabled = enableServer && yabaiServerRefresh; + + const currentDisplays = serverEnabled ? yabaiDisplays : displays; const displayId = parseInt(window.location.pathname.replace("/", ""), 10); const { index: displayIndex } = - displays?.find((d) => d.id === displayId) || {}; + currentDisplays?.find((d) => d.id === displayId) || {}; return ( {children} diff --git a/lib/components/yabai-context.jsx b/lib/components/yabai-context.jsx index 894863ef..ebcc48ac 100644 --- a/lib/components/yabai-context.jsx +++ b/lib/components/yabai-context.jsx @@ -1,4 +1,7 @@ import * as Uebersicht from "uebersicht"; +import { useSimpleBarContext } from "./simple-bar-context.jsx"; +import useServerSocket from "../hooks/use-server-socket.js"; +import * as Yabai from "../yabai.js"; const { React } = Uebersicht; @@ -14,9 +17,57 @@ export function useYabaiContext() { export default React.memo(YabaiContextProvider); +let renderCount = 0; + function YabaiContextProvider({ spaces, windows, skhdMode, children }) { + const { settings, setYabaiDisplays } = useSimpleBarContext(); + const { enableServer, yabaiServerRefresh } = settings.global; + const serverEnabled = enableServer && yabaiServerRefresh; + + const [yabaiSpaces, setYabaiSpaces] = React.useState(spaces); + const [yabaiWindows, setYabaiWindows] = React.useState(windows); + + const resetSpaces = () => { + setYabaiSpaces([]); + }; + + const resetWindows = () => { + setYabaiWindows([]); + }; + + const resetDisplays = () => { + setYabaiDisplays([]); + }; + + const getSpaces = async () => { + const newSpaces = await Yabai.getSpaces(); + setYabaiSpaces(newSpaces); + }; + + const getWindows = async () => { + const newWindows = await Yabai.getWindows(); + setYabaiWindows(newWindows); + }; + + const getDisplays = async () => { + const newDisplays = await Yabai.getDisplays(); + setYabaiDisplays(newDisplays); + }; + + useServerSocket("spaces", serverEnabled, getSpaces, resetSpaces); + useServerSocket("windows", serverEnabled, getWindows, resetWindows); + useServerSocket("displays", serverEnabled, getDisplays, resetDisplays); + + renderCount = renderCount + 1; + return ( - + {children} ); diff --git a/lib/hooks/use-server-socket.js b/lib/hooks/use-server-socket.js index 1b006f2d..7f4b9863 100644 --- a/lib/hooks/use-server-socket.js +++ b/lib/hooks/use-server-socket.js @@ -5,7 +5,7 @@ import { useSimpleBarContext } from "../components/simple-bar-context.jsx"; const { React } = Uebersicht; export default function useServerSocket( - widget, + target, visible, getter, resetWidget, @@ -16,9 +16,9 @@ export default function useServerSocket( const socket = React.useRef(null); React.useEffect(() => { - const isUserWidget = widget === "user-widget"; + const isUserWidget = target === "user-widget"; if (enableServer && socket.current === null) { - let queryParams = `widget=${widget}`; + let queryParams = `target=${target}`; if (userWidgetIndex !== undefined) { queryParams = queryParams.concat(`&userWidgetIndex=${userWidgetIndex}`); @@ -30,6 +30,7 @@ export default function useServerSocket( newSocket.onmessage = (e) => { const { action } = JSON.parse(e.data); + if (visible && action === "refresh") { getter(); } @@ -37,7 +38,7 @@ export default function useServerSocket( if (isUserWidget) { toggleUserWidget(userWidgetIndex, resetWidget, setSettings); } else { - toggleWidget(widget, resetWidget, setSettings); + toggleWidget(target, resetWidget, setSettings); } } }; @@ -52,8 +53,8 @@ export default function useServerSocket( settings, setSettings, socket, + target, visible, - widget, userWidgetIndex, ]); } diff --git a/lib/scripts/init.sh b/lib/scripts/init.sh index d75fc187..673e4e9f 100644 --- a/lib/scripts/init.sh +++ b/lib/scripts/init.sh @@ -1,6 +1,7 @@ yabai_path=$1 display_skhd_mode=$2 -enable_window_title_changed_signal=$3 +disable_signals=$3 +enable_window_title_changed_signal=$4 pgrep -x yabai > /dev/null @@ -27,22 +28,23 @@ fi if [ -z "$displays" ]; then displays=$($yabai_path -m query --displays) fi +if [ $disable_signals = "false" ]; then + $yabai_path -m signal --add event=window_focused action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when focused application changes" + $yabai_path -m signal --add event=window_minimized action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when a window is minimized" + $yabai_path -m signal --add event=window_resized action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when a window is resized" + $yabai_path -m signal --add event=window_destroyed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when an application window is closed" + $yabai_path -m signal --add event=space_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space change" + $yabai_path -m signal --add event=display_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on display focus change" -$yabai_path -m signal --add event=window_focused action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when focused application changes" -$yabai_path -m signal --add event=window_minimized action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when a window is minimized" -$yabai_path -m signal --add event=window_resized action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when a window is resized" -$yabai_path -m signal --add event=window_destroyed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when an application window is closed" -$yabai_path -m signal --add event=space_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space change" -$yabai_path -m signal --add event=display_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on display focus change" + if [ $enable_window_title_changed_signal = "true" ]; then + $yabai_path -m signal --add event=window_title_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when current window title changes" + fi -if [ $enable_window_title_changed_signal = "true" ]; then - $yabai_path -m signal --add event=window_title_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when current window title changes" -fi - -yabai_major_version=$($yabai_path -v | awk -F '.' '{print $1}' | sed 's/yabai-v//') -if [ $yabai_major_version -ge 6 ]; then - $yabai_path -m signal --add event=space_destroyed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space removal" - $yabai_path -m signal --add event=space_created action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space creation" + yabai_major_version=$($yabai_path -v | awk -F '.' '{print $1}' | sed 's/yabai-v//') + if [ $yabai_major_version -ge 6 ]; then + $yabai_path -m signal --add event=space_destroyed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space removal" + $yabai_path -m signal --add event=space_created action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space creation" + fi fi if [ $display_skhd_mode = "true" ]; then diff --git a/lib/settings.js b/lib/settings.js index 6667a7b9..7f299383 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -83,6 +83,7 @@ export const data = { type: "checkbox", fullWidth: true, }, + externalConfigFile: { label: "External config file", type: "checkbox" }, serverHttpPort: { label: "simple-bar-server http port", type: "number", @@ -95,7 +96,11 @@ export const data = { placeholder: "Default: 7777", fullWidth: true, }, - externalConfigFile: { label: "External config file", type: "checkbox" }, + yabaiServerRefresh: { + label: "Refresh spaces & process with simple-bar-server", + type: "checkbox", + fullWidth: true, + }, themes: { label: "Themes" }, darkTheme: { label: "Dark theme", type: "select", options: darkThemes }, @@ -447,9 +452,10 @@ export const defaultSettings = { yabaiPath: "/usr/local/bin/yabai", shell: "sh", slidingAnimationPace: 4, + externalConfigFile: false, enableServer: false, serverSocketPort: 7776, - externalConfigFile: false, + yabaiServerRefresh: false, }, themes: { lightTheme: "NightShiftLight",