diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/IconInfoViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/IconInfoViewModel.cs index f91a359593..64c6ac89e7 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/IconInfoViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/IconInfoViewModel.cs @@ -22,7 +22,7 @@ public partial class IconInfoViewModel : ObservableObject, IIconInfo public IconDataViewModel Dark { get; private set; } - public IconDataViewModel IconForTheme(bool light) => Light = light ? Light : Dark; + public IconDataViewModel IconForTheme(bool light) => light ? Light : Dark; public bool HasIcon(bool light) => IconForTheme(light).HasIcon; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml index e1164cd5dc..578233f366 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml @@ -92,13 +92,14 @@ @@ -173,6 +174,21 @@ + + + + + + + + + + + + + + + diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml.cs index 19a15b289c..5a7f736d60 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockItemControl.xaml.cs @@ -138,50 +138,46 @@ public sealed partial class DockItemControl : Control private void UpdateIconVisibility() { - if (Icon is IconBox icon) + var shouldShowIcon = ShouldShowIcon(); + if (_iconPresenter is not null) { - var dt = icon.DataContext; - var src = icon.Source; - - if (_iconPresenter is not null) - { - // n.b. this might be wrong - I think we always have an Icon (an IconBox), - // we need to check if the box has an icon - _iconPresenter.Visibility = Icon is null ? Visibility.Collapsed : Visibility.Visible; - } - - UpdateIconVisibilityState(); + _iconPresenter.Visibility = shouldShowIcon ? Visibility.Visible : Visibility.Collapsed; } + + UpdateIconVisibilityState(); } private void UpdateIconVisibilityState() { - var hasIcon = Icon is not null; - VisualStateManager.GoToState(this, hasIcon ? "IconVisible" : "IconHidden", true); + VisualStateManager.GoToState(this, ShouldShowIcon() ? "IconVisible" : "IconHidden", true); } private void UpdateAlignment() { - // If this item has both an icon and a label, left align so that the - // icons don't wobble if the text changes. - // - // Otherwise, center align. - var requestedTheme = ActualTheme; - var isLight = requestedTheme == ElementTheme.Light; - var showText = HasText; - if (Icon is IconBox icoBox && - icoBox.DataContext is DockItemViewModel item && - item.Icon is IconInfoViewModel icon) + HorizontalAlignment = HorizontalAlignment.Stretch; + UpdateTextAlignmentState(); + } + + private bool ShouldShowIcon() + { + if (Icon is IconBox icoBox) { - var showIcon = icon is not null && icon.HasIcon(isLight); - if (showText && showIcon) + if (icoBox.SourceKey is IconInfoViewModel icon) { - HorizontalAlignment = HorizontalAlignment.Left; - return; + return icon.HasIcon(ActualTheme == ElementTheme.Light); } + + return icoBox.Source is not null; } - HorizontalAlignment = HorizontalAlignment.Stretch; + return Icon is not null; + } + + private void UpdateTextAlignmentState() + { + var verticalDock = _parentDock?.DockSide is DockSide.Left or DockSide.Right; + var shouldCenterText = verticalDock && !ShouldShowIcon(); + VisualStateManager.GoToState(this, shouldCenterText ? "TextCentered" : "TextLeftAligned", true); } private void UpdateAllVisibility() @@ -195,12 +191,14 @@ public sealed partial class DockItemControl : Control { base.OnApplyTemplate(); IsEnabledChanged -= OnIsEnabledChanged; + ActualThemeChanged -= DockItemControl_ActualThemeChanged; PointerEntered -= Control_PointerEntered; PointerExited -= Control_PointerExited; Loaded -= DockItemControl_Loaded; Unloaded -= DockItemControl_Unloaded; + ActualThemeChanged += DockItemControl_ActualThemeChanged; PointerEntered += Control_PointerEntered; PointerExited += Control_PointerExited; Loaded += DockItemControl_Loaded; @@ -229,12 +227,19 @@ public sealed partial class DockItemControl : Control { _parentDock = dock; UpdateInnerMarginForDockSide(dock.DockSide); + UpdateAllVisibility(); _dockSideCallbackToken = dock.RegisterPropertyChangedCallback( DockControl.DockSideProperty, OnParentDockSideChanged); } } + private void DockItemControl_ActualThemeChanged(FrameworkElement sender, object args) + { + UpdateIconVisibility(); + UpdateAlignment(); + } + private void DockItemControl_Unloaded(object sender, RoutedEventArgs e) { if (_parentDock is not null && _dockSideCallbackToken >= 0) @@ -252,6 +257,7 @@ public sealed partial class DockItemControl : Control if (sender is DockControl dock) { UpdateInnerMarginForDockSide(dock.DockSide); + UpdateAlignment(); } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs index e2012559b8..2f06946563 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs @@ -57,7 +57,10 @@ public sealed partial class TimeDateCommandsProvider : CommandProvider var wrappedBand = new WrappedDockItem( [_bandItem], "com.microsoft.cmdpal.timedate.dockBand", - Resources.Microsoft_plugin_timedate_dock_band_title); + Resources.Microsoft_plugin_timedate_dock_band_title) + { + Icon = Icons.TimeDateExtIcon, + }; return new ICommandItem[] { wrappedBand }; }