From 54c20fe48a17d42bad4ccb473c5fd6f64426d7b5 Mon Sep 17 00:00:00 2001 From: Eilon Lipton Date: Wed, 5 Feb 2020 15:21:04 -0800 Subject: [PATCH] Clear event handler only if it's the current event handler value Previously, when an event handler was reset it would unconditionally clear the event. Instead, it should check if the value that is attempted to be clear is actually the current value. If not (say, because it's stale), it's ignored. If it is, it's cleared. Fixes #80 --- src/BlinForms.Framework/Controls/Button.cs | 2 +- src/BlinForms.Framework/Controls/CheckBox.cs | 4 ++-- src/BlinForms.Framework/Controls/TextBox.cs | 2 +- .../NativeComponentRenderer.cs | 6 +++--- .../Elements/Handlers/BaseShellItemHandler.cs | 4 ++-- .../Elements/Handlers/ButtonHandler.cs | 6 +++--- .../Elements/Handlers/CheckBoxHandler.cs | 2 +- .../Elements/Handlers/ElementHandler.cs | 2 +- .../Elements/Handlers/EntryHandler.cs | 4 ++-- .../Elements/Handlers/EventRegistration.cs | 4 ++-- .../Elements/Handlers/ImageButtonHandler.cs | 6 +++--- .../Elements/Handlers/MenuItemHandler.cs | 2 +- .../Elements/Handlers/ModalContainerHandler.cs | 2 +- .../Elements/Handlers/ShellHandler.cs | 4 ++-- .../Elements/Handlers/SliderHandler.cs | 6 +++--- .../Elements/Handlers/StepperHandler.cs | 2 +- .../Elements/Handlers/SwitchHandler.cs | 2 +- .../Elements/Handlers/VisualElementHandler.cs | 6 +++--- 18 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/BlinForms.Framework/Controls/Button.cs b/src/BlinForms.Framework/Controls/Button.cs index 4d15c281..64d8009e 100644 --- a/src/BlinForms.Framework/Controls/Button.cs +++ b/src/BlinForms.Framework/Controls/Button.cs @@ -59,7 +59,7 @@ public void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, Text = (string)attributeValue; break; case "onclick": - Renderer.RegisterEvent(attributeEventHandlerId, () => ClickEventHandlerId = 0); + Renderer.RegisterEvent(attributeEventHandlerId, id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; }); ClickEventHandlerId = attributeEventHandlerId; break; default: diff --git a/src/BlinForms.Framework/Controls/CheckBox.cs b/src/BlinForms.Framework/Controls/CheckBox.cs index dfa63eec..5f384695 100644 --- a/src/BlinForms.Framework/Controls/CheckBox.cs +++ b/src/BlinForms.Framework/Controls/CheckBox.cs @@ -102,11 +102,11 @@ public void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, ThreeState = AttributeHelper.GetBool(attributeValue); break; case "oncheckedchanged": - Renderer.RegisterEvent(attributeEventHandlerId, () => CheckedChangedEventHandlerId = 0); + Renderer.RegisterEvent(attributeEventHandlerId, id => { if (CheckedChangedEventHandlerId == id) CheckedChangedEventHandlerId = 0; }); CheckedChangedEventHandlerId = attributeEventHandlerId; break; case "oncheckstatechanged": - Renderer.RegisterEvent(attributeEventHandlerId, () => CheckStateChangedEventHandlerId = 0); + Renderer.RegisterEvent(attributeEventHandlerId, id => { if (CheckStateChangedEventHandlerId == id) CheckStateChangedEventHandlerId = 0; }); CheckStateChangedEventHandlerId = attributeEventHandlerId; break; default: diff --git a/src/BlinForms.Framework/Controls/TextBox.cs b/src/BlinForms.Framework/Controls/TextBox.cs index f39c113b..815489d5 100644 --- a/src/BlinForms.Framework/Controls/TextBox.cs +++ b/src/BlinForms.Framework/Controls/TextBox.cs @@ -96,7 +96,7 @@ public void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, ScrollBars = (ScrollBars)AttributeHelper.GetInt(attributeValue); break; case "ontextchanged": - Renderer.RegisterEvent(attributeEventHandlerId, () => TextChangedEventHandlerId = 0); + Renderer.RegisterEvent(attributeEventHandlerId, id => { if (TextChangedEventHandlerId == id) TextChangedEventHandlerId = 0; }); TextChangedEventHandlerId = attributeEventHandlerId; break; default: diff --git a/src/Microsoft.MobileBlazorBindings.Core/NativeComponentRenderer.cs b/src/Microsoft.MobileBlazorBindings.Core/NativeComponentRenderer.cs index 3aa5175b..ee033194 100644 --- a/src/Microsoft.MobileBlazorBindings.Core/NativeComponentRenderer.cs +++ b/src/Microsoft.MobileBlazorBindings.Core/NativeComponentRenderer.cs @@ -15,7 +15,7 @@ public abstract class NativeComponentRenderer : Renderer { private readonly Dictionary _componentIdToAdapter = new Dictionary(); private ElementManager _elementManager; - private readonly Dictionary _eventRegistrations = new Dictionary(); + private readonly Dictionary> _eventRegistrations = new Dictionary>(); public NativeComponentRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory) @@ -102,7 +102,7 @@ protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) return Task.CompletedTask; } - public void RegisterEvent(ulong eventHandlerId, Action unregisterCallback) + public void RegisterEvent(ulong eventHandlerId, Action unregisterCallback) { if (eventHandlerId == 0) { @@ -121,7 +121,7 @@ private void DisposeEvent(ulong eventHandlerId) { throw new InvalidOperationException($"Attempting to dispose unknown event handler id '{eventHandlerId}'."); } - unregisterCallback(); + unregisterCallback(eventHandlerId); } internal NativeComponentAdapter CreateAdapterForChildComponent(IElementHandler physicalParent, int componentId) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/BaseShellItemHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/BaseShellItemHandler.cs index 81ca95f7..e07a875e 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/BaseShellItemHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/BaseShellItemHandler.cs @@ -12,7 +12,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onappearing", setId: id => AppearingEventHandlerId = id, - clearId: () => AppearingEventHandlerId = 0); + clearId: id => { if (AppearingEventHandlerId == id) AppearingEventHandlerId = 0; }); BaseShellItemControl.Appearing += (s, e) => { if (AppearingEventHandlerId != default) @@ -23,7 +23,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "ondisappearing", setId: id => DisappearingEventHandlerId = id, - clearId: () => DisappearingEventHandlerId = 0); + clearId: id => { if (DisappearingEventHandlerId == id) DisappearingEventHandlerId = 0; }); BaseShellItemControl.Disappearing += (s, e) => { if (DisappearingEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ButtonHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ButtonHandler.cs index 67e009d2..84e44fb0 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ButtonHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ButtonHandler.cs @@ -12,7 +12,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onclick", setId: id => ClickEventHandlerId = id, - clearId: () => ClickEventHandlerId = 0); + clearId: id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; }); ButtonControl.Clicked += (s, e) => { if (ClickEventHandlerId != default) @@ -24,7 +24,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onpress", setId: id => PressEventHandlerId = id, - clearId: () => PressEventHandlerId = 0); + clearId: id => { if (PressEventHandlerId == id) PressEventHandlerId = 0; }); ButtonControl.Pressed += (s, e) => { if (PressEventHandlerId != default) @@ -36,7 +36,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onrelease", setId: id => ReleaseEventHandlerId = id, - clearId: () => ReleaseEventHandlerId = 0); + clearId: id => { if (ReleaseEventHandlerId == id) ReleaseEventHandlerId = 0; }); ButtonControl.Released += (s, e) => { if (ReleaseEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/CheckBoxHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/CheckBoxHandler.cs index 31a2cd3f..8cce319d 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/CheckBoxHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/CheckBoxHandler.cs @@ -13,7 +13,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onischeckedchanged", setId: id => IsCheckedChangedEventHandlerId = id, - clearId: () => IsCheckedChangedEventHandlerId = 0); + clearId: id => { if (IsCheckedChangedEventHandlerId == id) IsCheckedChangedEventHandlerId = 0; }); CheckBoxControl.CheckedChanged += (s, e) => { if (IsCheckedChangedEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ElementHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ElementHandler.cs index 85fbf21b..5a804c82 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ElementHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ElementHandler.cs @@ -16,7 +16,7 @@ public ElementHandler(NativeComponentRenderer renderer, XF.Element elementContro ElementControl = elementControl ?? throw new ArgumentNullException(nameof(elementControl)); } - protected void RegisterEvent(string eventName, Action setId, Action clearId) + protected void RegisterEvent(string eventName, Action setId, Action clearId) { RegisteredEvents[eventName] = new EventRegistration(eventName, setId, clearId); } diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EntryHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EntryHandler.cs index 9153557b..c0144996 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EntryHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EntryHandler.cs @@ -13,7 +13,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "oncompleted", setId: id => CompletedEventHandlerId = id, - clearId: () => CompletedEventHandlerId = 0); + clearId: id => { if (CompletedEventHandlerId == id) CompletedEventHandlerId = 0; }); EntryControl.Completed += (s, e) => { if (CompletedEventHandlerId != default) @@ -24,7 +24,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "ontextchanged", setId: id => TextChangedEventHandlerId = id, - clearId: () => TextChangedEventHandlerId = 0); + clearId: id => { if (TextChangedEventHandlerId == id) TextChangedEventHandlerId = 0; }); EntryControl.TextChanged += (s, e) => { if (TextChangedEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EventRegistration.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EventRegistration.cs index 07fb0728..a161a6c7 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EventRegistration.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/EventRegistration.cs @@ -7,7 +7,7 @@ namespace Microsoft.MobileBlazorBindings.Elements.Handlers { internal class EventRegistration { - public EventRegistration(string eventName, Action setId, Action clearId) + public EventRegistration(string eventName, Action setId, Action clearId) { if (string.IsNullOrEmpty(eventName)) { @@ -21,6 +21,6 @@ public EventRegistration(string eventName, Action setId, Action clearId) public string EventName { get; } public Action SetId { get; } - public Action ClearId { get; } + public Action ClearId { get; } } } diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ImageButtonHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ImageButtonHandler.cs index 8141af0e..b6ce9117 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ImageButtonHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ImageButtonHandler.cs @@ -12,7 +12,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onclick", setId: id => ClickEventHandlerId = id, - clearId: () => ClickEventHandlerId = 0); + clearId: id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; }); ImageButtonControl.Clicked += (s, e) => { if (ClickEventHandlerId != default) @@ -24,7 +24,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onpress", setId: id => PressEventHandlerId = id, - clearId: () => PressEventHandlerId = 0); + clearId: id => { if (PressEventHandlerId == id) PressEventHandlerId = 0; }); ImageButtonControl.Pressed += (s, e) => { if (PressEventHandlerId != default) @@ -36,7 +36,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onrelease", setId: id => ReleaseEventHandlerId = id, - clearId: () => ReleaseEventHandlerId = 0); + clearId: id => { if (ReleaseEventHandlerId == id) ReleaseEventHandlerId = 0; }); ImageButtonControl.Released += (s, e) => { if (ReleaseEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/MenuItemHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/MenuItemHandler.cs index 23e9650b..5c161bfa 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/MenuItemHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/MenuItemHandler.cs @@ -12,7 +12,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onclick", setId: id => ClickEventHandlerId = id, - clearId: () => ClickEventHandlerId = 0); + clearId: id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; }); MenuItemControl.Clicked += (s, e) => { if (ClickEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ModalContainerHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ModalContainerHandler.cs index b168b10a..581a5e4d 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ModalContainerHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ModalContainerHandler.cs @@ -59,7 +59,7 @@ public void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, } break; case "onclosed": - Renderer.RegisterEvent(attributeEventHandlerId, () => ClosedEventHandlerId = 0); + Renderer.RegisterEvent(attributeEventHandlerId, id => { if (ClosedEventHandlerId == id) ClosedEventHandlerId = 0; }); ClosedEventHandlerId = attributeEventHandlerId; break; default: diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ShellHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ShellHandler.cs index 30372fa5..fa8d5e14 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ShellHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ShellHandler.cs @@ -26,7 +26,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onnavigated", setId: id => NavigatedEventHandlerId = id, - clearId: () => NavigatedEventHandlerId = 0); + clearId: id => { if (NavigatedEventHandlerId == id) NavigatedEventHandlerId = 0; }); ShellControl.Navigated += (s, e) => { if (NavigatedEventHandlerId != default) @@ -37,7 +37,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onnavigating", setId: id => NavigatingEventHandlerId = id, - clearId: () => NavigatingEventHandlerId = 0); + clearId: id => { if (NavigatingEventHandlerId == id) NavigatingEventHandlerId = 0; }); ShellControl.Navigating += (s, e) => { if (NavigatingEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SliderHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SliderHandler.cs index 51317485..b34b67c1 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SliderHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SliderHandler.cs @@ -13,7 +13,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "ondragcompleted", setId: id => DragCompletedEventHandlerId = id, - clearId: () => DragCompletedEventHandlerId = 0); + clearId: id => { if (DragCompletedEventHandlerId == id) DragCompletedEventHandlerId = 0; }); SliderControl.DragCompleted += (s, e) => { if (DragCompletedEventHandlerId != default) @@ -25,7 +25,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "ondragstarted", setId: id => DragStartedEventHandlerId = id, - clearId: () => DragStartedEventHandlerId = 0); + clearId: id => { if (DragStartedEventHandlerId == id) DragStartedEventHandlerId = 0; }); SliderControl.DragStarted += (s, e) => { if (DragStartedEventHandlerId != default) @@ -37,7 +37,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onvaluechanged", setId: id => ValueChangedEventHandlerId = id, - clearId: () => ValueChangedEventHandlerId = 0); + clearId: id => { if (ValueChangedEventHandlerId == id) ValueChangedEventHandlerId = 0; }); SliderControl.ValueChanged += (s, e) => { if (ValueChangedEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/StepperHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/StepperHandler.cs index 9c650d97..438c0510 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/StepperHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/StepperHandler.cs @@ -13,7 +13,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onvaluechanged", setId: id => ValueChangedEventHandlerId = id, - clearId: () => ValueChangedEventHandlerId = 0); + clearId: id => { if (ValueChangedEventHandlerId == id) ValueChangedEventHandlerId = 0; }); StepperControl.ValueChanged += (s, e) => { if (ValueChangedEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SwitchHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SwitchHandler.cs index 774764d3..c09b53d3 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SwitchHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/SwitchHandler.cs @@ -13,7 +13,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onistoggledchanged", setId: id => IsToggledChangedEventHandlerId = id, - clearId: () => IsToggledChangedEventHandlerId = 0); + clearId: id => { if (IsToggledChangedEventHandlerId == id) IsToggledChangedEventHandlerId = 0; }); SwitchControl.Toggled += (s, e) => { if (IsToggledChangedEventHandlerId != default) diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/VisualElementHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/VisualElementHandler.cs index dd52a9f6..a32b512b 100644 --- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/VisualElementHandler.cs +++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/VisualElementHandler.cs @@ -12,7 +12,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onfocused", setId: id => FocusedEventHandlerId = id, - clearId: () => FocusedEventHandlerId = 0); + clearId: id => { if (FocusedEventHandlerId == id) FocusedEventHandlerId = 0; }); VisualElementControl.Focused += (s, e) => { if (FocusedEventHandlerId != default) @@ -23,7 +23,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onsizechanged", setId: id => SizeChangedEventHandlerId = id, - clearId: () => SizeChangedEventHandlerId = 0); + clearId: id => { if (SizeChangedEventHandlerId == id) SizeChangedEventHandlerId = 0; }); VisualElementControl.SizeChanged += (s, e) => { if (SizeChangedEventHandlerId != default) @@ -34,7 +34,7 @@ partial void Initialize(NativeComponentRenderer renderer) RegisterEvent( eventName: "onunfocused", setId: id => UnfocusedEventHandlerId = id, - clearId: () => UnfocusedEventHandlerId = 0); + clearId: id => { if (UnfocusedEventHandlerId == id) UnfocusedEventHandlerId = 0; }); VisualElementControl.Unfocused += (s, e) => { if (UnfocusedEventHandlerId != default)