awesome-wm-widgets/logout-popup-widget/logout-popup.lua

217 lines
7.1 KiB
Lua
Raw Permalink Normal View History

-------------------------------------------------
-- Logout widget for Awesome Window Manager
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/logout-widget
-- @author Pavel Makhov
-- @copyright 2020 Pavel Makhov
-------------------------------------------------
local awful = require("awful")
2020-09-02 04:19:52 +02:00
local capi = {keygrabber = keygrabber }
local wibox = require("wibox")
local gears = require("gears")
2020-09-02 04:19:52 +02:00
local beautiful = require("beautiful")
2020-09-07 03:47:07 +02:00
local awesomebuttons = require("awesome-buttons.awesome-buttons")
2020-09-02 04:19:52 +02:00
local HOME_DIR = os.getenv("HOME")
2020-12-19 03:10:29 +01:00
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/logout-popup-widget'
2020-09-02 04:19:52 +02:00
local w = wibox {
2020-09-02 04:19:52 +02:00
bg = beautiful.fg_normal,
max_widget_size = 500,
ontop = true,
2020-09-02 04:19:52 +02:00
height = 200,
width = 400,
shape = function(cr, width, height)
2020-09-02 04:19:52 +02:00
gears.shape.rounded_rect(cr, width, height, 8)
end
}
2020-09-02 04:19:52 +02:00
local action = wibox.widget {
text = ' ',
widget = wibox.widget.textbox
}
2020-09-22 03:49:09 +02:00
local phrase_widget = wibox.widget{
align = 'center',
widget = wibox.widget.textbox
}
2020-09-02 04:19:52 +02:00
local function create_button(icon_name, action_name, accent_color, label_color, onclick, icon_size, icon_margin)
2020-09-02 04:19:52 +02:00
2020-09-22 03:49:09 +02:00
local button = awesomebuttons.with_icon {
type = 'basic',
icon = icon_name,
color = accent_color,
icon_size = icon_size,
icon_margin = icon_margin,
2020-09-22 03:49:09 +02:00
onclick = function()
onclick()
w.visible = false
2020-09-26 09:11:13 +02:00
capi.keygrabber.stop()
2020-09-22 03:49:09 +02:00
end
}
2021-06-04 04:18:37 +02:00
button:connect_signal("mouse::enter", function()
action:set_markup('<span color="' .. label_color .. '">' .. action_name .. '</span>')
2021-06-04 02:55:53 +02:00
end)
button:connect_signal("mouse::leave", function() action:set_markup('<span> </span>') end)
2020-09-02 04:19:52 +02:00
return button
end
local function launch(args)
args = args or {}
2020-09-02 04:19:52 +02:00
local bg_color = args.bg_color or beautiful.bg_normal
local accent_color = args.accent_color or beautiful.bg_focus
local text_color = args.text_color or beautiful.fg_normal
local label_color = args.label_color or beautiful.fg_focus
2020-09-02 04:19:52 +02:00
local phrases = args.phrases or {'Goodbye!'}
local icon_size = args.icon_size or 40
local icon_margin = args.icon_margin or 16
2023-04-27 09:51:33 +02:00
local hide_on_leave = args.hide_on_leave or false
2020-09-02 04:19:52 +02:00
local onlogout = args.onlogout or function () awesome.quit() end
2020-12-19 03:10:29 +01:00
local onlock = args.onlock or function() awful.spawn.with_shell("i3lock") end
2020-09-07 03:47:07 +02:00
local onreboot = args.onreboot or function() awful.spawn.with_shell("reboot") end
local onsuspend = args.onsuspend or function() awful.spawn.with_shell("systemctl suspend") end
local onpoweroff = args.onpoweroff or function() awful.spawn.with_shell("shutdown now") end
2020-09-02 04:19:52 +02:00
local onlogout_key = args.onlogout_key or 'l'
local onlock_key = args.onlock_key or 'k'
local onreboot_key = args.onreboot_key or 'r'
local onsuspend_key = args.onsuspend_key or 'u'
local onpoweroff_key = args.onpoweroff_key or 's'
2024-05-17 17:14:56 +02:00
local all_keys = onlogout_key .. onlock_key .. onreboot_key .. onsuspend_key .. onpoweroff_key
local ignore_case = args.ignore_case or true
2020-09-02 04:19:52 +02:00
w:set_bg(bg_color)
2020-09-30 04:23:02 +02:00
if #phrases > 0 then
2020-12-06 02:57:04 +01:00
phrase_widget:set_markup(
'<span color="'.. text_color .. '" size="20000">' .. phrases[ math.random( #phrases ) ] .. '</span>')
2020-09-30 04:23:02 +02:00
end
2020-09-03 03:30:00 +02:00
2020-09-02 04:19:52 +02:00
w:setup {
{
2020-09-03 03:30:00 +02:00
phrase_widget,
2020-09-02 04:19:52 +02:00
{
{
create_button('log-out', 'Log Out (' .. onlogout_key .. ')',
2021-06-04 02:55:53 +02:00
accent_color, label_color, onlogout, icon_size, icon_margin),
create_button('lock', 'Lock (' .. onlock_key .. ')',
2021-06-04 02:55:53 +02:00
accent_color, label_color, onlock, icon_size, icon_margin),
create_button('refresh-cw', 'Reboot (' .. onreboot_key .. ')',
2021-06-04 02:55:53 +02:00
accent_color, label_color, onreboot, icon_size, icon_margin),
create_button('moon', 'Suspend (' .. onsuspend_key .. ')',
2021-06-04 02:55:53 +02:00
accent_color, label_color, onsuspend, icon_size, icon_margin),
create_button('power', 'Power Off (' .. onpoweroff_key .. ')',
2021-06-04 02:55:53 +02:00
accent_color, label_color, onpoweroff, icon_size, icon_margin),
2020-09-02 04:19:52 +02:00
id = 'buttons',
spacing = 8,
layout = wibox.layout.fixed.horizontal
},
2021-12-11 02:53:09 +01:00
valign = 'center',
2020-09-02 04:19:52 +02:00
layout = wibox.container.place
},
{
action,
2024-07-25 22:16:53 +02:00
halign = 'center',
2020-09-02 04:19:52 +02:00
layout = wibox.container.place
},
spacing = 32,
layout = wibox.layout.fixed.vertical
},
2020-09-02 04:19:52 +02:00
id = 'a',
shape_border_width = 1,
2021-12-11 02:53:09 +01:00
valign = 'center',
2020-09-02 04:19:52 +02:00
layout = wibox.container.place
}
2020-11-02 02:07:33 +01:00
w.screen = mouse.screen
w.visible = true
2023-04-27 09:51:33 +02:00
if hide_on_leave then
w:connect_signal("mouse::leave", function()
phrase_widget:set_text('')
capi.keygrabber.stop()
w.visible = false
end)
end
2020-09-02 04:19:52 +02:00
awful.placement.centered(w)
capi.keygrabber.run(function(_, key, event)
if event == "release" then return end
if key then
2020-09-30 04:23:02 +02:00
if key == 'Escape' then
phrase_widget:set_text('')
capi.keygrabber.stop()
w.visible = false
2024-05-17 17:14:56 +02:00
else
if ignore_case then
key = string.lower(key)
2024-05-17 17:17:00 +02:00
onlogout_key = string.lower(onlogout_key)
onlock_key = string.lower(onlock_key)
onreboot_key = string.lower(onreboot_key)
onsuspend_key = string.lower(onsuspend_key)
onpoweroff_key = string.lower(onpoweroff_key)
all_keys = string.lower(all_keys)
2024-05-17 17:14:56 +02:00
end
2020-09-30 04:23:02 +02:00
2024-05-17 17:14:56 +02:00
if key == onpoweroff_key then onpoweroff()
elseif key == onreboot_key then onreboot()
elseif key == onsuspend_key then onsuspend()
elseif key == onlock_key then onlock()
elseif key == onlogout_key then onlogout()
end
2024-05-17 17:14:56 +02:00
if string.match(all_keys, key) then
phrase_widget:set_text('')
capi.keygrabber.stop()
w.visible = false
end
2020-09-30 04:23:02 +02:00
end
end
2020-09-02 04:19:52 +02:00
end)
end
2020-09-03 03:30:00 +02:00
local function widget(args)
2020-09-07 03:47:07 +02:00
local icon = args.icon or WIDGET_DIR .. '/power.svg'
2020-09-03 03:30:00 +02:00
local res = wibox.widget {
{
{
2020-09-07 03:47:07 +02:00
image = icon,
2020-09-03 03:30:00 +02:00
widget = wibox.widget.imagebox
},
margins = 4,
layout = wibox.container.margin
},
layout = wibox.layout.fixed.horizontal,
}
res:buttons(
awful.util.table.join(
awful.button({}, 1, function()
2020-09-22 03:49:09 +02:00
if w.visible then
phrase_widget:set_text('')
capi.keygrabber.stop()
w.visible = false
else
launch(args)
end
2020-09-03 03:30:00 +02:00
end)
))
return res
end
return {
2020-09-03 03:30:00 +02:00
launch = launch,
widget = widget
}