Skip to content

Commit 40c3f49

Browse files
committed
Added window management options for Torch UI.
- Introduced settings to save window size/position, start minimized, and minimize on server start. - Implemented logic to restore window position only if it remains visible on connected screens.
1 parent 26064e8 commit 40c3f49

3 files changed

Lines changed: 88 additions & 10 deletions

File tree

‎Torch.API/ITorchConfig.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ public interface ITorchConfig
2626
string ChatColor { get; set; }
2727
string TestPlugin { get; set; }
2828
bool DisconnectOnRestart { get; set; }
29+
bool SaveWindowChanges { get; set; }
2930
int WindowWidth { get; set; }
3031
int WindowHeight { get; set; }
32+
int WindowX { get; set; }
33+
int WindowY { get; set; }
3134
int FontSize { get; set; }
35+
bool StartMinimized { get; set; }
36+
bool MinimizeOnServerStart { get; set; }
3237
UGCServiceType UgcServiceType { get; set; }
3338
TorchBranchType BranchName { get; set; }
3439
bool SendLogsToKeen { get; set; }

‎Torch.Server/TorchConfig.cs‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
using System.ComponentModel;
44
using System.IO;
55
using System.Runtime.CompilerServices;
6-
using System.Windows;
76
using System.Xml.Serialization;
8-
using Newtonsoft.Json;
97
using NLog;
108
using Torch.API;
119
using Torch.Views;
12-
using VRage.Game;
1310

1411
namespace Torch.Server
1512
{
@@ -37,8 +34,13 @@ public class TorchConfig : CommandLine, ITorchConfig, INotifyPropertyChanged
3734
private string _chatColor = "Red";
3835
private bool _enableWhitelist = false;
3936
private List<ulong> _whitelist = new List<ulong>();
40-
private int _windowWidth = 980;
41-
private int _windowHeight = 588;
37+
private bool _saveWindowChanges = true;
38+
private bool _startMinimized = false;
39+
private bool _minimizeOnServerStart = false;
40+
private int _windowWidth;
41+
private int _windowHeight;
42+
private int _windowX;
43+
private int _windowY;
4244
private bool _independentConsole = false;
4345
private bool _enableAsserts = false;
4446
private int _fontSize = 16;
@@ -153,11 +155,26 @@ public string InstancePath
153155
[Display(Name = "Whitelist", Description = "Collection of whitelisted steam ids.", GroupName = "In-Game")]
154156
public List<ulong> Whitelist { get => _whitelist; set => Set(value, ref _whitelist); }
155157

158+
[Display(Name = "Save Window Changes", Description = "Save window size and location.", GroupName = "Window")]
159+
public bool SaveWindowChanges { get => _saveWindowChanges; set => Set(value, ref _saveWindowChanges); }
160+
161+
[Display(Name = "Start Minimized", Description = "Start Torch minimized.", GroupName = "Window")]
162+
public bool StartMinimized { get => _startMinimized; set => Set(value, ref _startMinimized); }
163+
164+
[Display(Name = "Minimize On Server Start", Description = "Minimize Torch window on server start.", GroupName = "Window")]
165+
public bool MinimizeOnServerStart { get => _minimizeOnServerStart; set => Set(value, ref _minimizeOnServerStart); }
166+
156167
[Display(Name = "Width", Description = "Default window width.", GroupName = "Window")]
157168
public int WindowWidth { get => _windowWidth; set => Set(value, ref _windowWidth); }
158169

159170
[Display(Name = "Height", Description = "Default window height", GroupName = "Window")]
160171
public int WindowHeight { get => _windowHeight; set => Set(value, ref _windowHeight); }
172+
173+
[Display(Name = "WindowX", Description = "Default window X position", GroupName = "Window")]
174+
public int WindowX { get => _windowX; set => Set(value, ref _windowX); }
175+
176+
[Display(Name = "WindowY", Description = "Default window Y position", GroupName = "Window")]
177+
public int WindowY { get => _windowY; set => Set(value, ref _windowY); }
161178

162179
[Display(Name = "Font Size", Description = "Font size for logging text box. (default is 16)", GroupName = "Window")]
163180
public int FontSize { get => _fontSize; set => Set(value, ref _fontSize); }
@@ -176,7 +193,7 @@ public TorchBranchType BranchName
176193
set => Set(value, ref _torchBranch);
177194
}
178195

179-
public string LastUsedTheme { get; set; } = "Torch Theme";
196+
public string LastUsedTheme { get; set; } = "Torch Theme";
180197

181198
//Prevent reserved players being written to disk, but allow it to be read
182199
//remove this when ReservedPlayers is removed

‎Torch.Server/Views/TorchUI.xaml.cs‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using System.Timers;
89
using System.Windows;
910
using System.Windows.Controls;
1011
using System.Windows.Data;
1112
using System.Windows.Documents;
13+
using System.Windows.Forms;
1214
using System.Windows.Input;
1315
using System.Windows.Media;
1416
using System.Windows.Media.Imaging;
@@ -22,7 +24,10 @@
2224
using Torch.API.Managers;
2325
using Torch.Patches;
2426
using Torch.Server.Managers;
27+
using MessageBox = System.Windows.MessageBox;
2528
using MessageBoxResult = System.Windows.MessageBoxResult;
29+
using Rectangle = System.Drawing.Rectangle;
30+
using TextBox = System.Windows.Controls.TextBox;
2631

2732
namespace Torch.Server
2833
{
@@ -41,12 +46,11 @@ public partial class TorchUI : Window
4146
private System.Windows.Forms.Timer _scrollTimer;
4247
public TorchUI(TorchServer server)
4348
{
44-
WindowStartupLocation = WindowStartupLocation.Manual;
4549
_config = (TorchConfig)server.Config;
46-
Width = _config.WindowWidth;
47-
Height = _config.WindowHeight;
50+
WindowStartupLocation = WindowStartupLocation.Manual;
51+
4852
_server = server;
49-
//TODO: data binding for whole server
53+
// TODO: data binding for whole server
5054
DataContext = server;
5155
InitializeComponent();
5256

@@ -91,6 +95,58 @@ private void TorchUI_Loaded(object sender, RoutedEventArgs e)
9195
_scrollTimer.Tick += ScrollIfNeed;
9296
_scrollTimer.Interval = 120;
9397
_scrollTimer.Start();
98+
99+
// Set the default window size if no position is saved
100+
if ( _config.WindowWidth == 0 || _config.WindowHeight == 0)
101+
{
102+
Width = 980;
103+
Height = 588;
104+
}
105+
else
106+
{
107+
Width = _config.WindowWidth;
108+
Height = _config.WindowHeight;
109+
}
110+
111+
// Only restore if visible on a screen, otherwise let windows position it.
112+
const int tolerance = 10;
113+
var rect = new Rectangle(_config.WindowX, _config.WindowY, _config.WindowWidth, _config.WindowHeight);
114+
if (Screen.AllScreens.Any(s =>
115+
{
116+
Rectangle area = s.WorkingArea;
117+
area.Inflate(tolerance, tolerance);
118+
return area.Contains(rect);
119+
}))
120+
{
121+
Left = _config.WindowX;
122+
Top = _config.WindowY;
123+
}
124+
125+
LocationChanged += (_, args) =>
126+
{
127+
if (!_config.SaveWindowChanges) return;
128+
_config.WindowX = (int)Top;
129+
_config.WindowY = (int)Left;
130+
_config.Save();
131+
};
132+
SizeChanged += (_, args) =>
133+
{
134+
if (!_config.SaveWindowChanges) return;
135+
_config.WindowHeight = (int)args.NewSize.Height;
136+
_config.WindowWidth = (int)args.NewSize.Width;
137+
};
138+
139+
if (_config.StartMinimized)
140+
WindowState = WindowState.Minimized;
141+
142+
_server.GameStateChanged += (game, state) =>
143+
{
144+
if (state == TorchGameState.Loaded && _config.MinimizeOnServerStart)
145+
BtnStart.Dispatcher.Invoke(() => // Cheap way to get to UI thread
146+
{
147+
WindowState = WindowState.Minimized;
148+
});
149+
};
94150
}
95151

96152
private void ScrollIfNeed(object sender, EventArgs e)

0 commit comments

Comments
 (0)