Skip to content
Permalink
Browse files

Yoga: handling percentage for various properties

  • Loading branch information...
paraboul committed Apr 20, 2017
1 parent eb4de4f commit f5d5d049926778d25152771a639b816dd319910a
Showing with 66 additions and 26 deletions.
  1. +31 −17 src/Binding/JSCanvas.cpp
  2. +34 −8 src/Graphics/CanvasHandler.cpp
  3. +1 −1 src/Graphics/CanvasHandler.h
@@ -544,6 +544,12 @@ bool JSCanvas::JSSetter_width(JSContext *cx, JS::MutableHandleValue vp)
{
double dval;

if (vp.isNullOrUndefined()) {
m_CanvasHandler->setWidth(NAN);
return true;
}


m_CanvasHandler->p_Width.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

if (!m_CanvasHandler->setWidth((float)dval)) {
@@ -557,6 +563,11 @@ bool JSCanvas::JSSetter_height(JSContext *cx, JS::MutableHandleValue vp)
{
double dval;

if (vp.isNullOrUndefined()) {
m_CanvasHandler->setHeight(NAN);
return true;
}

m_CanvasHandler->p_Height.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

if (!m_CanvasHandler->setHeight((float)dval)) {
@@ -576,9 +587,7 @@ bool JSCanvas::JSSetter_left(JSContext *cx, JS::MutableHandleValue vp)
}


if (!JS::ToNumber(cx, vp, &dval)) {
return true;
}
m_CanvasHandler->p_Left.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropLeft((float)dval);

@@ -594,9 +603,7 @@ bool JSCanvas::JSSetter_right(JSContext *cx, JS::MutableHandleValue vp)
return true;
}

if (!JS::ToNumber(cx, vp, &dval)) {
return true;
}
m_CanvasHandler->p_Right.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropRight((float)dval);

@@ -612,9 +619,7 @@ bool JSCanvas::JSSetter_top(JSContext *cx, JS::MutableHandleValue vp)
return true;
}

if (!JS::ToNumber(cx, vp, &dval)) {
return true;
}
m_CanvasHandler->p_Top.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropTop((float)dval);

@@ -630,10 +635,7 @@ bool JSCanvas::JSSetter_bottom(JSContext *cx, JS::MutableHandleValue vp)
return true;
}


if (!JS::ToNumber(cx, vp, &dval)) {
return true;
}
m_CanvasHandler->p_Bottom.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropBottom((float)dval);

@@ -644,10 +646,13 @@ bool JSCanvas::JSSetter_minWidth(JSContext *cx, JS::MutableHandleValue vp)
{
double dval;

if (!JS::ToNumber(cx, vp, &dval)) {
if (vp.isNullOrUndefined()) {
m_CanvasHandler->setPropMinWidth(NAN);
return true;
}

m_CanvasHandler->p_MinWidth.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropMinWidth((float)dval);

return true;
@@ -657,10 +662,13 @@ bool JSCanvas::JSSetter_minHeight(JSContext *cx, JS::MutableHandleValue vp)
{
double dval;

if (!JS::ToNumber(cx, vp, &dval)) {
if (vp.isNullOrUndefined()) {
m_CanvasHandler->setPropMinHeight(NAN);
return true;
}

m_CanvasHandler->p_MinHeight.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropMinHeight((float)dval);

return true;
@@ -670,10 +678,13 @@ bool JSCanvas::JSSetter_maxWidth(JSContext *cx, JS::MutableHandleValue vp)
{
double dval;

if (!JS::ToNumber(cx, vp, &dval)) {
if (vp.isNullOrUndefined()) {
m_CanvasHandler->setPropMaxWidth(NAN);
return true;
}

m_CanvasHandler->p_MaxWidth.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropMaxWidth((float)dval);

return true;
@@ -683,10 +694,13 @@ bool JSCanvas::JSSetter_maxHeight(JSContext *cx, JS::MutableHandleValue vp)
{
double dval;

if (!JS::ToNumber(cx, vp, &dval)) {
if (vp.isNullOrUndefined()) {
m_CanvasHandler->setPropMaxHeight(NAN);
return true;
}

m_CanvasHandler->p_MaxHeight.setIsPercentageValue(JSUtils::ValuePercent(cx, vp, &dval));

m_CanvasHandler->setPropMaxHeight((float)dval);

return true;
@@ -133,36 +133,52 @@ void CanvasHandler::setId(const char *str)

void CanvasHandler::setPropMinWidth(float width)
{
if (width < 1) width = 1;
p_MinWidth = width;

p_MinWidth = p_MaxWidth ? nidium_min(width, p_MaxWidth) : width;
if (p_MinWidth.isPercentageValue()) {
YGNodeStyleSetMinWidthPercent(m_YogaRef, p_MinWidth);

return;
}

YGNodeStyleSetMinWidth(m_YogaRef, p_MinWidth);
}

void CanvasHandler::setPropMinHeight(float height)
{
if (height < 1) height = 1;
p_MinHeight = height;

p_MinHeight = p_MaxHeight ? nidium_min(height, p_MaxHeight) : height;
if (p_MinHeight.isPercentageValue()) {
YGNodeStyleSetMinHeightPercent(m_YogaRef, p_MinHeight);

return;
}

YGNodeStyleSetMinHeight(m_YogaRef, p_MinHeight);
}

void CanvasHandler::setPropMaxWidth(float width)
{
if (width < 1) width = 1;
p_MaxWidth = width;

if (p_MaxWidth.isPercentageValue()) {
YGNodeStyleSetMaxWidthPercent(m_YogaRef, p_MaxWidth);

p_MaxWidth = nidium_max(p_MinWidth, width);
return;
}

YGNodeStyleSetMaxWidth(m_YogaRef, p_MaxWidth);
}

void CanvasHandler::setPropMaxHeight(float height)
{
if (height < 1) height = 1;
p_MaxHeight = height;

p_MaxHeight = nidium_max(p_MinHeight, height);
if (p_MaxHeight.isPercentageValue()) {
YGNodeStyleSetMaxHeightPercent(m_YogaRef, p_MaxHeight);

return;
}

YGNodeStyleSetMaxHeight(m_YogaRef, p_MaxHeight);
}
@@ -175,6 +191,11 @@ bool CanvasHandler::setWidth(float width, bool force)

p_Width = width;

if (isnan(width)) {
YGNodeStyleSetWidthAuto(m_YogaRef);
return true;
}

if (p_Width.isPercentageValue()) {
YGNodeStyleSetWidthPercent(m_YogaRef, width >= 0 && !isnan(width) ? width : YGUndefined);
} else {
@@ -192,6 +213,11 @@ bool CanvasHandler::setHeight(float height, bool force)

p_Height = height;

if (isnan(height)) {
YGNodeStyleSetHeightAuto(m_YogaRef);
return true;
}

if (p_Height.isPercentageValue()) {
YGNodeStyleSetHeightPercent(m_YogaRef, height >= 0 && !isnan(height) ? height : YGUndefined);
} else {
@@ -435,7 +435,7 @@ class CanvasHandler : public CanvasHandlerBase, public Core::Events
YGNodeStyleSetMargin(m_YogaRef, YGEdgeRight, right);
YGNodeStyleSetMargin(m_YogaRef, YGEdgeBottom, bottom);
YGNodeStyleSetMargin(m_YogaRef, YGEdgeLeft, left);

m_Margin.top = top;
m_Margin.right = right;
m_Margin.bottom = bottom;

0 comments on commit f5d5d04

Please sign in to comment.
You can’t perform that action at this time.