Skip to content

Commit 79bafbb

Browse files
authored
DYN-5921: Clear code block edit flag on unload and focus loss (#17311)
1 parent 09486e0 commit 79bafbb

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/DynamoCoreWpf/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Dynamo.Wpf.Views.GuidedTour.RealTimeInfoWindow.HyperlinkUri.get -> System.Uri
5858
Dynamo.Wpf.Views.GuidedTour.RealTimeInfoWindow.HyperlinkUri.set -> void
5959
override Dynamo.PackageManager.UI.PackageManagerTabControl.OnKeyDown(System.Windows.Input.KeyEventArgs e) -> void
6060
override Dynamo.UI.Controls.CodeBlockEditor.OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e) -> void
61+
override Dynamo.UI.Controls.CodeBlockEditor.OnIsKeyboardFocusWithinChanged(System.Windows.DependencyPropertyChangedEventArgs e) -> void
6162
override Dynamo.PackageManager.UI.PackageManagerTabControl.OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e) -> void
6263
static Dynamo.PackageManager.UI.PackageManagerTabControl.GetSuppressHomeEndWhenSelected(System.Windows.DependencyObject element) -> bool
6364
static Dynamo.PackageManager.UI.PackageManagerTabControl.SetSuppressHomeEndWhenSelected(System.Windows.DependencyObject element, bool value) -> void

src/DynamoCoreWpf/Views/CodeBlocks/CodeBlockEditor.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Windows;
23
using Dynamo.Controls;
34
using Dynamo.Core;
45
using Dynamo.Graph.Nodes;
@@ -19,6 +20,11 @@ internal enum State
1920

2021
private State state;
2122

23+
/// <summary>
24+
/// Current editor state.
25+
/// </summary>
26+
internal State CurrentState => state;
27+
2228
/// <summary>
2329
/// Event handler for editor state changed.
2430
/// </summary>
@@ -84,6 +90,22 @@ public CodeBlockEditor(NodeView nodeView): base(nodeView)
8490

8591
WatermarkLabel.Text = Properties.Resources.WatermarkLabelText;
8692
stateMachine.OnStateChanged += OnEditorStateChanged;
93+
nodeView.Unloaded += OnNodeViewUnloaded;
94+
}
95+
96+
/// <summary>
97+
/// Ensures graph shortcuts are restored if this editor is unloaded while still
98+
/// in edit mode (e.g. node removal or workspace close). LostFocus may not commit
99+
/// when <see cref="CodeCompletionEditor.IsDisposed"/> is already true.
100+
/// Only clears the shared flag when this editor was the active one, so unloading
101+
/// another Code Block does not re-enable shortcuts mid-edit.
102+
/// </summary>
103+
private void OnNodeViewUnloaded(object sender, System.Windows.RoutedEventArgs e)
104+
{
105+
if (stateMachine.CurrentState == EditorStateMachine.State.Editing)
106+
{
107+
nodeViewModel.DynamoViewModel.IsCodeBlockEditorActive = false;
108+
}
87109
}
88110

89111
protected override void OnEscape()
@@ -116,6 +138,21 @@ protected override void OnCommitChange()
116138
protected override void OnTextAreaGotFocus(object sender, System.Windows.RoutedEventArgs e)
117139
{
118140
stateMachine.Transit(EditorStateMachine.State.Editing);
141+
// Ensure the flag is set even if the state machine was already Editing
142+
// (e.g. keyboard focus returned after a modal dialog closed).
143+
nodeViewModel.DynamoViewModel.IsCodeBlockEditorActive = true;
144+
}
145+
146+
/// <summary>
147+
/// Keeps the shared edit flag in sync with keyboard focus. Clears it when focus
148+
/// leaves (e.g. dialog or AA assistant chat) and sets it again when focus returns,
149+
/// including cases where TextArea GotFocus does not fire reliably.
150+
/// </summary>
151+
protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
152+
{
153+
base.OnIsKeyboardFocusWithinChanged(e);
154+
155+
nodeViewModel.DynamoViewModel.IsCodeBlockEditorActive = IsKeyboardFocusWithin;
119156
}
120157

121158
private void OnEditorStateChanged(EditorStateMachine.State state)

0 commit comments

Comments
 (0)