From ef95a78fe52ca4dd2d5e80ef790d168a14a865f4 Mon Sep 17 00:00:00 2001 From: changbw001 Date: Fri, 30 Dec 2016 18:08:13 +0800 Subject: [PATCH] Add boundary check for RoundedWindow. --- MainWindow.xaml | 3 ++ MainWindow.xaml.cs | 22 ++++++++----- RoundedWindow.cs | 62 ++++++++++++++++++++++++++++++------ Themes/Generic.xaml | 7 +++- Win_Search.xaml | 6 ++-- bin/Release/DesktopNote.exe | Bin 1203200 -> 1204224 bytes 6 files changed, 78 insertions(+), 22 deletions(-) diff --git a/MainWindow.xaml b/MainWindow.xaml index 9789e32..413205c 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -6,6 +6,9 @@ Title="Desktop Note" Height="350" Width="300" ShowInTaskbar="False" AllowsTransparency="True" WindowStyle="None" Background="{x:Null}" ResizeMode="CanResizeWithGrip" UseLayoutRounding="True" MouseEnter="Win_Main_MouseEnter" MouseLeave="Win_Main_MouseLeave" Loaded="Win_Main_Loaded"> + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index d3c89a8..10155e9 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -27,7 +27,7 @@ public MainWindow() #region Docking public DockStatus lastdockstatus; - private Rect currScrnRect; + public Rect currScrnRect; public enum DockStatus { None, Docking, Left, Right, Top, Bottom } @@ -43,8 +43,17 @@ public DockStatus DockedTo private static void OnDockedToChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ds = (DockStatus)e.NewValue; - if (ds == DockStatus.None) ((Window)d).ResizeMode = ResizeMode.CanResizeWithGrip; - else ((Window)d).ResizeMode = ResizeMode.NoResize; + var win = (Window)d; + if (ds == DockStatus.None) + { + win.ResizeMode = ResizeMode.CanResizeWithGrip; + win.Topmost = false; + } + else + { + win.ResizeMode = ResizeMode.NoResize; + win.Topmost = true; + } } internal void DockToSide(bool changpos = false) @@ -88,7 +97,6 @@ internal void DockToSide(bool changpos = false) else { lastdockstatus = DockStatus.None; - Topmost = false; return; } lastdockstatus = dockto; @@ -119,7 +127,6 @@ internal void DockToSide(bool changpos = false) } } - Topmost = true; var anim_move = new DoubleAnimation(toval, new Duration(new TimeSpan(0, 0, 0, 0, 500)), FillBehavior.Stop); anim_move.EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }; var anim_fade = new DoubleAnimation(0.4, new Duration(new TimeSpan(0, 0, 0, 0, 300))); @@ -168,7 +175,6 @@ private void UnDock() else return; - Topmost = true; var anim_move = new DoubleAnimation(toval, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.Stop); anim_move.EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }; var anim_fade = new DoubleAnimationUsingKeyFrames(); @@ -193,7 +199,7 @@ private void Win_Main_MouseLeave(object sender, MouseEventArgs e) if (Properties.Settings.Default.AutoDock && Application.Current.Windows.Count <= App.MaxWindowCount && //to prevent docking when search window is visible. FormatBox is the 2nd window. !RTB_Main.IsKeyboardFocusWithin && - !fbopen) + App.fb.Opacity != 1) DockToSide(); } #endregion @@ -237,7 +243,7 @@ private void RTB_Main_LostKeyboardFocus(object sender, KeyboardFocusChangedEvent { if (Properties.Settings.Default.AutoDock && Application.Current.Windows.Count <= App.MaxWindowCount && - !fbopen) + App.fb.Opacity != 1) DockToSide(); } diff --git a/RoundedWindow.cs b/RoundedWindow.cs index 7e3d6c4..d3187e2 100644 --- a/RoundedWindow.cs +++ b/RoundedWindow.cs @@ -95,6 +95,18 @@ public CloseBehaviors CloseBehavior public static readonly new DependencyProperty FocusableProperty = DependencyProperty.Register("Focusable", typeof(bool), typeof(RoundedWindow), new PropertyMetadata(true)); + /// + /// This maps to the RenderTransformOrigin property of BackgroundGrid. + /// + public Point RenderTransformOrigin_BG + { + get { return (Point)GetValue(RenderTransformOrigin_BGProperty); } + set { SetValue(RenderTransformOrigin_BGProperty, value); } + } + public static readonly DependencyProperty RenderTransformOrigin_BGProperty = + DependencyProperty.Register("RenderTransformOrigin_BG", typeof(Point), typeof(RoundedWindow), new PropertyMetadata(new Point(0, 0))); + + #endregion @@ -138,13 +150,16 @@ public RoundedWindow() base.Background = null; } - private Grid Grid_Main; + /// + /// This is a grid on which the scaling animations are applied. + /// + internal Grid BackgroundGrid { get; set; } public override void OnApplyTemplate() { base.OnApplyTemplate(); - Grid_Main = (Grid)GetTemplateChild("Grid_Main"); + BackgroundGrid = (Grid)GetTemplateChild("Grid_Main"); Button minimizeButton = GetTemplateChild("minimizeButton") as Button; if (minimizeButton != null) minimizeButton.Click += MinimizeClick; @@ -179,7 +194,7 @@ protected void RestoreClick(object sender, RoutedEventArgs e) protected void CloseClick(object sender, RoutedEventArgs e) { //make sure the scale animation ends at the top right corner when close is clicked. - Grid_Main.RenderTransformOrigin = new Point(1, 0); + RenderTransformOrigin_BG = new Point(1, 0); Close(); } #endregion @@ -213,7 +228,7 @@ protected override void OnClosing(CancelEventArgs e) { base.Show(); //ensure the contents are visible when not using fading animation. - Grid_Main.RenderTransform = new ScaleTransform(1, 1); + BackgroundGrid.RenderTransform = new ScaleTransform(1, 1); Opacity = 1; } @@ -224,20 +239,47 @@ public void FadeIn(double left = double.NaN, double top = double.NaN) { if (!IsLoaded) base.Show();//how to call something similar to initializecomponent? - //make sure the scale animation starts from left top corner. - Grid_Main.RenderTransformOrigin = new Point(0, 0); - //compute mouse position or set to existing values - Point newpos; - Point realpos; + Point newpos, realpos; + double currscrnW = App.mainwin.currScrnRect.Right; + double currscrnH = App.mainwin.currScrnRect.Bottom; if (left.Equals(double.NaN) || top.Equals(double.NaN))//nan==nan returns false. { + //get the physical pixel-based position. newpos = PointToScreen(Mouse.GetPosition(this)); + //convert to the actual position considering the DPI settings etc. realpos = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformFromDevice.Transform(newpos); + + //make sure the window is displayed inside the screens. + double originX = 0d, originY = 0d; + if (currscrnW - realpos.X > ActualWidth) + originX = 0d; + else + { + originX = 1d; + realpos.X -= ActualWidth; + } + if (currscrnH - realpos.Y > ActualHeight) + originY = 0d; + else + { + originY = 1d; + realpos.Y -= ActualHeight; + } + RenderTransformOrigin_BG = new Point(originX, originY); } else + { + //make sure the window is displayed inside the screens. + if (left < 0d) left = 0d; + if (top < 0d) top = 0d; + if (left + ActualWidth > currscrnW) + left = currscrnW - ActualWidth; + if (top + ActualHeight > currscrnH) + top = currscrnH - ActualHeight; realpos = new Point(left, top); - + } + if (Opacity == 0) { Left = realpos.X; diff --git a/Themes/Generic.xaml b/Themes/Generic.xaml index d12460a..ebda2e4 100644 --- a/Themes/Generic.xaml +++ b/Themes/Generic.xaml @@ -40,7 +40,7 @@ - + @@ -202,5 +202,10 @@ + + + + + \ No newline at end of file diff --git a/Win_Search.xaml b/Win_Search.xaml index e8c3026..c5194a9 100644 --- a/Win_Search.xaml +++ b/Win_Search.xaml @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:DesktopNote" - Height="110" Width="250" FontSize="10" ResizeMode="NoResize" SizeToContent="Height" + Height="110" Width="250" FontSize="10" ResizeMode="NoResize" SizeToContent="Height" RenderTransformOrigin_BG="0.5,0.5" Loaded="Window_Loaded" ShowInTaskbar="False" Topmost="True" ButtonCloseVisible="False"> @@ -13,8 +13,8 @@ - - + +