Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: sync settings and recently opened file list across all instances and modify settings in shadow instances #603

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Notepads/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public App()
InitializeComponent();

Suspending += OnSuspending;
LeavingBackground += OnLeavingBackground;
}

/// <summary>
Expand Down Expand Up @@ -107,7 +108,6 @@ private async Task ActivateAsync(IActivatedEventArgs e)
{
{ "OSArchitecture", SystemInformation.OperatingSystemArchitecture.ToString() },
{ "OSVersion", $"{SystemInformation.OperatingSystemVersion.Major}.{SystemInformation.OperatingSystemVersion.Minor}.{SystemInformation.OperatingSystemVersion.Build}" },
{ "UseWindowsTheme", ThemeSettingsService.UseWindowsTheme.ToString() },
{ "ThemeMode", ThemeSettingsService.ThemeMode.ToString() },
{ "UseWindowsAccentColor", ThemeSettingsService.UseWindowsAccentColor.ToString() },
{ "AppBackgroundTintOpacity", $"{(int) (ThemeSettingsService.AppBackgroundPanelTintOpacity * 10.0) * 10}" },
Expand Down Expand Up @@ -219,6 +219,18 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
deferral.Complete();
}

/// <summary>
/// Occurs when the app moves to foreground from background.
/// Pending changes to the UI is made before app comes to focus.
/// </summary>
/// <param name="sender">The source of the leaving background request.</param>
/// <param name="e">Details about the leaving background request.</param>
private void OnLeavingBackground(object sender, LeavingBackgroundEventArgs e)
{
ThemeSettingsService.Initialize(true);
AppSettingsService.Initialize(true);
}

// Occurs when an exception is not handled on the UI thread.
private static void OnUnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Notepads/Controls/Dialog/NotepadsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class NotepadsDialog : ContentDialog

public NotepadsDialog()
{
RequestedTheme = ThemeSettingsService.ThemeMode;
Background = ThemeSettingsService.ThemeMode == ElementTheme.Dark
RequestedTheme = ThemeSettingsService.GetActualTheme(ThemeSettingsService.ThemeMode);
Background = RequestedTheme == ElementTheme.Dark
? _darkModeBackgroundBrush
: _lightModeBackgroundBrush;

Expand Down
1 change: 1 addition & 0 deletions src/Notepads/Notepads.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<Compile Include="Controls\TextEditor\TextEditorCore.MoveText.cs" />
<Compile Include="Extensions\DispatcherExtensions.cs" />
<Compile Include="Extensions\ScrollViewerExtensions.cs" />
<Compile Include="Services\InterInstanceSyncService.cs" />
<Compile Include="Utilities\FutureAccessListUtility.cs" />
<Compile Include="Utilities\LanguageUtility.cs" />
<Compile Include="Views\Settings\AboutPage.xaml.cs">
Expand Down
98 changes: 71 additions & 27 deletions src/Notepads/Services/AppSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,36 +284,41 @@ public static bool IsSmartCopyEnabled
}
}

public static void Initialize()
public static void Initialize(bool shouldInvokeChangedEvent = false)
{
InitializeFontSettings();
InitializeFontFamilySettings(shouldInvokeChangedEvent);
InitializeFontSizeSettings(shouldInvokeChangedEvent);
InitializeFontStyleSettings(shouldInvokeChangedEvent);
InitializeFontWeightSettings(shouldInvokeChangedEvent);

InitializeTextWrappingSettings();
InitializeTextWrappingSettings(shouldInvokeChangedEvent);

InitializeSpellingSettings();
InitializeSpellingSettings(shouldInvokeChangedEvent);

InitializeDisplaySettings();
InitializeDisplayLineHighlighterSettings(shouldInvokeChangedEvent);
InitializeDisplayLineNumbersSettings(shouldInvokeChangedEvent);

InitializeSmartCopySettings();
InitializeSmartCopySettings(shouldInvokeChangedEvent);

InitializeLineEndingSettings();
InitializeLineEndingSettings(shouldInvokeChangedEvent);

InitializeEncodingSettings();
InitializeEncodingSettings(shouldInvokeChangedEvent);

InitializeDecodingSettings();
InitializeDecodingSettings(shouldInvokeChangedEvent);

InitializeTabIndentsSettings();
InitializeTabIndentsSettings(shouldInvokeChangedEvent);

InitializeSearchEngineSettings();
InitializeSearchEngineSettings(shouldInvokeChangedEvent);
InitializeCustomSearchUrlSettings(shouldInvokeChangedEvent);

InitializeStatusBarSettings();
InitializeStatusBarSettings(shouldInvokeChangedEvent);

InitializeSessionSnapshotSettings();
InitializeSessionSnapshotSettings(shouldInvokeChangedEvent);

InitializeAppOpeningPreferencesSettings();
InitializeAppOpeningPreferencesSettings(shouldInvokeChangedEvent);
}

private static void InitializeStatusBarSettings()
public static void InitializeStatusBarSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorShowStatusBarBool) is bool showStatusBar)
{
Expand All @@ -323,9 +328,11 @@ private static void InitializeStatusBarSettings()
{
_showStatusBar = true;
}

if (invokeChangedEvent) OnStatusBarVisibilityChanged?.Invoke(null, _showStatusBar);
}

private static void InitializeSessionSnapshotSettings()
private static void InitializeSessionSnapshotSettings(bool invokeChangedEvent = false)
{
// We should disable session snapshot feature on multi instances
if (!App.IsPrimaryInstance)
Expand All @@ -349,7 +356,7 @@ private static void InitializeSessionSnapshotSettings()
}
}

private static void InitializeLineEndingSettings()
public static void InitializeLineEndingSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultLineEndingStr) is string lineEndingStr &&
Enum.TryParse(typeof(LineEnding), lineEndingStr, out var lineEnding))
Expand All @@ -360,9 +367,11 @@ private static void InitializeLineEndingSettings()
{
_editorDefaultLineEnding = LineEnding.Crlf;
}

if (invokeChangedEvent) OnDefaultLineEndingChanged?.Invoke(null, _editorDefaultLineEnding);
}

private static void InitializeTextWrappingSettings()
public static void InitializeTextWrappingSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultTextWrappingStr) is string textWrappingStr &&
Enum.TryParse(typeof(TextWrapping), textWrappingStr, out var textWrapping))
Expand All @@ -373,9 +382,11 @@ private static void InitializeTextWrappingSettings()
{
_editorDefaultTextWrapping = TextWrapping.NoWrap;
}

if (invokeChangedEvent) OnDefaultTextWrappingChanged?.Invoke(null, _editorDefaultTextWrapping);
}

private static void InitializeSpellingSettings()
public static void InitializeSpellingSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorHighlightMisspelledWordsBool) is bool highlightMisspelledWords)
{
Expand All @@ -385,9 +396,11 @@ private static void InitializeSpellingSettings()
{
_isHighlightMisspelledWordsEnabled = false;
}

if (invokeChangedEvent) OnHighlightMisspelledWordsChanged?.Invoke(null, _isHighlightMisspelledWordsEnabled);
}

private static void InitializeDisplaySettings()
public static void InitializeDisplayLineHighlighterSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultLineHighlighterViewStateBool) is bool displayLineHighlighter)
{
Expand All @@ -398,6 +411,11 @@ private static void InitializeDisplaySettings()
_editorDisplayLineHighlighter = true;
}

if (invokeChangedEvent) OnDefaultLineHighlighterViewStateChanged?.Invoke(null, _editorDisplayLineHighlighter);
}

public static void InitializeDisplayLineNumbersSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultDisplayLineNumbersBool) is bool displayLineNumbers)
{
_displayLineNumbers = displayLineNumbers;
Expand All @@ -406,9 +424,11 @@ private static void InitializeDisplaySettings()
{
_displayLineNumbers = true;
}

if (invokeChangedEvent) OnDefaultDisplayLineNumbersViewStateChanged?.Invoke(null, _displayLineNumbers);
}

private static void InitializeSmartCopySettings()
public static void InitializeSmartCopySettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorEnableSmartCopyBool) is bool enableSmartCopy)
{
Expand All @@ -420,7 +440,7 @@ private static void InitializeSmartCopySettings()
}
}

private static void InitializeEncodingSettings()
public static void InitializeEncodingSettings(bool invokeChangedEvent = false)
{
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

Expand All @@ -446,9 +466,11 @@ private static void InitializeEncodingSettings()
{
_editorDefaultEncoding = new UTF8Encoding(false);
}

if (invokeChangedEvent) OnDefaultEncodingChanged?.Invoke(null, _editorDefaultEncoding);
}

private static void InitializeDecodingSettings()
public static void InitializeDecodingSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultDecodingCodePageInt) is int decodingCodePage)
{
Expand Down Expand Up @@ -480,7 +502,7 @@ private static void InitializeDecodingSettings()
}
}

private static void InitializeTabIndentsSettings()
public static void InitializeTabIndentsSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultTabIndentsInt) is int tabIndents)
{
Expand All @@ -490,9 +512,11 @@ private static void InitializeTabIndentsSettings()
{
_editorDefaultTabIndents = -1;
}

if (invokeChangedEvent) OnDefaultTabIndentsChanged?.Invoke(null, _editorDefaultTabIndents);
}

private static void InitializeSearchEngineSettings()
public static void InitializeSearchEngineSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorDefaultSearchEngineStr) is string searchEngineStr &&
Enum.TryParse(typeof(SearchEngine), searchEngineStr, out var searchEngine))
Expand All @@ -503,7 +527,10 @@ private static void InitializeSearchEngineSettings()
{
_editorDefaultSearchEngine = SearchEngine.Bing;
}
}

public static void InitializeCustomSearchUrlSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorCustomMadeSearchUrlStr) is string customMadeSearchUrl)
{
_editorCustomMadeSearchUrl = customMadeSearchUrl;
Expand All @@ -514,7 +541,7 @@ private static void InitializeSearchEngineSettings()
}
}

private static void InitializeFontSettings()
public static void InitializeFontFamilySettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorFontFamilyStr) is string fontFamily)
{
Expand All @@ -525,6 +552,11 @@ private static void InitializeFontSettings()
_editorFontFamily = "Consolas";
}

if (invokeChangedEvent) OnFontFamilyChanged?.Invoke(null, _editorFontFamily);
}

public static void InitializeFontSizeSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorFontSizeInt) is int fontSize)
{
_editorFontSize = fontSize;
Expand All @@ -534,6 +566,11 @@ private static void InitializeFontSettings()
_editorFontSize = 14;
}

if (invokeChangedEvent) OnFontSizeChanged?.Invoke(null, _editorFontSize);
}

public static void InitializeFontStyleSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorFontStyleStr) is string fontStyleStr &&
Enum.TryParse(typeof(FontStyle), fontStyleStr, out var fontStyle))
{
Expand All @@ -544,6 +581,11 @@ private static void InitializeFontSettings()
_editorFontStyle = FontStyle.Normal;
}

if (invokeChangedEvent) OnFontStyleChanged?.Invoke(null, _editorFontStyle);
}

public static void InitializeFontWeightSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorFontWeightUshort) is ushort fontWeight)
{
_editorFontWeight = new FontWeight()
Expand All @@ -555,9 +597,11 @@ private static void InitializeFontSettings()
{
_editorFontWeight = FontWeights.Normal;
}

if (invokeChangedEvent) OnFontWeightChanged?.Invoke(null, _editorFontWeight);
}

private static void InitializeAppOpeningPreferencesSettings()
public static void InitializeAppOpeningPreferencesSettings(bool invokeChangedEvent = false)
{
if (ApplicationSettingsStore.Read(SettingsKey.AlwaysOpenNewWindowBool) is bool alwaysOpenNewWindow)
{
Expand Down
68 changes: 68 additions & 0 deletions src/Notepads/Services/InterInstanceSyncService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Notepads.Services
{
using Notepads.Extensions;
using Notepads.Settings;
using Notepads.Views.MainPage;
using System;
using System.Collections.Generic;
using Windows.Storage;

public static class InterInstanceSyncService
{
private static NotepadsMainPage _notepadsMainPage = null;

public static readonly string RecentFilesListKey = "BuildOpenRecentButtonSubItems";

public static readonly IReadOnlyDictionary<string, Action<bool>> SyncManager = new Dictionary<string, Action<bool>>
{
{SettingsKey.AppBackgroundTintOpacityDouble, ThemeSettingsService.InitializeAppBackgroundPanelTintOpacity },
{SettingsKey.RequestedThemeStr, ThemeSettingsService.InitializeThemeMode },
{SettingsKey.UseWindowsAccentColorBool, ThemeSettingsService.InitializeAppAccentColor },
{SettingsKey.AppAccentColorHexStr, ThemeSettingsService.InitializeAppAccentColor },
{SettingsKey.CustomAccentColorHexStr, ThemeSettingsService.InitializeCustomAccentColor },
{SettingsKey.EditorDefaultLineHighlighterViewStateBool, AppSettingsService.InitializeDisplayLineHighlighterSettings },
{SettingsKey.EditorDefaultDisplayLineNumbersBool, AppSettingsService.InitializeDisplayLineNumbersSettings },
{SettingsKey.EditorDefaultTabIndentsInt, AppSettingsService.InitializeTabIndentsSettings },
{SettingsKey.EditorDefaultTextWrappingStr, AppSettingsService.InitializeTextWrappingSettings },
{SettingsKey.EditorFontFamilyStr, AppSettingsService.InitializeFontFamilySettings },
{SettingsKey.EditorFontSizeInt, AppSettingsService.InitializeFontSizeSettings },
{SettingsKey.EditorFontStyleStr, AppSettingsService.InitializeFontStyleSettings },
{SettingsKey.EditorFontWeightUshort, AppSettingsService.InitializeFontWeightSettings },
{SettingsKey.EditorHighlightMisspelledWordsBool, AppSettingsService.InitializeSpellingSettings },
{SettingsKey.EditorDefaultEncodingCodePageInt, AppSettingsService.InitializeEncodingSettings },
{SettingsKey.EditorDefaultLineEndingStr, AppSettingsService.InitializeLineEndingSettings },
{SettingsKey.EditorShowStatusBarBool, AppSettingsService.InitializeStatusBarSettings },
{SettingsKey.EditorCustomMadeSearchUrlStr, AppSettingsService.InitializeCustomSearchUrlSettings },
{SettingsKey.EditorDefaultDecodingCodePageInt, AppSettingsService.InitializeDecodingSettings },
{SettingsKey.EditorDefaultSearchEngineStr, AppSettingsService.InitializeSearchEngineSettings },
{SettingsKey.EditorEnableSmartCopyBool, AppSettingsService.InitializeSmartCopySettings },
{SettingsKey.AlwaysOpenNewWindowBool, AppSettingsService.InitializeAppOpeningPreferencesSettings },
{RecentFilesListKey, async (permission) => await _notepadsMainPage.BuildOpenRecentButtonSubItems(!permission) }
};

public static void Initialize(NotepadsMainPage page)
{
_notepadsMainPage = page;
ApplicationData.Current.DataChanged += Application_OnDataChanged;
}

private static async void Application_OnDataChanged(ApplicationData sender, object args)
{
if (ApplicationSettingsStore.Read(SettingsKey.LastChangedSettingsAppInstanceIdStr) is string lastChangedSettingsAppInstanceIdStr &&
lastChangedSettingsAppInstanceIdStr == App.Id.ToString())
{
return;
}

if (ApplicationSettingsStore.Read(SettingsKey.LastChangedSettingsKeyStr) is string lastChangedSettingsKeyStr &&
SyncManager.ContainsKey(lastChangedSettingsKeyStr) && _notepadsMainPage != null)
{
await DispatcherExtensions.CallOnUIThreadAsync(_notepadsMainPage.Dispatcher, () =>
{
if (lastChangedSettingsKeyStr != RecentFilesListKey) _notepadsMainPage.CloseSettingsPane();
SyncManager[lastChangedSettingsKeyStr].Invoke(true);
});
}
}
}
}
Loading