Quantcast

* Act properly with regards to tooltip metadata, refactored handling code a bit

James Whitehead II [02-04-09 - 11:29]
* Act properly with regards to tooltip metadata, refactored handling code a bit
Filename
NinjaPanel.lua
diff --git a/NinjaPanel.lua b/NinjaPanel.lua
index 2ec217c..bcc63ca 100644
--- a/NinjaPanel.lua
+++ b/NinjaPanel.lua
@@ -52,6 +52,7 @@ end)
 local SortWeightName
 local Button_OnEnter, Button_OnLeave
 local Button_OnDragStart, Button_OnDragStop, Button_OnUpdateDragging
+local Button_Tooltip_OnEnter, Button_Tooltip_OnLeave
 local Panel_UpdateLayout

 function NinjaPanel:SpawnPanel(name, position)
@@ -235,29 +236,9 @@ function NinjaPanel:UpdatePlugin(event, name, key, value, dataobj)
 		self:UpdateButtonWidth(button)
 	elseif key == "icon" then
 		button.icon:SetTexture(value)
-	elseif key == "tooltip" then
-		-- This means we have a custom frame to be displayed so we must handle the
-		-- showing/hiding and anchoring
-		button:SetScript("OnEnter", function(self)
-			value:ClearAllPoints()
-			value:SetPoint("TOPLEFT", button, "BOTTOMLEFT", 0, 0)
-			value:Show()
-		end)
-		button:SetScript("OnLeave", function(self)
-			value:Hide()
-		end)
-	elseif key == "OnTooltipShow" and type(value) == "function" then
-		button:SetScript("OnEnter", Button_OnEnter)
-		button:SetScript("OnLeave", Button_OnLeave)
-	elseif key == "OnEnter" and type(value) == "function" then
-		-- Set the OnEnter script
-		button:SetScript("OnEnter", value)
-	elseif key == "OnEnter" then
-		button:SetScript("OnEnter", Button_OnEnter)
-	elseif key == "OnLeave" and type(value) == "function" then
-		button:SetScript("OnLeave", value)
-	elseif key == "OnLeave" then
-		button:SetScript("OnEnter", Button_OnLeave)
+	elseif key == "tooltip" or key == "OnTooltipShow" or key == "OnEnter" or key == "OnLeave" then
+		-- Update the tooltip handers on the frame
+		self:UpdateTooltipHandlers(button, dataobj)
 	elseif key == "OnClick" then
 		button:SetScript("OnClick", value)
 	end
@@ -285,6 +266,30 @@ function NinjaPanel:UpdatePlugin(event, name, key, value, dataobj)
 	end
 end

+function NinjaPanel:UpdateTooltipHandlers(button, dataobj)
+	-- It’s possible that a source addon may provide more that one tooltip method.
+	-- The display addon should only use one of these (even if it support all
+	-- three in the spec). The generally preferred order is: tooltip >
+	-- OnEnter/OnLeave > OnTooltipShow.
+
+	-- Generally speaking, tooltip is not likely to be implemented along with
+	-- another render method. OnEnter may also provide (and use) OnTooltipShow, in
+	-- this case it’s usually preferred that the display simply set the OnEnter
+	-- handler directly to the frame, thus bypassing the display’s tooltip
+	-- handling code and never calling OnTooltipShow from the display.
+
+	if dataobj.tooltip then
+		button:SetScript("OnEnter", Button_Tooltip_OnEnter)
+		button:SetScript("OnLeave", Button_Tooltip_OnLeave)
+	elseif dataobj.OnEnter and dataobj.OnLeave then
+		button:SetScript("OnEnter", dataobj.OnEnter)
+		button:SetScript("OnLeave", dataobj.OnLeave)
+	elseif dataobj.OnTooltipShow then
+		button:SetScript("OnEnter", Button_OnEnter)
+		button:SetScript("OnLeave", Button_OnLeave)
+	end
+end
+
 function NinjaPanel:UpdatePanels()
 	-- Ensure the options table exists
 	db.panels = db.panels or {}
@@ -397,14 +402,7 @@ function Panel_UpdateLayout(self)
 			button.icon:Hide()
 		end

-		-- Attach the button scripts
-		if entry.object.OnEnter and not entry.object.OnTooltipShow then
-			button:SetScript("OnEnter", entry.object.OnEnter)
-			button:SetScript("OnLeave", entry.object.OnLeave)
-		else
-			button:SetScript("OnEnter", Button_OnEnter)
-			button:SetScript("OnLeave", Button_OnLeave)
-		end
+		self:UpdateTooltipHandlers(button, entry.object)

 		button:SetScript("OnClick", entry.object.OnClick)
 		button:SetScript("OnDragStart", Button_OnDragStart)
@@ -622,3 +620,15 @@ function Button_OnDragStop(self, button, ...)
 	Panel_UpdateLayout(p)
 end

+function Button_Tooltip_OnEnter(button)
+	local tooltip = button.object.tooltip
+	tooltip:ClearAllPoints()
+	tooltip:SetPoint("TOPLEFT", button, "BOTTOMLEFT", 0, 0)
+	tooltip:Show()
+end
+
+function Button_Tooltip_OnLeave(button)
+	local tooltip = button.object.tooltip
+	tooltip:Hide()
+end
+