Skip to content

Commit

Permalink
Command binding now binds through Clickable if possible (for example …
Browse files Browse the repository at this point in the history
…if VE is button). This brings support for keyboard/controllers navigation clicks
  • Loading branch information
bustedbunny committed Feb 21, 2023
1 parent bc0a5cb commit f27c697
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Runtime/Binding/CommandBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ public CommandBinding(VisualElement element, object boundObject, string key)
}


element.RegisterCallback<ClickEvent>(OnClick);
if (element is Button button)
{
button.clicked += OnClick;
}
else
{
element.RegisterCallback<ClickEvent>(OnClick);
}

Command.CanExecuteChanged += SetCanExecute;

SetCanExecute(null, null);
}


private static object ParseArgument(string key)
{
if (bool.TryParse(key, out var boolResult)) return boolResult;
Expand All @@ -60,7 +69,8 @@ private static object ParseArgument(string key)
return key;
}

private void OnClick(ClickEvent evt) => Command.Execute(_argument);
private void OnClick() => Command.Execute(_argument);
private void OnClick(ClickEvent evt) => OnClick();

private void SetCanExecute(object sender, EventArgs eventArgs)
{
Expand All @@ -71,7 +81,14 @@ private void SetCanExecute(object sender, EventArgs eventArgs)
public void Unbind()
{
Command.CanExecuteChanged -= SetCanExecute;
Element.UnregisterCallback<ClickEvent>(OnClick);
if (Element is Button button)
{
button.clicked -= OnClick;
}
else
{
Element.UnregisterCallback<ClickEvent>(OnClick);
}
}
}
}

0 comments on commit f27c697

Please sign in to comment.