style.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #ifdef __cplusplus
  20. #ifndef WEEXCORE_FLEXLAYOUT_WXCOREFLEXENUMS_H
  21. #define WEEXCORE_FLEXLAYOUT_WXCOREFLEXENUMS_H
  22. #include "flex_enum.h"
  23. #include <math.h>
  24. #include <cmath>
  25. namespace WeexCore {
  26. /**
  27. * Margin: margin-left、margin-right、margin-top、margin-bottom
  28. */
  29. class WXCoreMargin {
  30. private:
  31. float mMarginTop;
  32. float mMarginBottom;
  33. float mMarginLeft;
  34. float mMarginRight;
  35. public:
  36. WXCoreMargin() : mMarginTop(0),
  37. mMarginBottom(0),
  38. mMarginLeft(0),
  39. mMarginRight(0) {}
  40. ~WXCoreMargin() {
  41. mMarginTop = 0;
  42. mMarginBottom = 0;
  43. mMarginLeft = 0;
  44. mMarginRight = 0;
  45. }
  46. bool setMargin(const WXCoreMarginEdge &edge, float margin);
  47. float getMargin(const WXCoreMarginEdge &edge)const;
  48. };
  49. /**
  50. * Padding:padding-left、padding-right、padding-top、padding-bottom
  51. */
  52. class WXCorePadding {
  53. private:
  54. float mPaddingTop;
  55. float mPaddingBottom;
  56. float mPaddingLeft;
  57. float mPaddingRight;
  58. public:
  59. WXCorePadding() : mPaddingTop(0),
  60. mPaddingBottom(0),
  61. mPaddingLeft(0),
  62. mPaddingRight(0) {}
  63. ~WXCorePadding() {
  64. mPaddingTop = 0;
  65. mPaddingBottom = 0;
  66. mPaddingLeft = 0;
  67. mPaddingRight = 0;
  68. }
  69. bool setPadding(const WXCorePaddingEdge &edge, float padding);
  70. float getPadding(const WXCorePaddingEdge &edge)const;
  71. };
  72. /**
  73. * BorderWidth:borderwidth-left、borderwidth-right、borderwidth-top、borderwidth-bottom
  74. */
  75. class WXCoreBorderWidth {
  76. private:
  77. float mBorderWidthTop;
  78. float mBorderWidthBottom;
  79. float mBorderWidthLeft;
  80. float mBorderWidthRight;
  81. public:
  82. WXCoreBorderWidth() : mBorderWidthTop(0),
  83. mBorderWidthBottom(0),
  84. mBorderWidthLeft(0),
  85. mBorderWidthRight(0) {}
  86. ~WXCoreBorderWidth() {
  87. mBorderWidthTop = 0;
  88. mBorderWidthBottom = 0;
  89. mBorderWidthLeft = 0;
  90. mBorderWidthRight = 0;
  91. }
  92. bool setBorderWidth(const WXCoreBorderWidthEdge &edge, float borderWidth);
  93. float getBorderWidth(const WXCoreBorderWidthEdge &edge)const;
  94. };
  95. /**
  96. * position:left、right、top、bottom
  97. */
  98. class WXCorePosition {
  99. private:
  100. float mTop;
  101. float mBottom;
  102. float mLeft;
  103. float mRight;
  104. public:
  105. WXCorePosition() : mTop(NAN),
  106. mBottom(NAN),
  107. mLeft(NAN),
  108. mRight(NAN) {}
  109. ~WXCorePosition() {
  110. reset();
  111. }
  112. inline bool isNAN() {
  113. return isnan(mTop) || isnan(mBottom) || isnan(mLeft) || isnan(mRight);
  114. }
  115. inline void reset() {
  116. mTop = 0;
  117. mBottom = 0;
  118. mLeft = 0;
  119. mRight = 0;
  120. }
  121. bool setPosition(const WXCorePositionEdge &edge, float position);
  122. float getPosition(const WXCorePositionEdge &edge);
  123. };
  124. enum DimensionLevel{
  125. CSS_STYLE = 1,
  126. INSTANCE_STYLE = 2,
  127. FALLBACK_STYLE = 3
  128. };
  129. /**
  130. * css-style
  131. */
  132. class WXCoreCSSStyle {
  133. public:
  134. /**
  135. * The direction children items are placed inside the Flexbox layout, it determines the
  136. * direction of the main axis (and the cross axis, perpendicular to the main axis).
  137. * The default value is {@link WXCoreFlexDirection #WXCore_Flex_Direction_Row}.
  138. */
  139. WXCoreFlexDirection mFlexDirection;
  140. /**
  141. * This attribute controls whether the flex container is single-line or multi-line, and the
  142. * direction of the cross axis.
  143. * <ul>
  144. * <li>{@link WXCoreFlexWrap}: The flex container is single-line.</li>
  145. * <li>{@link WXCoreFlexWrap}: The flex container is multi-line.</li>
  146. * <li>{@link WXCoreFlexWrap}: The flex container is multi-line. The direction of the
  147. * cross axis is opposed to the direction as the {@link WXCoreFlexWrap}</li>
  148. * </ul>
  149. * The default value is {@link WXCoreFlexWrap #WXCore_Wrap_NoWrap}.
  150. */
  151. WXCoreFlexWrap mFlexWrap;
  152. /**
  153. * This attribute controls the alignment along the main axis.
  154. * The default value is {@link WXCoreJustifyContent #WXCore_Justify_Flex_Start}.
  155. */
  156. WXCoreJustifyContent mJustifyContent;
  157. /**
  158. * This attribute controls the alignment along the cross axis.
  159. * The default value is {@link WXCoreAlignItems #WXCore_AlignItems_Stretch}.
  160. */
  161. WXCoreAlignItems mAlignItems;
  162. /**
  163. * This attribute controls the alignment along the cross axis.
  164. * The default value is {@link WXCoreAlignSelf #WXCore_AlignSelf_Auto}.
  165. */
  166. WXCoreAlignSelf mAlignSelf;
  167. WXCorePositionType mPositionType;
  168. WXCoreDirection mDirection;
  169. float mFlexGrow;
  170. float mMinWidth;
  171. float mMinHeight;
  172. float mMaxWidth;
  173. float mMaxHeight;
  174. float mStyleWidth;
  175. float mStyleHeight;
  176. DimensionLevel mStyleWidthLevel;
  177. DimensionLevel mStyleHeightLevel;
  178. WXCoreMargin mMargin;
  179. WXCorePadding mPadding;
  180. WXCoreBorderWidth mBorderWidth;
  181. WXCorePosition mStylePosition;
  182. constexpr static float kFlexGrowDefault = 0;
  183. constexpr static WXCoreDirection kDirectionDefault = kDirectionLTR;
  184. constexpr static WXCoreFlexDirection kFlexDirectionDefault= kFlexDirectionColumn;
  185. constexpr static WXCoreFlexWrap kFlexWrapDefault = kNoWrap;
  186. constexpr static WXCoreJustifyContent kFlexJustifyContentDefault = kJustifyFlexStart;
  187. constexpr static WXCoreAlignItems kFlexAlignItemsDefault = kAlignItemsStretch;
  188. constexpr static WXCoreAlignSelf kFlexAlignSelfDefault = kAlignSelfAuto;
  189. constexpr static WXCorePositionType kWXCorePositionTypeDefault = kRelative;
  190. WXCoreCSSStyle() : mDirection(kDirectionInherit),
  191. mFlexDirection(kFlexDirectionDefault),
  192. mFlexWrap(kFlexWrapDefault),
  193. mJustifyContent(kFlexJustifyContentDefault),
  194. mAlignItems(kFlexAlignItemsDefault),
  195. mAlignSelf(kFlexAlignSelfDefault),
  196. mFlexGrow(kFlexGrowDefault),
  197. mPositionType(kWXCorePositionTypeDefault),
  198. mStyleWidth(NAN), mStyleHeight(NAN),
  199. mStyleHeightLevel(FALLBACK_STYLE), mStyleWidthLevel(FALLBACK_STYLE),
  200. mMaxWidth(NAN), mMaxHeight(NAN),
  201. mMinWidth(NAN), mMinHeight(NAN) {
  202. }
  203. ~WXCoreCSSStyle() {
  204. mDirection = kDirectionInherit;
  205. mFlexDirection = kFlexDirectionDefault;
  206. mFlexWrap = kFlexWrapDefault;
  207. mJustifyContent = kFlexJustifyContentDefault;
  208. mAlignItems = kFlexAlignItemsDefault;
  209. mAlignSelf = kFlexAlignSelfDefault;
  210. mFlexGrow = kFlexGrowDefault;
  211. mStyleWidth = NAN;
  212. mStyleHeight = NAN;
  213. mStyleWidthLevel = FALLBACK_STYLE;
  214. mStyleHeightLevel = FALLBACK_STYLE;
  215. mMaxWidth = NAN;
  216. mMaxHeight = NAN;
  217. mMinWidth = NAN;
  218. mMinHeight = NAN;
  219. }
  220. inline float sumPaddingBorderOfEdge(const WXCoreEdge edge){
  221. switch (edge) {
  222. case kTop:
  223. return mPadding.getPadding(kPaddingTop)
  224. + mBorderWidth.getBorderWidth(kBorderWidthTop);
  225. case kRight:
  226. return mPadding.getPadding(kPaddingRight)
  227. + mBorderWidth.getBorderWidth(kBorderWidthRight);
  228. case kBottom:
  229. return mPadding.getPadding(kPaddingBottom)
  230. + mBorderWidth.getBorderWidth(kBorderWidthBottom);
  231. case kLeft:
  232. return mPadding.getPadding(kPaddingLeft)
  233. + mBorderWidth.getBorderWidth(kBorderWidthLeft);
  234. }
  235. }
  236. float sumMarginOfDirection(bool horizontal){
  237. if(horizontal){
  238. return mMargin.getMargin(kMarginLeft) + mMargin.getMargin(kMarginRight);
  239. }
  240. else{
  241. return mMargin.getMargin(kMarginTop) + mMargin.getMargin(kMarginBottom);
  242. }
  243. }
  244. };
  245. }
  246. #endif //WEEXCORE_FLEXLAYOUT_WXCOREFLEXENUMS_H
  247. #endif