Skip to content

Commit

Permalink
Add window preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
LaughingLeader committed Jun 13, 2023
1 parent 47b4855 commit a6f7dc9
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 138 deletions.
13 changes: 10 additions & 3 deletions DivinityModManagerCore/Models/DivinityModManagerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public Hotkey SelectedHotkey

[Reactive] public int SelectedTabIndex { get; set; }

[DataMember] public WindowSettings Window { get; set; }

[SettingsEntry("Save Window Location", "Save and restore the window location when the application starts.")]
[DataMember][Reactive] public bool SaveWindowLocation { get; set; }

public ICommand SaveSettingsCommand { get; set; }
public ICommand OpenSettingsFolderCommand { get; set; }
public ICommand ExportExtenderSettingsCommand { get; set; }
Expand Down Expand Up @@ -216,9 +221,10 @@ public void Dispose()
public DivinityModManagerSettings()
{
Disposables = new CompositeDisposable();
ExtenderSettings = new OsirisExtenderSettings();

//Defaults
ExtenderSettings = new OsirisExtenderSettings();
Window = new WindowSettings();
GameDataPath = "";
GameExecutablePath = "";
DocumentsFolderPathOverride = "";
Expand All @@ -228,6 +234,7 @@ public DivinityModManagerSettings()
CheckForUpdates = true;
LastUpdateCheck = -1;
SelectedTabIndex = 0;
SaveWindowLocation = true;

var properties = typeof(DivinityModManagerSettings)
.GetRuntimeProperties()
Expand All @@ -240,13 +247,13 @@ public DivinityModManagerSettings()
if (SettingsWindowIsOpen) CanSaveSettings = true;
}).DisposeWith(Disposables);

var extender_properties = typeof(OsirisExtenderSettings)
var extenderProperties = typeof(OsirisExtenderSettings)
.GetRuntimeProperties()
.Where(prop => Attribute.IsDefined(prop, typeof(DataMemberAttribute)))
.Select(prop => prop.Name)
.ToArray();

ExtenderSettings.WhenAnyPropertyChanged(extender_properties).Subscribe((c) =>
ExtenderSettings.WhenAnyPropertyChanged(extenderProperties).Subscribe((c) =>
{
if (SettingsWindowIsOpen) CanSaveSettings = true;
this.RaisePropertyChanged("ExtenderLogDirectory");
Expand Down
27 changes: 27 additions & 0 deletions DivinityModManagerCore/Models/WindowSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using ReactiveUI;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DivinityModManager.Models
{
public class WindowSettings
{
public bool Maximized { get; set; }
public double X { get; set; }
public double Y { get; set; }
public int Screen { get; set; }

public WindowSettings()
{
Maximized = false;
X = 0;
Y = 0;
Screen = -1;
}
}
}
40 changes: 40 additions & 0 deletions GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
using DivinityModManager.Util.ScreenReader;
using DivinityModManager.ViewModels;

using DynamicData;

using ReactiveUI;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reactive.Concurrency;
Expand Down Expand Up @@ -71,6 +74,8 @@ public HorizontalModLayout GetModLayout()
return MainContentPresenter.FindVisualChildren<HorizontalModLayout>().FirstOrDefault();
}

private System.Windows.Interop.WindowInteropHelper _hwnd;

public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -151,6 +156,41 @@ public MainWindow()
});

AddHandler(UIElement.GotFocusEvent, new RoutedEventHandler(OnGotFocus));

_hwnd = new System.Windows.Interop.WindowInteropHelper(this);
}

void OnStateChanged(object sender, EventArgs e)
{
var windowSettings = ViewModel.Settings.Window;
windowSettings.Maximized = WindowState == WindowState.Maximized;
var screen = System.Windows.Forms.Screen.FromHandle(_hwnd.Handle);
windowSettings.Screen = System.Windows.Forms.Screen.AllScreens.IndexOf(screen);
ViewModel.QueueSave();
}

void OnLocationChanged(object sender, EventArgs e)
{
var windowSettings = ViewModel.Settings.Window;
var screen = System.Windows.Forms.Screen.FromHandle(_hwnd.Handle);
windowSettings.X = Left - screen.WorkingArea.Left;
windowSettings.Y = Top - screen.WorkingArea.Top;
windowSettings.Screen = System.Windows.Forms.Screen.AllScreens.IndexOf(screen);
ViewModel.QueueSave();
}

public void ToggleWindowPositionSaving(bool b)
{
if (b)
{
StateChanged += OnStateChanged;
LocationChanged += OnLocationChanged;
}
else
{
StateChanged -= OnStateChanged;
LocationChanged -= OnLocationChanged;
}
}

void OnGotFocus(object sender, RoutedEventArgs e)
Expand Down
44 changes: 40 additions & 4 deletions GUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,7 @@ private bool LoadSettings()
}
}).DisposeWith(Settings.Disposables);

//DivinityApp.DependencyFilter = this.WhenAnyValue(x => x.Settings.DebugModeEnabled).Select(MakeDependencyFilter);
//DisposeWith(Settings.Disposables);
this.WhenAnyValue(x => x.Settings.SaveWindowLocation).Subscribe(View.ToggleWindowPositionSaving).DisposeWith(Settings.Disposables);

if (Settings.LogEnabled)
{
Expand All @@ -1065,7 +1064,6 @@ private bool LoadSettings()
if (loaded)
{
Settings.CanSaveSettings = false;
//view.AlertBar.SetSuccessAlert($"Loaded settings from '{settingsFile}'.", 5);
}

return loaded;
Expand Down Expand Up @@ -1100,6 +1098,20 @@ public bool SaveSettings()
return false;
}

object deferSave = null;

public void QueueSave()
{
if (deferSave == null)
{
deferSave = RxApp.MainThreadScheduler.Schedule(TimeSpan.FromMilliseconds(250), () =>
{
SaveSettings();
deferSave = null;
});
}
}

public async Task<List<DivinityModData>> LoadWorkshopModsAsync(CancellationToken cts)
{
List<DivinityModData> newWorkshopMods = new List<DivinityModData>();
Expand Down Expand Up @@ -3559,14 +3571,38 @@ public void OnViewActivated(MainWindow parentView)
});
}

LoadSettings();
var loaded = LoadSettings();
Keys.LoadKeybindings(this);
if (Settings.CheckForUpdates)
{
CheckForUpdates();
}
SaveSettings();

if (loaded && Settings.SaveWindowLocation)
{
var window = Settings.Window;
View.WindowStartupLocation = WindowStartupLocation.Manual;

var screens = System.Windows.Forms.Screen.AllScreens;
var screen = screens.FirstOrDefault();
if (screen != null)
{
if (window.Screen > -1 && window.Screen < screens.Length - 1)
{
screen = screens[window.Screen];
}

View.Left = Math.Max(screen.WorkingArea.Left, Math.Min(screen.WorkingArea.Right, screen.WorkingArea.Left + window.X));
View.Top = Math.Max(screen.WorkingArea.Top, Math.Min(screen.WorkingArea.Bottom, screen.WorkingArea.Top + window.Y));
}

if (window.Maximized)
{
View.WindowState = WindowState.Maximized;
}
}

ModUpdatesViewVisible = ModUpdatesAvailable = false;
MainProgressTitle = "Loading...";
MainProgressValue = 0d;
Expand Down
Loading

0 comments on commit a6f7dc9

Please sign in to comment.