Add drag drop thresholds

This commit is contained in:
Craftplacer 2021-04-30 09:27:11 +02:00
parent 99f40a4951
commit 786206f2b8

View file

@ -88,6 +88,8 @@ public class NotificationForm : Form
private Size titleRenderSize;
private Size textRenderSize;
private Size totalRenderSize;
private bool isMouseDragging;
private Point dragStart;
protected override CreateParams CreateParams
{
@ -354,6 +356,7 @@ private void NotificationForm_MouseEnter(object sender, EventArgs e)
private void NotificationForm_MouseLeave(object sender, EventArgs e)
{
isMouseInside = false;
isMouseDragging = false;
Refresh();
if (isDurationEnd)
@ -364,13 +367,41 @@ private void NotificationForm_MouseLeave(object sender, EventArgs e)
private void NotificationForm_MouseDown(object sender, MouseEventArgs e)
{
if (!string.IsNullOrEmpty(Config.FilePath) && File.Exists(Config.FilePath))
dragStart = e.Location;
isMouseDragging = true;
}
private void NotificationForm_MouseMove(object sender, MouseEventArgs e)
{
// We add a threshold before triggering the drag-drop operation
// in order to fix MouseClick
if (isMouseDragging)
{
IDataObject dataObject = new DataObject(DataFormats.FileDrop, new string[] { Config.FilePath });
DoDragDrop(dataObject, DragDropEffects.Copy | DragDropEffects.Move);
// The radius around the mouse, until a drag-drop operation
// gets triggered
const int dragThreshold = 20;
Rectangle dragThresholdRectangle = new Rectangle(dragStart.X - dragThreshold,
dragStart.Y + dragThreshold,
dragThreshold * 2,
dragThreshold * 2);
bool isOverThreshold = !dragThresholdRectangle.Contains(e.Location);
if (isOverThreshold && !string.IsNullOrEmpty(Config.FilePath) && File.Exists(Config.FilePath))
{
IDataObject dataObject = new DataObject(DataFormats.FileDrop, new string[] { Config.FilePath });
DoDragDrop(dataObject, DragDropEffects.Copy | DragDropEffects.Move);
isMouseDragging = false;
}
}
}
private void NotificationForm_MouseUp(object sender, MouseEventArgs e)
{
isMouseDragging = false;
}
#region Windows Form Designer generated code
private Timer tDuration;
@ -423,6 +454,8 @@ private void InitializeComponent()
MouseEnter += new EventHandler(NotificationForm_MouseEnter);
MouseLeave += new EventHandler(NotificationForm_MouseLeave);
MouseDown += new MouseEventHandler(NotificationForm_MouseDown);
MouseMove += new MouseEventHandler(NotificationForm_MouseMove);
MouseUp += new MouseEventHandler(NotificationForm_MouseUp);
ResumeLayout(false);
}