Skip to content

Commit

Permalink
Merge pull request #84 from pec1985/timob-14129-3_1_X
Browse files Browse the repository at this point in the history
Backport to 3.1.x [TIMOB-14129]
  • Loading branch information
Russ McMahon committed Jun 6, 2013
2 parents d80fa81 + 21dbdb5 commit 54bd56b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 60 deletions.
32 changes: 29 additions & 3 deletions src/tibb/Layout/Composite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ struct ComputedSize doCompositeLayout(std::vector<struct Element*> children, dou
return computedSize;
}

void setDefaultCompositeWidthType(struct LayoutProperties layoutProperties, enum ValueType* measuredWidthType) {
if (*measuredWidthType == None) {
if ((layoutProperties.left.valueType == Fixed || layoutProperties.left.valueType == Percent) &&
(layoutProperties.right.valueType == Fixed || layoutProperties.right.valueType == Percent)) {
return;
}

*measuredWidthType = layoutProperties.defaultWidthType;
}
}

void setDefaultCompositeHeightType(struct LayoutProperties layoutProperties, enum ValueType* measuredHeightType) {
if (*measuredHeightType == None) {
if ((layoutProperties.top.valueType == Fixed || layoutProperties.top.valueType == Percent) &&
(layoutProperties.bottom.valueType == Fixed || layoutProperties.bottom.valueType == Percent)) {
return;
}

*measuredHeightType = layoutProperties.defaultHeightType;
}
}

void measureNodeForCompositeLayout(struct LayoutProperties layoutProperties, struct Element* element) {
enum ValueType widthType = layoutProperties.width.valueType;
double widthValue = layoutProperties.width.value;
Expand All @@ -145,14 +167,18 @@ void measureNodeForCompositeLayout(struct LayoutProperties layoutProperties, str
enum ValueType minHeightType = layoutProperties.minHeight.valueType;
double minHeightValue = layoutProperties.minHeight.value;

setDefaultCompositeWidthType(layoutProperties, &widthType);
setDefaultCompositeHeightType(layoutProperties, &heightType);

double x1 = 0;
double x2 = 0;
double x3 = 0;

if (widthType == Size) {
x1 = x2 = NAN;
} else if (widthType == Fill) {
x1 = 1;
if (widthType == Percent) {
if (leftType == Percent) {
x1 -= leftValue;
} else if (widthType == Fixed) {
x2 = -leftValue;
Expand All @@ -165,7 +191,7 @@ void measureNodeForCompositeLayout(struct LayoutProperties layoutProperties, str
x1 = widthValue;
} else if (widthType == Fixed) {
x2 = widthValue;
} else if (widthType == Percent) {
} else if (leftType == Percent) {
if (centerXType == Percent) {
x1 = 2 * (centerXValue - leftValue);
} else if (centerXType == Fixed) {
Expand All @@ -177,7 +203,7 @@ void measureNodeForCompositeLayout(struct LayoutProperties layoutProperties, str
x1 = 1 - leftValue;
x2 = -rightValue;
}
} else if (widthType == Fixed) {
} else if (leftType == Fixed) {
if (centerXType == Percent) {
x1 = 2 * centerXValue;
x2 = -2 * leftValue;
Expand Down
15 changes: 15 additions & 0 deletions src/tibb/Layout/Horizontal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ struct ComputedSize doHorizontalLayout(std::vector<struct Element*> children, do
return computedSize;
}

void setDefaultHorizontalWidthType(struct LayoutProperties layoutProperties, enum ValueType* measuredWidthType) {
if (*measuredWidthType == None) {
*measuredWidthType = layoutProperties.defaultWidthType;
}
}

void setDefaultHorizontalHeightType(struct LayoutProperties layoutProperties, enum ValueType* measuredHeightType) {
if (*measuredHeightType == None) {
*measuredHeightType = layoutProperties.defaultHeightType;
}
}

void measureNodeForHorizontalLayout(struct LayoutProperties layoutProperties, struct Element* element) {
enum ValueType widthType = layoutProperties.width.valueType;
double widthValue = layoutProperties.width.value;
Expand All @@ -192,6 +204,9 @@ void measureNodeForHorizontalLayout(struct LayoutProperties layoutProperties, st
enum ValueType minHeightType = layoutProperties.minHeight.valueType;
double minHeightValue = layoutProperties.minHeight.value;

setDefaultHorizontalWidthType(layoutProperties, &widthType);
setDefaultHorizontalHeightType(layoutProperties, &heightType);

double x1 = 0;
double x2 = 0;
double x3 = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/tibb/Layout/ParseProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct LayoutProperties {
struct layoutProp minHeight;
struct layoutProp centerX;
struct layoutProp centerY;
enum ValueType defaultWidthType;
enum ValueType defaultHeightType;
};

void layoutPropertiesInitialize(struct LayoutProperties*);
Expand Down
20 changes: 20 additions & 0 deletions src/tibb/Layout/Vertical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ struct ComputedSize doVerticalLayout(std::vector<struct Element*> children, doub
return computedSize;
}

void setDefaultVerticalWidthType(struct LayoutProperties layoutProperties, enum ValueType* measuredWidthType) {
if (*measuredWidthType == None) {
if ((layoutProperties.left.valueType == Fixed || layoutProperties.left.valueType == Percent) &&
(layoutProperties.right.valueType == Fixed || layoutProperties.right.valueType == Percent)) {
return;
}

*measuredWidthType = layoutProperties.defaultWidthType;
}
}

void setDefaultVerticalHeightType(struct LayoutProperties layoutProperties, enum ValueType* measuredHeightType) {
if (*measuredHeightType == None) {
*measuredHeightType = layoutProperties.defaultHeightType;
}
}

void measureNodeForVerticalLayout(struct LayoutProperties layoutProperties, struct Element* element) {
enum ValueType widthType = layoutProperties.width.valueType;
double widthValue = layoutProperties.width.value;
Expand All @@ -125,6 +142,9 @@ void measureNodeForVerticalLayout(struct LayoutProperties layoutProperties, stru
enum ValueType minHeightType = layoutProperties.minHeight.valueType;
double minHeightValue = layoutProperties.minHeight.value;

setDefaultVerticalWidthType(layoutProperties, &widthType);
setDefaultVerticalHeightType(layoutProperties, &heightType);

double x1 = 0;
double x2 = 0;
double x3 = 0;
Expand Down
82 changes: 53 additions & 29 deletions src/tibb/NativeControlObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,20 @@ public slots:

#include "NativeControlObject.moc"

static void onPostLayout(struct Node* node) {
static void onPostLayout(struct Node* node)
{
NativeControlObject* native = static_cast<NativeControlObject*>(node->data);
Control* control = static_cast<Control*>(native->getNativeHandle());
if (!control) {
// Cannot update layout until control has been created for the view.
return;
}

if (node->properties.width.valueType == Defer || node->properties.height.valueType == Defer) {
return;
}

float width = node->element._measuredWidth,
height = node->element._measuredHeight;

// Do not allow a control with Ti.UI.SIZE to go to 0 or the OS will not callback deferred sizes as the controls are hidden.
if ((node->properties.width.valueType == Size && width == 0) || (node->properties.height.valueType == Size && height == 0)) {
// do not allow a control to go to 0 or the OS will not render control and will stop call back os deferred sizes
if (width == 0 || height == 0) {
return;
}

Expand All @@ -184,23 +181,34 @@ NativeControlObject::NativeControlObject(TiObject* tiObject, NATIVE_TYPE objType
layoutNode_.onLayout = onPostLayout;
layoutNode_.data = this;


if (objType == N_TYPE_VIEW || objType == N_TYPE_WEBVIEW || objType == N_TYPE_LIST_VIEW || objType == N_TYPE_SCROLL_VIEW || objType == N_TYPE_SCROLLABLE_VIEW) {
layoutNode_.properties.width.valueType = Fill;
layoutNode_.properties.height.valueType = Fill;
}
if (objType == N_TYPE_VIEW || objType == N_TYPE_WEBVIEW || objType == N_TYPE_LIST_VIEW || objType == N_TYPE_SCROLL_VIEW || objType == N_TYPE_SCROLLABLE_VIEW ||
objType == N_TYPE_WINDOW || objType == N_TYPE_MAPVIEW || objType == N_TYPE_TEXT_AREA) {
layoutNode_.properties.defaultWidthType = Fill;
layoutNode_.properties.defaultHeightType = Fill;
}
else if (objType == N_TYPE_LABEL || objType == N_TYPE_BUTTON || objType == N_TYPE_TOGGLEBUTTON ||
objType == N_TYPE_SLIDER || objType == N_TYPE_PROGRESSBAR || objType == N_TYPE_TEXT_FIELD ||
objType == N_TYPE_ACTIVITYINDICATOR || objType == N_TYPE_WINDOW || objType == N_TYPE_MAPVIEW ||
objType == N_TYPE_TEXT_AREA) {
layoutNode_.properties.width.valueType = Defer;
layoutNode_.properties.height.valueType = Defer;
objType == N_TYPE_SLIDER || objType == N_TYPE_PROGRESSBAR || objType == N_TYPE_TEXT_FIELD ||
objType == N_TYPE_ACTIVITYINDICATOR) {
layoutNode_.properties.defaultWidthType = Size;
layoutNode_.properties.defaultHeightType = Size;

// in Cascades controls like labels and buttons there is no way to know the size of the control, and
// even if the container is larger then the control the actually control surface is based on the size
// calculated during the Cascades rendering phase, to know the size the os calls back the size during the
// updateLayout() method, the Defer type allows the Titanium layout engine to hold off setting the
// control size and any parents sized to content until rendering is complete
layoutNode_.properties.width.valueType = Defer;
layoutNode_.properties.height.valueType = Defer;
}

objType_ = objType;

TiUtils *tiUtils = TiUtils::getInstance();
ppi_ = tiUtils->getPPI();

bb::device::DisplayInfo display;
displayWidth_ = display.pixelSize().width();
displayHeight_ = display.pixelSize().height();
}

NativeControlObject::~NativeControlObject()
Expand All @@ -227,21 +235,31 @@ void NativeControlObject::updateLayout(QRectF rect)
bool requestLayout = false;
rect_ = rect;

if (rect.width() != 0 && layoutNode_.properties.width.valueType == Defer) {
layoutNode_.properties.width.value = rect.width();
layoutNode_.properties.width.valueType = Fixed;
requestLayout = true;
if ((layoutNode_.properties.width.valueType == Defer || layoutNode_.properties.width.valueType == Size) && rect.width() != 0) {
// do not set width if it will be calculated from left and right properties
if (!((layoutNode_.properties.left.valueType == Fixed || layoutNode_.properties.left.valueType == Percent) &&
(layoutNode_.properties.right.valueType == Fixed || layoutNode_.properties.right.valueType == Percent))) {
layoutNode_.properties.width.value = rect.width();
layoutNode_.properties.width.valueType = Fixed;
requestLayout = true;
}
}

if (rect.height() != 0 && layoutNode_.properties.height.valueType == Defer) {
layoutNode_.properties.height.value = rect.height();
layoutNode_.properties.height.valueType = Fixed;
requestLayout = true;
if ((layoutNode_.properties.height.valueType == Defer || layoutNode_.properties.height.valueType == Size) && rect.height() != 0) {
// do not set height if it will be calculated from top and bottom properties
if (!((layoutNode_.properties.top.valueType == Fixed || layoutNode_.properties.top.valueType == Percent) &&
(layoutNode_.properties.bottom.valueType == Fixed || layoutNode_.properties.bottom.valueType == Percent))) {
layoutNode_.properties.height.value = rect.height();
layoutNode_.properties.height.valueType = Fixed;
requestLayout = true;
}
}

if (requestLayout) {
struct Node* root = nodeRequestLayout(&layoutNode_);
if (root) {
root->element._measuredWidth = displayWidth_;
root->element._measuredHeight = displayHeight_;
nodeLayout(root);
}
}
Expand Down Expand Up @@ -325,6 +343,8 @@ int NativeControlObject::addChildImpl(NativeObject* obj)
nodeAddChild(&layoutNode_, &((NativeControlObject*) obj)->layoutNode_);
struct Node* root = nodeRequestLayout(&layoutNode_);
if (root) {
root->element._measuredWidth = displayWidth_;
root->element._measuredHeight = displayHeight_;
nodeLayout(root);
}
TiObject* tmpObj = new TiObject;
Expand Down Expand Up @@ -356,6 +376,8 @@ int NativeControlObject::removeChildImpl(NativeObject* obj)
nodeRemoveChild(&layoutNode_, &((NativeControlObject*) obj)->layoutNode_);
struct Node* root = nodeRequestLayout(&layoutNode_);
if (root) {
root->element._measuredWidth = displayWidth_;
root->element._measuredHeight = displayHeight_;
nodeLayout(root);
}
bb::cascades::Control* control = (bb::cascades::Control*) obj->getNativeHandle();
Expand Down Expand Up @@ -458,6 +480,8 @@ void NativeControlObject::updateLayoutProperty(ValueName name, TiObject* val) {

struct Node* root = nodeRequestLayout(&layoutNode_);
if (root) {
root->element._measuredWidth = displayWidth_;
root->element._measuredHeight = displayHeight_;
nodeLayout(root);
}
}
Expand Down Expand Up @@ -592,10 +616,10 @@ int NativeControlObject::setFont(TiObject*)
PROP_SETGET(setHeight)
int NativeControlObject::setHeight(TiObject* obj)
{
// auto and Ti.UI.SIZE uses defaults that have already been set
// auto uses defaults that have already been set for the control type
string str = *String::Utf8Value(obj->getValue());

if ((str == "auto" || str == "UI.SIZE") && layoutNode_.properties.height.valueType == Defer) {
if (str == "auto") {
return NATIVE_ERROR_OK;
}

Expand Down Expand Up @@ -872,10 +896,10 @@ int NativeControlObject::getSize(TiObject* obj)
PROP_SETGET(setWidth)
int NativeControlObject::setWidth(TiObject* obj)
{
// auto and Ti.UI.SIZE uses defaults that have already been set
// auto uses defaults that have already been set for the control type
string str = *String::Utf8Value(obj->getValue());

if ((str == "auto" || str == "UI.SIZE") && layoutNode_.properties.width.valueType == Defer) {
if (str == "auto") {
return NATIVE_ERROR_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tibb/NativeControlObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ class NativeControlObject : public NativeProxyObject
bool batchUpdating_;
NATIVE_TYPE objType_;
float ppi_; // pixels per inch
int displayWidth_;
int displayHeight_;
};

// Event handler for Ti.UI.View
Expand Down
79 changes: 51 additions & 28 deletions test/apps/native/tibbtest/Resources/app.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
var win = Ti.UI.createWindow({layout:'vertical'});

var label = Ti.UI.createLabel({
text: 'Type some stuff...',
width: '75%', height: 50
});
win.add(label);

var textField = Ti.UI.createTextField({
width: '75%', height: 75
});
win.add(textField);

var button = Ti.UI.createButton({
title: 'Click, me!',
width: '75%', height: 100
});
win.add(button);

button.addEventListener('click',function(e){
Ti.API.info('Sending feature event');
Titanium.Analytics.featureEvent('my.feature.blah');
});

var view = Ti.UI.createView({
backgroundColor: 'red'
});
win.add(view);

/*
var win = Ti.UI.createWindow({});
//Horizontal Layout behavior. Green child centered vertically (No positioning pins)
var parent = Ti.UI.createView({backgroundColor:'red',layout:'horizontal',width:100, height:100});
var child1 =Ti.UI.createView({backgroundColor:'green',height:20,width:50});
var child2 =Ti.UI.createView({backgroundColor:'blue',height:40,width:50});
parent.add(child1);
parent.add(child2);
win.add(parent);
win.open();
*/


var win = Ti.UI.createWindow({});

var view = Ti.UI.createView({
height:300,
width:320,
layout:'horizontal'

});
win.add(view);

var l1 = Ti.UI.createLabel({
text:'I am the first label',
left:5,
width:'auto',
height:20
});

view.add(l1);

var l2 = Ti.UI.createLabel({
text:'I am the second label',
left:2,
width:'auto',
height:20
});

view.add(l2);

var l3 = Ti.UI.createLabel({
text:'I am the third label',
left:2,
width:'auto',
height:20
});

view.add(l3);

win.open();
win.open();
Binary file modified test/apps/native/tibbtest/arm/o.le-v7/tibbtest
Binary file not shown.

0 comments on commit 54bd56b

Please sign in to comment.