Skip to content

Commit

Permalink
Merge pull request quicoli#50 from mitchcapper/selection_finishing_pr…
Browse files Browse the repository at this point in the history
…e_event_pr

Provide a PreSelectionFinished event with the cause (Allows control of enter,tab,escape etc actions)
  • Loading branch information
quicoli authored Feb 25, 2022
2 parents 5fc5932 + 39700cc commit 965384c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private void ItemsSelector_PreviewMouseDown(object sender, MouseButtonEventArgs
if (!ItemsSelector.Items.Contains(((FrameworkElement)e.OriginalSource)?.DataContext))
return;
ItemsSelector.SelectedItem = ((FrameworkElement)e.OriginalSource)?.DataContext;
OnSelectionAdapterCommit();
OnSelectionAdapterCommit(SelectionAdapter.EventCause.ItemClicked);
e.Handled = true;
}
private void AutoCompleteComboBox_GotFocus(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -443,18 +443,29 @@ private void OnPopupClosed(object sender, EventArgs e)
{
if (!_selectionCancelled)
{
OnSelectionAdapterCommit();
OnSelectionAdapterCommit(SelectionAdapter.EventCause.PopupClosed);
}
}

private void OnPopupOpened(object sender, EventArgs e)
{
_selectionCancelled = false;
ItemsSelector.SelectedItem = SelectedItem;
}

private void OnSelectionAdapterCancel()
{
}

public event EventHandler<SelectionAdapter.PreSelectionAdapterFinishArgs> PreSelectionAdapterFinish;
private bool PreSelectionEventSomeoneHandled(SelectionAdapter.EventCause cause, bool is_cancel) {
if (PreSelectionAdapterFinish == null)
return false;
var args = new SelectionAdapter.PreSelectionAdapterFinishArgs { cause = cause, is_cancel = is_cancel };
PreSelectionAdapterFinish?.Invoke(this, args);
return args.handled;

}
private void OnSelectionAdapterCancel(SelectionAdapter.EventCause cause)
{
if (PreSelectionEventSomeoneHandled(cause, true))
return;
_isUpdatingText = true;
Editor.Text = SelectedItem == null ? Filter : GetDisplayText(SelectedItem);
Editor.SelectionStart = Editor.Text.Length;
Expand All @@ -464,8 +475,11 @@ private void OnSelectionAdapterCancel()
_selectionCancelled = true;
}

private void OnSelectionAdapterCommit()
{
private void OnSelectionAdapterCommit(SelectionAdapter.EventCause cause)
{
if (PreSelectionEventSomeoneHandled(cause, false))
return;

if (ItemsSelector.SelectedItem != null)
{
SelectedItem = ItemsSelector.SelectedItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private void ItemsSelector_PreviewMouseDown(object sender, MouseButtonEventArgs
if (!ItemsSelector.Items.Contains(((FrameworkElement)e.OriginalSource)?.DataContext))
return;
ItemsSelector.SelectedItem = ((FrameworkElement)e.OriginalSource)?.DataContext;
OnSelectionAdapterCommit();
OnSelectionAdapterCommit(SelectionAdapter.EventCause.MouseDown);
e.Handled = true;
}
private void AutoCompleteTextBox_GotFocus(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -411,7 +411,7 @@ private void OnPopupClosed(object sender, EventArgs e)
{
if (!_selectionCancelled)
{
OnSelectionAdapterCommit();
OnSelectionAdapterCommit(SelectionAdapter.EventCause.PopupClosed);
}
}

Expand All @@ -421,19 +421,35 @@ private void OnPopupOpened(object sender, EventArgs e)
ItemsSelector.SelectedItem = SelectedItem;
}

private void OnSelectionAdapterCancel()
{
private void OnSelectionAdapterCancel(SelectionAdapter.EventCause cause)
{
if (PreSelectionEventSomeoneHandled(cause, true))
return;

_isUpdatingText = true;
Editor.Text = SelectedItem == null ? Filter : GetDisplayText(SelectedItem);
Editor.SelectionStart = Editor.Text.Length;
Editor.SelectionLength = 0;
_isUpdatingText = false;
IsDropDownOpen = false;
_selectionCancelled = true;
}

private void OnSelectionAdapterCommit()
}

public event EventHandler<SelectionAdapter.PreSelectionAdapterFinishArgs> PreSelectionAdapterFinish;
private bool PreSelectionEventSomeoneHandled(SelectionAdapter.EventCause cause, bool is_cancel) {
if (PreSelectionAdapterFinish == null)
return false;
var args = new SelectionAdapter.PreSelectionAdapterFinishArgs { cause = cause, is_cancel = is_cancel };
PreSelectionAdapterFinish?.Invoke(this, args);
return args.handled;

}
private void OnSelectionAdapterCommit(SelectionAdapter.EventCause cause)
{
if (PreSelectionEventSomeoneHandled(cause, false))
return;

if (ItemsSelector.SelectedItem != null)
{
SelectedItem = ItemsSelector.SelectedItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace AutoCompleteTextBox.Editors
{
public class SelectionAdapter
{
public class PreSelectionAdapterFinishArgs {
public EventCause cause;
public bool is_cancel;
public bool handled;
}

#region "Fields"
#endregion
Expand All @@ -22,9 +27,10 @@ public SelectionAdapter(Selector selector)

#region "Events"

public delegate void CancelEventHandler();
public enum EventCause { Other, PopupClosed, ItemClicked, EnterPressed, EscapePressed, TabPressed, MouseDown}
public delegate void CancelEventHandler(EventCause cause);

public delegate void CommitEventHandler();
public delegate void CommitEventHandler(EventCause cause);

public delegate void SelectionChangedEventHandler();

Expand Down Expand Up @@ -52,15 +58,15 @@ public void HandleKeyDown(KeyEventArgs key)
DecrementSelection();
break;
case Key.Enter:
Commit?.Invoke();
Commit?.Invoke(EventCause.EnterPressed);

break;
case Key.Escape:
Cancel?.Invoke();
Cancel?.Invoke(EventCause.EscapePressed);

break;
case Key.Tab:
Commit?.Invoke();
Commit?.Invoke(EventCause.TabPressed);

break;
default:
Expand Down Expand Up @@ -104,7 +110,7 @@ private void OnSelectorMouseDown(object sender, MouseButtonEventArgs e)
// and list is scrolled up or down til the end.
if (e.OriginalSource.GetType() != typeof(RepeatButton))
{
Commit?.Invoke();
Commit?.Invoke(EventCause.MouseDown);
e.Handled = true;
}
}
Expand Down

0 comments on commit 965384c

Please sign in to comment.