Skip to content

TabGroup active tab persistence #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 72 additions & 11 deletions Editor/Elements/TriTabGroupElement.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System.Collections.Generic;
using TriInspector.Resolvers;
using UnityEngine;
using UnityEditor;

namespace TriInspector.Elements
{
public class TriTabGroupElement : TriHeaderGroupBaseElement
{
private const string DefaultTabName = "Main";

private readonly List<TabInfo> _tabs;
private readonly Dictionary<string, TriElement> _tabElements;

private string _activeTabName;
private string _tabGroupId;
private bool _isInitializing;
private bool _activeTabLoaded = false;

private struct TabInfo
{
Expand All @@ -25,6 +27,54 @@ public TriTabGroupElement()
_tabs = new List<TabInfo>();
_tabElements = new Dictionary<string, TriElement>();
_activeTabName = null;
_tabGroupId = "";
}

private string GetTabGroupPreferenceKey()
{
if (string.IsNullOrEmpty(_tabGroupId) && _tabs.Count > 0 && _tabs[0].property != null)
{
// Create a unique key by walking up to find the root object
var property = _tabs[0].property;
var rootProperty = property;
while (rootProperty.Parent != null)
{
rootProperty = rootProperty.Parent;
}

var targetObject = rootProperty.Value as Object;
if (targetObject != null)
{
_tabGroupId = $"TriTabGroup_{targetObject.GetInstanceID()}_{property.PropertyPath}";
}
}
return _tabGroupId;
}

private void SaveActiveTab()
{
if (_isInitializing) return;

var preferenceKey = GetTabGroupPreferenceKey();
if (!string.IsNullOrEmpty(preferenceKey) && !string.IsNullOrEmpty(_activeTabName))
{
EditorPrefs.SetString(preferenceKey, _activeTabName);
}
}

private bool LoadActiveTab()
{
var preferenceKey = GetTabGroupPreferenceKey();
if (!string.IsNullOrEmpty(preferenceKey))
{
var savedTab = EditorPrefs.GetString(preferenceKey, null);
if (!string.IsNullOrEmpty(savedTab) && _tabElements.ContainsKey(savedTab))
{
SetActiveTabInternal(savedTab);
return true;
}
}
return false;
}

protected override void DrawHeader(Rect position)
Expand All @@ -34,6 +84,13 @@ protected override void DrawHeader(Rect position)
return;
}

// Load saved tab state if no active tab is set
if (!_activeTabLoaded)
{
_activeTabLoaded = true;
LoadActiveTab();
}

var tabRect = new Rect(position)
{
width = position.width / _tabs.Count,
Expand All @@ -54,13 +111,11 @@ protected override void DrawHeader(Rect position)
var tabStyle = index == 0 ? TriEditorStyles.TabFirst
: index == tabCount - 1 ? TriEditorStyles.TabLast
: TriEditorStyles.TabMiddle;

var isTabActive = GUI.Toggle(tabRect, _activeTabName == tab.name, content, tabStyle);
if (isTabActive && _activeTabName != tab.name)
{
SetActiveTab(tab.name);
}

tabRect.x += tabRect.width;
}
}
Expand All @@ -69,7 +124,6 @@ protected override void DrawHeader(Rect position)
protected override void AddPropertyChild(TriElement element, TriProperty property)
{
var tabName = DefaultTabName;

if (property.TryGetAttribute(out TabAttribute tab))
{
tabName = tab.TabName ?? tabName;
Expand All @@ -78,14 +132,12 @@ protected override void AddPropertyChild(TriElement element, TriProperty propert
if (!_tabElements.TryGetValue(tabName, out var tabElement))
{
tabElement = new TriElement();

var info = new TabInfo
{
name = tabName,
titleResolver = ValueResolver.ResolveString(property.Definition, tabName),
property = property,
};

_tabElements[tabName] = tabElement;
_tabs.Add(info);

Expand All @@ -96,20 +148,29 @@ protected override void AddPropertyChild(TriElement element, TriProperty propert

if (_activeTabName == null)
{
SetActiveTab(tabName);
_isInitializing = true;
if (!LoadActiveTab())
{
SetActiveTabInternal(tabName);
}
_isInitializing = false;
}
}

tabElement.AddChild(element);
}

private void SetActiveTab(string tabName)
private void SetActiveTabInternal(string tabName)
{
_activeTabName = tabName;

RemoveAllChildren();

AddChild(_tabElements[_activeTabName]);
}

private void SetActiveTab(string tabName)
{
SetActiveTabInternal(tabName);
SaveActiveTab();
}
}
}