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/docs/SSMS_INTEGRATION_NOTES.md b/docs/SSMS_INTEGRATION_NOTES.md index 2ac92e5..fb54e25 100644 --- a/docs/SSMS_INTEGRATION_NOTES.md +++ b/docs/SSMS_INTEGRATION_NOTES.md @@ -387,6 +387,15 @@ established. Plain file copies into `%LocalAppData%\Microsoft\SSMS\\Extensions\` are **not** picked up (not even by `/updateconfiguration`); only VSIXInstaller registers per-user extensions. +## Following the SSMS theme + +`VSColorTheme.GetThemedColor(EnvironmentColors.…)` and `VSColorTheme.ThemeChanged` work in all three versions, including the VS 2017 shell in SSMS 18/20 — the `EnvironmentColors`, `CommonControlsColors` and `InfoBarColors` keys resolve there to the Blue theme's values. `SsmsThemeAdapter` is the only consumer; see `docs/ARCHITECTURE.md` § Theming. + +- **A tool window gets nothing for free.** WPF content hosted in a `ToolWindowPane` is not given themed brushes or implicit styles; unresolved `DynamicResource` keys just leave the property unset (a transparent background, black text), which looks fine in a light theme and unreadable in a dark one. +- **Stock WPF templates hard-code light colours**: button hover/pressed fills, and the icon gutter in the `ContextMenu` template. Setting `Background`/`Foreground` is not enough for those two; they need their own templates. `ListBoxItem` selection is translucent and survives a dark background as it is. +- **`TreeView` and `TreeViewItem` set their own `Foreground`**, so text inside a tree does not inherit the host's colour. +- **SSMS 22 themes** are under `Tools > Theme` and switch live: Light, Dark and a set of tinted ones (Cool Breeze, Moonlight Glow, …). There is no "Blue" any more. SSMS 18/20 offer Blue and Light only. + ## Debug Logging `Debug.WriteLine` from an extension goes to... nowhere useful by default. For real diagnostics: 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 @@