Skip to content

Commit

Permalink
Merge branch 'v2_develop' into v2_3675-ObjectDisposedException
Browse files Browse the repository at this point in the history
  • Loading branch information
tig authored Aug 28, 2024
2 parents d3987af + c97e412 commit 03db057
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 28 deletions.
25 changes: 13 additions & 12 deletions Terminal.Gui/Drawing/ColorStrings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable enable
using System.Collections;
using System.Globalization;
using System.Resources;
using Terminal.Gui.Resources;

namespace Terminal.Gui;
Expand All @@ -11,8 +10,6 @@ namespace Terminal.Gui;
/// </summary>
public static class ColorStrings
{
private static readonly ResourceManager _resourceManager = new (typeof (Strings));

/// <summary>
/// Gets the W3C standard string for <paramref name="color"/>.
/// </summary>
Expand All @@ -21,7 +18,7 @@ public static class ColorStrings
public static string? GetW3CColorName (Color color)
{
// Fetch the color name from the resource file
return _resourceManager.GetString ($"#{color.R:X2}{color.G:X2}{color.B:X2}", CultureInfo.CurrentUICulture);
return GlobalResources.GetString ($"#{color.R:X2}{color.G:X2}{color.B:X2}", CultureInfo.CurrentUICulture);
}

/// <summary>
Expand All @@ -30,14 +27,18 @@ public static class ColorStrings
/// <returns></returns>
public static IEnumerable<string> GetW3CColorNames ()
{
foreach (DictionaryEntry entry in _resourceManager.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
{
string keyName = entry.Key.ToString () ?? string.Empty;
foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (
CultureInfo.CurrentUICulture,
true,
true,
e =>
{
string keyName = e.Key.ToString () ?? string.Empty;

if (entry.Value is string colorName && keyName.StartsWith ('#'))
{
yield return colorName;
}
return e.Value is string && keyName.StartsWith ('#');
})!)
{
yield return (entry.Value as string)!;
}
}

Expand All @@ -50,7 +51,7 @@ public static IEnumerable<string> GetW3CColorNames ()
public static bool TryParseW3CColorName (string name, out Color color)
{
// Iterate through all resource entries to find the matching color name
foreach (DictionaryEntry entry in _resourceManager.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
foreach (DictionaryEntry entry in GlobalResources.GetResourceSet (CultureInfo.CurrentUICulture, true, true)!)
{
if (entry.Value is string colorName && colorName.Equals (name, StringComparison.OrdinalIgnoreCase))
{
Expand Down
70 changes: 70 additions & 0 deletions Terminal.Gui/Resources/GlobalResources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#nullable enable

using System.Collections;
using System.Globalization;
using System.Resources;

namespace Terminal.Gui.Resources;

/// <summary>
/// Provide static access to the ResourceManagerWrapper
/// </summary>
public static class GlobalResources
{
private static readonly ResourceManagerWrapper _resourceManagerWrapper;

static GlobalResources ()
{
// Initialize the ResourceManagerWrapper once
var resourceManager = new ResourceManager (typeof (Strings));
_resourceManagerWrapper = new (resourceManager);
}

/// <summary>
/// Looks up a resource value for a particular name. Looks in the specified CultureInfo, and if not found, all parent
/// CultureInfos.
/// </summary>
/// <param name="name"></param>
/// <param name="culture"></param>
/// <returns>Null if the resource was not found in the current culture or the invariant culture.</returns>
public static object GetObject (string name, CultureInfo culture = null!) { return _resourceManagerWrapper.GetObject (name, culture); }

/// <summary>
/// Looks up a set of resources for a particular CultureInfo. This is not useful for most users of the ResourceManager
/// - call GetString() or GetObject() instead. The parameters let you control whether the ResourceSet is created if it
/// hasn't yet been loaded and if parent CultureInfos should be loaded as well for resource inheritance.
/// </summary>
/// <param name="culture"></param>
/// <param name="createIfNotExists"></param>
/// <param name="tryParents"></param>
/// <returns></returns>
public static ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents)
{
return _resourceManagerWrapper.GetResourceSet (culture, createIfNotExists, tryParents)!;
}

/// <summary>
/// Looks up a set of resources for a particular CultureInfo. This is not useful for most users of the ResourceManager
/// - call GetString() or GetObject() instead. The parameters let you control whether the ResourceSet is created if it
/// hasn't yet been loaded and if parent CultureInfos should be loaded as well for resource inheritance. Allows
/// filtering of resources.
/// </summary>
/// <param name="culture"></param>
/// <param name="createIfNotExists"></param>
/// <param name="tryParents"></param>
/// <param name="filter"></param>
/// <returns></returns>
public static ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents, Func<DictionaryEntry, bool>? filter)
{
return _resourceManagerWrapper.GetResourceSet (culture, createIfNotExists, tryParents, filter)!;
}

/// <summary>
/// Looks up a resource value for a particular name. Looks in the specified CultureInfo, and if not found, all parent
/// CultureInfos.
/// </summary>
/// <param name="name"></param>
/// <param name="culture"></param>
/// <returns>Null if the resource was not found in the current culture or the invariant culture.</returns>
public static string GetString (string name, CultureInfo? culture = null!) { return _resourceManagerWrapper.GetString (name, culture); }
}
112 changes: 112 additions & 0 deletions Terminal.Gui/Resources/ResourceManagerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#nullable enable

using System.Collections;
using System.Globalization;
using System.Resources;

namespace Terminal.Gui.Resources;

internal class ResourceManagerWrapper (ResourceManager resourceManager)
{
private readonly ResourceManager _resourceManager = resourceManager ?? throw new ArgumentNullException (nameof (resourceManager));

// Optionally, expose other ResourceManager methods as needed
public object GetObject (string name, CultureInfo culture = null!)
{
object value = _resourceManager.GetObject (name, culture)!;

if (Equals (culture, CultureInfo.InvariantCulture))
{
return value;
}

if (value is null)
{
value = _resourceManager.GetObject (name, CultureInfo.InvariantCulture)!;
}

return value;
}

public ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents)
{
ResourceSet value = _resourceManager.GetResourceSet (culture, createIfNotExists, tryParents)!;

if (Equals (culture, CultureInfo.InvariantCulture))
{
return value;
}

if (value!.Cast<DictionaryEntry> ().Any ())
{
value = _resourceManager.GetResourceSet (CultureInfo.InvariantCulture, createIfNotExists, tryParents)!;
}

return value;
}

public ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents, Func<DictionaryEntry, bool>? filter)
{
ResourceSet value = _resourceManager.GetResourceSet (culture, createIfNotExists, tryParents)!;

IEnumerable<DictionaryEntry> filteredEntries = value.Cast<DictionaryEntry> ().Where (filter ?? (_ => true));

ResourceSet? filteredValue = ConvertToResourceSet (filteredEntries);

if (Equals (culture, CultureInfo.InvariantCulture))
{
return filteredValue;
}

if (!filteredValue!.Cast<DictionaryEntry> ().Any ())
{
filteredValue = GetResourceSet (CultureInfo.InvariantCulture, createIfNotExists, tryParents, filter)!;
}

return filteredValue;
}

public string GetString (string name, CultureInfo? culture = null!)
{
// Attempt to get the string for the specified culture
string value = _resourceManager.GetString (name, culture)!;

// If it's already using the invariant culture return
if (Equals (culture, CultureInfo.InvariantCulture))
{
return value;
}

// If the string is empty or null, fall back to the invariant culture
if (string.IsNullOrEmpty (value))
{
value = _resourceManager.GetString (name, CultureInfo.InvariantCulture)!;
}

return value;
}

private static ResourceSet? ConvertToResourceSet (IEnumerable<DictionaryEntry> entries)
{
using var memoryStream = new MemoryStream ();

using var resourceWriter = new ResourceWriter (memoryStream);

// Add each DictionaryEntry to the ResourceWriter
foreach (DictionaryEntry entry in entries)
{
resourceWriter.AddResource ((string)entry.Key, entry.Value);
}

// Finish writing to the stream
resourceWriter.Generate ();

// Reset the stream position to the beginning
memoryStream.Position = 0;

// Create a ResourceSet from the MemoryStream
var resourceSet = new ResourceSet (memoryStream);

return resourceSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ namespace Terminal.Gui;

internal partial class HistoryText
{
public class HistoryTextItem : EventArgs
public class HistoryTextItemEventArgs : EventArgs
{
public Point CursorPosition;
public Point FinalCursorPosition;
public bool IsUndoing;
public List<List<RuneCell>> Lines;
public LineStatus LineStatus;
public HistoryTextItem RemovedOnAdded;
public HistoryTextItemEventArgs RemovedOnAdded;

public HistoryTextItem (List<List<RuneCell>> lines, Point curPos, LineStatus linesStatus)
public HistoryTextItemEventArgs (List<List<RuneCell>> lines, Point curPos, LineStatus linesStatus)
{
Lines = lines;
CursorPosition = curPos;
LineStatus = linesStatus;
}

public HistoryTextItem (HistoryTextItem historyTextItem)
public HistoryTextItemEventArgs (HistoryTextItemEventArgs historyTextItem)
{
Lines = new List<List<RuneCell>> (historyTextItem.Lines);
CursorPosition = new Point (historyTextItem.CursorPosition.X, historyTextItem.CursorPosition.Y);
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/TableView/DataTableSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public class DataTableSource : ITableSource
public int Columns => DataTable.Columns.Count;

/// <inheritdoc/>
public string [] ColumnNames => DataTable.Columns.Cast<DataColumn> ().Select (c => c.ColumnName).ToArray ();
public string [] ColumnNames => DataTable.Columns.Cast<DataColumn> ().Select (c => c.Caption).ToArray ();
}
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/TextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ public void ClearAllSelection ()
SetNeedsDisplay ();
}

/// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItem"/> items updating the original text.</summary>
/// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItemEventArgs"/> items updating the original text.</summary>
public void ClearHistoryChanges () { _historyText.Clear (Text); }

/// <summary>Copy the selected text to the clipboard.</summary>
Expand Down Expand Up @@ -1382,7 +1382,7 @@ private Attribute GetReadOnlyColor ()
return new Attribute (cs.Disabled.Foreground, cs.Focus.Background);
}

private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItem obj)
private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItemEventArgs obj)
{
if (obj is null)
{
Expand Down
18 changes: 9 additions & 9 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ public enum LineStatus
Added
}

private readonly List<HistoryTextItem> _historyTextItems = new ();
private readonly List<HistoryTextItemEventArgs> _historyTextItems = new ();
private int _idxHistoryText = -1;
private string? _originalText;
public bool HasHistoryChanges => _idxHistoryText > -1;
Expand Down Expand Up @@ -1304,7 +1304,7 @@ public void Add (List<List<RuneCell>> lines, Point curPos, LineStatus lineStatus
_idxHistoryText++;
}

public event EventHandler<HistoryTextItem>? ChangeText;
public event EventHandler<HistoryTextItemEventArgs>? ChangeText;

public void Clear (string text)
{
Expand All @@ -1324,7 +1324,7 @@ public void Redo ()

_idxHistoryText++;

var historyTextItem = new HistoryTextItem (_historyTextItems [_idxHistoryText]) { IsUndoing = false };
var historyTextItem = new HistoryTextItemEventArgs (_historyTextItems [_idxHistoryText]) { IsUndoing = false };

ProcessChanges (ref historyTextItem);

Expand All @@ -1334,7 +1334,7 @@ public void Redo ()

public void ReplaceLast (List<List<RuneCell>> lines, Point curPos, LineStatus lineStatus)
{
HistoryTextItem? found = _historyTextItems.FindLast (x => x.LineStatus == lineStatus);
HistoryTextItemEventArgs? found = _historyTextItems.FindLast (x => x.LineStatus == lineStatus);

if (found is { })
{
Expand All @@ -1351,17 +1351,17 @@ public void Undo ()

_idxHistoryText--;

var historyTextItem = new HistoryTextItem (_historyTextItems [_idxHistoryText]) { IsUndoing = true };
var historyTextItem = new HistoryTextItemEventArgs (_historyTextItems [_idxHistoryText]) { IsUndoing = true };

ProcessChanges (ref historyTextItem);

IsFromHistory = false;
}
}

private void OnChangeText (HistoryTextItem? lines) { ChangeText?.Invoke (this, lines!); }
private void OnChangeText (HistoryTextItemEventArgs? lines) { ChangeText?.Invoke (this, lines!); }

private void ProcessChanges (ref HistoryTextItem historyTextItem)
private void ProcessChanges (ref HistoryTextItemEventArgs historyTextItem)
{
if (historyTextItem.IsUndoing)
{
Expand Down Expand Up @@ -2859,7 +2859,7 @@ public bool WordWrap
}


/// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItem"/> items updating the original text.</summary>
/// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItemEventArgs"/> items updating the original text.</summary>
public void ClearHistoryChanges () { _historyText?.Clear (Text); }

/// <summary>Closes the contents of the stream into the <see cref="TextView"/>.</summary>
Expand Down Expand Up @@ -4655,7 +4655,7 @@ private string GetSelectedRegion ()
return new ValueTuple<int, int> (line, col);
}

private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItem obj)
private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItemEventArgs obj)
{
SetWrapModel ();

Expand Down
Loading

0 comments on commit 03db057

Please sign in to comment.