Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate method to hook up properties & events #671

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions Source/Eto.Mac/Forms/Controls/StepperHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@

namespace Eto.Mac.Forms.Controls
{
public class MacViewHandler2<TWidget, TControl> : WidgetHandler2<TWidget, TControl>, IMacControlHandler2
where TWidget : Control
where TControl : NSView, new()
{
public virtual NSView GetContainerControl(Widget widget)
{
return GetControl((TWidget)widget);
}

public SizeF GetPreferredSize(Widget widget, SizeF availableSize)
{
return SizeF.Empty;
}
}

public class StepperHandler : MacControl<NSStepper, Stepper, Stepper.ICallback>, Stepper.IHandler
{
public class EtoStepper : NSStepper, IMacControl
Expand All @@ -49,17 +64,9 @@ public StepperHandler()
};
}

static object ValidDirection_Key = new object();

public StepperValidDirections ValidDirection
{
get { return Widget.Properties.Get(ValidDirection_Key, StepperValidDirections.Both); }
set { Widget.Properties.Set(ValidDirection_Key, value, UpdateState, StepperValidDirections.Both); }
}

void UpdateState()
{
switch (ValidDirection)
switch (Widget.ValidDirection)
{
case StepperValidDirections.Both:
Control.ValueWraps = true;
Expand Down Expand Up @@ -94,12 +101,12 @@ public override bool Enabled

void SetStepperEnabled()
{
Control.Enabled = Enabled && ValidDirection != StepperValidDirections.None;
Control.Enabled = Enabled && Widget.ValidDirection != StepperValidDirections.None;
}

StepperDirection? GetDirection()
{
switch (ValidDirection)
switch (Widget.ValidDirection)
{
case StepperValidDirections.Both:
var dir = Control.IntValue == 1 ? StepperDirection.Up : StepperDirection.Down;
Expand All @@ -123,22 +130,42 @@ void SetStepperEnabled()
return null;
}

public override void AttachEvent(string id)
public override void Initialize(Stepper widget)
{
switch (id)
base.Initialize(widget);
GetControl(widget).SizeToFit();
}

public override Action<Stepper, object> SetProperty(object property)
{
if (property == Stepper.ValidDirectionProperty)
return SetValidDirection;

return base.SetProperty(property);
}

static void SetValidDirection(Stepper c, object value)
{
(c.Handler as StepperHandler)?.UpdateState();
}

public override Action<Stepper> GetEvent(object evt)
{
if (evt == Stepper.StepEvent)
return AttachStepEvent;

return base.GetEvent(evt);
}

static void AttachStepEvent(Stepper c)
{
var h = c.Handler as StepperHandler;
h.Control.Activated += (sender, e) =>
{
case Stepper.StepEvent:
Control.Activated += (sender, e) =>
{
var dir = GetDirection();
if (dir != null)
Callback.OnStep(Widget, new StepperEventArgs(dir.Value));
};
break;
default:
base.AttachEvent(id);
break;
}
var dir = h.GetDirection();
if (dir != null)
Stepper.StepEvent.Raise(c, new StepperEventArgs(dir.Value));
};
}
}
}
}
7 changes: 7 additions & 0 deletions Source/Eto.Mac/Forms/MacBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

namespace Eto.Mac.Forms
{
public interface IMacControlHandler2
{
NSView GetContainerControl(Widget widget);

SizeF GetPreferredSize(Widget widget, SizeF availableSize);
}

public interface IMacControlHandler
{
NSView ContainerControl { get; }
Expand Down
17 changes: 17 additions & 0 deletions Source/Eto.Mac/Forms/MacControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static SizeF GetPreferredSize(this Control control, SizeF availableSize)
{
return mh.GetPreferredSize(availableSize);
}
var mh2 = control.GetMacControl2();
if (mh2 != null)
return mh2.GetPreferredSize(control, availableSize);

var c = control.ControlObject as NSControl;
if (c != null)
Expand Down Expand Up @@ -89,13 +92,27 @@ public static IMacControlHandler GetMacControl(this Control control)
return child == null ? null : child.GetMacControl();
}

public static IMacControlHandler2 GetMacControl2(this Control control)
{
if (control == null)
return null;
var container = control.Handler as IMacControlHandler2;
if (container != null)
return container;
var child = control.ControlObject as Control;
return child == null ? null : child.GetMacControl2();
}

public static NSView GetContainerView(this Widget control)
{
if (control == null)
return null;
var containerHandler = control.Handler as IMacControlHandler;
if (containerHandler != null)
return containerHandler.ContainerControl;
var containerHandler2 = control.Handler as IMacControlHandler2;
if (containerHandler2 != null)
return containerHandler2.GetContainerControl(control);
var childControl = control.ControlObject as Control;
if (childControl != null)
return childControl.GetContainerView();
Expand Down
2 changes: 2 additions & 0 deletions Source/Eto.Mac/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public static void AddTo(Eto.Platform p)
// General
p.Add<EtoEnvironment.IHandler>(() => new EtoEnvironmentHandler());
p.Add<Thread.IHandler>(() => new ThreadHandler());

p.AddHandler<Stepper>(() => new StepperHandler());
}

public override IDisposable ThreadStart()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Eto.Forms;

namespace Eto.Test.Sections.Controls
{
[Section("Controls", typeof(Stepper))]
Expand Down
1 change: 1 addition & 0 deletions Source/Eto.WinRT/Eto.WinRT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<TargetPlatformVersion>8.1</TargetPlatformVersion>
<MinimumVisualStudioVersion>12</MinimumVisualStudioVersion>
<TargetFrameworkVersion />
<BaseIntermediateOutputPath>..\..\BuildOutput\obj\Eto.WinRT</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
1 change: 1 addition & 0 deletions Source/Eto/Eto - pcl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@
<Compile Include="Forms\Controls\FontPicker.cs" />
<Compile Include="Forms\ThemedControls\ThemedFontPickerHandler.cs" />
<Compile Include="Forms\OpenWithDialog.cs" />
<Compile Include="Handler2.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\LICENSE">
Expand Down
36 changes: 24 additions & 12 deletions Source/Eto/EventLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using Eto.Forms;

namespace Eto
{
static class EventLookup
{
static readonly Dictionary<Type, List<EventDeclaration>> registeredEvents = new Dictionary<Type, List<EventDeclaration>>();
static readonly Assembly etoAssembly = typeof(EventLookup).GetAssembly();
static readonly Dictionary<Type, string[]> externalEvents = new Dictionary<Type, string[]>();
static readonly Dictionary<Type, object[]> externalEvents = new Dictionary<Type, object[]>();

struct EventDeclaration
{
public readonly string Identifier;
public readonly object Identifier;
public readonly MethodInfo Method;

public EventDeclaration(MethodInfo method, string identifier)
public EventDeclaration(MethodInfo method, object identifier)
{
Method = method;
Identifier = identifier;
}
}

public static void Register<T, TArgs>(Expression<Action<T, TArgs>> expression, object identifier)
{
var method = ((MethodCallExpression)expression.Body).Method;
var declarations = GetDeclarations(typeof(T));
declarations.Add(new EventDeclaration(method, identifier));
}

public static void Register<T>(Expression<Action<T>> expression, string identifier)
{
var method = ((MethodCallExpression)expression.Body).Method;
Expand All @@ -38,14 +46,18 @@ public static void HookupEvents(Widget widget)
if (type.GetAssembly() == etoAssembly)
return;

var handler2 = widget.Handler as IHandler2;
var handler = widget.Handler as Widget.IHandler;
if (handler != null)

var events = GetEvents(type);
for (int i = 0; i < events.Length; i++)
{
var ids = GetEvents(type);
for (int i = 0; i < ids.Length; i++)
{
handler.HandleEvent(ids[i], true);
}
var evt = events[i];
var id = evt as string;
if (id != null)
handler?.HandleEvent(id, true);
else
handler2?.AttachEvent(widget, evt);
}
}

Expand All @@ -59,9 +71,9 @@ public static bool IsDefault(Widget widget, string identifier)
return Array.IndexOf(events, identifier) >= 0;
}

static string[] GetEvents(Type type)
static object[] GetEvents(Type type)
{
string[] events;
object[] events;
if (!externalEvents.TryGetValue(type, out events))
{
events = FindTypeEvents(type).Distinct().ToArray();
Expand All @@ -70,7 +82,7 @@ static string[] GetEvents(Type type)
return events;
}

static IEnumerable<string> FindTypeEvents(Type type)
static IEnumerable<object> FindTypeEvents(Type type)
{
var externalTypes = new List<Type>();
var current = type;
Expand Down
Loading