Improve tooltip behavior (#723)

This commit is contained in:
boatbomber
2023-07-08 21:41:47 -07:00
committed by GitHub
parent 7154113c13
commit 623fa06d52
2 changed files with 68 additions and 18 deletions

View File

@@ -165,9 +165,11 @@ function Trigger:init()
self.id = HttpService:GenerateGUID(false)
self.ref = Roact.createRef()
self.mousePos = Vector2.zero
self.showingPopup = false
self.destroy = function()
self.props.context.removeTip(self.id)
self.showingPopup = false
end
end
@@ -180,31 +182,77 @@ function Trigger:willUnmount()
end
end
function Trigger:didUpdate(prevProps)
if prevProps.text ~= self.props.text then
-- Any existing popup is now invalid
self.props.context.removeTip(self.id)
self.showingPopup = false
-- Let the new text propagate
self:managePopup()
end
end
function Trigger:isHovering()
local rbx = self.ref.current
if rbx then
local pos = rbx.AbsolutePosition
local size = rbx.AbsoluteSize
local mousePos = self.mousePos
return
mousePos.X >= pos.X and mousePos.X <= pos.X + size.X
and mousePos.Y >= pos.Y and mousePos.Y <= pos.Y + size.Y
end
return false
end
function Trigger:managePopup()
if self:isHovering() then
if self.showingPopup or self.showDelayThread then
-- Don't duplicate popups
return
end
self.showDelayThread = task.delay(DELAY, function()
self.props.context.addTip(self.id, {
Text = self.props.text,
Position = self.mousePos,
Trigger = self.ref,
})
self.showDelayThread = nil
self.showingPopup = true
end)
else
if self.showDelayThread then
task.cancel(self.showDelayThread)
self.showDelayThread = nil
end
self.props.context.removeTip(self.id)
self.showingPopup = false
end
end
function Trigger:render()
local function recalculate(rbx)
local widget = rbx:FindFirstAncestorOfClass("DockWidgetPluginGui")
if not widget then return end
self.mousePos = widget:GetRelativeMousePosition()
self:managePopup()
end
return e("Frame", {
Size = UDim2.fromScale(1, 1),
BackgroundTransparency = 1,
ZIndex = self.props.zIndex or 100,
[Roact.Ref] = self.ref,
[Roact.Event.MouseMoved] = function(_rbx, x, y)
self.mousePos = Vector2.new(x, y)
end,
[Roact.Event.MouseEnter] = function()
self.showDelayThread = task.delay(DELAY, function()
self.props.context.addTip(self.id, {
Text = self.props.text,
Position = self.mousePos,
Trigger = self.ref,
})
end)
end,
[Roact.Event.MouseLeave] = function()
if self.showDelayThread then
task.cancel(self.showDelayThread)
end
self.props.context.removeTip(self.id)
end,
[Roact.Change.AbsolutePosition] = recalculate,
[Roact.Change.AbsoluteSize] = recalculate,
[Roact.Event.MouseMoved] = recalculate,
[Roact.Event.MouseLeave] = recalculate,
[Roact.Event.MouseEnter] = recalculate,
})
end