From 67d0771b28aaeb221313502ebe7500ac1bbe6967 Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Mahunt Date: Mon, 20 Apr 2020 18:16:16 +0530 Subject: [PATCH 1/6] Enabled settings syncing accross all windowss --- .../InteropConnectionService.cs | 51 +++++++ .../Notepads.Services.csproj | 143 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 29 ++++ src/Notepads.sln | 38 +++++ src/Notepads/App.xaml.cs | 1 - src/Notepads/Notepads.csproj | 15 +- src/Notepads/Package.appxmanifest | 6 +- .../Services/EditorSettingsService.cs | 34 ++--- src/Notepads/Services/InteropService.cs | 100 ++++++++++++ src/Notepads/Services/ThemeSettingsService.cs | 70 +++------ src/Notepads/Settings/SettingsDelegate.cs | 138 +++++++++++++++++ src/Notepads/Settings/SettingsKey.cs | 1 - src/Notepads/Strings/en-US/Resources.resw | 2 +- .../Views/AdvancedSettingsPage.xaml.cs | 3 + .../Views/NotepadsMainPage.MainMenu.cs | 3 +- src/Notepads/Views/NotepadsMainPage.xaml.cs | 20 +++ .../Views/PersonalizationSettingsPage.xaml.cs | 43 +++--- .../Views/TextAndEditorSettingsPage.xaml.cs | 20 +++ 18 files changed, 627 insertions(+), 90 deletions(-) create mode 100644 src/Notepads.Services/InteropConnectionService.cs create mode 100644 src/Notepads.Services/Notepads.Services.csproj create mode 100644 src/Notepads.Services/Properties/AssemblyInfo.cs create mode 100644 src/Notepads/Services/InteropService.cs create mode 100644 src/Notepads/Settings/SettingsDelegate.cs diff --git a/src/Notepads.Services/InteropConnectionService.cs b/src/Notepads.Services/InteropConnectionService.cs new file mode 100644 index 000000000..2b09acd07 --- /dev/null +++ b/src/Notepads.Services/InteropConnectionService.cs @@ -0,0 +1,51 @@ +namespace Notepads.Services +{ + using System; + using System.Collections.Generic; + using Windows.ApplicationModel; + using Windows.ApplicationModel.AppService; + using Windows.ApplicationModel.Background; + using Windows.Foundation.Collections; + + public sealed class InteropConnectionService : IBackgroundTask + { + private BackgroundTaskDeferral backgroundTaskDeferral; + private AppServiceConnection appServiceConnection; + private static IList appServiceConnections = new List(); + + public void Run(IBackgroundTaskInstance taskInstance) + { + // Get a deferral so that the service isn't terminated. + this.backgroundTaskDeferral = taskInstance.GetDeferral(); + + // Associate a cancellation handler with the background task. + taskInstance.Canceled += OnTaskCanceled; + + // Retrieve the app service connection and set up a listener for incoming app service requests. + var details = taskInstance.TriggerDetails as AppServiceTriggerDetails; + appServiceConnection = details.AppServiceConnection; + appServiceConnections.Add(appServiceConnection); + appServiceConnection.RequestReceived += OnRequestReceived; + } + + private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) + { + foreach(var serviceConnection in appServiceConnections) + { + if (serviceConnection != appServiceConnection) + await serviceConnection.SendMessageAsync(args.Request.Message); + } + } + + private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) + { + if (this.backgroundTaskDeferral != null) + { + // Complete the service deferral. + this.backgroundTaskDeferral.Complete(); + var details = sender.TriggerDetails as AppServiceTriggerDetails; + appServiceConnections.Remove(details.AppServiceConnection); + } + } + } +} diff --git a/src/Notepads.Services/Notepads.Services.csproj b/src/Notepads.Services/Notepads.Services.csproj new file mode 100644 index 000000000..6e17a1787 --- /dev/null +++ b/src/Notepads.Services/Notepads.Services.csproj @@ -0,0 +1,143 @@ + + + + + Debug + AnyCPU + {29DCAA68-1B6B-4290-8C05-691F52645D55} + winmdobj + Properties + Notepads.Services + Notepads.Services + en-US + UAP + 10.0.18362.0 + 10.0.17763.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + false + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + x86 + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + x86 + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + ARM + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + ARM + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + ARM64 + true + bin\ARM64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + ARM64 + bin\ARM64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + x64 + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + x64 + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + PackageReference + + + + + + + + 6.2.9 + + + + 14.0 + + + + \ No newline at end of file diff --git a/src/Notepads.Services/Properties/AssemblyInfo.cs b/src/Notepads.Services/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..7b0f0f6c3 --- /dev/null +++ b/src/Notepads.Services/Properties/AssemblyInfo.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Notepads.Services")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Notepads.Services")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: ComVisible(false)] \ No newline at end of file diff --git a/src/Notepads.sln b/src/Notepads.sln index ad5a84593..cf7f8698f 100644 --- a/src/Notepads.sln +++ b/src/Notepads.sln @@ -15,16 +15,24 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .editorconfig = .editorconfig EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notepads.Services", "Notepads.Services\Notepads.Services.csproj", "{29DCAA68-1B6B-4290-8C05-691F52645D55}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|ARM = Debug|ARM Debug|ARM64 = Debug|ARM64 Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|ARM = Release|ARM Release|ARM64 = Release|ARM64 Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {99274932-9E86-480C-8142-38525F80007D}.Debug|Any CPU.ActiveCfg = Debug|x86 + {99274932-9E86-480C-8142-38525F80007D}.Debug|ARM.ActiveCfg = Debug|x86 {99274932-9E86-480C-8142-38525F80007D}.Debug|ARM64.ActiveCfg = Debug|ARM64 {99274932-9E86-480C-8142-38525F80007D}.Debug|ARM64.Build.0 = Debug|ARM64 {99274932-9E86-480C-8142-38525F80007D}.Debug|ARM64.Deploy.0 = Debug|ARM64 @@ -34,6 +42,8 @@ Global {99274932-9E86-480C-8142-38525F80007D}.Debug|x86.ActiveCfg = Debug|x86 {99274932-9E86-480C-8142-38525F80007D}.Debug|x86.Build.0 = Debug|x86 {99274932-9E86-480C-8142-38525F80007D}.Debug|x86.Deploy.0 = Debug|x86 + {99274932-9E86-480C-8142-38525F80007D}.Release|Any CPU.ActiveCfg = Release|x86 + {99274932-9E86-480C-8142-38525F80007D}.Release|ARM.ActiveCfg = Release|x86 {99274932-9E86-480C-8142-38525F80007D}.Release|ARM64.ActiveCfg = Release|ARM64 {99274932-9E86-480C-8142-38525F80007D}.Release|ARM64.Build.0 = Release|ARM64 {99274932-9E86-480C-8142-38525F80007D}.Release|ARM64.Deploy.0 = Release|ARM64 @@ -43,18 +53,46 @@ Global {99274932-9E86-480C-8142-38525F80007D}.Release|x86.ActiveCfg = Release|x86 {99274932-9E86-480C-8142-38525F80007D}.Release|x86.Build.0 = Release|x86 {99274932-9E86-480C-8142-38525F80007D}.Release|x86.Deploy.0 = Release|x86 + {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|ARM.ActiveCfg = Debug|ARM + {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|ARM.Build.0 = Debug|ARM {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|ARM64.ActiveCfg = Debug|ARM64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|ARM64.Build.0 = Debug|ARM64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|x64.ActiveCfg = Debug|x64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|x64.Build.0 = Debug|x64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|x86.ActiveCfg = Debug|x86 {7AA5E631-B663-420E-A08F-002CD81DF855}.Debug|x86.Build.0 = Debug|x86 + {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|Any CPU.Build.0 = Release|Any CPU + {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|ARM.ActiveCfg = Release|ARM + {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|ARM.Build.0 = Release|ARM {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|ARM64.ActiveCfg = Release|ARM64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|ARM64.Build.0 = Release|ARM64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|x64.ActiveCfg = Release|x64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|x64.Build.0 = Release|x64 {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|x86.ActiveCfg = Release|x86 {7AA5E631-B663-420E-A08F-002CD81DF855}.Release|x86.Build.0 = Release|x86 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|Any CPU.Build.0 = Debug|Any CPU + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|ARM.ActiveCfg = Debug|ARM + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|ARM.Build.0 = Debug|ARM + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|ARM64.Build.0 = Debug|ARM64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|x64.ActiveCfg = Debug|x64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|x64.Build.0 = Debug|x64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|x86.ActiveCfg = Debug|x86 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Debug|x86.Build.0 = Debug|x86 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|Any CPU.ActiveCfg = Release|Any CPU + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|Any CPU.Build.0 = Release|Any CPU + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|ARM.ActiveCfg = Release|ARM + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|ARM.Build.0 = Release|ARM + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|ARM64.ActiveCfg = Release|ARM64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|ARM64.Build.0 = Release|ARM64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|x64.ActiveCfg = Release|x64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|x64.Build.0 = Release|x64 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|x86.ActiveCfg = Release|x86 + {29DCAA68-1B6B-4290-8C05-691F52645D55}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Notepads/App.xaml.cs b/src/Notepads/App.xaml.cs index 7db90ff14..980f080f6 100644 --- a/src/Notepads/App.xaml.cs +++ b/src/Notepads/App.xaml.cs @@ -92,7 +92,6 @@ private async Task ActivateAsync(IActivatedEventArgs e) { { "OSArchitecture", SystemInformation.OperatingSystemArchitecture.ToString() }, { "OSVersion", SystemInformation.OperatingSystemVersion.ToString() }, - { "UseWindowsTheme", ThemeSettingsService.UseWindowsTheme.ToString() }, { "ThemeMode", ThemeSettingsService.ThemeMode.ToString() }, { "UseWindowsAccentColor", ThemeSettingsService.UseWindowsAccentColor.ToString() }, { "AppBackgroundTintOpacity", $"{(int) (ThemeSettingsService.AppBackgroundPanelTintOpacity * 100.0)}" }, diff --git a/src/Notepads/Notepads.csproj b/src/Notepads/Notepads.csproj index aaeaf7495..7b3a9c8ed 100644 --- a/src/Notepads/Notepads.csproj +++ b/src/Notepads/Notepads.csproj @@ -124,6 +124,8 @@ PrintPageFormat.xaml + + AboutPage.xaml @@ -612,9 +614,15 @@ {7aa5e631-b663-420e-a08f-002cd81df855} Notepads.Controls + + {29dcaa68-1b6b-4290-8c05-691f52645d55} + Notepads.Services + - + + Designer + @@ -739,6 +747,11 @@ + + + Windows Desktop Extensions for the UWP + + 14.0 diff --git a/src/Notepads/Package.appxmanifest b/src/Notepads/Package.appxmanifest index 40063562f..dc09e0ffe 100644 --- a/src/Notepads/Package.appxmanifest +++ b/src/Notepads/Package.appxmanifest @@ -4,11 +4,12 @@ xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" + xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" - IgnorableNamespaces="uap mp uap5 desktop4 iot2 rescap"> + IgnorableNamespaces="mp uap uap3 uap5 desktop4 iot2 rescap"> + + + diff --git a/src/Notepads/Services/EditorSettingsService.cs b/src/Notepads/Services/EditorSettingsService.cs index 2a50bfc4b..7e66f0114 100644 --- a/src/Notepads/Services/EditorSettingsService.cs +++ b/src/Notepads/Services/EditorSettingsService.cs @@ -28,7 +28,7 @@ public static string EditorFontFamily { _editorFontFamily = value; OnFontFamilyChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorFontFamilyStr, value); + if(InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorFontFamilyStr, value); } } @@ -41,7 +41,7 @@ public static int EditorFontSize { _editorFontSize = value; OnFontSizeChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorFontSizeInt, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorFontSizeInt, value); } } @@ -54,7 +54,7 @@ public static TextWrapping EditorDefaultTextWrapping { _editorDefaultTextWrapping = value; OnDefaultTextWrappingChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultTextWrappingStr, value.ToString()); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultTextWrappingStr, value.ToString()); } } @@ -67,7 +67,7 @@ public static bool IsLineHighlighterEnabled { _isLineHighlighterEnabled = value; OnDefaultLineHighlighterViewStateChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultLineHighlighterViewStateBool, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultLineHighlighterViewStateBool, value); } } @@ -80,7 +80,7 @@ public static LineEnding EditorDefaultLineEnding { _editorDefaultLineEnding = value; OnDefaultLineEndingChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultLineEndingStr, value.ToString()); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultLineEndingStr, value.ToString()); } } @@ -95,12 +95,12 @@ public static Encoding EditorDefaultEncoding if (value is UTF8Encoding) { - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultUtf8EncoderShouldEmitByteOrderMarkBool, + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultUtf8EncoderShouldEmitByteOrderMarkBool, Equals(value, new UTF8Encoding(true))); } OnDefaultEncodingChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultEncodingCodePageInt, value.CodePage); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultEncodingCodePageInt, value.CodePage); } } @@ -137,7 +137,7 @@ public static Encoding EditorDefaultDecoding { _editorDefaultDecoding = value; var codePage = value?.CodePage ?? -1; - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultDecodingCodePageInt, codePage); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultDecodingCodePageInt, codePage); } } @@ -150,7 +150,7 @@ public static int EditorDefaultTabIndents { _editorDefaultTabIndents = value; OnDefaultTabIndentsChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultTabIndentsInt, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultTabIndentsInt, value); } } @@ -162,7 +162,7 @@ public static SearchEngine EditorDefaultSearchEngine set { _editorDefaultSearchEngine = value; - ApplicationSettingsStore.Write(SettingsKey.EditorDefaultSearchEngineStr, value.ToString()); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorDefaultSearchEngineStr, value.ToString()); } } @@ -174,7 +174,7 @@ public static string EditorCustomMadeSearchUrl set { _editorCustomMadeSearchUrl = value; - ApplicationSettingsStore.Write(SettingsKey.EditorCustomMadeSearchUrlStr, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorCustomMadeSearchUrlStr, value); } } @@ -187,7 +187,7 @@ public static bool ShowStatusBar { _showStatusBar = value; OnStatusBarVisibilityChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorShowStatusBarBool, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorShowStatusBarBool, value); } } @@ -200,7 +200,7 @@ public static bool IsSessionSnapshotEnabled { _isSessionSnapshotEnabled = value; OnSessionBackupAndRestoreOptionChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorEnableSessionBackupAndRestoreBool, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorEnableSessionBackupAndRestoreBool, value); } } @@ -213,7 +213,7 @@ public static bool IsHighlightMisspelledWordsEnabled { _isHighlightMisspelledWordsEnabled = value; OnHighlightMisspelledWordsChanged?.Invoke(null, value); - ApplicationSettingsStore.Write(SettingsKey.EditorHighlightMisspelledWordsBool, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.EditorHighlightMisspelledWordsBool, value); } } @@ -225,7 +225,7 @@ public static bool AlwaysOpenNewWindow set { _alwaysOpenNewWindow = value; - ApplicationSettingsStore.Write(SettingsKey.AlwaysOpenNewWindowBool, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.AlwaysOpenNewWindowBool, value); } } @@ -342,7 +342,7 @@ private static void InitializeLineHighlighterSettings() } } - private static void InitializeEncodingSettings() + public static void InitializeEncodingSettings() { Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); @@ -370,7 +370,7 @@ private static void InitializeEncodingSettings() } } - private static void InitializeDecodingSettings() + public static void InitializeDecodingSettings() { if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultDecodingCodePageInt) is int decodingCodePage) { diff --git a/src/Notepads/Services/InteropService.cs b/src/Notepads/Services/InteropService.cs new file mode 100644 index 000000000..f3a122bea --- /dev/null +++ b/src/Notepads/Services/InteropService.cs @@ -0,0 +1,100 @@ +namespace Notepads.Services +{ + using Notepads.Settings; + using System; + using System.Collections.Generic; + using Windows.ApplicationModel; + using Windows.ApplicationModel.AppService; + using Windows.Foundation.Collections; + using Windows.UI.Xaml; + + public static class InteropService + { + public static EventHandler HideSettingsPane; + public static bool EnableSettingsLogging = true; + public static bool ISAppExited = false; + + public static AppServiceConnection InteropServiceConnection = null; + + private static readonly string _appIdLabel = "Instance"; + private static readonly string _settingsKeyLabel = "Settings"; + private static readonly string _valueLabel = "Value"; + + private static IReadOnlyDictionary SettingsManager = new Dictionary + { + {SettingsKey.AlwaysOpenNewWindowBool, SettingsDelegate.AlwaysOpenNewWindow}, + {SettingsKey.AppAccentColorHexStr, SettingsDelegate.AppAccentColor}, + {SettingsKey.AppBackgroundTintOpacityDouble, SettingsDelegate.AppBackgroundPanelTintOpacity}, + {SettingsKey.CustomAccentColorHexStr, SettingsDelegate.CustomAccentColor}, + {SettingsKey.EditorCustomMadeSearchUrlStr, SettingsDelegate.EditorCustomMadeSearchUrl}, + {SettingsKey.EditorDefaultDecodingCodePageInt, SettingsDelegate.EditorDefaultDecoding}, + {SettingsKey.EditorDefaultEncodingCodePageInt, SettingsDelegate.EditorDefaultEncoding}, + {SettingsKey.EditorDefaultLineEndingStr, SettingsDelegate.EditorDefaultLineEnding}, + {SettingsKey.EditorDefaultLineHighlighterViewStateBool, SettingsDelegate.IsLineHighlighterEnabled}, + {SettingsKey.EditorDefaultSearchEngineStr, SettingsDelegate.EditorDefaultSearchEngine}, + {SettingsKey.EditorDefaultTabIndentsInt, SettingsDelegate.EditorDefaultTabIndents}, + {SettingsKey.EditorDefaultTextWrappingStr, SettingsDelegate.EditorDefaultTextWrapping}, + {SettingsKey.EditorFontFamilyStr, SettingsDelegate.EditorFontFamily}, + {SettingsKey.EditorFontSizeInt, SettingsDelegate.EditorFontSize}, + {SettingsKey.EditorHighlightMisspelledWordsBool, SettingsDelegate.IsHighlightMisspelledWordsEnabled}, + {SettingsKey.EditorShowStatusBarBool, SettingsDelegate.ShowStatusBar}, + {SettingsKey.UseWindowsAccentColorBool, SettingsDelegate.UseWindowsAccentColor}, + {SettingsKey.RequestedThemeStr, SettingsDelegate.ThemeMode} + }; + + public static async void Initialize() + { + InteropServiceConnection = new AppServiceConnection() + { + AppServiceName = "InteropServiceConnection", + PackageFamilyName = Package.Current.Id.FamilyName + }; + + InteropServiceConnection.RequestReceived += InteropServiceConnection_RequestReceived; + InteropServiceConnection.ServiceClosed += InteropServiceConnection_ServiceClosed; + + AppServiceConnectionStatus status = await InteropServiceConnection.OpenAsync(); + if (status != AppServiceConnectionStatus.Success) Application.Current.Exit(); + } + + private static void InteropServiceConnection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) + { + var message = args.Request.Message; + if (message.ContainsKey(_appIdLabel) && message[_appIdLabel] is Guid appID && appID != App.Id) + { + EnableSettingsLogging = false; + HideSettingsPane?.Invoke(null, true); + var settingsKey = message[_settingsKeyLabel] as string; + var value = message[_valueLabel] as object; + SettingsManager[settingsKey](value); + EnableSettingsLogging = true; + } + } + + private static void InteropServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args) + { + return; + } + + public static async void SyncSettings(string settingsKey, object value) + { + var message = new ValueSet(); + message.Add(_appIdLabel, App.Id); + message.Add(_settingsKeyLabel, settingsKey); + try + { + message.Add(_valueLabel, value); + } + catch (Exception) + { + message.Add(_valueLabel, value.ToString()); + } + await InteropServiceConnection.SendMessageAsync(message); + } + + public static void CloseConnectionOnExit() + { + InteropServiceConnection.Dispose(); + } + } +} diff --git a/src/Notepads/Services/ThemeSettingsService.cs b/src/Notepads/Services/ThemeSettingsService.cs index ef1202c13..cc546e348 100644 --- a/src/Notepads/Services/ThemeSettingsService.cs +++ b/src/Notepads/Services/ThemeSettingsService.cs @@ -18,28 +18,22 @@ public static class ThemeSettingsService public static event EventHandler OnBackgroundChanged; public static event EventHandler OnAccentColorChanged; - public static ElementTheme ThemeMode { get; set; } - private static readonly UISettings UISettings = new UISettings(); private static readonly ThemeListener ThemeListener = new ThemeListener(); private static Brush _currentAppBackgroundBrush; - private static bool _useWindowsTheme; + private static ElementTheme _themeMode; - public static bool UseWindowsTheme + public static ElementTheme ThemeMode { - get => _useWindowsTheme; + get => _themeMode; set { - if (value != _useWindowsTheme) + if(value!=_themeMode) { - _useWindowsTheme = value; - if (value) - { - ThemeMode = Application.Current.RequestedTheme.ToElementTheme(); - OnThemeChanged?.Invoke(null, ThemeMode); - } - ApplicationSettingsStore.Write(SettingsKey.UseWindowsThemeBool, _useWindowsTheme); + _themeMode = value; + OnThemeChanged?.Invoke(null, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.RequestedThemeStr, value.ToString()); } } } @@ -56,7 +50,7 @@ public static bool UseWindowsAccentColor { AppAccentColor = UISettings.GetColorValue(UIColorType.Accent); } - ApplicationSettingsStore.Write(SettingsKey.UseWindowsAccentColorBool, _useWindowsAccentColor); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.UseWindowsAccentColorBool, _useWindowsAccentColor); } } @@ -69,7 +63,7 @@ public static double AppBackgroundPanelTintOpacity { _appBackgroundPanelTintOpacity = value; OnBackgroundChanged?.Invoke(null, GetAppBackgroundBrush(ThemeMode)); - ApplicationSettingsStore.Write(SettingsKey.AppBackgroundTintOpacityDouble, value); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.AppBackgroundTintOpacityDouble, value); } } @@ -82,7 +76,7 @@ public static Color AppAccentColor { _appAccentColor = value; OnAccentColorChanged?.Invoke(null, _appAccentColor); - ApplicationSettingsStore.Write(SettingsKey.AppAccentColorHexStr, value.ToHex()); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.AppAccentColorHexStr, value.ToHex()); } } @@ -94,7 +88,7 @@ public static Color CustomAccentColor set { _customAccentColor = value; - ApplicationSettingsStore.Write(SettingsKey.CustomAccentColorHexStr, value.ToHex()); + if (InteropService.EnableSettingsLogging) ApplicationSettingsStore.Write(SettingsKey.CustomAccentColorHexStr, value.ToHex()); } } @@ -167,44 +161,24 @@ private static void InitializeAppBackgroundPanelTintOpacity() private static void InitializeThemeMode() { - if (ApplicationSettingsStore.Read(SettingsKey.UseWindowsThemeBool) is bool useWindowsTheme) - { - _useWindowsTheme = useWindowsTheme; - } - else - { - _useWindowsTheme = true; - } - ThemeListener.ThemeChanged += ThemeListener_ThemeChanged; - ThemeMode = Application.Current.RequestedTheme.ToElementTheme(); - - if (!UseWindowsTheme) + if (ApplicationSettingsStore.Read(SettingsKey.RequestedThemeStr) is string themeModeStr) { - if (ApplicationSettingsStore.Read(SettingsKey.RequestedThemeStr) is string themeModeStr) + if (Enum.TryParse(typeof(ElementTheme), themeModeStr, out var theme)) { - if (Enum.TryParse(typeof(ElementTheme), themeModeStr, out var theme)) - { - ThemeMode = (ElementTheme)theme; - } + _themeMode = (ElementTheme)theme; } } - } - - private static void ThemeListener_ThemeChanged(ThemeListener sender) - { - if (UseWindowsTheme) + else { - SetTheme(sender.CurrentTheme.ToElementTheme()); + _themeMode = ElementTheme.Default; } } - public static void SetTheme(ElementTheme theme) + private static void ThemeListener_ThemeChanged(ThemeListener sender) { - ThemeMode = theme; - OnThemeChanged?.Invoke(null, theme); - ApplicationSettingsStore.Write(SettingsKey.RequestedThemeStr, ThemeMode.ToString()); + _themeMode = sender.CurrentTheme.ToElementTheme(); } public static void SetRequestedTheme(Panel backgroundPanel, UIElement currentContent, ApplicationViewTitleBar titleBar) @@ -227,8 +201,10 @@ public static void SetRequestedTheme(Panel backgroundPanel, UIElement currentCon } // Set ContentDialog background dimming color + var themeMode = ThemeMode; + if (themeMode == ElementTheme.Default) themeMode = Application.Current.RequestedTheme.ToElementTheme(); ((SolidColorBrush)Application.Current.Resources["SystemControlPageBackgroundMediumAltMediumBrush"]).Color = - ThemeMode == ElementTheme.Dark ? Color.FromArgb(153, 0, 0, 0) : Color.FromArgb(153, 255, 255, 255); + themeMode == ElementTheme.Dark ? Color.FromArgb(153, 0, 0, 0) : Color.FromArgb(153, 255, 255, 255); // Set accent color UpdateSystemAccentColorAndBrushes(AppAccentColor); @@ -257,6 +233,7 @@ private static Brush GetAppBackgroundBrush(ElementTheme theme) var darkModeBaseColor = Color.FromArgb(255, 50, 50, 50); var lightModeBaseColor = Color.FromArgb(255, 240, 240, 240); + if (theme == ElementTheme.Default) theme = Application.Current.RequestedTheme.ToElementTheme(); var baseColor = theme == ElementTheme.Light ? lightModeBaseColor : darkModeBaseColor; if (AppBackgroundPanelTintOpacity > 0.99f || @@ -279,6 +256,7 @@ private static Brush GetAppBackgroundBrush(ElementTheme theme) public static void ApplyThemeForTitleBarButtons(ApplicationViewTitleBar titleBar, ElementTheme theme) { + if (theme == ElementTheme.Default) theme = Application.Current.RequestedTheme.ToElementTheme(); if (theme == ElementTheme.Dark) { // Set active window colors @@ -368,7 +346,7 @@ private static void UpdateSystemAccentColorAndBrushes(Color color) } } - private static Color GetColor(string hex) + public static Color GetColor(string hex) { hex = hex.Replace("#", string.Empty); byte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16)); diff --git a/src/Notepads/Settings/SettingsDelegate.cs b/src/Notepads/Settings/SettingsDelegate.cs new file mode 100644 index 000000000..a25d57d40 --- /dev/null +++ b/src/Notepads/Settings/SettingsDelegate.cs @@ -0,0 +1,138 @@ +using Notepads.Services; +using Notepads.Utilities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.UI; +using Windows.UI.Core; +using Windows.UI.Xaml; + +namespace Notepads.Settings +{ + public delegate void Settings(object value); + + public static class SettingsDelegate + { + public static CoreDispatcher Dispatcher; + + public static Settings EditorFontFamily = SetEditorFontFamily; + public static Settings EditorFontSize = SetEditorFontSize; + public static Settings EditorDefaultTextWrapping = SetEditorDefaultTextWrapping; + public static Settings IsLineHighlighterEnabled = SetIsLineHighlighterEnabled; + public static Settings EditorDefaultLineEnding = SetEditorDefaultLineEnding; + public static Settings EditorDefaultEncoding = SetEditorDefaultEncoding; + public static Settings EditorDefaultDecoding = SetEditorDefaultDecoding; + public static Settings EditorDefaultTabIndents = SetEditorDefaultTabIndents; + public static Settings EditorDefaultSearchEngine = SetEditorDefaultSearchEngine; + public static Settings EditorCustomMadeSearchUrl = SetEditorCustomMadeSearchUrl; + public static Settings ShowStatusBar = SetShowStatusBar; + public static Settings IsHighlightMisspelledWordsEnabled = SetIsHighlightMisspelledWordsEnabled; + public static Settings AlwaysOpenNewWindow = SetAlwaysOpenNewWindow; + + private static void SetEditorFontFamily(object value) + { + EditorSettingsService.EditorFontFamily = (string)value; + } + + private static void SetEditorFontSize(object value) + { + EditorSettingsService.EditorFontSize = (int)value; + } + + private static void SetEditorDefaultTextWrapping(object value) + { + Enum.TryParse(typeof(TextWrapping), (string)value, out var result); + EditorSettingsService.EditorDefaultTextWrapping = (TextWrapping)result; + } + + private static void SetIsLineHighlighterEnabled(object value) + { + EditorSettingsService.IsLineHighlighterEnabled = (bool)value; + } + + private static void SetEditorDefaultLineEnding(object value) + { + Enum.TryParse(typeof(LineEnding), (string)value, out var result); + EditorSettingsService.EditorDefaultLineEnding = (LineEnding)result; + } + + private static void SetEditorDefaultEncoding(object value) + { + EditorSettingsService.InitializeEncodingSettings(); + } + + private static void SetEditorDefaultDecoding(object value) + { + EditorSettingsService.InitializeDecodingSettings(); + } + + private static void SetEditorDefaultTabIndents(object value) + { + EditorSettingsService.EditorDefaultTabIndents = (int)value; + } + + private static void SetEditorDefaultSearchEngine(object value) + { + Enum.TryParse(typeof(SearchEngine), (string)value, out var result); + EditorSettingsService.EditorDefaultSearchEngine = (SearchEngine)result; + } + + private static void SetEditorCustomMadeSearchUrl(object value) + { + EditorSettingsService.EditorCustomMadeSearchUrl = (string)value; + } + + private static void SetShowStatusBar(object value) + { + EditorSettingsService.ShowStatusBar = (bool)value; + } + + private static void SetIsHighlightMisspelledWordsEnabled(object value) + { + EditorSettingsService.IsHighlightMisspelledWordsEnabled = (bool)value; + } + + private static void SetAlwaysOpenNewWindow(object value) + { + EditorSettingsService.AlwaysOpenNewWindow = (bool)value; + } + + + public static Settings UseWindowsAccentColor = SetUseWindowsAccentColor; + public static Settings AppBackgroundPanelTintOpacity = SetAppBackgroundPanelTintOpacity; + public static Settings AppAccentColor = SetAppAccentColor; + public static Settings CustomAccentColor = SetCustomAccentColor; + public static Settings ThemeMode = SetThemeMode; + + private static void SetUseWindowsAccentColor(object value) + { + ThemeSettingsService.UseWindowsAccentColor = (bool)value; + } + + private static async void SetAppBackgroundPanelTintOpacity(object value) + { + await ThreadUtility.CallOnUIThreadAsync(Dispatcher, () => + { + ThemeSettingsService.AppBackgroundPanelTintOpacity = (double)value; + }); + } + + private static void SetAppAccentColor(object value) + { + ThemeSettingsService.AppAccentColor = ThemeSettingsService.GetColor((string)value); + } + + private static void SetCustomAccentColor(object value) + { + ThemeSettingsService.CustomAccentColor = ThemeSettingsService.GetColor((string)value); + } + + private static void SetThemeMode(object value) + { + Enum.TryParse(typeof(ElementTheme), (string)value, out var result); + ThemeSettingsService.ThemeMode = (ElementTheme)result; + } + } +} diff --git a/src/Notepads/Settings/SettingsKey.cs b/src/Notepads/Settings/SettingsKey.cs index 6ee35742a..121a63427 100644 --- a/src/Notepads/Settings/SettingsKey.cs +++ b/src/Notepads/Settings/SettingsKey.cs @@ -10,7 +10,6 @@ internal static class SettingsKey // Theme related internal static string RequestedThemeStr = "RequestedThemeStr"; - internal static string UseWindowsThemeBool = "UseWindowsThemeBool"; internal static string AppBackgroundTintOpacityDouble = "AppBackgroundTintOpacityDouble"; internal static string AppAccentColorHexStr = "AppAccentColorHexStr"; internal static string CustomAccentColorHexStr = "CustomAccentColorHexStr"; diff --git a/src/Notepads/Strings/en-US/Resources.resw b/src/Notepads/Strings/en-US/Resources.resw index 91afff174..a65a265b2 100644 --- a/src/Notepads/Strings/en-US/Resources.resw +++ b/src/Notepads/Strings/en-US/Resources.resw @@ -438,7 +438,7 @@ App: DragAndDrop UIOverride Caption: "Open with Notepads" display text - This is a shadow window of Notepads. Session snapshot and settings are disabled. + This is a shadow window of Notepads. Session snapshot is disabled. App: ShadowWindowIndicator Description display text. diff --git a/src/Notepads/Views/AdvancedSettingsPage.xaml.cs b/src/Notepads/Views/AdvancedSettingsPage.xaml.cs index 1e1a64322..29ad0fe5d 100644 --- a/src/Notepads/Views/AdvancedSettingsPage.xaml.cs +++ b/src/Notepads/Views/AdvancedSettingsPage.xaml.cs @@ -1,6 +1,7 @@ namespace Notepads.Views { using Notepads.Services; + using Notepads.Settings; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -60,11 +61,13 @@ private void EnableSessionBackupAndRestoreToggleSwitch_Toggled(object sender, Ro private void ShowStatusBarToggleSwitch_Toggled(object sender, RoutedEventArgs e) { EditorSettingsService.ShowStatusBar = ShowStatusBarToggleSwitch.IsOn; + InteropService.SyncSettings(SettingsKey.EditorShowStatusBarBool, EditorSettingsService.ShowStatusBar); } private void AlwaysOpenNewWindowToggleSwitch_Toggled(object sender, RoutedEventArgs e) { EditorSettingsService.AlwaysOpenNewWindow = AlwaysOpenNewWindowToggleSwitch.IsOn; + InteropService.SyncSettings(SettingsKey.AlwaysOpenNewWindowBool, EditorSettingsService.AlwaysOpenNewWindow); } } } \ No newline at end of file diff --git a/src/Notepads/Views/NotepadsMainPage.MainMenu.cs b/src/Notepads/Views/NotepadsMainPage.MainMenu.cs index e01ca1456..45c2d0842 100644 --- a/src/Notepads/Views/NotepadsMainPage.MainMenu.cs +++ b/src/Notepads/Views/NotepadsMainPage.MainMenu.cs @@ -34,8 +34,7 @@ private void InitializeMainMenu() if (!App.IsFirstInstance) { - MainMenuButton.Foreground = new SolidColorBrush(ThemeSettingsService.AppAccentColor); - MenuSettingsButton.IsEnabled = false; + MainMenuButton.Foreground = (SolidColorBrush)Application.Current.Resources["SystemControlForegroundAccentBrush"]; } if (App.IsGameBarWidget) diff --git a/src/Notepads/Views/NotepadsMainPage.xaml.cs b/src/Notepads/Views/NotepadsMainPage.xaml.cs index b54c28a82..59ad7cfe9 100644 --- a/src/Notepads/Views/NotepadsMainPage.xaml.cs +++ b/src/Notepads/Views/NotepadsMainPage.xaml.cs @@ -26,6 +26,7 @@ using Windows.UI.Xaml.Navigation; using Microsoft.AppCenter.Analytics; using Windows.Graphics.Printing; + using Windows.ApplicationModel; public sealed partial class NotepadsMainPage : Page { @@ -273,6 +274,10 @@ private async void Sets_Loaded(object sender, RoutedEventArgs e) Window.Current.CoreWindow.Activated -= CoreWindow_Activated; Window.Current.CoreWindow.Activated += CoreWindow_Activated; } + + InteropService.Initialize(); + InteropService.HideSettingsPane += HideSettingsPane_OnSettingsChanged; + SettingsDelegate.Dispatcher = Dispatcher; } private async void App_EnteredBackground(object sender, Windows.ApplicationModel.EnteredBackgroundEventArgs e) @@ -284,6 +289,9 @@ private async void App_EnteredBackground(object sender, Windows.ApplicationModel await SessionManager.SaveSessionAsync(); } + if (InteropService.ISAppExited) + InteropService.CloseConnectionOnExit(); + deferral.Complete(); } @@ -348,12 +356,14 @@ private async void MainPage_CloseRequested(object sender, Windows.UI.Core.Previe { // Save session before app exit await SessionManager.SaveSessionAsync(() => { SessionManager.IsBackupEnabled = false; }); + InteropService.ISAppExited = true; deferral.Complete(); return; } if (!NotepadsCore.HaveUnsavedTextEditor()) { + InteropService.ISAppExited = true; deferral.Complete(); return; } @@ -383,10 +393,12 @@ private async void MainPage_CloseRequested(object sender, Windows.UI.Core.Previe await BuildOpenRecentButtonSubItems(); } + InteropService.ISAppExited = true; deferral.Complete(); }, discardAndExitAction: () => { + InteropService.ISAppExited = true; deferral.Complete(); }, cancelAction: () => @@ -435,6 +447,14 @@ private static void UpdateApplicationTitle(ITextEditor activeTextEditor) } } + private async void HideSettingsPane_OnSettingsChanged(object sender, bool hideArgs) + { + await ThreadUtility.CallOnUIThreadAsync(Dispatcher, () => + { + RootSplitView.IsPaneOpen = !hideArgs; + }); + } + #endregion #region NotepadsCore Events diff --git a/src/Notepads/Views/PersonalizationSettingsPage.xaml.cs b/src/Notepads/Views/PersonalizationSettingsPage.xaml.cs index b9978856c..dd1b46057 100644 --- a/src/Notepads/Views/PersonalizationSettingsPage.xaml.cs +++ b/src/Notepads/Views/PersonalizationSettingsPage.xaml.cs @@ -1,6 +1,7 @@ namespace Notepads.Views { using Notepads.Services; + using Notepads.Settings; using Notepads.Utilities; using Windows.System.Power; using Windows.UI; @@ -18,21 +19,17 @@ public PersonalizationSettingsPage() { InitializeComponent(); - if (ThemeSettingsService.UseWindowsTheme) + switch (ThemeSettingsService.ThemeMode) { - ThemeModeDefaultButton.IsChecked = true; - } - else - { - switch (ThemeSettingsService.ThemeMode) - { - case ElementTheme.Light: - ThemeModeLightButton.IsChecked = true; - break; - case ElementTheme.Dark: - ThemeModeDarkButton.IsChecked = true; - break; - } + case ElementTheme.Default: + ThemeModeDefaultButton.IsChecked = true; + break; + case ElementTheme.Light: + ThemeModeLightButton.IsChecked = true; + break; + case ElementTheme.Dark: + ThemeModeDarkButton.IsChecked = true; + break; } AccentColorToggle.IsOn = ThemeSettingsService.UseWindowsAccentColor; @@ -124,17 +121,16 @@ private void ThemeRadioButton_OnChecked(object sender, RoutedEventArgs e) switch (radioButton.Tag) { case "Light": - ThemeSettingsService.UseWindowsTheme = false; - ThemeSettingsService.SetTheme(ElementTheme.Light); + ThemeSettingsService.ThemeMode = ElementTheme.Light; break; case "Dark": - ThemeSettingsService.UseWindowsTheme = false; - ThemeSettingsService.SetTheme(ElementTheme.Dark); + ThemeSettingsService.ThemeMode = ElementTheme.Dark; break; case "Default": - ThemeSettingsService.UseWindowsTheme = true; + ThemeSettingsService.ThemeMode = ElementTheme.Default; break; } + InteropService.SyncSettings(SettingsKey.RequestedThemeStr, ThemeSettingsService.ThemeMode); } } @@ -143,13 +139,19 @@ private void AccentColorPicker_OnColorChanged(ColorPicker sender, ColorChangedEv if (AccentColorPicker.IsEnabled) { ThemeSettingsService.AppAccentColor = args.NewColor; - if (!AccentColorToggle.IsOn) ThemeSettingsService.CustomAccentColor = args.NewColor; + if (!AccentColorToggle.IsOn) + { + ThemeSettingsService.CustomAccentColor = args.NewColor; + InteropService.SyncSettings(SettingsKey.CustomAccentColorHexStr, ThemeSettingsService.CustomAccentColor); + } + InteropService.SyncSettings(SettingsKey.AppAccentColorHexStr, ThemeSettingsService.AppAccentColor); } } private void BackgroundTintOpacitySlider_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e) { ThemeSettingsService.AppBackgroundPanelTintOpacity = e.NewValue / 100; + InteropService.SyncSettings(SettingsKey.AppBackgroundTintOpacityDouble, ThemeSettingsService.AppBackgroundPanelTintOpacity); } private void WindowsAccentColorToggle_OnToggled(object sender, RoutedEventArgs e) @@ -157,6 +159,7 @@ private void WindowsAccentColorToggle_OnToggled(object sender, RoutedEventArgs e AccentColorPicker.IsEnabled = !AccentColorToggle.IsOn; ThemeSettingsService.UseWindowsAccentColor = AccentColorToggle.IsOn; AccentColorPicker.Color = AccentColorToggle.IsOn ? ThemeSettingsService.AppAccentColor : ThemeSettingsService.CustomAccentColor; + InteropService.SyncSettings(SettingsKey.UseWindowsAccentColorBool, ThemeSettingsService.UseWindowsAccentColor); } } } \ No newline at end of file diff --git a/src/Notepads/Views/TextAndEditorSettingsPage.xaml.cs b/src/Notepads/Views/TextAndEditorSettingsPage.xaml.cs index f8b684f34..411ee94d5 100644 --- a/src/Notepads/Views/TextAndEditorSettingsPage.xaml.cs +++ b/src/Notepads/Views/TextAndEditorSettingsPage.xaml.cs @@ -10,6 +10,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Microsoft.AppCenter.Analytics; + using Notepads.Settings; public sealed partial class TextAndEditorSettingsPage : Page { @@ -250,8 +251,11 @@ private void SearchEngineRadioButton_Checked(object sender, RoutedEventArgs e) CustomSearchUrl.Select(CustomSearchUrl.Text.Length, 0); CustomUrlErrorReport.Visibility = IsValidUrl(CustomSearchUrl.Text) ? Visibility.Collapsed : Visibility.Visible; EditorSettingsService.EditorCustomMadeSearchUrl = CustomSearchUrl.Text; + InteropService.SyncSettings(SettingsKey.EditorCustomMadeSearchUrlStr, CustomSearchUrl.Text); break; } + + InteropService.SyncSettings(SettingsKey.EditorDefaultSearchEngineStr, EditorSettingsService.EditorDefaultSearchEngine); } private void TabBehaviorRadioButton_Checked(object sender, RoutedEventArgs e) @@ -273,6 +277,8 @@ private void TabBehaviorRadioButton_Checked(object sender, RoutedEventArgs e) EditorSettingsService.EditorDefaultTabIndents = 8; break; } + + InteropService.SyncSettings(SettingsKey.EditorDefaultTabIndentsInt, EditorSettingsService.EditorDefaultTabIndents); } private void EncodingRadioButton_Checked(object sender, RoutedEventArgs e) @@ -294,6 +300,8 @@ private void EncodingRadioButton_Checked(object sender, RoutedEventArgs e) EditorSettingsService.EditorDefaultEncoding = new UnicodeEncoding(true, true); break; } + + InteropService.SyncSettings(SettingsKey.EditorDefaultEncodingCodePageInt, EditorSettingsService.EditorDefaultEncoding); } private void DecodingRadioButton_Checked(object sender, RoutedEventArgs e) @@ -323,6 +331,8 @@ private void DecodingRadioButton_Checked(object sender, RoutedEventArgs e) } break; } + + InteropService.SyncSettings(SettingsKey.EditorDefaultDecodingCodePageInt, EditorSettingsService.EditorDefaultDecoding); } private void LineEndingRadioButton_OnChecked(object sender, RoutedEventArgs e) @@ -341,31 +351,38 @@ private void LineEndingRadioButton_OnChecked(object sender, RoutedEventArgs e) EditorSettingsService.EditorDefaultLineEnding = LineEnding.Lf; break; } + + InteropService.SyncSettings(SettingsKey.EditorDefaultLineEndingStr, EditorSettingsService.EditorDefaultLineEnding); } private void FontFamilyPicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { EditorSettingsService.EditorFontFamily = (string)e.AddedItems.First(); + InteropService.SyncSettings(SettingsKey.EditorFontFamilyStr, EditorSettingsService.EditorFontFamily); } private void FontSizePicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { EditorSettingsService.EditorFontSize = (int)e.AddedItems.First(); + InteropService.SyncSettings(SettingsKey.EditorFontSizeInt, EditorSettingsService.EditorFontSize); } private void TextWrappingToggle_OnToggled(object sender, RoutedEventArgs e) { EditorSettingsService.EditorDefaultTextWrapping = TextWrappingToggle.IsOn ? TextWrapping.Wrap : TextWrapping.NoWrap; + InteropService.SyncSettings(SettingsKey.EditorDefaultTextWrappingStr, EditorSettingsService.EditorDefaultTextWrapping); } private void HighlightMisspelledWordsToggle_OnToggled(object sender, RoutedEventArgs e) { EditorSettingsService.IsHighlightMisspelledWordsEnabled = HighlightMisspelledWordsToggle.IsOn; + InteropService.SyncSettings(SettingsKey.EditorHighlightMisspelledWordsBool, EditorSettingsService.IsHighlightMisspelledWordsEnabled); } private void LineHighlighterToggle_OnToggled(object sender, RoutedEventArgs e) { EditorSettingsService.IsLineHighlighterEnabled = LineHighlighterToggle.IsOn; + InteropService.SyncSettings(SettingsKey.EditorDefaultLineHighlighterViewStateBool, EditorSettingsService.IsLineHighlighterEnabled); } private void CustomSearchUrl_TextChanged(object sender, TextChangedEventArgs e) @@ -380,10 +397,13 @@ private void CustomSearchUrl_LostFocus(object sender, RoutedEventArgs e) (IsValidUrl(CustomSearchUrl.Text) && (bool)CustomSearchUrlRadioButton.IsChecked)) { EditorSettingsService.EditorDefaultSearchEngine = SearchEngine.Custom; + InteropService.SyncSettings(SettingsKey.EditorCustomMadeSearchUrlStr, CustomSearchUrl.Text); + InteropService.SyncSettings(SettingsKey.EditorDefaultSearchEngineStr, EditorSettingsService.EditorDefaultSearchEngine); } else if (!IsValidUrl(CustomSearchUrl.Text) && EditorSettingsService.EditorDefaultSearchEngine == SearchEngine.Custom) { EditorSettingsService.EditorDefaultSearchEngine = SearchEngine.Bing; + InteropService.SyncSettings(SettingsKey.EditorDefaultSearchEngineStr, EditorSettingsService.EditorDefaultSearchEngine); } CustomUrlErrorReport.Visibility = IsValidUrl(CustomSearchUrl.Text) ? Visibility.Collapsed : Visibility.Visible; From 73bc69965dda654add4fff33d0ad8ea8b6f7cecf Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Mahunt Date: Mon, 20 Apr 2020 19:27:31 +0530 Subject: [PATCH 2/6] Codefactor changes. --- src/Notepads/Settings/SettingsDelegate.cs | 3 +-- src/Notepads/Views/NotepadsMainPage.xaml.cs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Notepads/Settings/SettingsDelegate.cs b/src/Notepads/Settings/SettingsDelegate.cs index a25d57d40..83e8100e6 100644 --- a/src/Notepads/Settings/SettingsDelegate.cs +++ b/src/Notepads/Settings/SettingsDelegate.cs @@ -99,7 +99,6 @@ private static void SetAlwaysOpenNewWindow(object value) EditorSettingsService.AlwaysOpenNewWindow = (bool)value; } - public static Settings UseWindowsAccentColor = SetUseWindowsAccentColor; public static Settings AppBackgroundPanelTintOpacity = SetAppBackgroundPanelTintOpacity; public static Settings AppAccentColor = SetAppAccentColor; @@ -113,7 +112,7 @@ private static void SetUseWindowsAccentColor(object value) private static async void SetAppBackgroundPanelTintOpacity(object value) { - await ThreadUtility.CallOnUIThreadAsync(Dispatcher, () => + await Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { ThemeSettingsService.AppBackgroundPanelTintOpacity = (double)value; }); diff --git a/src/Notepads/Views/NotepadsMainPage.xaml.cs b/src/Notepads/Views/NotepadsMainPage.xaml.cs index 0a1bc5168..dab64cdf6 100644 --- a/src/Notepads/Views/NotepadsMainPage.xaml.cs +++ b/src/Notepads/Views/NotepadsMainPage.xaml.cs @@ -449,7 +449,7 @@ private static void UpdateApplicationTitle(ITextEditor activeTextEditor) private async void HideSettingsPane_OnSettingsChanged(object sender, bool hideArgs) { - await ThreadUtility.CallOnUIThreadAsync(Dispatcher, () => + await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { RootSplitView.IsPaneOpen = !hideArgs; }); From c50834a70b2db041f1ff9b6e4b64a0e6d8da7116 Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Mahunt Date: Mon, 20 Apr 2020 19:53:34 +0530 Subject: [PATCH 3/6] Made security improvement. --- .../InteropConnectionService.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Notepads.Services/InteropConnectionService.cs b/src/Notepads.Services/InteropConnectionService.cs index 2b09acd07..7a9f924ac 100644 --- a/src/Notepads.Services/InteropConnectionService.cs +++ b/src/Notepads.Services/InteropConnectionService.cs @@ -18,14 +18,21 @@ public void Run(IBackgroundTaskInstance taskInstance) // Get a deferral so that the service isn't terminated. this.backgroundTaskDeferral = taskInstance.GetDeferral(); - // Associate a cancellation handler with the background task. - taskInstance.Canceled += OnTaskCanceled; - // Retrieve the app service connection and set up a listener for incoming app service requests. var details = taskInstance.TriggerDetails as AppServiceTriggerDetails; - appServiceConnection = details.AppServiceConnection; - appServiceConnections.Add(appServiceConnection); - appServiceConnection.RequestReceived += OnRequestReceived; + if(details.CallerPackageFamilyName == Package.Current.Id.FamilyName) + { + appServiceConnection = details.AppServiceConnection; + appServiceConnections.Add(appServiceConnection); + appServiceConnection.RequestReceived += OnRequestReceived; + + // Associate a cancellation handler with the background task. + taskInstance.Canceled += OnTaskCanceled; + } + else + { + this.backgroundTaskDeferral.Complete(); + } } private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) From a9f16e83f0f9ba993bade63b44bc6e12b1b2670b Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Mahunt Date: Mon, 20 Apr 2020 20:21:08 +0530 Subject: [PATCH 4/6] Small refactoring. --- src/Notepads.Services/InteropConnectionService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Notepads.Services/InteropConnectionService.cs b/src/Notepads.Services/InteropConnectionService.cs index 7a9f924ac..96b1f57f8 100644 --- a/src/Notepads.Services/InteropConnectionService.cs +++ b/src/Notepads.Services/InteropConnectionService.cs @@ -5,7 +5,6 @@ using Windows.ApplicationModel; using Windows.ApplicationModel.AppService; using Windows.ApplicationModel.Background; - using Windows.Foundation.Collections; public sealed class InteropConnectionService : IBackgroundTask { From ccd750057b0c5643e860390fef104cc3912c0e2e Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Mahunt Date: Mon, 20 Apr 2020 22:59:35 +0530 Subject: [PATCH 5/6] Made some enhancements. --- src/Notepads/Services/EditorSettingsService.cs | 14 ++++++++++++++ src/Notepads/Services/InteropService.cs | 6 ------ src/Notepads/Services/ThemeSettingsService.cs | 8 ++++++++ src/Notepads/Views/NotepadsMainPage.xaml.cs | 17 ++++++++++------- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Notepads/Services/EditorSettingsService.cs b/src/Notepads/Services/EditorSettingsService.cs index 7e66f0114..4a6374a87 100644 --- a/src/Notepads/Services/EditorSettingsService.cs +++ b/src/Notepads/Services/EditorSettingsService.cs @@ -468,5 +468,19 @@ private static void InitializeAppOpeningPreferencesSettings() _alwaysOpenNewWindow = false; } } + + public static void UpdateAllSettings() + { + Initialize(); + OnStatusBarVisibilityChanged?.Invoke(null, _showStatusBar); + OnFontFamilyChanged?.Invoke(null, _editorFontFamily); + OnFontSizeChanged?.Invoke(null, _editorFontSize); + OnDefaultTextWrappingChanged?.Invoke(null, _editorDefaultTextWrapping); + OnDefaultLineHighlighterViewStateChanged?.Invoke(null, _isLineHighlighterEnabled); + OnHighlightMisspelledWordsChanged?.Invoke(null, _isHighlightMisspelledWordsEnabled); + OnDefaultLineEndingChanged?.Invoke(null, _editorDefaultLineEnding); + OnDefaultEncodingChanged?.Invoke(null, _editorDefaultEncoding); + OnDefaultTabIndentsChanged?.Invoke(null, _editorDefaultTabIndents); + } } } \ No newline at end of file diff --git a/src/Notepads/Services/InteropService.cs b/src/Notepads/Services/InteropService.cs index f3a122bea..bc7ca1162 100644 --- a/src/Notepads/Services/InteropService.cs +++ b/src/Notepads/Services/InteropService.cs @@ -12,7 +12,6 @@ public static class InteropService { public static EventHandler HideSettingsPane; public static bool EnableSettingsLogging = true; - public static bool ISAppExited = false; public static AppServiceConnection InteropServiceConnection = null; @@ -91,10 +90,5 @@ public static async void SyncSettings(string settingsKey, object value) } await InteropServiceConnection.SendMessageAsync(message); } - - public static void CloseConnectionOnExit() - { - InteropServiceConnection.Dispose(); - } } } diff --git a/src/Notepads/Services/ThemeSettingsService.cs b/src/Notepads/Services/ThemeSettingsService.cs index cc546e348..cd2772ea5 100644 --- a/src/Notepads/Services/ThemeSettingsService.cs +++ b/src/Notepads/Services/ThemeSettingsService.cs @@ -355,5 +355,13 @@ public static Color GetColor(string hex) byte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16)); return Windows.UI.Color.FromArgb(a, r, g, b); } + + public static void UpdateAllSettings() + { + Initialize(); + OnThemeChanged?.Invoke(null, _themeMode); + OnBackgroundChanged?.Invoke(null, GetAppBackgroundBrush(ThemeMode)); + OnAccentColorChanged?.Invoke(null, _appAccentColor); + } } } \ No newline at end of file diff --git a/src/Notepads/Views/NotepadsMainPage.xaml.cs b/src/Notepads/Views/NotepadsMainPage.xaml.cs index dab64cdf6..316f7e942 100644 --- a/src/Notepads/Views/NotepadsMainPage.xaml.cs +++ b/src/Notepads/Views/NotepadsMainPage.xaml.cs @@ -269,7 +269,9 @@ private async void Sets_Loaded(object sender, RoutedEventArgs e) // To work around this do not use the EnteredBackground event when running as a widget. // Microsoft is tracking this issue as VSO#25735260 Application.Current.EnteredBackground -= App_EnteredBackground; + Application.Current.LeavingBackground -= App_LeavingBackground; Application.Current.EnteredBackground += App_EnteredBackground; + Application.Current.LeavingBackground += App_LeavingBackground; Window.Current.CoreWindow.Activated -= CoreWindow_Activated; Window.Current.CoreWindow.Activated += CoreWindow_Activated; @@ -289,12 +291,17 @@ private async void App_EnteredBackground(object sender, Windows.ApplicationModel await SessionManager.SaveSessionAsync(); } - if (InteropService.ISAppExited) - InteropService.CloseConnectionOnExit(); - + InteropService.InteropServiceConnection.Dispose(); deferral.Complete(); } + private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e) + { + ThemeSettingsService.UpdateAllSettings(); + EditorSettingsService.UpdateAllSettings(); + InteropService.Initialize(); + } + public void ExecuteProtocol(Uri uri) { LoggingService.LogInfo($"[{nameof(NotepadsMainPage)}] Executing protocol: {uri}", consoleOnly: true); @@ -356,14 +363,12 @@ private async void MainPage_CloseRequested(object sender, Windows.UI.Core.Previe { // Save session before app exit await SessionManager.SaveSessionAsync(() => { SessionManager.IsBackupEnabled = false; }); - InteropService.ISAppExited = true; deferral.Complete(); return; } if (!NotepadsCore.HaveUnsavedTextEditor()) { - InteropService.ISAppExited = true; deferral.Complete(); return; } @@ -393,12 +398,10 @@ private async void MainPage_CloseRequested(object sender, Windows.UI.Core.Previe await BuildOpenRecentButtonSubItems(); } - InteropService.ISAppExited = true; deferral.Complete(); }, discardAndExitAction: () => { - InteropService.ISAppExited = true; deferral.Complete(); }, cancelAction: () => From d19f6e78d2eca8f734495fa175f00cd664d8264e Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Mahunt Date: Sun, 3 May 2020 02:22:07 +0530 Subject: [PATCH 6/6] Improved syncing perfermance. --- .../InteropConnectionService.cs | 10 +++++----- src/Notepads/Services/InteropService.cs | 16 ++++++---------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Notepads.Services/InteropConnectionService.cs b/src/Notepads.Services/InteropConnectionService.cs index 96b1f57f8..c0112aa6b 100644 --- a/src/Notepads.Services/InteropConnectionService.cs +++ b/src/Notepads.Services/InteropConnectionService.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.ApplicationModel.AppService; using Windows.ApplicationModel.Background; @@ -34,13 +35,12 @@ public void Run(IBackgroundTaskInstance taskInstance) } } - private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) + private void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { - foreach(var serviceConnection in appServiceConnections) + Parallel.ForEach(appServiceConnections, async (serviceConnection) => { - if (serviceConnection != appServiceConnection) - await serviceConnection.SendMessageAsync(args.Request.Message); - } + if (serviceConnection != appServiceConnection) await serviceConnection.SendMessageAsync(args.Request.Message); + }); } private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) diff --git a/src/Notepads/Services/InteropService.cs b/src/Notepads/Services/InteropService.cs index bc7ca1162..a4a672dc6 100644 --- a/src/Notepads/Services/InteropService.cs +++ b/src/Notepads/Services/InteropService.cs @@ -59,15 +59,12 @@ public static async void Initialize() private static void InteropServiceConnection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { var message = args.Request.Message; - if (message.ContainsKey(_appIdLabel) && message[_appIdLabel] is Guid appID && appID != App.Id) - { - EnableSettingsLogging = false; - HideSettingsPane?.Invoke(null, true); - var settingsKey = message[_settingsKeyLabel] as string; - var value = message[_valueLabel] as object; - SettingsManager[settingsKey](value); - EnableSettingsLogging = true; - } + EnableSettingsLogging = false; + HideSettingsPane?.Invoke(null, true); + var settingsKey = message[_settingsKeyLabel] as string; + var value = message[_valueLabel] as object; + SettingsManager[settingsKey](value); + EnableSettingsLogging = true; } private static void InteropServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args) @@ -78,7 +75,6 @@ private static void InteropServiceConnection_ServiceClosed(AppServiceConnection public static async void SyncSettings(string settingsKey, object value) { var message = new ValueSet(); - message.Add(_appIdLabel, App.Id); message.Add(_settingsKeyLabel, settingsKey); try {