From 6b972ffde0bfd395b247ea6baf8013160aa5e04d Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 6 Oct 2010 21:17:05 +0200 Subject: [PATCH] widgets: Add mouse::{enter,leave} signals Signed-off-by: Uli Schlachter --- lib/wibox/init.lua.in | 46 ++++++++++++++++++++++++++++++++++++ lib/wibox/widget/base.lua.in | 2 ++ 2 files changed, 48 insertions(+) diff --git a/lib/wibox/init.lua.in b/lib/wibox/init.lua.in index 78838ddc..ede40954 100644 --- a/lib/wibox/init.lua.in +++ b/lib/wibox/init.lua.in @@ -151,6 +151,48 @@ function geometry(box, ...) return box.drawin:geometry(...) end +local function emit_difference(name, list, skip) + local function in_table(table, val) + for k, v in pairs(table) do + if v == val then + return true + end + end + return false + end + + for k, v in pairs(list) do + if not in_table(skip, v) then + v:emit_signal(name) + end + end +end + +local function handle_leave(wibox) + emit_difference("mouse::leave", wibox._widgets_under_mouse, {}) + wibox._widgets_under_mouse = {} +end + +local function handle_motion(wibox, x, y) + if x < 0 or y < 0 or x > wibox.drawin.width or y > wibox.drawin.height then + return handle_leave(wibox) + end + + -- Build a plain list of all widgets on that point + local widgets_list = wibox:find_widgets(x, y) + local widgets = {} + for k, v in pairs(widgets_list) do + widgets[#widgets + 1] = v.widget + end + + -- First, "leave" all widgets that were left + emit_difference("mouse::leave", wibox._widgets_under_mouse, widgets) + -- Then enter some widgets + emit_difference("mouse::enter", widgets, wibox._widgets_under_mouse) + + wibox._widgets_under_mouse = widgets +end + local function setup_signals(wibox) local w = wibox.drawin @@ -198,6 +240,9 @@ local function setup_signals(wibox) end button_signal("button::press") button_signal("button::release") + + wibox:connect_signal("mouse::move", handle_motion) + wibox:connect_signal("mouse::leave", handle_leave) end local function new(args) @@ -240,6 +285,7 @@ local function new(args) -- Due to the metatable below, we need this trick ret.widget = { __fake_widget = true } ret._widget_geometries = {} + ret._widgets_under_mouse = {} -- Redirect all non-existing indexes to the "real" drawin setmetatable(ret, { diff --git a/lib/wibox/widget/base.lua.in b/lib/wibox/widget/base.lua.in index ed6ee203..54a59122 100644 --- a/lib/wibox/widget/base.lua.in +++ b/lib/wibox/widget/base.lua.in @@ -76,6 +76,8 @@ function make_widget(proxy) -- Mouse input, oh noes! ret:add_signal("button::press") ret:add_signal("button::release") + ret:add_signal("mouse::enter") + ret:add_signal("mouse::leave") -- No buttons yet ret.widget_buttons = {}