From b45606c795c10510b125294b0497c807294f11c7 Mon Sep 17 00:00:00 2001 From: Christian Mourier Date: Thu, 19 Nov 2026 01:23:43 +0100 Subject: [PATCH 1/2] Follow the SSMS colour theme in the tool window Closes #9. The XAML asked for brush keys (ResultsBackground, PrimaryForeground, ...) that only the demo app's DarkTheme.xaml ever defined, and the tool window used Windows SystemColors plus fixed colours, so inside SSMS the pane stayed light whatever the theme. SsmsThemeAdapter (Package, C#) now fills those keys on the hosted control from VSColorTheme and refills them on ThemeChanged; the tool window creates it before showing the control and disposes it. C# rather than XAML keeps VSSDK out of SqlPilot.UI and avoids naming a Shell assembly that differs between the 15.x and 17.x builds. SystemColors and fixed colours are gone from the tool window and SearchControl; the search box sets its colours directly (its local Style blocked the host's), the placeholder is an overlay so it can be themed, and buttons and the context menu get small templates because the stock ones hard-code light hover colours and a light icon gutter. No theme setting. Checked in the demo app and a XAML harness with dark and light palettes; not yet run inside SSMS. --- docs/ARCHITECTURE.md | 13 ++ .../SqlPilot.Package.Legacy.csproj | 1 + .../Integration/SsmsThemeAdapter.cs | 132 ++++++++++++++++++ src/SqlPilot.Package/SqlPilotToolWindow.cs | 19 ++- .../SqlPilotToolWindowControl.xaml | 97 ++++++++++++- src/SqlPilot.UI/Controls/ScopeControl.xaml | 7 +- src/SqlPilot.UI/Controls/SearchControl.xaml | 58 ++++---- src/SqlPilot.UI/Themes/DarkTheme.xaml | 6 + src/SqlPilot.UI/Themes/LightTheme.xaml | 6 + 9 files changed, 304 insertions(+), 35 deletions(-) create mode 100644 src/SqlPilot.Package/Integration/SsmsThemeAdapter.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 16d3b0e..e7ad53a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -104,6 +104,19 @@ The UI side is `ScopeViewModel` + `ServerScopeNode` (tri-state `bool? IsChecked` **LineStore gotcha**: composite keys are built with `LineStore.Join` / read back with `LineStore.Split`, so the escape scheme has exactly one owner. `LoadSettings` splits each line at the first *unescaped* separator — a plain `IndexOf('|')` would split a `D|server|database` key in the middle of its own escaped separator. +## Theming + +The pane follows the SSMS colour theme; there is no theme setting of its own. + +All XAML — `SearchControl` and `ScopeControl` in `SqlPilot.UI`, and `SqlPilotToolWindowControl` in the Package — refers only to SQL Pilot's own brush keys through `DynamicResource` (`ResultsBackground`, `PrimaryForeground`, `SecondaryForeground`, `IconForeground`, `InputBackground`, `ButtonBackground`, `MenuBackground`, `InfoBarBackground`, …). Nothing names a Windows `SystemColors` key or a fixed colour. Who supplies the brushes depends on the host: + +- **In SSMS** — `SqlPilot.Package/Integration/SsmsThemeAdapter.cs`. `SqlPilotToolWindow.Initialize` creates one for the control before it is shown. It reads colours with `VSColorTheme.GetThemedColor` (`EnvironmentColors`, `CommonControlsColors`, `InfoBarColors`), writes frozen brushes into the control's `Resources`, and does it again on `VSColorTheme.ThemeChanged`, so switching theme repaints an open pane. The tool window disposes it, which unsubscribes. A key the shell has no colour for falls back to the matching Windows system colour. It also adopts the shell's scroll bar style (`VsResourceKeys.ScrollBarStyleKey`) when the shell publishes one. +- **In the demo** — `SqlPilot.UI/Themes/DarkTheme.xaml`, merged by `SqlPilot.UI.Demo/App.xaml`. `LightTheme.xaml` carries the same keys. + +The adapter is C# in the Package project on purpose: `SqlPilot.UI` keeps no VSSDK reference, and no XAML has to name a Shell assembly, which would need to resolve in both the VSSDK 15.x and 17.x builds. Every type it uses exists in both. + +Adding a colour means adding the key in three places: the XAML that uses it, `SsmsThemeAdapter.Apply`, and both theme dictionaries. Two WPF details shaped the XAML: a control with a local `Style` ignores implicit styles from its host (so the search box sets its colours as plain properties), and `TreeView`, `TreeViewItem` and the stock button and context-menu templates set or hard-code their own colours (so the tool window carries small themed templates for buttons and the context menu, and hands the text colour to the tree explicitly). + ## SSMS Integration Points All verified working via Phase 0 spike on SSMS 18, 20, and 22: diff --git a/src/SqlPilot.Package.Legacy/SqlPilot.Package.Legacy.csproj b/src/SqlPilot.Package.Legacy/SqlPilot.Package.Legacy.csproj index 4c651a6..3b6b316 100644 --- a/src/SqlPilot.Package.Legacy/SqlPilot.Package.Legacy.csproj +++ b/src/SqlPilot.Package.Legacy/SqlPilot.Package.Legacy.csproj @@ -23,6 +23,7 @@ + diff --git a/src/SqlPilot.Package/Integration/SsmsThemeAdapter.cs b/src/SqlPilot.Package/Integration/SsmsThemeAdapter.cs new file mode 100644 index 0000000..5233b86 --- /dev/null +++ b/src/SqlPilot.Package/Integration/SsmsThemeAdapter.cs @@ -0,0 +1,132 @@ +using System; +using System.Windows; +using System.Windows.Controls.Primitives; +using System.Windows.Media; +using Microsoft.VisualStudio.PlatformUI; +using Microsoft.VisualStudio.Shell; + +namespace SqlPilot.Package.Integration +{ + /// + /// Makes the tool window follow the SSMS colour theme. The XAML in SqlPilot.UI and + /// the tool window only ever name SQL Pilot's own brush keys; this fills those keys + /// on the hosted control from the shell's theme and refills them when it changes. + /// It lives here, in C#, so SqlPilot.UI needs no VSSDK reference and no XAML has to + /// name a Shell assembly that differs between the VSSDK 15.x and 17.x builds. + /// + internal sealed class SsmsThemeAdapter : IDisposable + { + private readonly FrameworkElement _target; + private bool _disposed; + + public SsmsThemeAdapter(FrameworkElement target) + { + _target = target ?? throw new ArgumentNullException(nameof(target)); + Apply(); + VSColorTheme.ThemeChanged += OnThemeChanged; + } + + private void OnThemeChanged(ThemeChangedEventArgs e) + { + if (_disposed) return; + + if (_target.Dispatcher.CheckAccess()) + Apply(); + else + _target.Dispatcher.BeginInvoke(new Action(Apply)); + } + + private void Apply() + { + if (_disposed) return; + + var r = _target.Resources; + + // Surfaces and text + Color background = Themed(EnvironmentColors.ToolWindowBackgroundColorKey, SystemColors.WindowColor); + r["ResultsBackground"] = Brush(background); + r["SearchBoxBackground"] = Brush(background); + r["StatusBarBackground"] = Brush(background); + r["PanelBorder"] = Brush(Themed(EnvironmentColors.ToolWindowBorderColorKey, SystemColors.ControlDarkColor)); + r["PrimaryForeground"] = Brush(Themed(EnvironmentColors.ToolWindowTextColorKey, SystemColors.WindowTextColor)); + r["SecondaryForeground"] = Brush(Themed(EnvironmentColors.SystemGrayTextColorKey, SystemColors.GrayTextColor)); + r["IconForeground"] = Brush(Themed(EnvironmentColors.PanelHyperlinkColorKey, SystemColors.HotTrackColor)); + + // Gold reads well on a dark surface and washes out on a light one. + r["FavoriteForeground"] = Brush(IsDark(background) ? Colors.Gold : Colors.DarkGoldenrod); + + // Search box + r["InputBackground"] = Brush(Themed(CommonControlsColors.TextBoxBackgroundColorKey, SystemColors.WindowColor)); + r["InputForeground"] = Brush(Themed(CommonControlsColors.TextBoxTextColorKey, SystemColors.WindowTextColor)); + r["InputBorder"] = Brush(Themed(CommonControlsColors.TextBoxBorderColorKey, SystemColors.ControlDarkColor)); + + // Buttons + r["ButtonBackground"] = Brush(Themed(CommonControlsColors.ButtonColorKey, SystemColors.ControlColor)); + r["ButtonForeground"] = Brush(Themed(CommonControlsColors.ButtonTextColorKey, SystemColors.ControlTextColor)); + r["ButtonBorder"] = Brush(Themed(CommonControlsColors.ButtonBorderColorKey, SystemColors.ControlDarkColor)); + r["ButtonHoverBackground"] = Brush(Themed(CommonControlsColors.ButtonHoverColorKey, SystemColors.ControlLightColor)); + r["ButtonHoverForeground"] = Brush(Themed(CommonControlsColors.ButtonHoverTextColorKey, SystemColors.ControlTextColor)); + r["ButtonHoverBorder"] = Brush(Themed(CommonControlsColors.ButtonBorderHoverColorKey, SystemColors.HighlightColor)); + r["ButtonPressedBackground"] = Brush(Themed(CommonControlsColors.ButtonPressedColorKey, SystemColors.ControlDarkColor)); + r["ButtonPressedForeground"] = Brush(Themed(CommonControlsColors.ButtonPressedTextColorKey, SystemColors.ControlTextColor)); + r["ButtonPressedBorder"] = Brush(Themed(CommonControlsColors.ButtonBorderPressedColorKey, SystemColors.HighlightColor)); + r["ButtonDisabledForeground"] = Brush(Themed(CommonControlsColors.ButtonDisabledTextColorKey, SystemColors.GrayTextColor)); + + // Context menu + r["MenuBackground"] = Brush(Themed(EnvironmentColors.CommandBarMenuBackgroundGradientBeginColorKey, SystemColors.MenuColor)); + r["MenuForeground"] = Brush(Themed(EnvironmentColors.CommandBarTextActiveColorKey, SystemColors.MenuTextColor)); + r["MenuBorder"] = Brush(Themed(EnvironmentColors.CommandBarMenuBorderColorKey, SystemColors.ControlDarkColor)); + + // Update notification bar + r["InfoBarBackground"] = Brush(Themed(InfoBarColors.InfoBarBackgroundColorKey, SystemColors.InfoColor)); + r["InfoBarForeground"] = Brush(Themed(InfoBarColors.InfoBarBackgroundTextColorKey, SystemColors.InfoTextColor)); + r["InfoBarBorder"] = Brush(Themed(InfoBarColors.InfoBarBorderColorKey, SystemColors.ControlDarkColor)); + + // The shell's own scroll bar style, when it publishes one. It is built on + // theme brushes itself, so it only needs putting in place once. + if (!r.Contains(typeof(ScrollBar))) + { + try + { + if (Application.Current?.TryFindResource(VsResourceKeys.ScrollBarStyleKey) is Style scrollBarStyle) + r[typeof(ScrollBar)] = scrollBarStyle; + } + catch { /* stock scroll bars */ } + } + } + + /// + /// A colour from the current theme. Falls back to the matching Windows system + /// colour when the shell has no value for the key, which is how the window + /// looked before it followed the theme. + /// + private static Color Themed(ThemeResourceKey key, Color fallback) + { + try + { + var c = VSColorTheme.GetThemedColor(key); + return c.A == 0 ? fallback : Color.FromArgb(c.A, c.R, c.G, c.B); + } + catch + { + return fallback; + } + } + + private static SolidColorBrush Brush(Color color) + { + var brush = new SolidColorBrush(color); + brush.Freeze(); + return brush; + } + + private static bool IsDark(Color c) => (0.299 * c.R + 0.587 * c.G + 0.114 * c.B) < 128; + + public void Dispose() + { + if (_disposed) return; + _disposed = true; + VSColorTheme.ThemeChanged -= OnThemeChanged; + } + } +} diff --git a/src/SqlPilot.Package/SqlPilotToolWindow.cs b/src/SqlPilot.Package/SqlPilotToolWindow.cs index 53b7bc2..7d93bab 100644 --- a/src/SqlPilot.Package/SqlPilotToolWindow.cs +++ b/src/SqlPilot.Package/SqlPilotToolWindow.cs @@ -1,12 +1,15 @@ using System; using System.Runtime.InteropServices; using Microsoft.VisualStudio.Shell; +using SqlPilot.Package.Integration; namespace SqlPilot.Package { [Guid("c7d8e9f0-1a2b-3c4d-5e6f-7a8b9c0d1e2f")] public class SqlPilotToolWindow : ToolWindowPane { + private SsmsThemeAdapter _themeAdapter; + public SqlPilotToolWindow() : base(null) { Caption = "SQL Pilot"; @@ -18,8 +21,22 @@ protected override void Initialize() if (Package is SqlPilotPackage package) { - Content = new SqlPilotToolWindowControl(package); + var control = new SqlPilotToolWindowControl(package); + + // Before the control is shown, so it never paints unthemed. + _themeAdapter = new SsmsThemeAdapter(control); + Content = control; + } + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _themeAdapter?.Dispose(); + _themeAdapter = null; } + base.Dispose(disposing); } } } diff --git a/src/SqlPilot.Package/SqlPilotToolWindowControl.xaml b/src/SqlPilot.Package/SqlPilotToolWindowControl.xaml index 7510bf5..1f83c9e 100644 --- a/src/SqlPilot.Package/SqlPilotToolWindowControl.xaml +++ b/src/SqlPilot.Package/SqlPilotToolWindowControl.xaml @@ -2,10 +2,91 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:SqlPilot.UI.Controls;assembly=SqlPilot.UI" - Focusable="True"> + Focusable="True" + Background="{DynamicResource ResultsBackground}" + Foreground="{DynamicResource PrimaryForeground}"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -19,7 +100,9 @@ @@ -30,7 +113,7 @@