480 likes | 717 Views
Flex 4 and Components. Speaker Name. Who am I?. Senior Technical Architect @ EffectiveUI Blogger for InsideRIA Adobe Community Expert in Flex Lead developer on some cool projects for cool clients (just like you.). Who are you?. Software Developers, perhaps Designers
E N D
Flex 4 and Components Speaker Name
Who am I? • Senior Technical Architect @ EffectiveUI • Blogger for InsideRIA • Adobe Community Expert in Flex • Lead developer on some cool projects for cool clients (just like you.)
Who are you? • Software Developers, perhaps Designers At CFUnited? Pfffft. Yeah right! • Some experience in Flex 3? • Interested in building better interfaces • Relatively good looking Compared to those .NET developers, we’re frickin’ movie stars in here. Am I right? Seriously.
What will we talk about today? • Flex 4 component types • Flex 4 component life-cycle • New features of Flex 4 that help w / components Credit: I’m using some stuff from Ely Greenfield’s MAX talk last year
Components: What are they? • Contained bits of functionality • Controls displayed on the screen • Containers for those controls • Highly Visual • Examples: Button, ComboBox, List
Components: what’s broken? • In Halo (Flex 3), components were rigid • Hard to extend • “Monkey patching” to make small changes to component view • Example: HTML text in the panel title bar
Flex 4 Components: Issues to address • Separate data, logic and behavior from visuals, states, animation and layout • Create component-logic with no assumption about appearance • Create component-view without digging into the logic
Flex 4: fundamental changes to Designer Developer Workflow Designer Developer Workflow
New Designer / Developer Workflow OMG POWNIES!! Designer stuff Developer stuff OMG POWNIES!!
V M C MVC in a button component label:String, selected:Boolean text field, icon, background mouse handlers, selection logic
V V C M Flex 3 Components (Halo) Skin (background) Component
V C M Flex 4 Components (Spark) Skin (entire view) Component
Flex 4 Components and Skins (it’s the “hue” transform in Fireworks.)
Skin Parts • Component specifies optional/required skin parts as metadata [SkinPart(required=”true”)] public var labelElement:SimpleText; • Skin implements skin parts • Exception thrown if required part is not found • Component should set data on skin parts in the setters AND component.partAdded(); method
Skin States • Defined in the component using metadata [SkinState(“up”)] [SkinState(“down”)] • Current state is passed to the skin from the component • All states are required. An exception will be thrown if a state is not implemented in the skin
Anatomy of a Button ButtonBase Button ButtonSkin IconButtonSkin Icon Button
Example: icon buttons • Halo • Extend viewIconForPhase and duplicate logic for new icon ~100 lines • Extend layoutContents to position/size new icon and position/size existing components so nothing overlaps • Requires ActionScript knowledge and understanding of Button component • 2-4 hours, depending on the developer • Spark • Copy MXML from default Button Skin • Add two tags to create two icons • Adjust position/size of icons and label • All MXML! • 1 hour at most!
Example: Brightkite Cards • Everyone loves Twitter – try Brightkite • It’s like Twitter, but creates community around places • You don’t have to be honest so no one will steal your information. Calm down.
Flex 4 Components are built on Flex 3 Components UIComponent Skinnable Component Flex 4 Component Model (Spark) Flex 3 Component Model (Halo) Mrinal Wadhwa gave me the idea for this image and has a great set of slides for a similar talk on his blog.
Flex 4 Component Lifecycle • What is it? • The way the framework interacts with every Flex component • A set of methods the framework calls to instantiate, control, and destroy components • Methods that make the most of every frame
Flex 4 Component Lifecycle • Elastic Racetrack • The frame-rate of your application varies if there’s too much processing or rendering Image courtesy of Ted Patrick
Flex 4 Component Lifecycle Construction Configuration Birth Addition Initialization Invalidation Life Validation Interaction Removal Death Garbage Collection
Birth: Construction • Calls your constructor • Can access properties and methods of the class, but note that children have not been created yet!! • Must be public, no return type • Call super(); • Not used often in Flex
Birth: Configuration • The process of assigning values to properties on objects • Containers must not expect children to be added yet • For optimizaton, make setters fast and DEFER the real work until validation (more later) <local:SampleChildproperty1="value!"/>
Birth: Attachment • Adding a component to the display elements list myComponent.addElement(); • Component Lifecycle stalls here until attachment really happens
Attachment: Element Lists • Every component on the screen lives in a Display List • Components are added to display lists • Graphical Elements (rectangles, circles, text, etc.) are DRAWN into Components • Flex 4 lets us treat graphical elements as first class citizens (like components) • The Elements List abstracts the components created to draw graphics for us
Birth: Initialization • Create children, do first validation pass Skin Component Constructor(), … commitProperties() loadSkin() Skin.hostComponent = this addChild(skin) findSkinParts() partAdded() getCurrentSkinState() Measure()/updateDisplayList()/updateComplete Constructor() Initialize() currentState = … commitProperties()/Measure()/updateDisplayList()/updateComplete
Life • After initialization, we’re ready to go • The component is added to the screen and available to users • User interaction should cause changes in components – set invalidation flags • When the next Render event occurs, validation methods will be called
Life: Deferment • Deferment is the core concept of the component lifecycle! • Put off doing the “real work” until the appropriate time • Make the most of every frame • Example: resizing a component
Life: Deferment • The invalidation process: • When a property is set, retain the value on a private variable • Set a “dirty” flag • Invalidate the Component • The validation process: • When the framework calls validation methods, update your component accordingly • Example: set “Text” on the TextGraphicElement class
Life: Invalidation • Invalidation Methods • Called to “invalidate” a component, but not do any work on it • invalidateProperties() • invalidateDisplayList() • invalidateSize() • invalidateSkinState() • Just sets skinChanged=true; • Calls invalidateProperties();
Life: Validation • “Do the work” that invalidation requires • Move things, add things, remove things, etc. • The goal: don’t duplicate or re-do things in the same frame • validateProperties() • updateDisplayList() • measure()
Death: Removal and GC • Any object not on an element’s list with no active references is eligible for destruction, garbage collection • The flash player does not reduce it’s footprint, though memory can be recycled
Composite Components • 98% of the components you make will likely be composites of other components • This is good, and is the point of having a robust component library like Flex to work from.
Big Changes in Flex 4 • Separate Layout from Component Logic • FXG - declarative graphics • Enhanced Text renderer • Updated Styles • Updated States
Flex 4 Layout • Layouts are now applied to Flex groups • Allows containment logic and layout logic to be separated • “List” is now a very powerful and useful container • Layout Example
FXG • MXML Graphics library providing rich primitive support • Simple Shape primitives • Complex Paths • Full range of fills and strokes • Masking, filters, blend modes, and more. • Color and 2D transformations • Integrated text, bitmaps
FXG <s:Group id="group1”> <s:Group> <s:Path winding="nonZero" data="M 0 246 L 0 0 L 239 0 L 239 246 L 0 246 Z”> <s:fill> <s:LinearGradient x="119.5" y="246" scaleX="246" rotation="-90"> <s:GradientEntry color="0x333333" ratio="0"/> <s:GradientEntry color="0x4d4d4d" ratio="1"/> </s:LinearGradient> </s:fill> </s:Path> <s:Path winding="nonZero” data="M 239 1 L 239 ..."> <s:fill> <s:SolidColor color="0x666666"/> </s:fill> </s:Path> </s:Group> <s:Path winding="nonZero” data="M 61.998..." id="path1"> <s:fill> <s:SolidColor color="0x262626"/> </s:fill> </s:Path> </s:Group>
Ellipse Line Path Bitmap Image GraphicElement Graphics and Text • 1st class citizens • Dynamic • Easy to animate with effects, states, transitions, code • Freely mix and match with Spark components • GraphicElement • Focused on performance • optimized for fast rendering, low overhead • One DisplayObject shared by many GraphicElements • Important to understand: Components are DisplayObjects GraphicElements draw into DisplayObjects
Enhanced Text Renderer • FTE: New low level text engine (player 10) • Stands for “Flash Text Engine” • TLF: New text layout library built on top of FTE. • “Text Library for Flash?” • Might also stand for “Totally Like Fireballs”,“Typical Lightning Flamboyance” or “Tumultuous Latex Fusion”
Enhanced Text Renderer • Benefits: • Soft hyphens (we know you’ve all been missing those!) • Baseline control (e.g., superscripts and subscripts) • Right, center, and decimal tabs • Vertical justification • Multiple columns • Ligatures, capitalization styles, digit styles • Integrated Rendering of device fonts (a.k.a. I can fade and rotate my text FINALLY) • Bi-Directional text • etc.
Styles in Flex 4 • New components have overlapping class names • New with Flex 4: qualified CSS selectors <Style> @namespace s "library://ns.adobe.com/flex/spark”; s|Button {} </Style> • Required on all type identifiers in CSS • New syntax is global to an application – even in MXML 2006 documents • Namespaces are resolved at compile time – at runtime, types are fully qualified classnames • (i.e., s|Button above becomes spark.components.Button)
Advanced Styles in Flex 4 • Multiple Class Selectors: <Button id=“upButton” styleName=“default tiny” /> • ID Selectors: #upButton { fontSize: 14 } • Descendant Selectors: s|Scrollbar #upButton { baseColor: #FF8888 } • Pseudo Selectors: s|Scrollbar #upButton:over { baseColor: #8888FF }
States: what was broken? • States in Flex 3 and below suck • Overly verbose • Really awkward to use - AddChild children, RemoveChild children, etc. • Hard to optimize • We never ever ever use them. Ever. • Okay, sometimes we use them, but we hates them.
States in Flex 4 • States in Flex 4 are awesome • Declare your states with a ‘State’ tag • Describe ‘alternate views’ of your markup • Change values, bindings, event handlers • Include and exclude components as easily as setting visibility • Unscoped entries specify the ‘default’ for all states • States can be grouped into State Groups - see Flex documentation for more info • <fx:Reparent> tag to move children between states • WARNING: Use NEW STATES in 2009 documents – 2006 still supports legacy states.
States in Flex 4 <states> <State name=”login"/> <State name=”register"/> <State name=”someOtherState” stateGroups=”group1”/> </states> <Group> <TextBox text=“username:” /> <TextInput /> <TextBox text=“password:” /> <TextInput /> <Button label=“new user?” /> <Checkbox includeIn=“register” label=“agree to terms” /> <Button label=“log in” label.register=“sign up” /></Group>
Thanks! • More Resources: • My 360 | Flex Talk with Brad Umbaugh on Component Lifecycle • Mrinal Wadhwa’s blog • Ryan Campbell’s blog • Ely Greenfield: “Building a Flex Component” RJ Owen rj.owen@effectiveui.com Twitter: rjowen www.effectiveui.com