Reno is the second iteration of the AWMTK-powered AwesomeWM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
10 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. -- This file is part of Reno desktop.
  2. --
  3. -- Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  4. --
  5. -- Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. --
  7. -- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>.
  8. -- Dismal - not your average run prompt
  9. -- Reference implementation of a generic widget
  10. local awmtk2 = require("awmtk2")
  11. local wibox = require("wibox")
  12. local gears = require("gears")
  13. local awful = require("awful")
  14. local ask = require("asckey")
  15. local xdg_search = function(name,rlimit,sorting_method)
  16. local ranked_results = {}
  17. if sorting_method == "usage" then
  18. local filter = {}
  19. local keys = {}
  20. for k,v in pairs(xdg.apps) do
  21. if not v.count then
  22. v.count = 0
  23. end
  24. if v.name:lower():find(name,nil,true) then
  25. if not filter[v.count] then
  26. table.insert(keys, v.count)
  27. filter[v.count] = {}
  28. end
  29. table.insert(filter[v.count],{k,v})
  30. end
  31. end
  32. table.sort(keys,function(a,b) return a > b end)
  33. local count = 0
  34. local exit = false
  35. gears.debug.dump(keys)
  36. gears.debug.dump(filter)
  37. for k = 1,rlimit do
  38. local i = keys[k]
  39. if not filter[i] then
  40. break
  41. end
  42. for _,v in pairs(filter[i]) do
  43. table.insert(ranked_results, v)
  44. count = count + 1
  45. if count >= rlimit then
  46. exit = true
  47. break
  48. end
  49. end
  50. if exit == true then
  51. break
  52. end
  53. end
  54. gears.debug.dump(ranked_results)
  55. elseif sorting_method == "recent" then
  56. local most_recent = 0
  57. for k,v in pairs(xdg.apps) do
  58. if v.name:lower():find(name,nil,true) and v.atime and v.atime >= most_recent then
  59. most_recent = v.atime
  60. table.insert(ranked_results,1,{k,v})
  61. end
  62. if #ranked_results > rlimit then
  63. table.remove(ranked_results,rlimit+1)
  64. end
  65. end
  66. end
  67. return ranked_results
  68. end
  69. return function(args)
  70. local style = awmtk2.create_style("dismal",
  71. awmtk2.generic.popup,args.style,args.vertical)
  72. local templates = awmtk2.create_template_lib("dismal",awmtk2.templates,args.templates)
  73. local t = awmtk2.build_templates(templates,style,args.vertical)
  74. local dismal = awful.popup(t.popup({
  75. id = "dismal_root",
  76. layout = wibox.layout.fixed.horizontal,
  77. spacing = style.container.spacing
  78. }))
  79. local button_cache = gears.cache(function(name,exec,description,icon,key)
  80. -- beacause apparently cache doesn't allow nil values
  81. if icon == "" then icon = nil end
  82. -- contents of our button
  83. local widget = wibox.widget(t.button(t.article({
  84. icon = icon,
  85. resize = true,
  86. title = name,
  87. description = description,
  88. icon_size = (style.button.height and
  89. (style.button.height-style.button.margins)) or
  90. (34-style.button.margins)
  91. }),{
  92. forced_height = style.button.height or 30
  93. }))
  94. local exec = exec:gsub("%%%w","")
  95. -- theme-declared highlight function, because why not
  96. -- maybe you'd like to add duck noises to button presses idk.
  97. widget:connect_signal("button::press",style.button.onpress)
  98. widget:connect_signal("button::release",style.button.onrelease)
  99. widget:connect_signal("mouses::leave",style.button.onrelease)
  100. local bump = function()
  101. if not xdg.apps[key].count then
  102. xdg.apps[key].count = 0
  103. end
  104. xdg.apps[key].count = xdg.apps[key].count + 1
  105. xdg.apps[key].atime = os.time()
  106. end
  107. widget:buttons(
  108. gears.table.join(
  109. awful.button({},1,function()
  110. awful.spawn(exec)
  111. dismal.visible = false
  112. -- i know it's deprecated, you literally give me no option
  113. awful.keygrabber.stop()
  114. bump()
  115. end),
  116. awful.button({"Control"},1,function()
  117. awful.spawn({global.terminal,"-e",exec})
  118. dismal.visible = false
  119. awful.keygrabber.stop()
  120. bump()
  121. end),
  122. awful.button({},3,function()
  123. awful.spawn(exec)
  124. bump()
  125. end),
  126. awful.button({"Control"},3,function()
  127. awful.spawn({global.terminal,"-e",exec})
  128. bump()
  129. end)
  130. )
  131. )
  132. return widget
  133. end)
  134. local launchpad
  135. do -- Primary area
  136. launchpad = wibox.widget({
  137. t.container {
  138. t.textbox({
  139. text = "",
  140. id = "inputbox"
  141. }),
  142. layout = wibox.layout.fixed.vertical,
  143. },
  144. t.container({
  145. id = "resultbox",
  146. layout = wibox.layout.fixed.vertical,
  147. spacing = style.container.spacing
  148. },{
  149. bg = style.container.bg_highlight,
  150. bgimage = style.container.bgimage_highlight,
  151. forced_width = style.button.width or 180
  152. }),
  153. t.container({
  154. t.button(
  155. t.textbox({
  156. markup = "Most used"
  157. }),{
  158. id = "usage"
  159. }),
  160. t.button(
  161. t.textbox({
  162. markup = "Recent"
  163. }),{
  164. id = "recent"
  165. }),
  166. id = "buttonbox",
  167. spacing = style.container.spacing,
  168. layout = wibox.layout.flex.horizontal
  169. }),
  170. t.textbox({
  171. markup = "<b>Enter</b> - run\n<b>Ctrl+Enter</b> - run in terminal"
  172. }),
  173. spacing = style.container.spacing,
  174. layout = wibox.layout.fixed.vertical
  175. })
  176. local results_list = launchpad:get_children_by_id("resultbox")[1]
  177. local input_field = launchpad:get_children_by_id("inputbox")[1]
  178. local usage_sort = launchpad:get_children_by_id("usage")[1]
  179. local recent_sort = launchpad:get_children_by_id("recent")[1]
  180. local sorting_method = "usage"
  181. usage_sort:set_bg(style.bg_focus)
  182. if style.button.onpress then
  183. style.button.onpress(usage_sort)
  184. end
  185. usage_sort:connect_signal("button::press",function()
  186. recent_sort:set_bg(style.bg_normal)
  187. if style.button.onrelease then
  188. style.button.onrelease(recent_sort)
  189. end
  190. usage_sort:set_bg(style.bg_focus)
  191. if style.button.onpress then
  192. style.button.onpress(usage_sort)
  193. end
  194. sorting_method = "usage"
  195. end)
  196. recent_sort:connect_signal("button::press",function()
  197. usage_sort:set_bg(style.bg_normal)
  198. if style.button.onrelease then
  199. style.button.onrelease(usage_sort)
  200. end
  201. recent_sort:set_bg(style.bg_focus)
  202. if style.button.onpress then
  203. style.button.onpress(recent_sort)
  204. end
  205. sorting_method = "recent"
  206. end)
  207. root.keys(gears.table.join(
  208. root.keys(),
  209. ask.k(":dismal.run", function()
  210. results_list:reset()
  211. dismal.visible = true
  212. dismal.x = args.x or 0
  213. dismal.y = args.y or 0
  214. if dismal.visible then
  215. awful.prompt.run {
  216. prompt = "<b>Run: </b>",
  217. textbox = input_field,
  218. hooks = {
  219. { { "Control" }, "Return", function(cmd)
  220. awful.spawn({global.terminal, "-e", cmd})
  221. end},
  222. { { }, "Return", function(cmd)
  223. awful.spawn.with_shell(cmd)
  224. end},
  225. { {}, 'Escape', function()
  226. input_field.markup = ""
  227. end},
  228. },
  229. done_callback = function()
  230. dismal.visible = false
  231. end,
  232. changed_callback = function(command)
  233. local results = xdg_search(command,args.result_limit or 5,sorting_method)
  234. results_list:reset()
  235. for _,v in pairs(results) do
  236. results_list:add(button_cache:get(
  237. v[2].name,
  238. v[2].exec,
  239. v[2].description or "",
  240. v[2].icon or "",
  241. v[1]
  242. ))
  243. end
  244. end,
  245. completion_callback = function(before,after,ncomp)
  246. return awful.completion.shell(before,after,ncomp,global.shell)
  247. end,
  248. history_path = (gears.filesystem.get_xdg_cache_home() or "/tmp/")..".dismal_history",
  249. history_max = 50
  250. }
  251. end
  252. end,{description = "open run menu", group = "widgets"})
  253. ))
  254. end
  255. local dismal_root = dismal.widget:get_children_by_id("dismal_root")[1]
  256. dismal_root:add(launchpad)
  257. return dismal
  258. end