Skip to content

Commit

Permalink
Defered construction of gui color components
Browse files Browse the repository at this point in the history
  • Loading branch information
nbollis committed Jan 29, 2025
1 parent 92dbe2a commit 57383ad
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions MetaMorpheus/GuiFunctions/ViewModels/ModForTreeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ModForTreeViewModel : BaseViewModel
private bool _isChecked;
private string _selectedColor;
private SolidColorBrush _colorBrush;
private bool _colorDataLoaded;

#endregion

Expand All @@ -23,10 +24,7 @@ public class ModForTreeViewModel : BaseViewModel

public bool Use
{
get
{
return _isChecked;
}
get => _isChecked;
set
{
_isChecked = value;
Expand All @@ -40,17 +38,26 @@ public bool Use
public Brush Background { get; }
public string SelectedColor
{
get { return _selectedColor; }
get
{
EnsureColorDataLoaded();
return _selectedColor;
}
set
{
_selectedColor = value;
ColorBrush = DrawnSequence.ParseColorBrushFromName(_selectedColor);
_colorDataLoaded = true;
OnPropertyChanged(nameof(SelectedColor));
}
}
public SolidColorBrush ColorBrush
{
get { return _colorBrush; }
get
{
EnsureColorDataLoaded();
return _colorBrush;
}
set
{
_colorBrush = value;
Expand All @@ -69,16 +76,6 @@ public ModForTreeViewModel(string toolTip, bool use, string modName, bool bad, M
Use = use;
ModName = modName;
DisplayName = modName;
if (MetaDrawSettings.ModificationTypeToColor != null)
{
// This if statement prevents a crash from loading a search task modifications not found on launch
// This can occur due to new custom modifications or a mod in the xml database that was not in our initial list
if (!MetaDrawSettings.ModificationTypeToColor.TryGetValue(modName, out OxyColor color))
color = MetaDrawSettings.FallbackColor;
SelectedColor = AddSpaces(MetaDrawSettings.PossibleColors[color]);
ColorBrush = DrawnSequence.ParseColorBrushFromOxyColor(color);
}


if (toolTip.ToLower().Contains("terminal"))
{
Expand All @@ -104,6 +101,27 @@ public ModForTreeViewModel(string toolTip, bool use, string modName, bool bad, M

#endregion

/// <summary>
/// Ensures the color data is loaded. This is necessary because the color data is not loaded until the first time it is accessed.
/// This enables the use of the same control in MetaDraw and Task Windows without loading the color data for task windows.
/// </summary>
private void EnsureColorDataLoaded()
{
if (!_colorDataLoaded)
{
if (MetaDrawSettings.ModificationTypeToColor != null)
{
// This if statement prevents a crash from loading a search task modifications not found on launch
// This can occur due to new custom modifications or a mod in the xml database that was not in our initial list
if (!MetaDrawSettings.ModificationTypeToColor.TryGetValue(ModName, out OxyColor color))
color = MetaDrawSettings.FallbackColor;

Check warning on line 117 in MetaMorpheus/GuiFunctions/ViewModels/ModForTreeViewModel.cs

View check run for this annotation

Codecov / codecov/patch

MetaMorpheus/GuiFunctions/ViewModels/ModForTreeViewModel.cs#L117

Added line #L117 was not covered by tests
_selectedColor = AddSpaces(MetaDrawSettings.PossibleColors[color]);
_colorBrush = DrawnSequence.ParseColorBrushFromOxyColor(color);
}
_colorDataLoaded = true;
}
}

public void SelectionChanged(string newColor)
{
SelectedColor = newColor;
Expand Down

0 comments on commit 57383ad

Please sign in to comment.