forked from jeremytammik/RevitLookup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RevitLookupView.xaml.cs
232 lines (204 loc) · 8.38 KB
/
RevitLookupView.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2003-2023 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
using System.Collections;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using RevitLookup.Services.Contracts;
using Wpf.Ui;
using Wpf.Ui.Appearance;
namespace RevitLookup.Views;
public sealed partial class RevitLookupView : IWindow
{
private readonly ISettingsService _settingsService;
private RevitLookupView()
{
Wpf.Ui.Application.MainWindow = this;
Wpf.Ui.Application.Windows.Add(this);
InitializeComponent();
AddShortcuts();
}
public RevitLookupView(
INavigationService navigationService,
IContentDialogService dialogService,
ISnackbarService snackbarService,
ISettingsService settingsService)
: this()
{
_settingsService = settingsService;
RootNavigation.TransitionDuration = settingsService.TransitionDuration;
WindowBackdropType = settingsService.Background;
navigationService.SetNavigationControl(RootNavigation);
dialogService.SetContentPresenter(RootContentDialog);
snackbarService.SetSnackbarPresenter(RootSnackbar);
snackbarService.DefaultTimeOut = TimeSpan.FromSeconds(3);
RestoreSize(settingsService);
}
private void AddShortcuts()
{
var closeCurrentCommand = new RelayCommand(Close);
var closeAllCommand = new RelayCommand(() =>
{
for (var i = Wpf.Ui.Application.Windows.Count - 1; i >= 0; i--)
{
var window = Wpf.Ui.Application.Windows[i];
window.Close();
}
});
InputBindings.Add(new KeyBinding(closeAllCommand, new KeyGesture(Key.Escape, ModifierKeys.Shift)));
InputBindings.Add(new KeyBinding(closeCurrentCommand, new KeyGesture(Key.Escape)));
// ThemeChangedEvent to fix the issue with the window background not being updated when the theme is changed.
{
ThemeChangedEvent themeChanged = (sender, args) =>
{
this.Dispatcher.InvokeAsync(() =>
{
foreach (var resource in this.Resources.MergedDictionaries.ToList())
{
this.Resources.MergedDictionaries.Add(resource);
this.Resources.MergedDictionaries.RemoveAt(0);
}
});
};
if (this.IsLoaded)
{
ApplicationThemeManager.Changed += themeChanged;
}
this.Loaded += (s, e) =>
{
ApplicationThemeManager.Changed += themeChanged;
};
this.Unloaded += (s, e) =>
{
ApplicationThemeManager.Changed -= themeChanged;
};
}
#if DEBUG
this.KeyDown += (s, e) =>
{
if (e.Key == Key.E)
{
var theme = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light ? ApplicationTheme.Dark : ApplicationTheme.Light;
ApplicationThemeManager.Apply(theme);
ApplicationThemeManager.Apply(theme, Wpf.Ui.Controls.WindowBackdropType.Mica, forceBackground: true);
}
if (e.Key == Key.T)
{
var theme = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light ? ApplicationTheme.Dark : ApplicationTheme.Light;
ApplicationThemeManager.Apply(theme);
}
if (e.Key == Key.D)
{
var theme = ApplicationTheme.Dark;
ApplicationThemeManager.Apply(theme);
WindowBackgroundManager.UpdateBackground(this, theme, Wpf.Ui.Controls.WindowBackdropType.None, true);
}
if (e.Key == Key.F)
{
Debug.WriteLine($">>> {Wpf.Ui.Application.MainWindow}");
foreach (DictionaryEntry resource in Wpf.Ui.Application.MainWindow.Resources.OfType<DictionaryEntry>().OrderBy(e => e.Key))
{
this.Resources[resource.Key] = resource.Value;
Debug.WriteLine(
$"INFO | Copy Resource {resource.Key} - {resource.Value}",
"Wpf.Ui.Appearance"
);
}
}
if (e.Key == Key.Y)
{
var theme = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light ? ApplicationTheme.Dark : ApplicationTheme.Light;
ApplicationThemeManager.Apply(theme);
foreach (var resource in this.Resources.MergedDictionaries.ToList())
{
this.Resources.MergedDictionaries.Add(resource);
this.Resources.MergedDictionaries.RemoveAt(0);
}
}
if (e.Key == Key.P)
{
var theme = ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light ? ApplicationTheme.Dark : ApplicationTheme.Light;
ApplicationThemeManager.Apply(theme);
foreach (var resource in Wpf.Ui.Application.MainWindow.Resources.MergedDictionaries.ToList())
{
Wpf.Ui.Application.MainWindow.Resources.MergedDictionaries.Add(resource);
Wpf.Ui.Application.MainWindow.Resources.MergedDictionaries.RemoveAt(0);
}
}
if (e.Key == Key.U)
{
foreach (var resource in this.Resources.MergedDictionaries.ToList())
{
this.Resources.MergedDictionaries.Add(resource);
this.Resources.MergedDictionaries.RemoveAt(0);
}
}
if (e.Key == Key.R)
{
var resourceDictionary = new ResourceDictionary();
foreach (var resource in Wpf.Ui.Application.MainWindow.Resources.MergedDictionaries)
{
Debug.WriteLine(
$"INFO | Resources {resource.Source}",
"Wpf.Ui.Appearance"
);
resourceDictionary.MergedDictionaries.Add(resource);
}
//foreach (var resource in resourceDictionary.MergedDictionaries)
//{
// Wpf.Ui.Application.MainWindow.Resources.MergedDictionaries.Add(resource);
// Wpf.Ui.Application.MainWindow.Resources.MergedDictionaries.RemoveAt(0);
//}
}
};
#endif
}
private void RestoreSize(ISettingsService settingsService)
{
if (!settingsService.UseSizeRestoring) return;
if (settingsService.WindowWidth >= MinWidth) Width = settingsService.WindowWidth;
if (settingsService.WindowHeight >= MinHeight) Height = settingsService.WindowHeight;
EnableSizeTracking();
}
public void EnableSizeTracking()
{
SizeChanged += OnSizeChanged;
}
public void DisableSizeTracking()
{
SizeChanged -= OnSizeChanged;
}
private void OnSizeChanged(object sender, SizeChangedEventArgs args)
{
_settingsService.WindowWidth = args.NewSize.Width;
_settingsService.WindowHeight = args.NewSize.Height;
}
protected override void OnActivated(EventArgs args)
{
base.OnActivated(args);
Wpf.Ui.Application.MainWindow = this;
}
protected override void OnClosed(EventArgs args)
{
base.OnClosed(args);
Wpf.Ui.Application.Windows.Remove(this);
}
}