From 1bce0dc91569724c6d304da2c57f661a745eaab1 Mon Sep 17 00:00:00 2001 From: Pavel Klymentenko Date: Thu, 10 Aug 2023 14:10:57 +0200 Subject: [PATCH] feat: manually copied scripts from "feat/display-settings-in-preferences" branch because of repository folder structure changes (package places in root) --- Editor/UIToolkit/Controls.meta | 3 + Editor/UIToolkit/Controls/TabController.cs | 122 +++++++++++++++ .../UIToolkit/Controls/TabController.cs.meta | 3 + Editor/UIToolkit/Extensions.meta | 3 + .../Extensions/ListViewExtentions.cs | 20 +++ .../Extensions/ListViewExtentions.cs.meta | 3 + .../AboutPreferencesWindow.cs | 30 ++++ .../AboutPreferencesWindow.cs.meta | 3 + .../PackagePreferencesWindow.cs | 148 ++++++++++++++++++ .../PackagePreferencesWindow.cs.meta | 3 + .../SettingsWindow/PackageSettingsWindow.cs | 44 +++--- 11 files changed, 356 insertions(+), 26 deletions(-) create mode 100644 Editor/UIToolkit/Controls.meta create mode 100644 Editor/UIToolkit/Controls/TabController.cs create mode 100644 Editor/UIToolkit/Controls/TabController.cs.meta create mode 100644 Editor/UIToolkit/Extensions.meta create mode 100644 Editor/UIToolkit/Extensions/ListViewExtentions.cs create mode 100644 Editor/UIToolkit/Extensions/ListViewExtentions.cs.meta create mode 100644 Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs create mode 100644 Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs.meta create mode 100644 Editor/UIToolkit/PreferencesWindow/PackagePreferencesWindow.cs create mode 100644 Editor/UIToolkit/PreferencesWindow/PackagePreferencesWindow.cs.meta diff --git a/Editor/UIToolkit/Controls.meta b/Editor/UIToolkit/Controls.meta new file mode 100644 index 0000000..6611739 --- /dev/null +++ b/Editor/UIToolkit/Controls.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 271bb5495bf049a5b4090914b94076d9 +timeCreated: 1679558331 \ No newline at end of file diff --git a/Editor/UIToolkit/Controls/TabController.cs b/Editor/UIToolkit/Controls/TabController.cs new file mode 100644 index 0000000..8a5451f --- /dev/null +++ b/Editor/UIToolkit/Controls/TabController.cs @@ -0,0 +1,122 @@ +#if UNITY_2019_4_OR_NEWER + +using System; +using System.Collections.Generic; +using System.Linq; +using StansAssets.Foundation.UIElements; +using UnityEngine.Assertions; +using UnityEngine.UIElements; + +namespace StansAssets.Plugins.Editor +{ + /// + /// Tab controller based on to switch between tabs + /// and to display their contents + /// + public class TabController + { + readonly Dictionary m_Tabs = new Dictionary(); + + readonly ButtonStrip m_TabsButtons; + readonly ScrollView m_TabsContainer; + + /// + /// Available tabs' labels. + /// + public IEnumerable Tabs => m_Tabs.Keys; + + /// + /// Active tab label from . + /// + public string ActiveTab => m_TabsButtons.Value; + + /// + /// This constructor will looking for already existing elements: + /// (without name) and named "tabs-container" + /// The purpose of this is to support . + /// + /// Element that contains and named tabs-container + public TabController(VisualElement root) + { + m_TabsButtons = root.Q(); + m_TabsContainer = root.Q("tabs-container"); + + Init(); + } + + /// + /// Add tab to the window top bar. + /// + /// Tab label. + /// Tab content. + /// Will throw tab with the same label was already added. + public void AddTab(string label, VisualElement content) + { + if (!m_Tabs.ContainsKey(label)) + { + m_TabsButtons.AddChoice(label, label); + m_Tabs.Add(label, content); + content.viewDataKey = label; + } + else + { + throw new ArgumentException($"Tab '{label}' already added", nameof(label)); + } + } + + /// + /// Activate tab by label + /// + /// Early specified tab label + public void ActivateTab(string label) + { + if (!m_Tabs.ContainsKey(label)) + { + return; + } + + m_TabsButtons.SetValue(label); + } + + /// + /// Set the flexible growth property of tabs content container + /// + /// + public void ContentContainerFlexGrow(StyleFloat styleFloat) + { + m_TabsContainer.contentContainer.style.flexGrow = styleFloat; + } + + /// + /// Refresh current tab + /// + public void RefreshActiveTab() + { + if (string.IsNullOrEmpty(ActiveTab)) + { + return; + } + + foreach (var tab in m_Tabs) + { + tab.Value.RemoveFromHierarchy(); + } + + var element = m_Tabs.First(i => i.Key.Equals(m_TabsButtons.Value)).Value; + m_TabsContainer.Add(element); + } + + void Init() + { + Assert.IsNotNull(m_TabsButtons); + Assert.IsNotNull(m_TabsContainer); + + m_TabsButtons.CleanUp(); + m_TabsButtons.OnButtonClick += RefreshActiveTab; + + RefreshActiveTab(); + } + } +} + +#endif \ No newline at end of file diff --git a/Editor/UIToolkit/Controls/TabController.cs.meta b/Editor/UIToolkit/Controls/TabController.cs.meta new file mode 100644 index 0000000..6df2e70 --- /dev/null +++ b/Editor/UIToolkit/Controls/TabController.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 264ca0a8faf641d1a91f524c4ff90bc9 +timeCreated: 1679498491 \ No newline at end of file diff --git a/Editor/UIToolkit/Extensions.meta b/Editor/UIToolkit/Extensions.meta new file mode 100644 index 0000000..4fe177c --- /dev/null +++ b/Editor/UIToolkit/Extensions.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 418ce6e2c1044a42a6e09268dbe1d1d3 +timeCreated: 1690555305 \ No newline at end of file diff --git a/Editor/UIToolkit/Extensions/ListViewExtentions.cs b/Editor/UIToolkit/Extensions/ListViewExtentions.cs new file mode 100644 index 0000000..2cb007b --- /dev/null +++ b/Editor/UIToolkit/Extensions/ListViewExtentions.cs @@ -0,0 +1,20 @@ +using UnityEngine.UIElements; + +namespace StansAssets.Plugins.Editor +{ + public static class ListViewExtensions + { + /// + /// Rebuild/refresh ListView in compatible mode with Unity 2019/2021 editor + /// + /// + public static void RebuildInCompatibleMode(this ListView listView) + { +#if UNITY_2019_4_40 + listView.Refresh(); +#else + listView.Rebuild(); +#endif + } + } +} \ No newline at end of file diff --git a/Editor/UIToolkit/Extensions/ListViewExtentions.cs.meta b/Editor/UIToolkit/Extensions/ListViewExtentions.cs.meta new file mode 100644 index 0000000..bdff59a --- /dev/null +++ b/Editor/UIToolkit/Extensions/ListViewExtentions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 04f6020b34264dc692dfe135e5360661 +timeCreated: 1690540541 \ No newline at end of file diff --git a/Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs b/Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs new file mode 100644 index 0000000..c1a6900 --- /dev/null +++ b/Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs @@ -0,0 +1,30 @@ +#if UNITY_2019_4_OR_NEWER + +using JetBrains.Annotations; +using StansAssets.Foundation.Editor; +using UnityEditor; +using UnityEngine.UIElements; +using PackageInfo = UnityEditor.PackageManager.PackageInfo; + +namespace StansAssets.Plugins.Editor +{ + [UsedImplicitly] + sealed class AboutPreferencesWindow : PackagePreferencesWindow + { + protected override PackageInfo GetPackageInfo() + => PackageManagerUtility.GetPackageInfo(PluginsDevKitPackage.Name); + + protected override string SettingsPath => $"{PluginsDevKitPackage.RootMenu}/{GetPackageInfo().displayName}"; + protected override SettingsScope Scope => SettingsScope.User; + + protected override void OnActivate(string searchContext, VisualElement rootElement) + { + ContentContainerFlexGrow(1); + AddTab("About", new AboutTab()); + } + + protected override void OnDeactivate() { } + } +} + +#endif \ No newline at end of file diff --git a/Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs.meta b/Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs.meta new file mode 100644 index 0000000..1f73971 --- /dev/null +++ b/Editor/UIToolkit/PreferencesWindow/AboutPreferencesWindow.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c25ec7554d2142a0a7514c55f3676097 +timeCreated: 1679567415 \ No newline at end of file diff --git a/Editor/UIToolkit/PreferencesWindow/PackagePreferencesWindow.cs b/Editor/UIToolkit/PreferencesWindow/PackagePreferencesWindow.cs new file mode 100644 index 0000000..56a94c8 --- /dev/null +++ b/Editor/UIToolkit/PreferencesWindow/PackagePreferencesWindow.cs @@ -0,0 +1,148 @@ +#if UNITY_2019_4_OR_NEWER + +using System; +using System.Collections.Generic; +using System.Linq; +using StansAssets.Foundation.Editor; +using UnityEditor; +using UnityEditor.UIElements; +using UnityEngine.UIElements; + +namespace StansAssets.Plugins.Editor +{ + /// + /// Base class for Plugin Preferences Window + /// + public abstract class PackagePreferencesWindow + { + TabController m_TabController; + + /// + /// Structure describing a Unity Package. + /// + protected abstract UnityEditor.PackageManager.PackageInfo GetPackageInfo(); + + + /// + /// Gets Path used to place the SettingsProvider in the tree view of the Preferences or Project Settings window. + /// The path should be unique among all other settings paths and should use "/" as its separator. + /// + protected abstract string SettingsPath { get; } + + /// + /// Gets the Scope of the SettingsProvider. The Scope determines whether the SettingsProvider appears + /// in the Preferences window (SettingsScope.User) or the Settings window (SettingsScope.Project). + /// + protected abstract SettingsScope Scope { get; } + + /// + /// Add tab to the window top bar. + /// + /// Tab label. + /// Tab content. + /// Will throw tab with the same label was already added. + protected void AddTab(string label, VisualElement content) + { + m_TabController.AddTab(label, content); + } + + /// + /// Activate tab by name + /// + /// Early specified tab name + protected void ActivateTab(string name) + { + m_TabController.ActivateTab(name); + } + + /// + /// Set the flexible growth property of tabs content container + /// + /// + protected void ContentContainerFlexGrow(StyleFloat styleFloat) + { + m_TabController.ContentContainerFlexGrow(styleFloat); + } + + /// + /// Overrides SettingsProvider.OnActivate. + /// + protected abstract void OnActivate(string searchContext, VisualElement rootElement); + + /// + /// Overrides SettingsProvider.OnDeactivate. + /// + protected abstract void OnDeactivate(); + + void OnActivateWindow(string searchContext, VisualElement rootElement) + { + UIToolkitEditorUtility.CloneTreeAndApplyStyle(rootElement, + $"{PluginsDevKitPackage.UIToolkitPath}/SettingsWindow/PackageSettingsWindow"); + + // Hide search bar from PackageSettingsWindow. In preferences we already have search bar + // and it's value in "searchContext" parameter + var searchBar = rootElement.Q(); + if (searchBar != null) + { + searchBar.style.visibility = Visibility.Hidden; + } + + var packageInfo = GetPackageInfo(); + rootElement.Q