Add & use a dpi property on screen objects

Once upon a time, beautiful.xresources.get_dpi was added to query
Xft.dpi. That made sense since this queried an xresources property. Over
time, other, non-xresources-based ways to query DPI were added to this
function. Now, it makes no more sense to have this function here.

Also, recently it became possible to add new properties to C objects
from Lua code. Thus, we no longer need to have a get_dpi() function
somewhere, but can add s.dpi directly.

Thus, this commit adds s.dpi and makes everything use it. No functional
changes are intended.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2017-10-08 12:30:21 +02:00
parent 7e395e7bc0
commit a137655791
5 changed files with 41 additions and 9 deletions

View File

@ -504,6 +504,40 @@ function screen.object.get_selected_tag(s)
return screen.object.get_selected_tags(s)[1] return screen.object.get_selected_tags(s)[1]
end end
--- The number of pixels per inch of the screen.
-- @property dpi
-- @treturn number the DPI value.
local xft_dpi, fallback_dpi
function screen.object.get_dpi(s)
local mm_per_inch = 25.4
if s.data.dpi then
return s.data.dpi
end
-- Xft.dpi is explicit user configuration, so honor it
if not xft_dpi and awesome and awesome.xrdb_get_value then
xft_dpi = tonumber(awesome.xrdb_get_value("", "Xft.dpi")) or false
end
if xft_dpi then
return xft_dpi
end
-- We have no outputs, so guess based on the size of the root window.
if root and not fallback_dpi then
local _, h = root.size()
local _, hmm = root.size_mm()
fallback_dpi = hmm ~= 0 and h * mm_per_inch / hmm
end
return fallback_dpi or 96
end
function screen.object.set_dpi(s, dpi)
s.data.dpi = dpi
end
--- When the tag history changed. --- When the tag history changed.
-- @signal tag::history::update -- @signal tag::history::update

View File

@ -173,7 +173,7 @@ local function get_geometry(widget, screen, position)
local pos, s = position or "cc", screen or ascreen.focused() local pos, s = position or "cc", screen or ascreen.focused()
local margin = widget._calendar_margin or 0 local margin = widget._calendar_margin or 0
local wa = s.workarea local wa = s.workarea
local width, height = widget:fit({screen=s, dpi=beautiful.xresources.get_dpi(s)}, wa.width, wa.height) local width, height = widget:fit({screen=s, dpi=s.dpi}, wa.width, wa.height)
width = width < wa.width and width or wa.width width = width < wa.width and width or wa.width
height = height < wa.height and height or wa.height height = height < wa.height and height or wa.height

View File

@ -64,8 +64,6 @@ function xresources.get_current_theme()
end end
local dpi_per_screen = {}
local function get_screen(s) local function get_screen(s)
return s and screen[s] return s and screen[s]
end end
@ -75,8 +73,8 @@ end
-- @treturn number DPI value. -- @treturn number DPI value.
function xresources.get_dpi(s) function xresources.get_dpi(s)
s = get_screen(s) s = get_screen(s)
if dpi_per_screen[s] then if s then
return dpi_per_screen[s] return s.dpi
end end
if not xresources.dpi then if not xresources.dpi then
-- Might not be present when run under unit tests -- Might not be present when run under unit tests
@ -115,7 +113,7 @@ function xresources.set_dpi(dpi, s)
if not s then if not s then
xresources.dpi = dpi xresources.dpi = dpi
else else
dpi_per_screen[s] = dpi s.dpi = dpi
end end
end end

View File

@ -44,7 +44,7 @@ local function get_widget_context(self)
end end
local context = self._widget_context local context = self._widget_context
local dpi = beautiful.xresources.get_dpi(s) local dpi = s.dpi
if (not context) or context.screen ~= s or context.dpi ~= dpi then if (not context) or context.screen ~= s or context.dpi ~= dpi then
context = { context = {
screen = s, screen = s,

View File

@ -73,7 +73,7 @@ end
-- @treturn number The preferred width. -- @treturn number The preferred width.
-- @treturn number The preferred height. -- @treturn number The preferred height.
function textbox:get_preferred_size(s) function textbox:get_preferred_size(s)
return self:get_preferred_size_at_dpi(beautiful.xresources.get_dpi(s)) return self:get_preferred_size_at_dpi(screen[s].dpi)
end end
--- Get the preferred height of a textbox at a given width. --- Get the preferred height of a textbox at a given width.
@ -83,7 +83,7 @@ end
-- @tparam integer|screen s The screen on which the textbox will be displayed. -- @tparam integer|screen s The screen on which the textbox will be displayed.
-- @treturn number The needed height. -- @treturn number The needed height.
function textbox:get_height_for_width(width, s) function textbox:get_height_for_width(width, s)
return self:get_height_for_width_at_dpi(width, beautiful.xresources.get_dpi(s)) return self:get_height_for_width_at_dpi(width, screen[s].dpi)
end end
--- Get the preferred size of a textbox. --- Get the preferred size of a textbox.