diff options
Diffstat (limited to '.config/awesome/ui/statusbar/panel/widgets/imagebox.lua')
-rw-r--r-- | .config/awesome/ui/statusbar/panel/widgets/imagebox.lua | 144 |
1 files changed, 79 insertions, 65 deletions
diff --git a/.config/awesome/ui/statusbar/panel/widgets/imagebox.lua b/.config/awesome/ui/statusbar/panel/widgets/imagebox.lua index 8c6e8a5..5caadc5 100644 --- a/.config/awesome/ui/statusbar/panel/widgets/imagebox.lua +++ b/.config/awesome/ui/statusbar/panel/widgets/imagebox.lua @@ -24,13 +24,13 @@ -- @supermodule wibox.widget.base --------------------------------------------------------------------------- -local lgi = require("lgi") +local lgi = require "lgi" local cairo = lgi.cairo -local base = require("wibox.widget.base") -local surface = require("gears.surface") -local gtable = require("gears.table") -local gdebug = require("gears.debug") +local base = require "wibox.widget.base" +local gdebug = require "gears.debug" +local gtable = require "gears.table" +local surface = require "gears.surface" local setmetatable = setmetatable local type = type local math = math @@ -40,7 +40,9 @@ local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility -- Safe load for optional Rsvg module local Rsvg = nil do - local success, err = pcall(function() Rsvg = lgi.Rsvg end) + 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 @@ -48,14 +50,16 @@ end local imagebox = { mt = {} } -local rsvg_handle_cache = setmetatable({}, { __mode = 'k' }) +local rsvg_handle_cache = setmetatable({}, { __mode = "k" }) ---Load rsvg handle form image file -- @tparam string file Path to svg file. -- @return Rsvg handle -- @treturn table A table where cached data can be stored. local function load_rsvg_handle(file) - if not Rsvg then return end + if not Rsvg then + return + end local cache = (rsvg_handle_cache[file] or {})["handle"] @@ -65,7 +69,7 @@ local function load_rsvg_handle(file) local handle, err - if file:match("<[?]?xml") or file:match("<svg") then + if file:match "<[?]?xml" or file:match "<svg" then handle, err = Rsvg.Handle.new_from_data(file) else handle, err = Rsvg.Handle.new_from_file(file) @@ -81,7 +85,9 @@ end ---Apply cairo surface for given imagebox widget local function set_surface(ib, surf) local is_surf_valid = surf.width > 0 and surf.height > 0 - if not is_surf_valid then return false end + if not is_surf_valid then + return false + end ib._private.default = { width = surf.width, height = surf.height } ib._private.handle = nil @@ -93,7 +99,9 @@ end local function set_handle(ib, handle, cache) local dim = handle:get_dimensions() local is_handle_valid = dim.width > 0 and dim.height > 0 - if not is_handle_valid then return false end + if not is_handle_valid then + return false + end ib._private.default = { width = dim.width, height = dim.height } ib._private.handle = handle @@ -124,18 +132,15 @@ end -- 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 + if not self._private.handle then + return + end - local dpi = self._private.auto_dpi and - ctx.dpi or - self._private.dpi or - nil + 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_dpi = dpi and self._private.last_dpi ~= dpi - local need_style = self._private.handle.set_stylesheet and - self._private.stylesheet + local need_style = self._private.handle.set_stylesheet and self._private.stylesheet local old_size = self._private.default and self._private.default.width @@ -163,14 +168,16 @@ local function update_dpi(self, ctx) -- 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") + self:emit_signal "widget::redraw_needed" + self:emit_signal "widget::layout_changed" end end -- Draw an imagebox with the given cairo context in the given geometry. function imagebox:draw(ctx, cr, width, height) - if width == 0 or height == 0 or not self._private.default then return end + if width == 0 or height == 0 or not self._private.default then + return + end -- For valign = "top" and halign = "left" local translate = { @@ -186,15 +193,15 @@ function imagebox:draw(ctx, cr, width, height) -- That's for the "fit" policy. local aspects = { w = width / w, - h = height / h + h = height / h, } local policy = { w = self._private.horizontal_fit_policy or "auto", - h = self._private.vertical_fit_policy or "auto" + h = self._private.vertical_fit_policy or "auto", } - for _, aspect in ipairs {"w", "h"} do + for _, aspect in ipairs { "w", "h" } do 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 @@ -209,15 +216,15 @@ function imagebox:draw(ctx, cr, width, height) end if self._private.halign == "center" then - translate.x = math.floor((width - w*aspects.w)/2) + translate.x = math.floor((width - w * aspects.w) / 2) elseif self._private.halign == "right" then - translate.x = math.floor(width - (w*aspects.w)) + translate.x = math.floor(width - (w * aspects.w)) end if self._private.valign == "center" then - translate.y = math.floor((height - h*aspects.h)/2) + translate.y = math.floor((height - h * aspects.h) / 2) elseif self._private.valign == "bottom" then - translate.y = math.floor(height - (h*aspects.h)) + translate.y = math.floor(height - (h * aspects.h)) end cr:translate(translate.x, translate.y) @@ -226,25 +233,25 @@ function imagebox:draw(ctx, cr, width, height) 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 + 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))) + 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) + 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) + translate.y = math.floor((height - h) / 2) elseif self._private.valign == "bottom" then translate.y = math.floor(height - h) end @@ -274,7 +281,9 @@ end -- Fit the imagebox into the given geometry function imagebox:fit(ctx, width, height) - if not self._private.default then return 0, 0 end + if not self._private.default then + return 0, 0 + end update_dpi(self, ctx) @@ -349,11 +358,13 @@ function imagebox:set_image(image) self._private.default = nil end - if not setup_succeed then return false end + if not setup_succeed then + return false + end - self:emit_signal("widget::redraw_needed") - self:emit_signal("widget::layout_changed") - self:emit_signal("property::image") + self:emit_signal "widget::redraw_needed" + self:emit_signal "widget::layout_changed" + self:emit_signal "property::image" return true end @@ -383,8 +394,8 @@ end -- @see clip_shape function imagebox:set_clip_shape(clip_shape, ...) self._private.clip_shape = clip_shape - self._private.clip_args = {...} - self:emit_signal("widget::redraw_needed") + self._private.clip_args = { ... } + self:emit_signal "widget::redraw_needed" self:emit_signal("property::clip_shape", clip_shape) end @@ -468,14 +479,14 @@ end -- @propemits true false -- @see dpi -for _, prop in ipairs {"stylesheet", "dpi", "auto_dpi"} do +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 "widget::redraw_needed" + self:emit_signal "widget::layout_changed" self:emit_signal("property::" .. prop) end end @@ -490,12 +501,12 @@ function imagebox:set_resize(allowed) self:emit_signal("property::upscale", allowed) end - self:emit_signal("widget::redraw_needed") - self:emit_signal("widget::layout_changed") + self:emit_signal "widget::redraw_needed" + self:emit_signal "widget::layout_changed" self:emit_signal("property::resize", allowed) end -for _, prop in ipairs {"downscale", "upscale" } do +for _, prop in ipairs { "downscale", "upscale" } do imagebox["set_" .. prop] = function(self, allowed) self._private[prop] = allowed @@ -504,9 +515,9 @@ for _, prop in ipairs {"downscale", "upscale" } do 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) + self:emit_signal "widget::redraw_needed" + self:emit_signal "widget::layout_changed" + self:emit_signal("property::" .. prop, allowed) end end @@ -542,7 +553,6 @@ end -- @see horizontal_fit_policy -- @see resize - --- The vertical alignment. -- -- @DOC_wibox_widget_imagebox_valign_EXAMPLE@ @@ -627,30 +637,34 @@ end -- @see max_scaling_factor local defaults = { - halign = "left", - valign = "top", + halign = "left", + valign = "top", horizontal_fit_policy = "auto", - vertical_fit_policy = "auto", - max_scaling_factor = 0, - scaling_quality = "good" + 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 + 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 + 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]) + self:emit_signal "widget::redraw_needed" + self:emit_signal("property::" .. prop, self._private[prop]) end - imagebox["get_"..prop] = function(self) + imagebox["get_" .. prop] = function(self) if self._private[prop] == nil then return defaults[prop] end @@ -678,7 +692,7 @@ end -- @treturn wibox.widget.imagebox A new `wibox.widget.imagebox` widget instance. -- @constructorfct wibox.widget.imagebox local function new(image, resize_allowed, clip_shape, ...) - local ret = base.make_widget(nil, nil, {enable_properties = true}) + local ret = base.make_widget(nil, nil, { enable_properties = true }) gtable.crush(ret, imagebox, true) ret._private.resize = true @@ -692,7 +706,7 @@ local function new(image, resize_allowed, clip_shape, ...) end ret._private.clip_shape = clip_shape - ret._private.clip_args = {...} + ret._private.clip_args = { ... } return ret end |