From: Sébastien Gross <seb•ɱɩɲʋʃ•awesome•ɑƬ•chezwam•ɖɵʈ•org>

Signed-off-by: Sébastien Gross <seb•ɱɩɲʋʃ•awesome•ɑƬ•chezwam•ɖɵʈ•org>
---
 lib/awful/init.lua.in    |    1 +
 lib/awful/tooltip.lua.in |  242 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 243 insertions(+), 0 deletions(-)
 create mode 100644 lib/awful/tooltip.lua.in

diff --git a/lib/awful/init.lua.in b/lib/awful/init.lua.in
index d5644ef..9954a0f 100644
--- a/lib/awful/init.lua.in
+++ b/lib/awful/init.lua.in
@@ -22,6 +22,7 @@ require("awful.key")
 require("awful.button")
 require("awful.wibox")
 require("awful.startup_notification")
+require("awful.tooltip")
 
 --- AWesome Functions very UsefuL
 module("awful")
diff --git a/lib/awful/tooltip.lua.in b/lib/awful/tooltip.lua.in
new file mode 100644
index 0000000..a6f7ab1
--- /dev/null
+++ b/lib/awful/tooltip.lua.in
@@ -0,0 +1,242 @@
+-------------------------------------------------------------------------
+-- @author Sébastien Gross &lt;seb•ɱɩɲʋʃ•awesome•ɑƬ•chezwam•ɖɵʈ•org&gt
+-- @copyright 2009 Sébastien Gross
+-- @release @AWESOME_VERSION@
+-------------------------------------------------------------------------
+
+-- Grab needed environment
+
+-- capi
+local mouse = mouse
+local widget = widget
+local wibox = wibox
+local screen = screen
+local timer = timer
+-- awful
+local a_placement = require("awful.placement")
+local beautiful = require("beautiful")
+--lua
+local setmetatable = setmetatable
+local ipairs = ipairs
+
+--- Tooltip usage
+-- @fied definition A tooltip is a small hint displayed when the mouse cursor
+--      hovers a specific item.
+-- @field application In awesome, a tooltip can be linked with almost any
+--      object having a -- <code>add_signal()</code> method.
+-- @field create <p>How to create a tooltip?<br/>
+--  <code>
+--  myclock = awful.widget.textclock({}, "%T", 1)<br/> 
+--  myclock_t = awful.tooltip({<br/>
+--      objects = { K },<br/>
+--      timer_function = function()<br/>
+--              return os.date("Today is %A %B %d %Y\nThe time is %T")<br/>
+--          end,<br/>
+--      })<br/>
+--  </code>
+-- </p>
+-- @field usage
+--  <p>How to add the same tooltip to many objects?<br/>
+--  <code>
+--      myclock_t:add_to_objects({ obj1, obj2 })<br/>
+--  </code>
+--  Now the same tooltip is attached to <code>K</code>, <code>obj1</code>,
+--     <code>obj2</code>.<br/>
+--  </p>
+--  <p>How to remove tooltip from many objects?<br/>
+--  <code>
+--      myclock_t:remove_from_objects({ obj1, obj2 })<br/>
+--  </code>
+--  Now the same tooltip is only attached to <code>K</code>.<br/>
+--  </p>
+-- @class table
+-- @name usage
+
+--- Tooltip module for awesome objects.
+module("awful.tooltip")
+
+local data = setmetatable({}, { __mode = 'k' })
+
+--- Tooltip object definition.
+-- @name awful.tooltip
+-- @field tt_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.tt_wibox)
+    a_placement.no_offscreen(self.tt_wibox)
+end
+
+-- Place the tooltip under the mouse.
+-- @param self A tooltip object.
+local function set_geometry(self)
+    local my_geo = self.tt_wibox:geometry()
+    -- calculate width / height
+    n_s = self.tt_wibox.widgets[1]:extents()
+    if my_geo.width ~= n_s.width or my_geo.height ~= n_s.height then
+        self.tt_wibox:geometry(n_s)
+        place(self)
+    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
+            -- make sure the tooltip is on the same screen as the mouse
+            self.tt_wibox.screen = mouse.screen
+            data[self].timer_function()
+            data[self].timer:start()
+        end
+    end
+    place(self)
+    self.tt_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.tt_wibox.visible = false
+end
+
+--- Change displayed text.
+-- @param self The tooltip object.
+-- @param text New tooltip text.
+local function set_text(self, text)
+    self.tt_wibox.widgets[1].text = '<span color="' .. data[self].fg 
+        .. '" font_desc="' .. data[self].font .. '">' .. text .. "</span>"
+end
+
+
+--- 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
+
+-- Load Default values.
+-- @param self A tooltip object.
+local function set_defaults(self)
+    self.tt_wibox.border_width = beautiful.tooltip_border_width or 
beautiful.border_width or 1
+    self.tt_wibox.border_color = beautiful.tooltip_border_color or 
beautiful.border_normal or "#ffcb60"
+    self.tt_wibox.opacity = beautiful.tooltip_opacity or 1
+    self.tt_wibox.bg = beautiful.tooltip_bg_color or beautiful.bg_focus or 
"#ffcb60"
+    data[self].fg = beautiful.tooltip_fg_color or beautiful.fg_focus or 
"#000000"
+    data[self].font = beautiful.tooltip_font or beautiful.font or "terminus 6"
+end
+
+--- Add tooltip to objects.
+-- @param self The tooltip.
+-- @param objects A list of objects.
+local function add_to_objects(self, objects)
+    for _, object in ipairs(objects) do
+        object:add_signal("mouse::enter", data[self].show)
+        object:add_signal("mouse::leave", data[self].hide)
+    end
+end
+
+--- Remove tooltip from objects.
+-- @param self The tooltip.
+-- @param objects A list of objects.
+local function remove_from_objects(self, objects)
+    for _, object in ipairs(objects) do
+        object:remove_signal("mouse::enter", data[self].show)
+        object:remove_signal("mouse::leave", data[self].hide)
+    end
+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/>
+-- <code>timer_function</code>: A function to dynamicaly change the tooltip
+--     text.<br/>
+-- <code>objects</code>: A list of objects linked to the tooltip.<br/>
+-- @return The created tooltip.
+-- @see add_to_objects
+-- @see set_timeout
+-- @see set_text
+local function new(args)
+    local self = {
+        tt_wibox =  wibox({ }),
+        visible = false,
+    }
+
+    local my_textbox = widget({
+        type = "textbox",
+        name = "tooltip_textbox",
+        align="right"})
+
+    -- private data
+    data[self] = {
+        show = function() show(self) end,
+        hide = function() hide(self) end
+    }
+
+    -- export functions
+    self.set_text = set_text
+    self.set_timeout = set_timeout
+    self.add_to_objects = add_to_objects
+    self.remove_from_objects = remove_from_objects
+
+    set_defaults(self)
+
+    -- 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()
+                self:set_text(args.timer_function())
+                set_geometry(self)
+            end
+        data[self].timer:add_signal("timeout", data[self].timer_function)
+    end
+
+    -- set tooltip properties
+    self.tt_wibox.visible = false
+    -- Who want a non ontop tooltip ?
+    self.tt_wibox.ontop = true
+    self.tt_wibox.widgets = { my_textbox }
+
+    -- add some signals on both the tooltip and widget
+    self.tt_wibox:add_signal("mouse::enter", function () hide(self) end)
+
+    -- Add tooltip to objects
+    if args.objects then
+        self:add_to_objects(args.objects)
+    end
+
+    return self
+end
+
+setmetatable(_M, { __call = function(_, ...) return new(...) end })
+
+-- vim: ft=lua:et:sw=4:ts=4:sts=4:enc=utf-8:tw=78
-- 
1.5.6.5


-- 
To unsubscribe, send mail to [email protected].

Reply via email to