Skip to content

Commit

Permalink
[Settings]ImageResizer settings accessibility updates, fixes and refa…
Browse files Browse the repository at this point in the history
…ctor (#36903)

* Fix issue with missing Image Resizer unit and fit information in settings description.

* Fix accessibility issues on Edit and Remove buttons. Fix various issues and refactor view model and ImageSize. New resources for accessibility text formats.

* Fix unit test because of change to new preset width and height. Fix 2 unit tests having incorrect expected/actual orderings.

* Post-review update: accessibility strings now formatted within the converter, instead of via format strings; simplified encoder GUID collection declaration and retrieval.

* Minor example text fix.
  • Loading branch information
daverayment authored Jan 21, 2025
1 parent b33e0be commit 438d173
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 619 deletions.
294 changes: 86 additions & 208 deletions src/settings-ui/Settings.UI.Library/ImageSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,241 +3,119 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using Settings.UI.Library.Resources;

namespace Microsoft.PowerToys.Settings.UI.Library
{
public class ImageSize : INotifyPropertyChanged
{
public ImageSize(int id)
{
Id = id;
Name = string.Empty;
Fit = ResizeFit.Fit;
Width = 0;
Height = 0;
Unit = ResizeUnit.Pixel;
}

public ImageSize()
{
Id = 0;
Name = string.Empty;
Fit = ResizeFit.Fit;
Width = 0;
Height = 0;
Unit = ResizeUnit.Pixel;
}

public ImageSize(int id, string name, ResizeFit fit, double width, double height, ResizeUnit unit)
{
Id = id;
Name = name;
Fit = fit;
Width = width;
Height = height;
Unit = unit;
}
namespace Microsoft.PowerToys.Settings.UI.Library;

private int _id;
private string _name;
private ResizeFit _fit;
private double _height;
private double _width;
private ResizeUnit _unit;

public int Id
{
get
{
return _id;
}

set
{
if (_id != value)
{
_id = value;
OnPropertyChanged();
}
}
}

public int ExtraBoxOpacity
{
get
{
if (Unit == ResizeUnit.Percent && Fit != ResizeFit.Stretch)
{
return 0;
}
else
{
return 100;
}
}
}
public partial class ImageSize : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public bool EnableEtraBoxes
private bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
bool changed = !EqualityComparer<T>.Default.Equals(field, value);
if (changed)
{
get
{
if (Unit == ResizeUnit.Percent && Fit != ResizeFit.Stretch)
{
return false;
}
else
{
return true;
}
}
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AccessibleTextHelper)));
}

[JsonPropertyName("name")]
public string Name
{
get
{
return _name;
}
return changed;
}

set
{
if (_name != value)
{
_name = value;
OnPropertyChanged();
}
}
}
public ImageSize(int id = 0, string name = "", ResizeFit fit = ResizeFit.Fit, double width = 0, double height = 0, ResizeUnit unit = ResizeUnit.Pixel)
{
Id = id;
Name = name;
Fit = fit;
Width = width;
Height = height;
Unit = unit;
}

[JsonPropertyName("fit")]
public ResizeFit Fit
{
get
{
return _fit;
}
private int _id;
private string _name;
private ResizeFit _fit;
private double _height;
private double _width;
private ResizeUnit _unit;

set
{
if (_fit != value)
{
_fit = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ExtraBoxOpacity));
OnPropertyChanged(nameof(EnableEtraBoxes));
}
}
}
public int Id
{
get => _id;
set => SetProperty(ref _id, value);
}

[JsonPropertyName("width")]
public double Width
{
get
{
return _width;
}
/// <summary>
/// Gets a value indicating whether the <see cref="Height"/> property is used. When false, the
/// <see cref="Width"/> property is used to evenly scale the image in both X and Y dimensions.
/// </summary>
[JsonIgnore]
public bool IsHeightUsed
{
// Height is ignored when using percentage scaling where the aspect ratio is maintained
// (i.e. non-stretch fits). In all other cases, both Width and Height are needed.
get => !(Unit == ResizeUnit.Percent && Fit != ResizeFit.Stretch);
}

set
{
double newWidth = -1;

if (value < 0 || double.IsNaN(value))
{
newWidth = 0;
}
else
{
newWidth = value;
}

if (_width != newWidth)
{
_width = newWidth;
OnPropertyChanged();
}
}
}
[JsonPropertyName("name")]
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}

[JsonPropertyName("height")]
public double Height
[JsonPropertyName("fit")]
public ResizeFit Fit
{
get => _fit;
set
{
get
{
return _height;
}

set
if (SetProperty(ref _fit, value))
{
double newHeight = -1;

if (value < 0 || double.IsNaN(value))
{
newHeight = 0;
}
else
{
newHeight = value;
}

if (_height != newHeight)
{
_height = newHeight;
OnPropertyChanged();
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsHeightUsed)));
}
}
}

[JsonPropertyName("unit")]
public ResizeUnit Unit
{
get
{
return _unit;
}

set
{
if (_unit != value)
{
_unit = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ExtraBoxOpacity));
OnPropertyChanged(nameof(EnableEtraBoxes));
}
}
}
[JsonPropertyName("width")]
public double Width
{
get => _width;
set => SetProperty(ref _width, value < 0 || double.IsNaN(value) ? 0 : value);
}

public event PropertyChangedEventHandler PropertyChanged;
[JsonPropertyName("height")]
public double Height
{
get => _height;
set => SetProperty(ref _height, value < 0 || double.IsNaN(value) ? 0 : value);
}

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
[JsonPropertyName("unit")]
public ResizeUnit Unit
{
get => _unit;
set
{
var handler = PropertyChanged;
if (handler != null)
if (SetProperty(ref _unit, value))
{
handler(this, new PropertyChangedEventArgs(propertyName));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsHeightUsed)));
}
}
}

public void Update(ImageSize modifiedSize)
{
ArgumentNullException.ThrowIfNull(modifiedSize);

Id = modifiedSize.Id;
Name = modifiedSize.Name;
Fit = modifiedSize.Fit;
Width = modifiedSize.Width;
Height = modifiedSize.Height;
Unit = modifiedSize.Unit;
}
/// <summary>
/// Gets access to all properties for formatting accessibility descriptions.
/// </summary>
[JsonIgnore]
public ImageSize AccessibleTextHelper => this;

public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
public string ToJsonString() => JsonSerializer.Serialize(this);
}
Loading

0 comments on commit 438d173

Please sign in to comment.