Skip to content

Commit 1f9a99b

Browse files
committed
Adjustments to Window Control and changed the ConfigControl tab.
- Moved the code into a private method to keep the main logic clean and fixed where I had the Top and Left switched on load. - ConfigControl no longer completely shuts down all access. - Loading it from code behind to pass the TorchServer allows access to the CanRun property. - Added logic to disable textboxes, listboxes, checkboxes, textboxes, buttons, etc. Admins can now see their settings without being able to adjust them.
1 parent 47a6e0a commit 1f9a99b

3 files changed

Lines changed: 77 additions & 42 deletions

File tree

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

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
using System.Runtime.CompilerServices;
66
using System.Windows;
77
using System.Windows.Controls;
8+
using System.Windows.Controls.Primitives;
89
using System.Windows.Data;
9-
using System.Windows.Media;
1010
using System.Windows.Threading;
1111
using Torch.API.Managers;
1212
using Torch.Server.Annotations;
1313
using Torch.Server.Managers;
1414
using Torch.Server.ViewModels;
15-
using Torch.Views;
1615
using VRage.Game.ModAPI;
1716
using VRage.Serialization;
1817

@@ -24,23 +23,31 @@ namespace Torch.Server.Views
2423
public partial class ConfigControl : UserControl, INotifyPropertyChanged
2524
{
2625
private InstanceManager _instanceManager;
27-
2826
private bool _configValid;
2927
public bool ConfigValid { get => _configValid; private set { _configValid = value; OnPropertyChanged(); } }
30-
3128
private List<BindingExpression> _bindingExpressions = new List<BindingExpression>();
29+
private TorchServer _server;
3230

33-
public ConfigControl()
31+
public ConfigControl(TorchServer server)
3432
{
3533
InitializeComponent();
34+
_server = server;
3635
_instanceManager = TorchBase.Instance.Managers.GetManager<InstanceManager>();
3736
_instanceManager.InstanceLoaded += _instanceManager_InstanceLoaded;
3837
DataContext = _instanceManager.DedicatedConfig;
3938
TorchSettings.DataContext = (TorchConfig)TorchBase.Instance.Config;
4039
// Gets called once all children are loaded
4140
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(ApplyStyles));
41+
42+
_server.PropertyChanged += (sender, args) =>
43+
{
44+
if (args.PropertyName == nameof(TorchServer.CanRun))
45+
{
46+
Dispatcher.Invoke(SetReadOnly);
47+
}
48+
};
4249
}
43-
50+
4451
private void CheckValid()
4552
{
4653
ConfigValid = !_bindingExpressions.Any(x => x.HasError);
@@ -149,5 +156,48 @@ private void RoleEdit_Onlick(object sender, RoutedEventArgs e)
149156
d.Edit(w.Checkpoint.PromotedUsers.Dictionary);
150157
_instanceManager.DedicatedConfig.Administrators = w.Checkpoint.PromotedUsers.Dictionary.Where(k => k.Value >= MyPromoteLevel.Admin).Select(k => k.Key.ToString()).ToList();
151158
}
159+
160+
private void SetReadOnly()
161+
{
162+
foreach (var textbox in GetAllChildren<TextBox>(this))
163+
{
164+
textbox.IsReadOnly = !_server.CanRun;
165+
}
166+
167+
foreach (var button in GetAllChildren<Button>(this))
168+
{
169+
button.IsEnabled = _server.CanRun;
170+
}
171+
172+
foreach (var comboBox in GetAllChildren<ComboBox>(this))
173+
{
174+
comboBox.IsEnabled = _server.CanRun;
175+
}
176+
177+
foreach (var slider in GetAllChildren<Slider>(this))
178+
{
179+
slider.IsEnabled = _server.CanRun;
180+
}
181+
182+
foreach (var checkbox in GetAllChildren<CheckBox>(this))
183+
{
184+
checkbox.IsEnabled = _server.CanRun;
185+
}
186+
187+
foreach (var toggleButton in GetAllChildren<ToggleButton>(this))
188+
{
189+
toggleButton.IsEnabled = _server.CanRun;
190+
}
191+
192+
foreach (var radioButton in GetAllChildren<RadioButton>(this))
193+
{
194+
radioButton.IsEnabled = _server.CanRun;
195+
}
196+
197+
foreach (var listBox in GetAllChildren<ListBox>(this))
198+
{
199+
listBox.IsEnabled = _server.CanRun;
200+
}
201+
}
152202
}
153203
}

‎Torch.Server/Views/TorchUI.xaml‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,7 @@
7070
<views:LogEventViewer />
7171
</TabItem>
7272

73-
<TabItem Header="Configuration">
74-
<Grid IsEnabled="{Binding Path=CanRun}">
75-
<Grid.RowDefinitions>
76-
<RowDefinition Height="Auto"/>
77-
<RowDefinition/>
78-
</Grid.RowDefinitions>
79-
<Grid Grid.Row="0">
80-
<Grid.ColumnDefinitions>
81-
<ColumnDefinition Width="Auto"/>
82-
<ColumnDefinition/>
83-
</Grid.ColumnDefinitions>
84-
</Grid>
85-
<views:ConfigControl Grid.Row="1" x:Name="ConfigControl" Margin="3" DockPanel.Dock="Bottom" IsEnabled="{Binding CanRun}"/>
86-
</Grid>
87-
</TabItem>
73+
<TabItem Header="Configuration" Name="ConfigTab" />
8874
<TabItem Header="Mods">
8975
<views:ModListControl/>
9076
</TabItem>

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6-
using System.Threading;
76
using System.Threading.Tasks;
8-
using System.Timers;
97
using System.Windows;
108
using System.Windows.Controls;
11-
using System.Windows.Data;
129
using System.Windows.Documents;
1310
using System.Windows.Forms;
1411
using System.Windows.Input;
1512
using System.Windows.Media;
16-
using System.Windows.Media.Imaging;
17-
using System.Windows.Navigation;
18-
using System.Windows.Shapes;
1913
using NLog;
2014
using NLog.Config;
2115
using NLog.Targets.Wrappers;
22-
using Sandbox;
2316
using Torch.API;
2417
using Torch.API.Managers;
2518
using Torch.Patches;
2619
using Torch.Server.Managers;
20+
using Torch.Server.Views;
2721
using MessageBox = System.Windows.MessageBox;
2822
using MessageBoxResult = System.Windows.MessageBoxResult;
2923
using Rectangle = System.Drawing.Rectangle;
@@ -48,11 +42,13 @@ public TorchUI(TorchServer server)
4842
{
4943
_config = (TorchConfig)server.Config;
5044
WindowStartupLocation = WindowStartupLocation.Manual;
51-
45+
SetWindow();
46+
5247
_server = server;
5348
// TODO: data binding for whole server
5449
DataContext = server;
5550
InitializeComponent();
51+
ConfigTab.Content = new ConfigControl(_server);
5652

5753
var config = LogManager.Configuration;
5854
var customTarget = new NlogCustomTarget()
@@ -95,9 +91,21 @@ private void TorchUI_Loaded(object sender, RoutedEventArgs e)
9591
_scrollTimer.Tick += ScrollIfNeed;
9692
_scrollTimer.Interval = 120;
9793
_scrollTimer.Start();
98-
94+
95+
_server.GameStateChanged += (game, state) =>
96+
{
97+
if (state == TorchGameState.Loaded && _config.MinimizeOnServerStart)
98+
BtnStart.Dispatcher.Invoke(() => // Cheap way to get to UI thread
99+
{
100+
WindowState = WindowState.Minimized;
101+
});
102+
};
103+
}
104+
105+
private void SetWindow()
106+
{
99107
// Set the default window size if no position is saved
100-
if ( _config.WindowWidth == 0 || _config.WindowHeight == 0)
108+
if ( _config.WindowWidth == 0 && _config.WindowHeight == 0)
101109
{
102110
Width = 980;
103111
Height = 588;
@@ -125,8 +133,8 @@ private void TorchUI_Loaded(object sender, RoutedEventArgs e)
125133
LocationChanged += (_, args) =>
126134
{
127135
if (!_config.SaveWindowChanges) return;
128-
_config.WindowX = (int)Top;
129-
_config.WindowY = (int)Left;
136+
_config.WindowX = (int)Left;
137+
_config.WindowY = (int)Top;
130138
_config.Save();
131139
};
132140
SizeChanged += (_, args) =>
@@ -138,15 +146,6 @@ private void TorchUI_Loaded(object sender, RoutedEventArgs e)
138146

139147
if (_config.StartMinimized)
140148
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-
};
150149
}
151150

152151
private void ScrollIfNeed(object sender, EventArgs e)

0 commit comments

Comments
 (0)