-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
800 lines (717 loc) · 31.8 KB
/
MainForm.cs
File metadata and controls
800 lines (717 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using System.Linq;
namespace GateSwitchWay
{
public partial class MainForm : Form
{
private static bool isAutoStartOn = false;
private static bool startHidden = false;
private static bool isLoadingSettings = false;
private static bool isAutoAlterOn = false;
// Dark mode color constants
private static readonly Color DarkFormBackground = Color.FromArgb(32, 32, 32); // #202020
private static readonly Color DarkGroupBoxBackground = Color.FromArgb(45, 45, 45); // #2D2D2D
private static readonly Color DarkControlBackground = Color.FromArgb(55, 55, 55); // #373737
private static readonly Color DarkButtonBackground = Color.FromArgb(60, 60, 60); // #3C3C3C
private static readonly Color DarkButtonBorder = Color.FromArgb(80, 80, 80); // #505050
private static readonly Color DarkForeground = Color.LightGray;
private NetworkHelper.NetworkInfo currentNetworkInfo;
private NetworkHelper.NetworkInfo alternativeNetworkInfo;
// Track the last applied theme to avoid unnecessary reapplications
private bool? lastAppliedThemeWasLight = null;
// Auto-refresh timer for network status
private System.Windows.Forms.Timer autoRefreshTimer = new System.Windows.Forms.Timer();
public MainForm()
{
InitializeComponent();
// Apply initial theme before any other UI operations
ApplyThemeToMainForm();
// Subscribe to system theme changes
Microsoft.Win32.SystemEvents.UserPreferenceChanged += OnSystemThemeChanged;
isAutoStartOn = AppAutoStart.GetAutoStart();
autoStartMenu.Checked = isAutoStartOn;
startHidden = Settings.Default.StartHidden;
startHiddenMenu.Checked = startHidden;
// Load AutoAlter setting
isAutoAlterOn = Settings.Default.AutoAlter;
autoAlterMenu.Checked = isAutoAlterOn;
isSwitchedOn = false;
notifyIcon1.Icon = isSwitchedOn ? Res.gw64_yg_TEA_icon : Res.gw64_g_vzI_icon;
this.Icon = isSwitchedOn ? Res.gw64_yg_TEA_icon : Res.gw64_g_vzI_icon;
currentNetworkInfo = NetworkHelper.GetCurrentNetworkInfo(); // Call the method to update the network info on startup
isLoadingSettings = true;
NetworkHelper.LoadAlterNativeSettings(textBoxGw4, textBoxGw6, textBoxDns4, textBoxDns6, checkBoxGw4, checkBoxGw6, checkBoxDns4, checkBoxDns6);
// Load native settings to UI
NetworkHelper.LoadNativeSettingsToUI(textBoxNativeGw4, textBoxNativeGw6, textBoxNativeDns4, textBoxNativeDns6, checkBoxNativeGw4, checkBoxNativeGw6, checkBoxNativeDns4, checkBoxNativeDns6);
// Initialize auto-refresh settings
checkBoxAutoRefresh.Checked = Settings.Default.AutoRefreshEnabled;
numericUpDownRefreshInterval.Value = Settings.Default.AutoRefreshInterval / 1000; // Convert to seconds
numericUpDownRefreshInterval.Minimum = 1;
numericUpDownRefreshInterval.Maximum = 300; // 5 minutes max
// Check if there are any saved AlterNative settings
alternativeNetworkInfo = NetworkHelper.GetAlterNativeSettings();
if ((alternativeNetworkInfo.Gateway4 == "N/A") && (alternativeNetworkInfo.Gateway6 == "N/A") && (alternativeNetworkInfo.Dns4 == "N/A") && (alternativeNetworkInfo.Dns6 == "N/A"))
{
// No saved alternative settings, save current settings as native settings
NetworkHelper.SaveNativeSettings(currentNetworkInfo);
}
else
{
// Compare current settings with saved alternative settings
if (NetworkHelper.AreSettingsEqual(currentNetworkInfo, alternativeNetworkInfo))
{
// Current settings are equal to saved alternative settings, restore native settings from storage
currentNetworkInfo = NetworkHelper.LoadNativeSettings();
//check if restored native settings are valid
if (currentNetworkInfo.Gateway4 == "N/A" && currentNetworkInfo.Gateway6 == "N/A" && currentNetworkInfo.Dns4 == "N/A" && currentNetworkInfo.Dns6 == "N/A")
{
// restored settings are invalid, check if need to do anything else here
}
else
{
// restore native settings from saved settings from previous run
// Update the network info to reflect the restored native settings
NetworkHelper.TemporaryModifyNetworkInfo(currentNetworkInfo);
}
}
else
{
// Current settings are different from saved alternative settings, save current settings as new native settings
NetworkHelper.SaveNativeSettings(currentNetworkInfo);
}
}
// Update the native network info box to be consistent with native settings
NetworkHelper.PopulateNetworkInfoTextBoxes(currentNetworkInfo, textBoxNativeGw4, textBoxNativeGw6, textBoxNativeDns4, textBoxNativeDns6);
// Switch to AlterNative mode if AutoAlter is enabled
if (isAutoAlterOn)
{
ToggleSwitch();
}
NetworkHelper.UpdateTaskbarNetworkInfo(currentNetworkInfo, notifyIcon1, isSwitchedOn); // Display the current network info
NetworkHelper.PopulateNetworkInfoTextBoxes(currentNetworkInfo, textBoxCurrentGw4, textBoxCurrentGw6, textBoxCurrentDns4, textBoxCurrentDns6);
isLoadingSettings = false;
TrackBar_Set(isSwitchedOn);
// Setup auto-refresh timer
SetupAutoRefreshTimer();
clickTimer.Interval = SystemInformation.DoubleClickTime - 1; // Just under the double-click speed
clickTimer.Tick += (s, e) =>
{
clickTimer.Stop(); // Stop the timer
// Perform the single-click action if not a double-click
if (!isDoubleClick)
{
ToggleSwitch(); // Switch mode with single-click
}
isDoubleClick = false; // Reset the flag
};
if (notifyIcon1.ContextMenuStrip is not null)
{
notifyIcon1.ContextMenuStrip.Opening += (sender, e) =>
{
UpdateContextMenuTheme(notifyIcon1.ContextMenuStrip);
autoStartMenu.Checked = AppAutoStart.GetAutoStart();
mainToolStripMenuItem.Checked = this.Visible;
refreshIntervalMenu.Checked = Settings.Default.AutoRefreshEnabled;
};
}
// Register the MouseClick event
this.notifyIcon1.MouseClick += new MouseEventHandler(notifyIcon1_MouseClick);
// Hide main window if startHidden is true
if (startHidden)
{
this.Hide();
}
}
private void SetupAutoRefreshTimer()
{
autoRefreshTimer.Interval = Settings.Default.AutoRefreshInterval; // Default 5 seconds from settings
autoRefreshTimer.Tick += AutoRefreshTimer_Tick;
// Start timer only if auto-refresh is enabled
if (Settings.Default.AutoRefreshEnabled)
{
autoRefreshTimer.Start();
}
}
private void AutoRefreshTimer_Tick(object sender, EventArgs e)
{
// Only update if auto-refresh is enabled
if (!Settings.Default.AutoRefreshEnabled)
{
autoRefreshTimer.Stop();
return;
}
// Get current network information
var latestNetworkInfo = NetworkHelper.GetCurrentNetworkInfo();
// Update current network info textboxes
NetworkHelper.PopulateNetworkInfoTextBoxes(latestNetworkInfo, textBoxCurrentGw4, textBoxCurrentGw6, textBoxCurrentDns4, textBoxCurrentDns6);
// Update taskbar network info
NetworkHelper.UpdateTaskbarNetworkInfo(latestNetworkInfo, notifyIcon1, isSwitchedOn);
// Update currentNetworkInfo for consistency
if (!isSwitchedOn)
{
currentNetworkInfo = latestNetworkInfo;
}
}
private void buttonSaveNative_Click(object sender, EventArgs e)
{
try
{
NetworkHelper.SaveNativeSettingsFromUI(textBoxNativeGw4, textBoxNativeGw6, textBoxNativeDns4, textBoxNativeDns6, checkBoxNativeGw4, checkBoxNativeGw6, checkBoxNativeDns4, checkBoxNativeDns6);
MessageBox.Show("Native network settings saved successfully!", "Settings Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save native settings: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonLoadNative_Click(object sender, EventArgs e)
{
try
{
var currentInfo = NetworkHelper.GetCurrentNetworkInfo();
NetworkHelper.PopulateNetworkInfoTextBoxes(currentInfo, textBoxNativeGw4, textBoxNativeGw6, textBoxNativeDns4, textBoxNativeDns6);
// Enable checkboxes for valid network settings
checkBoxNativeGw4.Checked = currentInfo.Gateway4_enable;
checkBoxNativeGw6.Checked = currentInfo.Gateway6_enable;
checkBoxNativeDns4.Checked = currentInfo.Dns4_enable;
checkBoxNativeDns6.Checked = currentInfo.Dns6_enable;
MessageBox.Show("Current network settings loaded into Native settings!", "Settings Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load current settings: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
// Update AlterNative settings display
AlterCheckBoxes_ReEnable();
// Update Native settings display
NativeCheckBoxes_ReEnable();
DisplayCurrentMode(isSwitchedOn);
TrackBar_Set(isSwitchedOn);
// Initialize auto-refresh UI state
isLoadingSettings = true;
checkBoxAutoRefresh.Checked = Settings.Default.AutoRefreshEnabled;
numericUpDownRefreshInterval.Value = Settings.Default.AutoRefreshInterval / 1000;
isLoadingSettings = false;
// Hide main window if startHidden is true
if (startHidden)
{
this.Hide();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private static bool firstTimeCloseClicked = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
// Check if the close reason is a user action (e.g., clicking the 'X' button)
if (e.CloseReason == CloseReason.UserClosing)
{
// Cancel the form's closure
e.Cancel = true;
// Hide the form
this.Hide();
// Optionally, show a notification or message indicating the application is still running
if (firstTimeCloseClicked)
{
notifyIcon1.ShowBalloonTip(600, "Attempt to close", $"{AppAutoStart.taskName} running in the background.\nDouble click system tray icon to show main window. Once click to toggle active mode.", ToolTipIcon.Info);
firstTimeCloseClicked = false;
}
}
else
{
// Allow the form to close for other close reasons (e.g., Application.Exit())
base.OnFormClosing(e);
}
}
// add theme support to context menu
public class DarkModeToolStripRenderer : ToolStripProfessionalRenderer
{
public DarkModeToolStripRenderer() : base(new DarkModeColorTable())
{
}
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
// Background of the entire menu
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(40, 40, 40)), e.AffectedBounds);
}
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
if (e.Item.Selected)
{
// Highlight color for selected items
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(70, 70, 70)), new Rectangle(Point.Empty, e.Item.Size));
}
else
{
// Regular item background color
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(40, 40, 40)), new Rectangle(Point.Empty, e.Item.Size));
}
}
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
// Override the space on the left of items (where check marks or images go)
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(40, 40, 40)), e.AffectedBounds);
}
// Add overrides for other elements as needed, like separators, arrows, etc.
}
public class DarkModeColorTable : ProfessionalColorTable
{
public override Color MenuItemSelected => Color.FromArgb(70, 70, 70);
public override Color MenuItemBorder => Color.FromArgb(70, 70, 70);
public override Color MenuBorder => Color.FromArgb(40, 40, 40);
public override Color MenuStripGradientBegin => Color.FromArgb(40, 40, 40);
public override Color MenuStripGradientEnd => Color.FromArgb(40, 40, 40);
public override Color SeparatorDark => Color.FromArgb(32, 100, 32); // Dark background
}
// Timer to handle single-click action delay
private System.Windows.Forms.Timer clickTimer = new System.Windows.Forms.Timer();
// Flag to distinguish between single and double clicks
private bool isDoubleClick = false;
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
// Check if the double click was with the left mouse button
if (e.Button == MouseButtons.Left)
{
isDoubleClick = true; // It's a double-click
clickTimer.Stop(); // Stop the timer to prevent the single-click action
if (this.Visible)
{
this.Hide();
}
else
{
activateMainWindow();
}
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
// Check if the click was with the left mouse button
if (e.Button == MouseButtons.Left)
{
isDoubleClick = false; // Assume it's not a double-click
clickTimer.Start(); // Start the delay timer
}
}
private void activateMainWindow()
{
this.Show();
// Ensure the window is not minimized, but keep maximized or other state
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate(); // Bring the form to the forefront
}
private static bool isSwitchedOn = false;
private void ToggleSwitch()
{
// Implement the switch on/off logic here
isSwitchedOn = !isSwitchedOn;
// set icon to `gw64_1_Jnv_icon` as not defined yet mode
notifyIcon1.Icon = Res.gw64_1_Jnv_icon;
this.Icon = Res.gw64_1_Jnv_icon;
if (isSwitchedOn)
{
// Switched on
UpdateAlternativeNetworkInfo();
NetworkHelper.TemporaryModifyNetworkInfo(alternativeNetworkInfo);
}
else
{
// Switched off
currentNetworkInfo = NetworkHelper.LoadNativeSettings();
NetworkHelper.TemporaryModifyNetworkInfo(currentNetworkInfo);
}
// Update the icon based on the new state
notifyIcon1.Icon = isSwitchedOn ? Res.gw64_yg_TEA_icon : Res.gw64_g_vzI_icon;
this.Icon = isSwitchedOn ? Res.gw64_yg_TEA_icon : Res.gw64_g_vzI_icon;
this.Text = isSwitchedOn ? "AlterNative GateWay" : "Native GateWay";
// Update the network info to be consistent with current mode
currentNetworkInfo = NetworkHelper.GetCurrentNetworkInfo();
// update current network info textboxes
NetworkHelper.PopulateNetworkInfoTextBoxes(currentNetworkInfo, textBoxCurrentGw4, textBoxCurrentGw6, textBoxCurrentDns4, textBoxCurrentDns6);
// Update taskbar popup network info to be consistent with current mode
NetworkHelper.UpdateTaskbarNetworkInfo(currentNetworkInfo, notifyIcon1, isSwitchedOn);
// Update the display of the current mode
DisplayCurrentMode(isSwitchedOn);
TrackBar_Set(isSwitchedOn);
}
private bool IsLightThemeEnabled()
{
var key = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
var valueName = "AppsUseLightTheme";
bool isLightTheme = false;
using (var regKey = Registry.CurrentUser.OpenSubKey(key))
{
if (regKey != null)
{
var value = regKey.GetValue(valueName);
if (value != null)
{
isLightTheme = Convert.ToInt32(value) != 0;
}
}
}
return isLightTheme; // Default to dark theme if unable to read the registry
}
private void UpdateContextMenuTheme(ContextMenuStrip contextMenu)
{
if (IsLightThemeEnabled())
{
contextMenu.BackColor = SystemColors.ControlLightLight; // Light background
contextMenu.ForeColor = Color.Black; // Dark text
}
else
{
contextMenu.BackColor = Color.FromArgb(255, 32, 32, 32); // Dark background
contextMenu.ForeColor = Color.LightGray; // Light text
}
// add dark mode custom renderer to display menu items properly
contextMenu.Renderer = IsLightThemeEnabled() ? new ToolStripProfessionalRenderer() : new DarkModeToolStripRenderer();
}
private void ApplyThemeToMainForm()
{
bool isLightTheme = IsLightThemeEnabled();
// Only apply theme if it has actually changed to avoid unnecessary UI updates
if (lastAppliedThemeWasLight.HasValue && lastAppliedThemeWasLight.Value == isLightTheme)
{
return;
}
lastAppliedThemeWasLight = isLightTheme;
// Set form colors
if (isLightTheme)
{
this.BackColor = SystemColors.Control;
this.ForeColor = SystemColors.ControlText;
}
else
{
this.BackColor = DarkFormBackground;
this.ForeColor = DarkForeground;
}
// Apply theme to all group boxes
ApplyThemeToGroupBox(groupBoxNative, isLightTheme);
ApplyThemeToGroupBox(groupBoxAlterNative, isLightTheme);
ApplyThemeToGroupBox(groupBoxCurrent, isLightTheme);
// Apply theme to trackbar
ApplyThemeToTrackBar(trackBarToggle, isLightTheme);
}
private void ApplyThemeToGroupBox(GroupBox groupBox, bool isLightTheme)
{
if (groupBox == null) return;
if (isLightTheme)
{
groupBox.BackColor = SystemColors.Control;
groupBox.ForeColor = SystemColors.ControlText;
}
else
{
groupBox.BackColor = DarkGroupBoxBackground;
groupBox.ForeColor = DarkForeground;
}
// Apply theme to all controls within the group box
foreach (Control control in groupBox.Controls)
{
ApplyThemeToControl(control, isLightTheme);
}
}
private void ApplyThemeToControl(Control control, bool isLightTheme)
{
if (control is TextBox textBox)
{
if (isLightTheme)
{
textBox.BackColor = SystemColors.Window;
textBox.ForeColor = SystemColors.WindowText;
}
else
{
textBox.BackColor = DarkControlBackground;
textBox.ForeColor = DarkForeground;
}
}
else if (control is Button button)
{
if (isLightTheme)
{
button.BackColor = SystemColors.Control;
button.ForeColor = SystemColors.ControlText;
button.FlatStyle = FlatStyle.Standard;
}
else
{
button.BackColor = DarkButtonBackground;
button.ForeColor = DarkForeground;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderColor = DarkButtonBorder;
}
}
else if (control is CheckBox checkBox)
{
if (isLightTheme)
{
checkBox.ForeColor = SystemColors.ControlText;
}
else
{
checkBox.ForeColor = DarkForeground;
}
}
else if (control is Label label)
{
if (isLightTheme)
{
label.ForeColor = SystemColors.ControlText;
}
else
{
label.ForeColor = DarkForeground;
}
}
else if (control is NumericUpDown numericUpDown)
{
if (isLightTheme)
{
numericUpDown.BackColor = SystemColors.Window;
numericUpDown.ForeColor = SystemColors.WindowText;
}
else
{
numericUpDown.BackColor = DarkControlBackground;
numericUpDown.ForeColor = DarkForeground;
}
}
}
private void ApplyThemeToTrackBar(TrackBar trackBar, bool isLightTheme)
{
if (trackBar == null) return;
if (isLightTheme)
{
trackBar.BackColor = SystemColors.Control;
}
else
{
trackBar.BackColor = DarkFormBackground;
}
}
private void OnSystemThemeChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
{
// Only respond to theme changes (General category includes theme changes)
if (e.Category == Microsoft.Win32.UserPreferenceCategory.General)
{
// Apply the new theme
if (this.InvokeRequired)
{
this.BeginInvoke(ApplyThemeToMainForm);
}
else
{
ApplyThemeToMainForm();
}
}
}
private void autoStartMenu_Click(object sender, EventArgs e)
{
// Toggle the auto-start setting based on its current state
bool currentState = AppAutoStart.GetAutoStart();
bool success = AppAutoStart.SetAutoStart(!currentState);
if (success)
{
// Update the check state of the menu item to reflect the new state
autoStartMenu.Checked = !currentState;
}
else
{
MessageBox.Show("Failed to toggle auto-start setting.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void mainToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.Visible)
{
if (this.WindowState == FormWindowState.Minimized)
{
activateMainWindow();
}
else
{
this.Hide();
}
}
else
{
activateMainWindow();
}
}
private void startHiddenMenu_Click(object sender, EventArgs e)
{
startHidden = !startHidden;
startHiddenMenu.Checked = startHidden;
Settings.Default.StartHidden = startHidden;
Settings.Default.Save();
}
private void autoAlterMenu_Click(object sender, EventArgs e)
{
isAutoAlterOn = !isAutoAlterOn;
autoAlterMenu.Checked = isAutoAlterOn;
Settings.Default.AutoAlter = isAutoAlterOn;
Settings.Default.Save();
}
private void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox? checkBox = sender as CheckBox;
if (checkBox != null)
{
bool needSave = false;
bool needNativeSave = false;
switch (checkBox.Name)
{
case "checkBoxGw4":
case "checkBoxGw6":
case "checkBoxDns4":
case "checkBoxDns6":
{
AlterCheckBoxes_ReEnable();
needSave = true;
break;
}
case "checkBoxNativeGw4":
case "checkBoxNativeGw6":
case "checkBoxNativeDns4":
case "checkBoxNativeDns6":
{
NativeCheckBoxes_ReEnable();
needNativeSave = true;
break;
}
case "checkBoxAutoRefresh":
{
if (!isLoadingSettings)
{
Settings.Default.AutoRefreshEnabled = checkBoxAutoRefresh.Checked;
Settings.Default.Save();
if (checkBoxAutoRefresh.Checked)
{
autoRefreshTimer.Start();
}
else
{
autoRefreshTimer.Stop();
}
}
break;
}
}
if (!isLoadingSettings)
{
if (needSave)
{
NetworkHelper.SaveAlterNativeSettings(textBoxGw4, textBoxGw6, textBoxDns4, textBoxDns6, checkBoxGw4, checkBoxGw6, checkBoxDns4, checkBoxDns6);
}
if (needNativeSave)
{
NetworkHelper.SaveNativeSettingsFromUI(textBoxNativeGw4, textBoxNativeGw6, textBoxNativeDns4, textBoxNativeDns6, checkBoxNativeGw4, checkBoxNativeGw6, checkBoxNativeDns4, checkBoxNativeDns6);
}
}
}
}
private void refreshIntervalMenu_Click(object sender, EventArgs e)
{
// Toggle auto-refresh setting
checkBoxAutoRefresh.Checked = !checkBoxAutoRefresh.Checked;
}
private void NumericUpDownRefreshInterval_ValueChanged(object sender, EventArgs e)
{
if (!isLoadingSettings)
{
int newInterval = (int)(numericUpDownRefreshInterval.Value * 1000); // Convert to milliseconds
Settings.Default.AutoRefreshInterval = newInterval;
Settings.Default.Save();
// Update timer interval
autoRefreshTimer.Interval = newInterval;
// Restart timer if it's running
if (autoRefreshTimer.Enabled)
{
autoRefreshTimer.Stop();
autoRefreshTimer.Start();
}
}
}
private void AlterCheckBoxes_ReEnable()
{
textBoxGw4.Enabled = checkBoxGw4.Checked;
textBoxGw6.Enabled = checkBoxGw6.Checked;
textBoxDns4.Enabled = checkBoxDns4.Checked;
textBoxDns6.Enabled = checkBoxDns6.Checked;
}
private void NativeCheckBoxes_ReEnable()
{
textBoxNativeGw4.Enabled = checkBoxNativeGw4.Checked;
textBoxNativeGw6.Enabled = checkBoxNativeGw6.Checked;
textBoxNativeDns4.Enabled = checkBoxNativeDns4.Checked;
textBoxNativeDns6.Enabled = checkBoxNativeDns6.Checked;
}
private void TrackBarToggle_ValueChanged(object sender, EventArgs e)
{
if (!isLoadingSettings)
{
if (trackBarToggle.Value == 0)
{
if (isSwitchedOn)
{
ToggleSwitch();
}
}
else
{
if (!isSwitchedOn)
{
ToggleSwitch();
}
}
}
}
private void TrackBar_Set(bool isSwitchedOn)
{
isLoadingSettings = true;
trackBarToggle.Value = isSwitchedOn ? 1 : 0; // Set the trackbar value to match the current mode
isLoadingSettings = false;
}
private void DisplayCurrentMode(bool isSwitchedOn)
{
if (isSwitchedOn)
{
groupBoxAlterNative.Font = new Font(groupBoxAlterNative.Font, FontStyle.Bold);
groupBoxNative.Font = new Font(groupBoxNative.Font, FontStyle.Regular);
}
else
{
groupBoxAlterNative.Font = new Font(groupBoxAlterNative.Font, FontStyle.Regular);
groupBoxNative.Font = new Font(groupBoxNative.Font, FontStyle.Bold);
}
}
private void UpdateAlternativeNetworkInfo()
{
alternativeNetworkInfo.Gateway4_enable = checkBoxGw4.Checked;
alternativeNetworkInfo.Gateway4 = textBoxGw4.Text;
alternativeNetworkInfo.Gateway6_enable = checkBoxGw6.Checked;
alternativeNetworkInfo.Gateway6 = textBoxGw6.Text;
alternativeNetworkInfo.Dns4_enable = checkBoxDns4.Checked;
alternativeNetworkInfo.Dns4 = textBoxDns4.Text;
alternativeNetworkInfo.Dns6_enable = checkBoxDns6.Checked;
alternativeNetworkInfo.Dns6 = textBoxDns6.Text;
}
}
}