From 8af57938099169554e9afedd22da4e9941ec21db Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Sun, 7 Jul 2024 21:17:42 -0300 Subject: [PATCH] Add `RibbonThemePanelUtils` to update the theme for itens in the `RibbonPanel` --- CHANGELOG.md | 2 + .../Resources/Box-Grey-Dark.ico | Bin 0 -> 424 bytes .../Resources/Box-Grey-Light.ico | Bin 0 -> 430 bytes ricaun.Revit.UI.Example/Revit/AppTheme.cs | 65 +++++++++++++--- .../ricaun.Revit.UI.Example.csproj | 4 + ricaun.Revit.UI/RibbonPanelExtension.cs | 6 +- .../Utils/RibbonThemePanelUtils.cs | 70 ++++++++++++++++++ 7 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 ricaun.Revit.UI.Example/Resources/Box-Grey-Dark.ico create mode 100644 ricaun.Revit.UI.Example/Resources/Box-Grey-Light.ico create mode 100644 ricaun.Revit.UI/Utils/RibbonThemePanelUtils.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f13c7f..aca61cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/ricaun.Revit.UI.Example/Resources/Box-Grey-Dark.ico b/ricaun.Revit.UI.Example/Resources/Box-Grey-Dark.ico new file mode 100644 index 0000000000000000000000000000000000000000..c41c70ece4069e9714a5f8509083af264474e5ba GIT binary patch literal 424 zcmZQzU}Ruo5D;Jh(h3ZF85kJUfLK8R!aoXRKL+B?06%wLE-5Y`lh@P3B?w3Zbub8U zumMT=&;JvF6k~CayA#8@b22Z19F}xPUq=Rpjs4tz5?O(K&H|6fVg?4j!ywFfJby(B zP*AeOHKHUqKdq!Zu_%?Hyu4g5GcUV1Ik6yBFTW^#_B$IXpdwvQ7sn6_|KtT+J1Rdv zt1mJL`MrWsp}}{O1kY8wEC!B*$trBlTlu+}Pq6dwFlbnBJ-*R4Dh8;L!PC{xWt~$( z6BfrS0HqbcjxVmN`U2$8$nmBi$49uoJ-1Pi!GMR!@Z0?fnUB=0Z?yjFlX~isd;i!! cTcG6#aDeeZ+)0LN^TNC0MF;#r;xJDD0JVy89smFU literal 0 HcmV?d00001 diff --git a/ricaun.Revit.UI.Example/Resources/Box-Grey-Light.ico b/ricaun.Revit.UI.Example/Resources/Box-Grey-Light.ico new file mode 100644 index 0000000000000000000000000000000000000000..83bba94c18474ffcf0f636e67e0e890c465c43b5 GIT binary patch literal 430 zcmZQzU}Ruo5D;Jh(h3Z_85kJUfLK8R!aoaSKLp~=06%wLE-5Y`lh@P3B?w3Zbub8U zumMT=&;JvF6k~CayA#8@b22Z19F}xPUq=Rpjs4tz5?O(K&H|6fVg?4j!ywFfJby(B zP*AeOHKHUqKdq!Zu_%?Hyu4g5GcUV1Ik6yBFTW^#_B$IXpdxKg7sn6_|KtT+E7q*} zJje-mY9L$E-((>3mx(u8bOnAR("Revit") + // .SetLargeImage(REVIT); + + //ribbonPanel.CreatePushButton("Base64") + // .SetLargeImage(Base64Image); + ribbonPanel.CreatePushButton("Light") .SetLargeImage(LIGHT); ribbonPanel.CreatePushButton("Dark") - .SetLargeImage(DARK); - string stringNull = null; - ribbonPanel.CreatePushButton("Null") - .SetLargeImage(LIGHT) - .SetLargeImage(stringNull); + .SetLargeImage(LIGHT); + + ribbonPanel.CreatePushButton(".") + .SetLargeImage(LIGHT_GREEN) + .SetImage(LIGHT_RED); + + ribbonPanel.CreatePushButton(".") + .SetLargeImage(LIGHT_GREEN) + .SetImage(LIGHT_RED) + .SetItemSize(Autodesk.Windows.RibbonItemSize.Standard); + + ribbonPanel.RowStackedItems( + ribbonPanel.CreatePushButton("Grey") + .SetLargeImage("Resources/Box-Grey-Light.ico"), + ribbonPanel.CreatePushButton("Grey") + .SetLargeImage("Resources/Box-Grey-Light.ico"), + ribbonPanel.CreatePushButton("Grey") + .SetLargeImage("Resources/Box-Grey-Light.ico") + ); + ribbonPanel.RowLargeStackedItems( + ribbonPanel.CreatePushButton("Grey") + .SetLargeImage("Resources/Box-Grey-Light.ico"), + ribbonPanel.CreatePushButton("Grey") + .SetLargeImage("Resources/Box-Grey-Light.ico") + ); + + ribbonPanel.AddSeparator(); ribbonPanel.FlowStackedItems( - ribbonPanel.CreatePushButton("1").SetLargeImage(LIGHT), - ribbonPanel.CreatePushButton("2").SetLargeImage(DARK), - ribbonPanel.CreatePushButton("3").SetLargeImage(LIGHT) + ribbonPanel.CreatePushButton("1").SetLargeImage(LIGHT_RED), + ribbonPanel.CreatePushButton("2").SetLargeImage(DARK_RED), + ribbonPanel.CreatePushButton("3").SetLargeImage(LIGHT_BLUE), + ribbonPanel.CreatePushButton("4").SetLargeImage(DARK_GREEN), + ribbonPanel.CreatePushButton("5").SetLargeImage(LIGHT_GREEN), + ribbonPanel.CreatePushButton("6").SetLargeImage(DARK_BLUE) ); ribbonPanel.AddSeparator(); @@ -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); diff --git a/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj b/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj index 910951c..cb6fb12 100644 --- a/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj +++ b/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj @@ -84,6 +84,8 @@ + + @@ -112,6 +114,8 @@ + + diff --git a/ricaun.Revit.UI/RibbonPanelExtension.cs b/ricaun.Revit.UI/RibbonPanelExtension.cs index 0e23734..38f2e25 100644 --- a/ricaun.Revit.UI/RibbonPanelExtension.cs +++ b/ricaun.Revit.UI/RibbonPanelExtension.cs @@ -1,4 +1,5 @@ using Autodesk.Revit.UI; +using ricaun.Revit.UI.Utils; using System; using System.Collections.Generic; using System.Linq; @@ -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(); } /// @@ -72,7 +73,7 @@ public static RibbonPanel CreatePanel(this UIControlledApplication application, ribbonPanel.Title = panelName; } } - return ribbonPanel; + return ribbonPanel.ThemeChangeEnable(); } /// @@ -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; diff --git a/ricaun.Revit.UI/Utils/RibbonThemePanelUtils.cs b/ricaun.Revit.UI/Utils/RibbonThemePanelUtils.cs new file mode 100644 index 0000000..ca6bfaf --- /dev/null +++ b/ricaun.Revit.UI/Utils/RibbonThemePanelUtils.cs @@ -0,0 +1,70 @@ +using Autodesk.Revit.UI; +using System.Collections.Generic; + +namespace ricaun.Revit.UI.Utils +{ + internal static class RibbonThemePanelUtils + { + private static List RibbonThemePanels = new List(); + 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 { } + } + } +}