From 6de6419180dd7e833f452b93594d518227fa5301 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 30 May 2017 10:32:02 +0200 Subject: [PATCH 1/3] Add functions to draw widgets to a cairo surface These are supposed to eventually replace the already-existing functions in gears.surface which have a similar signature Signed-off-by: Uli Schlachter --- docs/config.ld | 1 - lib/wibox/widget/init.lua | 60 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/docs/config.ld b/docs/config.ld index c9c1cbf8..84370c54 100644 --- a/docs/config.ld +++ b/docs/config.ld @@ -105,7 +105,6 @@ file = { '../lib/awful/startup_notification.lua', '../lib/gears/init.lua', '../lib/wibox/layout/init.lua', - '../lib/wibox/widget/init.lua', '../lib/wibox/container/init.lua', -- Ignore some parts of the widget library diff --git a/lib/wibox/widget/init.lua b/lib/wibox/widget/init.lua index 43c6fc0d..f33ce345 100644 --- a/lib/wibox/widget/init.lua +++ b/lib/wibox/widget/init.lua @@ -3,10 +3,12 @@ -- @copyright 2010 Uli Schlachter -- @classmod wibox.widget --------------------------------------------------------------------------- -local base = require("wibox.widget.base") -return setmetatable({ - base = base; +local cairo = require("lgi").cairo +local hierarchy = require("wibox.hierarchy") + +local widget = { + base = require("wibox.widget.base"); textbox = require("wibox.widget.textbox"); imagebox = require("wibox.widget.imagebox"); background = require("wibox.widget.background"); @@ -17,6 +19,56 @@ return setmetatable({ checkbox = require("wibox.widget.checkbox"); piechart = require("wibox.widget.piechart"); slider = require("wibox.widget.slider"); -}, {__call = function(_, args) return base.make_widget_declarative(args) end}) +} + +setmetatable(widget, { + __call = function(_, args) + return widget.base.make_widget_declarative(args) + end +}) + +--- Draw a widget directly to a given cairo context. +-- This function creates a temporary `wibox.hierarchy` instance and uses that to +-- draw the given widget once to the given cairo context. +-- @tparam widget wdg A widget to draw +-- @tparam cairo_context cr The cairo context to draw the widget on +-- @tparam number width The width of the widget +-- @tparam number height The height of the widget +-- @tparam[opt={dpi=96}] table context The context information to give to the widget. +function widget.draw_to_cairo_context(wdg, cr, width, height, context) + local function no_op() end + context = context or {dpi=96} + local h = hierarchy.new(context, wdg, width, height, no_op, no_op, {}) + h:draw(context, cr) +end + +--- Create an SVG file showing this widget. +-- @tparam widget wdg A widget +-- @tparam string path The output file path +-- @tparam number width The surface width +-- @tparam number height The surface height +-- @tparam[opt={dpi=96}] table context The context information to give to the widget. +function widget.draw_to_svg_file(wdg, path, width, height, context) + local img = cairo.SvgSurface.create(path, width, height) + local cr = cairo.Context(img) + widget.draw_to_cairo_context(wdg, cr, width, height, context) + img:finish() +end + +--- Create a cairo image surface showing this widget. +-- @tparam widget wdg A widget +-- @tparam number width The surface width +-- @tparam number height The surface height +-- @param[opt=cairo.Format.ARGB32] format The surface format +-- @tparam[opt={dpi=96}] table context The context information to give to the widget. +-- @return The cairo surface +function widget.draw_to_image_surface(wdg, width, height, format, context) + local img = cairo.ImageSurface(format or cairo.Format.ARGB32, width, height) + local cr = cairo.Context(img) + widget.draw_to_cairo_context(wdg, cr, width, height, context) + return img +end + +return widget -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 From 0abfed54decf6e2dc5185f1697b2ea436f200017 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 30 May 2017 10:33:52 +0200 Subject: [PATCH 2/3] example tests: Switch to new API This makes the templates used by the example tests use the new API added in the previous commit. Signed-off-by: Uli Schlachter --- tests/examples/wibox/container/defaults/template.lua | 4 +--- tests/examples/wibox/layout/template.lua | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/examples/wibox/container/defaults/template.lua b/tests/examples/wibox/container/defaults/template.lua index 6e7a4551..5f6fffdb 100644 --- a/tests/examples/wibox/container/defaults/template.lua +++ b/tests/examples/wibox/container/defaults/template.lua @@ -3,7 +3,6 @@ require("_common_template")(...) local beautiful = require( "beautiful" ) local wibox = require( "wibox" ) -local surface = require( "gears.surface" ) local shape = require( "gears.shape" ) -- Let the test request a size and file format @@ -72,7 +71,6 @@ end local f_w, f_h = container:fit({dpi=96}, 9999, 9999) -- Save to the output file -local img = surface.widget_to_svg(container, image_path..".svg", f_w, f_h) -img:finish() +wibox.widget.draw_to_svg_file(container, image_path..".svg", f_w, f_h) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/wibox/layout/template.lua b/tests/examples/wibox/layout/template.lua index f399049c..1fbdc322 100644 --- a/tests/examples/wibox/layout/template.lua +++ b/tests/examples/wibox/layout/template.lua @@ -2,7 +2,6 @@ local file_path, image_path = ... require("_common_template")(...) local wibox = require( "wibox" ) -local surface = require( "gears.surface" ) local color = require( "gears.color" ) local beautiful = require( "beautiful" ) local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) @@ -99,7 +98,6 @@ for _ = 1, 10 do end -- Save to the output file -local img = surface["widget_to_svg"](widget, image_path..".svg", w or 200, h or 30) -img:finish() +wibox.widget.draw_to_svg_file(widget, image_path..".svg", w or 200, h or 30) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 From 801a1f0a89a754dd7806b1fcd3006b3b364936cc Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 30 May 2017 10:34:33 +0200 Subject: [PATCH 3/3] Deprecate widget-drawing functions in gears.surface This marks the functions gears.surface.widget_to_svg() and gears.surface.widget_to_surface() as deprecated in awesome 5. This means that by the time that awesome 6 becomes a thing, we can finally remove these... Signed-off-by: Uli Schlachter --- lib/gears/surface.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/gears/surface.lua b/lib/gears/surface.lua index 21b5c795..1edfcd19 100644 --- a/lib/gears/surface.lua +++ b/lib/gears/surface.lua @@ -218,6 +218,7 @@ end --- Create an SVG file with this widget content. -- This is dynamic, so the SVG will be updated along with the widget content. -- because of this, the painting may happen hover multiple event loop cycles. +-- @deprecated wibox.widget.draw_to_svg_file -- @tparam widget widget A widget -- @tparam string path The output file path -- @tparam number width The surface width @@ -225,6 +226,8 @@ end -- @return The cairo surface -- @return The hierarchy function surface.widget_to_svg(widget, path, width, height) + gdebug.deprecate("Use wibox.widget.draw_to_svg_file instead of ".. + "gears.surface.render_to_svg", {deprecated_in=5}) local img = cairo.SvgSurface.create(path, width, height) local cr = cairo.Context(img) @@ -234,6 +237,7 @@ end --- Create a cairo surface with this widget content. -- This is dynamic, so the SVG will be updated along with the widget content. -- because of this, the painting may happen hover multiple event loop cycles. +-- @deprecated wibox.widget.draw_to_image_surface -- @tparam widget widget A widget -- @tparam number width The surface width -- @tparam number height The surface height @@ -241,6 +245,8 @@ end -- @return The cairo surface -- @return The hierarchy function surface.widget_to_surface(widget, width, height, format) + gdebug.deprecate("Use wibox.widget.draw_to_image_surface instead of ".. + "gears.surface.render_to_surface", {deprecated_in=5}) local img = cairo.ImageSurface(format or cairo.Format.ARGB32, width, height) local cr = cairo.Context(img)