|
| 1 | +local astal = require("astal") |
| 2 | + |
| 3 | +local Apps = astal.require("AstalApps") |
| 4 | +local App = require("astal.gtk3.app") |
| 5 | +local Astal = require("astal.gtk3").Astal |
| 6 | +local Gdk = require("astal.gtk3").Gdk |
| 7 | +local Variable = astal.Variable |
| 8 | +local Widget = require("astal.gtk3.widget") |
| 9 | + |
| 10 | +local slice = require("lib").slice |
| 11 | +local map = require("lib").map |
| 12 | + |
| 13 | +local MAX_ITEMS = 8 |
| 14 | + |
| 15 | +local function hide() |
| 16 | + local launcher = App:get_window("launcher") |
| 17 | + if launcher then launcher:hide() end |
| 18 | +end |
| 19 | + |
| 20 | +local function AppButton(app) |
| 21 | + return Widget.Button({ |
| 22 | + class_name = "AppButton", |
| 23 | + on_clicked = function() |
| 24 | + hide() |
| 25 | + app:launch() |
| 26 | + end, |
| 27 | + Widget.Box({ |
| 28 | + Widget.Icon({ icon = app.icon_name }), |
| 29 | + Widget.Box({ |
| 30 | + valign = "CENTER", |
| 31 | + vertical = true, |
| 32 | + Widget.Label({ |
| 33 | + class_name = "name", |
| 34 | + wrap = true, |
| 35 | + xalign = 0, |
| 36 | + label = app.name, |
| 37 | + }), |
| 38 | + app.description and Widget.Label({ |
| 39 | + class_name = "description", |
| 40 | + wrap = true, |
| 41 | + xalign = 0, |
| 42 | + label = app.description, |
| 43 | + }), |
| 44 | + }), |
| 45 | + }), |
| 46 | + }) |
| 47 | +end |
| 48 | + |
| 49 | +return function() |
| 50 | + local apps = Apps.Apps() |
| 51 | + |
| 52 | + local text = Variable("") |
| 53 | + local list = text( |
| 54 | + function(t) return slice(apps:fuzzy_query(t), 1, MAX_ITEMS) end |
| 55 | + ) |
| 56 | + |
| 57 | + local on_enter = function() |
| 58 | + local found = apps:fuzzy_query(text:get())[1] |
| 59 | + if found then |
| 60 | + found:launch() |
| 61 | + hide() |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + return Widget.Window({ |
| 66 | + name = "launcher", |
| 67 | + anchor = Astal.WindowAnchor.TOP + Astal.WindowAnchor.BOTTOM, |
| 68 | + exclusivity = "IGNORE", |
| 69 | + keymode = "ON_DEMAND", |
| 70 | + application = App, |
| 71 | + on_show = function() text:set("") end, |
| 72 | + on_key_press_event = function(self, event) |
| 73 | + if event.keyval == Gdk.KEY_Escape then self:hide() end |
| 74 | + end, |
| 75 | + Widget.Box({ |
| 76 | + Widget.EventBox({ |
| 77 | + expand = true, |
| 78 | + on_click = hide, |
| 79 | + width_request = 4000, |
| 80 | + }), |
| 81 | + Widget.Box({ |
| 82 | + hexpand = false, |
| 83 | + vertical = true, |
| 84 | + Widget.EventBox({ on_click = hide, height_request = 100 }), |
| 85 | + Widget.Box({ |
| 86 | + vertical = true, |
| 87 | + width_request = 500, |
| 88 | + class_name = "Applauncher", |
| 89 | + Widget.Entry({ |
| 90 | + placeholder_text = "Search", |
| 91 | + text = text(), |
| 92 | + on_changed = function(self) text:set(self.text) end, |
| 93 | + on_activate = on_enter, |
| 94 | + }), |
| 95 | + Widget.Box({ |
| 96 | + spacing = 6, |
| 97 | + vertical = true, |
| 98 | + list:as(function(l) return map(l, AppButton) end), |
| 99 | + }), |
| 100 | + Widget.Box({ |
| 101 | + halign = "CENTER", |
| 102 | + class_name = "not-found", |
| 103 | + vertical = true, |
| 104 | + visible = list:as(function(l) return #l == 0 end), |
| 105 | + Widget.Icon({ icon = "system-search-symbolic" }), |
| 106 | + Widget.Label({ label = "No match found" }), |
| 107 | + }), |
| 108 | + }), |
| 109 | + Widget.EventBox({ expand = true, on_click = hide }), |
| 110 | + }), |
| 111 | + Widget.EventBox({ |
| 112 | + width_request = 4000, |
| 113 | + expand = true, |
| 114 | + on_click = hide, |
| 115 | + }), |
| 116 | + }), |
| 117 | + }) |
| 118 | +end |
0 commit comments