//
// Copyright 2014 Perforce Software Inc.
//
using System;
namespace Perforce.Helper {
public class UserPreferences {
#region Member Variables
private double _windowTop;
private double _windowLeft;
private double _windowHeight;
private double _windowWidth;
private System.Windows.WindowState _windowState;
private int _syncInterval;
private bool _syncEnabled;
private string _p4port;
private string _p4user;
private string _p4client;
private string _p4ticket;
#endregion //Member Variables
#region Public Properties
public double WindowTop {
get { return _windowTop; }
set { _windowTop = value; }
}
public double WindowLeft {
get { return _windowLeft; }
set { _windowLeft = value; }
}
public double WindowHeight {
get { return _windowHeight; }
set { _windowHeight = value; }
}
public double WindowWidth {
get { return _windowWidth; }
set { _windowWidth = value; }
}
public System.Windows.WindowState WindowState {
get { return _windowState; }
set { _windowState = value; }
}
public int SyncInterval {
get { return _syncInterval; }
set { _syncInterval = value; }
}
public bool SyncEnabled {
get { return _syncEnabled; }
set { _syncEnabled = value; }
}
public string LastP4Port {
get { return _p4port; }
set { _p4port = value; }
}
public string LastP4User {
get { return _p4user; }
set { _p4user = value; }
}
public string LastP4Client {
get { return _p4client; }
set { _p4client = value; }
}
public string LastP4Ticket {
get { return _p4ticket; }
set { _p4ticket = value; }
}
#endregion //Public Properties
#region Constructor
public UserPreferences() {
//Load the settings
Load();
//Size it to fit the current screen
SizeToFit();
//Move the window at least partially into view
MoveIntoView();
Utility.SetProperty(Constants.USER_PREFERENCES, this);
}
#endregion //Constructor
#region Functions
///
/// If the saved window dimensions are larger than the current screen shrink the
/// window to fit.
///
public void SizeToFit() {
if (_windowHeight > System.Windows.SystemParameters.VirtualScreenHeight) {
_windowHeight = System.Windows.SystemParameters.VirtualScreenHeight;
}
if (_windowWidth > System.Windows.SystemParameters.VirtualScreenWidth) {
_windowWidth = System.Windows.SystemParameters.VirtualScreenWidth;
}
}
///
/// If the window is more than half off of the screen move it up and to the left
/// so half the height and half the width are visible.
///
public void MoveIntoView() {
if (_windowTop + _windowHeight / 2 > System.Windows.SystemParameters.VirtualScreenHeight) {
_windowTop = System.Windows.SystemParameters.VirtualScreenHeight - _windowHeight;
}
if (_windowLeft + _windowWidth / 2 > System.Windows.SystemParameters.VirtualScreenWidth) {
_windowLeft = System.Windows.SystemParameters.VirtualScreenWidth - _windowWidth;
}
if (_windowTop < 0) {
_windowTop = 0;
}
if (_windowLeft < 0) {
_windowLeft = 0;
}
}
private void Load() {
// window settings
_windowTop = Properties.Settings.Default.WindowTop;
_windowLeft = Properties.Settings.Default.WindowLeft;
_windowHeight = Properties.Settings.Default.WindowHeight;
_windowWidth = Properties.Settings.Default.WindowWidth;
_windowState = Properties.Settings.Default.WindowState;
// other propreties
_syncInterval = Properties.Settings.Default.SyncInterval;
_syncEnabled = Properties.Settings.Default.SyncEnabled;
if (!string.IsNullOrEmpty(Properties.Settings.Default.LastP4Port)) {
_p4port = Properties.Settings.Default.LastP4Port;
}
if (!string.IsNullOrEmpty(Properties.Settings.Default.LastP4User)) {
_p4user = Properties.Settings.Default.LastP4User;
}
if (!string.IsNullOrEmpty(Properties.Settings.Default.LastP4Client)) {
_p4client = Properties.Settings.Default.LastP4Client;
}
if (!string.IsNullOrEmpty(Properties.Settings.Default.LastP4Ticket)) {
_p4ticket = Properties.Settings.Default.LastP4Ticket;
}
}
public void Save() {
if (_windowState != System.Windows.WindowState.Minimized) {
Properties.Settings.Default.WindowTop = _windowTop;
Properties.Settings.Default.WindowLeft = _windowLeft;
Properties.Settings.Default.WindowHeight = _windowHeight;
Properties.Settings.Default.WindowWidth = _windowWidth;
Properties.Settings.Default.WindowState = _windowState;
Properties.Settings.Default.SyncInterval = _syncInterval;
Properties.Settings.Default.SyncEnabled = _syncEnabled;
Properties.Settings.Default.LastP4Port = _p4port;
Properties.Settings.Default.LastP4User = _p4user;
Properties.Settings.Default.LastP4Client = _p4client;
Properties.Settings.Default.Save();
}
}
public void SaveProperty(string propname) {
var changed = false;
switch (propname) {
case Constants.PREFS_SYNCINTERVAL:
Properties.Settings.Default.SyncInterval = _syncInterval;
changed = true;
break;
case Constants.PREFS_SYNCENABLED:
Properties.Settings.Default.SyncEnabled = _syncEnabled;
changed = true;
break;
case Constants.PREFS_LASTP4PORT:
Properties.Settings.Default.LastP4Port = _p4port;
changed = true;
break;
case Constants.PREFS_LASTP4USER:
Properties.Settings.Default.LastP4User = _p4user;
changed = true;
break;
case Constants.PREFS_LASTP4CLIENT:
Properties.Settings.Default.LastP4Client = _p4client;
changed = true;
break;
case Constants.PREFS_LASTP4TICKET:
Properties.Settings.Default.LastP4Ticket = _p4ticket;
changed = true;
break;
}
if (changed) {
Properties.Settings.Default.Save();
}
}
#endregion //Functions
}
}