Skip to content

Commit e281672

Browse files
authored
Detect mouse capture loss during text selection
There may be times when SelectionMouseHandler detects a left mouse button down event on its TextArea, but then immediately the mouse capture is lost, and SelectionMouseHandler does not reset the selection mode. I noticed this when I started showing a dialog in response to my main window being activated, if I have detected that the file being edited by the user was changed by another process. When I activate my application by clicking on the editor, the SelectionMouseHandler detects this and begins selecting text. But immediately the TextArea loses mouse capture, as in my handler I show a modal dialog. When the user dismisses this dialog, event though the left mouse button is up, moving the mouse around extends the selection as if it were down. I mimic exactly the approach in the source code for System.Windows.Controls.Primitives.Thumb, the only difference being that here I have to go looking for the SelectionMouseHandler that is associated with the TextArea in the sender parameter. Perhaps there is a more direct way of finding it, though this seems to work for me.
1 parent 697ff0d commit e281672

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

ICSharpCode.AvalonEdit/Editing/SelectionMouseHandler.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public SelectionMouseHandler(TextArea textArea)
8989
this.textArea = textArea;
9090
}
9191

92+
static SelectionMouseHandler()
93+
{
94+
EventManager.RegisterClassHandler(typeof(TextArea), Mouse.LostMouseCaptureEvent, new MouseEventHandler(OnLostMouseCapture));
95+
}
96+
97+
private static void OnLostMouseCapture(object sender, MouseEventArgs e)
98+
{
99+
TextArea textArea = (TextArea)sender;
100+
if (Mouse.Captured != textArea)
101+
{
102+
SelectionMouseHandler handler = textArea.DefaultInputHandler.NestedInputHandlers.OfType<SelectionMouseHandler>().FirstOrDefault();
103+
if (handler != null)
104+
handler.mode = SelectionMode.None;
105+
}
106+
}
107+
92108
public TextArea TextArea {
93109
get { return textArea; }
94110
}

0 commit comments

Comments
 (0)