2014-05-20 13:02:39 +02:00
|
|
|
|
-------------------------------------------------------------------------
|
2009-09-09 14:57:27 +02:00
|
|
|
|
--- Tooltip module for awesome objects.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
--
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- A tooltip is a small hint displayed when the mouse cursor
|
|
|
|
|
-- hovers a specific item.
|
|
|
|
|
-- In awesome, a tooltip can be linked with almost any
|
2014-05-20 13:02:39 +02:00
|
|
|
|
-- object having a `:connect_signal()` method and receiving
|
|
|
|
|
-- `mouse::enter` and `mouse::leave` signals.
|
|
|
|
|
--
|
|
|
|
|
-- How to create a tooltip?
|
|
|
|
|
-- ---
|
|
|
|
|
--
|
|
|
|
|
-- myclock = awful.widget.textclock({}, "%T", 1)
|
|
|
|
|
-- myclock_t = awful.tooltip({
|
|
|
|
|
-- objects = { myclock },
|
|
|
|
|
-- timer_function = function()
|
|
|
|
|
-- return os.date("Today is %A %B %d %Y\nThe time is %T")
|
|
|
|
|
-- end,
|
|
|
|
|
-- })
|
|
|
|
|
--
|
|
|
|
|
-- How to add the same tooltip to several objects?
|
|
|
|
|
-- ---
|
|
|
|
|
--
|
|
|
|
|
-- myclock_t:add_to_object(obj1)
|
|
|
|
|
-- myclock_t:add_to_object(obj2)
|
|
|
|
|
--
|
|
|
|
|
-- Now the same tooltip is attached to `myclock`, `obj1`, `obj2`.
|
|
|
|
|
--
|
|
|
|
|
-- How to remove tooltip from many objects?
|
|
|
|
|
-- ---
|
|
|
|
|
--
|
|
|
|
|
-- myclock_t:remove_from_object(obj1)
|
|
|
|
|
-- myclock_t:remove_from_object(obj2)
|
|
|
|
|
--
|
|
|
|
|
-- Now the same tooltip is only attached to `myclock`.
|
|
|
|
|
--
|
|
|
|
|
-- @author Sébastien Gross <seb•ɱɩɲʋʃ•awesome•ɑƬ•chezwam•ɖɵʈ•org>
|
2015-02-14 20:31:53 +01:00
|
|
|
|
-- @copyright 2009 Sébastien Gross
|
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
|
-- @module awful.tooltip
|
2015-02-14 20:31:53 +01:00
|
|
|
|
-------------------------------------------------------------------------
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
2015-02-14 20:31:53 +01:00
|
|
|
|
local mouse = mouse
|
|
|
|
|
local screen = screen
|
|
|
|
|
local timer = require("gears.timer")
|
|
|
|
|
local wibox = require("wibox")
|
|
|
|
|
local a_placement = require("awful.placement")
|
2015-07-29 20:59:43 +02:00
|
|
|
|
local abutton = require("awful.button")
|
2015-02-14 20:31:53 +01:00
|
|
|
|
local beautiful = require("beautiful")
|
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
|
|
|
|
local background = require("wibox.widget.background")
|
2015-07-15 18:47:20 +02:00
|
|
|
|
local dpi = require("beautiful").xresources.apply_dpi
|
2015-02-14 20:31:53 +01:00
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
|
local ipairs = ipairs
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
--- Tooltip object definition.
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @table tooltip
|
|
|
|
|
-- @tfield wibox wibox The wibox displaying the tooltip.
|
|
|
|
|
-- @tfield boolean visible True if tooltip is visible.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
local tooltip = { mt = {} }
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
2015-02-14 20:31:53 +01:00
|
|
|
|
--- Tooltip private data.
|
|
|
|
|
-- @local
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @table tooltip.data
|
|
|
|
|
-- @tfield string fg tooltip foreground color.
|
|
|
|
|
-- @tfield string font Tooltip font.
|
|
|
|
|
-- @tfield function hide The hide() function.
|
|
|
|
|
-- @tfield function show The show() function.
|
|
|
|
|
-- @tfield gears.timer timer The text update timer.
|
|
|
|
|
-- @tfield function timer_function The text update timer function.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
local data = setmetatable({}, { __mode = 'k' })
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
2015-02-14 20:31:53 +01:00
|
|
|
|
-- Place the tooltip on the screen.
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self A tooltip object.
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local function place(self)
|
2015-08-02 19:38:26 +02:00
|
|
|
|
a_placement.next_to_mouse(self.wibox)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Place the tooltip under the mouse.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self A tooltip object.
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local function set_geometry(self)
|
|
|
|
|
local my_geo = self.wibox:geometry()
|
|
|
|
|
-- calculate width / height
|
2014-03-16 13:25:01 +01:00
|
|
|
|
local n_w, n_h = self.textbox:fit(-1, -1)
|
2015-07-15 18:47:20 +02:00
|
|
|
|
n_w = n_w + self.marginbox.left + self.marginbox.right
|
|
|
|
|
n_h = n_h + self.marginbox.top + self.marginbox.bottom
|
2010-10-07 11:11:48 +02:00
|
|
|
|
if my_geo.width ~= n_w or my_geo.height ~= n_h then
|
|
|
|
|
self.wibox:geometry({ width = n_w, height = n_h })
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Show a tooltip.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self The tooltip to show.
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local function show(self)
|
|
|
|
|
-- do nothing if the tooltip is already shown
|
|
|
|
|
if self.visible then return end
|
|
|
|
|
if data[self].timer then
|
|
|
|
|
if not data[self].timer.started then
|
|
|
|
|
data[self].timer_function()
|
|
|
|
|
data[self].timer:start()
|
|
|
|
|
end
|
|
|
|
|
end
|
2009-11-07 20:09:39 +01:00
|
|
|
|
set_geometry(self)
|
2013-01-05 20:54:38 +01:00
|
|
|
|
place(self)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
self.wibox.visible = true
|
|
|
|
|
self.visible = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Hide a tooltip.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self The tooltip to hide.
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local function hide(self)
|
|
|
|
|
-- do nothing if the tooltip is already hidden
|
|
|
|
|
if not self.visible then return end
|
|
|
|
|
if data[self].timer then
|
|
|
|
|
if data[self].timer.started then
|
|
|
|
|
data[self].timer:stop()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
self.visible = false
|
|
|
|
|
self.wibox.visible = false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Change displayed text.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self The tooltip object.
|
|
|
|
|
-- @tparam string text New tooltip text, passed to
|
|
|
|
|
-- `wibox.widget.textbox.set_text`.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
tooltip.set_text = function(self, text)
|
2014-03-16 13:25:01 +01:00
|
|
|
|
self.textbox:set_text(text)
|
2015-07-29 18:50:02 +02:00
|
|
|
|
if self.visible then
|
|
|
|
|
set_geometry(self)
|
|
|
|
|
end
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-07-29 19:02:01 +02:00
|
|
|
|
--- Change displayed markup.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self The tooltip object.
|
|
|
|
|
-- @tparam string text New tooltip markup, passed to
|
|
|
|
|
-- `wibox.widget.textbox.set_markup`.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
tooltip.set_markup = function(self, text)
|
2014-03-30 23:35:35 +02:00
|
|
|
|
self.textbox:set_markup(text)
|
2015-07-29 18:50:02 +02:00
|
|
|
|
if self.visible then
|
|
|
|
|
set_geometry(self)
|
|
|
|
|
end
|
2014-03-30 23:35:35 +02:00
|
|
|
|
end
|
|
|
|
|
|
2009-09-09 14:57:27 +02:00
|
|
|
|
--- Change the tooltip's update interval.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self A tooltip object.
|
|
|
|
|
-- @tparam number timeout The timeout value.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
tooltip.set_timeout = function(self, timeout)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
if data[self].timer then
|
|
|
|
|
data[self].timer.timeout = timeout
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Add tooltip to an object.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self The tooltip.
|
|
|
|
|
-- @tparam gears.object object An object with `mouse::enter` and
|
|
|
|
|
-- `mouse::leave` signals.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
tooltip.add_to_object = function(self, object)
|
2009-10-09 20:39:55 +02:00
|
|
|
|
object:connect_signal("mouse::enter", data[self].show)
|
|
|
|
|
object:connect_signal("mouse::leave", data[self].hide)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Remove tooltip from an object.
|
2014-05-20 13:02:39 +02:00
|
|
|
|
--
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam tooltip self The tooltip.
|
|
|
|
|
-- @tparam gears.object object An object with `mouse::enter` and
|
|
|
|
|
-- `mouse::leave` signals.
|
2015-02-14 20:31:53 +01:00
|
|
|
|
tooltip.remove_from_object = function(self, object)
|
2009-10-09 20:39:55 +02:00
|
|
|
|
object:disconnect_signal("mouse::enter", data[self].show)
|
|
|
|
|
object:disconnect_signal("mouse::leave", data[self].hide)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Create a new tooltip and link it to a widget.
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- @tparam table args Arguments for tooltip creation.
|
|
|
|
|
-- @tparam[opt=1] number args.timeout The timeout value for
|
|
|
|
|
-- `timer_function`.
|
|
|
|
|
-- @tparam function args.timer_function A function to dynamically set the
|
|
|
|
|
-- tooltip text. Its return value will be passed to
|
|
|
|
|
-- `wibox.widget.textbox.set_markup`.
|
|
|
|
|
-- @tparam[opt] table args.objects A list of objects linked to the tooltip.
|
|
|
|
|
-- @tparam[opt] number args.delay_show Delay showing the tooltip by this many
|
|
|
|
|
-- seconds.
|
|
|
|
|
-- @tparam[opt=apply_dpi(5)] integer args.margin_leftright The left/right margin for the text.
|
|
|
|
|
-- @tparam[opt=apply_dpi(3)] integer args.margin_topbottom The top/bottom margin for the text.
|
|
|
|
|
-- @treturn awful.tooltip The created tooltip.
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- @see add_to_object
|
|
|
|
|
-- @see set_timeout
|
|
|
|
|
-- @see set_text
|
2014-03-30 23:35:35 +02:00
|
|
|
|
-- @see set_markup
|
2015-02-14 20:31:53 +01:00
|
|
|
|
tooltip.new = function(args)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local self = {
|
|
|
|
|
wibox = wibox({ }),
|
|
|
|
|
visible = false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-- private data
|
2015-02-14 20:37:30 +01:00
|
|
|
|
if args.delay_show then
|
2015-07-29 18:55:22 +02:00
|
|
|
|
local delay_timeout
|
|
|
|
|
|
2015-02-14 20:37:30 +01:00
|
|
|
|
delay_timeout = timer { timeout = args.delay_show }
|
2015-07-29 18:55:22 +02:00
|
|
|
|
delay_timeout:connect_signal("timeout", function ()
|
|
|
|
|
show(self)
|
|
|
|
|
delay_timeout:stop()
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
data[self] = {
|
|
|
|
|
show = function()
|
|
|
|
|
if not delay_timeout.started then
|
|
|
|
|
delay_timeout:start()
|
|
|
|
|
end
|
|
|
|
|
end,
|
|
|
|
|
hide = function()
|
|
|
|
|
if delay_timeout.started then
|
|
|
|
|
delay_timeout:stop()
|
|
|
|
|
end
|
|
|
|
|
hide(self)
|
|
|
|
|
end,
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
data[self] = {
|
|
|
|
|
show = function() show(self) end,
|
|
|
|
|
hide = function() hide(self) end,
|
|
|
|
|
}
|
2015-02-14 20:37:30 +01:00
|
|
|
|
end
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- export functions
|
2015-02-14 20:31:53 +01:00
|
|
|
|
self.set_text = tooltip.set_text
|
|
|
|
|
self.set_markup = tooltip.set_markup
|
|
|
|
|
self.set_timeout = tooltip.set_timeout
|
|
|
|
|
self.add_to_object = tooltip.add_to_object
|
|
|
|
|
self.remove_from_object = tooltip.remove_from_object
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- setup the timer action only if needed
|
|
|
|
|
if args.timer_function then
|
|
|
|
|
data[self].timer = timer { timeout = args.timeout and args.timeout or 1 }
|
|
|
|
|
data[self].timer_function = function()
|
2014-03-30 23:35:35 +02:00
|
|
|
|
self:set_markup(args.timer_function())
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
2009-10-09 20:39:55 +02:00
|
|
|
|
data[self].timer:connect_signal("timeout", data[self].timer_function)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2014-03-30 23:31:38 +02:00
|
|
|
|
-- Set default properties
|
|
|
|
|
self.wibox.border_width = beautiful.tooltip_border_width or beautiful.border_width or 1
|
|
|
|
|
self.wibox.border_color = beautiful.tooltip_border_color or beautiful.border_normal or "#ffcb60"
|
|
|
|
|
self.wibox.opacity = beautiful.tooltip_opacity or 1
|
|
|
|
|
self.wibox:set_bg(beautiful.tooltip_bg_color or beautiful.bg_focus or "#ffcb60")
|
|
|
|
|
local fg = beautiful.tooltip_fg_color or beautiful.fg_focus or "#000000"
|
|
|
|
|
local font = beautiful.tooltip_font or beautiful.font or "terminus 6"
|
|
|
|
|
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- set tooltip properties
|
|
|
|
|
self.wibox.visible = false
|
2012-11-04 21:53:38 +01:00
|
|
|
|
-- Who wants a non ontop tooltip ?
|
2009-09-09 14:57:27 +02:00
|
|
|
|
self.wibox.ontop = true
|
2014-03-16 13:25:01 +01:00
|
|
|
|
self.textbox = textbox()
|
2014-03-30 23:31:38 +02:00
|
|
|
|
self.textbox:set_font(font)
|
2014-03-16 13:25:01 +01:00
|
|
|
|
self.background = background(self.textbox)
|
2014-03-30 23:31:38 +02:00
|
|
|
|
self.background:set_fg(fg)
|
2015-07-15 18:47:20 +02:00
|
|
|
|
|
|
|
|
|
-- Add margin.
|
2015-07-24 01:52:51 +02:00
|
|
|
|
local m_lr = args.margin_leftright or dpi(5)
|
|
|
|
|
local m_tb = args.margin_topbottom or dpi(3)
|
2015-07-15 18:47:20 +02:00
|
|
|
|
self.marginbox = wibox.layout.margin(self.background, m_lr, m_lr, m_tb, m_tb)
|
|
|
|
|
self.wibox:set_widget(self.marginbox)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
2015-07-29 20:59:43 +02:00
|
|
|
|
-- Close the tooltip when clicking it. This gets done on release, to not
|
|
|
|
|
-- emit the release event on an underlying object, e.g. the titlebar icon.
|
|
|
|
|
self.wibox:buttons(abutton({}, 1, nil, function() data[self].hide() end))
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- Add tooltip to objects
|
|
|
|
|
if args.objects then
|
|
|
|
|
for _, object in ipairs(args.objects) do
|
|
|
|
|
self:add_to_object(object)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
end
|
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
|
function tooltip.mt:__call(...)
|
2015-02-14 20:31:53 +01:00
|
|
|
|
return tooltip.new(...)
|
2012-06-12 20:13:09 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return setmetatable(tooltip, tooltip.mt)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
2015-02-09 22:54:54 +01:00
|
|
|
|
-- vim: ft=lua:et:sw=4:ts=4:sts=4:tw=78
|