Skip to content
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
13 changes: 13 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions docs/SSMS_INTEGRATION_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,15 @@ established.
Plain file copies into `%LocalAppData%\Microsoft\SSMS\<ver>\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:
Expand Down
1 change: 1 addition & 0 deletions src/SqlPilot.Package.Legacy/SqlPilot.Package.Legacy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="..\SqlPilot.Package\Integration\ScriptingBridge.cs" Link="Integration\ScriptingBridge.cs" />
<Compile Include="..\SqlPilot.Package\Integration\ManagedConnectionWrapper.cs" Link="Integration\ManagedConnectionWrapper.cs" />
<Compile Include="..\SqlPilot.Package\Integration\ReflectionTypeCache.cs" Link="Integration\ReflectionTypeCache.cs" />
<Compile Include="..\SqlPilot.Package\Integration\SsmsThemeAdapter.cs" Link="Integration\SsmsThemeAdapter.cs" />
<Compile Include="..\SqlPilot.Package\Options\SqlPilotOptionsPage.cs" Link="Options\SqlPilotOptionsPage.cs" />
<Compile Include="..\SqlPilot.Package\Services\UpdateChecker.cs" Link="Services\UpdateChecker.cs" />
<Compile Include="..\SqlPilot.Package\Services\ConnectionWatcher.cs" Link="Services\ConnectionWatcher.cs" />
Expand Down
132 changes: 132 additions & 0 deletions src/SqlPilot.Package/Integration/SsmsThemeAdapter.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
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 */ }
}
}

/// <summary>
/// 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.
/// </summary>
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;
}
}
}
19 changes: 18 additions & 1 deletion src/SqlPilot.Package/SqlPilotToolWindow.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
}
}
}
Loading
Loading