Skip to content

Commit e9fddf6

Browse files
committed
Adds selection key bindings shortcuts support.
1 parent f781b61 commit e9fddf6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/DatasetCrop/MainWindow.axaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
mc:Ignorable="d" d:DesignWidth="1100" d:DesignHeight="450" MinWidth="1325" MinHeight="500"
66
x:Class="DatasetCrop.MainWindow"
7-
Title="Dataset Crop" WindowStartupLocation="CenterScreen" WindowState="Maximized" Icon="/Assets/crop.png">
7+
Title="Dataset Crop" WindowStartupLocation="CenterScreen" WindowState="Maximized" Icon="/Assets/crop.png" KeyDown="Window_KeyDown">
88
<DockPanel Background="{DynamicResource WindowBackground}">
99
<Menu DockPanel.Dock="Top" Background="{DynamicResource WindowBackground}" Foreground="{DynamicResource TextForeground}">
1010
<MenuItem Header="_File" Background="{DynamicResource WindowBackground}" Foreground="{DynamicResource TextForeground}">
@@ -14,6 +14,9 @@
1414
</MenuItem>
1515
<MenuItem Header="Quit" Background="{DynamicResource WindowBackground}" Foreground="{DynamicResource TextForeground}" Click="CloseApplication_Click"/>
1616
</MenuItem>
17+
<MenuItem Header="_Tools" Background="{DynamicResource WindowBackground}" Foreground="{DynamicResource TextForeground}">
18+
<MenuItem Header="Key Bindings" Background="{DynamicResource WindowBackground}" Foreground="{DynamicResource TextForeground}" Click="ShowKeyBingings_Click"/>
19+
</MenuItem>
1720
</Menu>
1821
<Grid>
1922
<Label Content="Input path:" Foreground="{DynamicResource TextForeground}" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Left" Margin="6,15,0,0" Width="100" Height="23"/>

src/DatasetCrop/MainWindow.axaml.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private async Task RefreshImagePreviewsAsync()
168168
return;
169169
if (!await ValidateCropParameters())
170170
return;
171+
Title = "Dataset Crop - Working...";
171172
// clear previous image previews
172173
ClearImagePreviews();
173174
int column = 0;
@@ -233,6 +234,8 @@ await Dispatcher.UIThread.InvokeAsync(() =>
233234
// for each image, ensure the drag panel is not in any way bigger than the visible area of the scaled down image preview
234235
if (!ValidateCropPanelSize(loadedImage.OriginalSize.Width, loadedImage.OriginalSize.Height, cropWidth, cropHeight, previewWidth, previewHeight))
235236
{
237+
dragPanel.Width = previewWidth;
238+
dragPanel.Height = previewHeight;
236239
dragPanel.Tag = false; // do not select the image, if its crop parameters are not ok
237240
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, 0, 0), 0.3);
238241
dragPanel.IsEnabled = false; // don't allow dragging, when the image is not valid for cropping
@@ -263,6 +266,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
263266
}
264267
});
265268
});
269+
Title = "Dataset Crop";
266270
}
267271

268272
/// <summary>
@@ -316,6 +320,7 @@ private async Task CropImagesAsync()
316320
return;
317321
if (!await ValidateImageFiles())
318322
return;
323+
Title = "Dataset Crop - Working...";
319324
try
320325
{
321326
foreach (var container in grdImages.Children.OfType<Grid>())
@@ -352,7 +357,6 @@ private async Task CropImagesAsync()
352357
originalCropWidth = dragPanel.Width * scaleX;
353358
originalCropHeight = dragPanel.Height * scaleY;
354359
}
355-
byte[] imageBytes = await File.ReadAllBytesAsync(originalImagePath);
356360
IImageFormat imageFormat = originalExtension.ToLower() switch
357361
{
358362
".png" => PngFormat.Instance,
@@ -366,6 +370,7 @@ private async Task CropImagesAsync()
366370
}
367371
}
368372
}
373+
Title = "Dataset Crop";
369374
await MessageBoxManager.GetMessageBoxStandardWindow("Success!", "Images cropped!", ButtonEnum.Ok, MessageBox.Avalonia.Enums.Icon.Success).ShowDialog(this);
370375
}
371376
catch (Exception ex)
@@ -745,5 +750,48 @@ private void DragPanel_PointerMoved(object? sender, PointerEventArgs e)
745750
panel.Margin = new Thickness(newX, newY, 0, 0);
746751
}
747752
}
753+
754+
/// <summary>
755+
/// Handles the window key down events.
756+
/// </summary>
757+
private void Window_KeyDown(object? sender, KeyEventArgs e)
758+
{
759+
if (IsSelectionMode)
760+
{
761+
foreach (var container in grdImages.Children.OfType<Grid>())
762+
{
763+
var dragPanel = container.Children.OfType<Panel>().FirstOrDefault();
764+
if (!dragPanel.IsEnabled) // skip images where the drag panel is bigger than the images - they are not valid for selecting/cropping
765+
continue;
766+
if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.A) // ctrl+a selects all
767+
{
768+
dragPanel.Tag = true;
769+
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, 255, 255), 0.3);
770+
}
771+
else if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.N) // ctrl+n selects none
772+
{
773+
dragPanel.Tag = false;
774+
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, 0, 0), 0.3);
775+
}
776+
else if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.I) // ctrl+i inverts selection
777+
{
778+
bool isSelected = (bool)dragPanel.Tag!;
779+
dragPanel.Tag = !isSelected;
780+
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, (byte)(!isSelected ? 255 : 0), (byte)(!isSelected ? 255 : 0)), 0.3);
781+
}
782+
}
783+
}
784+
}
785+
786+
/// <summary>
787+
/// Handles Show Key Bindings click event.
788+
/// </summary>
789+
private async void ShowKeyBingings_Click(object? sender, RoutedEventArgs e)
790+
{
791+
await MessageBoxManager.GetMessageBoxStandardWindow("Key Bindings",
792+
"Ctrl + A: in Selection Mode, selects all images\n" +
793+
"Ctrl + N: in Selection Mode, deselects all images\n" +
794+
"Ctrl + I: in Selection Mode, inverts images selection\n", ButtonEnum.Ok, MessageBox.Avalonia.Enums.Icon.Info).ShowDialog(this);
795+
}
748796
#endregion
749797
}

0 commit comments

Comments
 (0)