2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
2021-07-14 23:17:49 +02:00
|
|
|
-- A widget to display an image.
|
2019-11-21 16:14:59 +01:00
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- The `wibox.widget.imagebox` is part of the Awesome WM's widget system
|
2019-11-22 19:06:37 +01:00
|
|
|
-- (see @{03-declarative-layout.md}).
|
|
|
|
--
|
|
|
|
-- This widget displays an image. The image can be a file,
|
|
|
|
-- a cairo image surface, or an rsvg handle object (see the
|
|
|
|
-- [image property](#image)).
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- Examples using a `wibox.widget.imagebox`:
|
2019-11-22 19:06:37 +01:00
|
|
|
-- ---
|
2019-11-21 16:14:59 +01:00
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_defaults_imagebox_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- Alternatively, you can declare the `imagebox` widget using the
|
2021-07-14 23:17:49 +02:00
|
|
|
-- declarative pattern (both variants are strictly equivalent):
|
2019-11-21 16:14:59 +01:00
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_declarative-pattern_imagebox_EXAMPLE@
|
2016-05-25 23:56:23 +02:00
|
|
|
--
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod wibox.widget.imagebox
|
2021-03-27 20:41:28 +01:00
|
|
|
-- @supermodule wibox.widget.base
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
local lgi = require("lgi")
|
|
|
|
local cairo = lgi.cairo
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
2017-03-08 21:18:33 +01:00
|
|
|
local gtable = require("gears.table")
|
2019-06-21 23:30:10 +02:00
|
|
|
local gdebug = require("gears.debug")
|
2010-10-06 12:42:56 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local type = type
|
2019-06-21 23:30:10 +02:00
|
|
|
local math = math
|
|
|
|
|
2016-02-07 14:13:43 +01:00
|
|
|
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
-- Safe load for optional Rsvg module
|
|
|
|
local Rsvg = nil
|
|
|
|
do
|
|
|
|
local success, err = pcall(function() Rsvg = lgi.Rsvg end)
|
|
|
|
if not success then
|
|
|
|
gdebug.print_warning(debug.traceback("Could not load Rsvg: " .. tostring(err)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local imagebox = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
local rsvg_handle_cache = setmetatable({}, { __mode = 'k' })
|
2019-06-21 23:30:10 +02:00
|
|
|
|
|
|
|
---Load rsvg handle form image file
|
2021-09-23 05:51:11 +02:00
|
|
|
-- @tparam string file Path to svg file.
|
|
|
|
-- @return Rsvg handle
|
|
|
|
-- @treturn table A table where cached data can be stored.
|
2019-06-21 23:30:10 +02:00
|
|
|
local function load_rsvg_handle(file)
|
|
|
|
if not Rsvg then return end
|
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
local cache = (rsvg_handle_cache[file] or {})["handle"]
|
2021-09-12 01:27:44 +02:00
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
if cache then
|
2021-09-12 08:07:27 +02:00
|
|
|
return cache, rsvg_handle_cache[file]
|
2019-06-21 23:30:10 +02:00
|
|
|
end
|
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
local handle, err
|
2021-09-12 01:27:44 +02:00
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
if file:match("<[?]?xml") or file:match("<svg") then
|
2021-09-12 01:27:44 +02:00
|
|
|
handle, err = Rsvg.Handle.new_from_data(file)
|
|
|
|
else
|
|
|
|
handle, err = Rsvg.Handle.new_from_file(file)
|
|
|
|
end
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
if not err then
|
2021-09-12 08:07:27 +02:00
|
|
|
rsvg_handle_cache[file] = rsvg_handle_cache[file] or {}
|
|
|
|
rsvg_handle_cache[file]["handle"] = handle
|
|
|
|
return handle, rsvg_handle_cache[file]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---Apply cairo surface for given imagebox widget
|
|
|
|
local function set_surface(ib, surf)
|
2021-09-23 05:51:11 +02:00
|
|
|
local is_surf_valid = surf.width > 0 and surf.height > 0
|
|
|
|
if not is_surf_valid then return false end
|
2021-09-12 08:07:27 +02:00
|
|
|
|
|
|
|
ib._private.default = { width = surf.width, height = surf.height }
|
|
|
|
ib._private.handle = nil
|
|
|
|
ib._private.image = surf
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
---Apply RsvgHandle for given imagebox widget
|
|
|
|
local function set_handle(ib, handle, cache)
|
|
|
|
local dim = handle:get_dimensions()
|
|
|
|
local is_handle_valid = dim.width > 0 and dim.height > 0
|
2021-09-23 05:51:11 +02:00
|
|
|
if not is_handle_valid then return false end
|
2021-09-12 08:07:27 +02:00
|
|
|
|
|
|
|
ib._private.default = { width = dim.width, height = dim.height }
|
|
|
|
ib._private.handle = handle
|
|
|
|
ib._private.cache = cache
|
|
|
|
ib._private.image = nil
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
---Try to load some image object from file then apply it to imagebox.
|
|
|
|
---@tparam table ib Imagebox
|
|
|
|
---@tparam string file Image file name
|
|
|
|
---@tparam function image_loader Function to load image object from file
|
|
|
|
---@tparam function image_setter Function to set image object to imagebox
|
|
|
|
---@treturn boolean True if image was successfully applied
|
|
|
|
local function load_and_apply(ib, file, image_loader, image_setter)
|
|
|
|
local image_applied
|
|
|
|
local object, cache = image_loader(file)
|
|
|
|
|
|
|
|
if object then
|
|
|
|
image_applied = image_setter(ib, object, cache)
|
|
|
|
end
|
|
|
|
return image_applied
|
|
|
|
end
|
|
|
|
|
|
|
|
---Update the cached size depending on the stylesheet and dpi.
|
|
|
|
--
|
|
|
|
-- It's necessary because a single RSVG handle can be used by
|
|
|
|
-- many imageboxes. So DPI and Stylesheet need to be set each time.
|
|
|
|
local function update_dpi(self, ctx)
|
|
|
|
if not self._private.handle then return end
|
|
|
|
|
|
|
|
local dpi = self._private.auto_dpi and
|
|
|
|
ctx.dpi or
|
|
|
|
self._private.dpi or
|
|
|
|
nil
|
|
|
|
|
|
|
|
local need_dpi = dpi and
|
|
|
|
self._private.last_dpi ~= dpi
|
|
|
|
|
|
|
|
local need_style = self._private.handle.set_stylesheet and
|
|
|
|
self._private.stylesheet
|
|
|
|
|
|
|
|
local old_size = self._private.default and self._private.default.width
|
|
|
|
|
|
|
|
if dpi and dpi ~= self._private.cache.dpi then
|
|
|
|
if type(dpi) == "table" then
|
|
|
|
self._private.handle:set_dpi_x_y(dpi.x, dpi.y)
|
|
|
|
else
|
|
|
|
self._private.handle:set_dpi(dpi)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if need_style and self._private.cache.stylesheet ~= self._private.stylesheet then
|
|
|
|
self._private.handle:set_stylesheet(self._private.stylesheet)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Reload the size.
|
|
|
|
if need_dpi or (need_style and self._private.stylesheet ~= self._private.last_stylesheet) then
|
|
|
|
set_handle(self, self._private.handle, self._private.cache)
|
|
|
|
end
|
|
|
|
|
|
|
|
self._private.last_dpi = dpi
|
|
|
|
self._private.cache.dpi = dpi
|
|
|
|
self._private.last_stylesheet = self._private.stylesheet
|
|
|
|
self._private.cache.stylesheet = self._private.stylesheet
|
|
|
|
|
|
|
|
-- This can happen in the constructor when `dpi` is set after `image`.
|
|
|
|
if old_size and old_size ~= self._private.default.width then
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2019-06-21 23:30:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Draw an imagebox with the given cairo context in the given geometry.
|
2021-09-12 08:07:27 +02:00
|
|
|
function imagebox:draw(ctx, cr, width, height)
|
2019-06-21 23:30:10 +02:00
|
|
|
if width == 0 or height == 0 or not self._private.default then return end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2021-04-26 09:15:15 +02:00
|
|
|
-- For valign = "top" and halign = "left"
|
|
|
|
local translate = {
|
|
|
|
x = 0,
|
|
|
|
y = 0,
|
|
|
|
}
|
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
update_dpi(self, ctx)
|
|
|
|
|
2021-04-26 09:15:15 +02:00
|
|
|
local w, h = self._private.default.width, self._private.default.height
|
2019-06-13 01:39:01 +02:00
|
|
|
|
2021-09-11 00:45:49 +02:00
|
|
|
if self._private.resize then
|
2021-04-26 09:15:15 +02:00
|
|
|
-- That's for the "fit" policy.
|
|
|
|
local aspects = {
|
|
|
|
w = width / w,
|
|
|
|
h = height / h
|
|
|
|
}
|
|
|
|
|
|
|
|
local policy = {
|
|
|
|
w = self._private.horizontal_fit_policy or "auto",
|
|
|
|
h = self._private.vertical_fit_policy or "auto"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, aspect in ipairs {"w", "h"} do
|
2021-09-11 00:45:49 +02:00
|
|
|
if self._private.upscale == false and (w < width and h < height) then
|
|
|
|
aspects[aspect] = 1
|
|
|
|
elseif self._private.downscale == false and (w >= width and h >= height) then
|
|
|
|
aspects[aspect] = 1
|
2021-04-26 09:15:15 +02:00
|
|
|
elseif policy[aspect] == "none" then
|
|
|
|
aspects[aspect] = 1
|
2021-09-11 00:45:49 +02:00
|
|
|
elseif policy[aspect] == "auto" then
|
|
|
|
aspects[aspect] = math.min(width / w, height / h)
|
2021-04-26 09:15:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self._private.halign == "center" then
|
|
|
|
translate.x = math.floor((width - w*aspects.w)/2)
|
|
|
|
elseif self._private.halign == "right" then
|
|
|
|
translate.x = math.floor(width - (w*aspects.w))
|
|
|
|
end
|
|
|
|
|
|
|
|
if self._private.valign == "center" then
|
|
|
|
translate.y = math.floor((height - h*aspects.h)/2)
|
|
|
|
elseif self._private.valign == "bottom" then
|
|
|
|
translate.y = math.floor(height - (h*aspects.h))
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:translate(translate.x, translate.y)
|
|
|
|
|
|
|
|
-- Before using the scale, make sure it is below the threshold.
|
|
|
|
local threshold, max_factor = self._private.max_scaling_factor, math.max(aspects.w, aspects.h)
|
|
|
|
|
|
|
|
if threshold and threshold > 0 and threshold < max_factor then
|
|
|
|
aspects.w = (aspects.w*threshold)/max_factor
|
|
|
|
aspects.h = (aspects.h*threshold)/max_factor
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set the clip
|
|
|
|
if self._private.clip_shape then
|
|
|
|
cr:clip(self._private.clip_shape(cr, w*aspects.w, h*aspects.h, unpack(self._private.clip_args)))
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:scale(aspects.w, aspects.h)
|
|
|
|
else
|
|
|
|
if self._private.halign == "center" then
|
|
|
|
translate.x = math.floor((width - w)/2)
|
|
|
|
elseif self._private.halign == "right" then
|
|
|
|
translate.x = math.floor(width - w)
|
|
|
|
end
|
|
|
|
|
|
|
|
if self._private.valign == "center" then
|
|
|
|
translate.y = math.floor((height - h)/2)
|
|
|
|
elseif self._private.valign == "bottom" then
|
|
|
|
translate.y = math.floor(height - h)
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:translate(translate.x, translate.y)
|
|
|
|
|
|
|
|
-- Set the clip
|
|
|
|
if self._private.clip_shape then
|
|
|
|
cr:clip(self._private.clip_shape(cr, w, h, unpack(self._private.clip_args)))
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2016-01-20 08:09:07 +01:00
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
if self._private.handle then
|
|
|
|
self._private.handle:render_cairo(cr)
|
|
|
|
else
|
|
|
|
cr:set_source_surface(self._private.image, 0, 0)
|
2021-04-26 09:15:15 +02:00
|
|
|
|
|
|
|
local filter = self._private.scaling_quality
|
|
|
|
|
|
|
|
if filter then
|
|
|
|
cr:get_source():set_filter(cairo.Filter[filter:upper()])
|
|
|
|
end
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
cr:paint()
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Fit the imagebox into the given geometry
|
2021-09-12 08:07:27 +02:00
|
|
|
function imagebox:fit(ctx, width, height)
|
2019-06-21 23:30:10 +02:00
|
|
|
if not self._private.default then return 0, 0 end
|
2021-09-12 08:07:27 +02:00
|
|
|
|
|
|
|
update_dpi(self, ctx)
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
local w, h = self._private.default.width, self._private.default.height
|
|
|
|
|
2021-09-11 00:45:49 +02:00
|
|
|
if w <= width and h <= height and self._private.upscale == false then
|
|
|
|
return w, h
|
|
|
|
end
|
|
|
|
|
|
|
|
if (w < width or h < height) and self._private.downscale == false then
|
|
|
|
return w, h
|
|
|
|
end
|
|
|
|
|
|
|
|
if self._private.resize or w > width or h > height then
|
2019-06-21 23:30:10 +02:00
|
|
|
local aspect = math.min(width / w, height / h)
|
|
|
|
return w * aspect, h * aspect
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
return w, h
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2019-11-21 17:34:28 +01:00
|
|
|
--- The image rendered by the `imagebox`.
|
|
|
|
--
|
2019-11-22 19:06:37 +01:00
|
|
|
-- It can can be any of the following:
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- * A `string`: Interpreted as a path to an image file
|
|
|
|
-- * A cairo image surface: Directly used as-is
|
|
|
|
-- * A librsvg handle object: Directly used as-is
|
|
|
|
-- * `nil`: Unset the image.
|
2019-11-21 17:34:28 +01:00
|
|
|
--
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @property image
|
2021-09-23 05:51:11 +02:00
|
|
|
-- @tparam image image The image to render.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits false false
|
2019-11-21 17:34:28 +01:00
|
|
|
|
|
|
|
--- Set the `imagebox` image.
|
2019-11-22 19:06:37 +01:00
|
|
|
--
|
|
|
|
-- The image can be a file, a cairo image surface, or an rsvg handle object
|
|
|
|
-- (see the [image property](#image)).
|
2021-03-28 18:46:08 +02:00
|
|
|
-- @method set_image
|
2021-03-29 09:39:46 +02:00
|
|
|
-- @hidden
|
2019-11-21 17:34:28 +01:00
|
|
|
-- @tparam image image The image to render.
|
|
|
|
-- @treturn boolean `true` on success, `false` if the image cannot be used.
|
|
|
|
-- @usage my_imagebox:set_image(beautiful.awesome_icon)
|
|
|
|
-- @usage my_imagebox:set_image('/usr/share/icons/theme/my_icon.png')
|
2019-11-22 19:06:37 +01:00
|
|
|
-- @see image
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:set_image(image)
|
2019-06-21 23:30:10 +02:00
|
|
|
local setup_succeed
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
-- Keep the original to prevent the cache from being GCed.
|
2021-09-23 05:51:11 +02:00
|
|
|
self._private.original_image = image
|
2021-09-12 08:07:27 +02:00
|
|
|
|
|
|
|
if type(image) == "userdata" and not (Rsvg and Rsvg.Handle:is_type_of(image)) then
|
2019-10-11 16:14:57 +02:00
|
|
|
-- This function is not documented to handle userdata objects, but
|
|
|
|
-- historically it did, and it did by just assuming they refer to a
|
|
|
|
-- cairo surface.
|
|
|
|
image = surface.load(image)
|
|
|
|
end
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
if type(image) == "string" then
|
|
|
|
-- try to load rsvg handle from file
|
|
|
|
setup_succeed = load_and_apply(self, image, load_rsvg_handle, set_handle)
|
2012-05-27 19:20:34 +02:00
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
if not setup_succeed then
|
|
|
|
-- rsvg handle failed, try to load cairo surface with pixbuf
|
|
|
|
setup_succeed = load_and_apply(self, image, surface.load, set_surface)
|
2011-03-04 22:03:42 +01:00
|
|
|
end
|
2019-06-21 23:30:10 +02:00
|
|
|
elseif Rsvg and Rsvg.Handle:is_type_of(image) then
|
|
|
|
-- try to apply given rsvg handle
|
2021-09-12 08:07:27 +02:00
|
|
|
rsvg_handle_cache[image] = rsvg_handle_cache[image] or {}
|
|
|
|
setup_succeed = set_handle(self, image, rsvg_handle_cache[image])
|
2019-06-21 23:30:10 +02:00
|
|
|
elseif cairo.Surface:is_type_of(image) then
|
|
|
|
-- try to apply given cairo surface
|
|
|
|
setup_succeed = set_surface(self, image)
|
|
|
|
elseif not image then
|
|
|
|
-- nil as argument mean full imagebox reset
|
|
|
|
setup_succeed = true
|
|
|
|
self._private.handle = nil
|
|
|
|
self._private.image = nil
|
|
|
|
self._private.default = nil
|
2011-03-04 22:03:42 +01:00
|
|
|
end
|
|
|
|
|
2019-06-21 23:30:10 +02:00
|
|
|
if not setup_succeed then return false end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-06-14 16:36:00 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::image")
|
2010-10-06 12:42:56 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-11-21 17:34:28 +01:00
|
|
|
--- Set a clip shape for this imagebox.
|
2021-07-14 23:17:49 +02:00
|
|
|
--
|
|
|
|
-- A clip shape defines an area and dimension to which the content should be
|
|
|
|
-- trimmed.
|
2016-05-26 23:39:27 +02:00
|
|
|
--
|
2021-04-26 09:15:15 +02:00
|
|
|
-- @DOC_wibox_widget_imagebox_clip_shape_EXAMPLE@
|
|
|
|
--
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @property clip_shape
|
2019-11-21 17:34:28 +01:00
|
|
|
-- @tparam function|gears.shape clip_shape A `gears.shape` compatible shape function.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
|
2019-11-21 17:34:28 +01:00
|
|
|
--- Set a clip shape for this imagebox.
|
2016-01-20 08:09:07 +01:00
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- A clip shape defines an area and dimensions to which the content should be
|
|
|
|
-- trimmed.
|
|
|
|
--
|
|
|
|
-- Additional parameters will be passed to the clip shape function.
|
2016-01-20 08:09:07 +01:00
|
|
|
--
|
2019-11-21 17:34:28 +01:00
|
|
|
-- @tparam function|gears.shape clip_shape A `gears_shape` compatible shape function.
|
2021-03-28 18:46:08 +02:00
|
|
|
-- @method set_clip_shape
|
2021-03-29 09:39:46 +02:00
|
|
|
-- @hidden
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
-- @see clip_shape
|
2016-01-20 08:09:07 +01:00
|
|
|
function imagebox:set_clip_shape(clip_shape, ...)
|
2016-05-26 23:39:27 +02:00
|
|
|
self._private.clip_shape = clip_shape
|
|
|
|
self._private.clip_args = {...}
|
2016-01-20 08:09:07 +01:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::clip_shape", clip_shape)
|
2016-01-20 08:09:07 +01:00
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Should the image be resized to fit into the available space?
|
2021-09-11 00:45:49 +02:00
|
|
|
--
|
|
|
|
-- Note that `upscale` and `downscale` can affect the value of `resize`.
|
|
|
|
-- If conflicting values are passed to the constructor, then the result
|
|
|
|
-- is undefined.
|
|
|
|
--
|
2019-11-28 12:42:29 +01:00
|
|
|
-- @DOC_wibox_widget_imagebox_resize_EXAMPLE@
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @property resize
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2019-11-21 17:34:28 +01:00
|
|
|
-- @tparam boolean resize
|
|
|
|
|
2021-09-11 00:45:49 +02:00
|
|
|
--- Allow the image to be upscaled (made bigger).
|
|
|
|
--
|
|
|
|
-- Note that `upscale` and `downscale` can affect the value of `resize`.
|
|
|
|
-- If conflicting values are passed to the constructor, then the result
|
|
|
|
-- is undefined.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_upscale_EXAMPLE@
|
|
|
|
-- @property upscale
|
|
|
|
-- @tparam boolean upscale
|
|
|
|
-- @see downscale
|
|
|
|
-- @see resize
|
|
|
|
|
|
|
|
--- Allow the image to be downscaled (made smaller).
|
|
|
|
--
|
|
|
|
-- Note that `upscale` and `downscale` can affect the value of `resize`.
|
|
|
|
-- If conflicting values are passed to the constructor, then the result
|
|
|
|
-- is undefined.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_downscale_EXAMPLE@
|
|
|
|
-- @property downscale
|
|
|
|
-- @tparam boolean downscale
|
|
|
|
-- @see upscale
|
|
|
|
-- @see resize
|
|
|
|
|
2021-09-12 08:07:27 +02:00
|
|
|
--- Set the SVG CSS stylesheet.
|
|
|
|
--
|
|
|
|
-- If the image is an SVG (vector graphics), this property allows to set
|
|
|
|
-- a CSS stylesheet. It can be used to set colors and much more.
|
|
|
|
--
|
|
|
|
-- Note that this property is a string, not a path. If the stylesheet is
|
|
|
|
-- stored on disk, read the content first.
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_imagebox_stylesheet_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property stylesheet
|
|
|
|
-- @tparam string stylesheet
|
|
|
|
-- @propemits true false
|
|
|
|
|
|
|
|
--- Set the SVG DPI (dot per inch).
|
|
|
|
--
|
|
|
|
-- Force a specific DPI when rendering the `.svg`. For other file formats,
|
|
|
|
-- this does nothing.
|
|
|
|
--
|
|
|
|
-- It can either be a number of a table containing the `x` and `y` keys.
|
|
|
|
--
|
|
|
|
-- Please note that DPI and `resize` can "fight" each other and end up
|
|
|
|
-- making the image smaller instead of bigger.
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_imagebox_dpi_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property dpi
|
|
|
|
-- @tparam number|table dpi
|
|
|
|
-- @propemits true false
|
|
|
|
-- @see auto_dpi
|
|
|
|
|
|
|
|
--- Use the object DPI when rendering the SVG.
|
|
|
|
--
|
|
|
|
-- By default, the SVG are interpreted as-is. When this property is set,
|
|
|
|
-- the screen DPI will be passed to the SVG renderer. Depending on which
|
|
|
|
-- tool was used to create the `.svg`, this may do nothing at all. However,
|
|
|
|
-- for example, if the `.svg` uses `<text>` elements and doesn't have an
|
|
|
|
-- hardcoded stylesheet, the result will differ.
|
|
|
|
--
|
|
|
|
-- @property auto_dpi
|
|
|
|
-- @tparam[opt=false] boolean auto_dpi
|
|
|
|
-- @propemits true false
|
|
|
|
-- @see dpi
|
|
|
|
|
|
|
|
for _, prop in ipairs {"stylesheet", "dpi", "auto_dpi"} do
|
|
|
|
imagebox["set_" .. prop] = function(self, value)
|
|
|
|
-- It will be set in :fit and :draw. The handle is shared
|
|
|
|
-- by multiple imagebox, so it cannot be set just once.
|
|
|
|
self._private[prop] = value
|
|
|
|
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
self:emit_signal("property::" .. prop)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:set_resize(allowed)
|
2021-09-11 00:45:49 +02:00
|
|
|
self._private.resize = allowed
|
|
|
|
|
|
|
|
if allowed then
|
|
|
|
self._private.downscale = true
|
|
|
|
self._private.upscale = true
|
|
|
|
self:emit_signal("property::downscale", allowed)
|
|
|
|
self:emit_signal("property::upscale", allowed)
|
|
|
|
end
|
|
|
|
|
2015-06-14 16:36:00 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::resize", allowed)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2021-09-11 00:45:49 +02:00
|
|
|
for _, prop in ipairs {"downscale", "upscale" } do
|
|
|
|
imagebox["set_" .. prop] = function(self, allowed)
|
|
|
|
self._private[prop] = allowed
|
|
|
|
|
|
|
|
if self._private.resize ~= (self._private.upscale or self._private.downscale) then
|
|
|
|
self._private.resize = self._private.upscale or self._private.downscale
|
|
|
|
self:emit_signal("property::resize", self._private.resize)
|
|
|
|
end
|
|
|
|
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
self:emit_signal("property::"..prop, allowed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-26 09:15:15 +02:00
|
|
|
--- Set the horizontal fit policy.
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- Valid values are:
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- * `"auto"`: Honor the `resize` variable and preserve the aspect ratio.
|
|
|
|
-- This is the default behaviour.
|
|
|
|
-- * `"none"`: Do not resize at all.
|
|
|
|
-- * `"fit"`: Resize to the widget width.
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
|
|
|
-- Here is the result for a 22x32 image:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_horizontal_fit_policy_EXAMPLE@
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- @property horizontal_fit_policy
|
|
|
|
-- @tparam[opt="auto"] string horizontal_fit_policy
|
|
|
|
-- @propemits true false
|
|
|
|
-- @see vertical_fit_policy
|
|
|
|
-- @see resize
|
2021-04-26 09:15:15 +02:00
|
|
|
|
|
|
|
--- Set the vertical fit policy.
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- Valid values are:
|
|
|
|
--
|
|
|
|
-- * `"auto"`: Honor the `resize` varible and preserve the aspect ratio.
|
|
|
|
-- This is the default behaviour.
|
|
|
|
-- * `"none"`: Do not resize at all.
|
|
|
|
-- * `"fit"`: Resize to the widget height.
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
|
|
|
-- Here is the result for a 32x22 image:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_vertical_fit_policy_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property vertical_fit_policy
|
2021-07-14 23:17:49 +02:00
|
|
|
-- @tparam[opt="auto"] string horizontal_fit_policy
|
2021-04-26 09:15:15 +02:00
|
|
|
-- @propemits true false
|
|
|
|
-- @see horizontal_fit_policy
|
|
|
|
-- @see resize
|
|
|
|
|
|
|
|
|
|
|
|
--- The vertical alignment.
|
|
|
|
--
|
|
|
|
-- Possible values are:
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- * `"top"`
|
|
|
|
-- * `"center"` (default)
|
|
|
|
-- * `"bottom"`
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_valign_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property valign
|
2021-07-14 23:17:49 +02:00
|
|
|
-- @tparam[opt="center"] string valign
|
2021-04-26 09:15:15 +02:00
|
|
|
-- @propemits true false
|
|
|
|
-- @see wibox.container.place
|
|
|
|
-- @see halign
|
|
|
|
|
|
|
|
--- The horizontal alignment.
|
|
|
|
--
|
|
|
|
-- Possible values are:
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- * `"left"`
|
|
|
|
-- * `"center"` (default)
|
|
|
|
-- * `"right"`
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_halign_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property halign
|
2021-07-14 23:17:49 +02:00
|
|
|
-- @tparam[opt="center"] string halign
|
2021-04-26 09:15:15 +02:00
|
|
|
-- @propemits true false
|
|
|
|
-- @see wibox.container.place
|
|
|
|
-- @see valign
|
|
|
|
|
|
|
|
--- The maximum scaling factor.
|
|
|
|
--
|
|
|
|
-- If an image is scaled too much, it gets very blurry. This
|
2021-07-14 23:17:49 +02:00
|
|
|
-- property allows to limit the scaling.
|
|
|
|
-- Use the properties `valign` and `halign` to control how the image will be
|
|
|
|
-- aligned.
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
|
|
|
-- In the example below, the original size is 22x22
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_max_scaling_factor_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property max_scaling_factor
|
|
|
|
-- @tparam number max_scaling_factor
|
|
|
|
-- @propemits true false
|
|
|
|
-- @see valign
|
|
|
|
-- @see halign
|
|
|
|
-- @see scaling_quality
|
|
|
|
|
|
|
|
--- Set the scaling aligorithm.
|
|
|
|
--
|
|
|
|
-- Depending on how the image is used, what is the "correct" way to
|
|
|
|
-- scale can change. For example, upscaling a pixel art image should
|
|
|
|
-- not make it blurry. However, scaling up a photo should not make it
|
|
|
|
-- blocky.
|
|
|
|
--
|
|
|
|
--<table class='widget_list' border=1>
|
|
|
|
-- <tr style='font-weight: bold;'>
|
|
|
|
-- <th align='center'>Value</th>
|
|
|
|
-- <th align='center'>Description</th>
|
|
|
|
-- </tr>
|
|
|
|
-- <tr><td>fast</td><td>A high-performance filter</td></tr>
|
|
|
|
-- <tr><td>good</td><td>A reasonable-performance filter</td></tr>
|
|
|
|
-- <tr><td>best</td><td>The highest-quality available</td></tr>
|
|
|
|
-- <tr><td>nearest</td><td>Nearest-neighbor filtering (blocky)</td></tr>
|
|
|
|
-- <tr><td>bilinear</td><td>Linear interpolation in two dimensions</td></tr>
|
|
|
|
--</table>
|
|
|
|
--
|
2021-07-14 23:17:49 +02:00
|
|
|
-- The image used in the example below has a resolution of 32x22 and is
|
|
|
|
-- intentionally blocky to highlight the difference.
|
|
|
|
-- It is zoomed by a factor of 3.
|
2021-04-26 09:15:15 +02:00
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_imagebox_scaling_quality_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property scaling_quality
|
2021-07-14 23:17:49 +02:00
|
|
|
-- @tparam string scaling_quality Either `"fast"`, `"good"`, `"best"`,
|
|
|
|
-- `"nearest"` or `"bilinear"`.
|
2021-04-26 09:15:15 +02:00
|
|
|
-- @propemits true false
|
|
|
|
-- @see resize
|
|
|
|
-- @see horizontal_fit_policy
|
|
|
|
-- @see vertical_fit_policy
|
|
|
|
-- @see max_scaling_factor
|
|
|
|
|
|
|
|
local defaults = {
|
|
|
|
halign = "left",
|
|
|
|
valign = "top",
|
|
|
|
horizontal_fit_policy = "auto",
|
|
|
|
vertical_fit_policy = "auto",
|
|
|
|
max_scaling_factor = 0,
|
|
|
|
scaling_quality = "good"
|
|
|
|
}
|
|
|
|
|
|
|
|
local function get_default(prop, value)
|
|
|
|
if value == nil then return defaults[prop] end
|
|
|
|
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
|
|
|
for prop in pairs(defaults) do
|
|
|
|
imagebox["set_"..prop] = function(self, value)
|
|
|
|
if value == self._private[prop] then return end
|
|
|
|
|
|
|
|
self._private[prop] = get_default(prop, value)
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("property::"..prop, self._private[prop])
|
|
|
|
end
|
|
|
|
|
|
|
|
imagebox["get_"..prop] = function(self)
|
|
|
|
if self._private[prop] == nil then
|
|
|
|
return defaults[prop]
|
|
|
|
end
|
|
|
|
|
|
|
|
return self._private[prop]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-21 17:34:28 +01:00
|
|
|
--- Returns a new `wibox.widget.imagebox` instance.
|
2019-11-22 19:06:37 +01:00
|
|
|
--
|
|
|
|
-- This is the constructor of `wibox.widget.imagebox`. It creates a new
|
|
|
|
-- instance of imagebox widget.
|
|
|
|
--
|
|
|
|
-- Alternatively, the declarative layout syntax can handle
|
|
|
|
-- `wibox.widget.imagebox` instanciation.
|
|
|
|
--
|
|
|
|
-- The image can be a file, a cairo image surface, or an rsvg handle object
|
|
|
|
-- (see the [image property](#image)).
|
|
|
|
--
|
2019-11-21 17:34:28 +01:00
|
|
|
-- Any additional arguments will be passed to the clip shape function.
|
|
|
|
-- @tparam[opt] image image The image to display (may be `nil`).
|
|
|
|
-- @tparam[opt] boolean resize_allowed If `false`, the image will be
|
|
|
|
-- clipped, else it will be resized to fit into the available space.
|
|
|
|
-- @tparam[opt] function clip_shape A `gears.shape` compatible function.
|
|
|
|
-- @treturn wibox.widget.imagebox A new `wibox.widget.imagebox` widget instance.
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct wibox.widget.imagebox
|
2019-11-28 12:02:18 +01:00
|
|
|
local function new(image, resize_allowed, clip_shape, ...)
|
2016-05-26 23:39:27 +02:00
|
|
|
local ret = base.make_widget(nil, nil, {enable_properties = true})
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2017-03-08 21:18:33 +01:00
|
|
|
gtable.crush(ret, imagebox, true)
|
2021-09-11 00:45:49 +02:00
|
|
|
ret._private.resize = true
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-11-25 19:16:31 +01:00
|
|
|
if image then
|
|
|
|
ret:set_image(image)
|
|
|
|
end
|
2021-09-11 00:45:49 +02:00
|
|
|
|
2012-11-25 19:16:31 +01:00
|
|
|
if resize_allowed ~= nil then
|
2021-09-11 00:45:49 +02:00
|
|
|
ret.resize = resize_allowed
|
2012-11-25 19:16:31 +01:00
|
|
|
end
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
ret._private.clip_shape = clip_shape
|
2019-11-28 12:02:18 +01:00
|
|
|
ret._private.clip_args = {...}
|
2016-01-20 08:09:07 +01:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
function imagebox.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(imagebox, imagebox.mt)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|