Skip to content

Commit

Permalink
Add RibbonThemeBitmapUtils to change theme for BitmapSource
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Jul 7, 2024
1 parent e0c9e18 commit aa9e1ff
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Change `GetRibbonItem` to `GetRibbonItem_Alternative` to fix null when panel is removed.
- Update `SetImage` to work with `ComboBoxMember`
- Add `CreateComboBoxMember` to create `ComboBoxMember`.
- Add `RibbonThemeBitmapUtils` to change theme for `BitmapSource`.
### Tests
- Add `RibbonThemeUtilsTests` to test the theme change event.
- Add `ComboBoxMember` tests for `Image`, `Group` and `Current`.
### Example
- Add `AppTheme` to test theme change features for `RibbonItem`.

## [0.6.2] / 2024-01-09 - 2024-02-05
### Features
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.6.3-beta.1</Version>
<Version>0.6.3-beta.2</Version>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion ricaun.Revit.UI.Example/Revit/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ricaun.Revit.UI.Example.Revit
{
[AppLoader]
//[AppLoader]
//[Obsolete]
public class App : IExternalApplication
{
Expand Down
2 changes: 1 addition & 1 deletion ricaun.Revit.UI.Example/Revit/AppList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ricaun.Revit.UI.Example.Revit
{
[AppLoader]
//[AppLoader]
public class AppList : IExternalApplication
{
private static RibbonPanel ribbonPanel;
Expand Down
2 changes: 1 addition & 1 deletion ricaun.Revit.UI.Example/Revit/AppResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ricaun.Revit.UI.Example.Revit
{
[AppLoader]
//[AppLoader]
public class AppResource : IExternalApplication
{
private static RibbonPanel ribbonPanel;
Expand Down
2 changes: 1 addition & 1 deletion ricaun.Revit.UI.Example/Revit/AppStacked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ricaun.Revit.UI.Example.Revit
{
[AppLoader]
//[AppLoader]
public class AppStacked : IExternalApplication
{
private static RibbonPanel ribbonPanel;
Expand Down
76 changes: 76 additions & 0 deletions ricaun.Revit.UI.Example/Revit/AppTheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using ricaun.Revit.UI.Utils;

namespace ricaun.Revit.UI.Example.Revit
{
[AppLoader]
public class AppTheme : IExternalApplication
{
const string LIGHT = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Blue-Light.ico";
const string DARK = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Blue-Dark.ico";

private RibbonPanel ribbonPanel;
public Result OnStartup(UIControlledApplication application)
{
ribbonPanel = application.CreatePanel("Theme");

ribbonPanel.CreatePushButton<CommandTheme>("Light")
.SetLargeImage(LIGHT);
ribbonPanel.CreatePushButton<CommandTheme>("Dark")
.SetLargeImage(DARK);
string stringNull = null;
ribbonPanel.CreatePushButton<CommandTheme>("Null")
.SetLargeImage(LIGHT)
.SetLargeImage(stringNull);

ribbonPanel.FlowStackedItems(
ribbonPanel.CreatePushButton<CommandTheme>("1").SetLargeImage(LIGHT),
ribbonPanel.CreatePushButton<CommandTheme>("2").SetLargeImage(DARK),
ribbonPanel.CreatePushButton<CommandTheme>("3").SetLargeImage(LIGHT)
);

ribbonPanel.AddSeparator();

var pushButton = ribbonPanel.CreatePushButton<CommandTheme>("Theme")
.SetLargeImage(LIGHT);

var textBox = ribbonPanel.CreateTextBox()
.SetPromptText("TextBox")
.SetLargeImage(LIGHT);

var comboBox = ribbonPanel.CreateComboBox()
.SetLargeImage(LIGHT);
comboBox.CreateComboBoxMember("ComboBox")
.SetLargeImage(LIGHT);

ribbonPanel.RowStackedItems(pushButton, textBox, comboBox);

return Result.Succeeded;
}

public Result OnShutdown(UIControlledApplication application)
{
ribbonPanel?.Remove();
return Result.Succeeded;
}

[Transaction(TransactionMode.Manual)]
public class CommandTheme : IExternalCommand, IExternalCommandAvailability
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
{
UIApplication uiapp = commandData.Application;

UIThemeManager.CurrentTheme = UIThemeManager.CurrentTheme == UITheme.Light ? UITheme.Dark : UITheme.Light;
return Result.Succeeded;
}

public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
{
return true;
}
}
}
}
5 changes: 5 additions & 0 deletions ricaun.Revit.UI/RibbonItemDataExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Autodesk.Revit.UI;
using ricaun.Revit.UI.Utils;
using System.Windows.Media;

namespace ricaun.Revit.UI
Expand Down Expand Up @@ -150,6 +151,8 @@ public static TRibbonItem SetLargeImage<TRibbonItem>(this TRibbonItem ribbonItem
/// <returns></returns>
public static TRibbonItem SetImage<TRibbonItem>(this TRibbonItem ribbonItem, ImageSource image) where TRibbonItem : RibbonItemData
{
image = image.GetThemeImageSource(RibbonThemeUtils.IsLight);

if (ribbonItem is ButtonData ribbonButton)
ribbonButton.Image = image?.GetBitmapFrame(16, (frame) => { ribbonButton.Image = frame; });

Expand All @@ -174,6 +177,8 @@ public static TRibbonItem SetImage<TRibbonItem>(this TRibbonItem ribbonItem, Ima
/// <returns></returns>
public static TRibbonItem SetLargeImage<TRibbonItem>(this TRibbonItem ribbonItem, ImageSource largeImage) where TRibbonItem : RibbonItemData
{
largeImage = largeImage.GetThemeImageSource(RibbonThemeUtils.IsLight);

if (ribbonItem is ButtonData ribbonButton)
{
ribbonButton.LargeImage = largeImage?.GetBitmapFrame(32, (frame) => { ribbonButton.LargeImage = frame; });
Expand Down
5 changes: 5 additions & 0 deletions ricaun.Revit.UI/RibbonItemExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Autodesk.Revit.UI;
using ricaun.Revit.UI.Utils;
using System.Windows.Media;

namespace ricaun.Revit.UI
Expand Down Expand Up @@ -192,6 +193,8 @@ public static TRibbonItem SetLargeImage<TRibbonItem>(this TRibbonItem ribbonItem
/// <returns></returns>
public static TRibbonItem SetImage<TRibbonItem>(this TRibbonItem ribbonItem, ImageSource image) where TRibbonItem : RibbonItem
{
image = image.GetThemeImageSource(RibbonThemeUtils.IsLight);

if (ribbonItem is RibbonButton ribbonButton)
ribbonButton.Image = image?.GetBitmapFrame(16, (frame) => { ribbonButton.Image = frame; });

Expand All @@ -217,6 +220,8 @@ public static TRibbonItem SetImage<TRibbonItem>(this TRibbonItem ribbonItem, Ima
/// <remarks>When <see cref="ComboBox"/>, <see cref="ComboBoxMember"/> or <see cref="TextBox"/> does not have LargeImage, the Image is changed instead.</remarks>
public static TRibbonItem SetLargeImage<TRibbonItem>(this TRibbonItem ribbonItem, ImageSource largeImage) where TRibbonItem : RibbonItem
{
largeImage = largeImage.GetThemeImageSource(RibbonThemeUtils.IsLight);

if (ribbonItem is RibbonButton ribbonButton)
{
ribbonButton.LargeImage = largeImage?.GetBitmapFrame(32, (frame) => { ribbonButton.LargeImage = frame; });
Expand Down
53 changes: 53 additions & 0 deletions ricaun.Revit.UI/Utils/RibbonThemeBitmapUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ricaun.Revit.UI.Utils
{
internal static class RibbonThemeBitmapUtils
{
private const string NAME_DARK = "dark";
private const string NAME_LIGHT = "light";

internal static TImageSource GetThemeImageSource<TImageSource>(this TImageSource imageSource, bool isLight = true) where TImageSource : ImageSource
{
if (imageSource.GetSourceName().TryThemeImage(isLight, out string imageTheme))
{
if (imageTheme.GetBitmapSource() is TImageSource themeImageSource)
return themeImageSource;
}

return imageSource;
}

internal static bool TryThemeImage(this string image, bool isLight, out string imageTheme)
{
imageTheme = string.Empty;

if (string.IsNullOrEmpty(image))
return false;

var findThemeName = !isLight ? NAME_LIGHT : NAME_DARK;
var replaceThemeName = isLight ? NAME_LIGHT : NAME_DARK;

if (image.IndexOf(findThemeName, System.StringComparison.InvariantCultureIgnoreCase) != -1)
{
imageTheme = image.Replace(findThemeName, replaceThemeName);
return true;
}

return false;
}
internal static string GetSourceName(this ImageSource imageSource)
{
if (imageSource is TransformedBitmap transformedBitmap)
{
return transformedBitmap.Source.ToString().ToLowerInvariant();
}
if (imageSource is BitmapFrame bitmapFrame)
{
return bitmapFrame.Decoder.ToString().ToLowerInvariant();
}
return null;
}
}
}
1 change: 1 addition & 0 deletions ricaun.Revit.UI/ricaun.Revit.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@

<ItemGroup>
<InternalsVisibleTo Include="$(PackageId).Tests" />
<InternalsVisibleTo Include="$(PackageId).Example" />
</ItemGroup>

</Project>

0 comments on commit aa9e1ff

Please sign in to comment.