Skip to content

Commit

Permalink
Crop - Min size 1 and initial size for crop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Dec 25, 2024
1 parent acaabf1 commit 2ba5faa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/PicView.Avalonia/ViewModels/ImageCropperViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public double SelectionWidth
this.RaiseAndSetIfChanged(ref field, value);
PixelSelectionWidth = Convert.ToUInt32(SelectionWidth / AspectRatio);
}
} = 100;
}

public uint PixelSelectionWidth
{
Expand All @@ -82,7 +82,7 @@ public double SelectionHeight
this.RaiseAndSetIfChanged(ref field, value);
PixelSelectionHeight = Convert.ToUInt32(SelectionHeight / AspectRatio);
}
} = 100;
}

public uint PixelSelectionHeight
{
Expand Down
34 changes: 26 additions & 8 deletions src/PicView.Avalonia/Views/UC/CropControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,20 @@ private void InitializeLayout()
}

// Set initial width and height for the crop rectangle
vm.SelectionWidth = 200;
vm.SelectionHeight = 200;
var pixelWidth = vm.ImageWidth / vm.AspectRatio;
var pixelHeight = vm.ImageHeight / vm.AspectRatio;

if (pixelWidth >= 400 || pixelHeight >= 400)
{
vm.SelectionWidth = 200;
vm.SelectionHeight = 200;
}
else if (pixelWidth <= 200 || pixelHeight <= 200)
{
vm.SelectionWidth = pixelWidth / 2;
vm.SelectionHeight = pixelHeight / 2;
}


// Calculate centered position
vm.SelectionX = Convert.ToInt32((vm.ImageWidth - vm.SelectionWidth) / 2);
Expand Down Expand Up @@ -478,11 +490,10 @@ private void ResizeTopLeft(PointerEventArgs e)
{
newHeight = vm.ImageHeight - newTop;
}

if (vm.SelectionX is 0 && vm.SelectionY is 0 && newLeft is 0 && newTop is 0)
{
return;
}

// Prevent the size from becoming too small
newHeight = Math.Max(newHeight, 1);
newWidth = Math.Max(newWidth, 1);

// Apply the new size and position
vm.SelectionX = Convert.ToInt32(newLeft);
Expand Down Expand Up @@ -524,8 +535,9 @@ private void ResizeTopRight(PointerEventArgs e)
newY = 0;
}

// Prevent the height from becoming too small
// Prevent the size from becoming too small
newHeight = Math.Max(newHeight, 1);
newWidth = Math.Max(newWidth, 1);

// Apply the new size and position
vm.SelectionY = Convert.ToInt32(newY);
Expand Down Expand Up @@ -726,6 +738,9 @@ private void ResizeTopMiddle(PointerEventArgs e)
newTop = 0;
newHeight = _originalRect.Height + _originalRect.Y; // Adjust height to compensate
}

// Prevent the size from becoming too small
newHeight = Math.Max(newHeight, 1);

// Update the view model with the new top and height
vm.SelectionHeight = newHeight;
Expand Down Expand Up @@ -763,6 +778,9 @@ private void ResizeBottomMiddle(PointerEventArgs e)
{
newHeight = RootCanvas.Bounds.Height - _originalRect.Y;
}

// Prevent the size from becoming too small
newHeight = Math.Max(newHeight, 1);

// Update the view model with the new height
vm.SelectionHeight = newHeight;
Expand Down

0 comments on commit 2ba5faa

Please sign in to comment.