textbox: Honor per-screen DPI
This makes the textbox' :draw() and :fit() callbacks use the DPI that is specified in the given drawing context. With this, the textbox now scales correctly if different screens have different DPI values. Idea originally from Daniel. Closes https://github.com/awesomeWM/awesome/pull/457. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
b0d7e6bb6c
commit
8d6030819b
|
@ -20,16 +20,21 @@ local error = error
|
||||||
local textbox = { mt = {} }
|
local textbox = { mt = {} }
|
||||||
|
|
||||||
--- Setup a pango layout for the given textbox and cairo context
|
--- Setup a pango layout for the given textbox and cairo context
|
||||||
local function setup_layout(box, width, height)
|
local function setup_layout(box, width, height, dpi)
|
||||||
local layout = box._layout
|
local layout = box._layout
|
||||||
layout.width = Pango.units_from_double(width)
|
layout.width = Pango.units_from_double(width)
|
||||||
layout.height = Pango.units_from_double(height)
|
layout.height = Pango.units_from_double(height)
|
||||||
|
if box.dpi ~= dpi then
|
||||||
|
box.dpi = dpi
|
||||||
|
box._ctx:set_resolution(dpi)
|
||||||
|
box._layout:context_changed()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Draw the given textbox on the given cairo context in the given geometry
|
--- Draw the given textbox on the given cairo context in the given geometry
|
||||||
function textbox:draw(context, cr, width, height)
|
function textbox:draw(context, cr, width, height)
|
||||||
|
setup_layout(self, width, height, context.dpi)
|
||||||
cr:update_layout(self._layout)
|
cr:update_layout(self._layout)
|
||||||
setup_layout(self, width, height)
|
|
||||||
local ink, logical = self._layout:get_pixel_extents()
|
local ink, logical = self._layout:get_pixel_extents()
|
||||||
local offset = 0
|
local offset = 0
|
||||||
if self._valign == "center" then
|
if self._valign == "center" then
|
||||||
|
@ -43,7 +48,7 @@ end
|
||||||
|
|
||||||
--- Fit the given textbox
|
--- Fit the given textbox
|
||||||
function textbox:fit(context, width, height)
|
function textbox:fit(context, width, height)
|
||||||
setup_layout(self, width, height)
|
setup_layout(self, width, height, context.dpi)
|
||||||
local ink, logical = self._layout:get_pixel_extents()
|
local ink, logical = self._layout:get_pixel_extents()
|
||||||
|
|
||||||
if logical.width == 0 or logical.height == 0 then
|
if logical.width == 0 or logical.height == 0 then
|
||||||
|
@ -161,9 +166,9 @@ local function new(text, ignore_markup)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local ctx = PangoCairo.font_map_get_default():create_context()
|
ret._dpi = -1
|
||||||
ctx:set_resolution(beautiful.xresources.get_dpi())
|
ret._ctx = PangoCairo.font_map_get_default():create_context()
|
||||||
ret._layout = Pango.Layout.new(ctx)
|
ret._layout = Pango.Layout.new(ret._ctx)
|
||||||
|
|
||||||
ret:set_ellipsize("end")
|
ret:set_ellipsize("end")
|
||||||
ret:set_wrap("word_char")
|
ret:set_wrap("word_char")
|
||||||
|
|
Loading…
Reference in New Issue