diff --git a/Torch.API/ITorchConfig.cs b/Torch.API/ITorchConfig.cs index d7c2fee4..52507884 100644 --- a/Torch.API/ITorchConfig.cs +++ b/Torch.API/ITorchConfig.cs @@ -26,9 +26,14 @@ public interface ITorchConfig string ChatColor { get; set; } string TestPlugin { get; set; } bool DisconnectOnRestart { get; set; } + bool SaveWindowChanges { get; set; } int WindowWidth { get; set; } int WindowHeight { get; set; } + int WindowX { get; set; } + int WindowY { get; set; } int FontSize { get; set; } + bool StartMinimized { get; set; } + bool MinimizeOnServerStart { get; set; } UGCServiceType UgcServiceType { get; set; } TorchBranchType BranchName { get; set; } bool SendLogsToKeen { get; set; } diff --git a/Torch.Server/TorchConfig.cs b/Torch.Server/TorchConfig.cs index ce329467..697a694c 100644 --- a/Torch.Server/TorchConfig.cs +++ b/Torch.Server/TorchConfig.cs @@ -3,13 +3,10 @@ using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; -using System.Windows; using System.Xml.Serialization; -using Newtonsoft.Json; using NLog; using Torch.API; using Torch.Views; -using VRage.Game; namespace Torch.Server { @@ -37,8 +34,13 @@ public class TorchConfig : CommandLine, ITorchConfig, INotifyPropertyChanged private string _chatColor = "Red"; private bool _enableWhitelist = false; private List _whitelist = new List(); - private int _windowWidth = 980; - private int _windowHeight = 588; + private bool _saveWindowChanges = true; + private bool _startMinimized = false; + private bool _minimizeOnServerStart = false; + private int _windowWidth; + private int _windowHeight; + private int _windowX; + private int _windowY; private bool _independentConsole = false; private bool _enableAsserts = false; private int _fontSize = 16; @@ -153,11 +155,26 @@ public string InstancePath [Display(Name = "Whitelist", Description = "Collection of whitelisted steam ids.", GroupName = "In-Game")] public List Whitelist { get => _whitelist; set => Set(value, ref _whitelist); } + [Display(Name = "Save Window Changes", Description = "Save window size and location.", GroupName = "Window")] + public bool SaveWindowChanges { get => _saveWindowChanges; set => Set(value, ref _saveWindowChanges); } + + [Display(Name = "Start Minimized", Description = "Start Torch minimized.", GroupName = "Window")] + public bool StartMinimized { get => _startMinimized; set => Set(value, ref _startMinimized); } + + [Display(Name = "Minimize On Server Start", Description = "Minimize Torch window on server start.", GroupName = "Window")] + public bool MinimizeOnServerStart { get => _minimizeOnServerStart; set => Set(value, ref _minimizeOnServerStart); } + [Display(Name = "Width", Description = "Default window width.", GroupName = "Window")] public int WindowWidth { get => _windowWidth; set => Set(value, ref _windowWidth); } [Display(Name = "Height", Description = "Default window height", GroupName = "Window")] public int WindowHeight { get => _windowHeight; set => Set(value, ref _windowHeight); } + + [Display(Name = "WindowX", Description = "Default window X position", GroupName = "Window")] + public int WindowX { get => _windowX; set => Set(value, ref _windowX); } + + [Display(Name = "WindowY", Description = "Default window Y position", GroupName = "Window")] + public int WindowY { get => _windowY; set => Set(value, ref _windowY); } [Display(Name = "Font Size", Description = "Font size for logging text box. (default is 16)", GroupName = "Window")] public int FontSize { get => _fontSize; set => Set(value, ref _fontSize); } @@ -176,7 +193,7 @@ public TorchBranchType BranchName set => Set(value, ref _torchBranch); } -public string LastUsedTheme { get; set; } = "Torch Theme"; + public string LastUsedTheme { get; set; } = "Torch Theme"; //Prevent reserved players being written to disk, but allow it to be read //remove this when ReservedPlayers is removed diff --git a/Torch.Server/Views/TorchUI.xaml.cs b/Torch.Server/Views/TorchUI.xaml.cs index a63c8152..4f4d4dc2 100644 --- a/Torch.Server/Views/TorchUI.xaml.cs +++ b/Torch.Server/Views/TorchUI.xaml.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Timers; @@ -9,6 +10,7 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; +using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -22,7 +24,10 @@ using Torch.API.Managers; using Torch.Patches; using Torch.Server.Managers; +using MessageBox = System.Windows.MessageBox; using MessageBoxResult = System.Windows.MessageBoxResult; +using Rectangle = System.Drawing.Rectangle; +using TextBox = System.Windows.Controls.TextBox; namespace Torch.Server { @@ -41,12 +46,11 @@ public partial class TorchUI : Window private System.Windows.Forms.Timer _scrollTimer; public TorchUI(TorchServer server) { - WindowStartupLocation = WindowStartupLocation.Manual; _config = (TorchConfig)server.Config; - Width = _config.WindowWidth; - Height = _config.WindowHeight; + WindowStartupLocation = WindowStartupLocation.Manual; + _server = server; - //TODO: data binding for whole server + // TODO: data binding for whole server DataContext = server; InitializeComponent(); @@ -91,6 +95,58 @@ private void TorchUI_Loaded(object sender, RoutedEventArgs e) _scrollTimer.Tick += ScrollIfNeed; _scrollTimer.Interval = 120; _scrollTimer.Start(); + + // Set the default window size if no position is saved + if ( _config.WindowWidth == 0 || _config.WindowHeight == 0) + { + Width = 980; + Height = 588; + } + else + { + Width = _config.WindowWidth; + Height = _config.WindowHeight; + } + + // Only restore if visible on a screen, otherwise let windows position it. + const int tolerance = 10; + var rect = new Rectangle(_config.WindowX, _config.WindowY, _config.WindowWidth, _config.WindowHeight); + if (Screen.AllScreens.Any(s => + { + Rectangle area = s.WorkingArea; + area.Inflate(tolerance, tolerance); + return area.Contains(rect); + })) + { + Left = _config.WindowX; + Top = _config.WindowY; + } + + LocationChanged += (_, args) => + { + if (!_config.SaveWindowChanges) return; + _config.WindowX = (int)Top; + _config.WindowY = (int)Left; + _config.Save(); + }; + SizeChanged += (_, args) => + { + if (!_config.SaveWindowChanges) return; + _config.WindowHeight = (int)args.NewSize.Height; + _config.WindowWidth = (int)args.NewSize.Width; + }; + + if (_config.StartMinimized) + WindowState = WindowState.Minimized; + + _server.GameStateChanged += (game, state) => + { + if (state == TorchGameState.Loaded && _config.MinimizeOnServerStart) + BtnStart.Dispatcher.Invoke(() => // Cheap way to get to UI thread + { + WindowState = WindowState.Minimized; + }); + }; } private void ScrollIfNeed(object sender, EventArgs e)