-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RibbonThemeUtils
for ThemeChanged
- Loading branch information
Showing
7 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters