Skip to content

Commit

Permalink
Event lookup now uses expressions to define the events instead of str…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
cwensley committed Dec 12, 2013
1 parent 1049aa2 commit 3cf7fad
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 71 deletions.
31 changes: 14 additions & 17 deletions Source/Eto/EventLookup.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using System.Linq.Expressions;


namespace Eto namespace Eto
{ {
Expand All @@ -14,21 +15,20 @@ static class EventLookup
struct EventDeclaration struct EventDeclaration
{ {
public readonly string Identifier; public readonly string Identifier;
public readonly string MethodName; public readonly MethodInfo Method;
public readonly Type Type;


public EventDeclaration(Type type, string methodName, string identifier) public EventDeclaration(MethodInfo method, string identifier)
{ {
Type = type; Method = method;
MethodName = methodName;
Identifier = identifier; 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); var method = ((MethodCallExpression)expression.Body).Method;
declarations.Add(new EventDeclaration(type, methodName, identifier)); var declarations = GetDeclarations(typeof(T));
declarations.Add(new EventDeclaration(method, identifier));
} }


public static void HookupEvents(InstanceWidget widget) public static void HookupEvents(InstanceWidget widget)
Expand Down Expand Up @@ -70,17 +70,14 @@ static IEnumerable<string> FindTypeEvents(Type type)
List<EventDeclaration> declarations; List<EventDeclaration> declarations;
if (registeredEvents.TryGetValue(current, out 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); var currentItem = item;
if (method != null) if (methods.Any(r => r.GetBaseDefinition() == currentItem.Method))
{ yield return item.Identifier;
var baseMethod = method.GetBaseDefinition();
if (baseMethod != null && baseMethod.DeclaringType == item.Type)
yield return item.Identifier;
}
} }
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/Application.cs
Expand Up @@ -75,7 +75,7 @@ public Form MainForm


static Application() static Application()
{ {
EventLookup.Register(typeof(Application), "OnTerminating", Application.TerminatingEvent); EventLookup.Register<Application>(c => c.OnTerminating(null), Application.TerminatingEvent);
} }


public Application() : this(Generator.Detect) public Application() : this(Generator.Detect)
Expand Down
28 changes: 14 additions & 14 deletions Source/Eto/Forms/Controls/Control.cs
Expand Up @@ -378,20 +378,20 @@ protected internal virtual void OnDataContextChanged(EventArgs e)


static Control() static Control()
{ {
EventLookup.Register(typeof(Control), "OnGotFocus", Control.GotFocusEvent); EventLookup.Register<Control>(c => c.OnGotFocus(null), Control.GotFocusEvent);
EventLookup.Register(typeof(Control), "OnKeyDown", Control.KeyDownEvent); EventLookup.Register<Control>(c => c.OnKeyDown(null), Control.KeyDownEvent);
EventLookup.Register(typeof(Control), "OnKeyUp", Control.KeyUpEvent); EventLookup.Register<Control>(c => c.OnKeyUp(null), Control.KeyUpEvent);
EventLookup.Register(typeof(Control), "OnLostFocus", Control.LostFocusEvent); EventLookup.Register<Control>(c => c.OnLostFocus(null), Control.LostFocusEvent);
EventLookup.Register(typeof(Control), "OnMouseDoubleClick", Control.MouseDoubleClickEvent); EventLookup.Register<Control>(c => c.OnMouseDoubleClick(null), Control.MouseDoubleClickEvent);
EventLookup.Register(typeof(Control), "OnMouseDown", Control.MouseDownEvent); EventLookup.Register<Control>(c => c.OnMouseDown(null), Control.MouseDownEvent);
EventLookup.Register(typeof(Control), "OnMouseEnter", Control.MouseEnterEvent); EventLookup.Register<Control>(c => c.OnMouseEnter(null), Control.MouseEnterEvent);
EventLookup.Register(typeof(Control), "OnMouseLeave", Control.MouseLeaveEvent); EventLookup.Register<Control>(c => c.OnMouseLeave(null), Control.MouseLeaveEvent);
EventLookup.Register(typeof(Control), "OnMouseMove", Control.MouseMoveEvent); EventLookup.Register<Control>(c => c.OnMouseMove(null), Control.MouseMoveEvent);
EventLookup.Register(typeof(Control), "OnMouseUp", Control.MouseUpEvent); EventLookup.Register<Control>(c => c.OnMouseUp(null), Control.MouseUpEvent);
EventLookup.Register(typeof(Control), "OnMouseWheel", Control.MouseWheelEvent); EventLookup.Register<Control>(c => c.OnMouseWheel(null), Control.MouseWheelEvent);
EventLookup.Register(typeof(Control), "OnShown", Control.ShownEvent); EventLookup.Register<Control>(c => c.OnShown(null), Control.ShownEvent);
EventLookup.Register(typeof(Control), "OnSizeChanged", Control.SizeChangedEvent); EventLookup.Register<Control>(c => c.OnSizeChanged(null), Control.SizeChangedEvent);
EventLookup.Register(typeof(Control), "OnTextInput", Control.TextInputEvent); EventLookup.Register<Control>(c => c.OnTextInput(null), Control.TextInputEvent);
} }


protected Control(Generator generator, Type type, bool initialize = true) protected Control(Generator generator, Type type, bool initialize = true)
Expand Down
10 changes: 5 additions & 5 deletions Source/Eto/Forms/Controls/Grid.cs
Expand Up @@ -177,11 +177,11 @@ protected virtual GridViewCellArgs ViewToModel(GridViewCellArgs e)


static Grid() static Grid()
{ {
EventLookup.Register(typeof(Grid), "OnCellEdited", Grid.CellEditedEvent); EventLookup.Register<Grid>(c => c.OnCellEdited(null), Grid.CellEditedEvent);
EventLookup.Register(typeof(Grid), "OnCellEditing", Grid.CellEditingEvent); EventLookup.Register<Grid>(c => c.OnCellEditing(null), Grid.CellEditingEvent);
EventLookup.Register(typeof(Grid), "OnCellFormatting", Grid.CellFormattingEvent); EventLookup.Register<Grid>(c => c.OnCellFormatting(null), Grid.CellFormattingEvent);
EventLookup.Register(typeof(Grid), "OnSelectionChanged", Grid.SelectionChangedEvent); EventLookup.Register<Grid>(c => c.OnSelectionChanged(null), Grid.SelectionChangedEvent);
EventLookup.Register(typeof(Grid), "OnColumnHeaderClick", Grid.ColumnHeaderClickEvent); EventLookup.Register<Grid>(c => c.OnColumnHeaderClick(null), Grid.ColumnHeaderClickEvent);
} }


protected Grid(Generator generator, Type type, bool initialize = true) protected Grid(Generator generator, Type type, bool initialize = true)
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/Controls/GridView.cs
Expand Up @@ -59,7 +59,7 @@ public partial class GridView : Grid


static GridView() static GridView()
{ {
EventLookup.Register(typeof(GridView), "OnCellClick", GridView.CellClickEvent); EventLookup.Register<GridView>(c => c.OnCellClick(null), GridView.CellClickEvent);
} }


public GridView() public GridView()
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/Controls/Scrollable.cs
Expand Up @@ -56,7 +56,7 @@ public virtual void OnScroll(ScrollEventArgs e)


static Scrollable() static Scrollable()
{ {
EventLookup.Register(typeof(Scrollable), "OnScroll", Scrollable.ScrollEvent); EventLookup.Register<Scrollable>(c => c.OnScroll(null), Scrollable.ScrollEvent);
} }


public Scrollable() public Scrollable()
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/Controls/Splitter.cs
Expand Up @@ -74,7 +74,7 @@ public virtual void OnPositionChanged(EventArgs e)


static Splitter() static Splitter()
{ {
EventLookup.Register(typeof(Splitter), "OnPositionChanged", Splitter.PositionChangedEvent); EventLookup.Register<Splitter>(c => c.OnPositionChanged(null), Splitter.PositionChangedEvent);
} }


public Splitter() public Splitter()
Expand Down
4 changes: 2 additions & 2 deletions Source/Eto/Forms/Controls/TextArea.cs
Expand Up @@ -58,8 +58,8 @@ public virtual void OnCaretIndexChanged(EventArgs e)


static TextArea() static TextArea()
{ {
EventLookup.Register(typeof(TextArea), "OnSelectionChanged", TextArea.SelectionChangedEvent); EventLookup.Register<TextArea>(c => c.OnSelectionChanged(null), TextArea.SelectionChangedEvent);
EventLookup.Register(typeof(TextArea), "OnCaretIndexChanged", TextArea.CaretIndexChangedEvent); EventLookup.Register<TextArea>(c => c.OnCaretIndexChanged(null), TextArea.CaretIndexChangedEvent);
} }


public TextArea() public TextArea()
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/Controls/TextControl.cs
Expand Up @@ -17,7 +17,7 @@ public abstract class TextControl : CommonControl


static TextControl() 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) protected TextControl(Generator g, Type type, bool initialize = true) : base(g, type, initialize)
Expand Down
10 changes: 5 additions & 5 deletions Source/Eto/Forms/Controls/TreeGridView.cs
Expand Up @@ -124,11 +124,11 @@ public virtual void OnSelectedItemChanged(EventArgs e)


static TreeGridView() static TreeGridView()
{ {
EventLookup.Register(typeof(TreeGridView), "OnExpanding", TreeGridView.ExpandingEvent); EventLookup.Register<TreeGridView>(c => c.OnExpanding(null), TreeGridView.ExpandingEvent);
EventLookup.Register(typeof(TreeGridView), "OnExpanded", TreeGridView.ExpandedEvent); EventLookup.Register<TreeGridView>(c => c.OnExpanded(null), TreeGridView.ExpandedEvent);
EventLookup.Register(typeof(TreeGridView), "OnCollapsing", TreeGridView.CollapsingEvent); EventLookup.Register<TreeGridView>(c => c.OnCollapsing(null), TreeGridView.CollapsingEvent);
EventLookup.Register(typeof(TreeGridView), "OnCollapsed", TreeGridView.CollapsedEvent); EventLookup.Register<TreeGridView>(c => c.OnCollapsed(null), TreeGridView.CollapsedEvent);
EventLookup.Register(typeof(TreeGridView), "OnSelectedItemChanged", TreeGridView.SelectedItemChangedEvent); EventLookup.Register<TreeGridView>(c => c.OnSelectedItemChanged(null), TreeGridView.SelectedItemChangedEvent);
} }


public TreeGridView() public TreeGridView()
Expand Down
18 changes: 9 additions & 9 deletions Source/Eto/Forms/Controls/TreeView.cs
Expand Up @@ -183,15 +183,15 @@ public virtual void OnNodeMouseClick(TreeViewItemEventArgs e)


static TreeView() static TreeView()
{ {
EventLookup.Register(typeof(TreeView), "OnActivated", TreeView.ActivatedEvent); EventLookup.Register<TreeView>(c => c.OnActivated(null), TreeView.ActivatedEvent);
EventLookup.Register(typeof(TreeView), "OnSelectionChanged", TreeView.SelectionChangedEvent); EventLookup.Register<TreeView>(c => c.OnSelectionChanged(null), TreeView.SelectionChangedEvent);
EventLookup.Register(typeof(TreeView), "OnExpanding", TreeView.ExpandingEvent); EventLookup.Register<TreeView>(c => c.OnExpanding(null), TreeView.ExpandingEvent);
EventLookup.Register(typeof(TreeView), "OnExpanded", TreeView.ExpandedEvent); EventLookup.Register<TreeView>(c => c.OnExpanded(null), TreeView.ExpandedEvent);
EventLookup.Register(typeof(TreeView), "OnCollapsing", TreeView.CollapsingEvent); EventLookup.Register<TreeView>(c => c.OnCollapsing(null), TreeView.CollapsingEvent);
EventLookup.Register(typeof(TreeView), "OnCollapsed", TreeView.CollapsedEvent); EventLookup.Register<TreeView>(c => c.OnCollapsed(null), TreeView.CollapsedEvent);
EventLookup.Register(typeof(TreeView), "OnLabelEdited", TreeView.LabelEditedEvent); EventLookup.Register<TreeView>(c => c.OnLabelEdited(null), TreeView.LabelEditedEvent);
EventLookup.Register(typeof(TreeView), "OnLabelEditing", TreeView.LabelEditingEvent); EventLookup.Register<TreeView>(c => c.OnLabelEditing(null), TreeView.LabelEditingEvent);
EventLookup.Register(typeof(TreeView), "OnNodeMouseClick", TreeView.NodeMouseClickEvent); EventLookup.Register<TreeView>(c => c.OnNodeMouseClick(null), TreeView.NodeMouseClickEvent);
} }


public TreeView() public TreeView()
Expand Down
10 changes: 5 additions & 5 deletions Source/Eto/Forms/Controls/WebView.cs
Expand Up @@ -149,11 +149,11 @@ public virtual void OnDocumentTitleChanged(WebViewTitleEventArgs e)


static WebView() static WebView()
{ {
EventLookup.Register(typeof(WebView), "OnNavigated", WebView.NavigatedEvent); EventLookup.Register<WebView>(c => c.OnNavigated(null), WebView.NavigatedEvent);
EventLookup.Register(typeof(WebView), "OnDocumentLoaded", WebView.DocumentLoadedEvent); EventLookup.Register<WebView>(c => c.OnDocumentLoaded(null), WebView.DocumentLoadedEvent);
EventLookup.Register(typeof(WebView), "OnDocumentLoading", WebView.DocumentLoadingEvent); EventLookup.Register<WebView>(c => c.OnDocumentLoading(null), WebView.DocumentLoadingEvent);
EventLookup.Register(typeof(WebView), "OnDocumentTitleChanged", WebView.DocumentTitleChangedEvent); EventLookup.Register<WebView>(c => c.OnDocumentTitleChanged(null), WebView.DocumentTitleChangedEvent);
EventLookup.Register(typeof(WebView), "OnOpenNewWindow", WebView.OpenNewWindowEvent); EventLookup.Register<WebView>(c => c.OnOpenNewWindow(null), WebView.OpenNewWindowEvent);
} }


public WebView() public WebView()
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/FontDialog.cs
Expand Up @@ -27,7 +27,7 @@ public virtual void OnFontChanged(EventArgs e)


static FontDialog() static FontDialog()
{ {
EventLookup.Register(typeof(FontDialog), "OnFontChanged", FontDialog.FontChangedEvent); EventLookup.Register<FontDialog>(c => c.OnFontChanged(null), FontDialog.FontChangedEvent);
} }


public FontDialog() public FontDialog()
Expand Down
2 changes: 1 addition & 1 deletion Source/Eto/Forms/Menu/MenuActionItem.cs
Expand Up @@ -51,7 +51,7 @@ protected MenuActionItem(Generator g, Type type, bool initialize = true)


static MenuActionItem() static MenuActionItem()
{ {
EventLookup.Register(typeof(MenuActionItem), "OnValidate", MenuActionItem.ValidateEvent); EventLookup.Register<MenuActionItem>(c => c.OnValidate(null), MenuActionItem.ValidateEvent);
} }


public string Text public string Text
Expand Down
6 changes: 3 additions & 3 deletions Source/Eto/Forms/Printing/PrintDocument.cs
Expand Up @@ -62,9 +62,9 @@ public virtual void OnPrintPage(PrintPageEventArgs e)


static PrintDocument() static PrintDocument()
{ {
EventLookup.Register(typeof(PrintDocument), "OnPrinting", PrintDocument.PrintingEvent); EventLookup.Register<PrintDocument>(c => c.OnPrinting(null), PrintDocument.PrintingEvent);
EventLookup.Register(typeof(PrintDocument), "OnPrinted", PrintDocument.PrintedEvent); EventLookup.Register<PrintDocument>(c => c.OnPrinted(null), PrintDocument.PrintedEvent);
EventLookup.Register(typeof(PrintDocument), "OnPrintPage", PrintDocument.PrintPageEvent); EventLookup.Register<PrintDocument>(c => c.OnPrintPage(null), PrintDocument.PrintPageEvent);
} }


public PrintDocument() public PrintDocument()
Expand Down
8 changes: 4 additions & 4 deletions Source/Eto/Forms/Window.cs
Expand Up @@ -72,11 +72,11 @@ public virtual void OnLocationChanged(EventArgs e)


static Window() static Window()
{ {
EventLookup.Register(typeof(Window), "OnClosed", Window.ClosedEvent); EventLookup.Register<Window>(c => c.OnClosed(null), ClosedEvent);
EventLookup.Register(typeof(Window), "OnClosing", Window.ClosingEvent); EventLookup.Register<Window>(c => c.OnClosing(null), ClosingEvent);
EventLookup.Register(typeof(Window), "OnLocationChanged", Window.LocationChangedEvent); EventLookup.Register<Window>(c => c.OnLocationChanged(null), LocationChangedEvent);
#if DESKTOP #if DESKTOP
EventLookup.Register(typeof(Window), "OnWindowStateChanged", Window.WindowStateChangedEvent); EventLookup.Register<Window>(c => c.OnWindowStateChanged(null), WindowStateChangedEvent);
#endif #endif
} }


Expand Down

0 comments on commit 3cf7fad

Please sign in to comment.