Skip to content

Commit

Permalink
Fix: AvaloniaUI#413 CompletionWindow doesn't work with mouse click
Browse files Browse the repository at this point in the history
  • Loading branch information
mgarstenauer committed Apr 17, 2024
1 parent adbe72b commit 38e165f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/AvaloniaEdit/CodeCompletion/CompletionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
Expand All @@ -37,7 +38,7 @@ public class CompletionList : TemplatedControl
{
public CompletionList()
{
DoubleTapped += OnDoubleTapped;
AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Bubble, true);

CompletionAcceptKeys = new[]
{
Expand Down Expand Up @@ -110,7 +111,7 @@ public CompletionListBox ListBox
}

/// <summary>
/// Gets or sets the array of keys that are supposed to request insertation of the completion
/// Gets or sets the array of keys that are supposed to request insertion of the completion.
/// </summary>
public Key[] CompletionAcceptKeys { get; set; }

Expand Down Expand Up @@ -185,14 +186,23 @@ public void HandleKey(KeyEventArgs e)
}
}

protected void OnDoubleTapped(object sender, RoutedEventArgs e)
private void OnPointerPressed(object sender, PointerPressedEventArgs e)
{
//TODO TEST
if (((AvaloniaObject)e.Source).VisualAncestorsAndSelf()
.TakeWhile(obj => obj != this).Any(obj => obj is ListBoxItem))
var visual = e.Source as Visual;
if (e.GetCurrentPoint(visual).Properties.IsLeftButtonPressed)
{
e.Handled = true;
RequestInsertion(e);
var listBoxItem = visual.VisualAncestorsAndSelf()
.TakeWhile(v => v != this)
.OfType<ListBoxItem>()
.FirstOrDefault();

if (listBoxItem != null)
{
// A completion item was clicked.
Debug.Assert(e.Handled, "Click expected to be handled by ListBoxItem.");
Debug.Assert(listBoxItem.IsSelected, "Completion item expected to be selected.");
RequestInsertion(e);
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/AvaloniaEdit/CodeCompletion/CompletionWindowBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
if ((Child == null || !Child.IsKeyboardFocusWithin) && !IsTextAreaFocused)
{
Hide();
}
Expand Down

0 comments on commit 38e165f

Please sign in to comment.