Skip to content

Commit

Permalink
Persist window layout in configuration (#803)
Browse files Browse the repository at this point in the history
This replaces the use of the game layout cache for storing the position
of our main windowto instead persist it within our own saved variables,
using a combination of LibWindow for window positioning and our own
management of window size.

The layout cache is known to have a few issues - it restores layout
information at weird times in the load process, and it forgets frames
if they're not loaded in a UI session.

Storing the layout in our saved variables resolves this; we get a fully
controllable point in the load process from which we can restore the
layout and reposition/resize the window, and we don't have to worry
about the game forgetting our frame if the addon isn't loaded once.

Use of LibWindow for managing the window position is employed as it's
the recommended practice; it has smart logic for anchoring to the
nearest quadrant of the screen and handles scale changes. This can be
quite important for the small subset of users who might be mirroring
their SVs across devices with different screen resolutions.
  • Loading branch information
Meorawr authored Nov 7, 2023
1 parent 40fc627 commit f41fce8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions .pkgmeta
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ externals:
totalRP3/Libs/LibMSP: https://github.com/wow-rp-addons/LibMSP
totalRP3/Libs/LibRPMedia: https://github.com/wow-rp-addons/LibRPMedia
totalRP3/Libs/LibStub: https://repos.curseforge.com/wow/libstub/trunk
totalRP3/Libs/LibWindow-1.1: https://repos.curseforge.com/wow/libwindow-1-1/trunk
totalRP3/Libs/UTF8: https://repos.curseforge.com/wow/utf8/trunk

embedded-libraries:
Expand Down
1 change: 1 addition & 0 deletions totalRP3/Core/Configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ TRP3_API.RegisterCallback(TRP3_Addon, TRP3_Addon.Events.WORKFLOW_ON_LOAD, functi
registerConfigKey("ui_animations", true);
registerConfigKey("disable_welcome_message", false);
registerConfigKey("hide_maximize_button", false);
registerConfigKey("window_layout", {}); -- Contents managed by TRP3_MainFrameMixin.
registerConfigKey("default_color_picker", false);
registerConfigKey("color_contrast_level", TRP3_API.ColorContrastOption.Default);
registerConfigKey("date_format", "");
Expand Down
2 changes: 0 additions & 2 deletions totalRP3/Core/UIMain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ TRP3_API.RegisterCallback(TRP3_Addon, TRP3_Addon.Events.WORKFLOW_ON_LOADED, func
TRP3_API.navigation.delayedRefresh();
end);

TRP3_API.ui.frame.setupMove(TRP3_MainFrame);

-- Update frame
TRP3_UpdateFrame.popup.title:SetText(L.NEW_VERSION_TITLE);
end);
45 changes: 43 additions & 2 deletions totalRP3/UI/Main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-- Copyright The Total RP 3 Authors
-- SPDX-License-Identifier: Apache-2.0

local LibWindow = LibStub:GetLibrary("LibWindow-1.1");

local DEFAULT_WINDOW_WIDTH = 768;
local DEFAULT_WINDOW_HEIGHT = 500;

local WindowState = {
Normal = 1,
Maximized = 2,
Expand All @@ -9,12 +14,22 @@ local WindowState = {
TRP3_MainFrameMixin = {};

function TRP3_MainFrameMixin:OnLoad()
tinsert(UISpecialFrames, self:GetName());
self.windowState = WindowState.Normal;
self.windowLayout = nil; -- Aliases configuration table; set during addon load.

tinsert(UISpecialFrames, self:GetName());
TRP3_Addon.RegisterCallback(self, "CONFIGURATION_CHANGED", "OnConfigurationChanged");
TRP3_Addon.RegisterCallback(self, "WORKFLOW_ON_FINISH", "OnLayoutLoaded");
TRP3_API.ui.frame.initResize(self.Resize);
end

function TRP3_MainFrameMixin:OnLayoutLoaded()
self.windowLayout = TRP3_API.configuration.getValue("window_layout");
LibWindow.RegisterConfig(self, self.windowLayout);
LibWindow.MakeDraggable(self);
self:RestoreLayout();
end

function TRP3_MainFrameMixin:OnConfigurationChanged(_, key)
if key == "hide_maximize_button" then
self:UpdateWindowStateButtons();
Expand All @@ -26,6 +41,10 @@ function TRP3_MainFrameMixin:OnShow()
end

function TRP3_MainFrameMixin:OnSizeChanged()
if self:IsLayoutLoaded() then
self:SaveLayout();
end

TRP3_Addon:TriggerEvent(TRP3_Addon.Events.NAVIGATION_RESIZED, TRP3_MainFramePageContainer:GetSize());
end

Expand All @@ -39,7 +58,7 @@ function TRP3_MainFrameMixin:MaximizeWindow()
end

function TRP3_MainFrameMixin:RestoreWindow()
self:SetSize(768, 500);
self:SetSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
self:SetWindowState(WindowState.Normal);
end

Expand All @@ -48,6 +67,28 @@ function TRP3_MainFrameMixin:ResizeWindow(width, height)
self:SetWindowState(WindowState.Normal);
end

function TRP3_MainFrameMixin:IsLayoutLoaded()
return self.windowLayout ~= nil;
end

function TRP3_MainFrameMixin:RestoreLayout()
assert(self:IsLayoutLoaded(), "attempted to restore window layout before layout has been loaded");

local width = self.windowLayout.w or DEFAULT_WINDOW_WIDTH;
local height = self.windowLayout.h or DEFAULT_WINDOW_HEIGHT;
self:SetSize(width, height);
LibWindow.RestorePosition(self);
end

function TRP3_MainFrameMixin:SaveLayout()
assert(self:IsLayoutLoaded(), "attempted to save window layout before layout has been loaded");

local width, height = self:GetSize();
self.windowLayout.w = width;
self.windowLayout.h = height;
LibWindow.SavePosition(self);
end

function TRP3_MainFrameMixin:GetWindowState()
return self.windowState or WindowState.Normal;
end
Expand Down
2 changes: 1 addition & 1 deletion totalRP3/UI/Main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ https://raw.githubusercontent.com/Meorawr/wow-ui-schema/main/UI.xsd">
</Frames>
</Frame>

<Frame name="TRP3_MainFrame" inherits="TRP3_MainFrameTemplate" mixin="TRP3_MainFrameMixin" hidden="true" movable="true">
<Frame name="TRP3_MainFrame" inherits="TRP3_MainFrameTemplate" mixin="TRP3_MainFrameMixin" hidden="true" movable="true" dontSavePosition="true">
<Anchors>
<Anchor point="CENTER" x="0" y="0"/>
</Anchors>
Expand Down
1 change: 1 addition & 0 deletions totalRP3/totalRP3.toc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Libs\LibDeflate\lib.xml
Libs\LibDropDownExtension\LibDropDownExtension.lua
Libs\LibMSP\LibMSP.xml
Libs\LibRPMedia\lib.xml
Libs\LibWindow-1.1\LibWindow-1.1.lua
Libs\MSA-DropDownMenu-1.0\MSA-DropDownMenu-1.0.xml
Libs\UTF8\lib.xml

Expand Down

0 comments on commit f41fce8

Please sign in to comment.