From 5aa8d484f27883778ccd105e034f8bcd8bfe48a2 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 8 Jul 2026 01:33:54 -0700 Subject: hammerspoon: Apply layout changes automatically Whenever one of the applications that I care about is opened, or when the screen size changes, apply the layout as specified. --- hammerspoon/.hammerspoon/init.fnl | 65 +++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/hammerspoon/.hammerspoon/init.fnl b/hammerspoon/.hammerspoon/init.fnl index fda2cb0..15640c5 100644 --- a/hammerspoon/.hammerspoon/init.fnl +++ b/hammerspoon/.hammerspoon/init.fnl @@ -7,25 +7,52 @@ (hs.hotkey.bind ["cmd" "alt" "ctrl"] "e" show-window-title) (hs.hotkey.bind ["cmd" "shift"] "r" hs.reload) -(let [my-screen (hs.screen.primaryScreen) - screen-mode (: my-screen :currentMode) - geom [(. screen-mode "w") (. screen-mode "h")] - max (. hs "layout" "maximized")] - (case geom - [3440 1440] (hs.layout.apply - [["Emacs" nil nil (. hs "layout" "left70") nil nil] - ["Wezterm" nil nil (. hs "layout" "right30") nil nil] - ["Glide" nil nil max nil nil]]) - - [5120 1440] (hs.layout.apply - [["Glide" nil nil (. hs "layout" "left25") nil nil] - ["Emacs" nil nil (hs.geometry.rect 0.25 0 0.5 1) nil nil] - ["Wezterm" nil nil (. hs "layout" "right25") nil nil]]) - - _ (hs.layout.apply - [["Glide" nil nil max nil nil] - ["Emacs" nil nil max nil nil] - ["Wezterm" nil nil max nil nil]]))) +;;; Layout -- Automatically put windows in their place. + +(fn apply-layout [] + (let [my-screen (hs.screen.primaryScreen) + screen-mode (: my-screen :currentMode) + geom [(. screen-mode "w") (. screen-mode "h")] + max (. hs "layout" "maximized")] + (case geom + [3440 1440] (hs.layout.apply + [["Emacs" nil nil (. hs "layout" "left70") nil nil] + ["WezTerm" nil nil (. hs "layout" "right30") nil nil] + ["Glide" nil nil max nil nil]]) + + [5120 1440] (hs.layout.apply + [["Glide" nil nil (. hs "layout" "left25") nil nil] + ["Emacs" nil nil (hs.geometry.rect 0.25 0 0.5 1) nil nil] + ["WezTerm" nil nil (. hs "layout" "right25") nil nil]]) + + _ (hs.layout.apply + [["Glide" nil nil max nil nil] + ["Emacs" nil nil max nil nil] + ["WezTerm" nil nil max nil nil]])))) + +(fn make-application-watcher [] + (fn match-application [name event-type application] + (print (.. "Application " name " had event " event-type "; compared to " (. hs :application :watcher :launched) + "; deciision " (tostring (and (= event-type (. hs :application :watcher :launched)) + (or (= name "Emacs") + (= name "Wezterm") + (= name "Glide")))))) + (when (and (= event-type (. hs :application :watcher :launched)) + (or (= name "Emacs") + (= name "WezTerm") + (= name "Glide"))) + (apply-layout))) + (hs.application.watcher.new match-application)) + +(fn make-screen-watcher [] + (hs.screen.watcher.new apply-layout)) + +(apply-layout) + +(let [watcher (make-application-watcher)] + (watcher:start)) +(let [watcher (make-screen-watcher)] + (watcher:start)) ;;; Mouse -- Automatically set primary button based on mouse connected. -- cgit v1.3-2-g0d8e