From 7d86618e896fd321b0531fc0a873f4b50046177f Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Wed, 27 Nov 2013 08:09:22 -0500 Subject: [PATCH 1/4] =?UTF-8?q?Removing=20a=20subview=20now=20sets=20that?= =?UTF-8?q?=20subview=E2=80=99s=20parent=20to=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XibFree/ViewGroup.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/XibFree/ViewGroup.cs b/XibFree/ViewGroup.cs index f60361e..4e282a0 100644 --- a/XibFree/ViewGroup.cs +++ b/XibFree/ViewGroup.cs @@ -143,6 +143,7 @@ public void RemoveSubView(View view) /// The zero-based index of the view to remove. public void RemoveSubViewAt(int index) { + _subViews[index].Parent = null; _subViews.RemoveAt(index); } From b3463942b47078ac61d97b2d38323e3186bc1178 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Wed, 27 Nov 2013 08:16:51 -0500 Subject: [PATCH 2/4] Updated with other pull requests --- XibFree/LayoutParameters.cs | 2 +- XibFree/UILayoutHostScrollable.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/XibFree/LayoutParameters.cs b/XibFree/LayoutParameters.cs index 4b182ab..a532b7b 100644 --- a/XibFree/LayoutParameters.cs +++ b/XibFree/LayoutParameters.cs @@ -59,7 +59,7 @@ public LayoutParameters(float width, float height, float weight=1) Width = width; Height = height; Margins = UIEdgeInsets.Zero; - Weight = 1; + Weight = weight; Gravity = Gravity.None; } diff --git a/XibFree/UILayoutHostScrollable.cs b/XibFree/UILayoutHostScrollable.cs index dd337c1..c9d33c6 100644 --- a/XibFree/UILayoutHostScrollable.cs +++ b/XibFree/UILayoutHostScrollable.cs @@ -60,7 +60,7 @@ public ViewGroup Layout set { - _layoutHost.Layout = Layout; + _layoutHost.Layout = value; SetNeedsLayout(); } } From 4bbe6d27a42e1425f2de8bcc3f6a29244997fcf7 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Sat, 30 Nov 2013 08:41:05 -0500 Subject: [PATCH 3/4] Moved Width, WidthUnits, Height, and HeightUnits into a new Dimension class and updated throughout; general code cleanup --- XibFree/Dimension.cs | 57 +++++++++++++ XibFree/FrameLayout.cs | 8 +- XibFree/LayoutParameters.cs | 164 +++++++----------------------------- XibFree/LinearLayout.cs | 82 +++++++++--------- XibFree/NativeView.cs | 74 +++++----------- XibFree/UILayoutHost.cs | 11 +-- XibFree/View.cs | 61 ++++---------- XibFree/ViewGroup.cs | 154 +++++++++++---------------------- XibFree/XibFree.csproj | 1 + 9 files changed, 226 insertions(+), 386 deletions(-) create mode 100644 XibFree/Dimension.cs diff --git a/XibFree/Dimension.cs b/XibFree/Dimension.cs new file mode 100644 index 0000000..273e6c6 --- /dev/null +++ b/XibFree/Dimension.cs @@ -0,0 +1,57 @@ +using System; + +namespace XibFree +{ + public class Dimension + { + private float _value; + private Units _unit; + + public Dimension(float value, Units units = Units.Absolute) + { + _value = value; + _unit = units; + } + + public static Dimension FillParent + { + get { return Dimension.ParentRatio(1.0f); } + } + + public static Dimension WrapContent + { + get { return Dimension.ContentRatio(1.0f); } + } + + public static Dimension ParentRatio(float value) + { + return new Dimension(value, Units.ParentRatio); + } + + public static Dimension AspectRatio(float value) + { + return new Dimension(value, Units.AspectRatio); + } + + public static Dimension ContentRatio(float value) + { + return new Dimension(value, Units.ContentRatio); + } + + public static Dimension Absolute(float value) + { + return new Dimension(value, Units.Absolute); + } + + public float Value { get { return _value; } } + public Units Unit { get { return _unit; } } + public float Ratio + { + get + { + return (_unit == Units.Absolute) ? 1 : _value; + } + } + } +} + diff --git a/XibFree/FrameLayout.cs b/XibFree/FrameLayout.cs index a5b81ff..9f8671b 100644 --- a/XibFree/FrameLayout.cs +++ b/XibFree/FrameLayout.cs @@ -47,7 +47,7 @@ protected override void onMeasure(float parentWidth, float parentHeight) { // Try to resolve subview width var subViewWidth = float.MaxValue; - if (v.LayoutParameters.WidthUnits == Units.ParentRatio) + if (v.LayoutParameters.Width.Unit == Units.ParentRatio) { if (width==float.MaxValue) { @@ -62,7 +62,7 @@ protected override void onMeasure(float parentWidth, float parentHeight) // Try to resolve subview height var subViewHeight = float.MaxValue; - if (v.LayoutParameters.HeightUnits == Units.ParentRatio) + if (v.LayoutParameters.Height.Unit == Units.ParentRatio) { if (height==float.MaxValue) { @@ -96,13 +96,13 @@ protected override void onMeasure(float parentWidth, float parentHeight) foreach (var v in unresolved) { var subViewWidth = float.MaxValue; - if (v.LayoutParameters.WidthUnits == Units.ParentRatio && haveResolvedSize) + if (v.LayoutParameters.Width.Unit == Units.ParentRatio && haveResolvedSize) { subViewWidth = maxWidth - v.LayoutParameters.Margins.TotalWidth(); } var subViewHeight = float.MaxValue; - if (v.LayoutParameters.HeightUnits == Units.ParentRatio && haveResolvedSize) + if (v.LayoutParameters.Height.Unit == Units.ParentRatio && haveResolvedSize) { subViewHeight = maxHeight - v.LayoutParameters.Margins.TotalHeight(); } diff --git a/XibFree/LayoutParameters.cs b/XibFree/LayoutParameters.cs index a532b7b..2f02741 100644 --- a/XibFree/LayoutParameters.cs +++ b/XibFree/LayoutParameters.cs @@ -36,13 +36,15 @@ public enum Units /// public class LayoutParameters { + private UIEdgeInsets _margins; + /// /// Initializes a new instance of the class. /// public LayoutParameters() { - Width = AutoSize.WrapContent; - Height = AutoSize.WrapContent; + Width = Dimension.WrapContent; + Height = Dimension.WrapContent; Margins = UIEdgeInsets.Zero; Weight = 1; Gravity = Gravity.None; @@ -56,112 +58,33 @@ public LayoutParameters() /// Weight. public LayoutParameters(float width, float height, float weight=1) { - Width = width; - Height = height; + Width = new Dimension(width); + Height = new Dimension(height); Margins = UIEdgeInsets.Zero; Weight = weight; Gravity = Gravity.None; } /// - /// Gets or sets the width for this view + /// Gets or sets the width dimension (value and unit of measurement) /// - /// The width in pixels, or one of the AutoSize constants. - public float Width + /// The width. + public Dimension Width { get; set; } /// - /// Gets or sets the height for this view + /// Gets or sets the height dimension (value and unit of measurement) /// - /// The height in pixels, or one of the AutoSize constants. - public float Height + /// The height. + public Dimension Height { get; set; } - Units _widthUnits; - - /// - /// Gets or sets the width units. - /// - /// The width units. - public Units WidthUnits - { - get - { - if (_widthUnits == Units.Absolute) - { - if (Width == AutoSize.FillParent) - return Units.ParentRatio; - if (Width == AutoSize.WrapContent) - return Units.ContentRatio; - } - return _widthUnits; - } - set - { - _widthUnits = value; - } - } - - Units _heightUnits; - /// - /// Gets or sets the height units. - /// - /// The height units. - public Units HeightUnits - { - get - { - if (_heightUnits == Units.Absolute) - { - if (Height == AutoSize.FillParent) - return Units.ParentRatio; - if (Height == AutoSize.WrapContent) - return Units.ContentRatio; - } - return _heightUnits; - } - set - { - _heightUnits = value; - } - } - - internal float HeightRatio - { - get - { - if (_heightUnits == Units.Absolute) - { - return 1; - } - else - { - return Height; - } - } - } - - internal float WidthRatio - { - get - { - if (_widthUnits == Units.Absolute) - { - return 1; - } - else - { - return Width; - } - } - } - /// /// Gets or sets the weight of a AutoSize.FillParent view relative to its sibling views /// @@ -314,22 +237,21 @@ public float MaxHeight set; } - static float TryResolve(Units units, float size, float ratio, float parentSize) + private static float TryResolve(Dimension dimension, float parentSize) { - switch (units) + var ratio = dimension.Ratio; + switch (dimension.Unit) { case Units.Absolute: - return size; - + return dimension.Value; case Units.ParentRatio: - return parentSize==float.MaxValue ? float.MaxValue : parentSize * ratio; - + return parentSize == float.MaxValue ? float.MaxValue : parentSize * ratio; default: return float.MaxValue; } } - static SizeF GetScreenSize() + private static SizeF GetScreenSize() { var orientation = UIApplication.SharedApplication.StatusBarOrientation; if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown) @@ -347,15 +269,13 @@ internal SizeF GetHostSize(View view) { // Get the host var host = view.GetHost(); - if (host==null) - return GetScreenSize(); + if (host==null) return GetScreenSize(); var hostView = host.GetUIView(); // Use outer scroll view if present var parent = hostView.Superview; - if (parent is UIScrollView) - hostView = parent; + if (parent is UIScrollView) hostView = parent; // Return size return hostView.Bounds.Size; @@ -363,56 +283,32 @@ internal SizeF GetHostSize(View view) internal float TryResolveWidth(View view, float parentWidth) { - if (WidthUnits==Units.HostRatio) - { - return GetHostSize(view).Width * WidthRatio; - } + if (Width.Unit == Units.HostRatio) return GetHostSize(view).Width * Width.Ratio; + if (Width.Unit == Units.ScreenRatio) return GetScreenSize().Width * Width.Ratio; - if (WidthUnits==Units.ScreenRatio) - { - return GetScreenSize().Width * WidthRatio; - } - - return TryResolve(WidthUnits, Width, WidthRatio, parentWidth); + return TryResolve(Width, parentWidth); } internal float TryResolveHeight(View view, float parentHeight) { - if (HeightUnits==Units.HostRatio) - { - return GetHostSize(view).Height * HeightRatio; - } - - if (HeightUnits==Units.ScreenRatio) - { - return GetScreenSize().Height * HeightRatio; - } + if (Height.Unit == Units.HostRatio) return GetHostSize(view).Height * Height.Ratio; + if (Height.Unit == Units.ScreenRatio) return GetScreenSize().Height * Height.Ratio; - return TryResolve(HeightUnits, Height, HeightRatio, parentHeight); + return TryResolve(Height, parentHeight); } internal SizeF ResolveSize(SizeF size, SizeF sizeMeasured) { // Resolve measured size - if (size.Width == float.MaxValue) - size.Width = sizeMeasured.Width; - if (size.Height == float.MaxValue) - size.Height = sizeMeasured.Height; + if (size.Width == float.MaxValue) size.Width = sizeMeasured.Width; + if (size.Height == float.MaxValue) size.Height = sizeMeasured.Height; // Finally, resolve aspect ratios - if (WidthUnits == Units.AspectRatio) - { - size.Width = size.Height * WidthRatio; - } - if (HeightUnits == Units.AspectRatio) - { - size.Height = size.Width * HeightRatio; - } + if (Width.Unit == Units.AspectRatio) size.Width = size.Height * Width.Ratio; + if (Height.Unit == Units.AspectRatio) size.Height = size.Width * Height.Ratio; return size; } - - UIEdgeInsets _margins; } } diff --git a/XibFree/LinearLayout.cs b/XibFree/LinearLayout.cs index 14ea65b..2f6fff7 100644 --- a/XibFree/LinearLayout.cs +++ b/XibFree/LinearLayout.cs @@ -22,6 +22,10 @@ namespace XibFree { public class LinearLayout : ViewGroup { + // Fields + private Orientation _orientation; + private float _totalWeight; + /// /// Initializes a new instance of the class. /// @@ -70,6 +74,18 @@ public float Spacing set; } + /// + /// Sets an init Action that allows performing actions on the View + /// + /// The init. + public Action Init + { + set + { + value(this); + } + } + // Overridden to provide layout measurement protected override void onMeasure(float parentWidth, float parentHeight) { @@ -91,19 +107,17 @@ private void MeasureVertical(float parentWidth, float parentHeight) float height = LayoutParameters.TryResolveHeight(this, parentHeight); // Allow room for padding - if (width != float.MaxValue) - width -= Padding.TotalWidth(); + if (width != float.MaxValue) width -= Padding.TotalWidth(); // Work out the total fixed size float totalFixedSize = 0; float totalWeight = 0; int visibleViewCount = 0; - foreach (var v in SubViews.Where(x=>!x.Gone)) + foreach (var v in SubViews.Where(x => !x.Gone)) { - if (v.LayoutParameters.HeightUnits==Units.ParentRatio) + if (v.LayoutParameters.Height.Unit == Units.ParentRatio) { // We'll deal with this later - // For now, lets just total up the specified weights totalWeight += v.LayoutParameters.Weight; } @@ -124,15 +138,14 @@ private void MeasureVertical(float parentWidth, float parentHeight) totalFixedSize += Padding.TotalHeight(); // And spacing between controls - if (visibleViewCount>1) - totalFixedSize += (visibleViewCount-1) * Spacing; + if (visibleViewCount > 1) totalFixedSize += (visibleViewCount - 1) * Spacing; float totalVariableSize = 0; - if (LayoutParameters.HeightUnits == Units.ContentRatio || height == float.MaxValue) + if (LayoutParameters.Height.Unit == Units.ContentRatio || height == float.MaxValue) { // This is a weird case: we have a height of wrap content, but child items that want to fill parent too. // Temporarily switch those items to wrap content and use their natural size - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.HeightUnits==Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit == Units.ParentRatio)) { v.Measure(adjustLayoutWidth(width, v), float.MaxValue); totalVariableSize += v.GetMeasuredSize().Height; @@ -141,19 +154,17 @@ private void MeasureVertical(float parentWidth, float parentHeight) else { // If we've had an explicit weight passed to us, ignore the calculated total weight and use it instead - if (_totalWeight!=0) - totalWeight = _totalWeight; + if (_totalWeight!=0) totalWeight = _totalWeight; // Work out how much room we've got to share around float room = height - totalFixedSize; // Layout the fill parent items - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.HeightUnits==Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit == Units.ParentRatio)) { // Work out size - if (room<0) - room = 0; - float size = totalWeight==0 ? room : room * v.LayoutParameters.Weight / totalWeight; + if (room<0) room = 0; + float size = (totalWeight == 0) ? room : room * v.LayoutParameters.Weight / totalWeight; // Measure it v.Measure(adjustLayoutWidth(width, v), size); @@ -173,15 +184,14 @@ private void MeasureVertical(float parentWidth, float parentHeight) { // Work out the maximum width of all children that aren't fill parent sizeMeasured.Width = 0; - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.WidthUnits!=Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit != Units.ParentRatio)) { float totalChildWidth = v.GetMeasuredSize().Width + v.LayoutParameters.Margins.TotalWidth(); - if (totalChildWidth > sizeMeasured.Width) - sizeMeasured.Width = totalChildWidth; + if (totalChildWidth > sizeMeasured.Width) sizeMeasured.Width = totalChildWidth; } // Set the width of all children that are fill parent - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.WidthUnits==Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit == Units.ParentRatio)) { v.Measure(sizeMeasured.Width, v.GetMeasuredSize().Height); } @@ -210,8 +220,7 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) float layoutHeight = LayoutParameters.TryResolveHeight(this, parentHeight); // Allow room for padding - if (layoutHeight != float.MaxValue) - layoutHeight -= Padding.TotalHeight(); + if (layoutHeight != float.MaxValue) layoutHeight -= Padding.TotalHeight(); // Work out the total fixed size float totalFixedSize = 0; @@ -219,7 +228,7 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) int visibleViewCount = 0; foreach (var v in SubViews.Where(x=>!x.Gone)) { - if (v.LayoutParameters.WidthUnits==Units.ParentRatio) + if (v.LayoutParameters.Width.Unit == Units.ParentRatio) { // We'll deal with this later @@ -246,11 +255,11 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) totalFixedSize += (visibleViewCount-1) * Spacing; float totalVariableSize = 0; - if (LayoutParameters.WidthUnits == Units.ContentRatio || layoutWidth == float.MaxValue) + if (LayoutParameters.Width.Unit == Units.ContentRatio || layoutWidth == float.MaxValue) { // This is a weird case: we have a width of wrap content, but child items that want to fill parent too. // Temporarily switch those items to wrap content and use their natural size - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.WidthUnits==Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit == Units.ParentRatio)) { v.Measure(float.MaxValue, adjustLayoutHeight(layoutHeight, v)); totalVariableSize += v.GetMeasuredSize().Width; @@ -266,11 +275,10 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) float room = layoutWidth - totalFixedSize; // Layout the fill parent items - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.WidthUnits==Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit == Units.ParentRatio)) { // Work out size - if (room<0) - room = 0; + if (room<0) room = 0; float size = totalWeight==0 ? room : room * v.LayoutParameters.Weight / totalWeight; // Measure it @@ -291,7 +299,7 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) { // Work out the maximum height of all children that aren't fill parent sizeMeasured.Height = 0; - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.HeightUnits!=Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit != Units.ParentRatio)) { float totalChildHeight = v.GetMeasuredSize().Height + v.LayoutParameters.Margins.TotalHeight(); if (totalChildHeight > sizeMeasured.Height) @@ -299,7 +307,7 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) } // Set the height of all children that are fill parent - foreach (var v in SubViews.Where(x=>!x.Gone && x.LayoutParameters.HeightUnits==Units.ParentRatio)) + foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit == Units.ParentRatio)) { v.Measure(v.GetMeasuredSize().Width, sizeMeasured.Height); } @@ -341,7 +349,7 @@ protected override void onLayout(RectangleF newPosition, bool parentHidden) } // Do subview layout when in vertical orientation - void LayoutVertical(RectangleF newPosition) + private void LayoutVertical(RectangleF newPosition) { float y; switch (Gravity & Gravity.VerticalMask) @@ -411,7 +419,7 @@ void LayoutVertical(RectangleF newPosition) } // Do subview layout when in horizontal orientation - void LayoutHorizontal(RectangleF newPosition) + private void LayoutHorizontal(RectangleF newPosition) { float x; switch (Gravity & Gravity.HorizontalMask) @@ -520,18 +528,6 @@ private float adjustLayoutHeight(float height, View c) return height - c.LayoutParameters.Margins.TotalHeight(); } - - public Action Init - { - set - { - value(this); - } - } - - // Fields - private Orientation _orientation; - private float _totalWeight; } } diff --git a/XibFree/NativeView.cs b/XibFree/NativeView.cs index 92e6369..16b9392 100644 --- a/XibFree/NativeView.cs +++ b/XibFree/NativeView.cs @@ -27,13 +27,13 @@ namespace XibFree /// public class NativeView : View { + // The hosted native view + private UIView _view; + /// /// Initializes a new instance of the class. /// - public NativeView() - { - - } + public NativeView() {} /// /// Initializes a new instance of the class. @@ -51,7 +51,7 @@ public override LayoutParameters LayoutParameters get { var nestedHost = _view as UILayoutHost; - if (nestedHost!=null && nestedHost.Layout!=null) + if (nestedHost != null && nestedHost.Layout != null) { return nestedHost.Layout.LayoutParameters; } @@ -65,20 +65,14 @@ public override LayoutParameters LayoutParameters /// The view. public UIView View { - get - { - return _view; - } + get { return _view; } set { - if (_view!=value) + if (_view != value) { // Detach old view from host ViewGroup.IHost host = GetHost(); - if (host!=null) - { - onDetach(); - } + if (host != null) onDetach(); // Store the new view _view = value; @@ -87,10 +81,7 @@ public UIView View _view.AutoresizingMask = UIViewAutoresizing.None; // Attach the new view to the host - if (host!=null) - { - onAttach(host); - } + if (host != null) onAttach(host); } } } @@ -135,7 +126,13 @@ protected override void onMeasure(float parentWidth, float parentHeight) if (width == float.MaxValue || height == float.MaxValue) { SizeF sizeToFit = new SizeF(width, height); - sizeMeasured = Measurer!=null ? Measurer(_view, sizeToFit) : _view.SizeThatFits(sizeToFit); + if (Measurer != null) sizeMeasured = Measurer(_view, sizeToFit); + else + { + sizeMeasured = _view.SizeThatFits(sizeToFit); + if (LayoutParameters.Width.Unit == Units.ContentRatio) sizeMeasured.Width = sizeMeasured.Width * LayoutParameters.Width.Value; + if (LayoutParameters.Height.Unit == Units.ContentRatio) sizeMeasured.Height = sizeMeasured.Height * LayoutParameters.Height.Value; + } } // Set the measured size @@ -149,10 +146,7 @@ protected override void onMeasure(float parentWidth, float parentHeight) internal override void onAttach(ViewGroup.IHost host) { // If we have a view, attach to the hosting view by adding as a subview - if (_view!=null) - { - host.GetUIView().Add(_view); - } + if (_view != null) host.GetUIView().Add(_view); } /// @@ -161,20 +155,13 @@ internal override void onAttach(ViewGroup.IHost host) internal override void onDetach() { // If we have a view, remove from the hosting view by removing it from the superview - if (_view!=null) - { - _view.RemoveFromSuperview(); - } + if (_view != null) _view.RemoveFromSuperview(); } /// Delegate for a plugin measurement support public delegate SizeF NativeMeasurer(UIView native, SizeF constraint); - public NativeMeasurer Measurer - { - get; - set; - } + public NativeMeasurer Measurer { get; set; } /// /// Sets a an action to be immediately called. Provided to allowing execution of code inline @@ -183,32 +170,22 @@ public NativeMeasurer Measurer /// An Action to be called immediately public Action Init { - set - { - value(this); - } + set { value(this); } } internal override UIView UIViewWithTag(int tag) { - if (_view!=null) - return _view.ViewWithTag(tag); - return null; + return (_view != null) ? _view.ViewWithTag(tag) : null; } internal override View LayoutViewWithTag(int tag) { - if (_view!=null && _view.Tag==tag) - return this; - return null; + return (_view != null && _view.Tag == tag) ? this : null; } public override NativeView FindNativeView(UIView v) { - if (_view == v) - return this; - else - return null; + return (_view == v) ? this : null; } internal override CALayer GetDisplayLayer() @@ -220,11 +197,6 @@ internal override CALayer FindFirstSublayer() { return null; } - - - - // The hosted native view - private UIView _view; } } diff --git a/XibFree/UILayoutHost.cs b/XibFree/UILayoutHost.cs index e1f046a..d2b9f28 100644 --- a/XibFree/UILayoutHost.cs +++ b/XibFree/UILayoutHost.cs @@ -25,6 +25,8 @@ namespace XibFree /// public class UILayoutHost : UIView, ViewGroup.IHost { + private ViewGroup _layout; + /// /// Initializes a new instance of the class. /// @@ -60,13 +62,11 @@ public ViewGroup Layout set { - if (_layout!=null) - _layout.SetHost(null); + if (_layout != null) _layout.SetHost(null); _layout = value; - if (_layout!=null) - _layout.SetHost(this); + if (_layout != null) _layout.SetHost(this); } } @@ -117,9 +117,6 @@ UIView ViewGroup.IHost.GetUIView() } #endregion - - - private ViewGroup _layout; } } diff --git a/XibFree/View.cs b/XibFree/View.cs index afde91c..3b5ccf2 100644 --- a/XibFree/View.cs +++ b/XibFree/View.cs @@ -26,6 +26,10 @@ namespace XibFree /// public abstract class View { + private SizeF _measuredSize; + private bool _measuredSizeValid; + private ViewGroup _parent; + /// /// Initializes a new instance of the class. /// @@ -68,31 +72,19 @@ internal set /// Gets or sets the layout parameters for this view /// /// The layout parameters. - public virtual LayoutParameters LayoutParameters - { - get; - set; - } + public virtual LayoutParameters LayoutParameters { get; set; } // Internal helper to walk the parent view hierachy and find the view that's hosting this view hierarchy internal virtual ViewGroup.IHost GetHost() { - if (_parent==null) - return null; - - return _parent.GetHost(); + return (_parent != null) ? _parent.GetHost() : null; } // Internal notification that this view has been attached to a hosting view - internal virtual void onAttach(ViewGroup.IHost host) - { - } + internal virtual void onAttach(ViewGroup.IHost host) { } // Internal notification that this view has been detached from a hosting view - internal virtual void onDetach() - { - } - + internal virtual void onDetach() { } /// /// Layout the subviews in this view using dimensions calculated during the last measure cycle @@ -118,10 +110,7 @@ public void Measure(float parentWidth, float parentHeight) { _measuredSizeValid = false; onMeasure(parentWidth, parentHeight); - if (!_measuredSizeValid) - { - throw new InvalidOperationException("onMeasure didn't set measurement before returning"); - } + if (!_measuredSizeValid) throw new InvalidOperationException("onMeasure didn't set measurement before returning"); } /// @@ -144,14 +133,11 @@ public void InvalidateMeasure() /// Size. protected void SetMeasuredSize(SizeF size) { - if (LayoutParameters.MinWidth!=0 && size.Width < LayoutParameters.MinWidth) - size.Width = LayoutParameters.MinWidth; - if (LayoutParameters.MinHeight!=0 && size.Height < LayoutParameters.MinHeight) - size.Height = LayoutParameters.MinHeight; - if (LayoutParameters.MaxWidth!=0 && size.Width > LayoutParameters.MaxWidth) - size.Width = LayoutParameters.MaxWidth; - if (LayoutParameters.MaxHeight!=0 && size.Height > LayoutParameters.MaxHeight) - size.Height = LayoutParameters.MaxHeight; + if (LayoutParameters.MinWidth != 0 && size.Width < LayoutParameters.MinWidth) size.Width = LayoutParameters.MinWidth; + if (LayoutParameters.MinHeight != 0 && size.Height < LayoutParameters.MinHeight) size.Height = LayoutParameters.MinHeight; + + if (LayoutParameters.MaxWidth != 0 && size.Width > LayoutParameters.MaxWidth) size.Width = LayoutParameters.MaxWidth; + if (LayoutParameters.MaxHeight != 0 && size.Height > LayoutParameters.MaxHeight) size.Height = LayoutParameters.MaxHeight; _measuredSize = size; _measuredSizeValid = true; @@ -163,8 +149,7 @@ protected void SetMeasuredSize(SizeF size) /// The measured size. public SizeF GetMeasuredSize() { - if (!_measuredSizeValid) - throw new InvalidOperationException("Attempt to use measured size before measurement"); + if (!_measuredSizeValid) throw new InvalidOperationException("Attempt to use measured size before measurement"); return _measuredSize; } @@ -193,10 +178,8 @@ public SizeF GetMeasuredSize() /// The type of view to return public T ViewWithTag(int tag) { - if (typeof(UIView).IsAssignableFrom(typeof(T))) - return (T)(object)UIViewWithTag(tag); - else - return (T)(object)LayoutViewWithTag(tag); + if (typeof(UIView).IsAssignableFrom(typeof(T))) return (T)(object)UIViewWithTag(tag); + else return (T)(object)LayoutViewWithTag(tag); } public abstract NativeView FindNativeView(UIView v); @@ -227,16 +210,8 @@ public bool Visible public void RemoveFromSuperview() { - if (Parent!=null) - { - Parent.RemoveSubView(this); - } + if (Parent!=null) Parent.RemoveSubView(this); } - - - internal SizeF _measuredSize; - internal bool _measuredSizeValid; - internal ViewGroup _parent; } } diff --git a/XibFree/ViewGroup.cs b/XibFree/ViewGroup.cs index 4e282a0..7c1ce2f 100644 --- a/XibFree/ViewGroup.cs +++ b/XibFree/ViewGroup.cs @@ -28,13 +28,26 @@ namespace XibFree /// public abstract class ViewGroup : View { + // Fields + private List _subViews = new List(); + private CALayer _layer; + private IHost _host; + + /// + /// Gets or sets the padding that should be applied around the subviews contained in this view group + /// + /// The padding. + public UIEdgeInsets Padding { get; set; } + + public int Tag { get; set; } + /// /// Initializes a new instance of the class. /// public ViewGroup() { - LayoutParameters.Width = AutoSize.FillParent; - LayoutParameters.Height = AutoSize.FillParent; + LayoutParameters.Width = Dimension.FillParent; + LayoutParameters.Height = Dimension.FillParent; } /// @@ -43,25 +56,21 @@ public ViewGroup() /// The sub views. public IEnumerable SubViews { - get - { - return _subViews; - } + get { return _subViews; } set { - // Check none of the child already have parents - foreach (var c in value) + // Check that none of the child subviews already have parents + if (value.Any(c => c.Parent != null)) { - if (c.Parent!=null) - throw new InvalidOperationException("View is already a child of another ViewGroup"); + throw new InvalidOperationException("View is already a child of another ViewGroup"); } - foreach (var c in value) - { - c.Parent = this; - } + foreach (var c in value) c.Parent = this; + // Remove self as parent from current subviews and then replace them + foreach (var c in _subViews) c.Parent = null; _subViews.Clear(); + _subViews.AddRange(value); } } @@ -119,12 +128,10 @@ public void RemoveSubView(UIView view) /// The subview to add. public void InsertSubView(int position, View view) { - if (view.Parent!=null) - throw new InvalidOperationException("View is already a child of another ViewGroup"); + if (view.Parent != null) throw new InvalidOperationException("View is already a child of another ViewGroup"); view.Parent = this; - if (position<0) - position = _subViews.Count; + if (position < 0) position = _subViews.Count; _subViews.Insert(position, view); } @@ -147,22 +154,6 @@ public void RemoveSubViewAt(int index) _subViews.RemoveAt(index); } - /// - /// Gets or sets the padding that should be applied around the subviews contained in this view group - /// - /// The padding. - public UIEdgeInsets Padding - { - get - { - return _padding; - } - set - { - _padding = value; - } - } - public interface IHost { UIView GetUIView(); @@ -175,17 +166,11 @@ public interface IHost /// A reference to the host. public void SetHost(IHost host) { - if (_host!=null) - { - onDetach(); - } + if (_host != null) onDetach(); _host = host; - if (_host!=null) - { - onAttach(_host); - } + if (_host != null) onAttach(_host); } /// @@ -196,10 +181,7 @@ internal override ViewGroup.IHost GetHost() { // If this view group has been parented into an actual UIView, we'll have a IHost reference // that acts as the host for all views in the hierarchy. If not, ask our parent - if (_host==null) - return base.GetHost(); - else - return _host; + return (_host != null) ? _host : base.GetHost(); } /// @@ -209,12 +191,10 @@ internal override ViewGroup.IHost GetHost() internal override void onAttach(IHost host) { // Add the layer - if (_layer!=null) - host.GetUIView().Layer.AddSublayer(_layer); + if (_layer!=null) host.GetUIView().Layer.AddSublayer(_layer); // Forward on to all children - foreach (var c in _subViews) - c.onAttach(host); + foreach (var c in _subViews) c.onAttach(host); } /// @@ -223,21 +203,19 @@ internal override void onAttach(IHost host) internal override void onDetach() { // Remove from layer - if (_layer!=null) - _layer.RemoveFromSuperLayer(); + if (_layer != null) _layer.RemoveFromSuperLayer(); // Forward on to all children - foreach (var c in _subViews) - c.onDetach(); + foreach (var c in _subViews) c.onDetach(); } - protected override void onLayout(System.Drawing.RectangleF newPosition, bool parentHidden) + protected override void onLayout(RectangleF newPosition, bool parentHidden) { // Reposition the layer - if (_layer!=null) + if (_layer != null) { bool newHidden = parentHidden || !Visible; - if (newHidden!=_layer.Hidden) + if (newHidden != _layer.Hidden) { // If we're changing the visibility, disable animations since // the old rectangle for the position was probably wrong, resulting in @@ -250,39 +228,26 @@ protected override void onLayout(System.Drawing.RectangleF newPosition, bool par } else { - if (!_layer.Hidden) - { - _layer.Frame = newPosition; - } + if (!_layer.Hidden) _layer.Frame = newPosition; } } // Hide all subviews if (parentHidden || !Visible) { - foreach (var v in SubViews) - { - v.Layout(RectangleF.Empty, false); - } + foreach (var v in SubViews) v.Layout(RectangleF.Empty, false); return; } } - public int Tag - { - get; - set; - } internal override View LayoutViewWithTag(int tag) { - if (Tag==tag) - return this; + if (Tag == tag) return this; foreach (var v in _subViews) { var result = v.LayoutViewWithTag(tag); - if (result!=null) - return result; + if (result != null) return result; } return null; @@ -293,8 +258,7 @@ internal override UIView UIViewWithTag(int tag) foreach (var v in _subViews) { var result = v.UIViewWithTag(tag); - if (result!=null) - return result; + if (result != null) return result; } return null; } @@ -304,8 +268,7 @@ public override NativeView FindNativeView(UIView view) foreach (var v in _subViews) { var result = v.FindNativeView(view); - if (result!=null) - return result; + if (result != null) return result; } return null; } @@ -314,13 +277,11 @@ internal override CALayer FindFirstSublayer() { foreach (var v in SubViews) { - var l = v.GetDisplayLayer(); - if (l!=null) - return l; + var layer = v.GetDisplayLayer(); + if (layer != null) return layer; - l = v.FindFirstSublayer(); - if (l!=null) - return l; + layer = v.FindFirstSublayer(); + if (layer != null) return layer; } return null; @@ -335,15 +296,11 @@ internal override CALayer GetDisplayLayer() public CALayer Layer { - get - { - return _layer; - } + get { return _layer; } set { // Remove old layer - if (_layer!=null) - _layer.RemoveFromSuperLayer(); + if (_layer != null) _layer.RemoveFromSuperLayer(); // Store it _layer = value; @@ -356,25 +313,14 @@ public CALayer Layer { UIView hostView = host.GetUIView(); var nextSubLayer = FindFirstSublayer(); - if (nextSubLayer!=null) - { - hostView.Layer.InsertSublayerBelow(_layer, nextSubLayer); - } - else - { - hostView.Layer.AddSublayer(_layer); - } + + if (nextSubLayer!=null) hostView.Layer.InsertSublayerBelow(_layer, nextSubLayer); + else hostView.Layer.AddSublayer(_layer); } } } } - - // Fields - List _subViews = new List(); - UIEdgeInsets _padding = UIEdgeInsets.Zero; - CALayer _layer; - IHost _host; } } diff --git a/XibFree/XibFree.csproj b/XibFree/XibFree.csproj index 2d35335..501f97f 100644 --- a/XibFree/XibFree.csproj +++ b/XibFree/XibFree.csproj @@ -61,5 +61,6 @@ + \ No newline at end of file From 1c4abcda28723958be866d3f322961c8f5910df1 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Sat, 30 Nov 2013 12:06:43 -0500 Subject: [PATCH 4/4] Fixed Demo to match new Dimension class and cleaned it up a little --- Demo/AppDelegate.cs | 24 +- Demo/Demo.csproj | 4 +- Demo/Demo1.cs | 28 +-- Demo/FrameLayoutDemo.cs | 59 ++--- Demo/FullScreenDemo.cs | 83 ++++--- Demo/Info.plist | 12 + Demo/LinearLayoutDemo.cs | 71 +++--- Demo/Main.cs | 5 - Demo/MainViewController.cs | 52 ++-- Demo/NestedDemo.cs | 53 +++-- Demo/RecalculateLayoutDemo.cs | 64 +++-- Demo/TableViewCellDemo.cs | 105 ++++----- Demo/TableViewCellDemo2.cs | 378 +++++++++++++++--------------- Demo/ViewGroupLayerDemo.cs | 48 ++-- Demo/VisibilityDemo.cs | 62 +++-- XibFree.sln | 47 ++-- XibFree/AutoSize.cs | 38 --- XibFree/Dimension.cs | 23 +- XibFree/Extensions.cs | 20 +- XibFree/FrameLayout.cs | 56 ++--- XibFree/IHost.cs | 9 + XibFree/LayoutParameters.cs | 70 ++---- XibFree/LinearLayout.cs | 183 ++++++--------- XibFree/NativeView.cs | 61 ++--- XibFree/Orientation.cs | 2 - XibFree/UILayoutHost.cs | 37 ++- XibFree/UILayoutHostScrollable.cs | 44 ++-- XibFree/View.cs | 66 +++--- XibFree/ViewGroup.cs | 94 +++----- XibFree/Visibility.cs | 1 - XibFree/XibFree.csproj | 6 +- 31 files changed, 824 insertions(+), 981 deletions(-) delete mode 100644 XibFree/AutoSize.cs create mode 100644 XibFree/IHost.cs diff --git a/Demo/AppDelegate.cs b/Demo/AppDelegate.cs index 527a6a2..ec579c2 100644 --- a/Demo/AppDelegate.cs +++ b/Demo/AppDelegate.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; - using MonoTouch.Foundation; using MonoTouch.UIKit; @@ -11,11 +7,10 @@ namespace Demo // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register ("AppDelegate")] - public partial class AppDelegate : UIApplicationDelegate + public class AppDelegate : UIApplicationDelegate { // class-level declarations - UINavigationController navigationController; - UIWindow window; + UIWindow _window; // // This method is invoked when the application has loaded and is ready to run. In this @@ -26,18 +21,13 @@ public partial class AppDelegate : UIApplicationDelegate // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { - window = new UIWindow(UIScreen.MainScreen.Bounds); - - /* - var controller = new MainViewController(); - navigationController = new UINavigationController(controller); - window.RootViewController = navigationController; - */ - - window.RootViewController = new FullScreenDemo(); + _window = new UIWindow(UIScreen.MainScreen.Bounds) + { + RootViewController = new FullScreenDemo() + }; // make the window visible - window.MakeKeyAndVisible(); + _window.MakeKeyAndVisible(); return true; } diff --git a/Demo/Demo.csproj b/Demo/Demo.csproj index e50110f..309422f 100644 --- a/Demo/Demo.csproj +++ b/Demo/Demo.csproj @@ -1,4 +1,4 @@ - + Debug @@ -104,7 +104,7 @@ - + diff --git a/Demo/Demo1.cs b/Demo/Demo1.cs index 8ef8e9e..136ffed 100644 --- a/Demo/Demo1.cs +++ b/Demo/Demo1.cs @@ -1,15 +1,11 @@ -using System; using System.Drawing; -using System.Collections.Generic; using MonoTouch.UIKit; -using MonoTouch.Foundation; - using XibFree; namespace Demo { - public partial class Demo1 : UITableViewController + public sealed class Demo1 : UITableViewController { public Demo1() { @@ -25,7 +21,7 @@ public override void LoadView() SubViews = new View[] { // A NativeView contains an iOS UIView - new NativeView() + new NativeView { // This is the UIView View = new UIView(RectangleF.Empty) @@ -35,33 +31,33 @@ public override void LoadView() }, // This controls how it's laid out by its parent view group (in this case the outer linear layout) - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = 50, + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), }, }, // A second view that will be stacked below the first - new NativeView() + new NativeView { View = new UIView(RectangleF.Empty) { BackgroundColor = UIColor.Blue, }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = 50, + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), }, - }, + } }, }; // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor=UIColor.Gray; } } } diff --git a/Demo/FrameLayoutDemo.cs b/Demo/FrameLayoutDemo.cs index 8ece4e8..2e7a9af 100644 --- a/Demo/FrameLayoutDemo.cs +++ b/Demo/FrameLayoutDemo.cs @@ -1,29 +1,14 @@ using System; -using System.Drawing; -using System.Collections.Generic; - using MonoTouch.UIKit; -using MonoTouch.Foundation; - using XibFree; namespace Demo { - public partial class FrameLayoutDemo : UITableViewController + public sealed class FrameLayoutDemo : UITableViewController { public FrameLayoutDemo() { Title = "FrameLayout"; - - // Custom initialization - } - - public override void DidReceiveMemoryWarning() - { - // Releases the view if it doesn't have a superview. - base.DidReceiveMemoryWarning(); - - // Release any cached data, images, etc that aren't in use. } [Obsolete ("Deprecated in iOS6. Replace it with both GetSupportedInterfaceOrientations and PreferredInterfaceOrientationForPresentation")] @@ -31,59 +16,54 @@ public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientati { return true; } - - public override void ViewDidLoad() - { - base.ViewDidLoad(); - } public override void LoadView() { // Frame layouts allow subviews that overlap each other - var layout = new FrameLayout() + var layout = new FrameLayout { Padding = new UIEdgeInsets(10,10,10,10), SubViews = new View[] { - new NativeView() + new NativeView { - View = new UIView() + View = new UIView { BackgroundColor = UIColor.FromRGBA(255,0,0,128), }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.FillParent, + Width = Dimension.FillParent, + Height = Dimension.FillParent, } }, - new NativeView() + new NativeView { - View = new UIView() + View = new UIView { BackgroundColor = UIColor.FromRGBA(0,0,255,128), }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = 100, + Width = Dimension.FillParent, + Height = Dimension.Absolute(100), Margins = new UIEdgeInsets(10,10,10,10), Gravity = Gravity.Bottom, } }, - new NativeView() + new NativeView { - View = new UIView() + View = new UIView { BackgroundColor = UIColor.FromRGBA(0,0,0,128), }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = 80, + Width = Dimension.FillParent, + Height = Dimension.Absolute(80), Margins = new UIEdgeInsets(10,-10,10,-10), Gravity = Gravity.CenterVertical, } @@ -91,10 +71,9 @@ public override void LoadView() }, }; - // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor=UIColor.Gray; } } } diff --git a/Demo/FullScreenDemo.cs b/Demo/FullScreenDemo.cs index b424461..128bf6b 100644 --- a/Demo/FullScreenDemo.cs +++ b/Demo/FullScreenDemo.cs @@ -7,22 +7,25 @@ namespace Demo { - public class FullScreenDemo : UIViewController + public sealed class FullScreenDemo : UIViewController { public FullScreenDemo() { - this.Title = "XibFree"; + Title = "XibFree"; } [Register("GlassButton")] - class GlassButton : UIButton + private sealed class GlassButton : UIButton { + private readonly CALayer _layerGradient; + private readonly CALayer _layerDarken; + public GlassButton() : base(RectangleF.Empty) { // Create a mostly transparent gradient for the button background - _layerGradient = new CAGradientLayer() + _layerGradient = new CAGradientLayer { - Colors = new MonoTouch.CoreGraphics.CGColor[] + Colors = new[] { new MonoTouch.CoreGraphics.CGColor(1,1,1,0.5f), new MonoTouch.CoreGraphics.CGColor(1,1,1,0.1f) @@ -33,15 +36,15 @@ public GlassButton() : base(RectangleF.Empty) 1.0f }, CornerRadius = 5, - Frame = this.Bounds, + Frame = Bounds, }; // Create another mostly transparent layer to darken the button when it's pressed - _layerDarken = new CALayer() + _layerDarken = new CALayer { BackgroundColor = new MonoTouch.CoreGraphics.CGColor(0,0,0,0.2f), CornerRadius = 5, - Frame = this.Bounds, + Frame = Bounds, Hidden = true, // Normally hidden }; @@ -90,13 +93,9 @@ public override bool Highlighted base.Highlighted = value; } } - - - CALayer _layerGradient; - CALayer _layerDarken; } - class Label : NativeView + private class Label : NativeView { public Label(string title, UIFont font) { @@ -107,12 +106,16 @@ public Label(string title, UIFont font) BackgroundColor = UIColor.Clear, TextColor = UIColor.DarkGray, }; - - LayoutParameters = new LayoutParameters(AutoSize.WrapContent, AutoSize.WrapContent); + + LayoutParameters = new LayoutParameters + { + Width = Dimension.WrapContent, + Height = Dimension.WrapContent, + }; } } - class Button : NativeView + private class Button : NativeView { public Button(string title, Action handler) { @@ -126,8 +129,12 @@ public Button(string title, Action handler) button.TouchUpInside += (sender, e) => handler(); // Setup the layout parameters - LayoutParameters = new LayoutParameters(AutoSize.FillParent, AutoSize.WrapContent); - LayoutParameters.MaxWidth = 160; + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.WrapContent, + MaxWidth = 160, + }; } } @@ -138,20 +145,24 @@ public override void LoadView() { Padding = new UIEdgeInsets(10,10,10,10), Gravity = Gravity.CenterHorizontal, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, AutoSize.WrapContent), + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.WrapContent, + }, SubViews = new View[] { - new NativeView() + new NativeView { - View = new UIImageView() + View = new UIImageView { Image = UIImage.FromBundle("XibFree_512.png"), ContentMode = UIViewContentMode.ScaleAspectFit, }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = 120, - Height = 120, + Width = Dimension.Absolute(120), + Height = Dimension.Absolute(120), MarginTop = 30, MarginBottom = 20, } @@ -165,42 +176,42 @@ public override void LoadView() SubViews = new View[] { new Button("Download", () => Alert("Download")), - new Button("View Samples", () => Alert("Samples")), + new Button("View Samples", () => Alert("Samples")) }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, MarginTop = 50, } }, - new NativeView() + new NativeView { - View = new UIView() + View = new UIView { BackgroundColor = UIColor.FromRGBA(0, 0, 0, 10), }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = 2, + Width = Dimension.FillParent, + Height = Dimension.Absolute(2), MarginTop = 20, MarginBottom = 20, } }, - new Label("Step away from the mouse, build your UI in code!", UIFont.SystemFontOfSize(12)), + new Label("Step away from the mouse, build your UI in code!", UIFont.SystemFontOfSize(12)) } }; // Create a UILayoutHost view to host the layout - this.View = new UILayoutHostScrollable(layout) + View = new UILayoutHostScrollable(layout) { // Yellowish background color BackgroundColor = UIColor.FromRGB(0xF1, 0xE8, 0xDC), }; } - void Alert(string message) + private static void Alert(string message) { new UIAlertView(message, "", null, "OK").Show(); } diff --git a/Demo/Info.plist b/Demo/Info.plist index daa858b..07a2513 100644 --- a/Demo/Info.plist +++ b/Demo/Info.plist @@ -11,5 +11,17 @@ MinimumOSVersion 4.3 + UIDeviceFamily + + 1 + + CFBundleDisplayName + XibFree Demos + CFBundleIdentifier + xibfree.demos + CFBundleVersion + 1.0 + NSMainNibFile + diff --git a/Demo/LinearLayoutDemo.cs b/Demo/LinearLayoutDemo.cs index 2496a86..3b69c79 100644 --- a/Demo/LinearLayoutDemo.cs +++ b/Demo/LinearLayoutDemo.cs @@ -1,15 +1,12 @@ -using System; using System.Drawing; -using System.Collections.Generic; using MonoTouch.UIKit; -using MonoTouch.Foundation; using XibFree; namespace Demo { - public partial class LinearLayoutDemo : UITableViewController + public sealed class LinearLayoutDemo : UITableViewController { public LinearLayoutDemo() { @@ -18,23 +15,14 @@ public LinearLayoutDemo() // Custom initialization } - public override void DidReceiveMemoryWarning() + public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() { - // Releases the view if it doesn't have a superview. - base.DidReceiveMemoryWarning(); - - // Release any cached data, images, etc that aren't in use. + return UIInterfaceOrientationMask.All; } - [Obsolete ("Deprecated in iOS6. Replace it with both GetSupportedInterfaceOrientations and PreferredInterfaceOrientationForPresentation")] - public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) + public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation() { - return true; - } - - public override void ViewDidLoad() - { - base.ViewDidLoad(); + return UIInterfaceOrientation.Portrait; } public override void LoadView() @@ -48,7 +36,7 @@ public override void LoadView() SubViews = new View[] { // A NativeView contains an iOS UIView - new NativeView() + new NativeView { // This is the UIView View = new UIView(RectangleF.Empty) @@ -58,10 +46,10 @@ public override void LoadView() }, // This controls how it's laid out by its parent view group (in this case the outer linear layout) - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = 50, + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), }, }, @@ -69,16 +57,16 @@ public override void LoadView() new LinearLayout(Orientation.Horizontal) { // How to layout this linear layout within the outer one - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Height = AutoSize.WrapContent, - Width = AutoSize.FillParent, + Height = Dimension.WrapContent, + Width = Dimension.FillParent, }, // Sub view collection SubViews = new View[] { - new NativeView() + new NativeView { // This time we're showing a UILabel View = new UILabel(RectangleF.Empty) @@ -90,24 +78,24 @@ public override void LoadView() TextColor = UIColor.White }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, // Height calculated automatically based on text content! + Width = Dimension.FillParent, + Height = Dimension.WrapContent, // Height calculated automatically based on text content! }, }, - new NativeView() + new NativeView { // Here we're hosting a button View = new UIButton(UIButtonType.RoundedRect) { Tag = 123, }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.WrapContent, // Size of button determined by it's content - Height = AutoSize.WrapContent, + Width = Dimension.WrapContent, // Size of button determined by it's content + Height = Dimension.WrapContent, Gravity = Gravity.CenterVertical, Margins = new UIEdgeInsets(0, 10, 0, 0), // Put a margin on the left to separate it from the text @@ -120,25 +108,22 @@ public override void LoadView() v.As().SetTitle("Hello", UIControlState.Normal); // We can also setup an event handler - v.As().TouchUpInside += (sender,args) => - { - new UIAlertView("Clicked", "", null, "OK").Show(); - }; + v.As().TouchUpInside += (sender,args) => new UIAlertView("Clicked", "", null, "OK").Show(); } - }, + } } }, - new NativeView() + new NativeView { View = new UIImageView(UIImage.FromBundle("logo320.png")) { ContentMode = UIViewContentMode.ScaleAspectFit, //BackgroundColor = UIColor.White }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, // Overrall size determined by parent container width - Height = AutoSize.WrapContent, // Height will be calculated by calling Measurer below + Width = Dimension.FillParent, // Overrall size determined by parent container width + Height = Dimension.WrapContent, // Height will be calculated by calling Measurer below Margins = new UIEdgeInsets(10, 0, 0, 0) }, Measurer = (v,s) => @@ -155,8 +140,8 @@ public override void LoadView() }; // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor = UIColor.Gray; } } } diff --git a/Demo/Main.cs b/Demo/Main.cs index 2d67daa..cec2347 100644 --- a/Demo/Main.cs +++ b/Demo/Main.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -using MonoTouch.Foundation; using MonoTouch.UIKit; namespace Demo diff --git a/Demo/MainViewController.cs b/Demo/MainViewController.cs index a61f23a..e22956f 100644 --- a/Demo/MainViewController.cs +++ b/Demo/MainViewController.cs @@ -1,60 +1,48 @@ - using System; -using System.Drawing; - using MonoTouch.Foundation; using MonoTouch.UIKit; -using XibFree; namespace Demo { - public partial class MainViewController : UITableViewController + public sealed class MainViewController : UITableViewController { public MainViewController() : base(UITableViewStyle.Grouped) { - this.Title = "XibFree Demos"; - } - - public override void DidReceiveMemoryWarning() - { - // Releases the view if it doesn't have a superview. - base.DidReceiveMemoryWarning(); - - // Release any cached data, images, etc that aren't in use. + Title = "XibFree Demos"; } - + public override void ViewDidLoad() { base.ViewDidLoad(); - this.TableView.Source = new Source(this); + TableView.Source = new Source(this); } - class Source : UITableViewSource + private class Source : UITableViewSource { + private readonly MainViewController _owner; + public Source(MainViewController owner) { _owner = owner; } - MainViewController _owner; - - class Demo + private class Demo { public string Title; - public Type TClass; + public Type Class; }; - Demo[] _demos = new Demo[] + private readonly Demo[] _demos = new[] { - new Demo() { Title = "#1 Basics", TClass = typeof(Demo1) }, - new Demo() { Title = "LinearLayout", TClass = typeof(LinearLayoutDemo) }, - new Demo() { Title = "FrameLayout", TClass = typeof(FrameLayoutDemo) }, - new Demo() { Title = "Nested Hosts", TClass = typeof(NestedDemo) }, - new Demo() { Title = "ViewGroup Layers", TClass = typeof(ViewGroupLayerDemo) }, - new Demo() { Title = "TableViewCell", TClass = typeof(TableViewCellDemo) }, - new Demo() { Title = "TableViewCell Variable", TClass = typeof(TableViewCellDemo2) }, - new Demo() { Title = "Visibility", TClass = typeof(VisibilityDemo) }, - new Demo() { Title = "Recalculate Layout", TClass = typeof(RecalculateLayoutDemo) }, + new Demo { Title = "#1 Basics", Class = typeof(Demo1) }, + new Demo { Title = "LinearLayout", Class = typeof(LinearLayoutDemo) }, + new Demo { Title = "FrameLayout", Class = typeof(FrameLayoutDemo) }, + new Demo { Title = "Nested Hosts", Class = typeof(NestedDemo) }, + new Demo { Title = "ViewGroup Layers", Class = typeof(ViewGroupLayerDemo) }, + new Demo { Title = "TableViewCell", Class = typeof(TableViewCellDemo) }, + new Demo { Title = "TableViewCell Variable", Class = typeof(TableViewCellDemo2) }, + new Demo { Title = "Visibility", Class = typeof(VisibilityDemo) }, + new Demo { Title = "Recalculate Layout", Class = typeof(RecalculateLayoutDemo) } }; #region implemented abstract members of UITableViewSource @@ -78,7 +66,7 @@ public override UITableViewCell GetCell(UITableView tableView, NSIndexPath index public override void RowSelected(UITableView tableView, NSIndexPath indexPath) { - var vc = (UIViewController)Activator.CreateInstance(_demos[indexPath.Row].TClass); + var vc = (UIViewController)Activator.CreateInstance(_demos[indexPath.Row].Class); _owner.NavigationController.PushViewController(vc, true); } #endregion diff --git a/Demo/NestedDemo.cs b/Demo/NestedDemo.cs index 97736d4..4ccb888 100644 --- a/Demo/NestedDemo.cs +++ b/Demo/NestedDemo.cs @@ -1,15 +1,10 @@ -using System; using System.Drawing; -using System.Collections.Generic; - using MonoTouch.UIKit; -using MonoTouch.Foundation; - using XibFree; namespace Demo { - public partial class NestedDemo : UIViewController + public sealed class NestedDemo : UIViewController { public NestedDemo() { @@ -24,14 +19,21 @@ public override void LoadView() { SubViews = new View[] { - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + }, }, - new NativeView() + new NativeView { - View = new UILayoutHost() + View = new UILayoutHost { BackgroundColor = UIColor.Yellow, Layout = new LinearLayout(Orientation.Vertical) @@ -39,7 +41,7 @@ public override void LoadView() Padding = new UIEdgeInsets(3,3,3,3), SubViews = new View[] { - new NativeView() + new NativeView { View = new UILabel(RectangleF.Empty) { @@ -48,7 +50,7 @@ public override void LoadView() BackgroundColor = UIColor.Clear, } }, - new NativeView() + new NativeView { View = new UILabel(RectangleF.Empty) { @@ -58,10 +60,10 @@ public override void LoadView() } } }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, Margins = new UIEdgeInsets(10,10,10,10), }, }, @@ -72,17 +74,24 @@ public override void LoadView() v.View.Layer.MasksToBounds = true; } }, - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), - }, + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + } + } }, }; // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor=UIColor.Gray; } } } diff --git a/Demo/RecalculateLayoutDemo.cs b/Demo/RecalculateLayoutDemo.cs index 3a44a88..0392b7b 100644 --- a/Demo/RecalculateLayoutDemo.cs +++ b/Demo/RecalculateLayoutDemo.cs @@ -1,16 +1,14 @@ -using System; using System.Drawing; -using System.Collections.Generic; using MonoTouch.UIKit; using MonoTouch.Foundation; +using MonoTouch.CoreAnimation; using XibFree; -using MonoTouch.CoreAnimation; namespace Demo { - public partial class RecalculateLayoutDemo : UIViewController + public sealed class RecalculateLayoutDemo : UIViewController { public RecalculateLayoutDemo() { @@ -26,17 +24,24 @@ public override void LoadView() { SubViews = new View[] { - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50) + }, }, new LinearLayout(Orientation.Vertical) { Padding = new UIEdgeInsets(10,10,10,10), - Layer = new CAGradientLayer() + Layer = new CAGradientLayer { - Colors = new MonoTouch.CoreGraphics.CGColor[] + Colors = new[] { new MonoTouch.CoreGraphics.CGColor(0.9f, 0.9f, 0.9f, 1f), new MonoTouch.CoreGraphics.CGColor(0.7f, 0.7f, 0.7f, 1f) @@ -50,7 +55,7 @@ public override void LoadView() }, SubViews = new View[] { - new NativeView() + new NativeView { View = new UILabel(RectangleF.Empty) { @@ -59,7 +64,7 @@ public override void LoadView() BackgroundColor = UIColor.Clear, } }, - new NativeView() + new NativeView { View = label = new UILabel(RectangleF.Empty) { @@ -68,25 +73,40 @@ public override void LoadView() BackgroundColor = UIColor.Clear, Lines = 0, }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, AutoSize.WrapContent) + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.WrapContent + } } }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, Margins = new UIEdgeInsets(10,10,10,10), }, }, - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + }, }, - new NativeView() + new NativeView { View = new UIButton(UIButtonType.RoundedRect), - LayoutParameters = new LayoutParameters(AutoSize.FillParent, AutoSize.WrapContent), + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.WrapContent + }, Init = v => { v.As().SetTitle("Change Text", UIControlState.Normal); @@ -111,8 +131,8 @@ public override void LoadView() }; // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor = UIColor.Gray; } } } diff --git a/Demo/TableViewCellDemo.cs b/Demo/TableViewCellDemo.cs index 9bdc433..7134542 100644 --- a/Demo/TableViewCellDemo.cs +++ b/Demo/TableViewCellDemo.cs @@ -9,11 +9,11 @@ namespace Demo { - public partial class TableViewCellDemo : UITableViewController + public sealed class TableViewCellDemo : UITableViewController { public TableViewCellDemo() : base(UITableViewStyle.Grouped) { - this.Title = "TableViewCell"; + Title = "TableViewCell"; } public override void ViewDidLoad() @@ -22,105 +22,109 @@ public override void ViewDidLoad() // Create some items var r = new Random(); - for (int i=0; i<100; i++) + for (var i=0; i<100; i++) { - var item = new Item(); - item.Title = string.Format("Item {0}", i+1); - item.Total = r.Next(1000); + var item = new Item + { + Title = string.Format("Item {0}", i + 1), + Total = r.Next(1000) + }; item.Count = r.Next(item.Total); _items.Add(item); } - this.TableView.Source = new Source(this); - this.TableView.RowHeight = new DemoTableViewCell().MeasureHeight(); + TableView.Source = new Source(this); + TableView.RowHeight = new DemoTableViewCell().MeasureHeight(); } - class Item + private class Item { public string Title; public int Count; public int Total; public int Percentage { - get - { - return Count * 100 / Total; - } + get { return Count * 100 / Total; } } }; - List _items = new List(); + private readonly List _items = new List(); - class DemoTableViewCell : UITableViewCell + private sealed class DemoTableViewCell : UITableViewCell { + private readonly ViewGroup _layout; + private readonly UILabel _labelTitle; + private readonly UILabel _labelSubTitle; + private readonly UILabel _labelPercent; + public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCell") { _layout = new LinearLayout(Orientation.Horizontal) { Padding = new UIEdgeInsets(5,5,5,5), - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, }, SubViews = new View[] { - new NativeView() + new NativeView { View = new UIImageView(RectangleF.Empty) { Image = UIImage.FromBundle("tts512.png"), }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = 40, - Height = 40, + Width = Dimension.Absolute(40), + Height = Dimension.Absolute(40), Margins = new UIEdgeInsets(0,0,0,10), } }, new LinearLayout(Orientation.Vertical) { - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, }, SubViews = new View[] { - new NativeView() + new NativeView { - View = _labelTitle = new UILabel() + View = _labelTitle = new UILabel { BackgroundColor = UIColor.Clear, Font = UIFont.BoldSystemFontOfSize(18), HighlightedTextColor = UIColor.White, }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, } }, - new NativeView() + new NativeView { - View = _labelSubTitle = new UILabel() + View = _labelSubTitle = new UILabel { BackgroundColor = UIColor.Clear, Font = UIFont.SystemFontOfSize(12), TextColor = UIColor.DarkGray, HighlightedTextColor = UIColor.White, }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, } - }, + } } }, - new NativeView() + new NativeView { - View = _labelPercent = new UILabel() + View = _labelPercent = new UILabel { BackgroundColor = UIColor.Clear, TextColor = UIColor.FromRGB(51,102,153), @@ -128,10 +132,10 @@ public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCe Font = UIFont.BoldSystemFontOfSize(24), TextAlignment = UITextAlignment.Right, }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = 50, - Height = AutoSize.FillParent, + Width = Dimension.Absolute(50), + Height = Dimension.FillParent, Margins = new UIEdgeInsets(0, 10, 0, 0), } } @@ -139,8 +143,8 @@ public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCe }; - this.ContentView.Add(new UILayoutHost(_layout, this.ContentView.Bounds)); - this.Accessory = UITableViewCellAccessory.DisclosureIndicator; + ContentView.Add(new UILayoutHost(_layout, ContentView.Bounds)); + Accessory = UITableViewCellAccessory.DisclosureIndicator; } public void Init(Item i) @@ -157,22 +161,17 @@ public float MeasureHeight() _layout.Measure(float.MaxValue, float.MaxValue); return _layout.GetMeasuredSize().Height; } - - ViewGroup _layout; - UILabel _labelTitle; - UILabel _labelSubTitle; - UILabel _labelPercent; } class Source : UITableViewSource { + private readonly TableViewCellDemo _owner; + public Source(TableViewCellDemo owner) { _owner = owner; } - TableViewCellDemo _owner; - #region implemented abstract members of UITableViewSource public override int RowsInSection(UITableView tableview, int section) @@ -182,11 +181,7 @@ public override int RowsInSection(UITableView tableview, int section) public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) { - var cell = (DemoTableViewCell)tableView.DequeueReusableCell("DemoTableViewCell"); - if (cell==null) - { - cell = new DemoTableViewCell(); - } + var cell = (DemoTableViewCell)tableView.DequeueReusableCell("DemoTableViewCell") ?? new DemoTableViewCell(); var item = _owner._items[indexPath.Row]; cell.Init(item); diff --git a/Demo/TableViewCellDemo2.cs b/Demo/TableViewCellDemo2.cs index b65f285..1d11d7a 100644 --- a/Demo/TableViewCellDemo2.cs +++ b/Demo/TableViewCellDemo2.cs @@ -1,251 +1,245 @@ +using System; +using System.Drawing; - using System; - using System.Drawing; +using MonoTouch.Foundation; +using MonoTouch.UIKit; +using XibFree; +using System.Collections.Generic; - using MonoTouch.Foundation; - using MonoTouch.UIKit; - using XibFree; - using System.Collections.Generic; - - namespace Demo +namespace Demo +{ + public sealed class TableViewCellDemo2 : UITableViewController { - public partial class TableViewCellDemo2 : UITableViewController + private readonly List _items = new List(); + + public TableViewCellDemo2() : base(UITableViewStyle.Grouped) { - public TableViewCellDemo2() : base(UITableViewStyle.Grouped) - { - this.Title = "TableViewCell"; - } + Title = "TableViewCell"; + } - public override void ViewDidLoad() + public override void ViewDidLoad() + { + base.ViewDidLoad(); + + var messages = new[] { - base.ViewDidLoad(); + "Short message", + "A medium length message", + "A somewhat longer message that may wrap", + "A really long message that really really should wrap. This will allow us to properly test text wrapping when used inside a variable height table view cell" + }; - string[] messages = new string[] + // Create some items + var r = new Random(); + for (var i=0; i<100; i++) + { + var item = new Item { - "Short message", - "A medium length message", - "A somewhat longer message that may wrap", - "A really long message that really really should wrap. This will allow us to properly test text wrapping when used inside a variable height table view cell", + Title = string.Format("Item {0}", i + 1), + Total = r.Next(1000) }; - - // Create some items - var r = new Random(); - for (int i=0; i<100; i++) - { - var item = new Item(); - item.Title = string.Format("Item {0}", i+1); - item.Total = r.Next(1000); - item.Count = r.Next(item.Total); - item.LongText = messages[r.Next(messages.Length)]; - _items.Add(item); - } - - // Setup the datasource/delegate - this.TableView.Source = new Source(this); + item.Count = r.Next(item.Total); + item.LongText = messages[r.Next(messages.Length)]; + _items.Add(item); } - class Item + // Setup the datasource/delegate + TableView.Source = new Source(this); + } + + class Item + { + public string Title; + public int Count; + public int Total; + public int Percentage { - public string Title; - public int Count; - public int Total; - public int Percentage + get { - get - { - return Count * 100 / Total; - } + return Count * 100 / Total; } - public string LongText; - }; + } + public string LongText; + }; + + - List _items = new List(); + private sealed class DemoTableViewCell : UITableViewCell + { + private readonly UILabel _labelTitle; + private readonly UILabel _labelSubTitle; + private readonly UILabel _labelPercent; + private readonly UILabel _labelLongText; + private ViewGroup Layout { get; set; } - class DemoTableViewCell : UITableViewCell + public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCell") { - public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCell") + Layout = new LinearLayout(Orientation.Horizontal) { - Layout = new LinearLayout(Orientation.Horizontal) + Padding = new UIEdgeInsets(5,5,5,5), + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.WrapContent, + }, + SubViews = new View[] { - Padding = new UIEdgeInsets(5,5,5,5), - LayoutParameters = new LayoutParameters() + new NativeView { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + View = new UIImageView(RectangleF.Empty) + { + Image = UIImage.FromBundle("tts512.png"), + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.Absolute(40), + Height = Dimension.Absolute(40), + Margins = new UIEdgeInsets(0,0,0,10), + } }, - SubViews = new View[] + new LinearLayout(Orientation.Vertical) { - new NativeView() + LayoutParameters = new LayoutParameters { - View = new UIImageView(RectangleF.Empty) - { - Image = UIImage.FromBundle("tts512.png"), - }, - LayoutParameters = new LayoutParameters() - { - Width = 40, - Height = 40, - Margins = new UIEdgeInsets(0,0,0,10), - } + Width = Dimension.FillParent, + Height = Dimension.WrapContent, }, - new LinearLayout(Orientation.Vertical) + SubViews = new View[] { - LayoutParameters = new LayoutParameters() + new NativeView { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + View = _labelTitle = new UILabel + { + Text = "Title", + BackgroundColor = UIColor.Clear, + Font = UIFont.BoldSystemFontOfSize(18), + HighlightedTextColor = UIColor.White, + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.WrapContent, + } }, - SubViews = new View[] + new NativeView { - new NativeView() + View = _labelSubTitle = new UILabel { - View = _labelTitle = new UILabel() - { - Text = "Title", - BackgroundColor = UIColor.Clear, - Font = UIFont.BoldSystemFontOfSize(18), - HighlightedTextColor = UIColor.White, - }, - LayoutParameters = new LayoutParameters() - { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, - } + Text = "SubTitle", + BackgroundColor = UIColor.Clear, + Font = UIFont.SystemFontOfSize(12), + TextColor = UIColor.DarkGray, + HighlightedTextColor = UIColor.White, }, - new NativeView() + LayoutParameters = new LayoutParameters { - View = _labelSubTitle = new UILabel() - { - Text = "SubTitle", - BackgroundColor = UIColor.Clear, - Font = UIFont.SystemFontOfSize(12), - TextColor = UIColor.DarkGray, - HighlightedTextColor = UIColor.White, - }, - LayoutParameters = new LayoutParameters() - { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, - } + Width = Dimension.FillParent, + Height = Dimension.WrapContent, + } + }, + new NativeView + { + View = _labelLongText = new UILabel + { + BackgroundColor = UIColor.Clear, + Font = UIFont.SystemFontOfSize(12), + TextColor = UIColor.DarkGray, + HighlightedTextColor = UIColor.White, + Lines = 0, }, - new NativeView() + LayoutParameters = new LayoutParameters { - View = _labelLongText = new UILabel() - { - BackgroundColor = UIColor.Clear, - Font = UIFont.SystemFontOfSize(12), - TextColor = UIColor.DarkGray, - HighlightedTextColor = UIColor.White, - Lines = 0, - }, - LayoutParameters = new LayoutParameters() - { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, - } + Width = Dimension.FillParent, + Height = Dimension.WrapContent, } } + } + }, + new NativeView + { + View = _labelPercent = new UILabel + { + Text = "20%", + BackgroundColor = UIColor.Clear, + TextColor = UIColor.FromRGB(51,102,153), + HighlightedTextColor = UIColor.White, + Font = UIFont.BoldSystemFontOfSize(24), + TextAlignment = UITextAlignment.Right, }, - new NativeView() + LayoutParameters = new LayoutParameters { - View = _labelPercent = new UILabel() - { - Text = "20%", - BackgroundColor = UIColor.Clear, - TextColor = UIColor.FromRGB(51,102,153), - HighlightedTextColor = UIColor.White, - Font = UIFont.BoldSystemFontOfSize(24), - TextAlignment = UITextAlignment.Right, - }, - LayoutParameters = new LayoutParameters() - { - Width = 50, - Height = AutoSize.FillParent, - Margins = new UIEdgeInsets(0, 10, 0, 0), - } + Width = Dimension.Absolute(50), + Height = Dimension.FillParent, + Margins = new UIEdgeInsets(0, 10, 0, 0), } } - }; - - - this.ContentView.Add(new UILayoutHost(Layout, this.ContentView.Bounds)); - this.Accessory = UITableViewCellAccessory.DisclosureIndicator; - } + } + }; - public void Init(Item i) - { - _labelTitle.Text = i.Title; - _labelSubTitle.Text = string.Format("{0} of {1}", i.Count, i.Total); - _labelPercent.Text = string.Format("{0}%", i.Percentage); - _labelLongText.Text = i.LongText; - } - public float MeasureHeight(UITableView tableView, Item i) - { - // Initialize the view's so they have the correct content for height calculations - Init(i); + ContentView.Add(new UILayoutHost(Layout, ContentView.Bounds)); + Accessory = UITableViewCellAccessory.DisclosureIndicator; + } - // Remeasure the layout using the tableView width, allowing for grouped table view margins - // and the disclosure indicator - Layout.Measure(tableView.Bounds.Width - 20 - 18, float.MaxValue); + public void Init(Item i) + { + _labelTitle.Text = i.Title; + _labelSubTitle.Text = string.Format("{0} of {1}", i.Count, i.Total); + _labelPercent.Text = string.Format("{0}%", i.Percentage); + _labelLongText.Text = i.LongText; + } - // Grab the measured height - return Layout.GetMeasuredSize().Height; - } + public float MeasureHeight(UITableView tableView, Item i) + { + // Initialize the view's so they have the correct content for height calculations + Init(i); - public ViewGroup Layout - { - get; - set; - } + // Remeasure the layout using the tableView width, allowing for grouped table view margins + // and the disclosure indicator + Layout.Measure(tableView.Bounds.Width - 20 - 18, float.MaxValue); - UILabel _labelTitle; - UILabel _labelSubTitle; - UILabel _labelPercent; - UILabel _labelLongText; + // Grab the measured height + return Layout.GetMeasuredSize().Height; } + } - class Source : UITableViewSource - { - public Source(TableViewCellDemo2 owner) - { - _owner = owner; - } + private class Source : UITableViewSource + { + private readonly TableViewCellDemo2 _owner; - TableViewCellDemo2 _owner; + private readonly DemoTableViewCell _prototype = new DemoTableViewCell(); - DemoTableViewCell _prototype = new DemoTableViewCell(); + public Source(TableViewCellDemo2 owner) + { + _owner = owner; + } - #region implemented abstract members of UITableViewSource - public override int RowsInSection(UITableView tableview, int section) - { - return _owner._items.Count; - } - public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) - { - var cell = (DemoTableViewCell)tableView.DequeueReusableCell("DemoTableViewCell"); - if (cell==null) - { - cell = new DemoTableViewCell(); - } + #region implemented abstract members of UITableViewSource + public override int RowsInSection(UITableView tableview, int section) + { + return _owner._items.Count; + } + public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) + { + var cell = (DemoTableViewCell)tableView.DequeueReusableCell("DemoTableViewCell") ?? new DemoTableViewCell(); - var item = _owner._items[indexPath.Row]; - cell.Init(item); + var item = _owner._items[indexPath.Row]; + cell.Init(item); - return cell; - } + return cell; + } - public override void RowSelected(UITableView tableView, NSIndexPath indexPath) - { - } + public override void RowSelected(UITableView tableView, NSIndexPath indexPath) + { + } - public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) - { - return _prototype.MeasureHeight(tableView, _owner._items[indexPath.Row]); - } - #endregion + public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) + { + return _prototype.MeasureHeight(tableView, _owner._items[indexPath.Row]); } + #endregion } } +} diff --git a/Demo/ViewGroupLayerDemo.cs b/Demo/ViewGroupLayerDemo.cs index bf4145a..35ae317 100644 --- a/Demo/ViewGroupLayerDemo.cs +++ b/Demo/ViewGroupLayerDemo.cs @@ -1,7 +1,4 @@ -using System; using System.Drawing; -using System.Collections.Generic; - using MonoTouch.UIKit; using MonoTouch.Foundation; @@ -10,7 +7,7 @@ namespace Demo { - public partial class ViewGroupLayerDemo : UIViewController + public sealed class ViewGroupLayerDemo : UIViewController { public ViewGroupLayerDemo() { @@ -25,17 +22,24 @@ public override void LoadView() { SubViews = new View[] { - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + }, }, new LinearLayout(Orientation.Vertical) { Padding = new UIEdgeInsets(10,10,10,10), - Layer = new CAGradientLayer() + Layer = new CAGradientLayer { - Colors = new MonoTouch.CoreGraphics.CGColor[] + Colors = new[] { new MonoTouch.CoreGraphics.CGColor(0.9f, 0.9f, 0.9f, 1f), new MonoTouch.CoreGraphics.CGColor(0.7f, 0.7f, 0.7f, 1f) @@ -49,7 +53,7 @@ public override void LoadView() }, SubViews = new View[] { - new NativeView() + new NativeView { View = new UILabel(RectangleF.Empty) { @@ -58,7 +62,7 @@ public override void LoadView() BackgroundColor = UIColor.Clear, } }, - new NativeView() + new NativeView { View = new UILabel(RectangleF.Empty) { @@ -68,24 +72,28 @@ public override void LoadView() } } }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, Margins = new UIEdgeInsets(10,10,10,10), }, }, - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), - }, + View = new UIView { BackgroundColor = UIColor.Blue }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + }, + } }, }; // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor=UIColor.Gray; } } } diff --git a/Demo/VisibilityDemo.cs b/Demo/VisibilityDemo.cs index f7295bc..f782eef 100644 --- a/Demo/VisibilityDemo.cs +++ b/Demo/VisibilityDemo.cs @@ -1,16 +1,14 @@ -using System; using System.Drawing; -using System.Collections.Generic; using MonoTouch.UIKit; using MonoTouch.Foundation; +using MonoTouch.CoreAnimation; using XibFree; -using MonoTouch.CoreAnimation; namespace Demo { - public partial class VisibilityDemo : UIViewController + public sealed class VisibilityDemo : UIViewController { public VisibilityDemo() { @@ -26,17 +24,24 @@ public override void LoadView() { SubViews = new View[] { - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + }, }, new LinearLayout(Orientation.Vertical) { Padding = new UIEdgeInsets(10,10,10,10), - Layer = new CAGradientLayer() + Layer = new CAGradientLayer { - Colors = new MonoTouch.CoreGraphics.CGColor[] + Colors = new[] { new MonoTouch.CoreGraphics.CGColor(0.9f, 0.9f, 0.9f, 1f), new MonoTouch.CoreGraphics.CGColor(0.7f, 0.7f, 0.7f, 1f) @@ -48,9 +53,9 @@ public override void LoadView() }, CornerRadius = 5, }, - SubViews = new View[] + SubViews = new[] { - new NativeView() + new NativeView { View = new UILabel(RectangleF.Empty) { @@ -59,7 +64,7 @@ public override void LoadView() BackgroundColor = UIColor.Clear, } }, - panel = new NativeView() + panel = new NativeView { View = new UILabel(RectangleF.Empty) { @@ -69,22 +74,33 @@ public override void LoadView() } } }, - LayoutParameters = new LayoutParameters() + LayoutParameters = new LayoutParameters { - Width = AutoSize.FillParent, - Height = AutoSize.WrapContent, + Width = Dimension.FillParent, + Height = Dimension.WrapContent, Margins = new UIEdgeInsets(10,10,10,10), }, }, - new NativeView() + new NativeView { - View = new UIView() { BackgroundColor = UIColor.Blue }, - LayoutParameters = new LayoutParameters(AutoSize.FillParent, 50), + View = new UIView + { + BackgroundColor = UIColor.Blue + }, + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.Absolute(50), + }, }, - new NativeView() + new NativeView { View = new UIButton(UIButtonType.RoundedRect), - LayoutParameters = new LayoutParameters(AutoSize.FillParent, AutoSize.WrapContent), + LayoutParameters = new LayoutParameters + { + Width = Dimension.FillParent, + Height = Dimension.FillParent, + }, Init = v => { v.As().SetTitle("Change Visibility", UIControlState.Normal); @@ -103,7 +119,7 @@ public override void LoadView() break; } - this.View.SetNeedsLayout(); + View.SetNeedsLayout(); }; } } @@ -111,8 +127,8 @@ public override void LoadView() }; // We've now defined our layout, to actually use it we simply create a UILayoutHost control and pass it the layout - this.View = new XibFree.UILayoutHost(layout); - this.View.BackgroundColor=UIColor.Gray; + View = new UILayoutHost(layout); + View.BackgroundColor = UIColor.Gray; } } } diff --git a/XibFree.sln b/XibFree.sln index c24adf9..f874af9 100644 --- a/XibFree.sln +++ b/XibFree.sln @@ -1,36 +1,28 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XibFree", "XibFree\XibFree.csproj", "{3CAF9EB6-5E29-40E2-8798-2DE909310CBE}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|iPhoneSimulator = Debug|iPhoneSimulator - Release|iPhoneSimulator = Release|iPhoneSimulator - Debug|iPhone = Debug|iPhone - Release|iPhone = Release|iPhone Ad-Hoc|iPhone = Ad-Hoc|iPhone + Ad-Hoc|iPhoneSimulator = Ad-Hoc|iPhoneSimulator AppStore|iPhone = AppStore|iPhone + AppStore|iPhoneSimulator = AppStore|iPhoneSimulator + Debug|iPhone = Debug|iPhone + Debug|iPhoneSimulator = Debug|iPhoneSimulator + Release|iPhone = Release|iPhone + Release|iPhoneSimulator = Release|iPhoneSimulator EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.AppStore|iPhone.ActiveCfg = AppStore|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.AppStore|iPhone.Build.0 = AppStore|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhone.ActiveCfg = Debug|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhone.Build.0 = Debug|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhone.ActiveCfg = Release|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhone.Build.0 = Release|iPhone - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator - {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Ad-Hoc|iPhone.Build.0 = Release|Any CPU + {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.AppStore|iPhone.ActiveCfg = Release|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.AppStore|iPhone.Build.0 = Release|Any CPU + {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Debug|iPhone.ActiveCfg = Debug|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Debug|iPhone.Build.0 = Debug|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU @@ -39,6 +31,25 @@ Global {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Release|iPhone.Build.0 = Release|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU {3CAF9EB6-5E29-40E2-8798-2DE909310CBE}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Ad-Hoc|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Ad-Hoc|iPhoneSimulator.Build.0 = Ad-Hoc|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.AppStore|iPhone.ActiveCfg = AppStore|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.AppStore|iPhone.Build.0 = AppStore|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.AppStore|iPhoneSimulator.ActiveCfg = AppStore|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.AppStore|iPhoneSimulator.Build.0 = AppStore|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhone.ActiveCfg = Debug|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhone.Build.0 = Debug|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhone.ActiveCfg = Release|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhone.Build.0 = Release|iPhone + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator + {3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = Demo\Demo.csproj diff --git a/XibFree/AutoSize.cs b/XibFree/AutoSize.cs deleted file mode 100644 index 346cd11..0000000 --- a/XibFree/AutoSize.cs +++ /dev/null @@ -1,38 +0,0 @@ -// XibFree - http://www.toptensoftware.com/xibfree/ -// -// Copyright 2013 Copyright © 2013 Topten Software. All Rights Reserved -// -// Licensed 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. - -using System; - -namespace XibFree -{ - /// - /// AutoSize defines special float constants to indicate that views dimensions should - /// be automatically determined by wrapping the views content, or matching the parent view's size - /// - public static class AutoSize - { - /// - /// Specifies that a view's width or height should fill the available space in the parent view group. - /// - public const float FillParent = -1; - - /// - /// Specifies that a view's width or height should wrap the content contained within it. - /// - public const float WrapContent = -2; - } -} - diff --git a/XibFree/Dimension.cs b/XibFree/Dimension.cs index 273e6c6..b3d056c 100644 --- a/XibFree/Dimension.cs +++ b/XibFree/Dimension.cs @@ -1,26 +1,24 @@ -using System; - namespace XibFree { public class Dimension { - private float _value; - private Units _unit; + public float Value { get; private set; } + public Units Unit { get; private set; } public Dimension(float value, Units units = Units.Absolute) { - _value = value; - _unit = units; + Value = value; + Unit = units; } public static Dimension FillParent { - get { return Dimension.ParentRatio(1.0f); } + get { return ParentRatio(1.0f); } } public static Dimension WrapContent { - get { return Dimension.ContentRatio(1.0f); } + get { return ContentRatio(1.0f); } } public static Dimension ParentRatio(float value) @@ -40,17 +38,12 @@ public static Dimension ContentRatio(float value) public static Dimension Absolute(float value) { - return new Dimension(value, Units.Absolute); + return new Dimension(value); } - public float Value { get { return _value; } } - public Units Unit { get { return _unit; } } public float Ratio { - get - { - return (_unit == Units.Absolute) ? 1 : _value; - } + get { return (Unit == Units.Absolute) ? 1 : Value; } } } } diff --git a/XibFree/Extensions.cs b/XibFree/Extensions.cs index 398188c..4a46672 100644 --- a/XibFree/Extensions.cs +++ b/XibFree/Extensions.cs @@ -22,6 +22,16 @@ namespace XibFree { internal static class Extensions { + public static bool IsEqualTo(this float f, float other) + { + return Math.Abs(f - other) < float.Epsilon; + } + + public static bool IsMaxFloat(this float f) + { + return Math.Abs(f - float.MaxValue) < float.Epsilon; + } + /// /// Applies a set of UIEdgeInsets to a RectangleF /// @@ -45,26 +55,24 @@ public static float TotalHeight(this UIEdgeInsets insets) public static RectangleF ApplyGravity(this RectangleF bounds, SizeF size, Gravity g) { - float left; + float left = 0; switch (g & Gravity.HorizontalMask) { - default: + case Gravity.Left: left = bounds.Left; break; - case Gravity.Right: left = bounds.Right - size.Width; break; - case Gravity.CenterHorizontal: left = (bounds.Left + bounds.Right - size.Width)/2; break; } - float top; + float top = 0; switch (g & Gravity.VerticalMask) { - default: + case Gravity.Top: top = bounds.Top; break; diff --git a/XibFree/FrameLayout.cs b/XibFree/FrameLayout.cs index 9f8671b..0c4d778 100644 --- a/XibFree/FrameLayout.cs +++ b/XibFree/FrameLayout.cs @@ -21,13 +21,22 @@ namespace XibFree { - public class FrameLayout : ViewGroup + public sealed class FrameLayout : ViewGroup { public FrameLayout() { + LayoutParameters.Width = Dimension.FillParent; + LayoutParameters.Height = Dimension.FillParent; } - protected override void onMeasure(float parentWidth, float parentHeight) + public Gravity Gravity { get; set; } + + public Action Init + { + set { value(this); } + } + + protected override void OnMeasure(float parentWidth, float parentHeight) { var unresolved = new List(); @@ -35,21 +44,21 @@ protected override void onMeasure(float parentWidth, float parentHeight) var height = LayoutParameters.TryResolveHeight(this, parentHeight); // Remove padding - if (width!=float.MaxValue) - width -= Padding.TotalWidth(); - if (height!=float.MaxValue) - height -= Padding.TotalHeight(); + if (!width.IsMaxFloat()) width -= Padding.TotalWidth(); + if (!height.IsMaxFloat()) height -= Padding.TotalHeight(); // Measure all subviews where both dimensions can be resolved - bool haveResolvedSize = false; - float maxWidth=0, maxHeight=0; + var haveResolvedSize = false; + var maxWidth = 0f; + var maxHeight = 0f; + foreach (var v in SubViews.Where(x=>!x.Gone)) { // Try to resolve subview width var subViewWidth = float.MaxValue; if (v.LayoutParameters.Width.Unit == Units.ParentRatio) { - if (width==float.MaxValue) + if (width.IsMaxFloat()) { unresolved.Add(v); continue; @@ -64,7 +73,7 @@ protected override void onMeasure(float parentWidth, float parentHeight) var subViewHeight = float.MaxValue; if (v.LayoutParameters.Height.Unit == Units.ParentRatio) { - if (height==float.MaxValue) + if (height.IsMaxFloat()) { unresolved.Add(v); continue; @@ -111,14 +120,14 @@ protected override void onMeasure(float parentWidth, float parentHeight) v.Measure(subViewWidth, subViewHeight); } - SizeF sizeMeasured = SizeF.Empty; + var sizeMeasured = SizeF.Empty; - if (width == float.MaxValue) + if (width.IsMaxFloat()) { sizeMeasured.Width = SubViews.Max(x=>x.GetMeasuredSize().Width + x.LayoutParameters.Margins.TotalWidth()) + Padding.TotalWidth(); } - if (height == float.MaxValue) + if (height.IsMaxFloat()) { sizeMeasured.Height = SubViews.Max(x=>x.GetMeasuredSize().Height + x.LayoutParameters.Margins.TotalHeight()) + Padding.TotalHeight(); } @@ -127,7 +136,7 @@ protected override void onMeasure(float parentWidth, float parentHeight) SetMeasuredSize(LayoutParameters.ResolveSize(new SizeF(width, height), sizeMeasured)); } - protected override void onLayout(System.Drawing.RectangleF newPosition, bool parentHidden) + protected override void OnLayout(RectangleF newPosition, bool parentHidden) { // Make room for padding newPosition = newPosition.ApplyInsets(Padding); @@ -145,10 +154,7 @@ protected override void onLayout(System.Drawing.RectangleF newPosition, bool par // If subview has a gravity specified, use it, otherwise use our own var g = v.LayoutParameters.Gravity; - if (g==Gravity.None) - { - g = this.Gravity; - } + if (g == Gravity.None) g = Gravity; // Get it's size var size = v.GetMeasuredSize(); @@ -161,20 +167,6 @@ protected override void onLayout(System.Drawing.RectangleF newPosition, bool par } } } - - public Gravity Gravity - { - get; - set; - } - - public Action Init - { - set - { - value(this); - } - } } } diff --git a/XibFree/IHost.cs b/XibFree/IHost.cs new file mode 100644 index 0000000..e3dd542 --- /dev/null +++ b/XibFree/IHost.cs @@ -0,0 +1,9 @@ +using MonoTouch.UIKit; + +namespace XibFree +{ + public interface IHost + { + UIView GetUIView(); + } +} \ No newline at end of file diff --git a/XibFree/LayoutParameters.cs b/XibFree/LayoutParameters.cs index 2f02741..505ba19 100644 --- a/XibFree/LayoutParameters.cs +++ b/XibFree/LayoutParameters.cs @@ -14,7 +14,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; using MonoTouch.UIKit; using System.Drawing; @@ -56,10 +55,10 @@ public LayoutParameters() /// Width. /// Height. /// Weight. - public LayoutParameters(float width, float height, float weight=1) + public LayoutParameters(Dimension width, Dimension height, float weight = 1) { - Width = new Dimension(width); - Height = new Dimension(height); + Width = width; + Height = height; Margins = UIEdgeInsets.Zero; Weight = weight; Gravity = Gravity.None; @@ -69,31 +68,19 @@ public LayoutParameters(float width, float height, float weight=1) /// Gets or sets the width dimension (value and unit of measurement) /// /// The width. - public Dimension Width - { - get; - set; - } + public Dimension Width { get; set; } /// /// Gets or sets the height dimension (value and unit of measurement) /// /// The height. - public Dimension Height - { - get; - set; - } + public Dimension Height { get; set; } /// /// Gets or sets the weight of a AutoSize.FillParent view relative to its sibling views /// /// The weighting value for this view's size. - public float Weight - { - get; - set; - } + public float Weight { get; set; } /// /// Gets or sets the whitepsace margins that should be left around a view @@ -112,7 +99,6 @@ public UIEdgeInsets Margins } } - /// /// Gets or sets the left margin. /// @@ -181,61 +167,37 @@ public float MarginBottom /// Gets or sets the gravity for this view within it's parent subview /// /// One of the gravity constants - public Gravity Gravity - { - get; - set; - } + public Gravity Gravity { get; set; } /// /// Gets or sets the visibility of this view /// /// One of the Visibility constants - public Visibility Visibility - { - get; - set; - } + public Visibility Visibility { get; set; } /// /// Gets or sets the minimum width. /// /// The minimum width. - public float MinWidth - { - get; - set; - } + public float MinWidth { get; set; } /// /// Gets or sets the maximum width. /// /// The maximum width. - public float MaxWidth - { - get; - set; - } + public float MaxWidth { get; set; } /// /// Gets or sets the minimum height. /// /// The minimum height. - public float MinHeight - { - get; - set; - } + public float MinHeight { get; set; } /// /// Gets or sets the max height. /// /// The maximum height - public float MaxHeight - { - get; - set; - } + public float MaxHeight { get; set; } private static float TryResolve(Dimension dimension, float parentSize) { @@ -245,7 +207,7 @@ private static float TryResolve(Dimension dimension, float parentSize) case Units.Absolute: return dimension.Value; case Units.ParentRatio: - return parentSize == float.MaxValue ? float.MaxValue : parentSize * ratio; + return parentSize.IsMaxFloat() ? float.MaxValue : parentSize * ratio; default: return float.MaxValue; } @@ -269,7 +231,7 @@ internal SizeF GetHostSize(View view) { // Get the host var host = view.GetHost(); - if (host==null) return GetScreenSize(); + if (host == null) return GetScreenSize(); var hostView = host.GetUIView(); @@ -300,8 +262,8 @@ internal float TryResolveHeight(View view, float parentHeight) internal SizeF ResolveSize(SizeF size, SizeF sizeMeasured) { // Resolve measured size - if (size.Width == float.MaxValue) size.Width = sizeMeasured.Width; - if (size.Height == float.MaxValue) size.Height = sizeMeasured.Height; + if (size.Width.IsMaxFloat()) size.Width = sizeMeasured.Width; + if (size.Height.IsMaxFloat()) size.Height = sizeMeasured.Height; // Finally, resolve aspect ratios if (Width.Unit == Units.AspectRatio) size.Width = size.Height * Width.Ratio; diff --git a/XibFree/LinearLayout.cs b/XibFree/LinearLayout.cs index 2f6fff7..50962f2 100644 --- a/XibFree/LinearLayout.cs +++ b/XibFree/LinearLayout.cs @@ -20,17 +20,25 @@ namespace XibFree { - public class LinearLayout : ViewGroup + public sealed class LinearLayout : ViewGroup { // Fields - private Orientation _orientation; - private float _totalWeight; + private readonly Orientation _orientation; + + /// + /// Initializes a new instance of the class. + /// + public LinearLayout() + { + LayoutParameters.Width = Dimension.FillParent; + LayoutParameters.Height = Dimension.FillParent; + } /// /// Initializes a new instance of the class. /// /// Specifies the horizontal or vertical orientation of this layout. - public LinearLayout(Orientation orientation) + public LinearLayout(Orientation orientation) : this() { _orientation = orientation; Gravity = Gravity.TopLeft; @@ -42,37 +50,19 @@ public LinearLayout(Orientation orientation) /// The total weight. /// If not specified, the total weight is calculated by adding the LayoutParameters.Weight of /// each subview that has a size of FillParent. - public float TotalWeight - { - get - { - return _totalWeight; - } - set - { - _totalWeight = value; - } - } + public float TotalWeight { get; set; } /// /// Specifies the gravity for views contained within this layout /// /// One of the Gravity constants - public Gravity Gravity - { - get; - set; - } + public Gravity Gravity { get; set; } /// /// Gets or sets the spacing between stacked subviews /// /// The amount of spacing. - public float Spacing - { - get; - set; - } + public float Spacing { get; set; } /// /// Sets an init Action that allows performing actions on the View @@ -80,39 +70,30 @@ public float Spacing /// The init. public Action Init { - set - { - value(this); - } + set { value(this); } } // Overridden to provide layout measurement - protected override void onMeasure(float parentWidth, float parentHeight) + protected override void OnMeasure(float parentWidth, float parentHeight) { - if (_orientation==Orientation.Vertical) - { - MeasureVertical(parentWidth, parentHeight); - } - else - { - MeasureHorizontal(parentWidth, parentHeight); - } + if (_orientation==Orientation.Vertical) MeasureVertical(parentWidth, parentHeight); + else MeasureHorizontal(parentWidth, parentHeight); } // Do measurement when in vertical orientation private void MeasureVertical(float parentWidth, float parentHeight) { // Work out our width - float width = LayoutParameters.TryResolveWidth(this, parentWidth); - float height = LayoutParameters.TryResolveHeight(this, parentHeight); + var width = LayoutParameters.TryResolveWidth(this, parentWidth); + var height = LayoutParameters.TryResolveHeight(this, parentHeight); // Allow room for padding - if (width != float.MaxValue) width -= Padding.TotalWidth(); + if (!width.IsMaxFloat()) width -= Padding.TotalWidth(); // Work out the total fixed size - float totalFixedSize = 0; - float totalWeight = 0; - int visibleViewCount = 0; + var totalFixedSize = 0f; + var totalWeight = 0f; + var visibleViewCount = 0; foreach (var v in SubViews.Where(x => !x.Gone)) { if (v.LayoutParameters.Height.Unit == Units.ParentRatio) @@ -124,7 +105,7 @@ private void MeasureVertical(float parentWidth, float parentHeight) else { // Lay it out - v.Measure(adjustLayoutWidth(width, v), float.MaxValue); + v.Measure(AdjustLayoutWidth(width, v), float.MaxValue); totalFixedSize += v.GetMeasuredSize().Height; } @@ -141,33 +122,33 @@ private void MeasureVertical(float parentWidth, float parentHeight) if (visibleViewCount > 1) totalFixedSize += (visibleViewCount - 1) * Spacing; float totalVariableSize = 0; - if (LayoutParameters.Height.Unit == Units.ContentRatio || height == float.MaxValue) + if (LayoutParameters.Height.Unit == Units.ContentRatio || height.IsMaxFloat()) { // This is a weird case: we have a height of wrap content, but child items that want to fill parent too. // Temporarily switch those items to wrap content and use their natural size foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit == Units.ParentRatio)) { - v.Measure(adjustLayoutWidth(width, v), float.MaxValue); + v.Measure(AdjustLayoutWidth(width, v), float.MaxValue); totalVariableSize += v.GetMeasuredSize().Height; } } else { // If we've had an explicit weight passed to us, ignore the calculated total weight and use it instead - if (_totalWeight!=0) totalWeight = _totalWeight; + if (!TotalWeight.IsEqualTo(0)) totalWeight = TotalWeight; // Work out how much room we've got to share around - float room = height - totalFixedSize; + var room = height - totalFixedSize; // Layout the fill parent items foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit == Units.ParentRatio)) { // Work out size if (room<0) room = 0; - float size = (totalWeight == 0) ? room : room * v.LayoutParameters.Weight / totalWeight; + var size = (totalWeight.IsEqualTo(0)) ? room : room * v.LayoutParameters.Weight / totalWeight; // Measure it - v.Measure(adjustLayoutWidth(width, v), size); + v.Measure(AdjustLayoutWidth(width, v), size); // Update total size totalVariableSize += v.GetMeasuredSize().Height; @@ -178,15 +159,15 @@ private void MeasureVertical(float parentWidth, float parentHeight) } } - SizeF sizeMeasured = SizeF.Empty; + var sizeMeasured = SizeF.Empty; - if (width == float.MaxValue) + if (width.IsMaxFloat()) { // Work out the maximum width of all children that aren't fill parent sizeMeasured.Width = 0; foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit != Units.ParentRatio)) { - float totalChildWidth = v.GetMeasuredSize().Width + v.LayoutParameters.Margins.TotalWidth(); + var totalChildWidth = v.GetMeasuredSize().Width + v.LayoutParameters.Margins.TotalWidth(); if (totalChildWidth > sizeMeasured.Width) sizeMeasured.Width = totalChildWidth; } @@ -203,7 +184,7 @@ private void MeasureVertical(float parentWidth, float parentHeight) width += Padding.TotalWidth(); } - if (height == float.MaxValue) + if (height.IsMaxFloat()) { height = totalFixedSize + totalVariableSize; } @@ -216,16 +197,16 @@ private void MeasureVertical(float parentWidth, float parentHeight) private void MeasureHorizontal(float parentWidth, float parentHeight) { // Work out our height - float layoutWidth = LayoutParameters.TryResolveWidth(this, parentWidth); - float layoutHeight = LayoutParameters.TryResolveHeight(this, parentHeight); + var layoutWidth = LayoutParameters.TryResolveWidth(this, parentWidth); + var layoutHeight = LayoutParameters.TryResolveHeight(this, parentHeight); // Allow room for padding - if (layoutHeight != float.MaxValue) layoutHeight -= Padding.TotalHeight(); + if (!layoutHeight.IsMaxFloat()) layoutHeight -= Padding.TotalHeight(); // Work out the total fixed size - float totalFixedSize = 0; - float totalWeight = 0; - int visibleViewCount = 0; + var totalFixedSize = 0f; + var totalWeight = 0f; + var visibleViewCount = 0; foreach (var v in SubViews.Where(x=>!x.Gone)) { if (v.LayoutParameters.Width.Unit == Units.ParentRatio) @@ -238,7 +219,7 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) else { // Lay it out - v.Measure(float.MaxValue, adjustLayoutHeight(layoutHeight, v)); + v.Measure(float.MaxValue, AdjustLayoutHeight(layoutHeight, v)); totalFixedSize += v.GetMeasuredSize().Width; } @@ -255,34 +236,33 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) totalFixedSize += (visibleViewCount-1) * Spacing; float totalVariableSize = 0; - if (LayoutParameters.Width.Unit == Units.ContentRatio || layoutWidth == float.MaxValue) + if (LayoutParameters.Width.Unit == Units.ContentRatio || layoutWidth.IsMaxFloat()) { // This is a weird case: we have a width of wrap content, but child items that want to fill parent too. // Temporarily switch those items to wrap content and use their natural size foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit == Units.ParentRatio)) { - v.Measure(float.MaxValue, adjustLayoutHeight(layoutHeight, v)); + v.Measure(float.MaxValue, AdjustLayoutHeight(layoutHeight, v)); totalVariableSize += v.GetMeasuredSize().Width; } } else { // If we've had an explicit weight passed to us, ignore the calculated total weight and use it instead - if (_totalWeight!=0) - totalWeight = _totalWeight; + if (!TotalWeight.IsEqualTo(0)) totalWeight = TotalWeight; // Work out how much room we've got to share around - float room = layoutWidth - totalFixedSize; + var room = layoutWidth - totalFixedSize; // Layout the fill parent items foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Width.Unit == Units.ParentRatio)) { // Work out size if (room<0) room = 0; - float size = totalWeight==0 ? room : room * v.LayoutParameters.Weight / totalWeight; + var size = totalWeight.IsEqualTo(0) ? room : room * v.LayoutParameters.Weight / totalWeight; // Measure it - v.Measure(size, adjustLayoutHeight(layoutHeight, v)); + v.Measure(size, AdjustLayoutHeight(layoutHeight, v)); // Update total size totalVariableSize += v.GetMeasuredSize().Width; @@ -293,17 +273,16 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) } } - SizeF sizeMeasured = SizeF.Empty; + var sizeMeasured = SizeF.Empty; - if (layoutHeight == float.MaxValue) + if (layoutHeight.IsMaxFloat()) { // Work out the maximum height of all children that aren't fill parent sizeMeasured.Height = 0; foreach (var v in SubViews.Where(x => !x.Gone && x.LayoutParameters.Height.Unit != Units.ParentRatio)) { - float totalChildHeight = v.GetMeasuredSize().Height + v.LayoutParameters.Margins.TotalHeight(); - if (totalChildHeight > sizeMeasured.Height) - sizeMeasured.Height = totalChildHeight; + var totalChildHeight = v.GetMeasuredSize().Height + v.LayoutParameters.Margins.TotalHeight(); + if (totalChildHeight > sizeMeasured.Height) sizeMeasured.Height = totalChildHeight; } // Set the height of all children that are fill parent @@ -321,7 +300,7 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) - if (layoutWidth == float.MaxValue) + if (layoutWidth.IsMaxFloat()) { layoutWidth = totalFixedSize + totalVariableSize; } @@ -331,9 +310,9 @@ private void MeasureHorizontal(float parentWidth, float parentHeight) } // Overridden to layout the subviews - protected override void onLayout(RectangleF newPosition, bool parentHidden) + protected override void OnLayout(RectangleF newPosition, bool parentHidden) { - base.onLayout(newPosition, parentHidden); + base.OnLayout(newPosition, parentHidden); if (!parentHidden && Visible) { @@ -359,16 +338,16 @@ private void LayoutVertical(RectangleF newPosition) break; case Gravity.Bottom: - y = newPosition.Bottom - getTotalMeasuredHeight() + Padding.Top; + y = newPosition.Bottom - GetTotalMeasuredHeight() + Padding.Top; break; case Gravity.CenterVertical: - y = (newPosition.Top + newPosition.Bottom)/2 - getTotalMeasuredHeight()/2 + Padding.Top; + y = (newPosition.Top + newPosition.Bottom)/2 - GetTotalMeasuredHeight()/2 + Padding.Top; break; } - bool first = true; + var first = true; foreach (var v in SubViews) { @@ -387,7 +366,7 @@ private void LayoutVertical(RectangleF newPosition) y+= v.LayoutParameters.Margins.Top; - SizeF size = v.GetMeasuredSize(); + var size = v.GetMeasuredSize(); // Work out horizontal gravity for this control var g = v.LayoutParameters.Gravity & Gravity.HorizontalMask; @@ -429,16 +408,16 @@ private void LayoutHorizontal(RectangleF newPosition) break; case Gravity.Right: - x = newPosition.Right - getTotalMeasuredWidth() + Padding.Left; + x = newPosition.Right - GetTotalMeasuredWidth() + Padding.Left; break; case Gravity.CenterHorizontal: - x = (newPosition.Left + newPosition.Right)/2 - getTotalMeasuredWidth()/2 + Padding.Left; + x = (newPosition.Left + newPosition.Right)/2 - GetTotalMeasuredWidth()/2 + Padding.Left; break; } - bool first = true; + var first = true; foreach (var v in SubViews) { @@ -456,7 +435,7 @@ private void LayoutHorizontal(RectangleF newPosition) x += v.LayoutParameters.Margins.Left; - SizeF size = v.GetMeasuredSize(); + var size = v.GetMeasuredSize(); // Work out vertical gravity for this control var g = v.LayoutParameters.Gravity & Gravity.VerticalMask; @@ -487,46 +466,36 @@ private void LayoutHorizontal(RectangleF newPosition) } } - private float getTotalSpacing() + private float GetTotalSpacing() { - if (Spacing == 0) - return 0; + if (Spacing.IsEqualTo(0)) return 0; - int visibleViews = SubViews.Count(x=>!x.Gone); - if (visibleViews>1) - return (visibleViews-1) * Spacing; - else - return 0; + var visibleViews = SubViews.Count(x=>!x.Gone); + return visibleViews > 1 ? (visibleViews - 1)*Spacing : 0; } // Helper to get the total measured height of all subviews, including all padding and margins - private float getTotalMeasuredHeight() + private float GetTotalMeasuredHeight() { - return Padding.TotalWidth() + getTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Height + x.LayoutParameters.Margins.TotalHeight()); + return Padding.TotalWidth() + GetTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Height + x.LayoutParameters.Margins.TotalHeight()); } // Helper to get the total measured width of all subviews, including all padding and margins - private float getTotalMeasuredWidth() + private float GetTotalMeasuredWidth() { - return Padding.TotalHeight() + getTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Width + x.LayoutParameters.Margins.TotalWidth()); + return Padding.TotalHeight() + GetTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Width + x.LayoutParameters.Margins.TotalWidth()); } // Helper to adjust the parent width passed down to subviews during measurement - private float adjustLayoutWidth(float width, View c) + private static float AdjustLayoutWidth(float width, View c) { - if (width == float.MaxValue) - return width; - - return width - c.LayoutParameters.Margins.TotalWidth(); + return width.IsMaxFloat() ? width : width - c.LayoutParameters.Margins.TotalWidth(); } // Helper to adjust the parent height passed down to subviews during measurement - private float adjustLayoutHeight(float height, View c) + private static float AdjustLayoutHeight(float height, View c) { - if (height == float.MaxValue) - return height; - - return height - c.LayoutParameters.Margins.TotalHeight(); + return height.IsMaxFloat() ? height : height - c.LayoutParameters.Margins.TotalHeight(); } } } diff --git a/XibFree/NativeView.cs b/XibFree/NativeView.cs index 16b9392..4814b97 100644 --- a/XibFree/NativeView.cs +++ b/XibFree/NativeView.cs @@ -33,7 +33,10 @@ public class NativeView : View /// /// Initializes a new instance of the class. /// - public NativeView() {} + public NativeView() + { + LayoutParameters = new LayoutParameters(); + } /// /// Initializes a new instance of the class. @@ -43,10 +46,10 @@ public NativeView() {} public NativeView(UIView view, LayoutParameters lp) { _view = view; - this.LayoutParameters = lp; + LayoutParameters = lp; } - public override LayoutParameters LayoutParameters + public override sealed LayoutParameters LayoutParameters { get { @@ -68,21 +71,20 @@ public UIView View get { return _view; } set { - if (_view != value) - { - // Detach old view from host - ViewGroup.IHost host = GetHost(); - if (host != null) onDetach(); + if (_view == value) return; - // Store the new view - _view = value; + // Detach old view from host + var host = GetHost(); + if (host != null) OnDetach(); - // Turn off auto-resizing, we'll take care of that thanks - _view.AutoresizingMask = UIViewAutoresizing.None; + // Store the new view + _view = value; - // Attach the new view to the host - if (host != null) onAttach(host); - } + // Turn off auto-resizing, we'll take care of that thanks + _view.AutoresizingMask = UIViewAutoresizing.None; + + // Attach the new view to the host + if (host != null) OnAttach(host); } } @@ -95,19 +97,18 @@ public T As() where T:UIView return _view as T; } - /// /// Overridden to set the position of the native view /// /// New position. - protected override void onLayout(RectangleF newPosition, bool parentHidden) + /// If Parent is Hidden + protected override void OnLayout(RectangleF newPosition, bool parentHidden) { + if (_view == null) return; + // Simple, just reposition the view! - if (_view!=null) - { - _view.Hidden = parentHidden || !Visible; - _view.Frame = newPosition; - } + _view.Hidden = parentHidden || !Visible; + _view.Frame = newPosition; } /// @@ -115,17 +116,17 @@ protected override void onLayout(RectangleF newPosition, bool parentHidden) /// /// Parent width. /// Parent height. - protected override void onMeasure(float parentWidth, float parentHeight) + protected override void OnMeasure(float parentWidth, float parentHeight) { // Resolve width for absolute and parent ratio - float width = LayoutParameters.TryResolveWidth(this, parentWidth); - float height = LayoutParameters.TryResolveHeight(this, parentHeight); + var width = LayoutParameters.TryResolveWidth(this, parentWidth); + var height = LayoutParameters.TryResolveHeight(this, parentHeight); // Do we need to measure our content? - SizeF sizeMeasured = SizeF.Empty; - if (width == float.MaxValue || height == float.MaxValue) + var sizeMeasured = SizeF.Empty; + if (width.IsMaxFloat() || height.IsMaxFloat()) { - SizeF sizeToFit = new SizeF(width, height); + var sizeToFit = new SizeF(width, height); if (Measurer != null) sizeMeasured = Measurer(_view, sizeToFit); else { @@ -143,7 +144,7 @@ protected override void onMeasure(float parentWidth, float parentHeight) /// Overridden to add this native view to the parent native view /// /// Host. - internal override void onAttach(ViewGroup.IHost host) + internal override void OnAttach(IHost host) { // If we have a view, attach to the hosting view by adding as a subview if (_view != null) host.GetUIView().Add(_view); @@ -152,7 +153,7 @@ internal override void onAttach(ViewGroup.IHost host) /// /// Overridden to remove this native view from the parent native view /// - internal override void onDetach() + internal override void OnDetach() { // If we have a view, remove from the hosting view by removing it from the superview if (_view != null) _view.RemoveFromSuperview(); diff --git a/XibFree/Orientation.cs b/XibFree/Orientation.cs index 212016a..ed4d0c5 100644 --- a/XibFree/Orientation.cs +++ b/XibFree/Orientation.cs @@ -14,8 +14,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; - namespace XibFree { /// diff --git a/XibFree/UILayoutHost.cs b/XibFree/UILayoutHost.cs index d2b9f28..aaf4765 100644 --- a/XibFree/UILayoutHost.cs +++ b/XibFree/UILayoutHost.cs @@ -14,7 +14,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; using MonoTouch.UIKit; using System.Drawing; @@ -23,7 +22,7 @@ namespace XibFree /// /// UILayoutHost is the native UIView that hosts that XibFree layout /// - public class UILayoutHost : UIView, ViewGroup.IHost + public sealed class UILayoutHost : UIView, IHost { private ViewGroup _layout; @@ -31,35 +30,31 @@ public class UILayoutHost : UIView, ViewGroup.IHost /// Initializes a new instance of the class. /// /// Root of the view hierarchy to be hosted by this layout host + /// Frame for the UIView public UILayoutHost(ViewGroup layout, RectangleF frame) : base(frame) { - this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; + AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; Layout = layout; } public UILayoutHost() { - this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; + AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; } public UILayoutHost(ViewGroup layout) { - this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; + AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; Layout = layout; } - /// /// The ViewGroup declaring the layout to hosted /// /// The ViewGroup. public ViewGroup Layout { - get - { - return _layout; - } - + get { return _layout; } set { if (_layout != null) _layout.SetHost(null); @@ -82,28 +77,26 @@ public NativeView FindNativeView(UIView view) public override SizeF SizeThatFits(SizeF size) { - if (_layout==null) - return new SizeF(0,0); + if (_layout == null) return new SizeF(0, 0); // Measure the layout _layout.Measure(size.Width, size.Height); return _layout.GetMeasuredSize(); } - /// Lays out subviews. /// /// Called by iOS to update the layout of this view /// public override void LayoutSubviews() { - if (_layout!=null) - { - // Remeasure - _layout.Measure(Bounds.Width, Bounds.Height); - // Apply layout - _layout.Layout(Bounds, false); - } + if (_layout == null) return; + + // Remeasure + _layout.Measure(Bounds.Width, Bounds.Height); + + // Apply layout + _layout.Layout(Bounds, false); } #region IHost implementation @@ -111,7 +104,7 @@ public override void LayoutSubviews() /// /// Provide the hosting view /// - UIView ViewGroup.IHost.GetUIView() + public UIView GetUIView() { return this; } diff --git a/XibFree/UILayoutHostScrollable.cs b/XibFree/UILayoutHostScrollable.cs index c9d33c6..43724b1 100644 --- a/XibFree/UILayoutHostScrollable.cs +++ b/XibFree/UILayoutHostScrollable.cs @@ -14,7 +14,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; using MonoTouch.UIKit; using System.Drawing; @@ -23,29 +22,29 @@ namespace XibFree /// /// UILayoutHostScrollable is the native UIView that hosts that XibFree layout /// - public class UILayoutHostScrollable : UIScrollView + public sealed class UILayoutHostScrollable : UIScrollView { + private readonly UILayoutHost _layoutHost; + /// /// Initializes a new instance of the class. /// /// Root of the view hierarchy to be hosted by this layout host + /// Frame for the UIView public UILayoutHostScrollable(ViewGroup layout, RectangleF frame) : base(frame) { - _layoutHost = new UILayoutHost(layout); - _layoutHost.AutoresizingMask = UIViewAutoresizing.None; - this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; - this.AddSubview(_layoutHost); - } + _layoutHost = new UILayoutHost(layout) + { + AutoresizingMask = UIViewAutoresizing.None + }; - public UILayoutHostScrollable() : this(null, RectangleF.Empty) - { + AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; + AddSubview(_layoutHost); } - public UILayoutHostScrollable(ViewGroup layout) : this(layout, RectangleF.Empty) - { - } + public UILayoutHostScrollable() : this(null, RectangleF.Empty) { } - UILayoutHost _layoutHost; + public UILayoutHostScrollable(ViewGroup layout) : this(layout, RectangleF.Empty) { } /// /// The ViewGroup declaring the layout to hosted @@ -77,19 +76,18 @@ public override SizeF SizeThatFits(SizeF size) /// public override void LayoutSubviews() { - if (Layout!=null) - { - // Remeasure the layout - Layout.Measure(Bounds.Width, float.MaxValue); + if (Layout == null) return; - var size = Layout.GetMeasuredSize(); + // Remeasure the layout + Layout.Measure(Bounds.Width, float.MaxValue); - // Reposition the layout host - _layoutHost.Frame = new RectangleF(PointF.Empty, size); + var size = Layout.GetMeasuredSize(); - // Update the scroll view content - ContentSize = size; - } + // Reposition the layout host + _layoutHost.Frame = new RectangleF(PointF.Empty, size); + + // Update the scroll view content + ContentSize = size; } } } diff --git a/XibFree/View.cs b/XibFree/View.cs index 3b5ccf2..ed7a339 100644 --- a/XibFree/View.cs +++ b/XibFree/View.cs @@ -30,41 +30,27 @@ public abstract class View private bool _measuredSizeValid; private ViewGroup _parent; - /// - /// Initializes a new instance of the class. - /// - public View() - { - LayoutParameters = new LayoutParameters(); - } - /// /// Gets or sets this view's parent view /// /// A reference to the parent view (or null) public ViewGroup Parent { - get - { - return _parent; - } + get { return _parent; } internal set { - if (_parent!=value) - { - // Detach old host - ViewGroup.IHost host = GetHost(); - if (host!=null) - onDetach(); - - // Store new parent - _parent = value; - - // Attach to new host - host = GetHost(); - if (host!=null) - onAttach(host); - } + if (_parent == value) return; + + // Detach old host + var host = GetHost(); + if (host != null) OnDetach(); + + // Store new parent + _parent = value; + + // Attach to new host + host = GetHost(); + if (host != null) OnAttach(host); } } @@ -75,31 +61,33 @@ internal set public virtual LayoutParameters LayoutParameters { get; set; } // Internal helper to walk the parent view hierachy and find the view that's hosting this view hierarchy - internal virtual ViewGroup.IHost GetHost() + internal virtual IHost GetHost() { return (_parent != null) ? _parent.GetHost() : null; } // Internal notification that this view has been attached to a hosting view - internal virtual void onAttach(ViewGroup.IHost host) { } + internal virtual void OnAttach(IHost host) { } // Internal notification that this view has been detached from a hosting view - internal virtual void onDetach() { } + internal virtual void OnDetach() { } /// /// Layout the subviews in this view using dimensions calculated during the last measure cycle /// /// The new position of this view + /// Whether the parent is hidden public void Layout(RectangleF newPosition, bool parentHidden) { - onLayout(newPosition, parentHidden); + OnLayout(newPosition, parentHidden); } /// /// Overridden by view groups to perform the actual layout process /// /// New position. - protected abstract void onLayout(RectangleF newPosition, bool parentHidden); + /// Whether the parent is hidden + protected abstract void OnLayout(RectangleF newPosition, bool parentHidden); /// /// Measures the subviews of this view @@ -109,7 +97,7 @@ public void Layout(RectangleF newPosition, bool parentHidden) public void Measure(float parentWidth, float parentHeight) { _measuredSizeValid = false; - onMeasure(parentWidth, parentHeight); + OnMeasure(parentWidth, parentHeight); if (!_measuredSizeValid) throw new InvalidOperationException("onMeasure didn't set measurement before returning"); } @@ -118,7 +106,7 @@ public void Measure(float parentWidth, float parentHeight) /// /// Parent width. /// Parent height. - protected abstract void onMeasure(float parentWidth, float parentHeight); + protected abstract void OnMeasure(float parentWidth, float parentHeight); // Mark the measurement of this view as invalid public void InvalidateMeasure() @@ -133,11 +121,11 @@ public void InvalidateMeasure() /// Size. protected void SetMeasuredSize(SizeF size) { - if (LayoutParameters.MinWidth != 0 && size.Width < LayoutParameters.MinWidth) size.Width = LayoutParameters.MinWidth; - if (LayoutParameters.MinHeight != 0 && size.Height < LayoutParameters.MinHeight) size.Height = LayoutParameters.MinHeight; + if (!LayoutParameters.MinWidth.IsEqualTo(0) && size.Width < LayoutParameters.MinWidth) size.Width = LayoutParameters.MinWidth; + if (!LayoutParameters.MinHeight.IsEqualTo(0) && size.Height < LayoutParameters.MinHeight) size.Height = LayoutParameters.MinHeight; - if (LayoutParameters.MaxWidth != 0 && size.Width > LayoutParameters.MaxWidth) size.Width = LayoutParameters.MaxWidth; - if (LayoutParameters.MaxHeight != 0 && size.Height > LayoutParameters.MaxHeight) size.Height = LayoutParameters.MaxHeight; + if (!LayoutParameters.MaxWidth.IsEqualTo(0) && size.Width > LayoutParameters.MaxWidth) size.Width = LayoutParameters.MaxWidth; + if (!LayoutParameters.MaxHeight.IsEqualTo(0) && size.Height > LayoutParameters.MaxHeight) size.Height = LayoutParameters.MaxHeight; _measuredSize = size; _measuredSizeValid = true; @@ -210,7 +198,7 @@ public bool Visible public void RemoveFromSuperview() { - if (Parent!=null) Parent.RemoveSubView(this); + if (Parent != null) Parent.RemoveSubView(this); } } } diff --git a/XibFree/ViewGroup.cs b/XibFree/ViewGroup.cs index 7c1ce2f..3868090 100644 --- a/XibFree/ViewGroup.cs +++ b/XibFree/ViewGroup.cs @@ -29,7 +29,7 @@ namespace XibFree public abstract class ViewGroup : View { // Fields - private List _subViews = new List(); + private readonly List _subViews = new List(); private CALayer _layer; private IHost _host; @@ -41,15 +41,6 @@ public abstract class ViewGroup : View public int Tag { get; set; } - /// - /// Initializes a new instance of the class. - /// - public ViewGroup() - { - LayoutParameters.Width = Dimension.FillParent; - LayoutParameters.Height = Dimension.FillParent; - } - /// /// Gets or sets all the subviews of this view group /// @@ -99,7 +90,7 @@ public void AddSubView(UIView view, LayoutParameters lp) /// /// Insert a new subview at the end of the subview collection /// - /// The subview to add + /// The subview to add public void AddSubView(View view) { InsertSubView(-1, view); @@ -111,7 +102,7 @@ public void AddSubView(View view) /// The subview to remove. public void RemoveSubView(UIView view) { - for (int i=0; i<_subViews.Count; i++) + for (var i=0; i<_subViews.Count; i++) { var nv = _subViews[i] as NativeView; if (nv!=null && nv.View == view) @@ -154,67 +145,61 @@ public void RemoveSubViewAt(int index) _subViews.RemoveAt(index); } - public interface IHost - { - UIView GetUIView(); - } - - /// /// Sets the native host for this view hierachy /// /// A reference to the host. public void SetHost(IHost host) { - if (_host != null) onDetach(); + if (_host != null) OnDetach(); _host = host; - if (_host != null) onAttach(_host); + if (_host != null) OnAttach(_host); } /// /// Overridden to locate the parent host for this view hierarchy /// /// The host. - internal override ViewGroup.IHost GetHost() + internal override IHost GetHost() { // If this view group has been parented into an actual UIView, we'll have a IHost reference // that acts as the host for all views in the hierarchy. If not, ask our parent - return (_host != null) ? _host : base.GetHost(); + return _host ?? base.GetHost(); } /// /// We've been attached to a hosting view, notify all subviews /// /// The Host. - internal override void onAttach(IHost host) + internal override void OnAttach(IHost host) { // Add the layer if (_layer!=null) host.GetUIView().Layer.AddSublayer(_layer); // Forward on to all children - foreach (var c in _subViews) c.onAttach(host); + foreach (var c in _subViews) c.OnAttach(host); } /// /// We've been detached from a hosting view, notify all subviews /// - internal override void onDetach() + internal override void OnDetach() { // Remove from layer if (_layer != null) _layer.RemoveFromSuperLayer(); // Forward on to all children - foreach (var c in _subViews) c.onDetach(); + foreach (var c in _subViews) c.OnDetach(); } - protected override void onLayout(RectangleF newPosition, bool parentHidden) + protected override void OnLayout(RectangleF newPosition, bool parentHidden) { // Reposition the layer if (_layer != null) { - bool newHidden = parentHidden || !Visible; + var newHidden = parentHidden || !Visible; if (newHidden != _layer.Hidden) { // If we're changing the visibility, disable animations since @@ -233,44 +218,26 @@ protected override void onLayout(RectangleF newPosition, bool parentHidden) } // Hide all subviews - if (parentHidden || !Visible) - { - foreach (var v in SubViews) v.Layout(RectangleF.Empty, false); - return; - } + if (!parentHidden && Visible) return; + + foreach (var v in SubViews) v.Layout(RectangleF.Empty, false); } internal override View LayoutViewWithTag(int tag) { if (Tag == tag) return this; - foreach (var v in _subViews) - { - var result = v.LayoutViewWithTag(tag); - if (result != null) return result; - } - - return null; + return _subViews.Select(v => v.LayoutViewWithTag(tag)).FirstOrDefault(result => result != null); } internal override UIView UIViewWithTag(int tag) { - foreach (var v in _subViews) - { - var result = v.UIViewWithTag(tag); - if (result != null) return result; - } - return null; + return _subViews.Select(v => v.UIViewWithTag(tag)).FirstOrDefault(result => result != null); } public override NativeView FindNativeView(UIView view) { - foreach (var v in _subViews) - { - var result = v.FindNativeView(view); - if (result != null) return result; - } - return null; + return _subViews.Select(v => v.FindNativeView(view)).FirstOrDefault(result => result != null); } internal override CALayer FindFirstSublayer() @@ -287,13 +254,11 @@ internal override CALayer FindFirstSublayer() return null; } - internal override CALayer GetDisplayLayer() { return _layer; } - public CALayer Layer { get { return _layer; } @@ -306,19 +271,16 @@ public CALayer Layer _layer = value; // If we're attached, add the layer to our host - if (_layer!=null) - { - ViewGroup.IHost host = GetHost(); - if (host!=null) - { - UIView hostView = host.GetUIView(); - var nextSubLayer = FindFirstSublayer(); - - if (nextSubLayer!=null) hostView.Layer.InsertSublayerBelow(_layer, nextSubLayer); - else hostView.Layer.AddSublayer(_layer); - } - } + if (_layer == null) return; + + var host = GetHost(); + if (host == null) return; + + var hostView = host.GetUIView(); + var nextSubLayer = FindFirstSublayer(); + if (nextSubLayer != null) hostView.Layer.InsertSublayerBelow(_layer, nextSubLayer); + else hostView.Layer.AddSublayer(_layer); } } } diff --git a/XibFree/Visibility.cs b/XibFree/Visibility.cs index 89a1d5d..5e6d89b 100644 --- a/XibFree/Visibility.cs +++ b/XibFree/Visibility.cs @@ -13,7 +13,6 @@ // 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. -using System; namespace XibFree { diff --git a/XibFree/XibFree.csproj b/XibFree/XibFree.csproj index 501f97f..345ee38 100644 --- a/XibFree/XibFree.csproj +++ b/XibFree/XibFree.csproj @@ -1,4 +1,4 @@ - + Debug @@ -45,8 +45,9 @@ - + + @@ -56,7 +57,6 @@ -