Skip to content

Commit

Permalink
remember window settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rb111 committed May 16, 2018
1 parent 7c801e0 commit 710c309
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 7 deletions.
93 changes: 87 additions & 6 deletions BuildsAppReborn.Client/ViewModels/NotifyIconViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using BuildsAppReborn.Client.Interfaces;
using BuildsAppReborn.Client.Notification;
using BuildsAppReborn.Client.Resources;
using BuildsAppReborn.Contracts.Models;
using BuildsAppReborn.Infrastructure;
using Prism.Commands;

Expand All @@ -14,19 +17,27 @@
namespace BuildsAppReborn.Client.ViewModels
{
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class NotifyIconViewModel : ViewModelBase
{
#region Constructors

[ImportingConstructor]
internal NotifyIconViewModel(ExportFactory<IBuildsStatusView> buildsExportFactory, ExportFactory<ISettingsView> settingsExportFactory, BuildCache buildCache, NotificationManager notificationManager, UpdateChecker updateChecker)
internal NotifyIconViewModel(ExportFactory<IBuildsStatusView> buildsExportFactory, ExportFactory<ISettingsView> settingsExportFactory, BuildCache buildCache, NotificationManager notificationManager, UpdateChecker updateChecker, GlobalSettingsContainer settingsContainer)
{
if (buildCache.CacheStatus == BuildCacheStatus.NotConfigured) TrayIcon = IconProvider.SettingsIcon;
else TrayIcon = IconProvider.LoadingIcon;
if (buildCache.CacheStatus == BuildCacheStatus.NotConfigured)
{
TrayIcon = IconProvider.SettingsIcon;
}
else
{
TrayIcon = IconProvider.LoadingIcon;
}

this.buildsExportFactory = buildsExportFactory;
this.settingsExportFactory = settingsExportFactory;
this.updateChecker = updateChecker;
globalSettingsContainer = settingsContainer;
buildCache.CacheUpdated += (sender, args) => { TrayIcon = buildCache.CurrentIcon; };

Initialize();
Expand Down Expand Up @@ -59,7 +70,7 @@ public String TrayIcon

#endregion

#region Internal Static Methods
#region Internal Methods

internal static void OpenWindow<T>(ExportFactory<T> newWindow)
{
Expand All @@ -70,13 +81,16 @@ internal static void OpenWindow<T>(ExportFactory<T> newWindow)
{
currentMainWindow.WindowState = WindowState.Normal;
}

if (!currentMainWindow.IsActive)
{
currentMainWindow.Show();
}

currentMainWindow.Activate();
return;
}

Window buildStatusWindow = null;
foreach (var window in Application.Current.Windows)
{
Expand All @@ -86,20 +100,30 @@ internal static void OpenWindow<T>(ExportFactory<T> newWindow)
break;
}
}

if (buildStatusWindow == null)
{
(newWindow.CreateExport().Value as Window)?.Show();
var window = newWindow.CreateExport().Value as Window;
if (window != null)
{
RestoreWindowSettings(window, globalSettingsContainer.GeneralSettings.WindowSettings);
window.Closed += WindowOnClosed;
window.Show();
}
}

if (buildStatusWindow != null)
{
if (buildStatusWindow.WindowState == WindowState.Minimized)
{
buildStatusWindow.WindowState = WindowState.Normal;
}

if (currentMainWindow == null || !currentMainWindow.IsActive)
{
buildStatusWindow.Show();
}

buildStatusWindow.Activate();
}
}
Expand All @@ -119,12 +143,69 @@ private void Initialize()
CheckUpdateCommand = new DelegateCommand(() => this.updateChecker.UpdateCheck(true));
}

private static void RestoreWindowSettings(Window window, List<WindowSetting> windowSettings)
{
if (window == null || windowSettings == null)
{
return;
}

var windowId = window.GetType().FullName;
var setting = windowSettings.FirstOrDefault(f => f.Id == windowId);
if (setting == null)
{
return;
}

window.Top = setting.Top;
window.Left = setting.Left;
window.Height = setting.Height;
window.Width = setting.Width;
window.WindowState = setting.WindowState;
}

private static void SaveWindowSettings(Window window, List<WindowSetting> windowSettings)
{
if (window == null || windowSettings == null)
{
return;
}

var windowId = window.GetType().FullName;
var setting = windowSettings.FirstOrDefault(f => f.Id == windowId);
if (setting == null)
{
setting = new WindowSetting {Id = windowId};
globalSettingsContainer.GeneralSettings.WindowSettings.Add(setting);
}

setting.Top = window.Top;
setting.Left = window.Left;
setting.Height = window.Height;
setting.Width = window.Width;
setting.WindowState = window.WindowState;
globalSettingsContainer.Save();
}

private static void WindowOnClosed(Object sender, EventArgs e)
{
var window = sender as Window;
if (window == null)
{
return;
}

SaveWindowSettings(window, globalSettingsContainer.GeneralSettings.WindowSettings);

window.Closed -= WindowOnClosed;
}

#endregion

#region Private Fields

private readonly ExportFactory<IBuildsStatusView> buildsExportFactory;

private static GlobalSettingsContainer globalSettingsContainer;
private readonly ExportFactory<ISettingsView> settingsExportFactory;
private String trayIcon;
private readonly UpdateChecker updateChecker;
Expand Down
5 changes: 5 additions & 0 deletions BuildsAppReborn.Contracts/BuildsAppReborn.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.1.5.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -79,6 +83,7 @@
<Compile Include="Models\ISourceVersion.cs" />
<Compile Include="Models\ITestRun.cs" />
<Compile Include="Models\IUser.cs" />
<Compile Include="Models\WindowSetting.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion BuildsAppReborn.Contracts/Models/GeneralSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace BuildsAppReborn.Contracts.Models
{
Expand All @@ -16,6 +17,11 @@ public GeneralSettings()

UpdateCheckInterval = TimeSpan.FromHours(1);
PollingInterval = TimeSpan.FromMinutes(1);

if (WindowSettings == null)
{
WindowSettings = new List<WindowSetting>();
}
}

#endregion
Expand All @@ -30,11 +36,13 @@ public GeneralSettings()

public Boolean NotifyOnNewUpdate { get; set; }

public TimeSpan PollingInterval { get; set; }

public TimeSpan UpdateCheckInterval { get; set; }

public BuildViewStyle ViewStyle { get; set; }

public TimeSpan PollingInterval { get; set; }
public List<WindowSetting> WindowSettings { get; set; }

#endregion
}
Expand Down
23 changes: 23 additions & 0 deletions BuildsAppReborn.Contracts/Models/WindowSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Windows;

namespace BuildsAppReborn.Contracts.Models
{
public class WindowSetting
{
#region Public Properties

public Double Height { get; set; }

public String Id { get; set; }

public Double Left { get; set; }
public Double Top { get; set; }

public Double Width { get; set; }

public WindowState WindowState { get; set; }

#endregion
}
}

0 comments on commit 710c309

Please sign in to comment.