2020-09-16 19:21:01 +02:00
|
|
|
-- This project is licensed under the MIT License (see LICENSE).
|
|
|
|
|
2019-07-03 01:23:59 +02:00
|
|
|
--- Launch clients as panels.
|
|
|
|
--
|
|
|
|
-- @author James Reed <jcrd@tuta.io>
|
2020-08-19 20:36:08 +02:00
|
|
|
-- @copyright 2019-2020 James Reed
|
2019-07-03 01:23:59 +02:00
|
|
|
-- @module awesome-launch.panel
|
|
|
|
|
|
|
|
local awful = require("awful")
|
|
|
|
local gtable = require("gears.table")
|
2020-12-20 22:56:37 +01:00
|
|
|
local gtimer = require("gears.timer")
|
2019-07-03 01:23:59 +02:00
|
|
|
local launch = require("awesome-launch")
|
|
|
|
|
|
|
|
local panel = {}
|
|
|
|
|
2020-12-06 00:37:07 +01:00
|
|
|
--- Function to handle toggling of a panel client.
|
|
|
|
--
|
|
|
|
-- By default, the client's hidden state is toggled.
|
|
|
|
--
|
|
|
|
-- @param c The client being toggled.
|
|
|
|
-- @param state The toggle state: `true` if being toggled into view, `false`
|
|
|
|
-- otherwise.
|
|
|
|
-- @function panel.toggle_func
|
|
|
|
function panel.toggle_func(c, state)
|
|
|
|
c.hidden = not state
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Function to handle setup of a panel client.
|
|
|
|
--
|
|
|
|
-- By default, the client is hidden and made sticky.
|
|
|
|
--
|
|
|
|
-- @param c The client being setup.
|
|
|
|
-- @function panel.setup_func
|
|
|
|
function panel.setup_func(c)
|
|
|
|
c.hidden = true
|
|
|
|
c.sticky = true
|
|
|
|
end
|
|
|
|
|
2019-07-03 01:23:59 +02:00
|
|
|
-- TODO: Reapply args on restart with rule source.
|
|
|
|
-- See: https://github.com/awesomeWM/awesome/issues/2725
|
|
|
|
local function spawn(cmd, args)
|
2020-08-05 22:13:23 +02:00
|
|
|
local cb = args.callback
|
|
|
|
args.callback = function (c)
|
2019-07-03 01:23:59 +02:00
|
|
|
c.floating = true
|
2019-07-06 00:56:38 +02:00
|
|
|
c.skip_taskbar = true
|
2020-12-06 00:37:58 +01:00
|
|
|
c.launch_panel = true
|
2019-07-03 01:23:59 +02:00
|
|
|
awful.placement.scale(c, {to_percent=args.scale or 0.5})
|
|
|
|
awful.placement.centered(c)
|
2020-12-06 00:37:07 +01:00
|
|
|
if panel.setup_func then
|
|
|
|
panel.setup_func(c)
|
|
|
|
end
|
|
|
|
c:connect_signal("unfocus", function ()
|
2020-12-20 22:56:37 +01:00
|
|
|
gtimer.delayed_call(function ()
|
2021-01-25 13:27:45 +01:00
|
|
|
local valid = pcall(function () return c.valid end) and c.valid
|
|
|
|
if valid and not (client.focus and client.focus.floating) then
|
2020-12-20 22:56:37 +01:00
|
|
|
panel.toggle_func(c, false)
|
|
|
|
end
|
|
|
|
end)
|
2020-12-06 00:37:07 +01:00
|
|
|
end)
|
2020-12-21 01:11:35 +01:00
|
|
|
if cb then
|
|
|
|
cb(c)
|
|
|
|
end
|
2020-12-06 00:37:07 +01:00
|
|
|
panel.toggle_func(c, true)
|
2019-07-03 01:23:59 +02:00
|
|
|
end
|
|
|
|
launch.spawn.single_instance(cmd, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function toggle(c)
|
|
|
|
if c == client.focus then
|
2020-12-06 00:37:07 +01:00
|
|
|
panel.toggle_func(c, false)
|
2019-07-03 01:23:59 +02:00
|
|
|
else
|
2020-12-06 00:37:07 +01:00
|
|
|
panel.toggle_func(c, true)
|
2019-07-03 01:23:59 +02:00
|
|
|
c:emit_signal("request::activate", "panel.toggle", {raise=true})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Toggle the visibility of a panel, spawning the command if necessary.
|
|
|
|
--
|
|
|
|
-- A panel is a floating, centered client that can be scaled to a percentage of
|
|
|
|
-- its size.
|
|
|
|
--
|
|
|
|
-- @param cmd The command.
|
|
|
|
-- @param args Table containing the single instance ID and additional arguments for spawn
|
|
|
|
-- @param args.id Single instance ID.
|
|
|
|
-- @param args.props Properties to apply to the client.
|
|
|
|
-- @param args.pwd Pathname to the working directory for new clients.
|
|
|
|
-- @param args.timeout Seconds after which to stop waiting for a client to spawn.
|
2020-08-05 22:13:23 +02:00
|
|
|
-- @param args.callback Function to call with client when it spawns.
|
2019-07-03 01:23:59 +02:00
|
|
|
-- @param args.factory The factory to use (see wm-launch's -f flag).
|
2020-08-19 20:36:08 +02:00
|
|
|
-- @param args.systemd If true, run cmd with systemd-run.
|
2019-07-03 01:23:59 +02:00
|
|
|
-- @param args.firejail If true, run cmd with firejail.
|
|
|
|
-- @param args.filter Function to filter clients that are considered.
|
|
|
|
-- @param args.scale Percent to scale client (see awful.placement.scale).
|
|
|
|
-- @function panel.toggle
|
|
|
|
function panel.toggle(cmd, args)
|
|
|
|
local c = launch.client.by_id(args.id)
|
|
|
|
if c then
|
|
|
|
toggle(c)
|
|
|
|
else
|
2020-12-06 00:37:07 +01:00
|
|
|
spawn(cmd, args)
|
2019-07-03 01:23:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return panel
|