diff --git a/bar.lua b/bar.lua index 76f4bca..b6896b1 100644 --- a/bar.lua +++ b/bar.lua @@ -60,7 +60,7 @@ local function setup_drawable(data) --Getters data.get_x = function() return 0 end data.get_y = function() return 0 end - data.get_width = function() return internal.layout.fit(internal.layout,9999,99) end + data.get_width = function() return internal.layout:get_preferred_size() end data.get_height = function() return beautiful.default_height end data.get_visible = function() return true end data.get_direction = function() return "left" end diff --git a/base.lua b/base.lua index d653015..8c8b8a7 100644 --- a/base.lua +++ b/base.lua @@ -239,7 +239,7 @@ local function add_widget(data,widget,args) data._internal.items[#data._internal.items+1] = item data:emit_signal("widget::added",item,widget) if data.visible then - local fit_w,fit_h = data._internal.layout:fit({dpi=96}, 9999,9999) + local fit_w,fit_h = data._internal.layout:get_preferred_size() data.width = data._internal.width or fit_w data.height = fit_h end @@ -264,7 +264,7 @@ local function get_widget_fit_sum(data) local h,w = 0,0 -- TODO query this from the layout itself for k,v in ipairs(data._internal.widgets) do - local fw,fh = v.widget:fit({dpi=96},9999,9999) + local fw,fh = v.widget:get_preferred_size() w,h = w + fw,h + fh end return w,h @@ -409,7 +409,7 @@ print(beautiful.menu_border_color) data.set_visible = function(_,value) private_data.visible = value if value then - local fit_w,fit_h = data._internal.layout:fit({dpi=96}, 9999,9999) + local fit_w,fit_h = data._internal.layout:get_preferred_size() data.width = internal.width or fit_w data.height = fit_h elseif data._tmp_menu and data._current_item then diff --git a/dock.lua b/dock.lua index 69d9967..78fb05c 100644 --- a/dock.lua +++ b/dock.lua @@ -165,7 +165,7 @@ local function adapt_size(data,w,h,screen) local max = get_max_size(data,screen) -- Get the current size, then compare and ajust - local fit_w,fit_h = data._internal.layout:fit({dpi=96},20,9999,true) + local fit_w,fit_h = data._internal.layout:get_preferred_size({dpi=96,force_values=true},beautiful.default_height) -- Get the number of items minus the number of widgets -- This can be used to approximate the number of pixel to remove @@ -325,13 +325,13 @@ local function setup_drawable(data) data.get_x = function() return 0 end data.get_y = function() return 0 end data.get_width = function() - return internal.w and internal.w.width or data._internal.layout:fit({dpi=96},9999,9999,true) + return internal.w and internal.w.width or data._internal.layout:get_preferred_size({force_values=true}) end data.get_height = function() if internal.orientation == "horizontal" then return beautiful.default_height else - local w,h = internal.layout.fit(internal.layout,{dpi=96},9999,9999) + local w,h = internal.layout:get_preferred_size() return h end end diff --git a/init.lua b/init.lua index 1557002..e981527 100644 --- a/init.lua +++ b/init.lua @@ -57,16 +57,35 @@ local function set_underlay(self,udl,args) self:emit_signal("widget::updated") end +local function get_preferred_size(self, context, width, height) + local context = context or 1 + + if type(context) == "number" then + context = {dpi=beautiful.xresources.get_dpi(context)} + elseif not context.dpi then + context.dpi = beautiful.xresources.get_dpi(1) + end + + return self:fit(context, width or 9999, height or 9999) +end + -- Do some monkey patching to extend all wibox.widget base._make_widget =base.make_widget base.make_widget = function(...) local ret = base._make_widget(...) - ret.set_tooltip = set_tooltip - ret.set_menu = set_menu - ret.set_underlay = set_underlay + ret.set_tooltip = set_tooltip + ret.set_menu = set_menu + ret.set_underlay = set_underlay + + -- Textboxes already have it + if not ret.get_preferred_size then + ret.get_preferred_size = get_preferred_size + end + return ret end + local bar = require( "radical.bar" ) return { diff --git a/layout/horizontal.lua b/layout/horizontal.lua index c4f9efb..40af97a 100644 --- a/layout/horizontal.lua +++ b/layout/horizontal.lua @@ -109,7 +109,7 @@ function module:setup_item(data,item,args) text_w:set_markup(value) end if data.auto_resize then - local fit_w,fit_h = text_w:fit({dpi=96},999,9999) + local fit_w,fit_h = text_w:get_preferred_size() local is_largest = item == data._internal.largest_item_h --TODO find new largest is item is smaller if not data._internal.largest_item_h_v or data._internal.largest_item_h_v < fit_h then @@ -145,7 +145,7 @@ local function new(data) end local l = wibox.layout.fixed.horizontal() l.fit = function(self,context,w,h,force_values) --TODO use the context instead of extra argument - local result,r2 = wibox.layout.fixed.fit(self,context,force_values and w or 99999,force_values and h or 99999) + local result,r2 = wibox.layout.fixed:get_preferred_size(self,context, force_values and w, force_values and h) local w,h if data.auto_resize and data._internal.largest_item_h then w,h = data.rowcount*(data.item_width or data.default_width),data._internal.largest_item_h_v > data.item_height and data._internal.largest_item_h_v or data.item_height diff --git a/layout/vertical.lua b/layout/vertical.lua index ab85cd2..5396bad 100644 --- a/layout/vertical.lua +++ b/layout/vertical.lua @@ -199,11 +199,11 @@ local function compute_geo(data,width,height,force_values) if data.auto_resize and data._internal.largest_item_w then w = data._internal.largest_item_w_v > data.default_width and data._internal.largest_item_w_v or data.default_width end - + local visblerow = data.visible_row_count - - local sw,sh = data._internal.suf_l:fit({dpi=96},9999,9999) - local pw,ph = data._internal.pref_l:fit({dpi=96},9999,9999) + + local sw,sh = data._internal.suf_l:get_preferred_size() + local pw,ph = data._internal.pref_l:get_preferred_size() if not data._internal.has_widget then return w,(total and total > 0 and total or visblerow*data.item_height) + ph + sh else