Skip to content

Commit

Permalink
Use pattern matching with is operators
Browse files Browse the repository at this point in the history
Use pattern matching with is operators
  • Loading branch information
Gaoyifei1011 committed Sep 1, 2024
1 parent aee0276 commit a45ad8c
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 656 deletions.
88 changes: 37 additions & 51 deletions WindowsTools/Helpers/Root/RegistryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,40 @@ public static T ReadRegistryKey<T>(string rootKey, string key)
T value = default;
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(rootKey, false);

if (registryKey is not null)
if (Registry.CurrentUser.OpenSubKey(rootKey, false) is RegistryKey registryKey && registryKey.GetValue(key) is object getValue)
{
object getValue = registryKey.GetValue(key);

if (getValue is not null)
// 读取布尔值
if (typeof(T) == typeof(bool) || typeof(T) == typeof(bool?))
{
// 读取布尔值
if (typeof(T) == typeof(bool) || typeof(T) == typeof(bool?))
{
value = (T)(object)Convert.ToBoolean(getValue);
}
else if (typeof(T) == typeof(int) || typeof(T) == typeof(int?))
{
value = (T)(object)Convert.ToInt32(getValue);
}
else if (typeof(T) == typeof(uint) || typeof(T) == typeof(uint?))
{
value = (T)(object)Convert.ToUInt32(getValue);
}
else if (typeof(T) == typeof(long) || typeof(T) == typeof(long?))
{
value = (T)(object)Convert.ToInt64(getValue);
}
else if (typeof(T) == typeof(ulong) || typeof(T) == typeof(ulong?))
{
value = (T)(object)Convert.ToUInt64(getValue);
}
else if (typeof(T) == typeof(string))
{
value = (T)(object)Convert.ToString(getValue);
}
else if (typeof(T) == typeof(byte[]) || typeof(T) == typeof(string[]))
{
value = (T)getValue;
}
else
{
value = (T)getValue;
}
value = (T)(object)Convert.ToBoolean(getValue);
}
else if (typeof(T) == typeof(int) || typeof(T) == typeof(int?))
{
value = (T)(object)Convert.ToInt32(getValue);
}
else if (typeof(T) == typeof(uint) || typeof(T) == typeof(uint?))
{
value = (T)(object)Convert.ToUInt32(getValue);
}
else if (typeof(T) == typeof(long) || typeof(T) == typeof(long?))
{
value = (T)(object)Convert.ToInt64(getValue);
}
else if (typeof(T) == typeof(ulong) || typeof(T) == typeof(ulong?))
{
value = (T)(object)Convert.ToUInt64(getValue);
}
else if (typeof(T) == typeof(string))
{
value = (T)(object)Convert.ToString(getValue);
}
else if (typeof(T) == typeof(byte[]) || typeof(T) == typeof(string[]))
{
value = (T)getValue;
}
else
{
value = (T)getValue;
}

registryKey.Close();
Expand All @@ -84,9 +77,7 @@ public static void SaveRegistryKey<T>(string rootKey, string key, T value, bool
{
try
{
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(rootKey, true);

if (registryKey is not null)
if (Registry.CurrentUser.CreateSubKey(rootKey, true) is RegistryKey registryKey)
{
// 存储 32 位整数类型或者布尔值
if (typeof(T) == typeof(bool) || typeof(T) == typeof(int) || typeof(T) == typeof(uint))
Expand Down Expand Up @@ -149,15 +140,12 @@ public static void RemoveRegistryKey(string rootKey)
{
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(rootKey, true);

if (registryKey is not null)
if (Registry.CurrentUser.OpenSubKey(rootKey, true) is RegistryKey registryKey)
{
Registry.CurrentUser.DeleteSubKeyTree(rootKey, false);
registryKey.Close();
registryKey.Dispose();
}

registryKey.Close();
registryKey.Dispose();
}
catch (Exception e)
{
Expand All @@ -174,9 +162,7 @@ public static RegistryEnumKeyItem EnumSubKey(string rootKey)

try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(rootKey);

if (registryKey is not null)
if (Registry.CurrentUser.OpenSubKey(rootKey) is RegistryKey registryKey)
{
// 添加当前项信息
registryEnumKeyItem.RootKey = rootKey;
Expand Down
24 changes: 6 additions & 18 deletions WindowsTools/UI/Backdrop/DesktopAcrylicBackdrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,7 @@ public static bool IsSupported
{
get
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Wallpaper");
if (key is not null)
{
object value = key.GetValue("WallpaperSurfaceProvidedToDwm");
if (value is not null && Convert.ToInt32(value) is 1)
{
return true;
}
}

return false;
return Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Wallpaper") is RegistryKey key && key.GetValue("WallpaperSurfaceProvidedToDwm") is object value && Convert.ToInt32(value) is 1;
}
}

Expand Down Expand Up @@ -488,10 +478,10 @@ private void Dispose(bool disposing)
hPowerNotify = IntPtr.Zero;
}

if (DesktopWindowTarget.Root as SpriteVisual is not null && (DesktopWindowTarget.Root as SpriteVisual).Brush is not null)
if (DesktopWindowTarget.Root is SpriteVisual spriteVisual && spriteVisual.Brush is not null)
{
(DesktopWindowTarget.Root as SpriteVisual).Brush.Dispose();
(DesktopWindowTarget.Root as SpriteVisual).Brush = null;
spriteVisual.Brush.Dispose();
spriteVisual.Brush = null;
}
}
}
Expand All @@ -515,8 +505,7 @@ private void OnSizeChanged(object sender, EventArgs args)
{
synchronizationContext.Post(_ =>
{
SpriteVisual spriteVisual = DesktopWindowTarget.Root as SpriteVisual;
if (spriteVisual is not null)
if (DesktopWindowTarget.Root is SpriteVisual spriteVisual)
{
spriteVisual.Size = new Vector2(formRoot.Width, formRoot.Height);
}
Expand All @@ -530,8 +519,7 @@ private void OnDpiChanged(object sender, DpiChangedEventArgs args)
{
synchronizationContext.Post(_ =>
{
SpriteVisual spriteVisual = DesktopWindowTarget.Root as SpriteVisual;
if (spriteVisual is not null)
if (DesktopWindowTarget.Root is SpriteVisual spriteVisual)
{
spriteVisual.Size = new Vector2(formRoot.Width, formRoot.Height);
}
Expand Down
18 changes: 8 additions & 10 deletions WindowsTools/UI/Backdrop/MicaBackdrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ private void Dispose(bool disposing)

Comctl32Library.RemoveWindowSubclass(formRoot.Handle, formSubClassProc, 0);

if (DesktopWindowTarget.Root as SpriteVisual is not null && (DesktopWindowTarget.Root as SpriteVisual).Brush is not null)
if (DesktopWindowTarget.Root is SpriteVisual spriteVisual && spriteVisual.Brush is not null)
{
(DesktopWindowTarget.Root as SpriteVisual).Brush.Dispose();
(DesktopWindowTarget.Root as SpriteVisual).Brush = null;
spriteVisual.Brush.Dispose();
spriteVisual.Brush = null;
}
}
}
Expand All @@ -434,8 +434,7 @@ private void OnSizeChanged(object sender, EventArgs args)
{
synchronizationContext.Post(_ =>
{
SpriteVisual spriteVisual = DesktopWindowTarget.Root as SpriteVisual;
if (spriteVisual is not null)
if (DesktopWindowTarget.Root is SpriteVisual spriteVisual)
{
spriteVisual.Size = new Vector2(formRoot.Width, formRoot.Height);
}
Expand All @@ -449,8 +448,7 @@ private void OnDpiChanged(object sender, DpiChangedEventArgs args)
{
synchronizationContext.Post(_ =>
{
SpriteVisual spriteVisual = DesktopWindowTarget.Root as SpriteVisual;
if (spriteVisual is not null)
if (DesktopWindowTarget.Root is SpriteVisual spriteVisual)
{
spriteVisual.Size = new Vector2(formRoot.Width, formRoot.Height);
}
Expand Down Expand Up @@ -566,15 +564,15 @@ private void UpdateBrush()
// 直接设置新笔刷
oldBrush?.Dispose();
(DesktopWindowTarget.Root as SpriteVisual).Brush = newBrush;
(DesktopWindowTarget.Root as SpriteVisual).Size = new Vector2(formRoot.Width, formRoot.Height);
(DesktopWindowTarget.Root as SpriteVisual).Size = new(formRoot.Width, formRoot.Height);
}
else
{
// 回退色切换时的动画颜色
CompositionBrush crossFadeBrush = CreateCrossFadeEffectBrush(compositor, oldBrush, newBrush);
ScalarKeyFrameAnimation animation = CreateCrossFadeAnimation(compositor);
(DesktopWindowTarget.Root as SpriteVisual).Brush = crossFadeBrush;
(DesktopWindowTarget.Root as SpriteVisual).Size = new Vector2(formRoot.Width, formRoot.Height);
(DesktopWindowTarget.Root as SpriteVisual).Size = new(formRoot.Width, formRoot.Height);

CompositionScopedBatch crossFadeAnimationBatch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
crossFadeBrush.StartAnimation("CrossFade.CrossFade", animation);
Expand All @@ -585,7 +583,7 @@ private void UpdateBrush()
crossFadeBrush.Dispose();
oldBrush.Dispose();
(DesktopWindowTarget.Root as SpriteVisual).Brush = newBrush;
(DesktopWindowTarget.Root as SpriteVisual).Size = new Vector2(formRoot.Width, formRoot.Height);
(DesktopWindowTarget.Root as SpriteVisual).Size = new(formRoot.Width, formRoot.Height);
};
}
}
Expand Down
12 changes: 1 addition & 11 deletions WindowsTools/UI/Backdrop/SystemBackdrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,7 @@ public abstract class SystemBackdrop(DesktopWindowTarget target) : IDisposable
/// </summary>
protected bool IsAdvancedEffectsEnabled()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize");
if (key is not null)
{
object value = key.GetValue("EnableTransparency");
if (value is not null && Convert.ToInt32(value) is 1)
{
return true;
}
}

return false;
return Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize") is RegistryKey key && key.GetValue("EnableTransparency") is object value && Convert.ToInt32(value) is 1;
}
}
}
3 changes: 1 addition & 2 deletions WindowsTools/UI/Dialogs/OperationFailedDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public OperationFailedDialog(ObservableCollection<OperationFailedModel> operatio
/// </summary>
private async void OnCopyExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs args)
{
OperationFailedModel operationFailedItem = args.Parameter as OperationFailedModel;
if (operationFailedItem is not null)
if (args.Parameter is OperationFailedModel operationFailedItem)
{
StringBuilder builder = new();
builder.Append(Dialog.FileNameCopy);
Expand Down
19 changes: 6 additions & 13 deletions WindowsTools/Views/Pages/AboutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ private async void OnPinToStartScreenClicked(object sender, RoutedEventArgs args
{
IReadOnlyList<AppListEntry> appEntries = await Package.Current.GetAppListEntriesAsync();

AppListEntry defaultEntry = appEntries[0];

if (defaultEntry is not null)
if (appEntries[0] is AppListEntry defaultEntry)
{
StartScreenManager startScreenManager = StartScreenManager.GetDefault();

Expand Down Expand Up @@ -284,19 +282,14 @@ private void OnCheckUpdateClicked(object sender, RoutedEventArgs args)
{
GroupCollection tagGroups = tagCollection[0].Groups;

if (tagGroups.Count > 0)
if (tagGroups.Count > 0 && new Version(tagGroups[1].Value) is Version tagVersion)
{
Version tagVersion = new(tagGroups[1].Value);
bool isNewest = InfoHelper.AppVersion >= tagVersion;

if (tagVersion is not null)
synchronizationContext.Post(async (_) =>
{
bool isNewest = InfoHelper.AppVersion >= tagVersion;

synchronizationContext.Post(async (_) =>
{
await TeachingTipHelper.ShowAsync(new OperationResultTip(OperationKind.CheckUpdate, isNewest));
}, null);
}
await TeachingTipHelper.ShowAsync(new OperationResultTip(OperationKind.CheckUpdate, isNewest));
}, null);
}
}
}
Expand Down
21 changes: 7 additions & 14 deletions WindowsTools/Views/Pages/AllToolsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,15 @@ public AllToolsPage()
/// </summary>
private void OnItemClicked(object sender, ItemClickEventArgs args)
{
ControlItemModel controlItem = args.ClickedItem as ControlItemModel;

if (controlItem is not null)
if (args.ClickedItem is ControlItemModel controlItem && (MainWindow.Current.Content as MainPage).NavigationItemList.Find(item => item.NavigationTag.Equals(controlItem.Tag, StringComparison.OrdinalIgnoreCase)) is NavigationModel navigationItem)
{
NavigationModel navigationItem = (MainWindow.Current.Content as MainPage).NavigationItemList.Find(item => item.NavigationTag.Equals(controlItem.Tag, StringComparison.OrdinalIgnoreCase));

if (navigationItem is not null)
if (navigationItem.NavigationPage == typeof(ShellMenuPage))
{
(MainWindow.Current.Content as MainPage).NavigateTo(navigationItem.NavigationPage, "ShellMenu");
}
else
{
if (navigationItem.NavigationPage == typeof(ShellMenuPage))
{
(MainWindow.Current.Content as MainPage).NavigateTo(navigationItem.NavigationPage, "ShellMenu");
}
else
{
(MainWindow.Current.Content as MainPage).NavigateTo(navigationItem.NavigationPage);
}
(MainWindow.Current.Content as MainPage).NavigateTo(navigationItem.NavigationPage);
}
}
}
Expand Down
Loading

0 comments on commit a45ad8c

Please sign in to comment.