1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="WindowHelper.cs" company="HandBrake Project (http://handbrake.fr)">
3 //   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
4 // </copyright>
5 // <summary>
6 //   A helper to store a windows current state as part of the user settings service.
7 // </summary>
8 // --------------------------------------------------------------------------------------------------------------------
9 
10 namespace HandBrakeWPF.AttachedProperties
11 {
12     using System;
13     using System.ComponentModel;
14     using System.Diagnostics.CodeAnalysis;
15     using System.Text.Json;
16     using System.Windows;
17 
18     using Caliburn.Micro;
19 
20     using HandBrake.Interop.Utilities;
21 
22     using HandBrakeWPF.Services.Interfaces;
23 
24     public class WindowHelper
25     {
26         public static readonly DependencyProperty SaveProperty = DependencyProperty.RegisterAttached("SaveState", typeof(bool), typeof(WindowHelper), new FrameworkPropertyMetadata(SaveState));
27 
28         private readonly Window appWindow;
29         private readonly IUserSettingService userSettingService = IoC.Get<IUserSettingService>();
30 
WindowHelper(Window appWindow)31         public WindowHelper(Window appWindow)
32         {
33             this.appWindow = appWindow;
34         }
35 
SetSaveState(DependencyObject sender, bool isEnabled)36         public static void SetSaveState(DependencyObject sender, bool isEnabled)
37         {
38             sender.SetValue(SaveProperty, isEnabled);
39         }
40 
SaveState(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)41         private static void SaveState(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
42         {
43             if (sender is Window window)
44             {
45                 if ((bool)eventArgs.NewValue)
46                 {
47                     var settings = new WindowHelper(window);
48                     settings.Attach();
49                 }
50             }
51         }
52 
Attach()53         private void Attach()
54         {
55             if (this.appWindow != null)
56             {
57                 this.appWindow.Closing += this.WindowClosing;
58                 this.appWindow.Initialized += this.WindowInitialized;
59                 this.appWindow.Loaded += this.WindowLoaded;
60             }
61         }
62 
WindowClosing(object sender, CancelEventArgs cancelEventArgs)63         private void WindowClosing(object sender, CancelEventArgs cancelEventArgs)
64         {
65             string key = string.Format("{0}.Settings", this.appWindow.Name);
66             WindowInformation information = new WindowInformation(this.appWindow.Name, this.appWindow.WindowState, this.appWindow.RestoreBounds);
67 
68             string json = JsonSerializer.Serialize(information, JsonSettings.Options);
69             if (!string.IsNullOrEmpty(json))
70             {
71                 this.userSettingService.SetUserSetting(key, json);
72             }
73         }
74 
WindowInitialized(object sender, EventArgs eventArgs)75         private void WindowInitialized(object sender, EventArgs eventArgs)
76         {
77             string key = string.Format("{0}.Settings", this.appWindow.Name);
78             string json = this.userSettingService.GetUserSetting<string>(key);
79             if (!string.IsNullOrEmpty(json))
80             {
81                 WindowInformation settings = JsonSerializer.Deserialize<WindowInformation>(json, JsonSettings.Options);
82 
83                 if (settings.Location != Rect.Empty)
84                 {
85                     // We might use these in the future
86                     this.appWindow.Left = settings.Location.Left;
87                     this.appWindow.Top = settings.Location.Top;
88                     this.appWindow.Width = settings.Location.Width;
89                     this.appWindow.Height = settings.Location.Height;
90                 }
91 
92                 if (settings.WindowState != WindowState.Maximized)
93                 {
94                     this.appWindow.WindowState = settings.WindowState;
95                 }
96             }
97         }
98 
WindowLoaded(object sender, RoutedEventArgs routedEventArgs)99         private void WindowLoaded(object sender, RoutedEventArgs routedEventArgs)
100         {
101             string key = string.Format("{0}.Settings", this.appWindow.Name);
102             string json = this.userSettingService.GetUserSetting<string>(key);
103             if (!string.IsNullOrEmpty(json))
104             {
105                 WindowInformation settings = JsonSerializer.Deserialize<WindowInformation>(json, JsonSettings.Options);
106                 this.appWindow.WindowState = settings.WindowState;
107             }
108         }
109     }
110 
111     /// <summary>
112     /// An object we can store as JSON against a user setting key.
113     /// </summary>
114     [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Reviewed. Suppression is OK here.")]
115     public class WindowInformation
116     {
WindowInformation(string windowName, WindowState windowState, Rect location)117         public WindowInformation(string windowName, WindowState windowState, Rect location)
118         {
119             this.WindowName = windowName;
120             this.WindowState = windowState;
121             this.Location = location;
122         }
123 
124         public string WindowName { get; set; }
125         public WindowState WindowState { get; set; }
126         public Rect Location { get; set; }
127     }
128 }