Skip to content

Commit

Permalink
🎨 Try to resynchronize image cropping UI better.
Browse files Browse the repository at this point in the history
Sadly, the component is hard to work with. Still unable to detect when the mouse is released outside of the component.
  • Loading branch information
hexawyz committed Feb 8, 2025
1 parent ce9e004 commit 2e4c324
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
Height="{Binding ImageSize.Height}"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
PointerCanceled="OnImageCropperPointerCanceled"
PointerCaptureLost="OnImageCropperPointerCaptureLost"
PointerReleased="OnImageCropperPointerReleased"
KeyUp="OnImageCropperKeyUp" />
<controls:WrapPanel Grid.Column="1" Orientation="Vertical" Padding="{StaticResource RowContentMargin}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public EmbeddedMonitorImageGraphicsViewModel? ImageGraphics
public EmbeddedMonitorImageSettingsControl()
{
InitializeComponent();
ImageCropper.RegisterPropertyChangedCallback(CommunityToolkit.WinUI.Controls.ImageCropper.SourceProperty, OnImageCropperPropertyChanged);
}

private void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
Expand All @@ -38,6 +39,15 @@ private void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
}
}

private void OnImageCropperPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
if (dp == CommunityToolkit.WinUI.Controls.ImageCropper.SourceProperty)
{
// Quick and dirty fix to be sure that the cropping region is properly synchronized when we switch images.
UpdateCroppedRegionToGraphics();
}
}

private void OnImageGraphicsPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (BindableObject.Equals(e, ChangedProperty.CropRectangle))
Expand All @@ -61,6 +71,14 @@ private void UpdateCroppedRegionToGraphics()
if (ImageGraphics is { } imageGraphics)
{
var rectangle = ImageCropper.CroppedRegion;
if (rectangle._x < 0 || rectangle._x > int.MaxValue ||
rectangle._y < 0 || rectangle._y > int.MaxValue ||
rectangle._width < 0 || rectangle._width > int.MaxValue ||
rectangle._height < 0 || rectangle._height > int.MaxValue)
{
return;
}

var image = imageGraphics.Image;

// Resolve the aspect ratio thingy here. We want to truncate the coordinates into integers.
Expand All @@ -86,7 +104,7 @@ private void UpdateCroppedRegionToGraphics()
// Basically, determining the two WxH candidates would be the simplest by first identifying the smallest P/Q fraction corresponding to the requested aspect ratio.
// This is easily done by computing the GCD.

uint gcd = Gcd((uint)imageGraphics.ImageSize.Width, (uint)imageGraphics.ImageSize.Height);
uint gcd = MathUtils.Gcd((uint)imageGraphics.ImageSize.Width, (uint)imageGraphics.ImageSize.Height);
uint p = (uint)imageGraphics.ImageSize.Width / gcd;
uint q = (uint)imageGraphics.ImageSize.Height / gcd;

Expand Down Expand Up @@ -137,20 +155,17 @@ private static (uint a, uint b) GetMinimumMatchingArea(uint a, uint b, uint p, u
return (n * p, n * q);
}

private static uint Gcd(uint a, uint b)
private void OnImageCropperPointerReleased(object sender, PointerRoutedEventArgs e)
{
while (a != 0 && b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}
UpdateCroppedRegionToGraphics();
}

return a | b;
private void OnImageCropperPointerCanceled(object sender, PointerRoutedEventArgs e)
{
UpdateCroppedRegionToGraphics();
}

private void OnImageCropperPointerReleased(object sender, PointerRoutedEventArgs e)
private void OnImageCropperPointerCaptureLost(object sender, PointerRoutedEventArgs e)
{
UpdateCroppedRegionToGraphics();
}
Expand Down
17 changes: 17 additions & 0 deletions src/Exo/Ui/Exo.Settings.Ui/MathUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Exo.Settings.Ui;

public static class MathUtils
{
public static uint Gcd(uint a, uint b)
{
while (a != 0 && b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}

return a | b;
}
}
1 change: 0 additions & 1 deletion src/Exo/Ui/Exo.Settings.Ui/RgbLightingDefaultPalette.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using CommunityToolkit.WinUI.Controls;
using Windows.UI;

Expand Down
2 changes: 0 additions & 2 deletions src/Exo/Ui/Exo.Settings.Ui/WellKnownGuids.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Exo.Settings.Ui;

internal static class WellKnownGuids
Expand Down

0 comments on commit 2e4c324

Please sign in to comment.