Portable Tool for an Ideal Windows Setup
C# activatorbloatware-removalcsharpdebloatdebloaterdisable-servicesmonitoring-systemportableprivacy-toolsregistry-tweakssystem-tuningsystem-utilitytelemetrytweakerwindows-10windows-11windows-debloatwindows-defenderwindows-optimizationwindows-tweaks
add view caching & fix excess key allocations cleanup class layou
Greedeks4461e5f048ed
diff --git a/.Source/GTweak/Assets/UserControls/ColorPicker.xaml.cs b/.Source/GTweak/Assets/UserControls/ColorPicker.xaml.csindex 8e41a45a..2c16ae10 100644--- a/.Source/GTweak/Assets/UserControls/ColorPicker.xaml.cs+++ b/.Source/GTweak/Assets/UserControls/ColorPicker.xaml.cs@@ -43,6 +43,23 @@ namespace GTweak.Assets.UserControlsset => SetValue(SelectedColorStringProperty, value);}+ private const double WheelSize = 150.0, WheelRadius = WheelSize / 2.0;++ private static readonly char[] ColorSeparators = { ' ', ',', ';' };+ private static readonly Regex HexRegex = new Regex("[^0-9a-fA-F]", RegexOptions.Compiled);+ private static readonly MethodInfo UpdatePositionMethod = typeof(Popup).GetMethod("UpdatePosition", BindingFlags.NonPublic | BindingFlags.Instance);++ private bool _isWheelBeingDragged, _isValueBeingDragged, _isUpdatingFromProperty, _isUpdatingHexValue;+ private double _selectedWheelX, _selectedWheelY, _currentHue, _currentSaturation, _currentValue = 1.0;+ private Color _colorOnOpen;+ private Window _parentWindow;+ private readonly List<ScrollViewer> _subscribedScrollViewers = new List<ScrollViewer>();++ public ColorPicker()+ {+ InitializeComponent();+ }+private static void OnSelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is ColorPicker picker && e.NewValue is Color newColor && !picker._isUpdatingFromProperty)@@ -69,23 +86,6 @@ namespace GTweak.Assets.UserControls}}- private const double WheelSize = 150.0, WheelRadius = WheelSize / 2.0;-- private static readonly char[] ColorSeparators = { ' ', ',', ';' };- private static readonly Regex HexRegex = new Regex("[^0-9a-fA-F]", RegexOptions.Compiled);- private static readonly MethodInfo UpdatePositionMethod = typeof(Popup).GetMethod("UpdatePosition", BindingFlags.NonPublic | BindingFlags.Instance);-- private bool _isWheelBeingDragged, _isValueBeingDragged, _isUpdatingFromProperty, _isUpdatingHexValue;- private double _selectedWheelX, _selectedWheelY, _currentHue, _currentSaturation, _currentValue = 1.0;- private Color _colorOnOpen;- private Window _parentWindow;- private readonly List<ScrollViewer> _subscribedScrollViewers = new List<ScrollViewer>();-- public ColorPicker()- {- InitializeComponent();- }-private void UpdateColorFromWheel(Point mousePosition){double deltaX = mousePosition.X - WheelRadius;@@ -165,7 +165,9 @@ namespace GTweak.Assets.UserControlsrawHexInput = rawHexInput.PadRight(6, lastCharacter);}- if (byte.TryParse(rawHexInput.Substring(0, 2), NumberStyles.HexNumber, null, out byte r) && byte.TryParse(rawHexInput.Substring(2, 2), NumberStyles.HexNumber, null, out byte g) && byte.TryParse(rawHexInput.Substring(4, 2), NumberStyles.HexNumber, null, out byte b))+ if (byte.TryParse(rawHexInput.Substring(0, 2), NumberStyles.HexNumber, null, out byte r) &&+ byte.TryParse(rawHexInput.Substring(2, 2), NumberStyles.HexNumber, null, out byte g) &&+ byte.TryParse(rawHexInput.Substring(4, 2), NumberStyles.HexNumber, null, out byte b)){Color parsedColor = Color.FromRgb(r, g, b);bool prevHexFlag = _isUpdatingHexValue;@@ -251,6 +253,7 @@ namespace GTweak.Assets.UserControls{sv.ScrollChanged -= ParentScrollViewer_ScrollChanged;}+_subscribedScrollViewers.Clear();}@@ -310,27 +313,27 @@ namespace GTweak.Assets.UserControlsreturn Color.FromRgb((byte)(calcR * 255.0), (byte)(calcG * 255.0), (byte)(calcB * 255.0));}- private void UpdatePositionProxy(object s, EventArgs e) => UpdatePopupPosition();- private void ParentWindow_MovedOrResized(object sender, EventArgs e) => UpdatePopupPosition();- private void TglArrow_Checked(object sender, RoutedEventArgs e) => ColorPopup.IsOpen = true;- private void TglArrow_Unchecked(object sender, RoutedEventArgs e) => ColorPopup.IsOpen = false;- private void TglArrow_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) => e.Handled = true;+ private void OnParentWindowChanged(object sender, EventArgs e) => UpdatePopupPosition();- private void BtnDefault_Click(object sender, RoutedEventArgs e)+ private void OnLoaded(object sender, RoutedEventArgs e){- Color def = DefaultColor;- SelectedColor = def;- UpdateHsvFromColor(def);- NotifyColorChanged(def);+ UpdateHsvFromColor(SelectedColor);+ _parentWindow = Window.GetWindow(this);+ if (_parentWindow != null)+ {+ _parentWindow.LocationChanged += OnParentWindowChanged;+ _parentWindow.SizeChanged += OnParentWindowChanged;+ }}- private void ColorPopup_PreviewKeyDown(object sender, KeyEventArgs e)+ private void OnUnloaded(object sender, RoutedEventArgs e){- if (e.Key == Key.Escape)+ if (_parentWindow != null){- ColorPopup.IsOpen = false;- e.Handled = true;+ _parentWindow.LocationChanged -= OnParentWindowChanged;+ _parentWindow.SizeChanged -= OnParentWindowChanged;}+ UnsubscribeFromScrollParents();}private void ColorPopup_Opened(object sender, EventArgs e)@@ -356,6 +359,15 @@ namespace GTweak.Assets.UserControls}}+ private void ColorPopup_PreviewKeyDown(object sender, KeyEventArgs e)+ {+ if (e.Key == Key.Escape)+ {+ ColorPopup.IsOpen = false;+ e.Handled = true;+ }+ }+private void ParentScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e){if ((e.VerticalChange != 0 || e.HorizontalChange != 0) && ColorPopup?.IsOpen == true)@@ -364,6 +376,18 @@ namespace GTweak.Assets.UserControls}}+ private void TglArrow_Checked(object sender, RoutedEventArgs e) => ColorPopup.IsOpen = true;+ private void TglArrow_Unchecked(object sender, RoutedEventArgs e) => ColorPopup.IsOpen = false;+ private void TglArrow_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) => e.Handled = true;++ private void BtnDefault_Click(object sender, RoutedEventArgs e)+ {+ Color def = DefaultColor;+ SelectedColor = def;+ UpdateHsvFromColor(def);+ NotifyColorChanged(def);+ }+private void ColorWheel_MouseDown(object sender, MouseButtonEventArgs e){_isWheelBeingDragged = true;@@ -406,17 +430,6 @@ namespace GTweak.Assets.UserControlsPART_ValueCanvas?.ReleaseMouseCapture();}- private void HexBox_KeyDown(object sender, KeyEventArgs e)- {- if (e?.Key == Key.Enter)- {- ApplyHexColor();- HexBox?.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));- }- }-- private void HexBox_LostFocus(object sender, RoutedEventArgs e) => ApplyHexColor();-private void HexBox_TextChanged(object sender, TextChangedEventArgs e){if (_isUpdatingHexValue)@@ -442,25 +455,15 @@ namespace GTweak.Assets.UserControls}}- private void OnLoaded(object sender, RoutedEventArgs e)+ private void HexBox_KeyDown(object sender, KeyEventArgs e){- UpdateHsvFromColor(SelectedColor);- _parentWindow = Window.GetWindow(this);- if (_parentWindow != null)+ if (e?.Key == Key.Enter){- _parentWindow.LocationChanged += UpdatePositionProxy;- _parentWindow.SizeChanged += UpdatePositionProxy;+ ApplyHexColor();+ HexBox?.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));}}- private void OnUnloaded(object sender, RoutedEventArgs e)- {- if (_parentWindow != null)- {- _parentWindow.LocationChanged -= UpdatePositionProxy;- _parentWindow.SizeChanged -= UpdatePositionProxy;- }- UnsubscribeFromScrollParents();- }+ private void HexBox_LostFocus(object sender, RoutedEventArgs e) => ApplyHexColor();}}\ No newline at end of filediff --git a/.Source/GTweak/Assets/UserControls/ToggleButton.xaml.cs b/.Source/GTweak/Assets/UserControls/ToggleButton.xaml.csindex b6969149..9e6444d5 100644--- a/.Source/GTweak/Assets/UserControls/ToggleButton.xaml.cs+++ b/.Source/GTweak/Assets/UserControls/ToggleButton.xaml.cs@@ -71,19 +71,10 @@ namespace GTweak.Assets.UserControls}}- private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)- {- if (d is ToggleButton tbtn)- {- tbtn.UpdateToggleState((bool)e.NewValue, !tbtn._isUserAction);- }- }-private const double _leftPositionX = -12.0, _rightPositionX = 12.0;private bool _isUserAction = false;- private readonly SolidColorBrush brushOffColor = new SolidColorBrush(), brushOnColor = new SolidColorBrush(),- borderOffColor = new SolidColorBrush(), borderOnColor = new SolidColorBrush();+ private readonly SolidColorBrush brushOffColor = new SolidColorBrush(), brushOnColor = new SolidColorBrush(), borderOffColor = new SolidColorBrush(), borderOnColor = new SolidColorBrush();private Color _dotColorOn = (Color)Application.Current.Resources["Color_ToggleDot_On"], _dotColorOff = (Color)Application.Current.Resources["Color_ToggleDot_Off"];@@ -107,6 +98,14 @@ namespace GTweak.Assets.UserControlsUpdateToggleState(State, true);}+ private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)+ {+ if (d is ToggleButton tbtn)+ {+ tbtn.UpdateToggleState((bool)e.NewValue, !tbtn._isUserAction);+ }+ }+private void ApplyResource(object value, DependencyProperty dp, DependencyProperty textProperty = null, FrameworkElement target = null){if (value != null && (dp != null || target != null))@@ -153,36 +152,6 @@ namespace GTweak.Assets.UserControls}}- private void ToggleButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)- {- if (Back != null && Dot != null && ToggleText != null)- {- if (e.NewValue is bool newBool && !newBool)- {- Back.Opacity = 0.7;- Dot.Opacity = 0.7;- ToggleText.Opacity = 0.7;- }- else- {- Back.Opacity = 1.0;- Dot.Opacity = 1.0;- ToggleText.Opacity = 1.0;- }- }- }-- private void Toggle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)- {- if (IsEnabled)- {- _isUserAction = true;- State = !State;- _isUserAction = false;- RaiseEvent(new RoutedEventArgs(ChangedStateEvent));- }- }-private void UpdateToggleState(bool State, bool skipAnimation = false){if (State)@@ -264,6 +233,25 @@ namespace GTweak.Assets.UserControls}}+ private void ToggleButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)+ {+ if (Back != null && Dot != null && ToggleText != null)+ {+ Back.Opacity = Dot.Opacity = ToggleText.Opacity = (double)(e.NewValue is bool newBool && !newBool ? 0.7 : 1.0);+ }+ }++ private void Toggle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)+ {+ if (IsEnabled)+ {+ _isUserAction = true;+ State = !State;+ _isUserAction = false;+ RaiseEvent(new RoutedEventArgs(ChangedStateEvent));+ }+ }+private void Toggle_MouseEnter(object sender, MouseEventArgs e){if (IsEnabled && DotScale != null)diff --git a/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs b/.Source/GTweak/Core/ViewModel/MainWinViewModel.csindex 11e2c50f..494f157c 100644--- a/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs+++ b/.Source/GTweak/Core/ViewModel/MainWinViewModel.cs@@ -1,4 +1,5 @@using System;+using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;@@ -15,10 +16,11 @@ namespace GTweak.Core.ViewModel{internal class MainWinViewModel : ViewModelBase{- public ObservableCollection<MainWinModel.LanguageItem> Languages { get; } = new ObservableCollection<MainWinModel.LanguageItem>();-private readonly MainWinModel _model = new MainWinModel();private readonly HardwareProvider _hardwareProvider = new HardwareProvider();+ private readonly Dictionary<Type, object> _viewCache = new Dictionary<Type, object>();++ public ObservableCollection<MainWinModel.LanguageItem> Languages { get; } = new ObservableCollection<MainWinModel.LanguageItem>();public object CurrentView{@@ -127,52 +129,42 @@ 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; }- public ICommand NavigateToPackagesCommand { get; set; }- public ICommand NavigateToServicesCommand { get; set; }- public ICommand NavigateToSystemCommand { get; set; }- public ICommand NavigateToDataSystemCommand { get; set; }- public ICommand NavigateToAddonsCommand { get; set; }- public ICommand NavigateToToolsetCommand { get; set; }-- private void NavigateToUtils(object obj) => CurrentView = new UtilsView();- private void NavigateToConfidentiality(object obj) => CurrentView = new ConfidentialityViewModel();- private void NavigateToInterface(object obj) => CurrentView = new InterfaceViewModel();- private void NavigateToPackages(object obj) => CurrentView = new PackagesViewModel();- private void NavigateToServices(object obj) => CurrentView = new ServicesViewModel();- private void NavigateToSystem(object obj) => CurrentView = new SystemViewModel();- private void NavigateToDataSystem(object obj) => CurrentView = new DataSystemViewModel();- private void NavigateToAddons(object obj) => CurrentView = new AddonsViewModel();- private void NavigateToToolset(object obj) => CurrentView = new ToolsetViewModel();+ public ICommand ExportConfigCommand { get; }+ public ICommand ImportConfigCommand { get; }+ public ICommand SelfRemovalCommand { get; }++ public ICommand NavigateToUtilsCommand { get; }+ public ICommand NavigateToConfidentialityCommand { get; }+ public ICommand NavigateToInterfaceCommand { get; }+ public ICommand NavigateToPackagesCommand { get; }+ public ICommand NavigateToServicesCommand { get; }+ public ICommand NavigateToSystemCommand { get; }+ public ICommand NavigateToDataSystemCommand { get; }+ public ICommand NavigateToAddonsCommand { get; }+ public ICommand NavigateToToolsetCommand { get; }public MainWinViewModel(){App.TweaksImported += delegate{- CurrentView = new UtilsView();+ CurrentView = GetView<UtilsView>();};- CurrentView = new UtilsView();+ CurrentView = GetView<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);- NavigateToPackagesCommand = new RelayCommand(NavigateToPackages);- NavigateToServicesCommand = new RelayCommand(NavigateToServices);- NavigateToSystemCommand = new RelayCommand(NavigateToSystem);- NavigateToDataSystemCommand = new RelayCommand(NavigateToDataSystem);- NavigateToAddonsCommand = new RelayCommand(NavigateToAddons);- NavigateToToolsetCommand = new RelayCommand(NavigateToToolset);+ NavigateToUtilsCommand = new RelayCommand(_ => CurrentView = GetView<UtilsView>());+ NavigateToConfidentialityCommand = new RelayCommand(_ => CurrentView = GetView<ConfidentialityViewModel>());+ NavigateToInterfaceCommand = new RelayCommand(_ => CurrentView = GetView<InterfaceViewModel>());+ NavigateToPackagesCommand = new RelayCommand(_ => CurrentView = GetView<PackagesViewModel>());+ NavigateToServicesCommand = new RelayCommand(_ => CurrentView = GetView<ServicesViewModel>());+ NavigateToSystemCommand = new RelayCommand(_ => CurrentView = GetView<SystemViewModel>());+ NavigateToDataSystemCommand = new RelayCommand(_ => CurrentView = GetView<DataSystemViewModel>());+ NavigateToAddonsCommand = new RelayCommand(_ => CurrentView = GetView<AddonsViewModel>());+ NavigateToToolsetCommand = new RelayCommand(_ => CurrentView = GetView<ToolsetViewModel>());if (!DesignerProperties.GetIsInDesignMode(new DependencyObject())){@@ -180,6 +172,8 @@ namespace GTweak.Core.ViewModel}}+ private T GetView<T>() where T : new() => (T)(_viewCache.TryGetValue(typeof(T), out var v) ? v : _viewCache[typeof(T)] = new T());+private void FillLanguages(){Languages.Clear();diff --git a/.Source/GTweak/Modules/Managers/ControlWriterManager.cs b/.Source/GTweak/Modules/Managers/ControlWriterManager.csindex bb1e7f91..f4367fe3 100644--- a/.Source/GTweak/Modules/Managers/ControlWriterManager.cs+++ b/.Source/GTweak/Modules/Managers/ControlWriterManager.cs@@ -1,4 +1,3 @@-using System.Collections.Concurrent;using System.Collections.Generic;namespace GTweak.Modules.Managers@@ -21,27 +20,28 @@ namespace GTweak.Modules.ManagersSlider = new GenericCollection<object>(_controlStates, "Slider");ColorPicker = new GenericCollection<object>(_controlStates, "ColorPicker");}+internal class GenericCollection<T>{private readonly string _prefix;private readonly Dictionary<string, object> _controlStates;- private readonly ConcurrentDictionary<int, string> _keyCache = new ConcurrentDictionary<int, string>();+ private readonly string[] _keyCache;- internal GenericCollection(Dictionary<string, object> controlStates, string prefix)+ internal GenericCollection(Dictionary<string, object> controlStates, string prefix, int capacity = 64){_controlStates = controlStates;_prefix = prefix;+ _keyCache = new string[capacity];}internal T this[int index]{set{- string key = _keyCache.GetOrAdd(index, i => $"{_prefix}{i}");-- if (_controlStates != null)+ if ((uint)index < (uint)_keyCache.Length){- _controlStates[key] = value;+ _keyCache[index] ??= $"{_prefix}{index}";+ _controlStates[_keyCache[index]] = value;}}}