-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Continue implementing the embedded monitor UI.
This adds the ImageCropper component.
- Loading branch information
Showing
13 changed files
with
180 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Exo/Ui/Exo.Settings.Ui/Converters/FileNameToWriteableBitmapConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Exo.Settings.Ui.ViewModels; | ||
using Microsoft.UI.Xaml.Data; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
using Windows.Storage.Streams; | ||
|
||
namespace Exo.Settings.Ui.Converters; | ||
|
||
internal sealed class ImageToWriteableBitmapConverter : IValueConverter | ||
{ | ||
private readonly Dictionary<string, WeakReference<WriteableBitmap>> _bitmapCache = new(StringComparer.OrdinalIgnoreCase); | ||
|
||
public object? Convert(object value, Type targetType, object parameter, string language) | ||
{ | ||
if (value is not ImageViewModel image) return null; | ||
|
||
// In this case, we take the bet that the filename and the file will "always" be valid. As such, we pre-allocate a weak reference before knowing if the file will be read successfully. | ||
WriteableBitmap? bitmapImage; | ||
if (_bitmapCache.TryGetValue(image.FileName, out var wr)) | ||
{ | ||
if (wr.TryGetTarget(out bitmapImage)) return bitmapImage; | ||
bitmapImage = new(image.Width, image.Height); | ||
wr.SetTarget(bitmapImage); | ||
} | ||
else | ||
{ | ||
bitmapImage = new(image.Width, image.Height); | ||
_bitmapCache.Add(image.FileName, new(bitmapImage, false)); | ||
} | ||
|
||
LoadImage(bitmapImage, image.FileName); | ||
return bitmapImage; | ||
} | ||
|
||
private async void LoadImage(WriteableBitmap bitmapImage, string fileName) | ||
{ | ||
try | ||
{ | ||
using (var randomAccessStream = await FileRandomAccessStream.OpenAsync(fileName, Windows.Storage.FileAccessMode.Read, Windows.Storage.StorageOpenOptions.AllowOnlyReaders, FileOpenDisposition.OpenExisting)) | ||
{ | ||
await bitmapImage.SetSourceAsync(randomAccessStream); | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Exo/Ui/Exo.Settings.Ui/Converters/MonitorShapeToCropShapeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using CommunityToolkit.WinUI.Controls; | ||
using Exo.Contracts.Ui.Settings; | ||
using Microsoft.UI.Xaml.Data; | ||
|
||
namespace Exo.Settings.Ui.Converters; | ||
|
||
internal sealed class MonitorShapeToCropShapeConverter : IValueConverter | ||
{ | ||
public object? Convert(object value, Type targetType, object parameter, string language) | ||
=> value is MonitorShape.Circle ? CropShape.Circular : CropShape.Rectangular; | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, string language) | ||
=> throw new NotSupportedException(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters