2022-08-31 12:20:58 +00:00
-- This file is part of Reno desktop.
--
-- 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.
--
-- 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.
--
-- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>.
2022-07-23 19:10:02 +00:00
-- Dismal - not your average run prompt
2022-08-31 12:20:58 +00:00
-- Reference implementation of a generic widget
2022-07-23 19:10:02 +00:00
local awmtk2 = require ( " awmtk2 " )
local wibox = require ( " wibox " )
local gears = require ( " gears " )
local awful = require ( " awful " )
2022-08-11 19:34:25 +00:00
local beautiful = require ( " beautiful " )
2022-09-07 17:43:25 +00:00
local ask = require ( " asckey " )
2022-07-23 19:10:02 +00:00
local xdg_search = function ( name , rlimit )
local results = { }
for k , v in pairs ( xdg.apps ) do
if v.name : lower ( ) : find ( name , nil , true ) then
table.insert ( results , v )
end
if rlimit <= # results then
break
end
end
return results
end
return function ( args )
2022-09-06 22:40:51 +00:00
local style = awmtk2.create_style ( " dismal " ,
awmtk2.generic . popup , args.style )
2022-07-23 19:10:02 +00:00
local templates = awmtk2.create_template_lib ( " dismal " , awmtk2.templates , args.templates )
local t = awmtk2.build_templates ( templates , style )
local launchpad = awful.popup ( t.popup ( {
t.container {
t.textbox ( {
text = " " ,
id = " inputbox "
} ) ,
layout = wibox.layout . fixed.vertical ,
} ,
t.container ( {
id = " resultbox " ,
layout = wibox.layout . fixed.vertical ,
spacing = style.container . spacing
} , {
bg = style.container . bg_highlight ,
2022-09-07 17:43:25 +00:00
bgimage = style.container . bgimage_highlight ,
2022-07-23 19:10:02 +00:00
forced_width = style.button . width or 180
} ) ,
t.textbox ( {
2022-08-11 19:34:25 +00:00
markup = " <b>Enter</b> - run \n <b>Ctrl+Enter</b> - run in terminal "
2022-07-23 19:10:02 +00:00
} ) ,
spacing = style.container . spacing ,
layout = wibox.layout . fixed.vertical
} ) )
local button_cache = gears.cache ( function ( name , exec , description , icon )
-- beacause apparently cache doesn't allow nil values
if icon == " " then icon = nil end
2022-08-11 19:34:25 +00:00
-- contents of our button
2022-07-23 19:10:02 +00:00
local widget = wibox.widget ( t.button ( t.article ( {
icon = icon ,
resize = true ,
title = name ,
description = description ,
icon_size = ( style.button . height and
2022-08-11 19:34:25 +00:00
( style.button . height - style.button . margins ) ) or
34 - style.button . margins
2022-07-23 19:10:02 +00:00
} ) , {
forced_height = style.button . height or 30
} ) )
local exec = exec : gsub ( " %%%w " , " " )
2022-08-11 19:34:25 +00:00
-- theme-declared highlight function, because why not
-- maybe you'd like to add duck noises to button presses idk.
widget : connect_signal ( " button::press " , style.button . onpress )
widget : connect_signal ( " button::release " , style.button . onrelease )
widget : connect_signal ( " mouses::leave " , style.button . onrelease )
2022-07-23 19:10:02 +00:00
widget : buttons (
gears.table . join (
awful.button ( { } , 1 , function ( )
awful.spawn ( exec )
launchpad.visible = false
-- i know it's deprecated, you literally give me no option
awful.keygrabber . stop ( )
end ) ,
awful.button ( { " Control " } , 1 , function ( )
awful.spawn ( { global.terminal , " -e " , exec } )
launchpad.visible = false
awful.keygrabber . stop ( )
end ) ,
awful.button ( { } , 3 , function ( )
awful.spawn ( exec )
end ) ,
awful.button ( { " Control " } , 3 , function ( )
awful.spawn ( { global.terminal , " -e " , exec } )
end )
)
)
return widget
end )
local modifiers = { }
local results_list = launchpad.widget : get_children_by_id ( " resultbox " ) [ 1 ]
local input_field = launchpad.widget : get_children_by_id ( " inputbox " ) [ 1 ]
root.keys ( gears.table . join (
root.keys ( ) ,
2022-09-07 17:43:25 +00:00
ask.k ( " :dismal.run " , function ( )
2022-08-11 19:34:25 +00:00
results_list : reset ( )
2022-07-23 19:10:02 +00:00
launchpad.visible = true
2022-08-21 12:28:28 +00:00
launchpad.x = args.x or 0
launchpad.y = args.y or 0
2022-07-23 19:10:02 +00:00
if launchpad.visible then
awful.prompt . run {
prompt = " <b>Run: </b> " ,
textbox = input_field ,
hooks = {
{ { " Control " } , " Return " , function ( cmd )
awful.spawn ( { global.terminal , " -e " , cmd } )
end } ,
{ { } , " Return " , function ( cmd )
awful.spawn . with_shell ( cmd )
end } ,
{ { } , ' Escape ' , function ( cmd )
input_field.markup = " "
end } ,
} ,
done_callback = function ( )
launchpad.visible = false
end ,
changed_callback = function ( command )
local results = xdg_search ( command , args.result_limit or 5 )
results_list : reset ( )
for k , v in pairs ( results ) do
results_list : insert ( 1 , button_cache : get (
v.name ,
v.exec ,
v.description or " " ,
v.icon or " "
) )
end
end ,
completion_callback = function ( before , after , ncomp )
return awful.completion . shell ( before , after , ncomp , global.shell )
end ,
history_path = " /tmp/.dismal_history " ,
history_max = 50
}
end
2022-09-07 17:43:25 +00:00
end , { description = " open run menu " , group = " widgets " } )
2022-07-23 19:10:02 +00:00
) )
return launchpad
end