Skip to content

Commit

Permalink
[sketchybar] Separate window and space logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bustinbung committed Feb 4, 2024
1 parent 00e3294 commit 186ada3
Showing 1 changed file with 76 additions and 77 deletions.
153 changes: 76 additions & 77 deletions private_dot_config/sketchybar/plugins/executable_spaces_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,110 @@ source "$PLUGIN_DIR/icon_map.sh"
# set app icon font
APP_FONT="app-font:Regular:12.0"

update() {
# set icon and label color if selected
sketchybar --set "$NAME" icon.highlight="$SELECTED" \
label.highlight="$SELECTED"
# switch to space if clicked
mouse_clicked() {
yabai -m space --focus "$SID"
sketchybar --trigger space_change
sketchybar --animate sin 10 --set "$NAME" background.border_color="0x00FFFFFF"
}

# set background color if selected
if "$SELECTED"; then
sketchybar --animate sin 10 --set "$NAME" background.color="$ITEM_HIGHLIGHT"
else
sketchybar --animate sin 10 --set "$NAME" background.color="$ITEM_COLOR"
mouse_entered() {
if $SELECTED; then
return;
fi

# pull all spaces
SPACES=$(yabai -m query --spaces)
SPACES_LENGTH=$(jq '. | length' <<< "$SPACES")
sketchybar --animate sin 10 --set "$NAME" background.border_color="$BORDER"
}

# run following block for every space
for ((i = 0; i < SPACES_LENGTH; i++)); do
# set sid. we need both i and sid for indexing into the $SPACES variable
sid=$(( i + 1 ))
mouse_exited() {
if $SELECTED; then
return;
fi

# set color if space is in native fullscreen
if $(jq '.['$i']."is-native-fullscreen" == true' <<< "$SPACES"); then
sketchybar --set space.$sid background.color="$SPACE_NATIVE_FULLSCREEN" \
--set space.$sid icon.color="$ITEM_COLOR"
fi
sketchybar --animate sin 10 --set "$NAME" background.border_color="0x00FFFFFF"
}

# pull all apps in current space
APPS=$(yabai -m query --windows --space $sid)
APPS_LENGTH=$(jq '. | length' <<< "$APPS")
space_windows_change() {
sid=$(grep -Eo "[0-9]*" <<< "$NAME")

# continue if there are no apps in the space
if (( APPS_LENGTH == 0 )); then
sketchybar --set space.$sid label.drawing=off
continue
fi
# pull all apps in current space
APPS=$(yabai -m query --windows --space "$sid")
APPS_LENGTH=$(jq '. | length' <<< "$APPS")

# initialize empty label
LABEL=""
# continue if there are no apps in the space
if (( APPS_LENGTH == 0 )); then
sketchybar --set space."$sid" label.drawing=off
return
fi

# loop through all apps
for ((j = 0; j < APPS_LENGTH; j++)); do
# get current app
CURRENT_APP=$(jq '.['$j']' <<< "$APPS")
# initialize empty label
LABEL=""

# if app is hidden or minimized, continue and don't show in label
IS_HIDDEN=$(jq '."is-hidden" == true' <<< "$CURRENT_APP")
# IS_MINIMIZED=$(jq '."is-minimized" == true' <<< "$CURRENT_APP")
# loop through all apps
for ((i = 0; i < APPS_LENGTH; i++)); do
# get current app
CURRENT_APP=$(jq '.['$i']' <<< "$APPS")

# if $IS_HIDDEN || $IS_MINIMIZED; then
if $IS_HIDDEN; then
continue
fi
# if app is hidden or minimized, continue and don't show in label
IS_HIDDEN=$(jq '."is-hidden" == true' <<< "$CURRENT_APP")
# IS_MINIMIZED=$(jq '."is-minimized" == true' <<< "$CURRENT_APP")

# get app name and icon
APP_NAME=$(jq '.app' <<< "$CURRENT_APP" | tr -d '"')
APP_ICON=$(icon_map "$APP_NAME")
# if $IS_HIDDEN || $IS_MINIMIZED; then
if $IS_HIDDEN; then
continue
fi

# append icon to label
LABEL+="$APP_ICON"
# get app name and icon
APP_NAME=$(jq '.app' <<< "$CURRENT_APP" | tr -d '"')
APP_ICON=$(icon_map "$APP_NAME")

# add space between icons if not last icon
if ((j < APPS_LENGTH)); then
LABEL+=" "
fi
done
# append icon to label
LABEL+="$APP_ICON"

# set space label and font. if we made it this far, we can assume that the label is not empty
space=(
label="$LABEL"
label.drawing=on
# add space between icons if not last icon
if ((j < APPS_LENGTH)); then
LABEL+=" "
fi
done

label.font="$APP_FONT"
)
# set space label and font. if we made it this far, we can assume that the label is not empty
space=(
label="$LABEL"
label.drawing=on

sketchybar --set space.$sid "${space[@]}"
done
}
label.font="$APP_FONT"
)

# switch to space if clicked
mouse_clicked() {
yabai -m space --focus "$SID"
sketchybar --trigger space_change
sketchybar --animate sin 10 --set "$NAME" background.border_color="0x00FFFFFF"
sketchybar --set space."$sid" "${space[@]}"
}

mouse_entered() {
if $SELECTED; then
return;
fi
space_update() {
# set icon and label color if selected
sketchybar --set "$NAME" icon.highlight="$SELECTED" \
label.highlight="$SELECTED"

sketchybar --animate sin 10 --set "$NAME" background.border_color="$BORDER"
}
sid=$(grep -Eo "[0-9]*" <<< "$NAME")
i=$((sid - 1))
SPACES=$(yabai -m query --spaces)

mouse_exited() {
if $SELECTED; then
return;
# set color if space is in native fullscreen
if $(jq '.['$i']."is-native-fullscreen" == true' <<< "$SPACES"); then
sketchybar --set space."$sid" background.color="$SPACE_NATIVE_FULLSCREEN" \
--set space."$sid" icon.color="$ITEM_COLOR"
fi

sketchybar --animate sin 10 --set "$NAME" background.border_color="0x00FFFFFF"
# set background color if selected
if "$SELECTED"; then
sketchybar --animate sin 10 --set "$NAME" background.color="$ITEM_HIGHLIGHT"
else
sketchybar --animate sin 10 --set "$NAME" background.color="$ITEM_COLOR"
fi
}

case "$SENDER" in
"mouse.clicked") mouse_clicked ;;
"mouse.entered") mouse_entered ;;
"mouse.exited") mouse_exited ;;
*) update ;;
"space_windows_change") space_windows_change ;;
"space_change" | "space_created" | "space_destroyed") space_update ;;
esac

0 comments on commit 186ada3

Please sign in to comment.