2009-09-09 14:57:27 +02:00
|
|
|
|
-------------------------------------------------------------------------
|
|
|
|
|
-- @author Sébastien Gross <seb•ɱɩɲʋʃ•awesome•ɑƬ•chezwam•ɖɵʈ•org>
|
|
|
|
|
-- @copyright 2009 Sébastien Gross
|
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
|
-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
local mouse = mouse
|
|
|
|
|
local screen = screen
|
2014-12-06 16:44:04 +01:00
|
|
|
|
local timer = require("gears.timer")
|
2012-06-16 21:12:26 +02:00
|
|
|
|
local wibox = require("wibox")
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local a_placement = require("awful.placement")
|
|
|
|
|
local beautiful = require("beautiful")
|
2010-10-07 11:11:48 +02:00
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
2014-03-16 13:25:01 +01:00
|
|
|
|
local background = require("wibox.widget.background")
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
|
local ipairs = ipairs
|
|
|
|
|
|
|
|
|
|
--- Tooltip module for awesome objects.
|
|
|
|
|
-- 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
|
2009-10-09 20:39:55 +02:00
|
|
|
|
-- object having a <code>connect_signal()</code> method and receiving
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- <code>mouse::enter</code> and <code>mouse::leave</code> signals.
|
|
|
|
|
-- <p>How to create a tooltip?<br/>
|
|
|
|
|
-- <code>
|
|
|
|
|
-- myclock = awful.widget.textclock({}, "%T", 1)<br/>
|
|
|
|
|
-- myclock_t = awful.tooltip({<br/>
|
2012-01-10 11:22:11 +01:00
|
|
|
|
-- objects = { myclock },<br/>
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- timer_function = function()<br/>
|
|
|
|
|
-- return os.date("Today is %A %B %d %Y\nThe time is %T")<br/>
|
|
|
|
|
-- end,<br/>
|
|
|
|
|
-- })<br/>
|
|
|
|
|
-- </code>
|
|
|
|
|
-- </p>
|
|
|
|
|
-- <p>How to add the same tooltip to several objects?<br/>
|
|
|
|
|
-- <code>
|
|
|
|
|
-- myclock_t:add_to_object(obj1)<br/>
|
|
|
|
|
-- myclock_t:add_to_object(obj2)<br/>
|
|
|
|
|
-- </code>
|
2012-01-10 11:22:11 +01:00
|
|
|
|
-- Now the same tooltip is attached to <code>myclock</code>, <code>obj1</code>,
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- <code>obj2</code>.<br/>
|
|
|
|
|
-- </p>
|
|
|
|
|
-- <p>How to remove tooltip from many objects?<br/>
|
|
|
|
|
-- <code>
|
|
|
|
|
-- myclock_t:remove_from_object(obj1)<br/>
|
|
|
|
|
-- myclock_t:remove_from_object(obj2)<br/>
|
|
|
|
|
-- </code>
|
2012-01-10 11:22:11 +01:00
|
|
|
|
-- Now the same tooltip is only attached to <code>myclock</code>.<br/>
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- </p>
|
2012-06-12 20:13:09 +02:00
|
|
|
|
-- awful.tooltip
|
|
|
|
|
local tooltip = { mt = {} }
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
local data = setmetatable({}, { __mode = 'k' })
|
|
|
|
|
|
|
|
|
|
--- Tooltip object definition.
|
|
|
|
|
-- @name tooltip
|
|
|
|
|
-- @field wibox The wibox displaying the tooltip.
|
|
|
|
|
-- @field visible True if tooltip is visible.
|
|
|
|
|
-- @class table
|
|
|
|
|
|
|
|
|
|
-- Tooltip private data.
|
|
|
|
|
-- @name awful.tooltip.data
|
|
|
|
|
-- @field fg tooltip foreground color.
|
|
|
|
|
-- @field font Tooltip font.
|
|
|
|
|
-- @field hide The hide() function.
|
|
|
|
|
-- @field show The show() function.
|
|
|
|
|
-- @field timer The text update timer.
|
|
|
|
|
-- @field timer_function The text update timer function.
|
|
|
|
|
|
|
|
|
|
-- Place to tooltip on th screen.
|
|
|
|
|
-- @param self A tooltip object.
|
|
|
|
|
local function place(self)
|
|
|
|
|
a_placement.under_mouse(self.wibox)
|
|
|
|
|
a_placement.no_offscreen(self.wibox)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Place the tooltip under the mouse.
|
|
|
|
|
-- @param self A tooltip object.
|
|
|
|
|
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)
|
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.
|
|
|
|
|
-- @param self The tooltip to show.
|
|
|
|
|
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.
|
|
|
|
|
-- @param self The tooltip to hide.
|
|
|
|
|
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.
|
|
|
|
|
-- @param self The tooltip object.
|
|
|
|
|
-- @param text New tooltip text.
|
|
|
|
|
local function set_text(self, text)
|
2014-03-16 13:25:01 +01:00
|
|
|
|
self.textbox:set_text(text)
|
2012-11-04 21:53:38 +01:00
|
|
|
|
set_geometry(self)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2014-03-30 23:35:35 +02:00
|
|
|
|
--- Change displayed text.
|
|
|
|
|
-- @param self The tooltip object.
|
|
|
|
|
-- @param text New tooltip text, including pango markup.
|
|
|
|
|
local function set_markup(self, text)
|
|
|
|
|
self.textbox:set_markup(text)
|
|
|
|
|
set_geometry(self)
|
|
|
|
|
end
|
|
|
|
|
|
2009-09-09 14:57:27 +02:00
|
|
|
|
--- Change the tooltip's update interval.
|
|
|
|
|
-- @param self A tooltip object.
|
|
|
|
|
-- @param timeout The timeout value.
|
|
|
|
|
local function set_timeout(self, timeout)
|
|
|
|
|
if data[self].timer then
|
|
|
|
|
data[self].timer.timeout = timeout
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Add tooltip to an object.
|
|
|
|
|
-- @param self The tooltip.
|
|
|
|
|
-- @param object An object.
|
|
|
|
|
local function add_to_object(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.
|
|
|
|
|
-- @param self The tooltip.
|
|
|
|
|
-- @param object An object.
|
|
|
|
|
local function remove_from_object(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.
|
|
|
|
|
-- @param args Arguments for tooltip creation may containt:<br/>
|
|
|
|
|
-- <code>timeout</code>: The timeout value for update_func.<br/>
|
2012-11-04 21:53:38 +01:00
|
|
|
|
-- <code>timer_function</code>: A function to dynamically change the tooltip
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- text.<br/>
|
|
|
|
|
-- <code>objects</code>: A list of objects linked to the tooltip.<br/>
|
|
|
|
|
-- @return The created tooltip.
|
|
|
|
|
-- @see add_to_object
|
|
|
|
|
-- @see set_timeout
|
|
|
|
|
-- @see set_text
|
2014-03-30 23:35:35 +02:00
|
|
|
|
-- @see set_markup
|
2009-09-09 14:57:27 +02:00
|
|
|
|
local function new(args)
|
|
|
|
|
local self = {
|
|
|
|
|
wibox = wibox({ }),
|
|
|
|
|
visible = false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-- private data
|
|
|
|
|
data[self] = {
|
|
|
|
|
show = function() show(self) end,
|
|
|
|
|
hide = function() hide(self) end
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-- export functions
|
|
|
|
|
self.set_text = set_text
|
2014-03-30 23:35:35 +02:00
|
|
|
|
self.set_markup = set_markup
|
2009-09-09 14:57:27 +02:00
|
|
|
|
self.set_timeout = set_timeout
|
|
|
|
|
self.add_to_object = add_to_object
|
|
|
|
|
self.remove_from_object = remove_from_object
|
|
|
|
|
|
|
|
|
|
-- 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)
|
2014-03-16 13:25:01 +01:00
|
|
|
|
self.wibox:set_widget(self.background)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- add some signals on both the tooltip and widget
|
2009-10-09 20:39:55 +02:00
|
|
|
|
self.wibox:connect_signal("mouse::enter", data[self].hide)
|
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(...)
|
|
|
|
|
return new(...)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return setmetatable(tooltip, tooltip.mt)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- vim: ft=lua:et:sw=4:ts=4:sts=4:enc=utf-8:tw=78
|