Skip to content

Commit

Permalink
Update TryThemeImage to not change assembly name if is a resource c…
Browse files Browse the repository at this point in the history
…omponent.
  • Loading branch information
ricaun committed Jul 22, 2024
1 parent 3f2c152 commit b5a9783
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update `RibbonThemeImageUtils` with public `GetThemeImageSource`.
- Add `RibbonThemePanelUtils` to update the theme for itens in the `RibbonPanel`.
- Update `RibbonPanel` create and remove to update the theme of the itens.
- Update `TryThemeImage` to not change assembly name if is a resource component.
### Tests
- Add `RibbonThemeUtilsTests` to test the theme change event.
- Add `TryThemeImage` tests to replace image with theme.
- Add `ComboBoxMember` tests for `Image`, `Group` and `Current`.
### Example
- Add `AppTheme` to test theme change features for `RibbonItem`.
Expand Down
90 changes: 90 additions & 0 deletions ricaun.Revit.UI.Tests/Themes/RibbonThemeUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,95 @@ public void RibbonThemeUtils_LightDark()
Assert.IsTrue(isLight);
Assert.IsTrue(isDark);
}

[Test]
public void RibbonThemeUtils_TryThemeImage_IsFalse()
{
var lights = new string[]
{
"",
"test.ico",
"light.ico"
};
foreach (var image in lights)
{
Assert.IsFalse(image.TryThemeImage(true, out _));
}

var darks = new string[]
{
"",
"test.ico",
"dark.ico"
};
foreach (var image in darks)
{
Assert.IsFalse(image.TryThemeImage(false, out _));
}
}

[Test]
public void RibbonThemeUtils_TryThemeImage_IsTrue()
{
var lights = new string[]
{
"dark.ico"
};
foreach (var image in lights)
{
Assert.IsTrue(image.TryThemeImage(true, out _));
}

var darks = new string[]
{
"light.ico"
};
foreach (var image in darks)
{
Assert.IsTrue(image.TryThemeImage(false, out _));
}
}

[TestCase("ricaun")]
[TestCase("ricaun.Dark")]
[TestCase("ricaun.Light")]
public void RibbonThemeUtils_TryThemeImage_Component(string assemblyName)
{
var imageLight = $"pack://application:,,,/{assemblyName};component/Images/Image_Light.ico";
var imageDark = $"pack://application:,,,/{assemblyName};component/Images/Image_Dark.ico";

imageLight = imageLight.ToLowerInvariant();
imageDark = imageDark.ToLowerInvariant();

var isLight = imageDark.TryThemeImage(true, out string imageDarkToLight);
var isDark = imageLight.TryThemeImage(false, out string imageLightToDark);

Assert.IsTrue(isLight);
Assert.IsTrue(isDark);

Assert.AreEqual(imageLight, imageDarkToLight);
Assert.AreEqual(imageDark, imageLightToDark);
}

[TestCase("https://ricaun.com")]
[TestCase("https://github.com")]
public void RibbonThemeUtils_TryThemeImage_Url(string url)
{
var imageLight = $"{url}/Images/Image_Light.ico";
var imageDark = $"{url}/Images/Image_Dark.ico";

imageLight = imageLight.ToLowerInvariant();
imageDark = imageDark.ToLowerInvariant();

var isLight = imageDark.TryThemeImage(true, out string imageDarkToLight);
var isDark = imageLight.TryThemeImage(false, out string imageLightToDark);

Assert.IsTrue(isLight);
Assert.IsTrue(isDark);

Assert.AreEqual(imageLight, imageDarkToLight);
Assert.AreEqual(imageDark, imageLightToDark);
}

}
}
12 changes: 12 additions & 0 deletions ricaun.Revit.UI/Utils/RibbonThemeImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ internal static bool TryThemeImage(this string image, bool isLight, out string i

if (image.IndexOf(findThemeName, System.StringComparison.InvariantCultureIgnoreCase) != -1)
{
image = image.ToLowerInvariant();

if (image.StartsWith("pack://application:,,,/"))
{
// split to not change the assembly name if contains 'findThemeName'.
var components = image.Split(';');
components[components.Length - 1] = components[components.Length - 1].Replace(findThemeName, replaceThemeName);

imageTheme = string.Join(";", components);
return true;
}

imageTheme = image.Replace(findThemeName, replaceThemeName);
return true;
}
Expand Down

0 comments on commit b5a9783

Please sign in to comment.