From 0214eebf36f0388d1f03261eba428cf1a42529b0 Mon Sep 17 00:00:00 2001 From: shagu Date: Sat, 7 May 2022 21:35:52 +0200 Subject: [PATCH] core: switch from mousedown to drag functions Due to crashes/freezes while moving some frames, one possible reason could be that pfUI was using OnMouseDown and OnMouseUp to run its frame-moving code. I have noticed better performance when using the intended functions, OnDragStart and OnDragStop to move frames. This commit changes every occurrence of such functions and splits drag and click into seperate functions where required. --- api/api.lua | 14 ++++++++------ api/ui-widgets.lua | 5 +++-- modules/addons.lua | 5 +++-- modules/bags.lua | 5 ++++- modules/chat.lua | 5 +++-- modules/firstrun.lua | 5 +++-- modules/gui.lua | 5 +++-- modules/map.lua | 11 ++++++----- modules/share.lua | 5 +++-- modules/unlock.lua | 46 ++++++++++++++++++++++---------------------- 10 files changed, 59 insertions(+), 47 deletions(-) diff --git a/api/api.lua b/api/api.lua index 8bacbc9b..2b2d25b2 100644 --- a/api/api.lua +++ b/api/api.lua @@ -526,8 +526,8 @@ end -- 'name' [frame/string] Name of the Frame that should be movable -- 'addon' [string] Addon that must be loaded before being able to access the frame -- 'blacklist' [table] A list of frames that should be deactivated for mouse usage -local function MoveMouseDown() this:StartMoving() end -local function MoveMouseUp() this:StopMovingOrSizing() end +local function OnDragStart() this:StartMoving() end +local function OnDragStop() this:StopMovingOrSizing() end function pfUI.api.EnableMovable(name, addon, blacklist) if addon then local scan = CreateFrame("Frame") @@ -544,8 +544,9 @@ function pfUI.api.EnableMovable(name, addon, blacklist) frame:SetMovable(true) frame:EnableMouse(true) - frame:SetScript("OnMouseDown", MoveMouseDown) - frame:SetScript("OnMouseUp", MoveMouseUp) + frame:RegisterForDrag("LeftButton") + frame:SetScript("OnDragStart", OnDragStart) + frame:SetScript("OnDragStop", OnDragStop) this:UnregisterAllEvents() end @@ -561,8 +562,9 @@ function pfUI.api.EnableMovable(name, addon, blacklist) if type(name) == "string" then frame = _G[name] end frame:SetMovable(true) frame:EnableMouse(true) - frame:SetScript("OnMouseDown", MoveMouseDown) - frame:SetScript("OnMouseUp", MoveMouseUp) + frame:RegisterForDrag("LeftButton") + frame:SetScript("OnDragStart", OnDragStart) + frame:SetScript("OnDragStop", OnDragStop) end end diff --git a/api/ui-widgets.lua b/api/ui-widgets.lua index bd8375be..48fbfac4 100644 --- a/api/ui-widgets.lua +++ b/api/ui-widgets.lua @@ -1042,11 +1042,12 @@ function pfUI.api.CreateQuestionDialog(text, yes, no, editbox, onclose) question:SetFrameStrata("TOOLTIP") question:SetMovable(true) question:EnableMouse(true) - question:SetScript("OnMouseDown",function() + question:RegisterForDrag("LeftButton") + question:SetScript("OnDragStart",function() this:StartMoving() end) - question:SetScript("OnMouseUp",function() + question:SetScript("OnDragStop",function() this:StopMovingOrSizing() end) diff --git a/modules/addons.lua b/modules/addons.lua index a08ed06f..220035c8 100644 --- a/modules/addons.lua +++ b/modules/addons.lua @@ -24,8 +24,9 @@ pfUI:RegisterModule("addons", "vanilla:tbc", function () pfUI.addons:EnableMouseWheel(1) pfUI.addons:SetMovable(true) pfUI.addons:EnableMouse(true) - pfUI.addons:SetScript("OnMouseDown", function() this:StartMoving() end) - pfUI.addons:SetScript("OnMouseUp", function() this:StopMovingOrSizing() end) + pfUI.addons:RegisterForDrag("LeftButton") + pfUI.addons:SetScript("OnDragStart", function() this:StartMoving() end) + pfUI.addons:SetScript("OnDragStop", function() this:StopMovingOrSizing() end) pfUI.addons:Hide() CreateBackdrop(pfUI.addons, nil, true, .75) diff --git a/modules/bags.lua b/modules/bags.lua index d837763b..f06b1c2c 100644 --- a/modules/bags.lua +++ b/modules/bags.lua @@ -225,7 +225,10 @@ pfUI:RegisterModule("bags", "vanilla:tbc", function () frame:EnableMouse(1) frame:SetMovable(1) frame:RegisterForDrag("LeftButton") - frame:SetScript("OnDragStart", function() this:StartMoving() end) + frame:SetScript("OnDragStart", function() + this:StartMoving() + end) + frame:SetScript("OnDragStop", function() this:StopMovingOrSizing() SaveMovable(this) diff --git a/modules/chat.lua b/modules/chat.lua index 1a34c95e..aa611333 100644 --- a/modules/chat.lua +++ b/modules/chat.lua @@ -175,11 +175,12 @@ pfUI:RegisterModule("chat", "vanilla:tbc", function () pfUI.chat.urlcopy:SetMovable(true) pfUI.chat.urlcopy:EnableMouse(true) - pfUI.chat.urlcopy:SetScript("OnMouseDown",function() + pfUI.chat.urlcopy:RegisterForDrag("LeftButton") + pfUI.chat.urlcopy:SetScript("OnDragStart",function() this:StartMoving() end) - pfUI.chat.urlcopy:SetScript("OnMouseUp",function() + pfUI.chat.urlcopy:SetScript("OnDragStop",function() this:StopMovingOrSizing() end) diff --git a/modules/firstrun.lua b/modules/firstrun.lua index 34decd8c..76ef4c3b 100644 --- a/modules/firstrun.lua +++ b/modules/firstrun.lua @@ -55,13 +55,14 @@ pfUI:RegisterModule("firstrun", "vanilla:tbc", function () f:SetFrameStrata("TOOLTIP") f:SetMovable(true) f:EnableMouse(true) + f:RegisterForDrag("LeftButton") f:SetWidth(380) f:SetHeight(180) - f:SetScript("OnMouseDown",function() + f:SetScript("OnDragStart",function() this:StartMoving() end) - f:SetScript("OnMouseUp",function() + f:SetScript("OnDragStop",function() this:StopMovingOrSizing() end) diff --git a/modules/gui.lua b/modules/gui.lua index 2484cf15..85ec6427 100644 --- a/modules/gui.lua +++ b/modules/gui.lua @@ -543,6 +543,7 @@ pfUI:RegisterModule("gui", "vanilla:tbc", function () pfUI.gui = CreateFrame("Frame", "pfConfigGUI", UIParent) pfUI.gui:SetMovable(true) pfUI.gui:EnableMouse(true) + pfUI.gui:RegisterForDrag("LeftButton") pfUI.gui:SetWidth(720) pfUI.gui:SetHeight(480) pfUI.gui:SetFrameStrata("DIALOG") @@ -575,11 +576,11 @@ pfUI:RegisterModule("gui", "vanilla:tbc", function () pfUI.gui:Hide() end) - pfUI.gui:SetScript("OnMouseDown",function() + pfUI.gui:SetScript("OnDragStart",function() this:StartMoving() end) - pfUI.gui:SetScript("OnMouseUp",function() + pfUI.gui:SetScript("OnDragStop",function() this:StopMovingOrSizing() end) diff --git a/modules/map.lua b/modules/map.lua index 828e8ebb..5d70435a 100644 --- a/modules/map.lua +++ b/modules/map.lua @@ -39,6 +39,10 @@ pfUI:RegisterModule("map", "vanilla:tbc", function () UIPanelWindows["WorldMapFrame"] = { area = "center" } + WorldMapFrame:SetMovable(true) + WorldMapFrame:EnableMouse(true) + WorldMapFrame:RegisterForDrag("LeftButton") + WorldMapFrame:SetScript("OnShow", function() -- default events UpdateMicroButtons() @@ -67,18 +71,15 @@ pfUI:RegisterModule("map", "vanilla:tbc", function () SaveMovable(this, true) end) - WorldMapFrame:SetScript("OnMouseDown",function() + WorldMapFrame:SetScript("OnDragStart",function() WorldMapFrame:StartMoving() end) - WorldMapFrame:SetScript("OnMouseUp",function() + WorldMapFrame:SetScript("OnDragStop",function() WorldMapFrame:StopMovingOrSizing() SaveMovable(this, true) end) - WorldMapFrame:SetMovable(true) - WorldMapFrame:EnableMouse(true) - WorldMapFrame:SetAlpha(alpha) WorldMapFrame:SetScale(scale) UpdateTooltipScale() diff --git a/modules/share.lua b/modules/share.lua index 4b6f055f..701601e8 100644 --- a/modules/share.lua +++ b/modules/share.lua @@ -253,8 +253,9 @@ pfUI:RegisterModule("share", "vanilla:tbc", function () f:SetHeight(420) f:SetMovable(true) f:EnableMouse(true) - f:SetScript("OnMouseDown", function() f:StartMoving() end) - f:SetScript("OnMouseUp", function() f:StopMovingOrSizing() end) + f:RegisterForDrag("LeftButton") + f:SetScript("OnDragStart", function() f:StartMoving() end) + f:SetScript("OnDragStop", function() f:StopMovingOrSizing() end) f:SetScript("OnShow", function() if pfUI.gui and pfUI.gui:IsShown() then diff --git a/modules/unlock.lua b/modules/unlock.lua index 142ffecc..fa84c82d 100644 --- a/modules/unlock.lua +++ b/modules/unlock.lua @@ -322,16 +322,7 @@ pfUI:RegisterModule("unlock", "vanilla:tbc", function () QueueFunction(UpdateDockValues) end - local function DraggerOnMouseDown() - if arg1 == "MiddleButton" then return end - if arg1 == "RightButton" then - if pfUI.unlock.dock.parent == this and pfUI.unlock.dock:IsShown() then - SetDockToFrame(nil) - else - SetDockToFrame(this) - end - return - end + local function DraggerOnDragStart() local frame = this.frame pfUI.unlock.selection = GetFrames() @@ -349,10 +340,7 @@ pfUI:RegisterModule("unlock", "vanilla:tbc", function () if frame.OnMove then frame:OnMove() end end - local function DraggerOnMouseUp() - if arg1 == "MiddleButton" then return end - if arg1 == "RightButton" then return end - + local function DraggerOnDragStop() local frame = this.frame local name = this.fname @@ -410,14 +398,25 @@ pfUI:RegisterModule("unlock", "vanilla:tbc", function () end local function DraggerOnClick() - pfUI.unlock.selection = GetFrames() - for id, frame in pairs(pfUI.unlock.selection) do - pfUI_config["position"][frame:GetName()] = nil - UpdateMovable(frame) + if arg1 == "RightButton" then + -- add dockframe to the dragger to show advanced options + if pfUI.unlock.dock.parent == this and pfUI.unlock.dock:IsShown() then + SetDockToFrame(nil) + else + SetDockToFrame(this) + end + return + elseif arg1 == "MiddleButton" then + -- reset positions of dragger and all connected frames + pfUI.unlock.selection = GetFrames() + for id, frame in pairs(pfUI.unlock.selection) do + pfUI_config["position"][frame:GetName()] = nil + UpdateMovable(frame) - if frame.OnMove then frame:OnMove() end + if frame.OnMove then frame:OnMove() end + end + UpdateDockValues() end - UpdateDockValues() end local function DraggerOnEnter() @@ -437,7 +436,8 @@ pfUI:RegisterModule("unlock", "vanilla:tbc", function () local label = string.sub(fname, 1, 2) == "pf" and strsub(fname,3) or fname local d = CreateFrame("Button", fname .. "Drag", f) - d:RegisterForClicks("MiddleButtonUp") + d:RegisterForClicks("MiddleButtonUp", "RightButtonUp") + d:RegisterForDrag("LeftButton") d:SetAllPoints(f) d:SetFrameStrata("DIALOG") d:SetAlpha(1) @@ -461,8 +461,8 @@ pfUI:RegisterModule("unlock", "vanilla:tbc", function () d.text:SetText(label) d:SetScript("OnMouseWheel", DraggerOnMouseWheel) - d:SetScript("OnMouseDown", DraggerOnMouseDown) - d:SetScript("OnMouseUp", DraggerOnMouseUp) + d:SetScript("OnDragStart", DraggerOnDragStart) + d:SetScript("OnDragStop", DraggerOnDragStop) d:SetScript("OnClick", DraggerOnClick) d:SetScript("OnEnter", DraggerOnEnter) d:SetScript("OnLeave", DraggerOnLeave)