Skip to content

Commit e829154

Browse files
authored
Merge branch 'v2_develop' into v2_3767_restoring-drivers-and-fixes
2 parents 14dff21 + b55ed5a commit e829154

File tree

10 files changed

+177
-175
lines changed

10 files changed

+177
-175
lines changed

Example/Example.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System;
77
using Terminal.Gui;
88

9-
// Override the default configuraiton for the application to use the Light theme
9+
// Override the default configuration for the application to use the Light theme
1010
ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }""";
1111

1212
Application.Run<ExampleWindow> ().Dispose ();

Terminal.Gui/Configuration/ConfigLocations.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,34 @@ public enum ConfigLocations
2323
Default = 0b_0000_0001,
2424

2525
/// <summary>
26-
/// Global settings in the current directory (e.g. <c>./.tui/config.json</c>).
26+
/// App resources (e.g. <c>MyApp.Resources.config.json</c>).
2727
/// </summary>
28-
GlobalCurrent = 0b_0000_0010,
28+
AppResources = 0b_0000_0010,
2929

3030
/// <summary>
31-
/// Global settings in the home directory (e.g. <c>~/.tui/config.json</c>).
31+
/// Settings in the <see cref="ConfigurationManager.RuntimeConfig"/> static property.
3232
/// </summary>
33-
GlobalHome = 0b_0000_0100,
33+
Runtime = 0b_0000_0100,
3434

3535
/// <summary>
36-
/// App resources (e.g. <c>MyApp.Resources.config.json</c>).
36+
/// Global settings in the current directory (e.g. <c>./.tui/config.json</c>).
3737
/// </summary>
38-
AppResources = 0b_0000_1000,
38+
GlobalCurrent = 0b_0000_1000,
3939

4040
/// <summary>
41-
/// App settings in the current directory (e.g. <c>./.tui/MyApp.config.json</c>).
41+
/// Global settings in the home directory (e.g. <c>~/.tui/config.json</c>).
4242
/// </summary>
43-
AppCurrent = 0b_0001_0000,
43+
GlobalHome = 0b_0001_0000,
4444

4545
/// <summary>
46-
/// App settings in the home directory (e.g. <c>~/.tui/MyApp.config.json</c>).
46+
/// App settings in the current directory (e.g. <c>./.tui/MyApp.config.json</c>).
4747
/// </summary>
48-
AppHome = 0b_0010_0000,
48+
AppCurrent = 0b_0010_0000,
4949

5050
/// <summary>
51-
/// Settings in the <see cref="ConfigurationManager.RuntimeConfig"/> static property.
51+
/// App settings in the home directory (e.g. <c>~/.tui/MyApp.config.json</c>).
5252
/// </summary>
53-
Runtime = 0b_0100_0000,
53+
AppHome = 0b_0100_0000,
5454

5555
/// <summary>This constant is a combination of all locations</summary>
5656
All = 0b_1111_1111

Terminal.Gui/Configuration/ConfigurationManager.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public static string GetEmptyJson ()
223223
/// <summary>
224224
/// Gets or sets the in-memory config.json. See <see cref="ConfigLocations.Runtime"/>.
225225
/// </summary>
226-
public static string? RuntimeConfig { get; set; }
226+
public static string? RuntimeConfig { get; set; } = """{ }""";
227227

228228
/// <summary>
229229
/// Loads all settings found in the configuration storage locations (<see cref="ConfigLocations"/>). Optionally, resets
@@ -250,16 +250,6 @@ public static void Load (bool reset = false)
250250
Reset ();
251251
}
252252

253-
if (Locations.HasFlag (ConfigLocations.GlobalCurrent))
254-
{
255-
Settings?.Update ($"./.tui/{_configFilename}");
256-
}
257-
258-
if (Locations.HasFlag (ConfigLocations.GlobalHome))
259-
{
260-
Settings?.Update ($"~/.tui/{_configFilename}");
261-
}
262-
263253
if (Locations.HasFlag (ConfigLocations.AppResources))
264254
{
265255
string? embeddedStylesResourceName = Assembly.GetEntryAssembly ()
@@ -272,22 +262,33 @@ public static void Load (bool reset = false)
272262
embeddedStylesResourceName = _configFilename;
273263
}
274264

275-
Settings?.UpdateFromResource (Assembly.GetEntryAssembly ()!, embeddedStylesResourceName!);
265+
Settings?.UpdateFromResource (Assembly.GetEntryAssembly ()!, embeddedStylesResourceName!, ConfigLocations.AppResources);
276266
}
277267

278-
if (Locations.HasFlag (ConfigLocations.AppCurrent))
268+
if (Locations.HasFlag (ConfigLocations.Runtime) && !string.IsNullOrEmpty (RuntimeConfig))
279269
{
280-
Settings?.Update ($"./.tui/{AppName}.{_configFilename}");
270+
Settings?.Update (RuntimeConfig, "ConfigurationManager.RuntimeConfig", ConfigLocations.Runtime);
281271
}
282272

283-
if (Locations.HasFlag (ConfigLocations.AppHome))
273+
if (Locations.HasFlag (ConfigLocations.GlobalCurrent))
284274
{
285-
Settings?.Update ($"~/.tui/{AppName}.{_configFilename}");
275+
Settings?.Update ($"./.tui/{_configFilename}", ConfigLocations.GlobalCurrent);
286276
}
287277

288-
if (Locations.HasFlag (ConfigLocations.Runtime) && !string.IsNullOrEmpty (RuntimeConfig))
278+
if (Locations.HasFlag (ConfigLocations.GlobalHome))
279+
{
280+
Settings?.Update ($"~/.tui/{_configFilename}", ConfigLocations.GlobalHome);
281+
}
282+
283+
284+
if (Locations.HasFlag (ConfigLocations.AppCurrent))
285+
{
286+
Settings?.Update ($"./.tui/{AppName}.{_configFilename}", ConfigLocations.AppCurrent);
287+
}
288+
289+
if (Locations.HasFlag (ConfigLocations.AppHome))
289290
{
290-
Settings?.Update (RuntimeConfig, "ConfigurationManager.Memory");
291+
Settings?.Update ($"~/.tui/{AppName}.{_configFilename}", ConfigLocations.AppHome);
291292
}
292293

293294
ThemeManager.SelectedTheme = Settings!["Theme"].PropertyValue as string ?? "Default";
@@ -358,7 +359,8 @@ public static void Reset ()
358359
{
359360
Settings.UpdateFromResource (
360361
typeof (ConfigurationManager).Assembly,
361-
$"Terminal.Gui.Resources.{_configFilename}"
362+
$"Terminal.Gui.Resources.{_configFilename}",
363+
ConfigLocations.Default
362364
);
363365
}
364366

Terminal.Gui/Configuration/SettingsScope.cs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Terminal.Gui;
2727
public class SettingsScope : Scope<SettingsScope>
2828
{
2929
/// <summary>The list of paths to the configuration files.</summary>
30-
public List<string> Sources = new ();
30+
public Dictionary<ConfigLocations, string> Sources { get; } = new ();
3131

3232
/// <summary>Points to our JSON schema.</summary>
3333
[JsonInclude]
@@ -37,19 +37,20 @@ public class SettingsScope : Scope<SettingsScope>
3737
/// <summary>Updates the <see cref="SettingsScope"/> with the settings in a JSON string.</summary>
3838
/// <param name="stream">Json document to update the settings with.</param>
3939
/// <param name="source">The source (filename/resource name) the Json document was read from.</param>
40+
/// <param name="location">Location</param>
4041
[RequiresUnreferencedCode ("AOT")]
4142
[RequiresDynamicCode ("AOT")]
42-
public SettingsScope? Update (Stream stream, string source)
43+
public SettingsScope? Update (Stream stream, string source, ConfigLocations location)
4344
{
4445
// Update the existing settings with the new settings.
4546
try
4647
{
4748
Update ((SettingsScope)JsonSerializer.Deserialize (stream, typeof (SettingsScope), _serializerOptions)!);
4849
OnUpdated ();
4950
Debug.WriteLine ($"ConfigurationManager: Read configuration from \"{source}\"");
50-
if (!Sources.Contains (source))
51+
if (!Sources.ContainsValue (source))
5152
{
52-
Sources.Add (source);
53+
Sources.Add (location, source);
5354
}
5455

5556
return this;
@@ -68,19 +69,20 @@ public class SettingsScope : Scope<SettingsScope>
6869
}
6970

7071
/// <summary>Updates the <see cref="SettingsScope"/> with the settings in a JSON file.</summary>
71-
/// <param name="filePath"></param>
72+
/// <param name="filePath">Path to the file.</param>
73+
/// <param name="location">The location</param>
7274
[RequiresUnreferencedCode ("AOT")]
7375
[RequiresDynamicCode ("AOT")]
74-
public SettingsScope? Update (string filePath)
76+
public SettingsScope? Update (string filePath, ConfigLocations location)
7577
{
7678
string realPath = filePath.Replace ("~", Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
7779

7880
if (!File.Exists (realPath))
7981
{
8082
Debug.WriteLine ($"ConfigurationManager: Configuration file \"{realPath}\" does not exist.");
81-
if (!Sources.Contains (filePath))
83+
if (!Sources.ContainsValue (filePath))
8284
{
83-
Sources.Add (filePath);
85+
Sources.Add (location, filePath);
8486
}
8587

8688
return this;
@@ -95,15 +97,15 @@ public class SettingsScope : Scope<SettingsScope>
9597
try
9698
{
9799
FileStream? stream = File.OpenRead (realPath);
98-
SettingsScope? s = Update (stream, filePath);
100+
SettingsScope? s = Update (stream, filePath, location);
99101
stream.Close ();
100102
stream.Dispose ();
101103

102104
return s;
103105
}
104106
catch (IOException ioe)
105107
{
106-
Debug.WriteLine($"Couldn't open {filePath}. Retrying...: {ioe}");
108+
Debug.WriteLine ($"Couldn't open {filePath}. Retrying...: {ioe}");
107109
Task.Delay (100);
108110
retryCount++;
109111
}
@@ -115,32 +117,33 @@ public class SettingsScope : Scope<SettingsScope>
115117
/// <summary>Updates the <see cref="SettingsScope"/> with the settings in a JSON string.</summary>
116118
/// <param name="json">Json document to update the settings with.</param>
117119
/// <param name="source">The source (filename/resource name) the Json document was read from.</param>
120+
/// <param name="location">The location.</param>
118121
[RequiresUnreferencedCode ("AOT")]
119122
[RequiresDynamicCode ("AOT")]
120-
public SettingsScope? Update (string? json, string source)
123+
public SettingsScope? Update (string? json, string source, ConfigLocations location)
121124
{
122-
//if (string.IsNullOrEmpty (json))
123-
//{
124-
// Debug.WriteLine ($"ConfigurationManager: Configuration file \"{source}\" is empty.");
125-
// return this;
126-
//}
125+
if (string.IsNullOrEmpty (json))
126+
{
127+
return null;
128+
}
127129
var stream = new MemoryStream ();
128130
var writer = new StreamWriter (stream);
129131
writer.Write (json);
130132
writer.Flush ();
131133
stream.Position = 0;
132134

133-
return Update (stream, source);
135+
return Update (stream, source, location);
134136
}
135137

136138
/// <summary>Updates the <see cref="SettingsScope"/> with the settings from a Json resource.</summary>
137139
/// <param name="assembly"></param>
138140
/// <param name="resourceName"></param>
141+
/// <param name="location"></param>
139142
[RequiresUnreferencedCode ("AOT")]
140143
[RequiresDynamicCode ("AOT")]
141-
public SettingsScope? UpdateFromResource (Assembly assembly, string resourceName)
144+
public SettingsScope? UpdateFromResource (Assembly assembly, string resourceName, ConfigLocations location)
142145
{
143-
if (resourceName is null || string.IsNullOrEmpty (resourceName))
146+
if (string.IsNullOrEmpty (resourceName))
144147
{
145148
Debug.WriteLine (
146149
$"ConfigurationManager: Resource \"{resourceName}\" does not exist in \"{assembly.GetName ().Name}\"."
@@ -149,20 +152,13 @@ public class SettingsScope : Scope<SettingsScope>
149152
return this;
150153
}
151154

152-
// BUG: Not trim-compatible
153-
// Not a bug, per se, but it's easily fixable by just loading the file.
154-
// Defaults can just be field initializers for involved types.
155-
using Stream? stream = assembly.GetManifestResourceStream (resourceName)!;
155+
using Stream? stream = assembly.GetManifestResourceStream (resourceName);
156156

157157
if (stream is null)
158158
{
159-
Debug.WriteLine (
160-
$"ConfigurationManager: Failed to read resource \"{resourceName}\" from \"{assembly.GetName ().Name}\"."
161-
);
162-
163-
return this;
159+
return null;
164160
}
165161

166-
return Update (stream, $"resource://[{assembly.GetName ().Name}]/{resourceName}");
162+
return Update (stream, $"resource://[{assembly.GetName ().Name}]/{resourceName}", location);
167163
}
168164
}

0 commit comments

Comments
 (0)