From 478777a23e19ebdad8c629c9d6ec5517c4bde111 Mon Sep 17 00:00:00 2001 From: Lavius Motileng <58791731+laviusmotileng-ms@users.noreply.github.com> Date: Tue, 21 Apr 2020 15:53:52 -0700 Subject: [PATCH 01/10] added image resizer settings --- .../BoolProperty.cs | 5 + .../DoubleProperty.cs | 5 + .../EnabledModules.cs | 2 +- .../ImageResizerProperties.cs | 94 ++++++++ .../ImageResizerSettings.cs | 36 +++ .../ImageSize.cs | 57 +++++ .../ImageresizerCustomSizeProperty.cs | 24 ++ .../ImageresizerFallbackEncoder.cs | 19 ++ .../ImageresizerKeepDateModified.cs | 19 ++ .../ImageresizerSizes.cs | 29 +++ .../IntProperty.cs | 5 + .../SndImageResizerSettings.cs | 29 +++ .../SndShortcutGuideSettings.cs | 1 - .../StringProperty.cs | 5 + .../MainWindow.xaml.cs | 1 + .../ViewModels/ImageResizerViewModel.cs | 123 ++++++++++ .../Views/ImageResizerPage.xaml | 215 +++++++++++------- .../Views/ImageResizerPage.xaml.cs | 4 +- 18 files changed, 593 insertions(+), 80 deletions(-) create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerProperties.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerCustomSizeProperty.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerFallbackEncoder.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerKeepDateModified.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerSizes.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/SndImageResizerSettings.cs diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolProperty.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolProperty.cs index ca630919a065..c3b2611f8c87 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolProperty.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolProperty.cs @@ -17,6 +17,11 @@ public BoolProperty() this.Value = false; } + public BoolProperty(bool value) + { + Value = value; + } + [JsonPropertyName("value")] public bool Value { get; set; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/DoubleProperty.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/DoubleProperty.cs index 662d18a185fe..c953ffae67fd 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/DoubleProperty.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/DoubleProperty.cs @@ -16,6 +16,11 @@ public DoubleProperty() this.Value = 0.0; } + public DoubleProperty(double value) + { + Value = value; + } + // Gets or sets the double value of the settings configuration. [JsonPropertyName("value")] public double Value { get; set; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs index 8ade31aac7c5..322ec74de720 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs @@ -21,7 +21,7 @@ public EnabledModules() [JsonPropertyName("FancyZones")] public bool FancyZones { get; set; } - [JsonPropertyName("ImageResizer")] + [JsonPropertyName("Image Resizer")] public bool ImageResizer { get; set; } [JsonPropertyName("File Explorer Preview")] diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerProperties.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerProperties.cs new file mode 100644 index 000000000000..cb9d834625e5 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerProperties.cs @@ -0,0 +1,94 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + public class ImageResizerProperties + { + public ImageResizerProperties() + { + ImageresizerSelectedSizeIndex = new IntProperty(0); + ImageresizerShrinkOnly = new BoolProperty(false); + ImageresizerReplace = new BoolProperty(false); + ImageresizerIgnoreOrientation = new BoolProperty(true); + ImageresizerJpegQualityLevel = new IntProperty(90); + ImageresizerPngInterlaceOption = new IntProperty(); + ImageresizerTiffCompressOption = new IntProperty(); + ImageresizerFileName = new StringProperty("%1 (%2)"); + + ImageresizerSizes = new ImageresizerSizes(new ObservableCollection() + { + new ImageSize(0, "small", ResizeFit.Fit, 854, 480, ResizeUnit.Pixel), + new ImageSize(1, "medium", ResizeFit.Fit, 1366, 768, ResizeUnit.Pixel), + new ImageSize(2, "large", ResizeFit.Fit, 1920, 1080, ResizeUnit.Pixel), + new ImageSize(3, "phone", ResizeFit.Fit, 320, 568, ResizeUnit.Pixel), + }); + + ImageresizerKeepDateModified = new BoolProperty(); + ImageresizerFallbackEncoder = new StringProperty(new System.Guid("19e4a5aa-5662-4fc5-a0c0-1758028e1057").ToString()); + ImageresizerCustomSize = new ImageresizerCustomSizeProperty(new ImageSize(4, "custom", ResizeFit.Fit, 1024, 640, ResizeUnit.Pixel)); + } + + [JsonPropertyName("imageresizer_selectedSizeIndex")] + public IntProperty ImageresizerSelectedSizeIndex { get; set; } + + [JsonPropertyName("imageresizer_shrinkOnly")] + public BoolProperty ImageresizerShrinkOnly { get; set; } + + [JsonPropertyName("imageresizer_replace")] + public BoolProperty ImageresizerReplace { get; set; } + + [JsonPropertyName("imageresizer_ignoreOrientation")] + public BoolProperty ImageresizerIgnoreOrientation { get; set; } + + [JsonPropertyName("imageresizer_jpegQualityLevel")] + public IntProperty ImageresizerJpegQualityLevel { get; set; } + + [JsonPropertyName("imageresizer_pngInterlaceOption")] + public IntProperty ImageresizerPngInterlaceOption { get; set; } + + [JsonPropertyName("imageresizer_tiffCompressOption")] + public IntProperty ImageresizerTiffCompressOption { get; set; } + + [JsonPropertyName("imageresizer_fileName")] + public StringProperty ImageresizerFileName { get; set; } + + [JsonPropertyName("imageresizer_sizes")] + public ImageresizerSizes ImageresizerSizes { get; set; } + + [JsonPropertyName("imageresizer_keepDateModified")] + public BoolProperty ImageresizerKeepDateModified { get; set; } + + [JsonPropertyName("imageresizer_fallbackEncoder")] + public StringProperty ImageresizerFallbackEncoder { get; set; } + + [JsonPropertyName("imageresizer_customSize")] + public ImageresizerCustomSizeProperty ImageresizerCustomSize { get; set; } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } + } + + public enum ResizeFit + { + Fill = 0, + Fit = 1, + Stretch = 2, + } + + public enum ResizeUnit + { + Centimeter = 0, + Inch = 1, + Percent = 2, + Pixel = 3, + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs new file mode 100644 index 000000000000..f78fdded5d6b --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + public class ImageResizerSettings + { + [JsonPropertyName("version")] + public string Version { get; set; } + + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("properties")] + public ImageResizerProperties Properties { get; set; } + + public ImageResizerSettings() + { + this.Version = "1"; + this.Name = "Image Resizer"; + this.Properties = new ImageResizerProperties(); + } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs new file mode 100644 index 000000000000..7ac99179e2bc --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + public class ImageSize + { + public ImageSize() + { + Id = 0; + Name = string.Empty; + Fit = (int)ResizeFit.Fit; + Width = 0; + Height = 0; + Unit = (int)ResizeUnit.Pixel; + } + + public ImageSize(int id, string name, ResizeFit fit, double width, double height, ResizeUnit unit) + { + Id = id; + Name = name; + Fit = (int)fit; + Width = width; + Height = height; + Unit = (int)unit; + } + + public int Id { get; set; } + + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("fit")] + public int Fit { get; set; } + + [JsonPropertyName("width")] + public double Width { get; set; } + + [JsonPropertyName("height")] + public double Height { get; set; } + + [JsonPropertyName("unit")] + public int Unit { get; set; } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerCustomSizeProperty.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerCustomSizeProperty.cs new file mode 100644 index 000000000000..dab6f3ad4b01 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerCustomSizeProperty.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + + public class ImageresizerCustomSizeProperty + { + [JsonPropertyName("value")] + public ImageSize Value { get; set; } + + public ImageresizerCustomSizeProperty() + { + this.Value = new ImageSize(); + } + + public ImageresizerCustomSizeProperty(ImageSize value) + { + Value = value; + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerFallbackEncoder.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerFallbackEncoder.cs new file mode 100644 index 000000000000..5c8b785d2206 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerFallbackEncoder.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + + public class ImageresizerFallbackEncoder + { + [JsonPropertyName("value")] + public string Value { get; set; } + + public ImageresizerFallbackEncoder() + { + this.Value = string.Empty; + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerKeepDateModified.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerKeepDateModified.cs new file mode 100644 index 000000000000..b807eff0171c --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerKeepDateModified.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + + public class ImageresizerKeepDateModified + { + [JsonPropertyName("value")] + public bool Value { get; set; } + + public ImageresizerKeepDateModified() + { + this.Value = false; + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerSizes.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerSizes.cs new file mode 100644 index 000000000000..adc2d8bf6f4a --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageresizerSizes.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + + public class ImageresizerSizes + { + [JsonPropertyName("value")] + public ObservableCollection Value { get; set; } + + public ImageresizerSizes() + { + this.Value = new ObservableCollection(); + } + + public ImageresizerSizes(ObservableCollection value) + { + Value = value; + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/IntProperty.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/IntProperty.cs index 911503016611..8706806da238 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/IntProperty.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/IntProperty.cs @@ -15,6 +15,11 @@ public IntProperty() this.Value = 0; } + public IntProperty(int value) + { + Value = value; + } + // Gets or sets the integer value of the settings configuration. [JsonPropertyName("value")] public int Value { get; set; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndImageResizerSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndImageResizerSettings.cs new file mode 100644 index 000000000000..c49011d8ed0e --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndImageResizerSettings.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + + public class SndImageResizerSettings + { + [JsonPropertyName("Image Resizer")] + public ImageResizerSettings ImageResizer { get; set; } + + public SndImageResizerSettings(ImageResizerSettings settings) + { + this.ImageResizer = settings; + } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndShortcutGuideSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndShortcutGuideSettings.cs index 2b75e2aef2c4..9fd8fa359735 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndShortcutGuideSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndShortcutGuideSettings.cs @@ -6,7 +6,6 @@ namespace Microsoft.PowerToys.Settings.UI.Lib { - public class SndShortcutGuideSettings { [JsonPropertyName("Shortcut Guide")] diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/StringProperty.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/StringProperty.cs index fac463d70bfd..372e450aa964 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/StringProperty.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/StringProperty.cs @@ -18,6 +18,11 @@ public StringProperty() this.Value = string.Empty; } + public StringProperty(string value) + { + Value = value; + } + // Gets or sets the integer value of the settings configuration. [JsonPropertyName("value")] public string Value { get; set; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs index d079b5d3d5d8..afbd9138e62e 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs @@ -6,6 +6,7 @@ using System.Windows; using Microsoft.PowerToys.Settings.UI.Views; using Microsoft.Toolkit.Wpf.UI.XamlHost; +using Windows.UI.Popups; namespace Microsoft.PowerToys.Settings.UI.Runner { diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ImageResizerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ImageResizerViewModel.cs index 623c348c7137..aced29b9d568 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ImageResizerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ImageResizerViewModel.cs @@ -2,14 +2,137 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Windows.Input; using Microsoft.PowerToys.Settings.UI.Helpers; +using Microsoft.PowerToys.Settings.UI.Lib; +using Microsoft.PowerToys.Settings.UI.Views; +using Windows.UI.Popups; namespace Microsoft.PowerToys.Settings.UI.ViewModels { public class ImageResizerViewModel : Observable { + private ImageResizerSettings Settings { get; set; } + + private const string ModuleName = "ImageResizer"; + public ImageResizerViewModel() { + try + { + Settings = SettingsUtils.GetSettings(ModuleName); + } + catch + { + Settings = new ImageResizerSettings(); + SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); + } + + GeneralSettings generalSettings; + + try + { + generalSettings = SettingsUtils.GetSettings(string.Empty); + } + catch + { + generalSettings = new GeneralSettings(); + SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty); + } + + this._isEnabled = generalSettings.Enabled.ImageResizer; + this._advancedSizes = Settings.Properties.ImageresizerSizes.Value; + + Sizes.CollectionChanged += OnSizesCollectionChanged; + } + + private bool _isEnabled = false; + private ObservableCollection _advancedSizes = new ObservableCollection(); + + public bool IsEnabled + { + get + { + return _isEnabled; + } + + set + { + if (value != _isEnabled) + { + _isEnabled = value; + GeneralSettings generalSettings = SettingsUtils.GetSettings(string.Empty); + generalSettings.Enabled.ImageResizer = value; + OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings); + ShellPage.DefaultSndMSGCallback(snd.ToString()); + OnPropertyChanged("IsEnabled"); + } + } + } + + public ObservableCollection Sizes + { + get + { + return _advancedSizes; + } + + set + { + _advancedSizes = value; + Settings.Properties.ImageresizerSizes.Value = value; + } + } + + public ICommand DeleteImageSizeEventHandler + { + get + { + return new RelayCommand(DeleteImageSize); + } + } + + public ICommand AddImageSizeEventHandler + { + get + { + return new RelayCommand(AddRow); + } + } + + public void AddRow() + { + Sizes.Add(new ImageSize()); + } + + public void DeleteImageSize(int id) + { + try + { + ImageSize size = Sizes.Where(x => x.Id == id).First(); + Sizes.Remove(size); + } + catch + { + } + } + + public void OnSizesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + RaisePropertyChanged("Sizes"); + } + + public void RaisePropertyChanged([CallerMemberName] string propertyName = null) + { + OnPropertyChanged(propertyName); + SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml index c6a7fb977661..8f18c55656a1 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml @@ -2,13 +2,18 @@ x:Class="Microsoft.PowerToys.Settings.UI.Views.ImageResizerPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:Microsoft.PowerToys.Settings.UI.Views" + xmlns:viewModel="using:Microsoft.PowerToys.Settings.UI.ViewModels" + xmlns:models="using:Microsoft.PowerToys.Settings.UI.Lib" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> + + + + @@ -40,138 +45,187 @@ - + + + - + - - + + + + -