dotfiles/awesome/components/exit-screen.lua

226 lines
6.5 KiB
Lua
Raw Normal View History

2022-07-19 19:45:11 +02:00
-- ███████╗██╗ ██╗██╗████████╗ ███████╗ ██████╗██████╗ ███████╗███████╗███╗ ██╗
-- ██╔════╝╚██╗██╔╝██║╚══██╔══╝ ██╔════╝██╔════╝██╔══██╗██╔════╝██╔════╝████╗ ██║
-- █████╗ ╚███╔╝ ██║ ██║ ███████╗██║ ██████╔╝█████╗ █████╗ ██╔██╗ ██║
-- ██╔══╝ ██╔██╗ ██║ ██║ ╚════██║██║ ██╔══██╗██╔══╝ ██╔══╝ ██║╚██╗██║
-- ███████╗██╔╝ ██╗██║ ██║ ███████║╚██████╗██║ ██║███████╗███████╗██║ ╚████║
-- ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═══╝
-- ===================================================================
-- Initialization
-- ===================================================================
local awful = require("awful")
local gears = require("gears")
local wibox = require("wibox")
local beautiful = require("beautiful")
local clickable_container = require("widgets.clickable-container")
local dpi = beautiful.xresources.apply_dpi
local ICON_DIR = gears.filesystem.get_configuration_dir() .. "/icons/exit-screen/" .. beautiful.name .. "/"
-- define module table
local exit_screen = {}
-- ===================================================================
-- Appearance
-- ===================================================================
local icon_size = dpi(90)
local build_button = function(icon)
2022-07-23 19:31:38 +02:00
local button = wibox.widget {
wibox.widget {
2022-07-19 19:45:11 +02:00
wibox.widget {
2022-07-23 19:31:38 +02:00
wibox.widget {
image = icon,
widget = wibox.widget.imagebox
},
top = dpi(16),
bottom = dpi(16),
left = dpi(16),
right = dpi(16),
widget = wibox.container.margin
2022-07-19 19:45:11 +02:00
},
2022-07-23 19:31:38 +02:00
shape = gears.shape.circle,
forced_width = icon_size,
forced_height = icon_size,
widget = clickable_container
},
left = dpi(24),
right = dpi(24),
widget = wibox.container.margin
}
return button
2022-07-19 19:45:11 +02:00
end
-- ===================================================================
-- Functionality
-- ===================================================================
local exit_screen_grabber
local function suspend_command()
2022-07-23 19:31:38 +02:00
exit_screen.hide()
awful.spawn.with_shell(apps.lock .. " & systemctl suspend")
2022-07-19 19:45:11 +02:00
end
local function exit_command()
2022-07-23 19:31:38 +02:00
awesome.quit()
2022-07-19 19:45:11 +02:00
end
local function lock_command()
2022-07-23 19:31:38 +02:00
exit_screen.hide()
awful.spawn.with_shell("sleep 1 && " .. apps.lock)
2022-07-19 19:45:11 +02:00
end
local function poweroff_command()
2022-07-23 19:31:38 +02:00
awful.spawn.with_shell("poweroff")
awful.keygrabber.stop(exit_screen_grabber)
2022-07-19 19:45:11 +02:00
end
local function reboot_command()
2022-07-23 19:31:38 +02:00
awful.spawn.with_shell("reboot")
awful.keygrabber.stop(exit_screen_grabber)
2022-07-19 19:45:11 +02:00
end
local poweroff = build_button(ICON_DIR .. "power.png", "Shutdown")
poweroff:connect_signal(
2022-07-23 19:31:38 +02:00
"button::release",
function()
poweroff_command()
end
2022-07-19 19:45:11 +02:00
)
local reboot = build_button(ICON_DIR .. "restart.png", "Restart")
reboot:connect_signal(
2022-07-23 19:31:38 +02:00
"button::release",
function()
reboot_command()
end
2022-07-19 19:45:11 +02:00
)
local suspend = build_button(ICON_DIR .. "sleep.png", "Sleep")
suspend:connect_signal(
2022-07-23 19:31:38 +02:00
"button::release",
function()
suspend_command()
end
2022-07-19 19:45:11 +02:00
)
local exit = build_button(ICON_DIR .. "logout.png", "Logout")
exit:connect_signal(
2022-07-23 19:31:38 +02:00
"button::release",
function()
exit_command()
end
2022-07-19 19:45:11 +02:00
)
local lock = build_button(ICON_DIR .. "lock.png", "Lock")
lock:connect_signal(
2022-07-23 19:31:38 +02:00
"button::release",
function()
lock_command()
end
2022-07-19 19:45:11 +02:00
)
-- subscribe to the show_exit_screen signal
-- show the exit screen when signal is broadcasted
awesome.connect_signal("show_exit_screen",
2022-07-23 19:31:38 +02:00
function()
exit_screen_grabber = awful.keygrabber.run(
function(_, key, event)
if event == "release" then
return
end
if key == "s" then
suspend_command()
elseif key == "e" then
exit_command()
elseif key == "l" then
lock_command()
elseif key == "p" then
poweroff_command()
elseif key == "r" then
reboot_command()
elseif key == "Escape" or key == "q" or key == "x" then
exit_screen.hide()
end
2022-07-19 19:45:11 +02:00
end
2022-07-23 19:31:38 +02:00
)
exit_screen.widget.visible = true
end
2022-07-19 19:45:11 +02:00
)
-- hide exit screen
function exit_screen.hide()
2022-07-23 19:31:38 +02:00
awful.keygrabber.stop(exit_screen_grabber)
exit_screen.widget.visible = false
2022-07-19 19:45:11 +02:00
end
-- ===================================================================
-- Create Widget
-- ===================================================================
local screen_geometry = awful.screen.focused().geometry
-- Create the widget
exit_screen.widget = wibox({
2022-07-23 19:31:38 +02:00
x = screen_geometry.x,
y = screen_geometry.y,
visible = false,
ontop = true,
type = "splash",
height = screen_geometry.height,
width = screen_geometry.width
2022-07-19 19:45:11 +02:00
})
exit_screen.widget:buttons(
2022-07-23 19:31:38 +02:00
gears.table.join(
-- Middle click - Hide exit_screen
awful.button({}, 2,
function()
exit_screen.hide()
end
),
-- Right click - Hide exit_screen
awful.button({}, 3,
function()
exit_screen.hide()
end
)
)
2022-07-19 19:45:11 +02:00
)
-- Item placement
exit_screen.widget:setup {
2022-07-23 19:31:38 +02:00
nil,
{
nil,
{
poweroff,
reboot,
suspend,
exit,
lock,
layout = wibox.layout.fixed.horizontal
},
nil,
expand = "none",
layout = wibox.layout.align.horizontal
},
nil,
expand = "none",
layout = wibox.layout.align.vertical
2022-07-19 19:45:11 +02:00
}
return exit_screen