Skip to content

Portable Tool for an Ideal Windows Setup

refactor MainWindow by extracting logic and removing duplicate code

Greedeks8bd55e2f327f
diff --git a/.Source/GTweak/Behaviors/ScrollViewerBehavior.cs b/.Source/GTweak/Behaviors/ScrollViewerBehavior.cs
index 9c88138e..6a0ab199 100644
--- a/.Source/GTweak/Behaviors/ScrollViewerBehavior.cs
+++ b/.Source/GTweak/Behaviors/ScrollViewerBehavior.cs
@@ -8,8 +8,8 @@ namespace GTweak.Behaviors
internal static readonly DependencyProperty VerticalOffsetProperty =
DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVerticalOffsetChanged));
- internal static double GetVerticalOffset(ScrollViewer viewer) => (double)(viewer?.GetValue(VerticalOffsetProperty) ?? 0.0);
- internal static void SetVerticalOffset(ScrollViewer viewer, double value) => viewer?.SetValue(VerticalOffsetProperty, value);
+ internal static double GetVerticalOffset(DependencyObject d) => (double)(d?.GetValue(VerticalOffsetProperty) ?? 0.0);
+ internal static void SetVerticalOffset(DependencyObject d, double value) => d?.SetValue(VerticalOffsetProperty, value);
private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
diff --git a/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs b/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs
index 396c4fac..11e2c50f 100644
--- a/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs
+++ b/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs
@@ -127,6 +127,10 @@ namespace GTweak.Core.ViewModel
}
}
+ public ICommand ExportConfigCommand { get; set; }
+ public ICommand ImportConfigCommand { get; set; }
+ public ICommand SelfRemovalCommand { get; set; }
+
public ICommand NavigateToUtilsCommand { get; set; }
public ICommand NavigateToConfidentialityCommand { get; set; }
public ICommand NavigateToInterfaceCommand { get; set; }
@@ -156,6 +160,10 @@ namespace GTweak.Core.ViewModel
CurrentView = new UtilsView();
+ ExportConfigCommand = new RelayCommand(obj => GlobalOptions.SaveFileConfig());
+ ImportConfigCommand = new RelayCommand(obj => GlobalOptions.OpenFileConfig());
+ SelfRemovalCommand = new RelayCommand(obj => GlobalOptions.SelfRemoval());
+
NavigateToUtilsCommand = new RelayCommand(NavigateToUtils);
NavigateToConfidentialityCommand = new RelayCommand(NavigateToConfidentiality);
NavigateToInterfaceCommand = new RelayCommand(NavigateToInterface);
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csproj
index 3dd059f4..93e58231 100644
--- a/.Source/GTweak/GTweak.csproj
+++ b/.Source/GTweak/GTweak.csproj
@@ -225,6 +225,7 @@
<Compile Include="Modules\Configuration\HardwareData.cs" />
<Compile Include="Modules\Helpers\ControlDictionaryExtensions.cs" />
<Compile Include="Modules\Helpers\UnlockHandleHelper.cs" />
+ <Compile Include="Modules\Helpers\WindowExtensions.cs" />
<Compile Include="Modules\Maintenance\MemoryCleaner.cs" />
<Compile Include="Modules\Maintenance\RegistryExporter.cs" />
<Compile Include="Modules\Managers\BluetoothManager.cs" />
diff --git a/.Source/GTweak/Modules/Helpers/WindowExtensions.cs b/.Source/GTweak/Modules/Helpers/WindowExtensions.cs
new file mode 100644
index 00000000..8f83bab0
--- /dev/null
+++ b/.Source/GTweak/Modules/Helpers/WindowExtensions.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Windows;
+using System.Windows.Interop;
+
+namespace GTweak.Modules.Helpers
+{
+ internal static class WindowExtensions
+ {
+ private const int WM_NCLBUTTONDOWN = 0xA1;
+ private const int HTCAPTION = 0x2;
+
+ [DllImport("user32.dll")]
+ private static extern bool ReleaseCapture();
+
+ [DllImport("user32.dll")]
+ private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
+
+ internal static void Drag(this Window window)
+ {
+ IntPtr hwnd = new WindowInteropHelper(window).Handle;
+ ReleaseCapture();
+ SendMessage(hwnd, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero);
+ }
+ }
+}
diff --git a/.Source/GTweak/Windows/MainWindow.xaml b/.Source/GTweak/Windows/MainWindow.xaml
index 4a40f729..2eddfa1e 100644
--- a/.Source/GTweak/Windows/MainWindow.xaml
+++ b/.Source/GTweak/Windows/MainWindow.xaml
@@ -77,11 +77,11 @@
</Grid>
<StackPanel x:Name="TitleButtonsPanel" Grid.Column="2" Margin="0,0,0,2" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right">
- <ToggleButton x:Name="TglButtonTheme" IsChecked="{Binding StateButtonTheme}" Style="{StaticResource TglThemeSwitch}" Click="TitleButton_Click"/>
- <ToggleButton x:Name="TglButtonSettings" IsChecked="False" Style="{StaticResource TglSettings}" Margin="0,0,10,0" Click="TitleButton_Click"/>
- <Button x:Name="ButtonMinimize" Style="{StaticResource ButtonMinimize}" Click="TitleButton_Click"/>
- <Button x:Name="ButtonMaximize" Style="{StaticResource ButtonMaximize}" Click="TitleButton_Click"/>
- <Button x:Name="ButtonClose" Style="{StaticResource ButtonClose}" Margin="0,0,5,0" Click="TitleButton_Click"/>
+ <ToggleButton x:Name="TglButtonTheme" IsChecked="{Binding StateButtonTheme}" Style="{StaticResource TglThemeSwitch}" Click="TglButtonTheme_Click"/>
+ <ToggleButton x:Name="TglButtonSettings" IsChecked="False" Style="{StaticResource TglSettings}" Margin="0,0,10,0" Click="TglButtonSettings_Click"/>
+ <Button x:Name="ButtonMinimize" Style="{StaticResource ButtonMinimize}" Click="ButtonMinimize_Click"/>
+ <Button x:Name="ButtonMaximize" Style="{StaticResource ButtonMaximize}" Click="ButtonMaximize_Click"/>
+ <Button x:Name="ButtonClose" Style="{StaticResource ButtonClose}" Margin="0,0,5,0" Click="ButtonClose_Click"/>
</StackPanel>
</Grid>
@@ -162,7 +162,7 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
- <Button Grid.Column="0" Style="{StaticResource ButtonContent}" Height="34" PreviewMouseLeftButtonUp="BtnExport_PreviewMouseLeftButtonUp">
+ <Button Grid.Column="0" Style="{StaticResource ButtonContent}" Height="34" Command="{Binding ExportConfigCommand}">
<StackPanel Orientation="Horizontal">
<Image>
<Image.Style>
@@ -175,7 +175,7 @@
</StackPanel>
</Button>
- <Button Grid.Column="2" Style="{StaticResource ButtonContent}" Height="34" PreviewMouseLeftButtonUp="BtnImport_PreviewMouseLeftButtonUp">
+ <Button Grid.Column="2" Style="{StaticResource ButtonContent}" Height="34" Command="{Binding ImportConfigCommand}">
<StackPanel Orientation="Horizontal">
<Image>
<Image.Style>
@@ -189,7 +189,7 @@
</Button>
</Grid>
- <Button Margin="0,8,0,0" Style="{StaticResource ButtonContent}" Height="34" PreviewMouseLeftButtonUp="BtnDelete_PreviewMouseLeftButtonUp">
+ <Button Margin="0,8,0,0" Style="{StaticResource ButtonContent}" Height="34" Command="{Binding SelfRemovalCommand}">
<StackPanel Orientation="Horizontal">
<Image>
<Image.Style>
diff --git a/.Source/GTweak/Windows/MainWindow.xaml.cs b/.Source/GTweak/Windows/MainWindow.xaml.cs
index 20d032ec..1af910e8 100644
--- a/.Source/GTweak/Windows/MainWindow.xaml.cs
+++ b/.Source/GTweak/Windows/MainWindow.xaml.cs
@@ -1,17 +1,17 @@
using System;
using System.Diagnostics;
-using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
-using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using GTweak.Animations;
+using GTweak.Behaviors;
using GTweak.Modules.Common;
using GTweak.Modules.Configuration;
+using GTweak.Modules.Helpers;
using GTweak.Modules.Managers;
using Wpf.Ui.Controls;
@@ -19,26 +19,6 @@ namespace GTweak.Windows
{
public partial class MainWindow : FluentWindow
{
- [DllImport("user32.dll")]
- private static extern bool ReleaseCapture();
-
- [DllImport("user32.dll")]
- private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
-
- internal static readonly DependencyProperty CurrentVerticalOffsetProperty =
- DependencyProperty.RegisterAttached("CurrentVerticalOffset", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0, OnCurrentVerticalOffsetChanged));
-
- internal static void SetCurrentVerticalOffset(DependencyObject target, double value) => target.SetValue(CurrentVerticalOffsetProperty, value);
-
- private static void OnCurrentVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is ScrollViewer scrollViewer)
- {
- scrollViewer.ScrollToVerticalOffset((double)e.NewValue);
- }
- }
-
- private const int WM_NCLBUTTONDOWN = 0xA1, HTCAPTION = 0x2;
private bool _ignoreMouseClick = false;
private RadioButton _activeBtnCache;
@@ -98,61 +78,32 @@ namespace GTweak.Windows
}
else
{
- IntPtr hwnd = new WindowInteropHelper(this).Handle;
- ReleaseCapture();
- SendMessage(hwnd, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero);
+ this.Drag();
}
Dispatcher.BeginInvoke((Action)(() => { _ignoreMouseClick = false; TitleButtonsPanel.IsHitTestVisible = true; }));
}
}
- private void TitleButton_Click(object sender, RoutedEventArgs e)
+ private void ButtonClose_Click(object sender, RoutedEventArgs e) => Close();
+
+ private void ButtonMaximize_Click(object sender, RoutedEventArgs e) => HandleWindowState();
+
+ private void ButtonMinimize_Click(object sender, RoutedEventArgs e) => HandleWindowState(true);
+
+ private void TglButtonSettings_Click(object sender, RoutedEventArgs e)
{
- switch ((sender as FrameworkElement)?.Name)
+ SettingsPanel.CacheMode = new BitmapCache { RenderAtScale = 1 };
+ if (SettingsPanel.RenderTransform is TranslateTransform transform)
{
- case nameof(ButtonClose):
- Close();
- break;
- case nameof(ButtonMaximize):
- HandleWindowState();
- break;
- case nameof(ButtonMinimize):
- HandleWindowState(true);
- break;
- case nameof(TglButtonSettings):
- SettingsPanel.CacheMode = new BitmapCache { RenderAtScale = 1 };
- if (SettingsPanel.RenderTransform is TranslateTransform transform)
- {
- transform.BeginAnimation(TranslateTransform.XProperty, AnimationFactory.CreateIn(transform.X, TglButtonSettings.IsChecked.Value ? 0 : 400, 0.5, () => { SettingsPanel.CacheMode = null; }, useCubicEase: true));
- }
- break;
- case nameof(TglButtonTheme):
- GlobalOptions.SelfReboot();
- Visibility = Visibility.Collapsed;
- break;
- default:
- break;
+ transform.BeginAnimation(TranslateTransform.XProperty, AnimationFactory.CreateIn(transform.X, TglButtonSettings.IsChecked.Value ? 0 : 400, 0.5, () => { SettingsPanel.CacheMode = null; }, useCubicEase: true));
}
}
- #endregion
-
- #region Settings Panel
- private void BtnExport_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => GlobalOptions.SaveFileConfig();
-
- private void BtnImport_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => GlobalOptions.OpenFileConfig();
-
- private void BtnDelete_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => GlobalOptions.SelfRemoval();
- private void BtnContacts_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
+ private void TglButtonTheme_Click(object sender, RoutedEventArgs e)
{
- Process.Start(new ProcessStartInfo(((System.Windows.Controls.Image)sender).Uid switch
- {
- "git" => PathTargets.Links.GitHub,
- "tg" => PathTargets.Links.Telegram,
- _ => PathTargets.Links.Steam
- })
- { UseShellExecute = true });
+ Visibility = Visibility.Collapsed;
+ GlobalOptions.SelfReboot();
}
#endregion
@@ -202,8 +153,8 @@ namespace GTweak.Windows
if (targetOffset != viewTop)
{
- SetCurrentVerticalOffset(NavScroll, viewTop);
- NavScroll.BeginAnimation(CurrentVerticalOffsetProperty, AnimationFactory.CreateIn(viewTop, targetOffset, 0.5, useCubicEase: true));
+ ScrollViewerBehavior.SetVerticalOffset(NavScroll, viewTop);
+ NavScroll.BeginAnimation(ScrollViewerBehavior.VerticalOffsetProperty, AnimationFactory.CreateIn(viewTop, targetOffset, 0.5, useCubicEase: true));
}
}
}), DispatcherPriority.Loaded);
@@ -211,6 +162,17 @@ namespace GTweak.Windows
}
#endregion
+ private void BtnContacts_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
+ {
+ Process.Start(new ProcessStartInfo(((System.Windows.Controls.Image)sender).Uid switch
+ {
+ "git" => PathTargets.Links.GitHub,
+ "tg" => PathTargets.Links.Telegram,
+ _ => PathTargets.Links.Steam
+ })
+ { UseShellExecute = true });
+ }
+
private void BtnUpdate_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
UpdateBanner.BeginAnimation(OpacityProperty, AnimationFactory.CreateIn(1, 0, 0.3, () => { UpdateBanner.Visibility = Visibility.Collapsed; }));
diff --git a/.Source/GTweak/Windows/OverlayWindow.xaml.cs b/.Source/GTweak/Windows/OverlayWindow.xaml.cs
index e7fa2937..183561c5 100644
--- a/.Source/GTweak/Windows/OverlayWindow.xaml.cs
+++ b/.Source/GTweak/Windows/OverlayWindow.xaml.cs
@@ -10,10 +10,9 @@ namespace GTweak.Windows
/// <summary>
/// Darkened screen
/// </summary>
-
public partial class OverlayWindow
{
- private readonly KeyboardHookBlocker keyboardHook = new KeyboardHookBlocker();
+ private readonly KeyboardHookBlocker _keyboardHook = new KeyboardHookBlocker();
public OverlayWindow()
{
InitializeComponent();
@@ -26,10 +25,10 @@ namespace GTweak.Windows
Closing -= Window_Closing;
e!.Cancel = true;
- if (keyboardHook.ptrHook != IntPtr.Zero)
+ if (_keyboardHook.ptrHook != IntPtr.Zero)
{
- KeyboardHookBlocker.UnhookWindowsHookEx(keyboardHook.ptrHook);
- keyboardHook.ptrHook = IntPtr.Zero;
+ KeyboardHookBlocker.UnhookWindowsHookEx(_keyboardHook.ptrHook);
+ _keyboardHook.ptrHook = IntPtr.Zero;
}
BeginAnimation(OpacityProperty, AnimationFactory.CreateTo(0.15, () => { Close(); }));
@@ -39,8 +38,8 @@ namespace GTweak.Windows
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
- keyboardHook.objKeyboardProcess = new KeyboardHookBlocker.LowLevelKeyboardProc(keyboardHook.CaptureKey);
- keyboardHook.ptrHook = KeyboardHookBlocker.SetWindowsHookEx(13, keyboardHook.objKeyboardProcess, KeyboardHookBlocker.GetModuleHandle(objCurrentModule.ModuleName), 0);
+ _keyboardHook.objKeyboardProcess = new KeyboardHookBlocker.LowLevelKeyboardProc(_keyboardHook.CaptureKey);
+ _keyboardHook.ptrHook = KeyboardHookBlocker.SetWindowsHookEx(13, _keyboardHook.objKeyboardProcess, KeyboardHookBlocker.GetModuleHandle(objCurrentModule.ModuleName), 0);
BeginAnimation(OpacityProperty, AnimationFactory.CreateIn(0, 0.5, 0.3));
}
}