Naughty: Port to new widget system

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-10-06 14:19:20 +02:00
parent ad2bc9d183
commit 52e678a8c7
1 changed files with 46 additions and 30 deletions

View File

@ -13,14 +13,12 @@ local pcall = pcall
local capi = { screen = screen, local capi = { screen = screen,
awesome = awesome, awesome = awesome,
dbus = dbus, dbus = dbus,
widget = widget, timer = timer,
wibox = wibox, oocairo = oocairo }
oocairo = oocairo,
timer = timer }
local button = require("awful.button") local button = require("awful.button")
local util = require("awful.util") local util = require("awful.util")
local bt = require("beautiful") local bt = require("beautiful")
local layout = require("awful.widget.layout") local wibox = require("wibox")
--- Notification library --- Notification library
module("naughty") module("naughty")
@ -352,13 +350,15 @@ function notify(args)
end end
-- create textbox -- create textbox
local textbox = capi.widget({ type = "textbox", align = "flex" }) local textbox = wibox.widget.textbox()
textbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die))) local marginbox = wibox.layout.margin()
layout.margins[textbox] = { right = margin, left = margin, bottom = margin, top = margin } marginbox:set_margins(margin)
marginbox:set_widget(textbox)
textbox.valign = "middle" textbox.valign = "middle"
local function setText(pattern, replacements) local function setText(pattern, replacements)
textbox.text = string.format('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text:gsub(pattern, replacements)) textbox:set_markup(string.format('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text:gsub(pattern, replacements)))
textbox:check()
end end
-- First try to set the text while only interpreting <br>. -- First try to set the text while only interpreting <br>.
@ -366,12 +366,14 @@ function notify(args)
if not pcall(setText, "<br.->", "\n") then if not pcall(setText, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango -- That failed, escape everything which might cause an error from pango
if not pcall(setText, "[<>&]", { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }) then if not pcall(setText, "[<>&]", { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }) then
textbox.text = "<i>&lt;Invalid markup, cannot display message&gt;</i>" textbox:set_markup("<i>&lt;Invalid markup, cannot display message&gt;</i>")
end end
end end
-- create iconbox -- create iconbox
local iconbox = nil local iconbox = nil
local iconmargin = nil
local icon_w, icon_h = 0, 0
if icon then if icon then
-- try to guess icon if the provided one is non-existent/readable -- try to guess icon if the provided one is non-existent/readable
if type(icon) == "string" and not util.file_readable(icon) then if type(icon) == "string" and not util.file_readable(icon) then
@ -380,25 +382,33 @@ function notify(args)
-- if we have an icon, use it -- if we have an icon, use it
if icon then if icon then
iconbox = capi.widget({ type = "imagebox", align = "left" }) iconbox = wibox.widget.imagebox()
layout.margins[iconbox] = { right = margin, left = margin, bottom = margin, top = margin } iconmargin = wibox.layout.margin()
iconbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die))) iconmargin:set_widget(iconbox)
local img iconmargin:set_margins(margin)
local img = icon
if type(icon) == "string" then if type(icon) == "string" then
img = capi.oocairo.image_surface_create_from_png(icon) img = capi.oocairo.image_surface_create_from_png(icon)
else else
img = icon img = icon
end end
if icon_size then if icon_size then
img = img:crop_and_scale(0,0,img.height,img.width,icon_size,icon_size) local scaled = capi.oocairo.image_surface_create("argb32", icon_size, icon_size)
local cr = capi.oocairo.context_create(scaled)
cr:scale(icon_size / img:get_height(), icon_size / img:get_width())
cr:set_source(img, 0, 0)
cr:paint()
img = scaled
end end
iconbox.resize = false iconbox:set_resize(false)
iconbox.image = img iconbox:set_image(img)
icon_w = img:get_width()
icon_h = img:get_height()
end end
end end
-- create container wibox -- create container wibox
notification.box = capi.wibox({ fg = fg, notification.box = wibox({ fg = fg,
bg = bg, bg = bg,
border_color = border_color, border_color = border_color,
border_width = border_width, border_width = border_width,
@ -408,16 +418,18 @@ function notify(args)
-- calculate the height -- calculate the height
if not height then if not height then
if iconbox and iconbox:extents().height + 2 * margin > textbox:extents().height + 2 * margin then local w, h = textbox:fit(-1, -1)
height = iconbox:extents().height + 2 * margin if iconbox and icon_h + 2 * margin > h + 2 * margin then
height = icon_h + 2 * margin
else else
height = textbox:extents().height + 2 * margin height = h + 2 * margin
end end
end end
-- calculate the width -- calculate the width
if not width then if not width then
width = textbox:extents().width + (iconbox and iconbox:extents().width + 2 * margin or 0) + 2 * margin local w, h = textbox:fit(-1, -1)
width = w + (iconbox and icon_w + 2 * margin or 0) + 2 * margin
end end
-- crop to workarea size if too big -- crop to workarea size if too big
@ -445,11 +457,15 @@ function notify(args)
notification.idx = offset.idx notification.idx = offset.idx
-- populate widgets -- populate widgets
if iconbox then local layout = wibox.layout.fixed.horizontal()
notification.box.widgets = { iconbox, textbox, ["layout"] = layout.horizontal.leftright } if iconmargin then
else layout:add(iconmargin)
notification.box.widgets = { textbox, ["layout"] = layout.horizontal.leftright }
end end
layout:add(marginbox)
notification.box:set_widget(layout)
-- Setup the mouse events
layout:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die)))
-- insert the notification to the table -- insert the notification to the table
table.insert(notifications[screen][notification.position], notification) table.insert(notifications[screen][notification.position], notification)