From fcc0f9ca58df5a6e9ecb0b2b1f127b9bc1b4d298 Mon Sep 17 00:00:00 2001 From: mntone Date: Sun, 9 Aug 2020 22:50:09 +0900 Subject: [PATCH] Change the preview taskbar to the actual position. --- source/SylphyHorn/Interop/NativeMethods.cs | 5 +- source/SylphyHorn/Services/MonitorService.cs | 16 + .../UI/Bindings/SettingsWindowViewModel.cs | 33 ++ source/SylphyHorn/UI/SettingsWindow.xaml | 306 ++++++++++++++---- source/SylphyHorn/UI/SettingsWindow.xaml.cs | 31 ++ 5 files changed, 334 insertions(+), 57 deletions(-) diff --git a/source/SylphyHorn/Interop/NativeMethods.cs b/source/SylphyHorn/Interop/NativeMethods.cs index 83e5f7c..02e04b6 100644 --- a/source/SylphyHorn/Interop/NativeMethods.cs +++ b/source/SylphyHorn/Interop/NativeMethods.cs @@ -33,9 +33,12 @@ public static class NativeMethods [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi); - [DllImport("user32.dll", SetLastError = true)] + [DllImport("user32.dll", ExactSpelling = true)] public static extern IntPtr MonitorFromPoint(POINT pt, MonitorDefaultTo dwFlags); + [DllImport("user32.dll", ExactSpelling = true)] + public static extern IntPtr MonitorFromWindow(IntPtr hWnd, MonitorDefaultTo dwFlags); + [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)] public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, LayeredWindowAttributes dwFlags); diff --git a/source/SylphyHorn/Services/MonitorService.cs b/source/SylphyHorn/Services/MonitorService.cs index 28a4e81..da2c527 100644 --- a/source/SylphyHorn/Services/MonitorService.cs +++ b/source/SylphyHorn/Services/MonitorService.cs @@ -31,10 +31,20 @@ internal static class MonitorService public static Monitor[] GetAreas() => GetMonitorsInternal(); + public static Monitor GetPrimaryMonitor() => GetPrimaryMonitorInternal(); + + public static Monitor GetPrimaryArea() => GetPrimaryMonitorInternal(); + public static Monitor GetCurrentMonitor() => GetCurrentMonitorInternal(true); public static Monitor GetCurrentArea() => GetCurrentMonitorInternal(); + public static IntPtr GetPrimaryHMonitor() + { + var hPrimaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, MonitorDefaultTo.MONITOR_DEFAULTTOPRIMARY); + return hPrimaryMonitor; + } + public static IntPtr GetCurrentHMonitor() { POINT pt; @@ -65,6 +75,12 @@ private static Monitor[] GetMonitorsInternal(bool additionalRetrive = false) return list.ToArray(); } + private static Monitor GetPrimaryMonitorInternal(bool additionalRetrive = false) + { + var hPrimaryMonitor = GetPrimaryHMonitor(); + return GetMonitorInternal(hPrimaryMonitor, additionalRetrive); + } + private static Monitor GetCurrentMonitorInternal(bool additionalRetrive = false) { var hWorkingMonitor = GetCurrentHMonitor(); diff --git a/source/SylphyHorn/UI/Bindings/SettingsWindowViewModel.cs b/source/SylphyHorn/UI/Bindings/SettingsWindowViewModel.cs index 79fc80a..8047e81 100644 --- a/source/SylphyHorn/UI/Bindings/SettingsWindowViewModel.cs +++ b/source/SylphyHorn/UI/Bindings/SettingsWindowViewModel.cs @@ -201,6 +201,26 @@ public WallpaperPosition PreviewBackgroundPosition #endregion + #region PreviewTaskbarPosition notification property + + private string _PreviewTaskbarPosition = "Bottom"; + + public string PreviewTaskbarPosition + { + get => this._PreviewTaskbarPosition; + set + { + if (this._PreviewTaskbarPosition != value) + { + this._PreviewTaskbarPosition = value; + + this.RaisePropertyChanged(); + } + } + } + + #endregion + public Brush NotificationBackground => new SolidColorBrush(WindowsTheme.ColorPrevalence.Current ? ImmersiveColor.GetColorByTypeName(ImmersiveColorNames.SystemAccentDark1) : ImmersiveColor.GetColorByTypeName(ImmersiveColorNames.DarkChromeMedium)) @@ -267,6 +287,7 @@ public SettingsWindowViewModel(HookService hookService) this.PreviewBackgroundBrush = new SolidColorBrush(colAndWall.BackgroundColor); this.PreviewBackgroundPath = colAndWall.Path; this.PreviewBackgroundPosition = colAndWall.Position; + this.PreviewTaskbarPosition = GetTaskbarPosition(); this.Logs = ViewModelHelper.CreateReadOnlyDispatcherCollection( LoggingService.Instance.Logs, @@ -316,5 +337,17 @@ public void OpenBackgroundPathDialog() Settings.General.DesktopBackgroundFolderPath.Value = message.Response; } } + + private static string GetTaskbarPosition() + { + var workspace = MonitorService.GetPrimaryArea(); + var monitorArea = workspace.MonitorArea; + var workArea = workspace.WorkArea; + if (monitorArea.Top < workArea.Top) return "Top"; + if (monitorArea.Left < workArea.Left) return "Left"; + if (monitorArea.Height > workArea.Height) return "Bottom"; + if (monitorArea.Width > workArea.Width) return "Right"; + return "AutoHide"; + } } } diff --git a/source/SylphyHorn/UI/SettingsWindow.xaml b/source/SylphyHorn/UI/SettingsWindow.xaml index b946c2d..ceae6ae 100644 --- a/source/SylphyHorn/UI/SettingsWindow.xaml +++ b/source/SylphyHorn/UI/SettingsWindow.xaml @@ -354,7 +354,10 @@ - + - + - - + + - + + Visibility="{Binding HasWallpaper, Converter={StaticResource BooleanToVisibilityConverter}}"> - - + + + Style="{StaticResource PreviewTaskbarStyle}" /> diff --git a/source/SylphyHorn/UI/SettingsWindow.xaml.cs b/source/SylphyHorn/UI/SettingsWindow.xaml.cs index d26d084..6c71a78 100644 --- a/source/SylphyHorn/UI/SettingsWindow.xaml.cs +++ b/source/SylphyHorn/UI/SettingsWindow.xaml.cs @@ -1,4 +1,7 @@ using System; +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; using WindowsDesktop; namespace SylphyHorn.UI @@ -12,10 +15,38 @@ public SettingsWindow() this.InitializeComponent(); } + protected override void OnSourceInitialized(EventArgs e) + { + base.OnSourceInitialized(e); + + this.UpdatePreviewBlurImageMargin(); + DependencyPropertyDescriptor.FromProperty(LeftProperty, typeof(Grid)) + .AddValueChanged(this.PreviewRoot, this.OnPreviewRootCanvasPositionChanged); + DependencyPropertyDescriptor.FromProperty(TopProperty, typeof(Grid)) + .AddValueChanged(this.PreviewRoot, this.OnPreviewRootCanvasPositionChanged); + } + protected override void OnContentRendered(EventArgs e) { base.OnContentRendered(e); this.Pin(); } + + private void OnPreviewRootCanvasPositionChanged(object sender, EventArgs e) + => this.UpdatePreviewBlurImageMargin(); + + private void UpdatePreviewBlurImageMargin() + { + var previewLeft = Canvas.GetLeft(this.PreviewRoot); + if (double.IsNaN(previewLeft)) previewLeft = 0.0; + var previewTop = Canvas.GetTop(this.PreviewRoot); + if (double.IsNaN(previewTop)) previewTop = 0.0; + + this.PreviewBlurImage.Margin = new Thickness( + -previewLeft, + -previewTop, + -(this.CanvasRoot.Width - previewLeft - this.PreviewRoot.Width), + -(this.CanvasRoot.Height - previewTop - this.PreviewRoot.Height)); + } } }