Ashera provides attribute extension using factory registration.
Let us take an example on how to build a plugin which extends a View to add gradient background support. You need to build a cross platform plugin which is is required to work on iOS, Android, Desktop app (SWT) and browser You find out in the internet on how to draw gradient in each of the platform. You come up with the following snippets:
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
//create a new gradient color
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(0f);
//apply the button background to newly created drawable gradient
btn.setBackground(gd);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor blackColor].CGColor];
[view.layer insertSublayer:gradient atIndex:0];
gc.setBackgrouind(display,getSystemColor(SWT.COLOR_BLUE));
gc.fillGradientRectangle(5,5,90,45,false);
#grad {
background-image: linear-gradient(red, yellow);
}
For us to develop a very simple gradient plugin, we require 2 colors to to passed onto the view. i.e gradientStart and gradientEnd. This plugin we are going to develop adds an attribute linearGradientBackground to widget which needs to have comma separated color.
linearGradientBackground="#000,#fff"
The following are the steps required to create the plugin:
cordova plugin add CustomWidgetExtension
.WidgetFactory.registerAttributableFor("View", new CustomWidgetExtensionViewImpl());
The following example shows the Linear Gradient attribute in action: