Skip to content

Commit

Permalink
Allow simple-bar spaces & process refresh from simple-bar-server
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Tinland committed Jan 4, 2024
1 parent 540d673 commit ea1b680
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 26 deletions.
11 changes: 9 additions & 2 deletions index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", [
Expand Down
9 changes: 8 additions & 1 deletion lib/components/simple-bar-context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<SimpleBarContext.Provider
Expand All @@ -30,6 +36,7 @@ export default function SimpleBarContextProvider({
SIPDisabled,
settings,
setSettings,
setYabaiDisplays,
}}
>
{children}
Expand Down
53 changes: 52 additions & 1 deletion lib/components/yabai-context.jsx
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 (
<YabaiContext.Provider value={{ spaces, windows, skhdMode }}>
<YabaiContext.Provider
value={{
spaces: serverEnabled ? yabaiSpaces : spaces,
windows: serverEnabled ? yabaiWindows : windows,
skhdMode,
}}
>
{children}
</YabaiContext.Provider>
);
Expand Down
11 changes: 6 additions & 5 deletions lib/hooks/use-server-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSimpleBarContext } from "../components/simple-bar-context.jsx";
const { React } = Uebersicht;

export default function useServerSocket(
widget,
target,
visible,
getter,
resetWidget,
Expand All @@ -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}`);
Expand All @@ -30,14 +30,15 @@ export default function useServerSocket(

newSocket.onmessage = (e) => {
const { action } = JSON.parse(e.data);

if (visible && action === "refresh") {
getter();
}
if (action === "toggle") {
if (isUserWidget) {
toggleUserWidget(userWidgetIndex, resetWidget, setSettings);
} else {
toggleWidget(widget, resetWidget, setSettings);
toggleWidget(target, resetWidget, setSettings);
}
}
};
Expand All @@ -52,8 +53,8 @@ export default function useServerSocket(
settings,
setSettings,
socket,
target,
visible,
widget,
userWidgetIndex,
]);
}
Expand Down
32 changes: 17 additions & 15 deletions lib/scripts/init.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 },
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ea1b680

Please sign in to comment.