@@ -42,7 +42,9 @@ public void OnMouseMove(Point position, Canvas canvas, MouseButtonState leftButt
4242 if ( _isCreatingRectangle && _currentRectangle != null && _rectangleStartPoint . HasValue )
4343 {
4444 // Update rectangle dimensions to follow cursor
45- UpdateRectangle ( _rectangleStartPoint . Value , position ) ;
45+ // Check if Shift key is held down for perfect square
46+ bool isShiftPressed = Keyboard . IsKeyDown ( Key . LeftShift ) || Keyboard . IsKeyDown ( Key . RightShift ) ;
47+ UpdateRectangle ( _rectangleStartPoint . Value , position , isShiftPressed ) ;
4648 }
4749 }
4850
@@ -117,7 +119,7 @@ private void StartNewRectangle(Point startPoint, Canvas canvas)
117119 _logger . LogInformation ( "Rectangle started at ({X:F0}, {Y:F0})" , startPoint . X , startPoint . Y ) ;
118120 }
119121
120- private void UpdateRectangle ( Point startPoint , Point currentPoint )
122+ private void UpdateRectangle ( Point startPoint , Point currentPoint , bool isPerfectSquare = false )
121123 {
122124 if ( _currentRectangle == null )
123125 return ;
@@ -128,6 +130,14 @@ private void UpdateRectangle(Point startPoint, Point currentPoint)
128130 double width = Math . Abs ( currentPoint . X - startPoint . X ) ;
129131 double height = Math . Abs ( currentPoint . Y - startPoint . Y ) ;
130132
133+ // If Shift is held, make it a perfect square (width = height = min dimension)
134+ if ( isPerfectSquare )
135+ {
136+ double size = Math . Min ( width , height ) ;
137+ width = size ;
138+ height = size ;
139+ }
140+
131141 Canvas . SetLeft ( _currentRectangle , left ) ;
132142 Canvas . SetTop ( _currentRectangle , top ) ;
133143 _currentRectangle . Width = width ;
@@ -138,7 +148,9 @@ private void FinishRectangle(Point endPoint)
138148 {
139149 if ( _currentRectangle != null && _rectangleStartPoint . HasValue )
140150 {
141- UpdateRectangle ( _rectangleStartPoint . Value , endPoint ) ;
151+ // Check if Shift was held on the final click
152+ bool isShiftPressed = Keyboard . IsKeyDown ( Key . LeftShift ) || Keyboard . IsKeyDown ( Key . RightShift ) ;
153+ UpdateRectangle ( _rectangleStartPoint . Value , endPoint , isShiftPressed ) ;
142154 _logger . LogInformation ( "Rectangle finished at ({X:F0}, {Y:F0})" , endPoint . X , endPoint . Y ) ;
143155 }
144156
0 commit comments