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,
awesome = awesome,
dbus = dbus,
widget = widget,
wibox = wibox,
oocairo = oocairo,
timer = timer }
timer = timer,
oocairo = oocairo }
local button = require("awful.button")
local util = require("awful.util")
local bt = require("beautiful")
local layout = require("awful.widget.layout")
local wibox = require("wibox")
--- Notification library
module("naughty")
@ -352,13 +350,15 @@ function notify(args)
end
-- create textbox
local textbox = capi.widget({ type = "textbox", align = "flex" })
textbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die)))
layout.margins[textbox] = { right = margin, left = margin, bottom = margin, top = margin }
local textbox = wibox.widget.textbox()
local marginbox = wibox.layout.margin()
marginbox:set_margins(margin)
marginbox:set_widget(textbox)
textbox.valign = "middle"
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
-- First try to set the text while only interpreting <br>.
@ -366,12 +366,14 @@ function notify(args)
if not pcall(setText, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango
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
-- create iconbox
local iconbox = nil
local iconmargin = nil
local icon_w, icon_h = 0, 0
if icon then
-- try to guess icon if the provided one is non-existent/readable
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 icon then
iconbox = capi.widget({ type = "imagebox", align = "left" })
layout.margins[iconbox] = { right = margin, left = margin, bottom = margin, top = margin }
iconbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die)))
local img
iconbox = wibox.widget.imagebox()
iconmargin = wibox.layout.margin()
iconmargin:set_widget(iconbox)
iconmargin:set_margins(margin)
local img = icon
if type(icon) == "string" then
img = capi.oocairo.image_surface_create_from_png(icon)
else
img = icon
end
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
iconbox.resize = false
iconbox.image = img
iconbox:set_resize(false)
iconbox:set_image(img)
icon_w = img:get_width()
icon_h = img:get_height()
end
end
-- create container wibox
notification.box = capi.wibox({ fg = fg,
notification.box = wibox({ fg = fg,
bg = bg,
border_color = border_color,
border_width = border_width,
@ -408,16 +418,18 @@ function notify(args)
-- calculate the height
if not height then
if iconbox and iconbox:extents().height + 2 * margin > textbox:extents().height + 2 * margin then
height = iconbox:extents().height + 2 * margin
local w, h = textbox:fit(-1, -1)
if iconbox and icon_h + 2 * margin > h + 2 * margin then
height = icon_h + 2 * margin
else
height = textbox:extents().height + 2 * margin
height = h + 2 * margin
end
end
-- calculate the width
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
-- crop to workarea size if too big
@ -445,11 +457,15 @@ function notify(args)
notification.idx = offset.idx
-- populate widgets
if iconbox then
notification.box.widgets = { iconbox, textbox, ["layout"] = layout.horizontal.leftright }
else
notification.box.widgets = { textbox, ["layout"] = layout.horizontal.leftright }
local layout = wibox.layout.fixed.horizontal()
if iconmargin then
layout:add(iconmargin)
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
table.insert(notifications[screen][notification.position], notification)