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

Conversation

cwensley
Copy link
Member

@cwensley cwensley commented Jan 20, 2017

This is an experimental feature, trying to simplify the creation of new controls. In most cases this may even eliminate the need to declare an IHandler interface for the control.

This will probably be done for the 3.x series (which is a ways off)

Benefits:

  1. If done properly, we can make it so only a single handler instance is created for ALL child objects instead of one per object reducing the number of instantiated objects substantially
  2. Simplifies not only the declaration of the control but could also simplify platform implementations by eliminating the need for the Connector in GTK (due to using static methods to wire up event handlers)
  3. Removes the need for the (excessive) amount of code required for ICallback/Callback implementations
  4. Removes the need for EventManager.Register() in the static constructors
  5. Removes the need for IHandler in cases where there are no methods and only properties and/or events.
  6. Change is good

Drawbacks:

  1. Completely breaks any custom handlers, custom controls, or styles for native controls done through the handler
  2. If more properties are stored in the Widget.Properties, accessing them may be slower (though this can be mitigated by pushing more functionality into subclasses of the native controls)
  3. Change is bad

Incomplete:

  • Need to figure out how to style native controls, e.g. Style.Add<SomeHandler>(h => h.Control.NativeProperty = blah); won't work
  • Other stuff I haven't thought of yet

@cwensley
Copy link
Member Author

This allows us to change something like this (e.g. the Stepper control):

[Handler(typeof(IHandler))]
public class Stepper : Control
{
	new IHandler Handler { get { return (IHandler)bqase.Handler; } }
	
	static Stepper()
	{
		RegisterEvent<Stepper>(c => c.OnStep(null));
	}

	public const string StepEvent = "Stepper.Step";

	public event EventHandler<StepperEventArgs> Step
	{
		add { Properties.AddHandlerEvent(StepEvent, value); }
		remove { Properties.RemoveEvent(StepEvent, value); }
	}

	protected void OnStep(StepperEventArgs e)
	{
		Properties.TriggerEvent(StepEvent, this, e);
	}

	[DefaultValue(StepperValidDirections.Both)] 
	public StepperValidDirections ValidDirection
	{
		get { return Handler.ValidDirection; }
		set { Handler.ValidDirection = value; }
	}

	ICallback callback = new Callback();

	protected override object GetCallback() => callback;

	public new interface ICallback : Control.ICallback
	{
		void OnStep(Stepper widget, StepperEventArgs e);
	}

	protected new class Callback : Control.Callback, ICallback
	{
		public void OnStep(Stepper widget, StepperEventArgs e)
		{
			widget.Platform.Invoke(() => widget.OnStep(e));
		}
	}

	public new interface IHandler : Control.IHandler
	{
		StepperValidDirections ValidDirection { get; set; }
	}
}

To this:

public class Stepper : Control
{
	public static DependencyEvent<Stepper, StepperEventArgs> StepEvent = new DependencyEvent<Stepper, StepperEventArgs>((c, e) => c.OnStep(e));

	public event EventHandler<StepperEventArgs> Step
	{
		add { Properties.AddEvent(StepEvent, value); }
		remove { Properties.RemoveEvent(StepEvent, value); }
	}

	protected void OnStep(StepperEventArgs e) => Properties.TriggerEvent(StepEvent, this, e);


	public static DependencyProperty<Stepper, StepperValidDirections> ValidDirectionProperty = new DependencyProperty<Stepper, StepperValidDirections>(StepperValidDirections.Both);

	[DefaultValue(StepperValidDirections.Both)] 
	public StepperValidDirections ValidDirection
	{
		get { return Properties.Get(ValidDirectionProperty); }
		set { Properties.Set(ValidDirectionProperty, value); }
	}
}

@cwensley cwensley added this to the 3.0 milestone Jan 20, 2017
@cwensley cwensley marked this pull request as draft June 11, 2020 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant