From 3cf7fadc3f77d9170ffe6722b030fba10399af3d Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Wed, 11 Dec 2013 18:57:27 -0800 Subject: [PATCH] Event lookup now uses expressions to define the events instead of strings --- Source/Eto/EventLookup.cs | 31 ++++++++++------------ Source/Eto/Forms/Application.cs | 2 +- Source/Eto/Forms/Controls/Control.cs | 28 +++++++++---------- Source/Eto/Forms/Controls/Grid.cs | 10 +++---- Source/Eto/Forms/Controls/GridView.cs | 2 +- Source/Eto/Forms/Controls/Scrollable.cs | 2 +- Source/Eto/Forms/Controls/Splitter.cs | 2 +- Source/Eto/Forms/Controls/TextArea.cs | 4 +-- Source/Eto/Forms/Controls/TextControl.cs | 2 +- Source/Eto/Forms/Controls/TreeGridView.cs | 10 +++---- Source/Eto/Forms/Controls/TreeView.cs | 18 ++++++------- Source/Eto/Forms/Controls/WebView.cs | 10 +++---- Source/Eto/Forms/FontDialog.cs | 2 +- Source/Eto/Forms/Menu/MenuActionItem.cs | 2 +- Source/Eto/Forms/Printing/PrintDocument.cs | 6 ++--- Source/Eto/Forms/Window.cs | 8 +++--- 16 files changed, 68 insertions(+), 71 deletions(-) diff --git a/Source/Eto/EventLookup.cs b/Source/Eto/EventLookup.cs index 1babde4b74..ecfc27c4b0 100644 --- a/Source/Eto/EventLookup.cs +++ b/Source/Eto/EventLookup.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Reflection; using System.Linq; +using System.Linq.Expressions; namespace Eto { @@ -14,21 +15,20 @@ static class EventLookup struct EventDeclaration { public readonly string Identifier; - public readonly string MethodName; - public readonly Type Type; + public readonly MethodInfo Method; - public EventDeclaration(Type type, string methodName, string identifier) + public EventDeclaration(MethodInfo method, string identifier) { - Type = type; - MethodName = methodName; + Method = method; Identifier = identifier; } } - public static void Register(Type type, string methodName, string identifier) + public static void Register(Expression> expression, string identifier) { - var declarations = GetDeclarations(type); - declarations.Add(new EventDeclaration(type, methodName, identifier)); + var method = ((MethodCallExpression)expression.Body).Method; + var declarations = GetDeclarations(typeof(T)); + declarations.Add(new EventDeclaration(method, identifier)); } public static void HookupEvents(InstanceWidget widget) @@ -70,17 +70,14 @@ static IEnumerable FindTypeEvents(Type type) List declarations; if (registeredEvents.TryGetValue(current, out declarations)) { - foreach (var item in declarations) + foreach (var externalType in externalTypes) { - foreach (var externalType in externalTypes) + var methods = externalType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + foreach (var item in declarations) { - var method = externalType.GetMethod(item.MethodName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - if (method != null) - { - var baseMethod = method.GetBaseDefinition(); - if (baseMethod != null && baseMethod.DeclaringType == item.Type) - yield return item.Identifier; - } + var currentItem = item; + if (methods.Any(r => r.GetBaseDefinition() == currentItem.Method)) + yield return item.Identifier; } } } diff --git a/Source/Eto/Forms/Application.cs b/Source/Eto/Forms/Application.cs index 904b65733d..1dd828182b 100644 --- a/Source/Eto/Forms/Application.cs +++ b/Source/Eto/Forms/Application.cs @@ -75,7 +75,7 @@ public Form MainForm static Application() { - EventLookup.Register(typeof(Application), "OnTerminating", Application.TerminatingEvent); + EventLookup.Register(c => c.OnTerminating(null), Application.TerminatingEvent); } public Application() : this(Generator.Detect) diff --git a/Source/Eto/Forms/Controls/Control.cs b/Source/Eto/Forms/Controls/Control.cs index 790281fbeb..44d5077cd6 100644 --- a/Source/Eto/Forms/Controls/Control.cs +++ b/Source/Eto/Forms/Controls/Control.cs @@ -378,20 +378,20 @@ protected internal virtual void OnDataContextChanged(EventArgs e) static Control() { - EventLookup.Register(typeof(Control), "OnGotFocus", Control.GotFocusEvent); - EventLookup.Register(typeof(Control), "OnKeyDown", Control.KeyDownEvent); - EventLookup.Register(typeof(Control), "OnKeyUp", Control.KeyUpEvent); - EventLookup.Register(typeof(Control), "OnLostFocus", Control.LostFocusEvent); - EventLookup.Register(typeof(Control), "OnMouseDoubleClick", Control.MouseDoubleClickEvent); - EventLookup.Register(typeof(Control), "OnMouseDown", Control.MouseDownEvent); - EventLookup.Register(typeof(Control), "OnMouseEnter", Control.MouseEnterEvent); - EventLookup.Register(typeof(Control), "OnMouseLeave", Control.MouseLeaveEvent); - EventLookup.Register(typeof(Control), "OnMouseMove", Control.MouseMoveEvent); - EventLookup.Register(typeof(Control), "OnMouseUp", Control.MouseUpEvent); - EventLookup.Register(typeof(Control), "OnMouseWheel", Control.MouseWheelEvent); - EventLookup.Register(typeof(Control), "OnShown", Control.ShownEvent); - EventLookup.Register(typeof(Control), "OnSizeChanged", Control.SizeChangedEvent); - EventLookup.Register(typeof(Control), "OnTextInput", Control.TextInputEvent); + EventLookup.Register(c => c.OnGotFocus(null), Control.GotFocusEvent); + EventLookup.Register(c => c.OnKeyDown(null), Control.KeyDownEvent); + EventLookup.Register(c => c.OnKeyUp(null), Control.KeyUpEvent); + EventLookup.Register(c => c.OnLostFocus(null), Control.LostFocusEvent); + EventLookup.Register(c => c.OnMouseDoubleClick(null), Control.MouseDoubleClickEvent); + EventLookup.Register(c => c.OnMouseDown(null), Control.MouseDownEvent); + EventLookup.Register(c => c.OnMouseEnter(null), Control.MouseEnterEvent); + EventLookup.Register(c => c.OnMouseLeave(null), Control.MouseLeaveEvent); + EventLookup.Register(c => c.OnMouseMove(null), Control.MouseMoveEvent); + EventLookup.Register(c => c.OnMouseUp(null), Control.MouseUpEvent); + EventLookup.Register(c => c.OnMouseWheel(null), Control.MouseWheelEvent); + EventLookup.Register(c => c.OnShown(null), Control.ShownEvent); + EventLookup.Register(c => c.OnSizeChanged(null), Control.SizeChangedEvent); + EventLookup.Register(c => c.OnTextInput(null), Control.TextInputEvent); } protected Control(Generator generator, Type type, bool initialize = true) diff --git a/Source/Eto/Forms/Controls/Grid.cs b/Source/Eto/Forms/Controls/Grid.cs index 1f68dc9132..025fd5a633 100644 --- a/Source/Eto/Forms/Controls/Grid.cs +++ b/Source/Eto/Forms/Controls/Grid.cs @@ -177,11 +177,11 @@ protected virtual GridViewCellArgs ViewToModel(GridViewCellArgs e) static Grid() { - EventLookup.Register(typeof(Grid), "OnCellEdited", Grid.CellEditedEvent); - EventLookup.Register(typeof(Grid), "OnCellEditing", Grid.CellEditingEvent); - EventLookup.Register(typeof(Grid), "OnCellFormatting", Grid.CellFormattingEvent); - EventLookup.Register(typeof(Grid), "OnSelectionChanged", Grid.SelectionChangedEvent); - EventLookup.Register(typeof(Grid), "OnColumnHeaderClick", Grid.ColumnHeaderClickEvent); + EventLookup.Register(c => c.OnCellEdited(null), Grid.CellEditedEvent); + EventLookup.Register(c => c.OnCellEditing(null), Grid.CellEditingEvent); + EventLookup.Register(c => c.OnCellFormatting(null), Grid.CellFormattingEvent); + EventLookup.Register(c => c.OnSelectionChanged(null), Grid.SelectionChangedEvent); + EventLookup.Register(c => c.OnColumnHeaderClick(null), Grid.ColumnHeaderClickEvent); } protected Grid(Generator generator, Type type, bool initialize = true) diff --git a/Source/Eto/Forms/Controls/GridView.cs b/Source/Eto/Forms/Controls/GridView.cs index 67376230b6..b1e3201225 100644 --- a/Source/Eto/Forms/Controls/GridView.cs +++ b/Source/Eto/Forms/Controls/GridView.cs @@ -59,7 +59,7 @@ public partial class GridView : Grid static GridView() { - EventLookup.Register(typeof(GridView), "OnCellClick", GridView.CellClickEvent); + EventLookup.Register(c => c.OnCellClick(null), GridView.CellClickEvent); } public GridView() diff --git a/Source/Eto/Forms/Controls/Scrollable.cs b/Source/Eto/Forms/Controls/Scrollable.cs index 8d78435688..03de829d89 100644 --- a/Source/Eto/Forms/Controls/Scrollable.cs +++ b/Source/Eto/Forms/Controls/Scrollable.cs @@ -56,7 +56,7 @@ public virtual void OnScroll(ScrollEventArgs e) static Scrollable() { - EventLookup.Register(typeof(Scrollable), "OnScroll", Scrollable.ScrollEvent); + EventLookup.Register(c => c.OnScroll(null), Scrollable.ScrollEvent); } public Scrollable() diff --git a/Source/Eto/Forms/Controls/Splitter.cs b/Source/Eto/Forms/Controls/Splitter.cs index dd01ab6782..5447bfc2c9 100644 --- a/Source/Eto/Forms/Controls/Splitter.cs +++ b/Source/Eto/Forms/Controls/Splitter.cs @@ -74,7 +74,7 @@ public virtual void OnPositionChanged(EventArgs e) static Splitter() { - EventLookup.Register(typeof(Splitter), "OnPositionChanged", Splitter.PositionChangedEvent); + EventLookup.Register(c => c.OnPositionChanged(null), Splitter.PositionChangedEvent); } public Splitter() diff --git a/Source/Eto/Forms/Controls/TextArea.cs b/Source/Eto/Forms/Controls/TextArea.cs index b0f782ebfc..830ce3e19c 100644 --- a/Source/Eto/Forms/Controls/TextArea.cs +++ b/Source/Eto/Forms/Controls/TextArea.cs @@ -58,8 +58,8 @@ public virtual void OnCaretIndexChanged(EventArgs e) static TextArea() { - EventLookup.Register(typeof(TextArea), "OnSelectionChanged", TextArea.SelectionChangedEvent); - EventLookup.Register(typeof(TextArea), "OnCaretIndexChanged", TextArea.CaretIndexChangedEvent); + EventLookup.Register