forked from worron/redflat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip.lua
More file actions
141 lines (122 loc) · 4.96 KB
/
tooltip.lua
File metadata and controls
141 lines (122 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
-----------------------------------------------------------------------------------------------------------------------
-- RedFlat tooltip --
-----------------------------------------------------------------------------------------------------------------------
-- Slightly modded awful tooltip
-- style.margin were added
-- Proper placement on every text update
-----------------------------------------------------------------------------------------------------------------------
-- Some code was taken from
------ awful.tooltip v3.5.2
------ (c) 2009 Sébastien Gross
-----------------------------------------------------------------------------------------------------------------------
-- Grab environment
-----------------------------------------------------------------------------------------------------------------------
local setmetatable = setmetatable
local ipairs = ipairs
local unpack = unpack
local wibox = require("wibox")
local awful = require("awful")
local beautiful = require("beautiful")
local timer = require("gears.timer")
local redutil = require("redflat.util")
-- Initialize tables for module
-----------------------------------------------------------------------------------------------------------------------
local tooltip = { mt = {} }
-- Generate default theme vars
-----------------------------------------------------------------------------------------------------------------------
local function default_style()
local style = {
margin = { 5, 5, 3, 3 },
timeout = 1,
font = "Sans 12",
border_width = 2,
color = { border = "#404040", text = "#aaaaaa", wibox = "#202020" }
}
return redutil.table.merge(style, redutil.table.check(beautiful, "float.tooltip") or {})
end
-- Create a new tooltip
-----------------------------------------------------------------------------------------------------------------------
function tooltip.new(args, style)
-- Initialize vars
--------------------------------------------------------------------------------
local args = args or {}
local objects = args.objects or {}
local style = redutil.table.merge(default_style(), style or {})
-- Construct tooltip window with wibox and textbox
--------------------------------------------------------------------------------
local ttp = { wibox = wibox({ type = "tooltip" }) }
local tb = wibox.widget.textbox()
ttp.widget = tb
ttp.wibox:set_widget(wibox.container.margin(tb, unpack(style.margin)))
tb:set_font(style.font)
-- configure wibox properties
ttp.wibox.visible = false
ttp.wibox.ontop = true
ttp.wibox.border_width = style.border_width
ttp.wibox.border_color = style.color.border
ttp.wibox:set_bg(style.color.wibox)
ttp.wibox:set_fg(style.color.text)
-- Tooltip size configurator
--------------------------------------------------------------------------------
function ttp:set_geometry()
local geom = self.wibox:geometry()
local n_w, n_h = self.widget:get_preferred_size()
if geom.width ~= n_w or geom.height ~= n_h then
self.wibox:geometry({
width = n_w + style.margin[1] + style.margin[2],
height = n_h + style.margin[3] + style.margin[4]
})
end
end
-- Set timer to make delay before tooltip show
--------------------------------------------------------------------------------
local show_timer = timer({ timeout = style.timeout })
show_timer:connect_signal("timeout",
function()
ttp:set_geometry()
awful.placement.under_mouse(ttp.wibox)
awful.placement.no_offscreen(ttp.wibox)
ttp.wibox.visible = true
show_timer:stop()
end)
-- Tooltip metods
--------------------------------------------------------------------------------
function ttp.show()
if not show_timer.started then show_timer:start() end
end
function ttp.hide()
if show_timer.started then show_timer:stop() end
if ttp.wibox.visible then ttp.wibox.visible = false end
end
function ttp:set_text(text)
self.widget:set_text(text)
if self.wibox.visible then
self:set_geometry()
self.wibox.x = mouse.coords().x - self.wibox.width/2
awful.placement.no_offscreen(self.wibox)
end
end
function ttp:add_to_object(object)
object:connect_signal("mouse::enter", self.show)
object:connect_signal("mouse::leave", self.hide)
end
function ttp:remove_from_object(object)
object:disconnect_signal("mouse::enter", self.show)
object:disconnect_signal("mouse::leave", self.hide)
end
-- Add tooltip to objects
--------------------------------------------------------------------------------
if objects then
for _, object in ipairs(objects) do
ttp:add_to_object(object)
end
end
--------------------------------------------------------------------------------
return ttp
end
-- Config metatable to call tooltip module as function
-----------------------------------------------------------------------------------------------------------------------
function tooltip.mt:__call(...)
return tooltip.new(...)
end
return setmetatable(tooltip, tooltip.mt)