From 3592d33b6cacf99712dbf0e4f53103b7184efab7 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 13 Aug 2017 02:50:19 -0400 Subject: [PATCH] naughty: Add a title widget. It is a normal textbox with some extra boilerplate code. Having this in a separate widget allows the notification to be defined from rc.lua without a ton of beautiful options and connect_signal. --- lib/naughty/widget/init.lua | 1 + lib/naughty/widget/title.lua | 87 ++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 lib/naughty/widget/title.lua diff --git a/lib/naughty/widget/init.lua b/lib/naughty/widget/init.lua index b76f3cab..ff74b538 100644 --- a/lib/naughty/widget/init.lua +++ b/lib/naughty/widget/init.lua @@ -5,4 +5,5 @@ --------------------------------------------------------------------------- return { + title = require( "naughty.widget.title" ); } diff --git a/lib/naughty/widget/title.lua b/lib/naughty/widget/title.lua new file mode 100644 index 00000000..92bf4763 --- /dev/null +++ b/lib/naughty/widget/title.lua @@ -0,0 +1,87 @@ +---------------------------------------------------------------------------- +--- A notification title. +-- +-- This widget is a specialized `wibox.widget.textbox` with the following extra +-- features: +-- +-- * Honor the `beautiful` notification variables. +-- * React to the `naughty.notification` object title changes. +-- +--@DOC_wibox_nwidget_title_simple_EXAMPLE@ +-- +-- @author Emmanuel Lepage Vallee <elv1313@gmail.com> +-- @copyright 2017 Emmanuel Lepage Vallee +-- @widgetmod naughty.widget.title +-- @see wibox.widget.textbox +---------------------------------------------------------------------------- +local textbox = require("wibox.widget.textbox") +local gtable = require("gears.table") +local beautiful = require("beautiful") + +local title = {} + +local function markup(notif, wdg) + local ret = ""..(notif.title or "").."" + local fg = notif.fg or beautiful.notification_fg + + wdg:set_font(notif.font or beautiful.notification_font) + + if fg then + ret = "" .. ret .. "" + end + + return ret +end + +--- The attached notification. +-- @property notification +-- @tparam naughty.notification notification + +function title:set_notification(notif) + if self._private.notification == notif then return end + + if self._private.notification then + self._private.notification:disconnect_signal("poperty::message", + self._private.title_changed_callback) + self._private.notification:disconnect_signal("poperty::fg", + self._private.title_changed_callback) + end + + self:set_markup(markup(notif, self)) + + self._private.notification = notif + self._private.title_changed_callback() + + notif:connect_signal("poperty::title", self._private.title_changed_callback) + notif:connect_signal("poperty::fg" , self._private.title_changed_callback) +end + +--- Create a new naughty.widget.title. +-- @tparam table args +-- @tparam naughty.notification args.notification The notification. +-- @constructorfct naughty.widget.title + +local function new(args) + args = args or {} + local tb = textbox() + tb:set_wrap("word") + tb:set_font(beautiful.notification_font) + + gtable.crush(tb, title, true) + + function tb._private.title_changed_callback() + tb:set_markup(markup(tb._private.notification, tb)) + end + + if args.notification then + tb:set_notification(args.notification) + end + + return tb +end + +--@DOC_widget_COMMON@ + +--@DOC_object_COMMON@ + +return setmetatable(title, {__call = function(_, ...) return new(...) end})