From f27c6975d8ce6ecbae40b8ee83beb1d3cb0eafcd Mon Sep 17 00:00:00 2001 From: bustedbunny Date: Tue, 21 Feb 2023 22:44:09 +0300 Subject: [PATCH] Command binding now binds through Clickable if possible (for example if VE is button). This brings support for keyboard/controllers navigation clicks --- Runtime/Binding/CommandBinding.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Runtime/Binding/CommandBinding.cs b/Runtime/Binding/CommandBinding.cs index a3ae65b..82ff5dd 100644 --- a/Runtime/Binding/CommandBinding.cs +++ b/Runtime/Binding/CommandBinding.cs @@ -46,12 +46,21 @@ public CommandBinding(VisualElement element, object boundObject, string key) } - element.RegisterCallback(OnClick); + if (element is Button button) + { + button.clicked += OnClick; + } + else + { + element.RegisterCallback(OnClick); + } + Command.CanExecuteChanged += SetCanExecute; SetCanExecute(null, null); } + private static object ParseArgument(string key) { if (bool.TryParse(key, out var boolResult)) return boolResult; @@ -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) { @@ -71,7 +81,14 @@ private void SetCanExecute(object sender, EventArgs eventArgs) public void Unbind() { Command.CanExecuteChanged -= SetCanExecute; - Element.UnregisterCallback(OnClick); + if (Element is Button button) + { + button.clicked -= OnClick; + } + else + { + Element.UnregisterCallback(OnClick); + } } } } \ No newline at end of file