Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Settings] ImageResizer settings accessibility updates, fixes and refactor #36903

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading