Skip to content

Commit

Permalink
Add RibbonThemeUtils for ThemeChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Jul 6, 2024
1 parent 1318178 commit 15d4bf6
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [0.6.3] / 2024-07-06
### Features
- `RibbonThemeUtils` to change the theme of the Ribbon. (Revit 2019+)
- Set project configuration to support `net47` and `net48`.
### Updated
- Change `GetRibbonItem` to `GetRibbonItem_Alternative` to fix null when panel is removed.
### Tests
- Add `RibbonThemeUtilsTests` to test the theme change event.

## [0.6.2] / 2024-01-09 - 2024-02-05
### Features
Expand Down Expand Up @@ -342,6 +346,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- First Release

[vNext]: ../../compare/1.0.0...HEAD
[0.6.3]: ../../compare/0.6.2...0.6.3
[0.6.2]: ../../compare/0.6.1...0.6.2
[0.6.1]: ../../compare/0.6.0...0.6.1
[0.6.0]: ../../compare/0.5.7...0.6.0
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-alpha</Version>
<Version>0.6.3-beta</Version>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

<!-- RevitVersion -->
<PropertyGroup>
<TargetFrameworks>net46;net8.0-windows</TargetFrameworks>
<TargetFrameworks>net48;net8.0-windows</TargetFrameworks>
</PropertyGroup>
<Choose>
<When Condition="$(TargetFramework.StartsWith('net4'))">
<PropertyGroup>
<RevitVersion>2017</RevitVersion>
<RevitVersion>2021</RevitVersion>
</PropertyGroup>
</When>
<Otherwise>
Expand Down
39 changes: 39 additions & 0 deletions ricaun.Revit.UI.Tests/Themes/RibbonThemeUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using ricaun.Revit.UI.Utils;
using NUnit.Framework;
using System;

namespace ricaun.Revit.UI.Tests.Themes
{
public class RibbonThemeUtilsTests
{
[Test]
public void RibbonThemeUtils_ThemeChangedEventHandler()
{
bool themeChanged = false;
ThemeChangedEventHandler RibbonThemeUtils_ThemeChanged = (object sender, ThemeChangedEventArgs e) =>
{
themeChanged = true;
Console.WriteLine($"ThemeChangedEvent {e.IsLight}");
};

try
{
RibbonThemeUtils.ThemeChanged += RibbonThemeUtils_ThemeChanged;
RibbonThemeUtils.ThemeChangedTest();
Assert.IsTrue(themeChanged);
}
finally
{
RibbonThemeUtils.ThemeChanged -= RibbonThemeUtils_ThemeChanged;
}
}

[Test]
public void ThemeChangedEventArgs_Constructor()
{
var themeChangedEventArgs = new ThemeChangedEventArgs(true);
Assert.IsTrue(themeChangedEventArgs.IsLight);
Assert.IsFalse(themeChangedEventArgs.IsDark);
}
}
}
2 changes: 1 addition & 1 deletion ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="ricaun.RevitTest.TestAdapter" Version="*-*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" Condition="!$(TargetFramework.StartsWith('net4'))"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" Condition="!$(TargetFramework.StartsWith('net4'))" />
</ItemGroup>

<ItemGroup>
Expand Down
122 changes: 122 additions & 0 deletions ricaun.Revit.UI/Utils/RibbonThemeUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@

using System;

namespace ricaun.Revit.UI.Utils
{
/// <summary>
/// ThemeChangedEventArgs
/// </summary>
public sealed class ThemeChangedEventArgs : System.EventArgs
{
internal ThemeChangedEventArgs(bool isLight)
{
IsLight = isLight;
}
/// <summary>
/// Theme is Light
/// </summary>
public bool IsLight { get; private set; }
/// <summary>
/// Theme is Dark
/// </summary>
public bool IsDark => !IsLight;
}
/// <summary>
/// ThemeChangedEventHandler for RibbonThemeUtils
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>

public delegate void ThemeChangedEventHandler(object sender, ThemeChangedEventArgs e);
/// <summary>
/// RibbonThemeUtils using UIFramework.ApplicationTheme
/// </summary>
/// <remarks>
/// Available for Revit 2019+
/// </remarks>
public class RibbonThemeUtils
{
/// <summary>
/// ThemeChanged
/// </summary>
public static event ThemeChangedEventHandler ThemeChanged;
/// <summary>
/// Theme is Light
/// </summary>
public static bool IsLight { get; private set; } = true;
/// <summary>
/// Theme is Dark
/// </summary>
public bool IsDark => !IsLight;

#if NET47_OR_GREATER || NET
static RibbonThemeUtils()
{
ExecuteCurrentTheme();
UIFramework.ApplicationTheme.CurrentTheme.PropertyChanged += CurrentTheme_PropertyChanged;
}

internal static void Dispose()
{
UIFramework.ApplicationTheme.CurrentTheme.PropertyChanged -= CurrentTheme_PropertyChanged;
ThemeChanged = null;
}

internal static void ThemeChangedTest()
{
UIFramework.RevitRibbonControl.RibbonControl.Dispatcher.Invoke(() =>
{
var color = UIFramework.ApplicationTheme.CurrentTheme.ActiveTabBackgroundColor;
UIFramework.ApplicationTheme.CurrentTheme.ActiveTabBackgroundColor = System.Windows.Media.Colors.White;
UIFramework.ApplicationTheme.CurrentTheme.ActiveTabBackgroundColor = System.Windows.Media.Colors.Black;
UIFramework.ApplicationTheme.CurrentTheme.ActiveTabBackgroundColor = color;
});
}

private static void ExecuteCurrentTheme()
{
if (IsThemeChanged(out UIFramework.ApplicationTheme theme, out bool isLight))
{
ExecuteThemeChanged(theme, isLight);
}
}

const string ApplicationCurrentThemeChangedPropertyName = "ActiveTabBackgroundColor";
private static void CurrentTheme_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == ApplicationCurrentThemeChangedPropertyName)
{
ExecuteCurrentTheme();
}
}
private static void ExecuteThemeChanged(UIFramework.ApplicationTheme theme, bool isLight)
{
try
{
ThemeChanged?.Invoke(theme, new ThemeChangedEventArgs(IsLight));
}
catch { }
}
private static bool IsThemeChanged(out UIFramework.ApplicationTheme theme, out bool isLight)
{
theme = UIFramework.ApplicationTheme.CurrentTheme;
isLight = IsThemeLight(theme);

if (isLight == IsLight)
return false;

IsLight = isLight;
return true;
}
private static bool IsThemeLight(UIFramework.ApplicationTheme applicationTheme)
{
if (applicationTheme is null)
return true;

var color = applicationTheme.ActiveTabBackgroundColor;
var brightness = (byte)((color.R + color.G + color.B) / 3);
return brightness > 128;
}
#endif
}
}
14 changes: 12 additions & 2 deletions ricaun.Revit.UI/ricaun.Revit.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@

<!-- RevitVersion -->
<PropertyGroup>
<TargetFrameworks>net46;net8.0-windows</TargetFrameworks>
<TargetFrameworks>net46;net47;net48;net8.0-windows</TargetFrameworks>
</PropertyGroup>
<Choose>
<When Condition="$(TargetFramework.StartsWith('net4'))">
<When Condition="$(TargetFramework.StartsWith('net46'))">
<PropertyGroup>
<RevitVersion>2017</RevitVersion>
</PropertyGroup>
</When>
<When Condition="$(TargetFramework.StartsWith('net47'))">
<PropertyGroup>
<RevitVersion>2019</RevitVersion>
</PropertyGroup>
</When>
<When Condition="$(TargetFramework.StartsWith('net48'))">
<PropertyGroup>
<RevitVersion>2021</RevitVersion>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<RevitVersion>2025</RevitVersion>
Expand Down

0 comments on commit 15d4bf6

Please sign in to comment.