Permalink
Browse files

Event lookup now uses expressions to define the events instead of str…

…ings
  • Loading branch information...
1 parent 1049aa2 commit 3cf7fadc3f77d9170ffe6722b030fba10399af3d @cwensley cwensley committed Dec 12, 2013
View
31 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<T>(Expression<Action<T>> 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<string> FindTypeEvents(Type type)
List<EventDeclaration> 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;
}
}
}
View
2 Source/Eto/Forms/Application.cs
@@ -75,7 +75,7 @@ public Form MainForm
static Application()
{
- EventLookup.Register(typeof(Application), "OnTerminating", Application.TerminatingEvent);
+ EventLookup.Register<Application>(c => c.OnTerminating(null), Application.TerminatingEvent);
}
public Application() : this(Generator.Detect)
View
28 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<Control>(c => c.OnGotFocus(null), Control.GotFocusEvent);
+ EventLookup.Register<Control>(c => c.OnKeyDown(null), Control.KeyDownEvent);
+ EventLookup.Register<Control>(c => c.OnKeyUp(null), Control.KeyUpEvent);
+ EventLookup.Register<Control>(c => c.OnLostFocus(null), Control.LostFocusEvent);
+ EventLookup.Register<Control>(c => c.OnMouseDoubleClick(null), Control.MouseDoubleClickEvent);
+ EventLookup.Register<Control>(c => c.OnMouseDown(null), Control.MouseDownEvent);
+ EventLookup.Register<Control>(c => c.OnMouseEnter(null), Control.MouseEnterEvent);
+ EventLookup.Register<Control>(c => c.OnMouseLeave(null), Control.MouseLeaveEvent);
+ EventLookup.Register<Control>(c => c.OnMouseMove(null), Control.MouseMoveEvent);
+ EventLookup.Register<Control>(c => c.OnMouseUp(null), Control.MouseUpEvent);
+ EventLookup.Register<Control>(c => c.OnMouseWheel(null), Control.MouseWheelEvent);
+ EventLookup.Register<Control>(c => c.OnShown(null), Control.ShownEvent);
+ EventLookup.Register<Control>(c => c.OnSizeChanged(null), Control.SizeChangedEvent);
+ EventLookup.Register<Control>(c => c.OnTextInput(null), Control.TextInputEvent);
}
protected Control(Generator generator, Type type, bool initialize = true)
View
10 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<Grid>(c => c.OnCellEdited(null), Grid.CellEditedEvent);
+ EventLookup.Register<Grid>(c => c.OnCellEditing(null), Grid.CellEditingEvent);
+ EventLookup.Register<Grid>(c => c.OnCellFormatting(null), Grid.CellFormattingEvent);
+ EventLookup.Register<Grid>(c => c.OnSelectionChanged(null), Grid.SelectionChangedEvent);
+ EventLookup.Register<Grid>(c => c.OnColumnHeaderClick(null), Grid.ColumnHeaderClickEvent);
}
protected Grid(Generator generator, Type type, bool initialize = true)
View
2 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<GridView>(c => c.OnCellClick(null), GridView.CellClickEvent);
}
public GridView()
View
2 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<Scrollable>(c => c.OnScroll(null), Scrollable.ScrollEvent);
}
public Scrollable()
View
2 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<Splitter>(c => c.OnPositionChanged(null), Splitter.PositionChangedEvent);
}
public Splitter()
View
4 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<TextArea>(c => c.OnSelectionChanged(null), TextArea.SelectionChangedEvent);
+ EventLookup.Register<TextArea>(c => c.OnCaretIndexChanged(null), TextArea.CaretIndexChangedEvent);
}
public TextArea()
View
2 Source/Eto/Forms/Controls/TextControl.cs
@@ -17,7 +17,7 @@ public abstract class TextControl : CommonControl
static TextControl()
{
- EventLookup.Register(typeof(TextControl), "OnTextChanged", TextControl.TextChangedEvent);
+ EventLookup.Register<TextControl>(c => c.OnTextChanged(null), TextControl.TextChangedEvent);
}
protected TextControl(Generator g, Type type, bool initialize = true) : base(g, type, initialize)
View
10 Source/Eto/Forms/Controls/TreeGridView.cs
@@ -124,11 +124,11 @@ public virtual void OnSelectedItemChanged(EventArgs e)
static TreeGridView()
{
- EventLookup.Register(typeof(TreeGridView), "OnExpanding", TreeGridView.ExpandingEvent);
- EventLookup.Register(typeof(TreeGridView), "OnExpanded", TreeGridView.ExpandedEvent);
- EventLookup.Register(typeof(TreeGridView), "OnCollapsing", TreeGridView.CollapsingEvent);
- EventLookup.Register(typeof(TreeGridView), "OnCollapsed", TreeGridView.CollapsedEvent);
- EventLookup.Register(typeof(TreeGridView), "OnSelectedItemChanged", TreeGridView.SelectedItemChangedEvent);
+ EventLookup.Register<TreeGridView>(c => c.OnExpanding(null), TreeGridView.ExpandingEvent);
+ EventLookup.Register<TreeGridView>(c => c.OnExpanded(null), TreeGridView.ExpandedEvent);
+ EventLookup.Register<TreeGridView>(c => c.OnCollapsing(null), TreeGridView.CollapsingEvent);
+ EventLookup.Register<TreeGridView>(c => c.OnCollapsed(null), TreeGridView.CollapsedEvent);
+ EventLookup.Register<TreeGridView>(c => c.OnSelectedItemChanged(null), TreeGridView.SelectedItemChangedEvent);
}
public TreeGridView()
View
18 Source/Eto/Forms/Controls/TreeView.cs
@@ -183,15 +183,15 @@ public virtual void OnNodeMouseClick(TreeViewItemEventArgs e)
static TreeView()
{
- EventLookup.Register(typeof(TreeView), "OnActivated", TreeView.ActivatedEvent);
- EventLookup.Register(typeof(TreeView), "OnSelectionChanged", TreeView.SelectionChangedEvent);
- EventLookup.Register(typeof(TreeView), "OnExpanding", TreeView.ExpandingEvent);
- EventLookup.Register(typeof(TreeView), "OnExpanded", TreeView.ExpandedEvent);
- EventLookup.Register(typeof(TreeView), "OnCollapsing", TreeView.CollapsingEvent);
- EventLookup.Register(typeof(TreeView), "OnCollapsed", TreeView.CollapsedEvent);
- EventLookup.Register(typeof(TreeView), "OnLabelEdited", TreeView.LabelEditedEvent);
- EventLookup.Register(typeof(TreeView), "OnLabelEditing", TreeView.LabelEditingEvent);
- EventLookup.Register(typeof(TreeView), "OnNodeMouseClick", TreeView.NodeMouseClickEvent);
+ EventLookup.Register<TreeView>(c => c.OnActivated(null), TreeView.ActivatedEvent);
+ EventLookup.Register<TreeView>(c => c.OnSelectionChanged(null), TreeView.SelectionChangedEvent);
+ EventLookup.Register<TreeView>(c => c.OnExpanding(null), TreeView.ExpandingEvent);
+ EventLookup.Register<TreeView>(c => c.OnExpanded(null), TreeView.ExpandedEvent);
+ EventLookup.Register<TreeView>(c => c.OnCollapsing(null), TreeView.CollapsingEvent);
+ EventLookup.Register<TreeView>(c => c.OnCollapsed(null), TreeView.CollapsedEvent);
+ EventLookup.Register<TreeView>(c => c.OnLabelEdited(null), TreeView.LabelEditedEvent);
+ EventLookup.Register<TreeView>(c => c.OnLabelEditing(null), TreeView.LabelEditingEvent);
+ EventLookup.Register<TreeView>(c => c.OnNodeMouseClick(null), TreeView.NodeMouseClickEvent);
}
public TreeView()
View
10 Source/Eto/Forms/Controls/WebView.cs
@@ -149,11 +149,11 @@ public virtual void OnDocumentTitleChanged(WebViewTitleEventArgs e)
static WebView()
{
- EventLookup.Register(typeof(WebView), "OnNavigated", WebView.NavigatedEvent);
- EventLookup.Register(typeof(WebView), "OnDocumentLoaded", WebView.DocumentLoadedEvent);
- EventLookup.Register(typeof(WebView), "OnDocumentLoading", WebView.DocumentLoadingEvent);
- EventLookup.Register(typeof(WebView), "OnDocumentTitleChanged", WebView.DocumentTitleChangedEvent);
- EventLookup.Register(typeof(WebView), "OnOpenNewWindow", WebView.OpenNewWindowEvent);
+ EventLookup.Register<WebView>(c => c.OnNavigated(null), WebView.NavigatedEvent);
+ EventLookup.Register<WebView>(c => c.OnDocumentLoaded(null), WebView.DocumentLoadedEvent);
+ EventLookup.Register<WebView>(c => c.OnDocumentLoading(null), WebView.DocumentLoadingEvent);
+ EventLookup.Register<WebView>(c => c.OnDocumentTitleChanged(null), WebView.DocumentTitleChangedEvent);
+ EventLookup.Register<WebView>(c => c.OnOpenNewWindow(null), WebView.OpenNewWindowEvent);
}
public WebView()
View
2 Source/Eto/Forms/FontDialog.cs
@@ -27,7 +27,7 @@ public virtual void OnFontChanged(EventArgs e)
static FontDialog()
{
- EventLookup.Register(typeof(FontDialog), "OnFontChanged", FontDialog.FontChangedEvent);
+ EventLookup.Register<FontDialog>(c => c.OnFontChanged(null), FontDialog.FontChangedEvent);
}
public FontDialog()
View
2 Source/Eto/Forms/Menu/MenuActionItem.cs
@@ -51,7 +51,7 @@ protected MenuActionItem(Generator g, Type type, bool initialize = true)
static MenuActionItem()
{
- EventLookup.Register(typeof(MenuActionItem), "OnValidate", MenuActionItem.ValidateEvent);
+ EventLookup.Register<MenuActionItem>(c => c.OnValidate(null), MenuActionItem.ValidateEvent);
}
public string Text
View
6 Source/Eto/Forms/Printing/PrintDocument.cs
@@ -62,9 +62,9 @@ public virtual void OnPrintPage(PrintPageEventArgs e)
static PrintDocument()
{
- EventLookup.Register(typeof(PrintDocument), "OnPrinting", PrintDocument.PrintingEvent);
- EventLookup.Register(typeof(PrintDocument), "OnPrinted", PrintDocument.PrintedEvent);
- EventLookup.Register(typeof(PrintDocument), "OnPrintPage", PrintDocument.PrintPageEvent);
+ EventLookup.Register<PrintDocument>(c => c.OnPrinting(null), PrintDocument.PrintingEvent);
+ EventLookup.Register<PrintDocument>(c => c.OnPrinted(null), PrintDocument.PrintedEvent);
+ EventLookup.Register<PrintDocument>(c => c.OnPrintPage(null), PrintDocument.PrintPageEvent);
}
public PrintDocument()
View
8 Source/Eto/Forms/Window.cs
@@ -72,11 +72,11 @@ public virtual void OnLocationChanged(EventArgs e)
static Window()
{
- EventLookup.Register(typeof(Window), "OnClosed", Window.ClosedEvent);
- EventLookup.Register(typeof(Window), "OnClosing", Window.ClosingEvent);
- EventLookup.Register(typeof(Window), "OnLocationChanged", Window.LocationChangedEvent);
+ EventLookup.Register<Window>(c => c.OnClosed(null), ClosedEvent);
+ EventLookup.Register<Window>(c => c.OnClosing(null), ClosingEvent);
+ EventLookup.Register<Window>(c => c.OnLocationChanged(null), LocationChangedEvent);
#if DESKTOP
- EventLookup.Register(typeof(Window), "OnWindowStateChanged", Window.WindowStateChangedEvent);
+ EventLookup.Register<Window>(c => c.OnWindowStateChanged(null), WindowStateChangedEvent);
#endif
}

0 comments on commit 3cf7fad

Please sign in to comment.