From 9d08d8ba2fb80211503843482caa819a3eeaa964 Mon Sep 17 00:00:00 2001 From: Vorta <179685+Vorta@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:59:35 +0100 Subject: [PATCH 1/3] Keep World tree scroll position when focus returns --- CHANGELOG.md | 4 ++ .../MainWindow.OutlinerKeyboard.cs | 42 ++++++++++++ src/zStudio.Desktop/MainWindow.SceneTree.cs | 2 + src/zStudio.Desktop/MainWindow.xaml | 2 +- src/zStudio.Desktop/MainWindow.xaml.cs | 1 + .../SourceWorldTreeChecks.cs | 64 +++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9f0712d..1a4af906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Version numbers follow `MAJOR.MINOR.PATCH`; the 0.x series is under active development. Release tags and downloadable archives use the same version. +## X.Y.Z — Unreleased + +- Clicking a World tree row button after switching back to zStudio keeps the tree at its current scroll position. + ## 0.12.3 — 2026-11-12 - The Blender extension now installs on Blender 5.3 and newer, retaining the 5.2 minimum without an upper version cutoff. diff --git a/src/zStudio.Desktop/MainWindow.OutlinerKeyboard.cs b/src/zStudio.Desktop/MainWindow.OutlinerKeyboard.cs index 72b604a2..7534b634 100644 --- a/src/zStudio.Desktop/MainWindow.OutlinerKeyboard.cs +++ b/src/zStudio.Desktop/MainWindow.OutlinerKeyboard.cs @@ -3,6 +3,7 @@ using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; +using System.Windows.Threading; namespace zStudio.Desktop; @@ -19,11 +20,51 @@ public partial class MainWindow /// Set while a rebuild disables the tree that had keyboard focus: the rebuilt tree gives it back to the selection. private bool outlinerFocusPending; + private bool suppressOutlinerFocusScroll; + private int outlinerFocusScrollGeneration; + + private void InitializeOutlinerFocusScroll() + { + Activated += (_, _) => GuardOutlinerFocusScroll(); + DocumentSceneTree.AddHandler(Mouse.PreviewMouseDownEvent, + new MouseButtonEventHandler((_, _) => GuardOutlinerFocusScroll()), handledEventsToo: true); + DocumentSceneTree.IsVisibleChanged += (_, _) => { if (!DocumentSceneTree.IsVisible) ClearOutlinerFocusScroll(); }; + } + + /// + /// WPF restores the old focused row on activation, and on mouse-down even when a non-focusable row button handles it. + /// Let focus return without scrolling away from the row being clicked. Deliberate navigation clears this guard. + /// + private void GuardOutlinerFocusScroll() + { + if (!sourceChromeActive || sceneTree?.Projection == null || !DocumentSceneTree.IsVisible) return; + suppressOutlinerFocusScroll = true; + int generation = ++outlinerFocusScrollGeneration; + Dispatcher.BeginInvoke(DispatcherPriority.Input, () => + { + if (generation == outlinerFocusScrollGeneration) ClearOutlinerFocusScroll(); + }); + } + + private void ClearOutlinerFocusScroll() + { + suppressOutlinerFocusScroll = false; + ++outlinerFocusScrollGeneration; + } + + private void OutlinerBringIntoView(object sender, RequestBringIntoViewEventArgs e) + { + // This handler belongs on each item, below the internal ScrollViewer. WPF redirects an item's request to its header. + if (suppressOutlinerFocusScroll && sender is TreeViewItem { DataContext: SceneTreeItem row } && row.Owner == sceneTree) + e.Handled = true; + } + private void OutlinerKeyDown(object sender, KeyEventArgs e) { if (!sourceChromeActive || sceneTree is not { Projection: not null } model || System.Windows.Input.Keyboard.Modifiers != ModifierKeys.None) return; // The tree's context menu routes its keys through the tree: they are the menu's. if (e.OriginalSource is not Visual source || !DocumentSceneTree.IsAncestorOf(source)) return; + ClearOutlinerFocusScroll(); var focused = System.Windows.Input.Keyboard.FocusedElement as DependencyObject; var item = focused == null ? null : RowContainer(focused); var row = item?.DataContext as SceneTreeItem; @@ -98,6 +139,7 @@ private static void FocusRow(TreeViewItem item) /// The row's container, created (and scrolled to) when the tree has not made it yet. private TreeViewItem? RealizeRow(SceneTreeItem row) { + ClearOutlinerFocusScroll(); Stack path = new(); for (var at = row; at != null; at = at.Parent) path.Push(at); ItemsControl parent = DocumentSceneTree; TreeViewItem? container = null; diff --git a/src/zStudio.Desktop/MainWindow.SceneTree.cs b/src/zStudio.Desktop/MainWindow.SceneTree.cs index 641ae056..4ed78580 100644 --- a/src/zStudio.Desktop/MainWindow.SceneTree.cs +++ b/src/zStudio.Desktop/MainWindow.SceneTree.cs @@ -52,6 +52,7 @@ private void RefreshSceneTree() if (outlinerFocusPending) { if (sceneTree?.Selected == null) outlinerFocusPending = false; else ScheduleSceneTreeReveal(); } return; } + ClearOutlinerFocusScroll(); changingSceneTree = true; try { @@ -281,6 +282,7 @@ private void ScheduleSceneTreeReveal(SceneTreeItem? reveal = null) { if (sceneTree != model || generation != sceneTreeRevealGeneration) return; if (!DocumentSceneTree.IsVisible) { outlinerFocusPending = false; return; } + ClearOutlinerFocusScroll(); // An explicit reveal (including rebuild restoration) may scroll its intended row. changingSceneTree = true; try { diff --git a/src/zStudio.Desktop/MainWindow.xaml b/src/zStudio.Desktop/MainWindow.xaml index 5aead9d2..a627d356 100644 --- a/src/zStudio.Desktop/MainWindow.xaml +++ b/src/zStudio.Desktop/MainWindow.xaml @@ -324,7 +324,7 @@ - + diff --git a/src/zStudio.Desktop/MainWindow.xaml.cs b/src/zStudio.Desktop/MainWindow.xaml.cs index 4fe387d1..278e381c 100644 --- a/src/zStudio.Desktop/MainWindow.xaml.cs +++ b/src/zStudio.Desktop/MainWindow.xaml.cs @@ -60,6 +60,7 @@ public MainWindow() shutdownToken = shutdown.Token; preview = CancellationTokenSource.CreateLinkedTokenSource(shutdownToken); InitializeComponent(); DataContext = ViewModel; + InitializeOutlinerFocusScroll(); // The Animations rows' target text and Frame follow the World tree pane's width (narrow below 260 DIP, wide from 400). SourceOutlinerPane.SizeChanged += (_, e) => sceneTree?.SetPaneWidth(e.NewSize.Width); WorldDifficulty.ItemsSource = MainViewModel.DifficultyChoices; diff --git a/tests/zStudio.Desktop.Tests/SourceWorldTreeChecks.cs b/tests/zStudio.Desktop.Tests/SourceWorldTreeChecks.cs index d250c195..49748488 100644 --- a/tests/zStudio.Desktop.Tests/SourceWorldTreeChecks.cs +++ b/tests/zStudio.Desktop.Tests/SourceWorldTreeChecks.cs @@ -2,8 +2,10 @@ using System.IO.Pipes; using System.Reflection; using System.Text.Json.Nodes; +using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using ModelContextProtocol.Client; @@ -100,6 +102,60 @@ internal static async Task Run() await ClickEye(eye); await CheckScroll(); Assert.Same(eye, System.Windows.Input.Keyboard.FocusedElement); await ClickEye(eye); await CheckScroll(); + // A real tree mouse-down restores the selected row's focus even when a row button handles it. Raising Click + // alone missed the regression where that restoration scrolls away before the intended action is delivered. + var healthyContainer = Assert.IsType(eyeRow); + var wreckRow = TreeModel().CreatedRows.Single(r => r.Index == Node("wreck")); + var wreckContainer = (TreeViewItem)typeof(MainWindow).GetMethod("RealizeRow", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(main, [wreckRow])!; + var wreckEye = Descendants(wreckContainer).OfType