Skip to content

Commit

Permalink
Add boundary check for RoundedWindow.
Browse files Browse the repository at this point in the history
  • Loading branch information
changbw001 committed Dec 30, 2016
1 parent 88197d9 commit ef95a78
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
3 changes: 3 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Window.CacheMode>
<BitmapCache/>
</Window.CacheMode>
<Grid>
<Rectangle x:Name="Rec_BG" Fill="#FFFFF7C5" Margin="10" RadiusX="12" RadiusY="12"
MouseLeftButtonDown="Rec_BG_MouseLeftButtonDown" MouseLeftButtonUp="Rec_BG_MouseLeftButtonUp" MouseMove="Rec_BG_MouseMove" Cursor="SizeAll">
Expand Down
22 changes: 14 additions & 8 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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)
Expand Down Expand Up @@ -88,7 +97,6 @@ internal void DockToSide(bool changpos = false)
else
{
lastdockstatus = DockStatus.None;
Topmost = false;
return;
}
lastdockstatus = dockto;
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
62 changes: 52 additions & 10 deletions RoundedWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public CloseBehaviors CloseBehavior
public static readonly new DependencyProperty FocusableProperty =
DependencyProperty.Register("Focusable", typeof(bool), typeof(RoundedWindow), new PropertyMetadata(true));

/// <summary>
/// This maps to the RenderTransformOrigin property of BackgroundGrid.
/// </summary>
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


Expand Down Expand Up @@ -138,13 +150,16 @@ public RoundedWindow()
base.Background = null;
}

private Grid Grid_Main;
/// <summary>
/// This is a grid on which the scaling animations are applied.
/// </summary>
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</Style>

<ControlTemplate x:Key="RoundedWindowControlTemplate" TargetType="{x:Type l:RoundedWindow}">
<Grid x:Name="Grid_Main" RenderTransformOrigin="0,0">
<Grid x:Name="Grid_Main" RenderTransformOrigin="{TemplateBinding RenderTransformOrigin_BG}">
<Grid.CacheMode>
<BitmapCache/>
</Grid.CacheMode>
Expand Down Expand Up @@ -202,5 +202,10 @@
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
<Setter Property="Opacity" Value="0"/>
<Setter Property="CacheMode">
<Setter.Value>
<BitmapCache/>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
6 changes: 3 additions & 3 deletions Win_Search.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Grid>
<Grid.ColumnDefinitions>
Expand All @@ -13,8 +13,8 @@
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" Text="{StaticResource label_search_for}"/>
<TextBox Margin="5" Grid.Column="1" x:Name="TB_Search" Height="23" TextChanged="TB_Search_TextChanged"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{StaticResource label_search_for}"/>
<TextBox Margin="5,5,0,5" Grid.Column="1" x:Name="TB_Search" Height="23" TextChanged="TB_Search_TextChanged"/>
<Button Grid.Column="1" Grid.Row="1" x:Name="Btn_Search" Content="{StaticResource button_search}" HorizontalAlignment="Right" Margin="0,0,55,0" VerticalAlignment="Bottom" Width="50" IsDefault="True" Background="White" Height="20" Click="Btn_Search_Click"/>
<Button Grid.Column="1" Grid.Row="1" x:Name="Btn_Cancel" Content="{StaticResource button_cancel}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="50" IsCancel="True" Background="White" Height="20" Click="Btn_Cancel_Click"/>
</Grid>
Expand Down
Binary file modified bin/Release/DesktopNote.exe
Binary file not shown.

0 comments on commit ef95a78

Please sign in to comment.