Skip to content

Commit 37602f2

Browse files
committed
feat: ScreenShotAdorner does not follow when dragged outside the canvas
1 parent 92ed016 commit 37602f2

1 file changed

Lines changed: 68 additions & 55 deletions

File tree

src/WPFDevelopers.Shared/Controls/ScreenCut/ScreenCutAdorner.cs

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
using System.Windows;
1+
using System;
2+
using System.Windows;
23
using System.Windows.Controls;
34
using System.Windows.Controls.Primitives;
45
using System.Windows.Documents;
56
using System.Windows.Input;
67
using System.Windows.Media;
78
using System.Windows.Shapes;
8-
using WPFDevelopers.Core;
99

1010
namespace WPFDevelopers.Controls
1111
{
1212
public class ScreenCutAdorner : Adorner
1313
{
14-
private const double THUMB_SIZE = 15;
14+
private const double THUMB_SIZE = 10;
1515
private const double MINIMAL_SIZE = 20;
1616
private const double LINE_Size = 6;
1717
private readonly Thumb lc;
@@ -139,78 +139,91 @@ private Thumb GetResizeThumb(Cursor cur, HorizontalAlignment hor, VerticalAlignm
139139
}
140140
};
141141
}
142-
143-
144-
var maxWidth = double.IsNaN(canvas.Width) ? canvas.ActualWidth : canvas.Width;
145-
var maxHeight = double.IsNaN(canvas.Height) ? canvas.ActualHeight : canvas.Height;
142+
146143
thumb.DragDelta += (s, e) =>
147144
{
148145
var element = AdornedElement as FrameworkElement;
149146
if (element == null)
150147
return;
151148
Resize(element);
152149

150+
if (double.IsNaN(Canvas.GetLeft(element)))
151+
Canvas.SetLeft(element, 0);
152+
if (double.IsNaN(Canvas.GetTop(element)))
153+
Canvas.SetTop(element, 0);
154+
155+
double left = Canvas.GetLeft(element);
156+
double top = Canvas.GetTop(element);
157+
158+
double canvasWidth = canvas.ActualWidth;
159+
double canvasHeight = canvas.ActualHeight;
160+
153161
switch (thumb.VerticalAlignment)
154162
{
155-
case VerticalAlignment.Bottom:
156-
if (element.Height + e.VerticalChange > MINIMAL_SIZE)
163+
case VerticalAlignment.Top:
157164
{
158-
var newHeight = element.Height + e.VerticalChange;
159-
var top = Canvas.GetTop(element) + newHeight;
160-
if (newHeight > 0 && top <= canvas.ActualHeight)
161-
{
162-
element.Height = newHeight;
163-
if(_isRatioScale)
164-
ScaleWidth(thumb, element, newHeight);
165-
}
165+
double newTop = top + e.VerticalChange;
166+
double maxTop = top + element.Height - MINIMAL_SIZE;
167+
168+
newTop = Math.Max(0, Math.Min(newTop, maxTop));
169+
170+
double newHeight = element.Height + (top - newTop);
171+
172+
Canvas.SetTop(element, newTop);
173+
element.Height = newHeight;
174+
175+
if (_isRatioScale)
176+
ScaleWidth(thumb, element, newHeight);
166177
}
167178
break;
168179

169-
case VerticalAlignment.Top:
170-
if (element.Height - e.VerticalChange > MINIMAL_SIZE)
180+
case VerticalAlignment.Bottom:
171181
{
172-
var newHeight = element.Height - e.VerticalChange;
173-
var top = Canvas.GetTop(element);
174-
if (newHeight > 0 && top + e.VerticalChange >= 0)
175-
{
176-
element.Height = newHeight;
177-
Canvas.SetTop(element, top + e.VerticalChange);
178-
if (_isRatioScale)
179-
ScaleWidth(thumb, element, newHeight);
180-
}
181-
}
182+
double maxHeight = canvasHeight - top;
183+
184+
double newHeight = element.Height + e.VerticalChange;
185+
newHeight = Math.Max(MINIMAL_SIZE, newHeight);
186+
newHeight = Math.Min(newHeight, maxHeight);
182187

188+
element.Height = newHeight;
189+
190+
if (_isRatioScale)
191+
ScaleWidth(thumb, element, newHeight);
192+
}
183193
break;
184194
}
185195

186196
switch (thumb.HorizontalAlignment)
187197
{
188198
case HorizontalAlignment.Left:
189-
if (element.Width - e.HorizontalChange > MINIMAL_SIZE)
190199
{
191-
var newWidth = element.Width - e.HorizontalChange;
192-
var left = Canvas.GetLeft(element);
193-
if (newWidth > 0 && left + e.HorizontalChange >= 0)
194-
{
195-
element.Width = newWidth;
196-
Canvas.SetLeft(element, left + e.HorizontalChange);
197-
if (_isRatioScale)
198-
ScaleHeight(thumb, element, newWidth);
199-
}
200-
}
200+
double newLeft = left + e.HorizontalChange;
201+
double maxLeft = left + element.Width - MINIMAL_SIZE;
202+
203+
newLeft = Math.Max(0, Math.Min(newLeft, maxLeft));
204+
205+
double newWidth = element.Width + (left - newLeft);
201206

207+
Canvas.SetLeft(element, newLeft);
208+
element.Width = newWidth;
209+
210+
if (_isRatioScale)
211+
ScaleHeight(thumb, element, newWidth);
212+
}
202213
break;
214+
203215
case HorizontalAlignment.Right:
204-
if (element.Width + e.HorizontalChange > MINIMAL_SIZE)
205216
{
206-
var newWidth = element.Width + e.HorizontalChange;
207-
var left = Canvas.GetLeft(element) + newWidth;
208-
if (newWidth > 0 && left <= canvas.ActualWidth)
209-
{
210-
element.Width = newWidth;
211-
if (_isRatioScale)
212-
ScaleHeight(thumb, element, newWidth);
213-
}
217+
double maxWidth = canvasWidth - left;
218+
219+
double newWidth = element.Width + e.HorizontalChange;
220+
newWidth = Math.Max(MINIMAL_SIZE, newWidth);
221+
newWidth = Math.Min(newWidth, maxWidth);
222+
223+
element.Width = newWidth;
224+
225+
if (_isRatioScale)
226+
ScaleHeight(thumb, element, newWidth);
214227
}
215228
break;
216229
}
@@ -246,12 +259,12 @@ void ScaleWidth(Thumb thumb, FrameworkElement element, double newHeight)
246259
void ScaleHeight(Thumb thumb, FrameworkElement element, double newWidth)
247260
{
248261
if (_isRatioScale
249-
&&
250-
thumb.VerticalAlignment != VerticalAlignment.Top
251-
||
252-
(thumb.HorizontalAlignment != HorizontalAlignment.Left
253-
&&
254-
thumb.HorizontalAlignment != HorizontalAlignment.Right))
262+
&&
263+
thumb.VerticalAlignment != VerticalAlignment.Top
264+
||
265+
(thumb.HorizontalAlignment != HorizontalAlignment.Left
266+
&&
267+
thumb.HorizontalAlignment != HorizontalAlignment.Right))
255268
{
256269
if (!_scaleSize.IsEmpty
257270
&&

0 commit comments

Comments
 (0)