2020-12-21 15:55:50 +01:00
|
|
|
// Copyright (c) Microsoft Corporation
|
2020-04-26 17:34:03 -07:00
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
2020-10-19 13:32:05 -07:00
|
|
|
using System;
|
2025-01-21 11:55:02 +00:00
|
|
|
using System.Collections.Generic;
|
2020-04-26 17:34:03 -07:00
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2025-01-21 11:55:02 +00:00
|
|
|
using Settings.UI.Library.Resources;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Library;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
public partial class ImageSize : INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
2020-06-19 13:18:37 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
private bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
bool changed = !EqualityComparer<T>.Default.Equals(field, value);
|
|
|
|
|
if (changed)
|
2020-06-19 13:18:37 -07:00
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
field = value;
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AccessibleTextHelper)));
|
2020-06-19 13:18:37 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
return changed;
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
private int _id;
|
|
|
|
|
private string _name;
|
|
|
|
|
private ResizeFit _fit;
|
|
|
|
|
private double _height;
|
|
|
|
|
private double _width;
|
|
|
|
|
private ResizeUnit _unit;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
public int Id
|
|
|
|
|
{
|
|
|
|
|
get => _id;
|
|
|
|
|
set => SetProperty(ref _id, value);
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
/// <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);
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
[JsonPropertyName("name")]
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get => _name;
|
|
|
|
|
set => SetProperty(ref _name, value);
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
[JsonPropertyName("fit")]
|
|
|
|
|
public ResizeFit Fit
|
|
|
|
|
{
|
|
|
|
|
get => _fit;
|
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
if (SetProperty(ref _fit, value))
|
2020-04-26 17:34:03 -07:00
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsHeightUsed)));
|
2020-04-26 17:34:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
[JsonPropertyName("width")]
|
|
|
|
|
public double Width
|
|
|
|
|
{
|
|
|
|
|
get => _width;
|
|
|
|
|
set => SetProperty(ref _width, value < 0 || double.IsNaN(value) ? 0 : value);
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
[JsonPropertyName("height")]
|
|
|
|
|
public double Height
|
|
|
|
|
{
|
|
|
|
|
get => _height;
|
|
|
|
|
set => SetProperty(ref _height, value < 0 || double.IsNaN(value) ? 0 : value);
|
|
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
[JsonPropertyName("unit")]
|
|
|
|
|
public ResizeUnit Unit
|
|
|
|
|
{
|
|
|
|
|
get => _unit;
|
|
|
|
|
set
|
2020-04-26 17:34:03 -07:00
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
if (SetProperty(ref _unit, value))
|
2020-04-26 17:34:03 -07:00
|
|
|
{
|
2025-01-21 11:55:02 +00:00
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsHeightUsed)));
|
2020-04-26 17:34:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-21 11:55:02 +00:00
|
|
|
}
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets access to all properties for formatting accessibility descriptions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public ImageSize AccessibleTextHelper => this;
|
2020-04-26 17:34:03 -07:00
|
|
|
|
2025-01-21 11:55:02 +00:00
|
|
|
public string ToJsonString() => JsonSerializer.Serialize(this);
|
2020-04-26 17:34:03 -07:00
|
|
|
}
|