好的,所以您想要一个复杂的版本,将其包含的小部件裁剪为其大小并处理输入事件。以下是复杂且缓慢的版本:
local inner_widget = screen[1].mytaglist
local inner_width, inner_height = 200, 40
-- No idea how to pick a good width and height for the wibox.
local w = wibox{ x = 100, y = 100, width = 100, height = 20, visible = true }
local own_widget = wibox.widget.base.make_widget()
w:set_widget(own_widget)
local offset_x, offset_y = -20, 0
local own_context = { screen = screen[1], dpi = 92 } -- We have to invent something here... :-(
local hierarchy
hierarchy = wibox.hierarchy.new(own_context, inner_widget, inner_width, inner_height, function()
own_widget:emit_signal("widget::redraw_needed")
end, function()
hierarchy:update(own_context, inner_widget, inner_width, inner_height)
own_widget:emit_signal("widget::redraw_needed")
end, nil)
function own_widget:draw(context, cr, width, height)
-- This does the scrolling
cr:translate(offset_x, offset_y)
-- Then just draw the inner stuff directly
hierarchy:draw(own_context, cr)
end
-- Start a timer to simulate scrolling: Once per second we move things slightly
gears.timer.start_new(1, function()
offset_x = - offset_x
own_widget:emit_signal("widget::redraw_needed")
return true
end)
-- Finally, make input events work
local function button_signal(name)
-- This function is basically copy&paste from find_widgets() in
-- wibox.drawable
local function traverse_hierarchy_tree(h, x, y, ...)
local m = h:get_matrix_from_device()
-- Is (x,y) inside of this hierarchy or any child (aka the draw extents)?
-- If not, we can stop searching.
local x1, y1 = m:transform_point(x, y)
local x2, y2, w2, h2 = h:get_draw_extents()
if x1 < x2 or x1 >= x2 + w2 then
return
end
if y1 < y2 or y1 >= y2 + h2 then
return
end
-- Is (x,y) inside of this widget?
-- If yes, we have to emit the signal on the widget.
local width, height = h:get_size()
if x1 >= 0 and y1 >= 0 and x1 <= width and y1 <= height then
h:get_widget():emit_signal(name, x1, y1, ...)
end
-- Continue searching in all children.
for _, child in ipairs(h:get_children()) do
traverse_hierarchy_tree(child, x, y, ...)
end
end
own_widget:connect_signal(name, function(_, x, y, ...)
-- Translate to "local" coordinates
x = x - offset_x
y = y - offset_y
-- Figure out which widgets were hit and emit the signal on them
traverse_hierarchy_tree(hierarchy, x, y, ...)
end)
end
button_signal("button::press")
button_signal("button::release")
而不是让 AwesomeWM 处理所有事情并只是放置另一个小部件:layout
,这段代码本身做了更多的事情。也就是说,它直接管理一个小部件树(在 AwesomeWM 中称为“层次结构”)并自行绘制。然后,此代码的一半负责处理按钮事件:当按钮事件进入时,此代码将其转发到正确的小部件,同时考虑当前的滚动。
请注意,每当发生任何变化时,这都会重新绘制此自定义小部件显示的所有内容。我想对于你的滚动问题来说,无论如何这是必要的,因为当你滚动一点时“一切都会改变”。然而,当 AwesomeWM 自己绘制一些小部件时,它会尝试仅重绘实际更改的部分。例如,如果时钟因为时间改变而更新,则仅重绘时钟。这里的代码总是重绘所有内容。
(是的,我知道这段代码非常糟糕,但它应该可以帮助您找出必要的成分。)