Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[iOS] Add methods to retrieve css style values from weexcore c++ object.
Browse files Browse the repository at this point in the history
  • Loading branch information
wqyfavor committed Nov 21, 2018
1 parent 8cf3513 commit 942f0c5
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 4 deletions.
72 changes: 71 additions & 1 deletion ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,33 @@
#ifdef __cplusplus
#include "layout.h"

typedef WeexCore::WXCoreFlexDirection WXCoreFlexDirection;
typedef WeexCore::WXCoreFlexWrap WXCoreFlexWrap;
typedef WeexCore::WXCoreJustifyContent WXCoreJustifyContent;
typedef WeexCore::WXCoreAlignItems WXCoreAlignItems;
typedef WeexCore::WXCoreAlignSelf WXCoreAlignSelf;
typedef WeexCore::WXCorePositionType WXCorePositionType;

extern "C" {
#endif
bool flexIsUndefined(float value);
#ifdef __cplusplus
}
#endif

#ifndef __cplusplus
// Ensure that .m files can use css style enum definitions.
#include "flex_enum.h"

typedef enum WXCoreFlexDirection WXCoreFlexDirection;
typedef enum WXCoreFlexWrap WXCoreFlexWrap;
typedef enum WXCoreJustifyContent WXCoreJustifyContent;
typedef enum WXCoreAlignItems WXCoreAlignItems;
typedef enum WXCoreAlignSelf WXCoreAlignSelf;
typedef enum WXCorePositionType WXCorePositionType;

#endif

@interface WXComponent ()
{
@package
Expand All @@ -51,9 +71,59 @@ extern "C" {
* @warning Subclasses must not override this.
*/
#ifdef __cplusplus
@property(nonatomic, readonly, assign) WeexCore::WXCoreLayoutNode *flexCssNode;
@property (nonatomic, readonly, assign) WeexCore::WXCoreLayoutNode *flexCssNode;
#endif

/**
* @abstract Get css style value for key. The key should be of CSS standard form.
* This method is for convenience use in C/ObjC environment. And if you want to
* retrieve all style values or in C++, you could use flexCssNode directly.
*
* Thread usage:
* This method should be invoked in component thread by WXPerformBlockOnComponentThread.
* Note that all initWithRef methods of WXComponent and its subclasses are performed in
* component thread by default. Therefore you can call this method directly in initWithRef.
*
* Supported keys:
* width, height, min-width, min-height, max-width, max-height,
* margin-(left/right/top/bottom)
* padding-(left/right/top/bottom)
* border-(left/right/top/bottom)-width
* left, right, top, bottom
* flex-grow
*/
- (float)getCssStyleValueForKey:(NSString *)key;

/**
* @abstract Get css style flex-direction. Thread usage the same as getCssStyleValueForKey.
*/
- (WXCoreFlexDirection)getCssStyleFlexDirection;

/**
* @abstract Get css style flex-wrap. Thread usage the same as getCssStyleValueForKey.
*/
- (WXCoreFlexWrap)getCssStyleFlexWrap;

/**
* @abstract Get css style justify-content. Thread usage the same as getCssStyleValueForKey.
*/
- (WXCoreJustifyContent)getCssStyleJustifyContent;

/**
* @abstract Get css style align-items. Thread usage the same as getCssStyleValueForKey.
*/
- (WXCoreAlignItems)getCssStyleAlignItems;

/**
* @abstract Get css style align-self. Thread usage the same as getCssStyleValueForKey.
*/
- (WXCoreAlignSelf)getCssStyleAlignSelf;

/**
* @abstract Get css style position. Thread usage the same as getCssStyleValueForKey.
*/
- (WXCorePositionType)getCssStylePositionType;

/**
* @abstract Convert layout dimension value like 'left', 'width' to style value in js considering viewport and scale.
*/
Expand Down
80 changes: 80 additions & 0 deletions ios/sdk/WeexSDK/Sources/Layout/WXComponent+Layout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,86 @@ - (CGFloat)judgePropValuePropValue:(id)propValue defaultValue:(CGFloat)defaultVa
return defaultValue;
}

- (float)getCssStyleValueForKey:(NSString *)key
{
/*
* width, height, min-width, min-height, max-width, max-height,
* margin-(left/right/top/bottom)
* padding-(left/right/top/bottom)
* border-(left/right/top/bottom)-width
* left, right, top, bottom
* flex-grow
*/
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
if (_flexCssNode == nullptr) {
return NAN;
}

std::string ckey = [key UTF8String];
if (ckey == "width") return _flexCssNode->getStyleWidth();
if (ckey == "height") return _flexCssNode->getStyleHeight();
if (ckey == "min-width") return _flexCssNode->getMinWidth();
if (ckey == "min-height") return _flexCssNode->getMinHeight();
if (ckey == "max-width") return _flexCssNode->getMaxWidth();
if (ckey == "max-height") return _flexCssNode->getMaxHeight();
if (ckey == "margin-left") return _flexCssNode->getMarginLeft();
if (ckey == "margin-right") return _flexCssNode->getMarginRight();
if (ckey == "margin-top") return _flexCssNode->getMarginTop();
if (ckey == "margin-bottom") return _flexCssNode->getMarginBottom();
if (ckey == "padding-left") return _flexCssNode->getPaddingLeft();
if (ckey == "padding-right") return _flexCssNode->getPaddingRight();
if (ckey == "padding-top") return _flexCssNode->getPaddingTop();
if (ckey == "padding-bottom") return _flexCssNode->getPaddingBottom();
if (ckey == "border-left-width") return _flexCssNode->getBorderWidthLeft();
if (ckey == "border-right-width") return _flexCssNode->getBorderWidthRight();
if (ckey == "border-top-width") return _flexCssNode->getBorderWidthTop();
if (ckey == "border-bottom-width") return _flexCssNode->getBorderWidthBottom();
if (ckey == "left") return _flexCssNode->getStylePositionLeft();
if (ckey == "right") return _flexCssNode->getStylePositionRight();
if (ckey == "top") return _flexCssNode->getStylePositionTop();
if (ckey == "bottom") return _flexCssNode->getStylePositionBottom();
if (ckey == "flex-grow") return _flexCssNode->getFlex();

WXAssert(NO, @"Invalid css style key %@", key);
return NAN;
}

- (WXCoreFlexDirection)getCssStyleFlexDirection
{
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
return _flexCssNode ? _flexCssNode->getFlexDirection() : kFlexDirectionColumn;
}

- (WXCoreFlexWrap)getCssStyleFlexWrap
{
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
return _flexCssNode ? _flexCssNode->getFlexWrap() : kNoWrap;
}

- (WXCoreJustifyContent)getCssStyleJustifyContent
{
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
return _flexCssNode ? _flexCssNode->getJustifyContent() : kJustifyFlexStart;
}

- (WXCoreAlignItems)getCssStyleAlignItems
{
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
return _flexCssNode ? _flexCssNode->getAlignItems() : kAlignItemsStretch;
}

- (WXCoreAlignSelf)getCssStyleAlignSelf
{
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
return _flexCssNode ? _flexCssNode->getAlignSelf() : kAlignSelfAuto;
}

- (WXCorePositionType)getCssStylePositionType
{
WXAssert(_flexCssNode != nullptr, @"Css node is null.");
return _flexCssNode ? _flexCssNode->getStylePositionType() : kRelative;
}

- (NSString*)convertLayoutValueToStyleValue:(NSString*)valueName
{
if (_flexCssNode == nullptr) {
Expand Down
8 changes: 6 additions & 2 deletions weex_core/Source/core/layout/flex_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
#ifdef __cplusplus

#ifndef WEEXCORE_FLEXLAYOUT_WXCOREFLEXENUM_H
#define WEEXCORE_FLEXLAYOUT_WXCOREFLEXENUM_H

/* These enum definitions may also be used by C files. */
#ifdef __cplusplus
namespace WeexCore {
#endif

/**
* MainAxis direction
Expand Down Expand Up @@ -120,6 +122,8 @@ namespace WeexCore {
kLeft,
};

#ifdef __cplusplus
}
#endif

#endif //WEEXCORE_FLEXLAYOUT_WXCOREFLEXENUM_H
#endif
2 changes: 1 addition & 1 deletion weex_core/Source/core/layout/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ namespace WeexCore {
}
}

inline WXCorePositionType getStypePositionType() const {
inline WXCorePositionType getStylePositionType() const {
return mCssStyle->mPositionType;
}

Expand Down

0 comments on commit 942f0c5

Please sign in to comment.