2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
2021-12-21 06:54:15 +01:00
|
|
|
-- A simple button widget based on a background image.
|
2018-12-28 09:06:03 +01:00
|
|
|
--
|
2020-08-31 11:11:01 +02:00
|
|
|
-- @DOC_wibox_awidget_defaults_button_EXAMPLE@
|
2018-12-28 09:06:03 +01:00
|
|
|
--
|
2009-02-27 17:22:03 +01:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008-2009 Julien Danjou
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod awful.widget.button
|
2021-03-27 20:05:10 +01:00
|
|
|
-- @supermodule wibox.widget.imagebox
|
2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
2012-12-15 10:08:23 +01:00
|
|
|
local abutton = require("awful.button")
|
2010-10-06 14:28:43 +02:00
|
|
|
local imagebox = require("wibox.widget.imagebox")
|
2012-11-27 22:55:42 +01:00
|
|
|
local widget = require("wibox.widget.base")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
|
|
|
local cairo = require("lgi").cairo
|
2020-08-31 11:11:01 +02:00
|
|
|
local gtable = require("gears.table")
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
local button = { mt = {} }
|
2009-02-27 17:22:03 +01:00
|
|
|
|
|
|
|
--- Create a button widget. When clicked, the image is deplaced to make it like
|
|
|
|
-- a real button.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct awful.widget.button
|
2020-08-31 11:11:01 +02:00
|
|
|
-- @tparam table args Widget arguments.
|
|
|
|
-- @tparam string args.image "image" is the image to display (mandatory).
|
|
|
|
-- @tparam table args.buttons The buttons.
|
2009-02-27 17:22:03 +01:00
|
|
|
-- @return A textbox widget configured as a button.
|
2012-06-14 01:08:27 +02:00
|
|
|
function button.new(args)
|
2020-08-31 11:11:01 +02:00
|
|
|
args = args or {}
|
|
|
|
if not args.image then
|
2012-11-27 22:55:42 +01:00
|
|
|
return widget.empty_widget()
|
|
|
|
end
|
2010-09-29 15:57:10 +02:00
|
|
|
|
2010-10-06 14:28:43 +02:00
|
|
|
local w = imagebox()
|
2014-09-15 13:55:21 +02:00
|
|
|
local orig_set_image = w.set_image
|
|
|
|
local img_release
|
|
|
|
local img_press
|
|
|
|
|
2016-02-07 15:12:22 +01:00
|
|
|
function w:set_image(image)
|
2014-09-15 13:55:21 +02:00
|
|
|
img_release = surface.load(image)
|
|
|
|
img_press = img_release:create_similar(cairo.Content.COLOR_ALPHA, img_release.width, img_release.height)
|
|
|
|
local cr = cairo.Context(img_press)
|
|
|
|
cr:set_source_surface(img_release, 2, 2)
|
|
|
|
cr:paint()
|
2016-02-07 15:12:22 +01:00
|
|
|
orig_set_image(self, img_release)
|
2014-09-15 13:55:21 +02:00
|
|
|
end
|
|
|
|
w:set_image(args.image)
|
2019-10-06 09:04:51 +02:00
|
|
|
|
2020-08-31 11:11:01 +02:00
|
|
|
local btns = gtable.clone(args.buttons or {}, false)
|
|
|
|
|
|
|
|
table.insert(btns,
|
2019-10-06 09:04:51 +02:00
|
|
|
abutton({}, 1, function () orig_set_image(w, img_press) end,
|
|
|
|
function () orig_set_image(w, img_release) end)
|
2020-08-31 11:11:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
w.buttons = btns
|
2016-05-10 05:14:55 +02:00
|
|
|
|
|
|
|
w:connect_signal("mouse::leave", function(self) orig_set_image(self, img_release) end)
|
|
|
|
|
2009-02-27 17:22:03 +01:00
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function button.mt:__call(...)
|
|
|
|
return button.new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(button, button.mt)
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|