add which-key module

This commit is contained in:
Almarhoon Ibraheem 2024-06-23 00:10:51 +03:00
parent 8c04be22a9
commit 0efaa3b618
4 changed files with 168 additions and 71 deletions

View file

@ -10,6 +10,7 @@
(modules workspace-groups)
(modules workspace-grid)
(modules auto-reload)
(modules which-key)
(swayipc connection)
(swayipc records)
(swayipc info)
@ -51,7 +52,37 @@
`(,(string-append (getenv "HOME") "/.config/sway/")))
(auto-reload-init)
;; TODO: load which key module
;; init which-key
(which-key-configure #:delay-idle 1.2)
(which-key-init)
(define (show-rofi-message msg)
(hide-rofi-message)
(display (format #f "rofi -e \"~a\"" msg))
(system (format #f "rofi -e \"~a\"" msg)))
(define (hide-rofi-message)
(system "pkill -f '.*rofi -e.*'"))
(define (show-which-key submap bindings)
;; show your which-key viewer (rofi, eww, etc.)
(format #t "Displaying Submap ~a Bindings:\n" submap)
(let ((message ""))
(for-each
(lambda (ls)
(let ((nmsg (format #f " - ~a -> ~a\n" (list-ref ls 1) (list-ref ls 3))))
(display nmsg)
(set! message (string-append message nmsg))))
bindings)
(show-rofi-message message)))
(define (hide-which-key submap)
;; hide your which-key viewer (rofi, eww, etc.)
(format #t "Hiding Submap Bindings:\n")
(hide-rofi-message))
(add-hook! which-key-display-keybindings-hook show-which-key)
(add-hook! which-key-hide-keybindings-hook hide-which-key)
(start-event-listener-thread)
(thread-join! LISTENER-THREAD)

View file

@ -1,7 +1,6 @@
(use-modules (modules kbd)
(modules general)
(swayipc info)
(swayipc records)
(ice-9 popen)
(srfi srfi-18)
(ice-9 textual-ports))
@ -33,74 +32,74 @@
;; define root keybindings
(general-define-keys
;; media-keys
`("XF86AudioLowerVolume" (exec "pactl set-sink-volume @DEFAULT_SINK@ -5%"))
`("XF86AudioRaiseVolume" (exec "pactl set-sink-volume @DEFAULT_SINK@ +5%"))
`("s-[" (exec "pactl set-sink-volume @DEFAULT_SINK@ -5%"))
`("s-]" (exec "pactl set-sink-volume @DEFAULT_SINK@ +5%"))
`("XF86AudioMute" (exec "pactl set-sink-mute @DEFAULT_SINK@ toggle"))
`("XF86AudioNext" (exec "mpc next"))
`("XF86AudioPrev" (exec "mpc prev"))
`("XF86AudioPlay" (exec "mpc toggle"))
`("XF86AudioLowerVolume" (exec "pactl set-sink-volume @DEFAULT_SINK@ -5%") #:wk "Decrease Volume")
`("XF86AudioRaiseVolume" (exec "pactl set-sink-volume @DEFAULT_SINK@ +5%") #:wk "Increase Volume")
`("s-[" (exec "pactl set-sink-volume @DEFAULT_SINK@ -5%") #:wk "Decrease Volume")
`("s-]" (exec "pactl set-sink-volume @DEFAULT_SINK@ +5%") #:wk "Increase Volume")
`("XF86AudioMute" (exec "pactl set-sink-mute @DEFAULT_SINK@ toggle") #:wk "Toggle Mute")
`("XF86AudioNext" (exec "mpc next") #:wk "Next Song")
`("XF86AudioPrev" (exec "mpc prev") #:wk "Previous Song")
`("XF86AudioPlay" (exec "mpc toggle") #:wk "Toggle Player")
;; brightness-keys
`("XF86MonBrightnessUp" (exec "brightnessctl set +10%"))
`("XF86MonBrightnessDown" (exec "brightnessctl set 10%-"))
`("XF86MonBrightnessUp" (exec "brightnessctl set +10%") #:wk "Increase Brightness")
`("XF86MonBrightnessDown" (exec "brightnessctl set 10%-") #:wk "Decrease Brightness")
;; window and group management
`("s-f" (sway-fullscreen SWAY-FULLSCREEN-TOGGLE))
`("s-f" (sway-fullscreen SWAY-FULLSCREEN-TOGGLE) #:wk "Toggle Fullscreen")
;; move focus
`("s-h" (sway-focus-container SWAY-DIRECTION-LEFT))
`("s-j" (sway-focus-container SWAY-DIRECTION-DOWN))
`("s-k" (sway-focus-container SWAY-DIRECTION-UP))
`("s-l" (sway-focus-container SWAY-DIRECTION-RIGHT))
`("s-h" (sway-focus-container SWAY-DIRECTION-LEFT) #:wk "Focus Container Left")
`("s-j" (sway-focus-container SWAY-DIRECTION-DOWN) #:wk "Focus Container Down")
`("s-k" (sway-focus-container SWAY-DIRECTION-UP) #:wk "Focus Container Up")
`("s-l" (sway-focus-container SWAY-DIRECTION-RIGHT) #:wk "Focus Container Right")
;; move containers
`("s-S-h" (sway-move-container SWAY-DIRECTION-LEFT))
`("s-S-j" (sway-move-container SWAY-DIRECTION-DOWN))
`("s-S-k" (sway-move-container SWAY-DIRECTION-UP))
`("s-S-l" (sway-move-container SWAY-DIRECTION-RIGHT))
`("s-S-h" (sway-move-container SWAY-DIRECTION-LEFT) #:wk "Move Container Left")
`("s-S-j" (sway-move-container SWAY-DIRECTION-DOWN) #:wk "Move Container Down")
`("s-S-k" (sway-move-container SWAY-DIRECTION-UP) #:wk "Move Container Up")
`("s-S-l" (sway-move-container SWAY-DIRECTION-RIGHT) #:wk "Move Container Right")
;; switch workspace
`("s-C-h" (switch-workspace-left))
`("s-C-j" (switch-workspace-down))
`("s-C-k" (switch-workspace-up))
`("s-C-l" (switch-workspace-right))
`("s-C-h" (switch-workspace-left) #:wk "Switch Workspace Left")
`("s-C-j" (switch-workspace-down) #:wk "Switch Workspace Down")
`("s-C-k" (switch-workspace-up) #:wk "Switch Workspace Up")
`("s-C-l" (switch-workspace-right) #:wk "Switch Workspace Right")
;; move container to workspace
`("s-M-C-h" (move-container-to-workspace-left))
`("s-M-C-j" (move-container-to-workspace-down))
`("s-M-C-k" (move-container-to-workspace-up))
`("s-M-C-l" (move-container-to-workspace-right))
`("s-M-C-h" (move-container-to-workspace-left) #:wk "Move Container to Workspace Left")
`("s-M-C-j" (move-container-to-workspace-down) #:wk "Move Container to Workspace Down")
`("s-M-C-k" (move-container-to-workspace-up) #:wk "Move Container to Workspace Up")
`("s-M-C-l" (move-container-to-workspace-right) #:wk "Move Container to Workspace Right")
;; Tab like cycling
`("s-." (sway-focus-container-sibling SWAY-SIBLING-NEXT))
`("s-," (sway-focus-container-sibling SWAY-SIBLING-PREV))
`("s-." (sway-focus-container-sibling SWAY-SIBLING-NEXT) #:wk "Cycle Tabs Next")
`("s-," (sway-focus-container-sibling SWAY-SIBLING-PREV) #:wk "Cycle Tabs Previous")
`("s-w" (sway-kill))
`("s-Return" (exec "alacritty"))
`("M-s-Space" (exec "~/.bin/switch-keyboard-layout"))
`("C-s-Space" (exec "rofi -show drun")))
`("s-w" (sway-kill) #:wk "Kill Window")
`("s-Return" (exec "alacritty") #:wk "Spawn Terminal")
`("M-s-Space" (exec "~/.bin/switch-keyboard-layout") #:wk "Switch Keyboard Layout")
`("C-s-Space" (exec "rofi -show drun")) #:wk "Application Launcher")
;; define leader keymap
(general-define-keys
#:prefix "s-Space" #:wk "Leader"
`("o" (exec "rofi -show drun"))
`("C-g" (sway-mode "default") #:wk "abort")
`("o" (exec "rofi -show drun") #:wk "Applications")
`("C-g" (sway-mode "default") #:wk "Abort")
;; rofi keymap
`(general-define-keys
#:prefix "r" #:wk "Rofi"
("p" (exec "~/.config/rofi/bin/password-manager"))
("m" (exec "rofi-mount"))
("u" (exec "rofi-unmount"))
("w" (exec ".config/rofi/bin/wifi"))
("b" (exec "~/.config/rofi/bin/bluetooth"))
("f" (exec "~/.config/rofi/bin/finder"))
("k" (exec "~/.config/rofi/bin/keyboard-layout"))
("P" (exec "~/.config/rofi/bin/powermenu"))
("s" (exec "~/.config/rofi/bin/sound-input"))
("S" (exec "~/.config/rofi/bin/sound-output")))
("p" (exec "~/.config/rofi/bin/password-manager") #:wk "Password Manager")
("m" (exec "rofi-mount") #:wk "Mount Drives")
("u" (exec "rofi-unmount") #:wk "Unmount Drives")
("w" (exec ".config/rofi/bin/wifi") #:wk "Wifi")
("b" (exec "~/.config/rofi/bin/bluetooth") #:wk "Bluetooth")
("f" (exec "~/.config/rofi/bin/finder") #:wk "Finder")
("k" (exec "~/.config/rofi/bin/keyboard-layout") #:wk "Keyboard Layouts")
("P" (exec "~/.config/rofi/bin/powermenu") #:wk "Power")
("s" (exec "~/.config/rofi/bin/sound-input") #:wk "Sound Input")
("S" (exec "~/.config/rofi/bin/sound-output") #:wk "Sound Output"))
;; screenshot keymap
;; flameshot is not performing well under wayland & multiple monitors
@ -120,28 +119,28 @@
`(general-define-keys
#:prefix "s" #:wk "Screenshot"
("g" (exec "slurp | grim -g - - | wl-copy"))
("s" (exec (string-append "grim -o \"" (focused-output-name) "\" - | wl-copy")))
("f" (exec "grim - | wl-copy"))
("m" (exec "grim -g - - | wl-copy"))
("g" (exec "slurp | grim -g - - | wl-copy") #:wk "Gui Screenshot")
("s" (exec (string-append "grim -o \"" (focused-output-name) "\" - | wl-copy")) #:wk "Current Screen")
("f" (exec "grim - | wl-copy") #:wk "All Screens")
("m" (exec "grim -g - - | wl-copy") #:wk "Last Region")
(general-define-keys
#:prefix "d" #:wk "DelayScreenshot"
("g" (exec "sleep 2 && slurp | grim -g - - | wl-copy"))
("s" (exec (string-append "sleep 2 && grim -o \"" (focused-output-name) "\" - | wl-copy")))
("f" (exec "sleep 2 && grim - | wl-copy"))
("m" (exec "sleep 2 && grim -g - - | wl-copy"))))
#:prefix "d" #:wk "DelayedScreenshot"
("g" (exec "sleep 2 && slurp | grim -g - - | wl-copy") #:wk "Gui Screenshot")
("s" (exec (string-append "sleep 2 && grim -o \"" (focused-output-name) "\" - | wl-copy")) #:wk "Current Screen")
("f" (exec "sleep 2 && grim - | wl-copy") #:wk "All Screens")
("m" (exec "sleep 2 && grim -g - - | wl-copy") #:wk "Last Region")))
;; session keymap
`(general-define-keys
#:prefix "q" #:wk "Session"
("q" (sway-exit))
("r" (sway-reload)))
("q" (sway-exit) #:wk "Exit Sway")
("r" (sway-reload) #:wk "Reload Sway"))
`(general-define-keys
#:prefix "w" #:wk "Window"
("v" (sway-layout SWAY-LAYOUT-SPLITV))
("h" (sway-layout SWAY-LAYOUT-SPLITH))
("f" (sway-fullscreen SWAY-FULLSCREEN-TOGGLE))
("d" (sway-layout SWAY-LAYOUT-DEFAULT))
("t" (sway-layout SWAY-LAYOUT-TABBED)))))
("v" (sway-layout SWAY-LAYOUT-SPLITV) #:wk "Split Vertically")
("h" (sway-layout SWAY-LAYOUT-SPLITH) #:wk "Split Horizontally")
("f" (sway-fullscreen SWAY-FULLSCREEN-TOGGLE) #:wk "Fullscreen")
("d" (sway-layout SWAY-LAYOUT-DEFAULT) #:wk "Default Layout")
("t" (sway-layout SWAY-LAYOUT-TABBED) #:wk "Tabbed Layout"))))

View file

@ -13,7 +13,7 @@
general-define-key
general-keybindings
general-submaps
command-received-hook))
general-command-received-hook))
(define general-command-prefix "echo /general ")
(define general-command-signature
@ -35,7 +35,7 @@
;; data received: emitted on new command received via bindings.
;; Parameters:
;; - arg1: commandd.
(define command-received-hook
(define general-command-received-hook
(make-hook 1))
(define (general-keybinding-translator key)
@ -60,7 +60,7 @@ Parameters:
""))
(general-command (equal? prefix general-command-signature)))
(when general-command
(run-hook command-received-hook
(run-hook general-command-received-hook
(hex->string
(substring command (string-length general-command-signature)))))))
@ -69,7 +69,7 @@ Parameters:
(add-hook! sway-binding-hook binding-changed)
;; add a hook to listen to received commands
(add-hook! command-received-hook
(add-hook! general-command-received-hook
(lambda (command)
(format #t "executing command ~a\n" command)
(with-exception-handler
@ -128,7 +128,7 @@ Parameters:
(command (string-append type " " key " " (general-command (exp->string exp))))
(esc (string-append type " " key " " (general-command (exp->string `(and ,exp (sway-mode "default")))))))
(hash-set! general-keybindings chord '(key exp wk submap))
(hash-set! general-keybindings chord (list key exp wk submap))
(if (equal? submap "default")
(dispatch-command command)
(begin
@ -168,7 +168,7 @@ Parameters:
(if submap
;; if submap key is provided, then define a submap (ignore exp)
(define-submap chord wk submap
(define-submap chord submap submap
(cdr (find-submap chord-ls)))
;; otherwise, define a keybinding with exp
(define-keybindings chord exp wk

View file

@ -1,5 +1,72 @@
;; TODO
(define-module (modules which-key)
#:use-module (swayipc records)
#:use-module (swayipc info)
#:use-module (swayipc dispatcher)
#:export ())
#:use-module (swayipc events)
#:use-module (modules general)
#:use-module (ice-9 hash-table)
#:use-module (srfi srfi-18)
#:export (which-key-init
which-key-configure
which-key-delay-idle
which-key-display-keybindings-hook
which-key-event-counter
which-key-hide-keybindings-hook))
;; idle delay before showing
(define which-key-delay-idle 1.5)
;; used to keep track of how many events occured
(define which-key-event-counter 0)
;; used to keep track of whether the which-key is active or not
(define which-key-active #f)
(define* (get-submap-keybindings submap #:optional (keybindings general-keybindings))
"Return all provided submap keybindings.
Returned value is a list of keybindings. Each is a list
that contains the values: (CHORD KEY EXP WK SUBMAP)."
(filter (lambda (ls) (equal? (list-ref ls 4) submap))
(hash-map->list cons keybindings)))
;; emitted on submap change.
;; Parameters:
;; - arg1: submap.
;; - arg2: available keybindings.
(define which-key-display-keybindings-hook
(make-hook 2))
;; emitted on submap change to default.
;; Parameters:
;; - arg1: submap.
(define which-key-hide-keybindings-hook
(make-hook 1))
(define (mode-changed mode-event)
(let ((mode (sway-mode-event-change mode-event))
(counter (+ 1 which-key-event-counter)))
(set! which-key-event-counter counter)
(if (equal? "default" mode)
(begin
(set! which-key-active #f)
(run-hook which-key-hide-keybindings-hook mode))
(thread-start!
(make-thread
(lambda ()
(unless which-key-active
(usleep (inexact->exact (* 1000000 which-key-delay-idle))))
(when (equal? which-key-event-counter counter)
(set! which-key-active #t)
(run-hook which-key-display-keybindings-hook mode (get-submap-keybindings mode)))))))))
(define* (which-key-configure #:key delay-idle)
"Configure which-key
Parameters:
- delay-idle: the idle delay before showing the which key menu."
(when delay-idle
(set! which-key-delay-idle delay-idle)))
(define (which-key-init)
"Initialize the which-key module."
(add-hook! sway-mode-hook mode-changed)
(display "starting which-key\n"))