summaryrefslogtreecommitdiff
path: root/pkgs/astal/src/widget/launcher.lua
blob: 70714322bc52bbb073ce7f8209ba073ec664dc8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
local astal = require("astal")
local Widget = require("astal.gtk3.widget")
local App = require("astal.gtk3.app")
local Gdk = require("astal.gtk3").Gdk
local Gtk = require("astal.gtk3").Gtk
local astalify = require("astal.gtk3").astalify
local Apps = astal.require("AstalApps")
local Variable = astal.Variable
local lib = require("lib")

local MAX_ENTRIES = 20

local FlowBox = astalify(Gtk.FlowBox)
local FlowBoxChild = astalify(Gtk.FlowBoxChild)

local apps = Apps.Apps()

local text = Variable("")
local visible = Variable(false)
local selection = Variable(1)

local list = text(function(text)
	return lib.slice(apps:exact_query(text), 0, MAX_ENTRIES)
end)

function on_show()
	text:set("")
	selection:set(1)
end

function hide()
	visible:set(false)
end

function on_key_press(self, event)
	local pos = selection:get()

	if event.keyval == Gdk.KEY_Escape then
		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()
		hide()
	end
end

function Application(app, idx)
	return FlowBoxChild({
		Widget.Button({
			class_name = selection():as(function(c)
				if c == idx then
					return "app selected"
				else
					return "app"
				end
			end),
			on_clicked = function()
				app:launch()
				hide()
			end,
			Widget.Box({
				halign = "CENTER",
				valign = "CENTER",
				vertical = true,
				Widget.Icon({
					icon = app.icon_name,
				}),
				Widget.Label({
					class_name = "name",
					label = app.name,
					valign = "CENTER",
					ellipsize = "END",
					max_width_chars = 20,
				}),
			}),
		}),
	})
end

function Applications(apps)
	return FlowBox({
		hexpand = true,
		homogeneous = true,
		class_name = "apps",
		lib.map(apps, Application)
	})
end

function Launcher()
	return Widget.Box({
		vertical = true,
		Widget.Entry({
			class_name = "search",
			placeholder_text = "Search",
			halign = "CENTER",
			text = text(),
			on_changed = function(self)
				text:set(self.text)
				selection:set(1)
			end,
			on_activate = on_enter,
		}),
		Widget.Box({
			class_name = "apps",
			list:as(Applications),
		}),
	})
end

return function()
	local Anchor = astal.require('Astal').WindowAnchor

	Widget.Window({
		class_name = "launcher",
		anchor = Anchor.TOP + Anchor.BOTTOM + Anchor.LEFT + Anchor.RIGHT,
		exclusivity = "EXCLUSIVE",
		keymode = "ON_DEMAND",
		application = App,
		on_show = on_show,
		on_key_press_event = on_key_press,
		visible = visible(),
		Launcher(),
	})

	return visible
end