summaryrefslogtreecommitdiff
path: root/pkgs
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-06-20 16:16:16 -0400
committerFreya Murphy <freya@freyacat.org>2025-06-20 16:16:16 -0400
commit1ab1ca007d0073ccf07d8c8e4e61bced5deb7d0e (patch)
tree8e7b67455d192b3114744ec6d519b8d6f8162c04 /pkgs
parentfix astal notification text justification (diff)
downloaddotfiles-nix-1ab1ca007d0073ccf07d8c8e4e61bced5deb7d0e.tar.gz
dotfiles-nix-1ab1ca007d0073ccf07d8c8e4e61bced5deb7d0e.tar.bz2
dotfiles-nix-1ab1ca007d0073ccf07d8c8e4e61bced5deb7d0e.zip
make astal launcher wrap with arrow keys
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/astal/src/lib.lua10
-rw-r--r--pkgs/astal/src/widget/launcher.lua61
2 files changed, 49 insertions, 22 deletions
diff --git a/pkgs/astal/src/lib.lua b/pkgs/astal/src/lib.lua
index 98a5ecc..23c5dd1 100644
--- a/pkgs/astal/src/lib.lua
+++ b/pkgs/astal/src/lib.lua
@@ -103,6 +103,16 @@ lib = {
return lib.count(array) == 0
end,
+ --- clamps a number between two other numbers
+ clamp = function(num, low, high)
+ if low and num < low then
+ return low
+ elseif high and num > high then
+ return high
+ end
+ return num
+ end,
+
--- negates value
neg = function(val)
return not val
diff --git a/pkgs/astal/src/widget/launcher.lua b/pkgs/astal/src/widget/launcher.lua
index 7071432..7e7c8d8 100644
--- a/pkgs/astal/src/widget/launcher.lua
+++ b/pkgs/astal/src/widget/launcher.lua
@@ -9,6 +9,7 @@ local Variable = astal.Variable
local lib = require("lib")
local MAX_ENTRIES = 20
+local WIDTH = 7
local FlowBox = astalify(Gtk.FlowBox)
local FlowBoxChild = astalify(Gtk.FlowBoxChild)
@@ -18,6 +19,7 @@ local apps = Apps.Apps()
local text = Variable("")
local visible = Variable(false)
local selection = Variable(1)
+local entry = Variable(nil)
local list = text(function(text)
return lib.slice(apps:exact_query(text), 0, MAX_ENTRIES)
@@ -25,36 +27,49 @@ end)
function on_show()
text:set("")
- selection:set(1)
+ selection:set(0)
+ entry:get():grab_focus()
end
function hide()
visible:set(false)
end
-function on_key_press(self, event)
- local pos = selection:get()
-
- if event.keyval == Gdk.KEY_Escape then
+function on_enter()
+ local found = apps:exact_query(text:get())[selection:get() + 1]
+ if found then
+ found:launch()
hide()
- elseif event.keyval == Gdk.KEY_Left or
- event.keyval == Gdk.KEY_Down then
- if pos > 1 then
- selection:set(pos - 1)
- end
- elseif event.keyval == Gdk.KEY_Right or
- event.keyval == Gdk.KEY_Up then
- if pos < lib.count(list:get()) then
- selection:set(pos + 1)
- end
end
end
-function on_enter()
- local found = apps:exact_query(text:get())[selection:get()]
- if found then
- found:launch()
+function update_pos(change_x, change_y)
+ local pos = selection:get()
+ local pos_x = (pos % WIDTH) + change_x
+ local pos_y = math.floor(pos / WIDTH) + change_y
+
+ local count = lib.count(list:get())
+ local height = math.floor((count + WIDTH - 1) / WIDTH)
+
+ pos_x = pos_x % WIDTH
+ pos_y = pos_y % height
+ pos = lib.clamp(pos_y * WIDTH + pos_x, 0, count - 1)
+ selection:set(pos)
+end
+
+function on_key_press(self, event)
+ if event.keyval == Gdk.KEY_Escape then
hide()
+ elseif event.keyval == Gdk.KEY_Return then
+ on_enter()
+ elseif event.keyval == Gdk.KEY_Left then
+ update_pos(-1, 0)
+ elseif event.keyval == Gdk.KEY_Right then
+ update_pos(1, 0)
+ elseif event.keyval == Gdk.KEY_Up then
+ update_pos(0, -1)
+ elseif event.keyval == Gdk.KEY_Down then
+ update_pos(0, 1)
end
end
@@ -62,7 +77,7 @@ function Application(app, idx)
return FlowBoxChild({
Widget.Button({
class_name = selection():as(function(c)
- if c == idx then
+ if (c + 1) == idx then
return "app selected"
else
return "app"
@@ -108,11 +123,13 @@ function Launcher()
placeholder_text = "Search",
halign = "CENTER",
text = text(),
+ setup = function(self)
+ entry:set(self)
+ end,
on_changed = function(self)
text:set(self.text)
- selection:set(1)
+ selection:set(0)
end,
- on_activate = on_enter,
}),
Widget.Box({
class_name = "apps",