From 5b5a25a57c603ba1aba83253c4e77fa6ba403bc7 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Tue, 12 Mar 2019 14:23:09 -0400 Subject: [PATCH] naughty: Add a background widget. There is some boilerplate code that make using the widget_template harder when using the raw `wibox.container.background`. This widget takes care of it. --- lib/naughty/container/background.lua | 99 ++++++++++++++++++++++++++++ lib/naughty/container/init.lua | 9 +++ lib/naughty/init.lua | 2 + 3 files changed, 110 insertions(+) create mode 100644 lib/naughty/container/background.lua create mode 100644 lib/naughty/container/init.lua diff --git a/lib/naughty/container/background.lua b/lib/naughty/container/background.lua new file mode 100644 index 00000000..5bc97fa0 --- /dev/null +++ b/lib/naughty/container/background.lua @@ -0,0 +1,99 @@ +---------------------------------------------------------------------------- +--- A notification background. +-- +-- This widget holds the boilerplate code associated with the notification +-- background. This includes the color and potentially some other styling +-- elements such as the shape and border. +-- +-- * Honor the `beautiful` notification variables. +-- * React to the `naughty.notification` changes. +-- +--@DOC_wibox_nwidget_background_simple_EXAMPLE@ +-- +-- Note that this widget is based on the `wibox.container.background`. This is +-- an implementation detail and may change in the future without prior notice. +-- +-- @author Emmanuel Lepage Vallee <elv1313@gmail.com> +-- @copyright 2019 Emmanuel Lepage Vallee +-- @containermod naughty.widget.background +-- @see wibox.container.background +---------------------------------------------------------------------------- +local wbg = require("wibox.container.background") +local gtable = require("gears.table") +local beautiful = require("beautiful") +local gshape = require("gears.shape") + +local background = {} + +local function update_background(notif, wdg) + local bg = notif.bg or beautiful.notification_bg + local bw = notif.border_width or beautiful.notification_border_width + local bc = notif.border_color or beautiful.notification_border_color + + -- Always fallback to the rectangle to make sure the border works + local shape = notif.shape or + beautiful.notification_shape or gshape.rectangle + + wdg:set_bg(bg) + wdg:set_shape(shape) -- otherwise there's no borders + wdg:set_shape_border_width(bw) + wdg:set_shape_border_color(bc) +end + +--- The attached notification. +-- @property notification +-- @tparam naughty.notification notification + +function background:set_notification(notif) + if self._private.notification == notif then return end + + if self._private.notification then + self._private.notification:disconnect_signal("poperty::bg", + self._private.background_changed_callback) + self._private.notification:disconnect_signal("poperty::border_width", + self._private.background_changed_callback) + self._private.notification:disconnect_signal("poperty::border_color", + self._private.background_changed_callback) + self._private.notification:disconnect_signal("poperty::shape", + self._private.background_changed_callback) + end + + update_background(notif, self) + + self._private.notification = notif + + notif:connect_signal("poperty::bg" , self._private.background_changed_callback) + notif:connect_signal("poperty::border_width", self._private.background_changed_callback) + notif:connect_signal("poperty::border_color", self._private.background_changed_callback) + notif:connect_signal("poperty::shape" , self._private.background_changed_callback) +end + +--- Create a new naughty.container.background. +-- @tparam table args +-- @tparam naughty.notification args.notification The notification. +-- @constructorfct naughty.container.background + +local function new(args) + args = args or {} + + local bg = wbg() + bg:set_border_strategy("inner") + + gtable.crush(bg, background, true) + + function bg._private.background_changed_callback() + update_background(bg._private.notification, bg) + end + + if args.notification then + bg:set_notification(args.notification) + end + + return bg +end + +--@DOC_widget_COMMON@ + +--@DOC_object_COMMON@ + +return setmetatable(background, {__call = function(_, ...) return new(...) end}) diff --git a/lib/naughty/container/init.lua b/lib/naughty/container/init.lua new file mode 100644 index 00000000..1e3af095 --- /dev/null +++ b/lib/naughty/container/init.lua @@ -0,0 +1,9 @@ +--------------------------------------------------------------------------- +-- @author Emmanuel Lepage Vallee <elv1313@gmail.com> +-- @copyright 2019 Emmanuel Lepage Vallee +-- @module naughty.container +--------------------------------------------------------------------------- + +return { + background = require( "naughty.container.background" ); +} diff --git a/lib/naughty/init.lua b/lib/naughty/init.lua index 4450538c..e94f366d 100644 --- a/lib/naughty/init.lua +++ b/lib/naughty/init.lua @@ -13,6 +13,8 @@ naughty.action = require("naughty.action") naughty.list = require("naughty.list") naughty.layout = require("naughty.layout") naughty.widget = require("naughty.widget") +naughty.container = require("naughty.container") +naughty.action = require("naughty.action") naughty.notification = require("naughty.notification") return naughty