Skip to content

Commit

Permalink
Clear event handler only if it's the current event handler value
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Eilon committed Feb 5, 2020
1 parent ffaed69 commit 54c20fe
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/BlinForms.Framework/Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/BlinForms.Framework/Controls/CheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/BlinForms.Framework/Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class NativeComponentRenderer : Renderer
{
private readonly Dictionary<int, NativeComponentAdapter> _componentIdToAdapter = new Dictionary<int, NativeComponentAdapter>();
private ElementManager _elementManager;
private readonly Dictionary<ulong, Action> _eventRegistrations = new Dictionary<ulong, Action>();
private readonly Dictionary<ulong, Action<ulong>> _eventRegistrations = new Dictionary<ulong, Action<ulong>>();


public NativeComponentRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
Expand Down Expand Up @@ -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<ulong> unregisterCallback)
{
if (eventHandlerId == 0)
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class BaseShellItemHandler : NavigableElementHandler
RegisterEvent(
eventName: "onappearing",
setId: id => AppearingEventHandlerId = id,
clearId: () => AppearingEventHandlerId = 0);
clearId: id => { if (AppearingEventHandlerId == id) AppearingEventHandlerId = 0; });
BaseShellItemControl.Appearing += (s, e) =>
{
if (AppearingEventHandlerId != default)
Expand All @@ -23,7 +23,7 @@ public partial class BaseShellItemHandler : NavigableElementHandler
RegisterEvent(
eventName: "ondisappearing",
setId: id => DisappearingEventHandlerId = id,
clearId: () => DisappearingEventHandlerId = 0);
clearId: id => { if (DisappearingEventHandlerId == id) DisappearingEventHandlerId = 0; });
BaseShellItemControl.Disappearing += (s, e) =>
{
if (DisappearingEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class ButtonHandler : ViewHandler
RegisterEvent(
eventName: "onclick",
setId: id => ClickEventHandlerId = id,
clearId: () => ClickEventHandlerId = 0);
clearId: id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; });
ButtonControl.Clicked += (s, e) =>
{
if (ClickEventHandlerId != default)
Expand All @@ -24,7 +24,7 @@ public partial class ButtonHandler : ViewHandler
RegisterEvent(
eventName: "onpress",
setId: id => PressEventHandlerId = id,
clearId: () => PressEventHandlerId = 0);
clearId: id => { if (PressEventHandlerId == id) PressEventHandlerId = 0; });
ButtonControl.Pressed += (s, e) =>
{
if (PressEventHandlerId != default)
Expand All @@ -36,7 +36,7 @@ public partial class ButtonHandler : ViewHandler
RegisterEvent(
eventName: "onrelease",
setId: id => ReleaseEventHandlerId = id,
clearId: () => ReleaseEventHandlerId = 0);
clearId: id => { if (ReleaseEventHandlerId == id) ReleaseEventHandlerId = 0; });
ButtonControl.Released += (s, e) =>
{
if (ReleaseEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class CheckBoxHandler : ViewHandler
RegisterEvent(
eventName: "onischeckedchanged",
setId: id => IsCheckedChangedEventHandlerId = id,
clearId: () => IsCheckedChangedEventHandlerId = 0);
clearId: id => { if (IsCheckedChangedEventHandlerId == id) IsCheckedChangedEventHandlerId = 0; });
CheckBoxControl.CheckedChanged += (s, e) =>
{
if (IsCheckedChangedEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ElementHandler(NativeComponentRenderer renderer, XF.Element elementContro
ElementControl = elementControl ?? throw new ArgumentNullException(nameof(elementControl));
}

protected void RegisterEvent(string eventName, Action<ulong> setId, Action clearId)
protected void RegisterEvent(string eventName, Action<ulong> setId, Action<ulong> clearId)
{
RegisteredEvents[eventName] = new EventRegistration(eventName, setId, clearId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class EntryHandler : InputViewHandler
RegisterEvent(
eventName: "oncompleted",
setId: id => CompletedEventHandlerId = id,
clearId: () => CompletedEventHandlerId = 0);
clearId: id => { if (CompletedEventHandlerId == id) CompletedEventHandlerId = 0; });
EntryControl.Completed += (s, e) =>
{
if (CompletedEventHandlerId != default)
Expand All @@ -24,7 +24,7 @@ public partial class EntryHandler : InputViewHandler
RegisterEvent(
eventName: "ontextchanged",
setId: id => TextChangedEventHandlerId = id,
clearId: () => TextChangedEventHandlerId = 0);
clearId: id => { if (TextChangedEventHandlerId == id) TextChangedEventHandlerId = 0; });
EntryControl.TextChanged += (s, e) =>
{
if (TextChangedEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.MobileBlazorBindings.Elements.Handlers
{
internal class EventRegistration
{
public EventRegistration(string eventName, Action<ulong> setId, Action clearId)
public EventRegistration(string eventName, Action<ulong> setId, Action<ulong> clearId)
{
if (string.IsNullOrEmpty(eventName))
{
Expand All @@ -21,6 +21,6 @@ public EventRegistration(string eventName, Action<ulong> setId, Action clearId)

public string EventName { get; }
public Action<ulong> SetId { get; }
public Action ClearId { get; }
public Action<ulong> ClearId { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class ImageButtonHandler : ViewHandler
RegisterEvent(
eventName: "onclick",
setId: id => ClickEventHandlerId = id,
clearId: () => ClickEventHandlerId = 0);
clearId: id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; });
ImageButtonControl.Clicked += (s, e) =>
{
if (ClickEventHandlerId != default)
Expand All @@ -24,7 +24,7 @@ public partial class ImageButtonHandler : ViewHandler
RegisterEvent(
eventName: "onpress",
setId: id => PressEventHandlerId = id,
clearId: () => PressEventHandlerId = 0);
clearId: id => { if (PressEventHandlerId == id) PressEventHandlerId = 0; });
ImageButtonControl.Pressed += (s, e) =>
{
if (PressEventHandlerId != default)
Expand All @@ -36,7 +36,7 @@ public partial class ImageButtonHandler : ViewHandler
RegisterEvent(
eventName: "onrelease",
setId: id => ReleaseEventHandlerId = id,
clearId: () => ReleaseEventHandlerId = 0);
clearId: id => { if (ReleaseEventHandlerId == id) ReleaseEventHandlerId = 0; });
ImageButtonControl.Released += (s, e) =>
{
if (ReleaseEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class MenuItemHandler : BaseMenuItemHandler
RegisterEvent(
eventName: "onclick",
setId: id => ClickEventHandlerId = id,
clearId: () => ClickEventHandlerId = 0);
clearId: id => { if (ClickEventHandlerId == id) ClickEventHandlerId = 0; });
MenuItemControl.Clicked += (s, e) =>
{
if (ClickEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class ShellHandler : PageHandler
RegisterEvent(
eventName: "onnavigated",
setId: id => NavigatedEventHandlerId = id,
clearId: () => NavigatedEventHandlerId = 0);
clearId: id => { if (NavigatedEventHandlerId == id) NavigatedEventHandlerId = 0; });
ShellControl.Navigated += (s, e) =>
{
if (NavigatedEventHandlerId != default)
Expand All @@ -37,7 +37,7 @@ public partial class ShellHandler : PageHandler
RegisterEvent(
eventName: "onnavigating",
setId: id => NavigatingEventHandlerId = id,
clearId: () => NavigatingEventHandlerId = 0);
clearId: id => { if (NavigatingEventHandlerId == id) NavigatingEventHandlerId = 0; });
ShellControl.Navigating += (s, e) =>
{
if (NavigatingEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class SliderHandler : ViewHandler
RegisterEvent(
eventName: "ondragcompleted",
setId: id => DragCompletedEventHandlerId = id,
clearId: () => DragCompletedEventHandlerId = 0);
clearId: id => { if (DragCompletedEventHandlerId == id) DragCompletedEventHandlerId = 0; });
SliderControl.DragCompleted += (s, e) =>
{
if (DragCompletedEventHandlerId != default)
Expand All @@ -25,7 +25,7 @@ public partial class SliderHandler : ViewHandler
RegisterEvent(
eventName: "ondragstarted",
setId: id => DragStartedEventHandlerId = id,
clearId: () => DragStartedEventHandlerId = 0);
clearId: id => { if (DragStartedEventHandlerId == id) DragStartedEventHandlerId = 0; });
SliderControl.DragStarted += (s, e) =>
{
if (DragStartedEventHandlerId != default)
Expand All @@ -37,7 +37,7 @@ public partial class SliderHandler : ViewHandler
RegisterEvent(
eventName: "onvaluechanged",
setId: id => ValueChangedEventHandlerId = id,
clearId: () => ValueChangedEventHandlerId = 0);
clearId: id => { if (ValueChangedEventHandlerId == id) ValueChangedEventHandlerId = 0; });
SliderControl.ValueChanged += (s, e) =>
{
if (ValueChangedEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class StepperHandler : ViewHandler
RegisterEvent(
eventName: "onvaluechanged",
setId: id => ValueChangedEventHandlerId = id,
clearId: () => ValueChangedEventHandlerId = 0);
clearId: id => { if (ValueChangedEventHandlerId == id) ValueChangedEventHandlerId = 0; });
StepperControl.ValueChanged += (s, e) =>
{
if (ValueChangedEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class SwitchHandler : ViewHandler
RegisterEvent(
eventName: "onistoggledchanged",
setId: id => IsToggledChangedEventHandlerId = id,
clearId: () => IsToggledChangedEventHandlerId = 0);
clearId: id => { if (IsToggledChangedEventHandlerId == id) IsToggledChangedEventHandlerId = 0; });
SwitchControl.Toggled += (s, e) =>
{
if (IsToggledChangedEventHandlerId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class VisualElementHandler : NavigableElementHandler
RegisterEvent(
eventName: "onfocused",
setId: id => FocusedEventHandlerId = id,
clearId: () => FocusedEventHandlerId = 0);
clearId: id => { if (FocusedEventHandlerId == id) FocusedEventHandlerId = 0; });
VisualElementControl.Focused += (s, e) =>
{
if (FocusedEventHandlerId != default)
Expand All @@ -23,7 +23,7 @@ public partial class VisualElementHandler : NavigableElementHandler
RegisterEvent(
eventName: "onsizechanged",
setId: id => SizeChangedEventHandlerId = id,
clearId: () => SizeChangedEventHandlerId = 0);
clearId: id => { if (SizeChangedEventHandlerId == id) SizeChangedEventHandlerId = 0; });
VisualElementControl.SizeChanged += (s, e) =>
{
if (SizeChangedEventHandlerId != default)
Expand All @@ -34,7 +34,7 @@ public partial class VisualElementHandler : NavigableElementHandler
RegisterEvent(
eventName: "onunfocused",
setId: id => UnfocusedEventHandlerId = id,
clearId: () => UnfocusedEventHandlerId = 0);
clearId: id => { if (UnfocusedEventHandlerId == id) UnfocusedEventHandlerId = 0; });
VisualElementControl.Unfocused += (s, e) =>
{
if (UnfocusedEventHandlerId != default)
Expand Down

0 comments on commit 54c20fe

Please sign in to comment.