Skip to content

Commit

Permalink
fix #46 (#49)
Browse files Browse the repository at this point in the history
* Fixed a bug where expandable control don't follow the theme changes, and make MarkDown Preview use the app color theme by default.

* bumped up version number

Co-authored-by: Etienne Baudoux <[email protected]>
  • Loading branch information
veler and Etienne Baudoux authored Oct 29, 2021
1 parent f6dca38 commit d6b483f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var updateOptions = function (opt: monaco.editor.IEditorOptions) {

var updateDiffOptions = function (opt: monaco.editor.IDiffEditorOptions) {
var diffEditor = (editor as unknown) as monaco.editor.IStandaloneDiffEditor;
if (opt != null && typeof opt === "object") {
if (diffEditor != null && opt != null && typeof opt === "object") {
diffEditor.updateOptions(opt);
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/dev/impl/DevToys/Api/Core/Theme/IThemeListener.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using System;
using Windows.UI.Xaml;

namespace DevToys.Api.Core.Theme
{
Expand All @@ -16,6 +17,11 @@ public interface IThemeListener
/// </summary>
AppTheme CurrentAppTheme { get; }

/// <summary>
/// Gets the actual theme applied in the app.
/// </summary>
ApplicationTheme ActualAppTheme { get; }

/// <summary>
/// Gets or sets a value indicating whether the current theme is high contrast.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/dev/impl/DevToys/Assets/ReleaseNote.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
• RegEx Tester tool got new options.
• New layout.
• Text editor can now use any font.
• Russian translation improved.
🐛 Bug Fixes
• Minor bugs fixed.
4 changes: 4 additions & 0 deletions src/dev/impl/DevToys/Core/Theme/ThemeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal sealed class ThemeListener : IThemeListener

public AppTheme CurrentAppTheme => _settingsProvider.GetSetting(PredefinedSettings.Theme);

public ApplicationTheme ActualAppTheme { get; private set; }

public bool IsHighContrast { get; private set; }

public event EventHandler? ThemeChanged;
Expand Down Expand Up @@ -59,6 +61,7 @@ public void ApplyDesiredColorTheme()
if (Window.Current.Content is FrameworkElement frameworkElement)
{
frameworkElement.RequestedTheme = theme == AppTheme.Light ? ElementTheme.Light : ElementTheme.Dark;
ActualAppTheme = theme == AppTheme.Light ? ApplicationTheme.Light : ApplicationTheme.Dark;
}
}

Expand Down Expand Up @@ -96,6 +99,7 @@ private void SettingsProvider_SettingChanged(object sender, SettingChangedEventA
if (string.Equals(PredefinedSettings.Theme.Name, e.SettingName, StringComparison.Ordinal))
{
ApplyDesiredColorTheme();
ThemeChanged?.Invoke(this, EventArgs.Empty);
}
}

Expand Down
52 changes: 48 additions & 4 deletions src/dev/impl/DevToys/UI/Controls/ExpandableSettingControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#nullable enable

using DevToys.Api.Core.Theme;
using DevToys.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
Expand All @@ -10,9 +12,13 @@ namespace DevToys.UI.Controls
[ContentProperty(Name = nameof(SettingActionableElement))]
public sealed partial class ExpandableSettingControl : UserControl
{
private bool _isAppThemeChanging;
private bool _isDefaultHeaderBackgroundValue = true;
private bool _isDefaultContentBackgroundValue = true;

public FrameworkElement? SettingActionableElement { get; set; }

public static readonly DependencyProperty TitleProperty
public static readonly DependencyProperty TitleProperty
= DependencyProperty.Register(
nameof(Title),
typeof(string),
Expand Down Expand Up @@ -51,7 +57,7 @@ public IconElement Icon
set => SetValue(IconProperty, value);
}

public static readonly DependencyProperty ExpandableContentProperty
public static readonly DependencyProperty ExpandableContentProperty
= DependencyProperty.Register(
nameof(ExpandableContent),
typeof(FrameworkElement),
Expand Down Expand Up @@ -82,7 +88,14 @@ public static readonly DependencyProperty ContentBackgroundProperty
nameof(ContentBackground),
typeof(Brush),
typeof(ExpandableSettingControl),
new PropertyMetadata(Application.Current.Resources["ExpanderContentBackground"]));
new PropertyMetadata(null, (d, e) =>
{
var ctrl = (ExpandableSettingControl)d;
if (!ctrl._isAppThemeChanging)
{
ctrl._isDefaultContentBackgroundValue = false;
}
}));

public Brush ContentBackground
{
Expand All @@ -95,7 +108,14 @@ public static readonly DependencyProperty HeaderBackgroundProperty
nameof(HeaderBackground),
typeof(Brush),
typeof(ExpandableSettingControl),
new PropertyMetadata(Application.Current.Resources["ExpanderHeaderBackground"]));
new PropertyMetadata(null, (d, e) =>
{
var ctrl = (ExpandableSettingControl)d;
if (!ctrl._isAppThemeChanging)
{
ctrl._isDefaultHeaderBackgroundValue = false;
}
}));

public Brush HeaderBackground
{
Expand All @@ -106,6 +126,30 @@ public Brush HeaderBackground
public ExpandableSettingControl()
{
InitializeComponent();

MefComposer.Provider.Import<IThemeListener>().ThemeChanged += ExpandableSettingControl_ThemeChanged;

_isAppThemeChanging = true;
ContentBackground = (Brush)Application.Current.Resources["ExpanderContentBackground"];
HeaderBackground = (Brush)Application.Current.Resources["ExpanderHeaderBackground"];
_isAppThemeChanging = false;
}

private void ExpandableSettingControl_ThemeChanged(object sender, System.EventArgs e)
{
_isAppThemeChanging = true;

if (_isDefaultContentBackgroundValue)
{
ContentBackground = (Brush)Application.Current.Resources["ExpanderContentBackground"];
}

if (_isDefaultHeaderBackgroundValue)
{
HeaderBackground = (Brush)Application.Current.Resources["ExpanderHeaderBackground"];
}

_isAppThemeChanging = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using DevToys.Api.Core.Settings;
using DevToys.Api.Core.Theme;
using DevToys.Api.Tools;
using DevToys.Core;
using DevToys.Core.Threading;
Expand All @@ -23,12 +24,13 @@ public sealed class MarkdownPreviewToolViewModel : ObservableRecipient, IToolVie
{
private static string? _htmlDocument;

private static readonly SettingDefinition<ApplicationTheme> ThemeSetting
private static readonly SettingDefinition<string?> ThemeSetting
= new(
name: $"{nameof(MarkdownPreviewToolViewModel)}.{nameof(ThemeSetting)}",
isRoaming: true,
defaultValue: ApplicationTheme.Light);
defaultValue: null);

private readonly IThemeListener _themeListener;
private readonly Queue<string> _workQueue = new();

private bool _workInProgress;
Expand All @@ -53,12 +55,20 @@ internal string? InputValue

internal ApplicationTheme Theme
{
get => SettingsProvider.GetSetting(ThemeSetting);
get
{
string? theme = SettingsProvider.GetSetting(ThemeSetting);
if (string.IsNullOrEmpty(theme))
{
theme = _themeListener.ActualAppTheme.ToString();
}
return (ApplicationTheme)Enum.Parse(typeof(ApplicationTheme), theme);
}
set
{
if (SettingsProvider.GetSetting(ThemeSetting) != value)
if (!string.Equals(SettingsProvider.GetSetting(ThemeSetting), value.ToString()))
{
SettingsProvider.SetSetting(ThemeSetting, value);
SettingsProvider.SetSetting(ThemeSetting, value.ToString());
OnPropertyChanged();
QueueWork();
}
Expand All @@ -68,8 +78,9 @@ internal ApplicationTheme Theme
internal ISettingsProvider SettingsProvider { get; }

[ImportingConstructor]
public MarkdownPreviewToolViewModel(ISettingsProvider settingsProvider)
public MarkdownPreviewToolViewModel(ISettingsProvider settingsProvider, IThemeListener themeListener)
{
_themeListener = themeListener;
SettingsProvider = settingsProvider;

// Activate the view model's messenger.
Expand Down
2 changes: 1 addition & 1 deletion tools/app-version-number.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.5.0
0.6.5.1

0 comments on commit d6b483f

Please sign in to comment.