Skip to content

Commit 4ae5f02

Browse files
committed
Settings split into global and local override. Now you can set global settings in Preferences and override per project in Project Settings.
1 parent d6cdf7e commit 4ae5f02

File tree

1 file changed

+91
-30
lines changed

1 file changed

+91
-30
lines changed

Editor/PluginSettingsProvider.cs

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.IO;
3+
using System.Linq;
24
using UnityEditor;
35
using UnityEngine;
46
using UnityEngine.UIElements;
@@ -9,54 +11,113 @@ public static class PluginSettingsProvider
911
{
1012
static Vector2 scrollPosition = default;
1113

12-
public static string LocalRepoPaths { get => PlayerPrefs.GetString(nameof(LocalRepoPaths), "../"); set => PlayerPrefs.SetString(nameof(LocalRepoPaths), value);}
13-
public static string GitPath { get => PlayerPrefs.GetString(nameof(GitPath), "git"); set => PlayerPrefs.SetString(nameof(GitPath), value);}
14-
public static bool DisableWhileProjectRunning { get => PlayerPrefs.GetInt(nameof(DisableWhileProjectRunning), 1) == 1; set => PlayerPrefs.SetInt(nameof(DisableWhileProjectRunning), value ? 1 : 0);}
15-
public static bool EnableInProjectBrowser { get => PlayerPrefs.GetInt(nameof(EnableInProjectBrowser), 1) == 1; set => PlayerPrefs.SetInt(nameof(EnableInProjectBrowser), value ? 1 : 0);}
16-
public static bool ShowBranchesInProjectBrowser { get => PlayerPrefs.GetInt(nameof(ShowBranchesInProjectBrowser), 1) == 1; set => PlayerPrefs.SetInt(nameof(ShowBranchesInProjectBrowser), value ? 1 : 0); }
17-
public static bool ShowStatusInProjectBrowser { get => PlayerPrefs.GetInt(nameof(ShowStatusInProjectBrowser), 0) == 1; set => PlayerPrefs.SetInt(nameof(ShowStatusInProjectBrowser), value ? 1 : 0); }
18-
public static bool ShowLinesChangeInProjectBrowser { get => PlayerPrefs.GetInt(nameof(ShowLinesChangeInProjectBrowser), 0) == 1; set => PlayerPrefs.SetInt(nameof(ShowLinesChangeInProjectBrowser), value ? 1 : 0); }
19-
public static bool WatchRefsDir { get => PlayerPrefs.GetInt(nameof(WatchRefsDir), 1) == 1; set => PlayerPrefs.SetInt(nameof(WatchRefsDir), value ? 1 : 0); }
20-
public static int RemoteRefreshIntervalSec { get => Mathf.Max(30, PlayerPrefs.GetInt(nameof(RemoteRefreshIntervalSec)), 5 * 60); set => PlayerPrefs.SetInt(nameof(RemoteRefreshIntervalSec), value); }
21-
public static int MaxParallelProcesses { get => Mathf.Max(1, PlayerPrefs.GetInt(nameof(MaxParallelProcesses), 10)); set => PlayerPrefs.SetInt(nameof(MaxParallelProcesses), value); }
14+
[AttributeUsage(AttributeTargets.Property)]
15+
class PrefAttribute : Attribute { };
16+
17+
public static bool UseLocalSettings { get => PlayerPrefs.GetInt(nameof(UseLocalSettings), 0) == 1; set => PlayerPrefs.SetInt(nameof(UseLocalSettings), value ? 1 : 0);}
18+
19+
[Pref] public static string LocalRepoPaths { get => GetString(nameof(LocalRepoPaths), "../"); set => SetString(nameof(LocalRepoPaths), value);}
20+
[Pref] public static string GitPath { get => GetString(nameof(GitPath), "git"); set => SetString(nameof(GitPath), value);}
21+
[Pref] public static bool DisableWhileProjectRunning { get => GetInt(nameof(DisableWhileProjectRunning), 1) == 1; set => SetInt(nameof(DisableWhileProjectRunning), value ? 1 : 0);}
22+
[Pref] public static bool EnableInProjectBrowser { get => GetInt(nameof(EnableInProjectBrowser), 1) == 1; set => SetInt(nameof(EnableInProjectBrowser), value ? 1 : 0);}
23+
[Pref] public static bool ShowBranchesInProjectBrowser { get => GetInt(nameof(ShowBranchesInProjectBrowser), 1) == 1; set => SetInt(nameof(ShowBranchesInProjectBrowser), value ? 1 : 0); }
24+
[Pref] public static bool ShowStatusInProjectBrowser { get => GetInt(nameof(ShowStatusInProjectBrowser), 0) == 1; set => SetInt(nameof(ShowStatusInProjectBrowser), value ? 1 : 0); }
25+
[Pref] public static bool ShowLinesChangeInProjectBrowser { get => GetInt(nameof(ShowLinesChangeInProjectBrowser), 0) == 1; set => SetInt(nameof(ShowLinesChangeInProjectBrowser), value ? 1 : 0); }
26+
[Pref] public static bool WatchRefsDir { get => GetInt(nameof(WatchRefsDir), 1) == 1; set => SetInt(nameof(WatchRefsDir), value ? 1 : 0); }
27+
[Pref] public static int RemoteRefreshIntervalSec { get => Mathf.Max(30, GetInt(nameof(RemoteRefreshIntervalSec), 120), 5 * 60); set => SetInt(nameof(RemoteRefreshIntervalSec), value); }
28+
[Pref] public static int MaxParallelProcesses { get => Mathf.Max(1, GetInt(nameof(MaxParallelProcesses), 10)); set => SetInt(nameof(MaxParallelProcesses), value); }
29+
30+
static string[] AllPrefs => typeof(PluginSettingsProvider).GetProperties().Where(p => p.GetCustomAttributes(typeof(PrefAttribute), false).Length > 0).Select(p => p.Name).ToArray();
31+
32+
[SettingsProvider]
33+
public static SettingsProvider EditorSettings() => new("Preferences/External Tools/Unity Git UI", SettingsScope.User) {
34+
activateHandler = (_, rootElement) => { rootElement.Add(new IMGUIContainer(() => OnGUI(false))); },
35+
deactivateHandler = OnDisable
36+
};
2237

2338
[SettingsProvider]
24-
public static SettingsProvider CreateMyCustomSettingsProvider() => new("Preferences/External Tools/MR Unity Git UI", SettingsScope.User) {
25-
activateHandler = (_, rootElement) => { rootElement.Add(new IMGUIContainer(OnGUI)); },
39+
public static SettingsProvider ProjectSettings() => new("Project/External Tools/Unity Git UI", SettingsScope.Project)
40+
{
41+
activateHandler = (_, rootElement) => { rootElement.Add(new IMGUIContainer(() => OnGUI(true))); },
2642
deactivateHandler = OnDisable
2743
};
2844

29-
static void OnGUI()
45+
static void OnGUI(bool local)
3046
{
31-
DisableWhileProjectRunning = EditorGUILayout.Toggle("Disable while playing", DisableWhileProjectRunning);
32-
EnableInProjectBrowser = EditorGUILayout.Toggle("Enable in Project Browser", EnableInProjectBrowser);
33-
ShowBranchesInProjectBrowser = EditorGUILayout.Toggle("Show branches in Project Browser", ShowBranchesInProjectBrowser);
34-
ShowStatusInProjectBrowser = EditorGUILayout.Toggle("Show status in Project Browser", ShowStatusInProjectBrowser);
35-
ShowLinesChangeInProjectBrowser = EditorGUILayout.Toggle("Show lines in Project Browser", ShowLinesChangeInProjectBrowser);
36-
WatchRefsDir = EditorGUILayout.Toggle("Watch .git/refs changes", WatchRefsDir);
37-
RemoteRefreshIntervalSec = EditorGUILayout.IntField("Remote refresh interval", RemoteRefreshIntervalSec);
38-
MaxParallelProcesses = EditorGUILayout.IntField("Max parallel processes", MaxParallelProcesses);
39-
GitPath = EditorGUILayout.TextField("Git path:", GitPath);
40-
GUILayout.Space(10);
41-
GUILayout.Label("Dependencies search paths:");
42-
LocalRepoPaths = EditorGUILayout.TextField(LocalRepoPaths);
43-
GUILayout.Label("<b>Visible packages:</b>", Style.RichTextLabel.Value);
44-
using (var scroll = new GUILayout.ScrollViewScope(scrollPosition, GUILayout.Width(600), GUILayout.Height(300)))
47+
if (local)
48+
{
49+
UseLocalSettings = EditorGUILayout.Toggle("Override global settings", UseLocalSettings);
50+
if (!UseLocalSettings)
51+
return;
52+
if (GUILayout.Button("Reset to global settings"))
53+
{
54+
if (EditorUtility.DisplayDialog("Reset to global settings", "Are you sure you want to reset to global settings?", "Yes", "No"))
55+
{
56+
foreach (var key in AllPrefs)
57+
PlayerPrefs.DeleteKey(key.Prefixed());
58+
}
59+
}
60+
}
61+
else
4562
{
46-
foreach (var packageDir in Utils.ListLocalPackageDirectories())
47-
GUILayout.Label($" {packageDir.Name} {$"<color={Colors.CyanBlue}>(git)</color>".When(Directory.Exists(Path.Join(packageDir.Path, ".git")))}", Style.RichTextLabel.Value);
48-
scrollPosition = scroll.scrollPosition;
63+
if (UseLocalSettings)
64+
EditorGUILayout.HelpBox("Local settings are used (see Project Settings)", MessageType.Warning);
65+
else
66+
EditorGUILayout.HelpBox("See Project Settings to override Git UI settings for the current project", MessageType.Info);
67+
}
68+
using (new EditorGUI.DisabledScope(local != UseLocalSettings))
69+
{
70+
DisableWhileProjectRunning = EditorGUILayout.Toggle("Disable while playing", DisableWhileProjectRunning);
71+
EnableInProjectBrowser = EditorGUILayout.Toggle("Enable in Project Browser", EnableInProjectBrowser);
72+
ShowBranchesInProjectBrowser = EditorGUILayout.Toggle("Show branches in Project Browser", ShowBranchesInProjectBrowser);
73+
ShowStatusInProjectBrowser = EditorGUILayout.Toggle("Show status in Project Browser", ShowStatusInProjectBrowser);
74+
ShowLinesChangeInProjectBrowser = EditorGUILayout.Toggle("Show lines in Project Browser", ShowLinesChangeInProjectBrowser);
75+
WatchRefsDir = EditorGUILayout.Toggle("Watch .git/refs changes", WatchRefsDir);
76+
RemoteRefreshIntervalSec = EditorGUILayout.IntField("Remote refresh interval", RemoteRefreshIntervalSec);
77+
MaxParallelProcesses = EditorGUILayout.IntField("Max parallel processes", MaxParallelProcesses);
78+
GitPath = EditorGUILayout.TextField("Git path:", GitPath);
79+
GUILayout.Space(10);
80+
GUILayout.Label("Dependencies search paths:");
81+
LocalRepoPaths = EditorGUILayout.TextField(LocalRepoPaths);
82+
GUILayout.Label("<b>Visible packages:</b>", Style.RichTextLabel.Value);
83+
using (var scroll = new GUILayout.ScrollViewScope(scrollPosition, GUILayout.Width(400), GUILayout.Height(300)))
84+
{
85+
foreach (var packageDir in Utils.ListLocalPackageDirectories())
86+
GUILayout.Label($" {packageDir.Name} {$"<color={Colors.CyanBlue}>(git)</color>".When(Directory.Exists(Path.Join(packageDir.Path, ".git")))}", Style.RichTextLabel.Value);
87+
scrollPosition = scroll.scrollPosition;
88+
}
4989
}
5090
}
5191

5292
static void OnDisable()
5393
{
94+
PlayerPrefs.Save();
5495
foreach (var module in Utils.GetGitModules())
5596
{
5697
module.RefreshFilesStatus();
5798
module.RefreshReferences();
5899
module.RefreshRemoteStatus();
59100
}
60101
}
102+
103+
static string Prefixed(this string key) => $"UnityGitUI/{key}";
104+
105+
static void SetInt(string key, int value)
106+
{
107+
if (UseLocalSettings)
108+
PlayerPrefs.SetInt(key.Prefixed(), value);
109+
else
110+
EditorPrefs.SetInt(key.Prefixed(), value);
111+
}
112+
static int GetInt(string key, int defaultValue) => UseLocalSettings ? PlayerPrefs.GetInt(key.Prefixed(), EditorPrefs.GetInt(key.Prefixed(), defaultValue)) : EditorPrefs.GetInt(key.Prefixed(), defaultValue);
113+
114+
static void SetString(string key, string value)
115+
{
116+
if (UseLocalSettings)
117+
PlayerPrefs.SetString(key.Prefixed(), value);
118+
else
119+
EditorPrefs.SetString(key.Prefixed(), value);
120+
}
121+
static string GetString(string key, string defaultValue) => UseLocalSettings ? PlayerPrefs.GetString(key.Prefixed(), EditorPrefs.GetString(key.Prefixed(), defaultValue)) : EditorPrefs.GetString(key.Prefixed(), defaultValue);
61122
}
62123
}

0 commit comments

Comments
 (0)