From d329406eb8236c14c863e6882812303f9ed1d456 Mon Sep 17 00:00:00 2001 From: ryanbodrug-microsoft <56318517+ryanbodrug-microsoft@users.noreply.github.com> Date: Fri, 8 May 2020 16:12:37 -0700 Subject: [PATCH] Adding Telemetry Events for svg and markdown enable/disable (#2814) * Raising Telemetry events when svg and markdown preview pane is turned off * Properly serializing Bool property. This allows us to be backwards compatible with .17 settings but interact with the properties as boolean elements, and fire events on property changed notification --- .../BoolPropertyJsonConverter.cs | 23 ++++++++ .../EnabledModules.cs | 4 +- .../PowerPreviewProperties.cs | 52 ++++++++++++++++--- ...ModuleEvent.cs => SettingsEnabledEvent.cs} | 4 +- .../ViewModels/PowerPreviewViewModel.cs | 8 +-- .../ViewModelTests/PowerPreview.cs | 4 +- 6 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolPropertyJsonConverter.cs rename src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/{SettingsEnabledModuleEvent.cs => SettingsEnabledEvent.cs} (60%) diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolPropertyJsonConverter.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolPropertyJsonConverter.cs new file mode 100644 index 0000000000..11211e65cc --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/BoolPropertyJsonConverter.cs @@ -0,0 +1,23 @@ +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 BoolPropertyJsonConverter : JsonConverter + { + public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var boolProperty = JsonSerializer.Deserialize(ref reader, options); + return boolProperty.Value; + } + + public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) + { + var boolProperty = new BoolProperty(value); + JsonSerializer.Serialize(writer, boolProperty, options); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs index e256e69286..13a36217e8 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/EnabledModules.cs @@ -133,10 +133,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib private void LogTelemetryEvent(bool value, [CallerMemberName] string moduleName = null ) { - var dataEvent = new SettingsEnabledModuleEvent() + var dataEvent = new SettingsEnabledEvent() { Value = value, - ModuleName = moduleName, + Name = moduleName, }; PowerToysTelemetry.Log.WriteEvent(dataEvent); } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs index ff9438075c..878ee5cc76 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs @@ -2,28 +2,68 @@ // 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.Runtime.CompilerServices; using System.Text.Json; using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.Telemetry; +using Microsoft.PowerToys.Telemetry; namespace Microsoft.PowerToys.Settings.UI.Lib { public class PowerPreviewProperties - { - [JsonPropertyName("svg-previewer-toggle-setting")] - public BoolProperty EnableSvg { get; set; } + { + private bool enableSvg = true; + + [JsonPropertyName("svg-previewer-toggle-setting")] + [JsonConverter(typeof(BoolPropertyJsonConverter))] + public bool EnableSvg + { + get => this.enableSvg; + set + { + if (value != this.enableSvg) + { + LogTelemetryEvent(value); + this.enableSvg = value; + } + } + } + + private bool enableMd = true; [JsonPropertyName("md-previewer-toggle-setting")] - public BoolProperty EnableMd { get; set; } + [JsonConverter(typeof(BoolPropertyJsonConverter))] + public bool EnableMd + { + get => this.enableMd; + set + { + if (value != this.enableMd) + { + LogTelemetryEvent(value); + this.enableMd = value; + } + } + } public PowerPreviewProperties() { - EnableSvg = new BoolProperty(); - EnableMd = new BoolProperty(); + } public override string ToString() { return JsonSerializer.Serialize(this); } + + private void LogTelemetryEvent(bool value, [CallerMemberName] string propertyName = null) + { + var dataEvent = new SettingsEnabledEvent() + { + Value = value, + Name = propertyName, + }; + PowerToysTelemetry.Log.WriteEvent(dataEvent); + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/SettingsEnabledModuleEvent.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/SettingsEnabledEvent.cs similarity index 60% rename from src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/SettingsEnabledModuleEvent.cs rename to src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/SettingsEnabledEvent.cs index d96e7fc75a..59f3158ae7 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/SettingsEnabledModuleEvent.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Telemetry/Events/SettingsEnabledEvent.cs @@ -3,9 +3,9 @@ namespace Microsoft.PowerToys.Settings.Telemetry { [EventData] - public class SettingsEnabledModuleEvent + public class SettingsEnabledEvent { - public string ModuleName { get; set; } + public string Name { get; set; } public bool Value { get; set; } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs index fbd6fb5133..c3f47398d8 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs @@ -27,8 +27,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); } - this._svgRenderIsEnabled = Settings.properties.EnableSvg.Value; - this._mdRenderIsEnabled = Settings.properties.EnableMd.Value; + this._svgRenderIsEnabled = Settings.properties.EnableSvg; + this._mdRenderIsEnabled = Settings.properties.EnableMd; } private bool _svgRenderIsEnabled = false; @@ -46,7 +46,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels if (value != _svgRenderIsEnabled) { _svgRenderIsEnabled = value; - Settings.properties.EnableSvg.Value = value; + Settings.properties.EnableSvg = value; RaisePropertyChanged(); } } @@ -64,7 +64,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels if (value != _mdRenderIsEnabled) { _mdRenderIsEnabled = value; - Settings.properties.EnableMd.Value = value; + Settings.properties.EnableMd = value; RaisePropertyChanged(); } } diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs index bd1d8e0854..b95f1b3c4c 100644 --- a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs +++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs @@ -59,7 +59,7 @@ namespace ViewModelTests ShellPage.DefaultSndMSGCallback = msg => { SndModuleSettings snd = JsonSerializer.Deserialize>(msg); - Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.properties.EnableSvg.Value); + Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.properties.EnableSvg); }; // act @@ -76,7 +76,7 @@ namespace ViewModelTests ShellPage.DefaultSndMSGCallback = msg => { SndModuleSettings snd = JsonSerializer.Deserialize>(msg); - Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.properties.EnableMd.Value); + Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.properties.EnableMd); }; // act