Make it possible to change tooltip text
This commit is contained in:
parent
059572956e
commit
0a5f240ab1
96
tooltip.lua
96
tooltip.lua
|
@ -2,6 +2,7 @@ local setmetatable,math = setmetatable,math
|
|||
local beautiful = require( "beautiful" )
|
||||
local wibox = require( "wibox" )
|
||||
local cairo = require( "lgi" ).cairo
|
||||
local object = require( "radical.object" )
|
||||
local capi = { screen = screen ,
|
||||
mouse = mouse }
|
||||
local module={}
|
||||
|
@ -36,31 +37,12 @@ local function rel_parent(w,args2,args)
|
|||
return {}
|
||||
end
|
||||
|
||||
local function new(widget,text, args)
|
||||
local args,data = args or {},{}
|
||||
local function init(data,widget,args)
|
||||
if not data.init then
|
||||
data.init = true
|
||||
|
||||
data.text = text
|
||||
|
||||
local function hide_tooltip()
|
||||
if data.wibox then
|
||||
data.wibox.visible = false
|
||||
if data.drawable then
|
||||
data.drawable:disconnect_signal("mouse::leave",hide_tooltip)
|
||||
data.drawable = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function data:hide() hide_tooltip() end
|
||||
|
||||
function data:showToolTip(show,args2)
|
||||
local args2 = args2 or args or {}
|
||||
args.direction = args.direction or get_direction(args2)
|
||||
if not data.wibox then
|
||||
local vertical,textw = (args.direction == "left") or (args.direction == "right"),wibox.widget.textbox()
|
||||
textw.align = "center"
|
||||
textw:set_markup("<b>".. data.text .."</b>")
|
||||
local w,extents = wibox({position="free"}),textw._layout:get_pixel_extents()
|
||||
local vertical = (args.direction == "left") or (args.direction == "right")
|
||||
local w,extents = data.wibox or wibox({position="free"}),widget._layout:get_pixel_extents()
|
||||
extents.width = extents.width + 60
|
||||
w.visible = false
|
||||
w.width = extents.width
|
||||
|
@ -94,13 +76,8 @@ local function new(widget,text, args)
|
|||
end
|
||||
cr:rectangle(20-((vertical) and 5 or 0),vertical and 0 or 5, extents.width-40+((vertical) and 14 or 0 ), 20)
|
||||
cr:fill()
|
||||
local l,m = wibox.layout.fixed.horizontal(),wibox.layout.margin(textw)
|
||||
m:set_left ( 30 )
|
||||
m:set_right ( 10 )
|
||||
m:set_bottom ( not vertical and ((args.direction == "top") and 4 or -4) or 0 )
|
||||
l:add(m)
|
||||
l:fill_space(true)
|
||||
w:set_widget(l)
|
||||
|
||||
|
||||
w:set_fg(beautiful.fg_normal)
|
||||
|
||||
if args.direction == "left" or args.direction == "top" then --Mirror
|
||||
|
@ -116,10 +93,63 @@ local function new(widget,text, args)
|
|||
end
|
||||
w.shape_bounding = img._native
|
||||
|
||||
w:connect_signal("mouse::leave",hide_tooltip)
|
||||
data.wibox = w
|
||||
end
|
||||
end
|
||||
|
||||
local function set_text(self,text)
|
||||
self.init = nil
|
||||
self._text = text
|
||||
|
||||
textw:set_markup("<b>".. data._text .."</b>")
|
||||
init(data,data._w,dara._args)
|
||||
end
|
||||
|
||||
local function new(widget,text, args)
|
||||
local args,data = args or {},object({
|
||||
private_data = {
|
||||
},
|
||||
autogen_getmap = true,
|
||||
autogen_setmap = true,
|
||||
autogen_signals = true,
|
||||
})
|
||||
|
||||
data._text = text
|
||||
|
||||
local function hide_tooltip()
|
||||
if data.wibox then
|
||||
data.wibox.visible = false
|
||||
if data.drawable then
|
||||
data.drawable:disconnect_signal("mouse::leave",hide_tooltip)
|
||||
data.drawable = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function data:hide() hide_tooltip() end
|
||||
|
||||
function data:showToolTip(show,args2)
|
||||
local args2 = args2 or args or {}
|
||||
args.direction = args.direction or get_direction(args2)
|
||||
|
||||
local vertical,textw = (args.direction == "left") or (args.direction == "right"),wibox.widget.textbox()
|
||||
textw.align = "center"
|
||||
textw:set_markup("<b>".. data._text .."</b>")
|
||||
data._w = textw
|
||||
init(data,textw,args)
|
||||
|
||||
if data.wibox then
|
||||
|
||||
local l,m = wibox.layout.fixed.horizontal(),wibox.layout.margin(textw)
|
||||
m:set_left ( 30 )
|
||||
m:set_right ( 10 )
|
||||
m:set_bottom ( not vertical and ((args.direction == "top") and 4 or -4) or 0 )
|
||||
l:add(m)
|
||||
|
||||
l:fill_space(true)
|
||||
data.wibox:set_widget(l)
|
||||
|
||||
data.wibox:connect_signal("mouse::leave",hide_tooltip)
|
||||
local relative_to_parent = rel_parent(data.wibox,args2,args)
|
||||
data.wibox.x = args2.x or args.x or relative_to_parent.x or capi.mouse.coords().x - data.wibox.width/2 -5
|
||||
data.wibox.y = args2.y or args.y or relative_to_parent.y or ((not vertical) and capi.screen[capi.mouse.screen].geometry.height - 16 - 25 or 16)
|
||||
|
@ -133,6 +163,8 @@ local function new(widget,text, args)
|
|||
widget:connect_signal("mouse::enter" , function(widget,geometry) data:showToolTip( true , {parent=geometry}) end)
|
||||
widget:connect_signal("mouse::leave" , hide_tooltip)
|
||||
widget:connect_signal("button::press" , hide_tooltip)
|
||||
data.set_text = set_text
|
||||
data._args = args
|
||||
return data
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue