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 timer = require("gears.timer")
|
2016-03-02 20:04:19 +01:00
|
|
|
|
local object = require("gears.object")
|
2015-02-14 20:31:53 +01:00
|
|
|
|
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")
|
2016-05-23 05:56:45 +02:00
|
|
|
|
local background = require("wibox.container.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
|
|
|
|
|
2016-03-02 20:00:44 +01:00
|
|
|
|
local instance_mt = {}
|
|
|
|
|
|
|
|
|
|
function instance_mt:__index(key)
|
|
|
|
|
if key == "wibox" then
|
|
|
|
|
local wb = wibox(self.wibox_properties)
|
|
|
|
|
wb:set_widget(self.marginbox)
|
2016-02-28 15:34:43 +01: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.
|
2016-03-02 20:00:44 +01:00
|
|
|
|
wb:buttons(abutton({}, 1, nil, self.hide))
|
|
|
|
|
rawset(self, "wibox", wb)
|
|
|
|
|
return wb
|
2016-02-28 15:34:43 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2009-09-09 14:57:27 +02:00
|
|
|
|
-- 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)
|
|
|
|
|
-- calculate width / height
|
2015-10-17 18:44:07 +02:00
|
|
|
|
local n_w, n_h = self.textbox:get_preferred_size(mouse.screen)
|
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
|
2016-03-02 20:00:44 +01:00
|
|
|
|
self.wibox:geometry({ width = n_w, height = n_h })
|
|
|
|
|
a_placement.next_to_mouse(self.wibox)
|
|
|
|
|
a_placement.no_offscreen(self.wibox, mouse.screen)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
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
|
2015-09-27 16:04:20 +02:00
|
|
|
|
if self.timer then
|
|
|
|
|
if not self.timer.started then
|
|
|
|
|
self.timer_function()
|
|
|
|
|
self.timer:start()
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2009-11-07 20:09:39 +01:00
|
|
|
|
set_geometry(self)
|
2016-03-02 20:00:44 +01:00
|
|
|
|
self.wibox.visible = true
|
2009-09-09 14:57:27 +02:00
|
|
|
|
self.visible = true
|
2016-03-02 20:04:19 +01:00
|
|
|
|
self:emit_signal("property::visible")
|
2009-09-09 14:57:27 +02:00
|
|
|
|
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
|
2015-09-27 16:04:20 +02:00
|
|
|
|
if self.timer then
|
|
|
|
|
if self.timer.started then
|
|
|
|
|
self.timer:stop()
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2016-03-02 20:00:44 +01:00
|
|
|
|
self.wibox.visible = false
|
2009-09-09 14:57:27 +02:00
|
|
|
|
self.visible = false
|
2016-03-02 20:04:19 +01:00
|
|
|
|
self:emit_signal("property::visible")
|
2009-09-09 14:57:27 +02:00
|
|
|
|
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)
|
2015-09-27 16:04:20 +02:00
|
|
|
|
if self.timer then
|
|
|
|
|
self.timer.timeout = timeout
|
2009-09-09 14:57:27 +02:00
|
|
|
|
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.
|
2016-03-02 20:04:19 +01:00
|
|
|
|
-- @tparam gears.object obj An object with `mouse::enter` and
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- `mouse::leave` signals.
|
2016-03-02 20:04:19 +01:00
|
|
|
|
tooltip.add_to_object = function(self, obj)
|
|
|
|
|
obj:connect_signal("mouse::enter", self.show)
|
|
|
|
|
obj:connect_signal("mouse::leave", 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.
|
2016-03-02 20:04:19 +01:00
|
|
|
|
-- @tparam gears.object obj An object with `mouse::enter` and
|
2015-07-30 20:29:07 +02:00
|
|
|
|
-- `mouse::leave` signals.
|
2016-03-02 20:04:19 +01:00
|
|
|
|
tooltip.remove_from_object = function(self, obj)
|
|
|
|
|
obj:disconnect_signal("mouse::enter", self.show)
|
|
|
|
|
obj:disconnect_signal("mouse::leave", self.hide)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Create a new tooltip and link it to a widget.
|
2016-03-02 20:04:19 +01:00
|
|
|
|
-- Tooltips emit `property::visible` when their visibility changes.
|
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)
|
2016-03-02 20:04:19 +01:00
|
|
|
|
local self = setmetatable(object(), instance_mt)
|
|
|
|
|
self:add_signal("property::visible")
|
|
|
|
|
self.visible = false
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- 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)
|
|
|
|
|
|
2015-09-27 16:04:20 +02:00
|
|
|
|
function self.show()
|
|
|
|
|
if not delay_timeout.started then
|
|
|
|
|
delay_timeout:start()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
function self.hide()
|
|
|
|
|
if delay_timeout.started then
|
|
|
|
|
delay_timeout:stop()
|
|
|
|
|
end
|
|
|
|
|
hide(self)
|
|
|
|
|
end
|
2015-07-29 18:55:22 +02:00
|
|
|
|
else
|
2015-09-27 16:04:20 +02:00
|
|
|
|
function self.show()
|
|
|
|
|
show(self)
|
|
|
|
|
end
|
|
|
|
|
function self.hide()
|
|
|
|
|
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
|
2015-09-27 16:04:20 +02:00
|
|
|
|
self.timer = timer { timeout = args.timeout and args.timeout or 1 }
|
|
|
|
|
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
|
2015-09-27 16:04:20 +02:00
|
|
|
|
self.timer:connect_signal("timeout", self.timer_function)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2014-03-30 23:31:38 +02:00
|
|
|
|
-- Set default properties
|
2016-02-28 15:34:43 +01:00
|
|
|
|
self.wibox_properties = {
|
|
|
|
|
visible = false,
|
|
|
|
|
ontop = true,
|
|
|
|
|
border_width = beautiful.tooltip_border_width or beautiful.border_width or 1,
|
|
|
|
|
border_color = beautiful.tooltip_border_color or beautiful.border_normal or "#ffcb60",
|
|
|
|
|
opacity = beautiful.tooltip_opacity or 1,
|
|
|
|
|
bg = beautiful.tooltip_bg_color or beautiful.bg_focus or "#ffcb60"
|
|
|
|
|
}
|
2014-03-30 23:31:38 +02:00
|
|
|
|
local fg = beautiful.tooltip_fg_color or beautiful.fg_focus or "#000000"
|
|
|
|
|
local font = beautiful.tooltip_font or beautiful.font or "terminus 6"
|
|
|
|
|
|
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)
|
2016-05-23 05:56:45 +02:00
|
|
|
|
self.marginbox = wibox.container.margin(self.background, m_lr, m_lr, m_tb, m_tb)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
|
|
|
|
|
-- Add tooltip to objects
|
|
|
|
|
if args.objects then
|
2016-03-02 20:04:19 +01:00
|
|
|
|
for _, obj in ipairs(args.objects) do
|
|
|
|
|
self:add_to_object(obj)
|
2009-09-09 14:57:27 +02:00
|
|
|
|
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-12-12 17:42:33 +01:00
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|