Skip to content

Commit

Permalink
improve docking timing;
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Chang committed May 14, 2019
1 parent 3d7d7d9 commit b59f84e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 58 deletions.
3 changes: 2 additions & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class App : Application
public static List<MainWindow> MainWindows = new List<MainWindow>();
public static Win_Format FormatWindow;
public static Win_Search SearchWindow;
public static Win_Options OptionsWindow;
public static Rect CurrScrnRect;
//internal const int MaxWindowCount = 2;//need to set this to 4 while debugging if you use live debug toolbar in vs2015.
public const string MutexString = @"{39622D35-176E-453D-B1FD-E4EC1EAF31DC}";
Expand Down Expand Up @@ -130,7 +131,7 @@ public static void Quit(bool savesetting)
{
win.CurrentSetting.Win_Pos = new Point(win.Left, win.Top);
win.CurrentSetting.Win_Size = new Size(win.Width, win.Height);
win.CurrentSetting.DockedTo = (int)win.lastdockstatus;
win.CurrentSetting.DockedTo = (int)win.LastDockStatus;
}
}
DesktopNote.Properties.Settings.Default.Save();
Expand Down
8 changes: 4 additions & 4 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
xmlns:local="clr-namespace:DesktopNote"
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>
MouseEnter="Win_Main_MouseEnter" Loaded="Win_Main_Loaded">
<!--<Window.CacheMode>
<BitmapCache/>
</Window.CacheMode>
</Window.CacheMode>-->
<Window.Resources>
<ContextMenu x:Key="TrayMenu">
<MenuItem Header="{StaticResource menu_newnote}" Click="TM_NewNote_Click"/>
Expand All @@ -26,7 +26,7 @@
</Rectangle>
<RichTextBox x:Name="RTB_Main" Margin="20" Background="{x:Null}" BorderBrush="{x:Null}" AcceptsTab="True" BorderThickness="0" IsInactiveSelectionHighlightEnabled="True"
TextChanged="RTB_Main_TextChanged" PreviewKeyDown="RTB_Main_PreviewKeyDown" PreviewMouseWheel="RTB_Main_PreviewMouseWheel"
GotKeyboardFocus="RTB_Main_GotKeyboardFocus" LostKeyboardFocus="RTB_Main_LostKeyboardFocus"
GotKeyboardFocus="RTB_Main_GotKeyboardFocus"
ContextMenuOpening="RTB_Main_ContextMenuOpening" PreviewMouseDown="RTB_Main_PreviewMouseDown">
<!--<wtk:RichTextBoxFormatBarManager.FormatBar>
<wtk:RichTextBoxFormatBar/>
Expand Down
142 changes: 104 additions & 38 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
//using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace DesktopNote
{
Expand Down Expand Up @@ -40,6 +42,9 @@ public partial class MainWindow : Window
private object Lock_Save = new object();
private int CountDown = 0;
private Point mousepos;
private DispatcherTimer DockTimer;
private int DockTimerCountDown;
private bool IsResizing;

public MainWindow(int setidx)
{
Expand All @@ -48,7 +53,7 @@ public MainWindow(int setidx)
}

#region Docking
public DockStatus lastdockstatus;
public DockStatus LastDockStatus;

public enum DockStatus { None, Docking, Left, Right, Top, Bottom }

Expand Down Expand Up @@ -78,15 +83,19 @@ private static void OnDockedToChanged(DependencyObject d, DependencyPropertyChan
}
}

internal void DockToSide(bool changpos = false)
/// <summary>
/// Dock the MainWindow based on its location.
/// </summary>
/// <param name="changePos">Update LastDockStatus if set to True.</param>
internal void DockToSide(bool changePos = false)
{
if (DockedTo == DockStatus.None)
{
double toval;
DependencyProperty tgtpro;
double pad = 15d;
DockStatus dockto;
if (changpos)
if (changePos)
{
App.CurrScrnRect = new GetCurrentMonitor().GetInfo(this);
if (Left <= App.CurrScrnRect.Left) //dock left
Expand Down Expand Up @@ -118,15 +127,15 @@ internal void DockToSide(bool changpos = false)
}
else
{
lastdockstatus = DockStatus.None;
LastDockStatus = DockStatus.None;
return;
}
lastdockstatus = dockto;
LastDockStatus = dockto;
}
else //'restore last docking position
{
dockto = lastdockstatus;
switch (lastdockstatus)
dockto = LastDockStatus;
switch (LastDockStatus)
{
case DockStatus.Left:
toval = App.CurrScrnRect.Left - ActualWidth + pad;
Expand All @@ -149,10 +158,12 @@ internal void DockToSide(bool changpos = false)
}
}

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)));
anim_fade.BeginTime = new TimeSpan(0, 0, 0, 0, 200);
var anim_move = new DoubleAnimation(toval, new Duration(new TimeSpan(0, 0, 0, 0, 500))) {
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
};
var anim_fade = new DoubleAnimation(0.4, new Duration(new TimeSpan(0, 0, 0, 0, 300))) {
BeginTime = new TimeSpan(0, 0, 0, 0, 200)
};
var anim_prop = new ObjectAnimationUsingKeyFrames();
anim_prop.KeyFrames.Add(new DiscreteObjectKeyFrame(DockStatus.Docking, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
anim_prop.KeyFrames.Add(new DiscreteObjectKeyFrame(dockto, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 500))));
Expand All @@ -164,7 +175,7 @@ internal void DockToSide(bool changpos = false)

internal void UnDock()
{
if (DockedTo != DockStatus.Docking && DockedTo != DockStatus.None)
if ((int)DockedTo > 1)
{
double toval;
DependencyProperty tgtpro;
Expand Down Expand Up @@ -197,8 +208,9 @@ internal void UnDock()
else
return;

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_move = new DoubleAnimation(toval, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.Stop) {
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
};
var anim_fade = new DoubleAnimationUsingKeyFrames();
anim_fade.KeyFrames.Add(new DiscreteDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
var anim_prop = new ObjectAnimationUsingKeyFrames();
Expand All @@ -211,20 +223,80 @@ internal void UnDock()
}
}

/// <summary>
/// Check whether MainWindow should dock based on various mouse, keyboard and window conditions.
/// Should be used with DockToSide(false) as when changePos is true, additional conditions will cause LastDockStatus update to be ignored.
/// </summary>
private bool ShouldDock()
{
#if DEBUG
System.Diagnostics.Debug.Print(
(LastDockStatus != DockStatus.None) + ", " +
!RTB_Main.IsKeyboardFocusWithin + ", " +
!Win_Main.IsMouseOver + ", " +
!IsResizing + ", " +
(App.FormatWindow == null || !App.FormatWindow.MainWin.Equals(this) || App.FormatWindow.Opacity == 0) + ", " +
(App.SearchWindow == null || !App.SearchWindow.MainWin.Equals(this) || App.SearchWindow.Opacity == 0) + ", " +
(App.OptionsWindow == null || !App.OptionsWindow.MainWin.Equals(this) || App.OptionsWindow.Opacity == 0));
#endif
if (CurrentSetting.AutoDock &&
LastDockStatus != DockStatus.None &&
!RTB_Main.IsKeyboardFocusWithin &&
!Win_Main.IsMouseOver &&
!IsResizing &&
(App.FormatWindow == null || !App.FormatWindow.MainWin.Equals(this) || App.FormatWindow.Opacity == 0) &&
(App.SearchWindow == null || !App.SearchWindow.MainWin.Equals(this) || App.SearchWindow.Opacity == 0) &&
(App.OptionsWindow == null || !App.OptionsWindow.MainWin.Equals(this) || App.OptionsWindow.Opacity == 0))
return true;
return false;
}

private void Win_Main_MouseEnter(object sender, MouseEventArgs e)
{
//undocking
if (CurrentSetting.AutoDock && DockedTo != DockStatus.None) UnDock();
if (CurrentSetting.AutoDock && (DockedTo != DockStatus.None || (int)LastDockStatus > 1))
{
UnDock();
//dock if conditions met in 1 min
if (DockTimer != null && DockTimer.IsEnabled) return;
DockTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
DockTimerCountDown = 60;
DockTimer.Tick += (s1, e1) => {
DockTimerCountDown -= 2;
if (DockTimerCountDown <= 0 || (int)DockedTo > 1 || (int)LastDockStatus < 2)
{
DockTimer.Stop();
DockTimer = null;
return;
}
if (ShouldDock())
{
DockToSide();
DockTimer.Stop();
DockTimer = null;
return;
}
};
DockTimer.Start();
}
}

private void Win_Main_MouseLeave(object sender, MouseEventArgs e)
private void Rec_BG_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (CurrentSetting.AutoDock &&
!RTB_Main.IsKeyboardFocusWithin &&
App.FormatWindow.Opacity != 1 &&
App.SearchWindow == null)
DockToSide();
Rec_BG.ReleaseMouseCapture();
if (CurrentSetting.AutoDock) DockToSide(true);
}

//private void Win_Main_MouseLeave(object sender, MouseEventArgs e)
//{
// if (ShouldDock()) DockToSide();
//}

//private void RTB_Main_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
//{
// if (ShouldDock()) DockToSide();
//}
#endregion

#region RichTextBox Events
Expand Down Expand Up @@ -262,14 +334,6 @@ private void RTB_Main_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
}
}

private void RTB_Main_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (CurrentSetting.AutoDock &&
App.FormatWindow.Opacity != 1 &&
App.SearchWindow == null)
DockToSide();
}

private void RTB_Main_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
App.FormatWindow.UpdateTargets(this);
Expand Down Expand Up @@ -309,12 +373,6 @@ private void Rec_BG_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
mousepos = e.GetPosition(this);
}

private void Rec_BG_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Rec_BG.ReleaseMouseCapture();
if (CurrentSetting.AutoDock) DockToSide(true);
}

private void Rec_BG_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && mousepos != null)
Expand Down Expand Up @@ -417,7 +475,7 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e)
Left = CurrentSetting.Win_Pos.X;
Top = CurrentSetting.Win_Pos.Y;

lastdockstatus = (DockStatus)CurrentSetting.DockedTo;
LastDockStatus = (DockStatus)CurrentSetting.DockedTo;
//DockedTo = DockStatus.None;
RTB_Main.FontFamily = new FontFamily(CurrentSetting.Font);
RTB_Main.Foreground = new SolidColorBrush(CurrentSetting.FontColor);
Expand Down Expand Up @@ -489,7 +547,10 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e)
RTB_Main.IsUndoEnabled = true;
//without the above two lines, Load actions can be undone.

if (CurrentSetting.AutoDock) DockToSide(true);
if (CurrentSetting.AutoDock)
{
Task.Run(() => { Thread.Sleep(2000); Dispatcher.Invoke(() => DockToSide(true)); });
}

var task_save = new Thread(SaveNotes) { IsBackground = true };
task_save.Start();
Expand Down Expand Up @@ -529,6 +590,11 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
//resetting position was moved to tray menu.
UnDock();
}
else if (msg == 0x231) //WM_ENTERSIZEMOVE
IsResizing = true;
else if (msg == 0x232) //WM_EXITSIZEMOVE
IsResizing = false;

return IntPtr.Zero;
}

Expand All @@ -553,7 +619,7 @@ private void TM_ResetPos_Click(object sender, RoutedEventArgs e)
win.Top = App.CurrScrnRect.Top + delta;
win.Left = App.CurrScrnRect.Left + +delta;
win.DockedTo = DockStatus.None;
win.lastdockstatus = DockStatus.None;
win.LastDockStatus = DockStatus.None;
i += 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.3.0")]
[assembly: AssemblyFileVersion("1.4.3.0")]
[assembly: AssemblyVersion("1.4.4.0")]
[assembly: AssemblyFileVersion("1.4.4.0")]
16 changes: 12 additions & 4 deletions Win_Options.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System;
using System.Windows;
using System.Windows.Controls;
//using System.Windows.Data;
Expand All @@ -16,16 +17,23 @@ namespace DesktopNote
{
public partial class Win_Options : RoundedWindow
{
private readonly MainWindow MainWin;
public readonly MainWindow MainWin;
private readonly RichTextBox RTB_Main;
private string assname = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

public Win_Options(MainWindow owner)
public Win_Options(MainWindow mainwin)
{
InitializeComponent();
Owner = owner;
MainWin = owner;
Owner = mainwin;
MainWin = mainwin;
RTB_Main = MainWin.RTB_Main;
App.OptionsWindow = this;
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
App.OptionsWindow = null;
}

private void CB_AutoStart_Click(object sender, RoutedEventArgs e)
Expand Down
19 changes: 10 additions & 9 deletions Win_Search.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ namespace DesktopNote
{
public partial class Win_Search : RoundedWindow
{
private readonly MainWindow MainWin;
public readonly MainWindow MainWin;
private bool textchanged = true;

public Win_Search(MainWindow owner)
public Win_Search(MainWindow mainwin)
{
InitializeComponent();
Owner = owner;
MainWin = owner;
Owner = mainwin;
MainWin = mainwin;
App.SearchWindow = this;
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
App.SearchWindow = null;
}

private void MarkTextInRange(RichTextBox richTextBox, string searchText, bool searchNext)
{
//Get the range to search
Expand Down Expand Up @@ -91,10 +97,5 @@ private void Btn_Cancel_Click(object sender, RoutedEventArgs e)
Close();
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
App.SearchWindow = null;
}
}
}
Binary file modified bin/Release/DesktopNote.exe
Binary file not shown.

0 comments on commit b59f84e

Please sign in to comment.