-
-
Notifications
You must be signed in to change notification settings - Fork 521
/
TestContent.xaml.cs
680 lines (554 loc) · 20.9 KB
/
TestContent.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#pragma warning disable SA1402 // File may only contain a single class
namespace FluentTest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ControlzEx;
using ControlzEx.Theming;
using Fluent;
using Fluent.Extensions;
using Fluent.Internal;
using Fluent.Localization;
using FluentTest.Adorners;
using FluentTest.Commanding;
using FluentTest.Helpers;
using FluentTest.ViewModels;
#if MahApps_Metro
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
#endif
using Button = Fluent.Button;
/// <summary>
/// Test-Content
/// </summary>
public partial class TestContent
{
private readonly MainViewModel viewModel;
public TestContent()
{
this.InitializeComponent();
//RibbonLocalization.Current.Localization.Culture = new CultureInfo("ru-RU");
this.HookEvents();
this.viewModel = new MainViewModel();
this.WindowTitle = GetVersionText(this.GetType());
this.DataContext = this.viewModel;
this.Loaded += this.TestContent_Loaded;
this.InputBindings.Add(new InputBinding(new RelayCommand(() =>
{
this.Backstage.IsOpen = !this.Backstage.IsOpen;
if (this.Backstage.IsOpen
&& this.Backstage.Content is BackstageTabControl backstageTabControl)
{
var recentTabItem = backstageTabControl.Items.OfType<BackstageTabItem>().FirstOrDefault(x => x.Header is "Recent");
if (recentTabItem is not null)
{
recentTabItem.IsSelected = true;
}
}
}), new KeyGesture(Key.F11, ModifierKeys.Control)));
}
public string WindowTitle { get; }
#pragma warning disable WPF0060
/// <summary>Identifies the <see cref="Colors"/> dependency property.</summary>
public static readonly DependencyProperty ColorsProperty = DependencyProperty.Register(nameof(Colors), typeof(List<KeyValuePair<string, Color?>>), typeof(TestContent), new PropertyMetadata(default(List<KeyValuePair<string, Color>>)));
#pragma warning restore WPF0060
public List<KeyValuePair<string, Color?>>? Colors
{
get => (List<KeyValuePair<string, Color?>>?)this.GetValue(ColorsProperty);
set => this.SetValue(ColorsProperty, value);
}
public List<RibbonLocalizationBase> Localizations { get; } = GetLocalizations();
private static List<RibbonLocalizationBase> GetLocalizations()
{
return RibbonLocalization.Current.LocalizationMap.Values
.Select(x => (RibbonLocalizationBase)Activator.CreateInstance(x)!)
.ToList();
}
private static IEnumerable<KeyValuePair<string, Color?>> GetColors()
{
var colors = typeof(Colors)
.GetProperties()
.Where(prop =>
typeof(Brush).IsAssignableFrom(prop.PropertyType))
.Select(prop =>
new KeyValuePair<string, Color?>(prop.Name, (Color)prop.GetValue(null, null)!));
return ThemeManager.Current.Themes.GroupBy(x => x.ColorScheme)
.Select(x => x.First())
.Select(x => new KeyValuePair<string, Color?>(x.ColorScheme, ((SolidColorBrush)x.ShowcaseBrush).Color))
.Concat(colors)
.OrderBy(x => x.Key);
}
private static string GetVersionText(Type type)
{
var version = type.Assembly.GetName().Version;
var assemblyProductAttribute = type.Assembly.GetCustomAttribute<AssemblyProductAttribute>();
var assemblyInformationalVersionAttribute = type.Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return $"{assemblyProductAttribute?.Product} {version} ({assemblyInformationalVersionAttribute?.InformationalVersion})";
}
private string selectedMenu = "Backstage";
public string SelectedMenu
{
get => this.selectedMenu;
set
{
this.selectedMenu = value;
switch (this.selectedMenu)
{
case "ApplicationMenu":
this.ApplicationMenu.Visibility = Visibility.Visible;
this.Backstage.Visibility = Visibility.Collapsed;
break;
case "Backstage":
this.ApplicationMenu.Visibility = Visibility.Collapsed;
this.Backstage.Visibility = Visibility.Visible;
break;
case "Empty menu":
this.ApplicationMenu.Visibility = Visibility.Collapsed;
this.Backstage.Visibility = Visibility.Collapsed;
break;
}
}
}
private void HookEvents()
{
this.Loaded += this.HandleTestContentLoaded;
this.buttonBold.Checked += (s, e) => Debug.WriteLine("Checked");
this.buttonBold.Unchecked += (s, e) => Debug.WriteLine("Unchecked");
this.PreviewMouseWheel += this.OnPreviewMouseWheel;
}
private void TestContent_Loaded(object sender, RoutedEventArgs e)
{
this.Loaded -= this.TestContent_Loaded;
this.InitializeColors();
}
private void InitializeColors()
{
ColorGallery.RecentColors.Add((Color)this.FindResource("Fluent.Ribbon.Colors.AccentBase"));
var currentColors = new[]
{
new KeyValuePair<string, Color?>("None", null),
new KeyValuePair<string, Color?>("Initial glow", GetCurrentGlowColor()),
new KeyValuePair<string, Color?>("Initial non active glow", GetCurrentNonActiveGlowBrush()),
};
this.Colors = currentColors.Concat(GetColors()).ToList();
Color? GetCurrentGlowColor()
{
switch (Window.GetWindow(this))
{
case WindowChromeWindow x:
return x.GlowColor;
}
return null;
}
Color? GetCurrentNonActiveGlowBrush()
{
switch (Window.GetWindow(this))
{
case WindowChromeWindow x:
return x.NonActiveGlowColor;
}
return null;
}
}
private static void OnScreenTipHelpPressed(object? sender, ScreenTipHelpEventArgs e)
{
if (e.HelpTopic is null)
{
return;
}
Process.Start((string)e.HelpTopic);
}
private void HandleTestContentLoaded(object sender, RoutedEventArgs e)
{
ScreenTip.HelpPressed += OnScreenTipHelpPressed;
}
private void OnLauncherButtonClick(object sender, RoutedEventArgs e)
{
var groupBox = (RibbonGroupBox)sender;
var wnd = new Window
{
Content = $"Launcher-Window for: {groupBox.Header}",
Width = 300,
Height = 100,
Owner = Window.GetWindow(this),
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
wnd.ShowDialog();
}
private void OnSplitClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("Split Clicked!!!");
}
private void OnEnlargeClick(object sender, RoutedEventArgs e)
{
if (this.InRibbonGallery.IsLoaded)
{
this.InRibbonGallery.Enlarge();
}
}
private void OnReduceClick(object sender, RoutedEventArgs e)
{
if (this.InRibbonGallery.IsLoaded)
{
this.InRibbonGallery.Reduce();
}
}
private void SyncThemeNow_OnClick(object sender, RoutedEventArgs e)
{
ThemeManager.Current.SyncTheme();
}
public Button CreateRibbonButton()
{
var fooCommand1 = new TestRoutedCommand();
var button = new Button
{
Command = fooCommand1.ItemCommand,
Header = "Foo",
Icon = new BitmapImage(new Uri("pack://application:,,,/Fluent.Ribbon.Showcase;component/Images/Green.png", UriKind.Absolute)),
LargeIcon = new BitmapImage(new Uri("pack://application:,,,/Fluent.Ribbon.Showcase;component/Images/GreenLarge.png", UriKind.Absolute)),
};
this.CommandBindings.Add(fooCommand1.ItemCommandBinding);
return button;
}
#region Logical tree
private void OnShowLogicalTreeClick(object sender, RoutedEventArgs e)
{
this.CheckLogicalTree(this.ribbon);
this.logicalTreeView.Items.Clear();
this.BuildLogicalTree(this.ribbon, this.logicalTreeView);
}
private static string GetDebugInfo(DependencyObject? element)
{
if (element is null)
{
return "NULL";
}
var debugInfo = $"[{element}]";
if (element is IHeaderedControl headeredControl)
{
debugInfo += $" Header: \"{headeredControl.Header}\"";
}
if (element is FrameworkElement frameworkElement
&& string.IsNullOrEmpty(frameworkElement.Name) == false)
{
debugInfo += $" Name: \"{frameworkElement.Name}\"";
}
return debugInfo;
}
private void CheckLogicalTree(DependencyObject root)
{
var children = LogicalTreeHelper.GetChildren(root);
foreach (var child in children.OfType<DependencyObject>())
{
if (ReferenceEquals(LogicalTreeHelper.GetParent(child), root) == false)
{
Debug.WriteLine($"Incorrect logical parent for {GetDebugInfo(child)}");
Debug.WriteLine($"\tExpected: {GetDebugInfo(root)}");
Debug.WriteLine($"\tFound: {GetDebugInfo(LogicalTreeHelper.GetParent(child))}");
}
this.CheckLogicalTree(child);
}
}
private void BuildLogicalTree(DependencyObject current, ItemsControl parentControl)
{
var newItem = new TreeViewItem
{
Header = GetDebugInfo(current),
Tag = current
};
parentControl.Items.Add(newItem);
var children = LogicalTreeHelper.GetChildren(current);
foreach (var child in children.OfType<DependencyObject>())
{
this.BuildLogicalTree(child, newItem);
}
}
private void OnTreeDoubleClick(object sender, MouseButtonEventArgs e)
{
var treeView = sender as TreeView;
var item = treeView?.SelectedItem as TreeViewItem;
if (item is null)
{
return;
}
var stringBuilder = new StringBuilder();
this.BuildBackLogicalTree(item.Tag as DependencyObject, stringBuilder);
MessageBox.Show($"From buttom to top:\n{stringBuilder}");
}
private void BuildBackLogicalTree(DependencyObject? current, StringBuilder stringBuilder)
{
if (current is null
|| ReferenceEquals(current, this.ribbon))
{
return;
}
stringBuilder.AppendFormat(" -> {0}\n", GetDebugInfo(current));
var parent = LogicalTreeHelper.GetParent(current);
this.BuildBackLogicalTree(parent, stringBuilder);
}
#endregion Logical tree
private void OnFormatPainterClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("FP");
}
private void OnHelpClick(object sender, RoutedEventArgs e)
{
this.viewModel.AreContextGroupsVisible = !this.viewModel.AreContextGroupsVisible;
}
private void OnSpinnerValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
// MessageBox.Show(String.Format("Changed from {0} to {1}", e.OldValue, e.NewValue));
}
private void OnMenuItemClick(object sender, RoutedEventArgs e)
{
var wnd = new TestWindow
{
Owner = Window.GetWindow(this)
};
wnd.Show();
}
private void AddRibbonTab_OnClick(object sender, RoutedEventArgs e)
{
var tab = new RibbonTabItem
{
Header = "Test"
};
var group = new RibbonGroupBox();
for (var i = 0; i < 20; i++)
{
group.Items.Add(this.CreateRibbonButton());
}
tab.Groups.Add(group);
this.ribbon.Tabs.Add(tab);
}
private void AddButton_OnClick(object sender, RoutedEventArgs e)
{
if (this.ribbon.SelectedTabItem is null)
{
return;
}
var group = this.ribbon.SelectedTabItem.Groups.Last();
if (group.ItemsSource is not null)
{
return;
}
var button = new Button
{
Header = "Foo",
Icon = new BitmapImage(new Uri("pack://application:,,,/Fluent.Ribbon.Showcase;component/Images/Green.png", UriKind.Absolute)),
LargeIcon = new BitmapImage(new Uri("pack://application:,,,/Fluent.Ribbon.Showcase;component/Images/GreenLarge.png", UriKind.Absolute)),
SizeDefinition = new RibbonControlSizeDefinition(RibbonControlSize.Middle, RibbonControlSize.Middle, RibbonControlSize.Middle)
};
group.Items.Add(button);
}
private async void HandleSaveAsClick(object sender, RoutedEventArgs e)
{
var progressAdornerChild = new Border
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
Background = new SolidColorBrush(System.Windows.Media.Colors.Black) { Opacity = 0.25 },
IsHitTestVisible = true,
Child = new ProgressBar
{
IsIndeterminate = true,
Width = 300,
Height = 20,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
}
};
BindingOperations.SetBinding(progressAdornerChild, WidthProperty, new Binding(nameof(this.Backstage.AdornerLayer.ActualWidth)) { Source = this.Backstage.AdornerLayer });
BindingOperations.SetBinding(progressAdornerChild, HeightProperty, new Binding(nameof(this.Backstage.AdornerLayer.ActualHeight)) { Source = this.Backstage.AdornerLayer });
if (this.Backstage.AdornerLayer is null)
{
throw new Exception("Backstage does not have an adorner layer.");
}
var progressAdorner = new SimpleControlAdorner(this.Backstage.AdornerLayer)
{
Child = progressAdornerChild
};
this.Backstage.AdornerLayer.Add(progressAdorner);
await Task.Delay(TimeSpan.FromSeconds(3));
this.Backstage.AdornerLayer.Remove(progressAdorner);
BindingOperations.ClearAllBindings(progressAdornerChild);
}
private void OpenRegularWindow_OnClick(object sender, RoutedEventArgs e)
{
new RegularWindow().Show();
}
private void OpenMinimalRibbonWindowSample_OnClick(object sender, RoutedEventArgs e)
{
new MinimalWindowSample().Show();
}
private void OpenMahMetroWindow_OnClick(object sender, RoutedEventArgs e)
{
#if MahApps_Metro
new MahMetroWindow().Show();
#else
ShowMahAppsMetroNotAvailableMessageBox();
#endif
}
private void OpenRibbonWindowWithoutVisibileRibbon_OnClick(object sender, RoutedEventArgs e)
{
new RibbonWindowWithoutVisibleRibbon().Show();
}
private void OpenRibbonWindowWithoutRibbon_OnClick(object sender, RoutedEventArgs e)
{
new RibbonWindowWithoutRibbon().Show();
}
private void ZoomSlider_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (e.OldValue.AlmostEquals(0)
|| Window.GetWindow(this) is not { } window)
{
return;
}
var textFormattingMode = DoubleUtil.AreClose(e.NewValue, 1.0) ? TextFormattingMode.Display : TextFormattingMode.Ideal;
TextOptions.SetTextFormattingMode(window, textFormattingMode);
}
private void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftCtrl) == false
&& Keyboard.IsKeyDown(Key.RightCtrl) == false)
{
return;
}
var newZoomValue = this.zoomSlider.Value + (e.Delta > 0 ? 0.1 : -0.1);
this.zoomSlider.Value = Math.Max(Math.Min(newZoomValue, this.zoomSlider.Maximum), this.zoomSlider.Minimum);
e.Handled = true;
}
private void SleepButton_OnClick(object sender, RoutedEventArgs e)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
}
private void OpenModalRibbonWindow_OnClick(object sender, RoutedEventArgs e)
{
var childWindow = new TestWindow { Owner = Window.GetWindow(this) };
childWindow.ShowDialog();
}
private void OpenRibbonWindowOnNewThread_OnClick(object sender, RoutedEventArgs e)
{
var thread = new Thread(() =>
{
var testWindow = new TestWindow();
testWindow.Closed += OnTestWindowOnClosed;
testWindow.Show();
System.Windows.Threading.Dispatcher.Run();
void OnTestWindowOnClosed(object? o, EventArgs args)
{
testWindow.Closed -= OnTestWindowOnClosed;
((Window)o!).Dispatcher?.InvokeShutdown();
}
})
{
IsBackground = true
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private void OpenRibbonWindowColorized_OnClick(object sender, RoutedEventArgs e)
{
new RibbonWindowColorized().Show();
}
private void OpenRibbonWindowWithBackgroundImage_OnClick(object sender, RoutedEventArgs e)
{
new RibbonWindowWithBackgroundImage().Show();
}
private void ShowStartScreen_OnClick(object sender, RoutedEventArgs e)
{
this.startScreen.Shown = false;
this.startScreen.IsOpen = true;
}
private void HandleAddItemToFontsClick(object sender, RoutedEventArgs e)
{
this.viewModel.FontsViewModel.FontsData.Add($"Added item {this.viewModel.FontsViewModel.FontsData.Count}");
}
private void CreateThemeResourceDictionaryButton_OnClick(object sender, RoutedEventArgs e)
{
this.ThemeResourceDictionaryTextBox.Text = ThemeHelper.CreateTheme(ThemeManager.Current.DetectTheme()?.BaseColorScheme ?? "Dark", this.ThemeColorGallery.SelectedColor ?? this.viewModel.ColorViewModel.ThemeColor, changeImmediately: this.ChangeImmediatelyCheckBox.IsChecked ?? false).Item1;
}
private void HandleResetSavedState_OnClick(object sender, RoutedEventArgs e)
{
this.ribbon.AutomaticStateManagement = false;
this.ribbon.RibbonStateStorage.Reset();
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
}
private async void HandleShowMetroMessage(object sender, RoutedEventArgs e)
{
#if MahApps_Metro
var metroWindow = Window.GetWindow(this) as MetroWindow;
if (metroWindow is null)
{
return;
}
await metroWindow.ShowMessageAsync("Test", "Message");
#else
ShowMahAppsMetroNotAvailableMessageBox();
await Task.Yield();
#endif
}
private void Hyperlink_OnClick(object sender, RoutedEventArgs e)
{
new Window().ShowDialog();
}
private static void ShowMahAppsMetroNotAvailableMessageBox()
{
MessageBox.Show("MahApps.Metro is not available in this showcase version.");
}
private void StartSnoop_OnClick(object sender, RoutedEventArgs e)
{
var snoopPath = "snoop";
var alternativeSnoopPath = Environment.GetEnvironmentVariable("snoop_dev_path");
if (string.IsNullOrEmpty(alternativeSnoopPath) == false
&& File.Exists(alternativeSnoopPath))
{
snoopPath = alternativeSnoopPath;
}
var startInfo = new ProcessStartInfo(snoopPath, $"inspect --targetPID {Process.GetCurrentProcess().Id}")
{
UseShellExecute = true
};
try
{
using var p = Process.Start(startInfo);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}\n\nCommandline: {startInfo.FileName} {startInfo.Arguments}");
}
}
private void OpenSimplifiedRibbonWindow_OnClick(object sender, RoutedEventArgs e)
{
var window = new SimplifiedRibbonWindow();
window.DataContext = this.viewModel;
window.Show();
}
}
public class TestRoutedCommand
{
public static RoutedCommand TestPresenterCommand { get; } = new(nameof(TestPresenterCommand), typeof(TestRoutedCommand));
public ICommand ItemCommand => TestPresenterCommand;
public CommandBinding ItemCommandBinding => new(TestPresenterCommand, OnTestCommandExecuted, CanExecuteTestCommand);
private static void CanExecuteTestCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private static void OnTestCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("TestPresenterCommand");
}
}