doc: Add `awful.screenshot` examples.

This commit is contained in:
Emmanuel Lepage Vallee 2022-09-06 00:13:36 -07:00
parent 6a862f786b
commit 9207b78f81
3 changed files with 378 additions and 7 deletions

View File

@ -1,5 +1,23 @@
---------------------------------------------------------------------------
--- Screenshots and related configuration settings
--- Take screenshots of clients, screens, geometry and export to files or widgets.
--
-- Common keybindings
-- ==================
--
-- This example setups keybinding for the "Print Screen" key. Shift means
-- interactive, Control means delayed and Mod4 (Super) means current client only.
-- This example also creates convinient notifications.
--
-- @DOC_awful_screenshot_keybindings1_EXAMPLE@
--
-- Convert to widgets
-- ==================
--
-- This example creates and `Alt+Tab` like popup with client images. Note that
-- it might display black rectangles if you are not using a compositing manager
-- such as `picom`.
--
-- @DOC_awful_screenshot_popup_EXAMPLE@
--
-- @author Brian Sobulefsky <brian.sobulefsky@protonmail.com>
-- @copyright 2021 Brian Sobulefsky
@ -437,10 +455,10 @@ end
--
-- @property geometry
-- @tparam table geometry
-- @tparam table geometry.x
-- @tparam table geometry.y
-- @tparam table geometry.width
-- @tparam table geometry.height
-- @tparam number geometry.x
-- @tparam number geometry.y
-- @tparam number geometry.width
-- @tparam number geometry.height
-- @propemits true false
--- Get screenshot surface.
@ -727,7 +745,6 @@ function module:set_auto_save_delay(value)
self:emit_signal("timer::timeout")
awesome.sync()
self:refresh()
if not self.interactive then
self:save()
end
@ -786,7 +803,6 @@ function module:refresh()
else
self._private.surfaces[method] = {surface = surface, geometry = geo}
end
end
if self._private.output_imagebox then

View File

@ -0,0 +1,197 @@
--DOC_NO_USAGE --DOC_GEN_IMAGE --DOC_HIDE_START
local awful = require("awful")
local wibox = require("wibox")
local beautiful = require("beautiful")
local naughty = require("naughty")
local s = screen[1]
screen[1]._resize {width = 640, height = 320}
local wb = awful.wibar { position = "top" }
local modkey = "Mod4"
-- Create the same number of tags as the default config
awful.tag({ "1", "2", "3", "4", "5", "6", "7", "8", "9" }, screen[1], awful.layout.layouts[1])
-- Only bother with widgets that are visible by default
local mykeyboardlayout = awful.widget.keyboardlayout()
local mytextclock = wibox.widget.textclock()
local mytaglist = awful.widget.taglist(screen[1], awful.widget.taglist.filter.all, {})
local mytasklist = awful.widget.tasklist(screen[1], awful.widget.tasklist.filter.currenttags, {})
client.connect_signal("request::titlebars", function(c)
local top_titlebar = awful.titlebar(c, {
height = 20,
bg_normal = beautiful.bg_normal,
})
top_titlebar : setup {
{ -- Left
awful.titlebar.widget.iconwidget(c),
layout = wibox.layout.fixed.horizontal
},
{ -- Middle
{ -- Title
align = "center",
widget = awful.titlebar.widget.titlewidget(c)
},
layout = wibox.layout.flex.horizontal
},
{ -- Right
awful.titlebar.widget.floatingbutton (c),
awful.titlebar.widget.maximizedbutton(c),
awful.titlebar.widget.stickybutton (c),
awful.titlebar.widget.ontopbutton (c),
awful.titlebar.widget.closebutton (c),
layout = wibox.layout.fixed.horizontal()
},
layout = wibox.layout.align.horizontal
}
end)
wb:setup {
layout = wibox.layout.align.horizontal,
{
mytaglist,
layout = wibox.layout.fixed.horizontal,
},
mytasklist,
{
layout = wibox.layout.fixed.horizontal,
mykeyboardlayout,
mytextclock,
},
}
require("gears.timer").run_delayed_calls_now()
local counter = 0
local function gen_client(label)
local c = client.gen_fake {hide_first=true}
c:geometry {
x = 45 + counter*1.75,
y = 30 + counter,
height = 60,
width = 230,
}
c._old_geo = {c:geometry()}
c:set_label(label)
c:emit_signal("request::titlebars")
c.border_color = beautiful.bg_highlight
counter = counter + 40
return c
end
gen_client("C1")
gen_client("C2")
--DOC_HIDE_END
local function saved_screenshot(args)
local ss = awful.screenshot(args)
--DOC_NEWLINE
local function notify(self)
naughty.notification {
title = self.file_name,
message = "Screenshot saved",
icon = self.surface,
icon_size = 128,
}
end
--DOC_NEWLINE
if args.auto_save_delay > 0 then
ss:connect_signal("file::saved", notify)
else
notify(ss)
end
--DOC_NEWLINE
return ss
end
--DOC_NEWLINE
local function delayed_screenshot(args)
local ss = saved_screenshot(args)
local notif = naughty.notification {
title = "Screenshot in:",
message = tostring(args.auto_save_delay) .. " seconds"
}
--DOC_NEWLINE
ss:connect_signal("timer::tick", function(_, remain)
notif.message = tostring(remain) .. " seconds"
end)
--DOC_NEWLINE
ss:connect_signal("timer::timeout", function()
if notif then notif:destroy() end
end)
--DOC_NEWLINE
return ss
end
--DOC_NEWLINE
client.connect_signal("request::default_keybindings", function()
awful.keyboard.append_client_keybindings({
awful.key({modkey}, "Print",
function (c) saved_screenshot { auto_save_delay = 0, client = c } end ,
{description = "take client screenshot", group = "client"}),
awful.key({modkey, "Shift"}, "Print",
function (c) saved_screenshot { auto_save_delay = 0, interactive = true, client = c } end ,
{description = "take interactive client screenshot", group = "client"}),
awful.key({modkey, "Control"}, "Print",
function (c) delayed_screenshot { auto_save_delay = 5, client = c } end ,
{description = "take screenshot in 5 seconds", group = "client"}),
awful.key({modkey, "Shift", "Control"}, "Print",
function (c) delayed_screenshot { auto_save_delay = 5, interactive = true, client = c } end ,
{description = "take interactive screenshot in 5 seconds", group = "client"}),
})
end)
--DOC_NEWLINE
awful.keyboard.append_global_keybindings({
awful.key({}, "Print",
function () saved_screenshot { auto_save_delay = 0 } end ,
{description = "take screenshot", group = "client"}),
awful.key({"Shift"}, "Print",
function () saved_screenshot { auto_save_delay = 0, interactive = true } end ,
{description = "take interactive screenshot", group = "client"}),
awful.key({"Control"}, "Print",
function () delayed_screenshot { auto_save_delay = 5 } end ,
{description = "take screenshot in 5 seconds", group = "client"}),
awful.key({"Shift", "Control"}, "Print",
function () delayed_screenshot { auto_save_delay = 5, interactive = true } end ,
{description = "take interactive screenshot in 5 seconds", group = "client"}),
})
--DOC_HIDE_START
client.emit_signal("request::default_keybindings")
-- A notification popup using the default widget_template.
naughty.connect_signal("request::display", function(n)
naughty.layout.box {notification = n}
end)
awful.wallpaper {
screen = s,
widget = {
image = beautiful.wallpaper,
resize = true,
widget = wibox.widget.imagebox,
horizontal_fit_policy = "fit",
vertical_fit_policy = "fit",
}
}
saved_screenshot { auto_save_delay = 0 }
delayed_screenshot { auto_save_delay = 5 }
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
--DOC_HIDE_END

View File

@ -0,0 +1,158 @@
--DOC_NO_USAGE --DOC_GEN_IMAGE --DOC_HIDE_START
local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local beautiful = require("beautiful")
local s = screen[1]
screen[1]._resize {width = 640, height = 320}
local wb = awful.wibar { position = "top" }
-- Create the same number of tags as the default config
awful.tag({ "1", "2", "3", "4", "5", "6", "7", "8", "9" }, screen[1], awful.layout.layouts[1])
-- Only bother with widgets that are visible by default
local mykeyboardlayout = awful.widget.keyboardlayout()
local mytextclock = wibox.widget.textclock()
local mytaglist = awful.widget.taglist(screen[1], awful.widget.taglist.filter.all, {})
local mytasklist = awful.widget.tasklist(screen[1], awful.widget.tasklist.filter.currenttags, {})
client.connect_signal("request::titlebars", function(c)
local top_titlebar = awful.titlebar(c, {
height = 20,
bg_normal = beautiful.bg_normal,
})
top_titlebar : setup {
{ -- Left
awful.titlebar.widget.iconwidget(c),
layout = wibox.layout.fixed.horizontal
},
{ -- Middle
{ -- Title
align = "center",
widget = awful.titlebar.widget.titlewidget(c)
},
layout = wibox.layout.flex.horizontal
},
{ -- Right
awful.titlebar.widget.floatingbutton (c),
awful.titlebar.widget.maximizedbutton(c),
awful.titlebar.widget.stickybutton (c),
awful.titlebar.widget.ontopbutton (c),
awful.titlebar.widget.closebutton (c),
layout = wibox.layout.fixed.horizontal()
},
layout = wibox.layout.align.horizontal
}
end)
wb:setup {
layout = wibox.layout.align.horizontal,
{
mytaglist,
layout = wibox.layout.fixed.horizontal,
},
mytasklist,
{
layout = wibox.layout.fixed.horizontal,
mykeyboardlayout,
mytextclock,
},
}
require("gears.timer").run_delayed_calls_now()
local counter = 0
local function gen_client(label)
local c = client.gen_fake {hide_first=true}
c:geometry {
x = 45 + counter*1.75,
y = 30 + counter,
height = 60,
width = 230,
}
c._old_geo = {c:geometry()}
c:set_label(label)
c:emit_signal("request::titlebars")
c.border_color = beautiful.bg_highlight
counter = counter + 40
return c
end
local tasklist_buttons = {}
gen_client("C1")
gen_client("C2")
--DOC_HIDE_END
awful.popup {
widget = awful.widget.tasklist {
screen = screen[1],
filter = awful.widget.tasklist.filter.allscreen,
buttons = tasklist_buttons,
style = {
shape = gears.shape.rounded_rect,
align = "center"
},
layout = {
spacing = 5,
forced_num_rows = 1,
layout = wibox.layout.grid.horizontal
},
widget_template = {
{
{
id = "screenshot",
margins = 4,
forced_height = 128,
forced_width = 240,
widget = wibox.container.margin,
},
{
id = "text_role",
forced_height = 20,
forced_width = 240,
widget = wibox.widget.textbox,
},
widget = wibox.layout.fixed.vertical
},
id = "background_role",
widget = wibox.container.background,
create_callback = function(self, c) --luacheck: no unused
assert(c) --DOC_HIDE
local ss = awful.screenshot {
client = c,
}
ss:refresh()
local ib = ss.content_widget
ib.valign = "center"
ib.halign = "center"
self:get_children_by_id("screenshot")[1].widget = ib
assert(ss.surface) --DOC_HIDE
end,
},
},
border_color = "#777777",
border_width = 2,
ontop = true,
placement = awful.placement.centered,
shape = gears.shape.rounded_rect
}
--DOC_HIDE_START
awful.wallpaper {
screen = s,
widget = {
image = beautiful.wallpaper,
resize = true,
widget = wibox.widget.imagebox,
horizontal_fit_policy = "fit",
vertical_fit_policy = "fit",
}
}
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
--DOC_HIDE_END