Skip to content

Commit

Permalink
Merge pull request #2696 from alfunx/naughty-legacy-resize-on-update
Browse files Browse the repository at this point in the history
naughty: Resize notification when setting text
  • Loading branch information
Elv13 committed Feb 24, 2019
2 parents d119a9a + 04c9477 commit f049046
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
75 changes: 39 additions & 36 deletions lib/naughty/layout/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,6 @@ local function get_offset(s, position, idx, width, height)
return v
end

local escape_pattern = "[<>&]"
local escape_subs = { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }

-- Cache the markup
local function set_escaped_text(self)
local text = self.message or ""
local title = self.title and self.title .. "\n" or ""

local textbox = self.textbox

local function set_markup(pattern, replacements)
return textbox:set_markup_silently(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements)))
end

local function set_text()
textbox:set_text(string.format('%s %s', title, text))
end

-- Since the title cannot contain markup, it must be escaped first so that
-- it is not interpreted by Pango later.
title = title:gsub(escape_pattern, escape_subs)
-- Try to set the text while only interpreting <br>.
if not set_markup("<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango
if not set_markup(escape_pattern, escape_subs) then
-- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
if not pcall(set_text) then
textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>")
end
end
end
end

naughty.connect_signal("property::text" ,set_escaped_text)
naughty.connect_signal("property::title",set_escaped_text)


--- Re-arrange notifications according to their position and index - internal
--
Expand Down Expand Up @@ -242,6 +206,45 @@ local function update_size(notification)
end


local escape_pattern = "[<>&]"
local escape_subs = { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }

-- Cache the markup
local function set_escaped_text(self)
local text = self.message or ""
local title = self.title and self.title .. "\n" or ""

local textbox = self.textbox

local function set_markup(pattern, replacements)
return textbox:set_markup_silently(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements)))
end

local function set_text()
textbox:set_text(string.format('%s %s', title, text))
end

-- Since the title cannot contain markup, it must be escaped first so that
-- it is not interpreted by Pango later.
title = title:gsub(escape_pattern, escape_subs)
-- Try to set the text while only interpreting <br>.
if not set_markup("<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango
if not set_markup(escape_pattern, escape_subs) then
-- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
if not pcall(set_text) then
textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>")
end
end
end

if self.size_info then update_size(self) end
end

naughty.connect_signal("property::text" ,set_escaped_text)
naughty.connect_signal("property::title",set_escaped_text)


local function cleanup(self, _ --[[reason]], keep_visible)
-- It is not a legacy notification
if not self.box then return end
Expand Down
21 changes: 20 additions & 1 deletion tests/test-naughty-legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -735,13 +735,32 @@ table.insert(steps, function()
assert(not naughty.suspended)

-- Replace the text
assert(n.title == "foo")
assert(n.message == "bar")
assert(n.text == "bar")
assert(n.title == "foo")
local width, height = n.width, n.height
assert(width)
assert(height)
naughty.replace_text(n, "foobar", "baz")
assert(n.title == "foobar")
assert(n.message == "baz")
assert(n.text == "baz")
assert(n.width > width)
assert(n.height == height)
width, height = n.width, n.height
naughty.replace_text(n, "foo", "bar\nbaz")
assert(n.title == "foo")
assert(n.message == "bar\nbaz")
assert(n.text == "bar\nbaz")
assert(n.width < width)
assert(n.height > height)
width, height = n.width, n.height
naughty.replace_text(n, "foo", "bar")
assert(n.title == "foo")
assert(n.message == "bar")
assert(n.text == "bar")
assert(n.width == width)
assert(n.height < height)

-- Test the ID system
local id = n.id
Expand Down

0 comments on commit f049046

Please sign in to comment.