From 26cafb15992d12ca74859daef0858a7ed2cd278b Mon Sep 17 00:00:00 2001 From: reo101 Date: Wed, 6 Sep 2023 22:04:41 +0300 Subject: [PATCH] feat(yabai): update Add 10th workspace Add `get_menu_bar_height` objc program --- modules/nix-darwin/yabai/default.nix | 2 +- .../nix-darwin/yabai/sketchybar/default.nix | 39 +++++--- .../get_menu_bar_height/get_menu_bar_height.m | 21 ++++ .../yabai/sketchybar/plugins/battery.sh | 99 +++++++++++++------ .../yabai/sketchybar/utils/colors.sh | 26 +++++ modules/nix-darwin/yabai/skhdrc | 3 + modules/nix-darwin/yabai/yabairc | 3 +- 7 files changed, 148 insertions(+), 45 deletions(-) create mode 100644 modules/nix-darwin/yabai/sketchybar/get_menu_bar_height/get_menu_bar_height.m create mode 100644 modules/nix-darwin/yabai/sketchybar/utils/colors.sh diff --git a/modules/nix-darwin/yabai/default.nix b/modules/nix-darwin/yabai/default.nix index a8480f4..83a6f1f 100644 --- a/modules/nix-darwin/yabai/default.nix +++ b/modules/nix-darwin/yabai/default.nix @@ -35,7 +35,7 @@ in extraPackages = with pkgs; [ jq ]; - config = import ./sketchybar; + config = import ./sketchybar pkgs; }; }; diff --git a/modules/nix-darwin/yabai/sketchybar/default.nix b/modules/nix-darwin/yabai/sketchybar/default.nix index 346b15d..124549b 100755 --- a/modules/nix-darwin/yabai/sketchybar/default.nix +++ b/modules/nix-darwin/yabai/sketchybar/default.nix @@ -1,8 +1,28 @@ +# TODO: AppleSelectedInputSourcesChangedNotification +{ lib, darwin, ... }: + let plugin_dir = ./plugins; + util_dir = ./utils; + get_menu_bar_height = darwin.apple_sdk.stdenv.mkDerivation { + name = "get_menu_bar_height"; + version = "0.0.1"; + src = lib.cleanSource ./get_menu_bar_height; + buildInputs = with darwin.apple_sdk.frameworks; [ + Cocoa + ]; + buildPhase = '' + clang -framework cocoa get_menu_bar_height.m -o get_menu_bar_height + ''; + installPhase = '' + mkdir -p $out/bin + mv get_menu_bar_height $out/bin/get_menu_bar_height + ''; + }; in '' -PLUGIN_DIR="${plugin_dir}" +export PLUGIN_DIR="${plugin_dir}" +export UTIL_DIR="${util_dir}" ##### Bar Appearance ##### BACKGROUND_COLOR="0x502a2d3d" @@ -13,6 +33,8 @@ if [[ ''$''\{appearance''\} != 'Dark' ]]; then BACKGROUND_COLOR="0x50f5f0f5" fi +height=''$''\(${get_menu_bar_height}/bin/get_menu_bar_height''\) + sketchybar --bar height="32" \ blur_radius="25" \ position="top" \ @@ -96,20 +118,7 @@ sketchybar --add item space_separator left \ # volume is registered. More info about the event system can be found here: # https://felixkratz.github.io/SketchyBar/config/events -sketchybar --add item test right \ - --set test script="''$''\{PLUGIN_DIR''\}/theme.sh" \ - icon="􀝦" \ - background.height="30" \ - background.color="''$''\{BACKGROUND_COLOR''\}" \ - background.corner_radius="7" \ - icon.padding_left="10" \ - icon.padding_right="13" \ - blur_radius="30" \ - background.border_color="0x80c4a7e7" \ - background.border_width="1" \ - label.drawing="off" \ - click_script="~/.dotfiles/macos/wallpapers.zsh" \ - --add item clock right i \ +sketchybar --add item clock right i \ --set clock update_freq="10" \ icon="􀐬" \ background.color="''$''\{BACKGROUND_COLOR''\}" \ diff --git a/modules/nix-darwin/yabai/sketchybar/get_menu_bar_height/get_menu_bar_height.m b/modules/nix-darwin/yabai/sketchybar/get_menu_bar_height/get_menu_bar_height.m new file mode 100644 index 0000000..7df0e80 --- /dev/null +++ b/modules/nix-darwin/yabai/sketchybar/get_menu_bar_height/get_menu_bar_height.m @@ -0,0 +1,21 @@ +#import + +int main() { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSRect screenRect = [[NSScreen mainScreen] frame]; + NSRect visibleRect = [[NSScreen mainScreen] visibleFrame]; + + // Show the menu bar temporarily to get its height + [NSMenu setMenuBarVisible:YES]; + + CGFloat menuBarHeight = screenRect.size.height - visibleRect.size.height; + printf("%d\n", (int) menuBarHeight); + + // Hide the menu bar again + [NSMenu setMenuBarVisible:NO]; + + [pool drain]; + return 0; +} + +// vim: ft=objc : diff --git a/modules/nix-darwin/yabai/sketchybar/plugins/battery.sh b/modules/nix-darwin/yabai/sketchybar/plugins/battery.sh index 5c729eb..cc8fc7e 100755 --- a/modules/nix-darwin/yabai/sketchybar/plugins/battery.sh +++ b/modules/nix-darwin/yabai/sketchybar/plugins/battery.sh @@ -1,34 +1,77 @@ -#!/bin/sh +#!/usr/bin/env bash -PERCENTAGE=$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1) +source "${UTIL_DIR}/colors.sh" + +BATT_PERCENT=$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1) CHARGING=$(pmset -g batt | grep 'AC Power') -if [ ${PERCENTAGE} = "" ]; then - exit 0 -fi - -case ${PERCENTAGE} in - 9[0-9]|100) - ICON="􀛨" - ;; - [6-8][0-9]) - ICON="􀺸" - ;; - [3-5][0-9]) - ICON="􀛩" - ;; - [1-2][0-9]) - ICON="􀛩" - ;; - *) - ICON="􀛪" - ;; -esac +sketchybar --set "${NAME}" icon.color=0xff989898 if [ ${CHARGING} != "" ]; then - ICON="􀢋" -fi + case ${BATT_PERCENT} in + 100) + ICON="" + COLOR="${GREEN}" + ;; + 9[0-9]) + ICON="" + COLOR="${GREEN}" + ;; + 8[0-9]) + ICON="" + COLOR="${GREEN}" + ;; + 7[0-9]) + ICON="" + COLOR="${GREEN}" + ;; + 6[0-9]) + ICON="" + COLOR="${YELLOW}" + ;; + 5[0-9]) + ICON="" + COLOR="${YELLOW}" + ;; + 4[0-9]) + ICON="" + COLOR="${ORANGE}" + ;; + 3[0-9]) + ICON="" + COLOR="${ORANGE}" + ;; + 2[0-9]) + ICON="" + COLOR="${RED}" + ;; + 1[0-9]) + ICON="" + COLOR="${RED}" + ;; + *) + ICON="" + COLOR="${RED}" + ;; + esac -# The item invoking this script (name ${NAME}) will get its icon and label -# updated with the current battery status -sketchybar --set ${NAME} icon="${ICON}" label="${PERCENTAGE}%" + sketchybar --set "${NAME}" icon="${ICON}" icon.color="${COLOR}" + sketchybar --set "${NAME}" label="${BATT_PERCENT}%" +else + case ${BATT_PERCENT} in + 100) ICON="" COLOR="$GREEN" ;; + 9[0-9]) ICON="" COLOR="$GREEN" ;; + 8[0-9]) ICON="" COLOR="$GREEN" ;; + 7[0-9]) ICON="" COLOR="$GREEN" ;; + 6[0-9]) ICON="" COLOR="$YELLOW" ;; + 5[0-9]) ICON="" COLOR="$YELLOW" ;; + 4[0-9]) ICON="" COLOR="$ORANGE" ;; + 3[0-9]) ICON="" COLOR="$ORANGE" ;; + 2[0-9]) ICON="" COLOR="$RED" ;; + 1[0-9]) ICON="" COLOR="$RED" ;; + *) ICON="" COLOR="$RED" ;; + esac + + sketchybar --set "${NAME}" icon="${ICON}" icon.color="${COLOR}" + sketchybar --set "${NAME}" label="${BATT_PERCENT}%" +fi diff --git a/modules/nix-darwin/yabai/sketchybar/utils/colors.sh b/modules/nix-darwin/yabai/sketchybar/utils/colors.sh new file mode 100644 index 0000000..d06df1a --- /dev/null +++ b/modules/nix-darwin/yabai/sketchybar/utils/colors.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# Color Palette +export BLACK=0xff181926 +export WHITE=0xffcad3f5 +export RED=0xffed8796 +export GREEN=0xffa6da95 +export BLUE=0xff8aadf4 +export YELLOW=0xffeed49f +export ORANGE=0xfff5a97f +export MAGENTA=0xffc6a0f6 +export GREY=0xff939ab7 +export TRANSPARENT=0x00000000 + +# General bar colors +export BAR_COLOR=0xff1e1e2e +export BAR_BORDER_COLOR=0xff494d64 #0xa024273a +export ICON_COLOR=$WHITE # Color of all icons +export LABEL_COLOR=$WHITE # Color of all labels +export BACKGROUND_1=0x603c3e4f +export BACKGROUND_2=0x60494d64 + +export POPUP_BACKGROUND_COLOR=0xff1e1e2e +export POPUP_BORDER_COLOR=$WHITE + +export SHADOW_COLOR=$BLACK diff --git a/modules/nix-darwin/yabai/skhdrc b/modules/nix-darwin/yabai/skhdrc index beded6e..d178f0c 100644 --- a/modules/nix-darwin/yabai/skhdrc +++ b/modules/nix-darwin/yabai/skhdrc @@ -22,6 +22,7 @@ cmd - 6 : yabai -m space --focus 6 cmd - 7 : yabai -m space --focus 7 cmd - 8 : yabai -m space --focus 8 cmd - 9 : yabai -m space --focus 9 +cmd - 0 : yabai -m space --focus 10 cmd - tab : yabai -m space --focus recent # cmd - ; : yabai -m space --focus next @@ -44,6 +45,7 @@ shift + cmd - 6 : yabai -m window --space 6; sketchybar --trigger windows_on_spa shift + cmd - 7 : yabai -m window --space 7; sketchybar --trigger windows_on_spaces shift + cmd - 8 : yabai -m window --space 8; sketchybar --trigger windows_on_spaces shift + cmd - 9 : yabai -m window --space 9; sketchybar --trigger windows_on_spaces +shift + cmd - 0 : yabai -m window --space 0; sketchybar --trigger windows_on_spaces # Move focus container to workspace shift + alt - m : yabai -m window --space last; yabai -m space --focus last; sketchybar --trigger windows_on_spaces @@ -58,6 +60,7 @@ shift + alt - 6 : yabai -m window --space 6; yabai -m space --focus 6; sketchyba shift + alt - 7 : yabai -m window --space 7; yabai -m space --focus 7; sketchybar --trigger windows_on_spaces shift + alt - 8 : yabai -m window --space 8; yabai -m space --focus 8; sketchybar --trigger windows_on_spaces shift + alt - 9 : yabai -m window --space 9; yabai -m space --focus 9; sketchybar --trigger windows_on_spaces +shift + alt - 0 : yabai -m window --space 0; yabai -m space --focus 9; sketchybar --trigger windows_on_spaces # Resize windows lctrl + alt - h : yabai -m window --resize left:-50:0; \ diff --git a/modules/nix-darwin/yabai/yabairc b/modules/nix-darwin/yabai/yabairc index a91b044..fdd13cd 100644 --- a/modules/nix-darwin/yabai/yabairc +++ b/modules/nix-darwin/yabai/yabairc @@ -12,6 +12,7 @@ yabai -m signal --add event=window_created action="sketchybar --trigger windows_ yabai -m signal --add event=window_destroyed action="sketchybar --trigger windows_on_spaces" yabai -m signal --add event=window_destroyed action="yabai -m query --windows --window &> /dev/null || yabai -m window --focus mouse" yabai -m signal --add event=application_terminated action="yabai -m query --windows --window &> /dev/null || yabai -m window --focus mouse" +yabai -m signal --add event=display_resized action="launchctl stop org.nixos.sketchybar && launchctl start org.nixos.sketchybar" # { # DESIRED_SPACES_PER_DISPLAY=4 @@ -44,7 +45,7 @@ yabai -m signal --add event=application_terminated action="yabai -m query --wind yabai -m config "external_bar" "all:49:0" \ "window_border" "on" \ "mouse_follows_focus" "off" \ - "focus_follows_mouse" "autoraise" \ + "focus_follows_mouse" "off" \ "window_zoom_persist" "off" \ "window_placement" "second_child" \ "window_topmost" "off" \