Skip to content

Commit

Permalink
Add RibbonThemePanelUtils to update the theme for itens in the `Rib…
Browse files Browse the repository at this point in the history
…bonPanel`
  • Loading branch information
ricaun committed Jul 8, 2024
1 parent aa9e1ff commit 8af5793
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update `SetImage` to work with `ComboBoxMember`
- Add `CreateComboBoxMember` to create `ComboBoxMember`.
- Add `RibbonThemeBitmapUtils` to change theme for `BitmapSource`.
- Add `RibbonThemePanelUtils` to update the theme for itens in the `RibbonPanel`.
- Update `RibbonPanel` create and remove to update the theme of the itens.
### Tests
- Add `RibbonThemeUtilsTests` to test the theme change event.
- Add `ComboBoxMember` tests for `Image`, `Group` and `Current`.
Expand Down
Binary file added ricaun.Revit.UI.Example/Resources/Box-Grey-Dark.ico
Binary file not shown.
Binary file added ricaun.Revit.UI.Example/Resources/Box-Grey-Light.ico
Binary file not shown.
65 changes: 55 additions & 10 deletions ricaun.Revit.UI.Example/Revit/AppTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,66 @@ 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";
const string LIGHT = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Grey-Light.ico";
const string DARK = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Grey-Dark.ico";
const string LIGHT_RED = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Red-Light.ico";
const string DARK_RED = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Red-Dark.ico";
const string LIGHT_GREEN = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Green-Light.ico";
const string DARK_GREEN = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Green-Dark.ico";
const string LIGHT_BLUE = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Blue-Light.ico";
const string DARK_BLUE = "https://github.com/ricaun-io/Autodesk.Icon.Example/releases/download/1.0.1-alpha/Box-Blue-Dark.ico";
const string REVIT = "/UIFrameworkRes;component/ribbon/images/revit.ico";
const string Base64Image = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAHYcAAB2HAY/l8WUAAABOSURBVEhLtccxDQAwDMCw8odRdIWxP7cn+fHM3l8913M913M913M913M913M913M913M913M913M913M913M913M913M913M913O9tfcAG98oW3bd33wAAAAASUVORK5CYII=";

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

//ribbonPanel.CreatePushButton<CommandTheme>("Revit")
// .SetLargeImage(REVIT);

//ribbonPanel.CreatePushButton<CommandTheme>("Base64")
// .SetLargeImage(Base64Image);

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

ribbonPanel.CreatePushButton<CommandTheme>(".")
.SetLargeImage(LIGHT_GREEN)
.SetImage(LIGHT_RED);

ribbonPanel.CreatePushButton<CommandTheme>(".")
.SetLargeImage(LIGHT_GREEN)
.SetImage(LIGHT_RED)
.SetItemSize(Autodesk.Windows.RibbonItemSize.Standard);

ribbonPanel.RowStackedItems(
ribbonPanel.CreatePushButton<CommandTheme>("Grey")
.SetLargeImage("Resources/Box-Grey-Light.ico"),
ribbonPanel.CreatePushButton<CommandTheme>("Grey")
.SetLargeImage("Resources/Box-Grey-Light.ico"),
ribbonPanel.CreatePushButton<CommandTheme>("Grey")
.SetLargeImage("Resources/Box-Grey-Light.ico")
);
ribbonPanel.RowLargeStackedItems(
ribbonPanel.CreatePushButton<CommandTheme>("Grey")
.SetLargeImage("Resources/Box-Grey-Light.ico"),
ribbonPanel.CreatePushButton<CommandTheme>("Grey")
.SetLargeImage("Resources/Box-Grey-Light.ico")
);

ribbonPanel.AddSeparator();

ribbonPanel.FlowStackedItems(
ribbonPanel.CreatePushButton<CommandTheme>("1").SetLargeImage(LIGHT),
ribbonPanel.CreatePushButton<CommandTheme>("2").SetLargeImage(DARK),
ribbonPanel.CreatePushButton<CommandTheme>("3").SetLargeImage(LIGHT)
ribbonPanel.CreatePushButton<CommandTheme>("1").SetLargeImage(LIGHT_RED),
ribbonPanel.CreatePushButton<CommandTheme>("2").SetLargeImage(DARK_RED),
ribbonPanel.CreatePushButton<CommandTheme>("3").SetLargeImage(LIGHT_BLUE),
ribbonPanel.CreatePushButton<CommandTheme>("4").SetLargeImage(DARK_GREEN),
ribbonPanel.CreatePushButton<CommandTheme>("5").SetLargeImage(LIGHT_GREEN),
ribbonPanel.CreatePushButton<CommandTheme>("6").SetLargeImage(DARK_BLUE)
);

ribbonPanel.AddSeparator();
Expand All @@ -44,6 +83,12 @@ public Result OnStartup(UIControlledApplication application)
.SetLargeImage(LIGHT);
comboBox.CreateComboBoxMember("ComboBox")
.SetLargeImage(LIGHT);
comboBox.CreateComboBoxMember("Red")
.SetLargeImage(LIGHT_RED);
comboBox.CreateComboBoxMember("Green")
.SetLargeImage(LIGHT_GREEN);
comboBox.CreateComboBoxMember("Blue")
.SetLargeImage(LIGHT_BLUE);

ribbonPanel.RowStackedItems(pushButton, textBox, comboBox);

Expand Down
4 changes: 4 additions & 0 deletions ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Resources\Box-Grey-Dark.ico" />
<None Remove="Resources\Box-Grey-Light.ico" />
<None Remove="Resources\icon.png" />
<None Remove="Resources\Revit.ico" />
</ItemGroup>
Expand Down Expand Up @@ -112,6 +114,8 @@
</ItemGroup>

<ItemGroup>
<Resource Include="Resources\Box-Grey-Dark.ico" />
<Resource Include="Resources\Box-Grey-Light.ico" />
<Resource Include="Resources\icon.png" />
<Resource Include="Resources\Revit.ico" />
</ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions ricaun.Revit.UI/RibbonPanelExtension.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;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static RibbonPanel CreatePanel(this UIControlledApplication application,
ribbonPanel = application.CreateRibbonPanel(RibbonSafeExtension.SafeRibbonPanelName(panelName));
ribbonPanel.Title = panelName;
}
return ribbonPanel;
return ribbonPanel.ThemeChangeEnable();
}

/// <summary>
Expand Down Expand Up @@ -72,7 +73,7 @@ public static RibbonPanel CreatePanel(this UIControlledApplication application,
ribbonPanel.Title = panelName;
}
}
return ribbonPanel;
return ribbonPanel.ThemeChangeEnable();
}

/// <summary>
Expand Down Expand Up @@ -118,6 +119,7 @@ public static RibbonPanel Remove(this RibbonPanel ribbonPanel)
if (ribbonPanel is null)
return ribbonPanel;

ribbonPanel.ThemeChangeDisable();
ribbonPanel.Visible = false;
ribbonPanel.Enabled = false;

Expand Down
70 changes: 70 additions & 0 deletions ricaun.Revit.UI/Utils/RibbonThemePanelUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Autodesk.Revit.UI;
using System.Collections.Generic;

namespace ricaun.Revit.UI.Utils
{
internal static class RibbonThemePanelUtils
{
private static List<RibbonPanel> RibbonThemePanels = new List<RibbonPanel>();
static RibbonThemePanelUtils()
{
RibbonThemeUtils.ThemeChanged += RibbonThemeUtils_ThemeChanged;
}
internal static void Dispose()
{
RibbonThemeUtils.ThemeChanged -= RibbonThemeUtils_ThemeChanged;
RibbonThemePanels.Clear();
}
internal static RibbonPanel ThemeChangeDisable(this RibbonPanel ribbonPanel) => ThemeChangeEnable(ribbonPanel, false);
internal static RibbonPanel ThemeChangeEnable(this RibbonPanel ribbonPanel, bool enable = true)
{
if (ribbonPanel is null) return ribbonPanel;

RibbonThemePanels.Remove(ribbonPanel);

if (enable)
{
RibbonThemePanels.Add(ribbonPanel);
}

return ribbonPanel;
}

private static void RibbonThemeUtils_ThemeChanged(object sender, ThemeChangedEventArgs e)
{
foreach (var ribbonPanel in RibbonThemePanels)
{
foreach (var ribbonItem in ribbonPanel.GetRibbonItems())
{
var isLight = e.IsLight;
UpdateImageThemes(ribbonItem, isLight);
}
}
}

private static void UpdateImageThemes(RibbonItem ribbonItem, bool isLight)
{
try
{
switch (ribbonItem)
{
case RibbonButton ribbonButton:
var image = ribbonButton.Image;
ribbonButton.SetLargeImage(ribbonButton.LargeImage);
if (image is not null) ribbonButton.SetImage(image);
break;
case ComboBox comboBox:
comboBox.SetImage(comboBox.Image);
break;
case ComboBoxMember comboBoxMember:
comboBoxMember.SetImage(comboBoxMember.Image);
break;
case TextBox textBox:
textBox.SetImage(textBox.Image);
break;
}
}
catch { }
}
}
}

0 comments on commit 8af5793

Please sign in to comment.