Skip to content

Commit 2ef6ae4

Browse files
committed
update: theme change mechanism
1 parent 57e6cb8 commit 2ef6ae4

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Autodesk.Revit.UI;
2+
using MaterialDesignThemes.Wpf;
3+
4+
namespace RevitDevTool.Theme;
5+
6+
public class ThemeBundle : BundledTheme
7+
{
8+
public ThemeBundle()
9+
{
10+
BaseTheme = UIThemeManager.CurrentTheme switch
11+
{
12+
UITheme.Light => MaterialDesignThemes.Wpf.BaseTheme.Light,
13+
UITheme.Dark => MaterialDesignThemes.Wpf.BaseTheme.Dark,
14+
_ => throw new ArgumentOutOfRangeException()
15+
};
16+
PrimaryColor = MaterialDesignColors.PrimaryColor.Orange;
17+
SecondaryColor = MaterialDesignColors.SecondaryColor.Amber;
18+
ColorAdjustment = new ColorAdjustment();
19+
}
20+
}

source/RevitDevTool/View/TraceLog.xaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
99
xmlns:sys="clr-namespace:System;assembly=mscorlib"
10+
xmlns:theme="clr-namespace:RevitDevTool.Theme"
1011
xmlns:vm="clr-namespace:RevitDevTool.ViewModel"
1112
d:DesignHeight="600"
1213
d:DesignWidth="450"
@@ -21,7 +22,8 @@
2122
<Page.Resources>
2223
<ResourceDictionary>
2324
<ResourceDictionary.MergedDictionaries>
24-
<ResourceDictionary Source="../Theme/Theme.xaml" />
25+
<theme:ThemeBundle />
26+
<ResourceDictionary Source="/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign3.Defaults.xaml" />
2527
</ResourceDictionary.MergedDictionaries>
2628

2729
<ObjectDataProvider
@@ -41,7 +43,9 @@
4143
</Page.DataContext>
4244

4345
<DockPanel>
44-
<DockPanel Margin="5" DockPanel.Dock="Top">
46+
<DockPanel
47+
Margin="5"
48+
DockPanel.Dock="Top">
4549
<ComboBox
4650
Width="85"
4751
VerticalAlignment="Center"
@@ -54,19 +58,19 @@
5458
Orientation="Horizontal">
5559
<Button
5660
Margin="5,0"
57-
Padding="5 0"
61+
Padding="5,0"
62+
md:ButtonAssist.CornerRadius="5"
5863
Command="{Binding ClearCommand}"
5964
Content="{md:PackIcon Kind=Backspace,
6065
Size=25}"
61-
6266
ToolTip="Clear Log" />
6367
<Button
6468
Margin="5,0"
65-
Padding="5 0"
69+
Padding="5,0"
70+
md:ButtonAssist.CornerRadius="5"
6671
Command="{Binding ClearGeometryCommand}"
6772
Content="{md:PackIcon Kind=ShapePlus,
6873
Size=25}"
69-
7074
ToolTip="Clear Geometry" />
7175
<ToggleButton
7276
md:ToggleButtonAssist.OnContent="{md:PackIcon Kind=AccessPoint}"
Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
1-
using System.Windows.Controls;
1+
using System.ComponentModel;
2+
using System.Windows;
23
using Autodesk.Revit.UI;
4+
using MaterialDesignThemes.Wpf;
5+
using UIFramework;
36

47
namespace RevitDevTool.View;
58

6-
public partial class TraceLog : IDockablePaneProvider
9+
public partial class TraceLog : IDisposable
710
{
811
public TraceLog()
912
{
1013
InitializeComponent();
14+
#if REVIT2024_OR_GREATER
15+
ApplicationTheme.CurrentTheme.PropertyChanged += ApplyTheme;
16+
#endif
1117
}
1218

13-
public void SetupDockablePane(DockablePaneProviderData data)
19+
#if REVIT2024_OR_GREATER
20+
private void ApplyTheme(object? sender, PropertyChangedEventArgs args)
1421
{
15-
data.VisibleByDefault = true;
16-
data.InitialState = new DockablePaneState
22+
if (args.PropertyName != nameof(ApplicationTheme.CurrentTheme.RibbonPanelBackgroundBrush)) return;
23+
if (UIThemeManager.CurrentTheme.ToString() == ApplicationTheme.CurrentTheme.RibbonTheme.Name) return;
24+
25+
var resourceDictionary = GetThemeResourceDictionary();
26+
var materialDesignTheme = resourceDictionary.GetTheme();
27+
28+
var newTheme = UIThemeManager.CurrentTheme switch
1729
{
18-
DockPosition = DockPosition.Left,
30+
UITheme.Light => BaseTheme.Light,
31+
UITheme.Dark => BaseTheme.Dark,
32+
_ => throw new ArgumentOutOfRangeException()
1933
};
34+
materialDesignTheme.SetBaseTheme(newTheme);
35+
resourceDictionary.SetTheme(materialDesignTheme);
36+
}
37+
38+
private ResourceDictionary GetThemeResourceDictionary()
39+
{
40+
return Resources.MergedDictionaries.First(x => x is IMaterialDesignThemeDictionary);
41+
}
42+
#endif
43+
44+
public void Dispose()
45+
{
46+
#if REVIT2024_OR_GREATER
47+
ApplicationTheme.CurrentTheme.PropertyChanged -= ApplyTheme;
48+
#endif
49+
Resources.MergedDictionaries.Clear();
50+
GC.SuppressFinalize(this);
2051
}
2152
}

0 commit comments

Comments
 (0)