custom layout of title bars and other stretchy things
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/fea0a05e Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/fea0a05e Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/fea0a05e Branch: refs/heads/develop Commit: fea0a05ecc7061ee5effa474b23d84b27cbea7f6 Parents: 3114285 Author: Alex Harui <aha...@apache.org> Authored: Mon Sep 15 13:35:43 2014 -0700 Committer: Alex Harui <aha...@apache.org> Committed: Tue Sep 23 14:49:40 2014 -0700 ---------------------------------------------------------------------- .../FlexibleFirstChildHorizontalLayout.as | 179 +++++++++++++++++++ 1 file changed, 179 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/fea0a05e/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/FlexibleFirstChildHorizontalLayout.as ---------------------------------------------------------------------- diff --git a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/FlexibleFirstChildHorizontalLayout.as b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/FlexibleFirstChildHorizontalLayout.as new file mode 100644 index 0000000..dd47b2e --- /dev/null +++ b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/FlexibleFirstChildHorizontalLayout.as @@ -0,0 +1,179 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.flex.html.beads.layouts +{ + import flash.display.DisplayObject; + import flash.display.DisplayObjectContainer; + + import org.apache.flex.core.IBeadLayout; + import org.apache.flex.core.ILayoutParent; + import org.apache.flex.core.IStrand; + import org.apache.flex.core.UIBase; + import org.apache.flex.core.ValuesManager; + import org.apache.flex.events.Event; + import org.apache.flex.events.IEventDispatcher; + + /** + * The FlexibleFirstChildHorizontalLayout class is a simple layout + * bead. It takes the set of children and lays them out + * horizontally in one row, separating them according to + * CSS layout rules for margin and padding styles. But it + * will size the first child to take up as much or little + * room as possible. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + public class FlexibleFirstChildHorizontalLayout implements IBeadLayout + { + /** + * Constructor. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + public function FlexibleFirstChildHorizontalLayout() + { + } + + private var _strand:IStrand; + + /** + * @copy org.apache.flex.core.IBead#strand + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + public function set strand(value:IStrand):void + { + _strand = value; + IEventDispatcher(value).addEventListener("layoutNeeded", changeHandler); + IEventDispatcher(value).addEventListener("widthChanged", changeHandler); + IEventDispatcher(value).addEventListener("childrenAdded", changeHandler); + IEventDispatcher(value).addEventListener("itemsCreated", changeHandler); + } + + private function changeHandler(event:Event):void + { + var layoutParent:ILayoutParent = _strand.getBeadByType(ILayoutParent) as ILayoutParent; + var contentView:DisplayObjectContainer = layoutParent.contentView; + + var n:int = contentView.numChildren; + var marginLeft:Object; + var marginRight:Object; + var marginTop:Object; + var marginBottom:Object; + var margin:Object; + var maxHeight:Number = 0; + var verticalMargins:Array = []; + + for (var i:int = n - 1; i >= 0; i--) + { + var child:DisplayObject = contentView.getChildAt(i); + margin = ValuesManager.valuesImpl.getValue(child, "margin"); + if (margin is Array) + { + if (margin.length == 1) + marginLeft = marginTop = marginRight = marginBottom = margin[0]; + else if (margin.length <= 3) + { + marginLeft = marginRight = margin[1]; + marginTop = marginBottom = margin[0]; + } + else if (margin.length == 4) + { + marginLeft = margin[3]; + marginBottom = margin[2]; + marginRight = margin[1]; + marginTop = margin[0]; + } + } + else if (margin == null) + { + marginLeft = ValuesManager.valuesImpl.getValue(child, "margin-left"); + marginTop = ValuesManager.valuesImpl.getValue(child, "margin-top"); + marginRight = ValuesManager.valuesImpl.getValue(child, "margin-right"); + marginBottom = ValuesManager.valuesImpl.getValue(child, "margin-bottom"); + } + else + { + marginLeft = marginTop = marginBottom = marginRight = margin; + } + var ml:Number; + var mr:Number; + var mt:Number; + var mb:Number; + var lastmr:Number; + mt = Number(marginTop); + if (isNaN(mt)) + mt = 0; + mb = Number(marginBottom); + if (isNaN(mb)) + mb = 0; + if (marginLeft == "auto") + ml = 0; + else + { + ml = Number(marginLeft); + if (isNaN(ml)) + ml = 0; + } + if (marginRight == "auto") + mr = 0; + else + { + mr = Number(marginRight); + if (isNaN(mr)) + mr = 0; + } + child.y = mt; + maxHeight = Math.max(maxHeight, ml + child.height + mr); + var xx:Number = contentView.width; + if (i == 0) + { + child.x = ml; + child.width = xx - mr; + } + else + child.x = xx - child.width - mr; + xx -= child.width + mr + ml; + lastmr = mr; + var valign:Object = ValuesManager.valuesImpl.getValue(child, "vertical-align"); + verticalMargins.push({ marginTop: marginTop, marginBottom: marginBottom, valign: valign }); + } + for (i = 0; i < n; i++) + { + var obj:Object = verticalMargins[0] + child = contentView.getChildAt(i); + if (obj.valign == "middle") + child.y = maxHeight - child.height / 2; + else if (valign == "bottom") + child.y = maxHeight - child.height - obj.marginBottom; + else + child.y = obj.marginTop; + } + } + } +}