Skip to content

Commit

Permalink
Speed up task window launch (#2461)
Browse files Browse the repository at this point in the history
* Simplify color parsing and add fallback color handling

Simplified color parsing methods by using Color.FromArgb directly.
Updated ParseColorBrushFromName and ParseOxyColorFromName to handle
invalid color names with fallback colors. Added tests in
MetaDrawSettingsAndViewsTest.cs to verify fallback color behavior.

* Update color setting logic in TreeViewModel classes

Updated `SelectedColor` setting in `CoverageTypeForTreeViewModel.cs`, `IonForTreeViewModel.cs`, and `ModForTreeViewModel.cs` to use `MetaDrawSettings.PossibleColors` dictionary instead of `GetColorName()`. In `ModForTreeViewModel.cs`, added fallback to `MetaDrawSettings.FallbackColor` if color is not found in `ModificationTypeToColor` dictionary, replacing the previous default of `OxyColors.Aqua`.

* Defered construction of gui color components
  • Loading branch information
nbollis authored Jan 30, 2025
1 parent bd72779 commit e6cf8e7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
15 changes: 8 additions & 7 deletions MetaMorpheus/GuiFunctions/MetaDraw/DrawnSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,25 +381,26 @@ public static void ClearCanvas(Canvas cav)

public static SolidColorBrush ParseColorBrushFromOxyColor(OxyColor color)
{
var colorVal = color.ToByteString().Split(',');
return new SolidColorBrush(System.Windows.Media.Color.FromArgb(Byte.Parse(colorVal[0]), Byte.Parse(colorVal[1]), Byte.Parse(colorVal[2]), Byte.Parse(colorVal[3])));
return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
}

public static SolidColorBrush ParseColorBrushFromName(string name)
{
OxyColor color = MetaDrawSettings.PossibleColors.Keys.Where(p => p.GetColorName().Equals(name.Replace(" ", ""))).First();
return ParseColorBrushFromOxyColor(color);
string cleanedName = name.Replace(" ", "");
var foundColor = MetaDrawSettings.PossibleColors.FirstOrDefault(p => p.Value == cleanedName).Key;
return ParseColorBrushFromOxyColor(foundColor == default ? MetaDrawSettings.FallbackColor : foundColor);
}

public static OxyColor ParseOxyColorFromName(string name)
{
return MetaDrawSettings.PossibleColors.Keys.Where(p => p.GetColorName().Equals(name.Replace(" ", ""))).First();
string cleanedName = name.Replace(" ", "");
var foundColor = MetaDrawSettings.PossibleColors.FirstOrDefault(p => p.Value == cleanedName).Key;
return foundColor == default ? MetaDrawSettings.FallbackColor : foundColor;
}

public static Color ParseColorFromOxyColor(OxyColor color)
{
var colorVal = color.ToByteString().Split(',');
return System.Windows.Media.Color.FromArgb(Byte.Parse(colorVal[0]), Byte.Parse(colorVal[1]), Byte.Parse(colorVal[2]), Byte.Parse(colorVal[3]));
return Color.FromArgb(color.A, color.R, color.G, color.B);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions MetaMorpheus/GuiFunctions/MetaDraw/MetaDrawSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static class MetaDrawSettings
public static bool AnnotationBold { get; set; } = false;
public static bool DisplayInternalIons { get; set; } = true;
public static bool DisplayInternalIonAnnotations { get; set; }= true;
public static OxyColor FallbackColor { get; } = OxyColors.Aqua;
public static Dictionary<OxyColor, string> PossibleColors { get; set; }
public static Dictionary<ProductType, OxyColor> ProductTypeToColor { get; set; }
public static Dictionary<ProductType, OxyColor> BetaProductTypeToColor { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CoverageTypeForTreeViewModel(string name)
{
Name = name;
OxyColor color = MetaDrawSettings.CoverageTypeToColor[name];
SelectedColor = AddSpaces(color.GetColorName());
SelectedColor = AddSpaces(MetaDrawSettings.PossibleColors[color]);
ColorBrush = DrawnSequence.ParseColorBrushFromOxyColor(color);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IonForTreeViewModel(ProductType type, bool beta)
color = MetaDrawSettings.BetaProductTypeToColor[IonType];
else
color = MetaDrawSettings.ProductTypeToColor[IonType];
SelectedColor = AddSpaces(color.GetColorName());
SelectedColor = AddSpaces(MetaDrawSettings.PossibleColors[color]);
ColorBrush = DrawnSequence.ParseColorBrushFromOxyColor(color);
}

Expand Down
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 = OxyColors.Aqua;
SelectedColor = AddSpaces(color.GetColorName());
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;
_selectedColor = AddSpaces(MetaDrawSettings.PossibleColors[color]);
_colorBrush = DrawnSequence.ParseColorBrushFromOxyColor(color);
}
_colorDataLoaded = true;
}
}

public void SelectionChanged(string newColor)
{
SelectedColor = newColor;
Expand Down
4 changes: 4 additions & 0 deletions MetaMorpheus/Test/MetaDraw/MetaDrawSettingsAndViewsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,13 @@ public static void TestDrawnSequenceColorConversions()

var colorBrushfromName = DrawnSequence.ParseColorBrushFromName(oxyBlue.GetColorName());
Assert.That(colorBrushfromName.Color == brushBlue.Color);
var colorBrushfromNameBad = DrawnSequence.ParseColorBrushFromName("humbug");
Assert.That(colorBrushfromNameBad.Color == Colors.Aqua);

var oxyFromName = DrawnSequence.ParseOxyColorFromName(oxyBlue.GetColorName());
Assert.That(oxyFromName == oxyBlue);
var oxyFromNameBad = DrawnSequence.ParseOxyColorFromName("gobbledygook");
Assert.That(oxyFromNameBad == MetaDrawSettings.FallbackColor);

var colorFromOxy = DrawnSequence.ParseColorFromOxyColor(oxyBlue);
Assert.That(colorFromOxy == colorBlue);
Expand Down

0 comments on commit e6cf8e7

Please sign in to comment.