Merge pull request #2696 from alfunx/naughty-legacy-resize-on-update

naughty: Resize notification when setting text
This commit is contained in:
Emmanuel Lepage Vallée 2019-02-24 15:12:35 -05:00 committed by GitHub
commit f049046332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 37 deletions

View File

@ -126,42 +126,6 @@ local function get_offset(s, position, idx, width, height)
return v return v
end 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 --- Re-arrange notifications according to their position and index - internal
-- --
@ -242,6 +206,45 @@ local function update_size(notification)
end 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) local function cleanup(self, _ --[[reason]], keep_visible)
-- It is not a legacy notification -- It is not a legacy notification
if not self.box then return end if not self.box then return end

View File

@ -735,13 +735,32 @@ table.insert(steps, function()
assert(not naughty.suspended) assert(not naughty.suspended)
-- Replace the text -- Replace the text
assert(n.title == "foo")
assert(n.message == "bar") assert(n.message == "bar")
assert(n.text == "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") naughty.replace_text(n, "foobar", "baz")
assert(n.title == "foobar") assert(n.title == "foobar")
assert(n.message == "baz") assert(n.message == "baz")
assert(n.text == "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 -- Test the ID system
local id = n.id local id = n.id