WXComponent.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. /**
  20. * def : use weex_flex_engin
  21. * ndef: use yoga
  22. **/
  23. #import <Foundation/Foundation.h>
  24. #import "WXType.h"
  25. @class WXSDKInstance;
  26. typedef enum : NSUInteger {
  27. WXDisplayTypeNone,
  28. WXDisplayTypeBlock
  29. } WXDisplayType;
  30. /**
  31. * @abstract the component callback , result can be string or dictionary.
  32. * @discussion callback data to js, the id of callback function will be removed to save memory.
  33. */
  34. typedef void (^WXCallback)(_Nonnull id result);
  35. // DEPRECATED_MSG_ATTRIBUTE("use WXKeepAliveCallback, you can specify keep the callback or not, if keeped, it can be called multi times, or it will be removed after called.")
  36. /**
  37. * @abstract the component callback , result can be string or dictionary.
  38. * @discussion callback data to js, you can specify the keepAlive parameter to keep callback function id keepalive or not. If the keepAlive is true, it won't be removed unitl instance destroyed, so you can call it repetitious.
  39. */
  40. typedef void (^WXKeepAliveCallback)(_Nonnull id result, BOOL keepAlive);
  41. NS_ASSUME_NONNULL_BEGIN
  42. @interface WXComponent : NSObject <NSCopying>
  43. ///--------------------------------------
  44. /// @name Component Hierarchy Management
  45. ///--------------------------------------
  46. /**
  47. * @abstract Initializes a new component using the specified properties.
  48. *
  49. * @param ref the identity string of component
  50. * @param type component type
  51. * @param styles component's styles
  52. * @param attributes component's attributes
  53. * @param events component's events
  54. * @param weexInstance the weexInstance with which the component associated
  55. *
  56. * @return A WXComponent instance.
  57. */
  58. - (instancetype)initWithRef:(NSString *)ref
  59. type:(NSString*)type
  60. styles:(nullable NSDictionary *)styles
  61. attributes:(nullable NSDictionary *)attributes
  62. events:(nullable NSArray *)events
  63. weexInstance:(WXSDKInstance *)weexInstance;
  64. /**
  65. * @abstract The component's identifier string.
  66. */
  67. @property (nonatomic, readonly, strong) NSString *ref;
  68. /**
  69. * @abstract The component's type string.
  70. */
  71. @property (nonatomic, readonly, copy) NSString *type;
  72. /**
  73. * @abstract The component's type.
  74. */
  75. @property (nonatomic, assign) WXComponentType componentType;
  76. /**
  77. * @abstract The component's styles.
  78. */
  79. @property (nonatomic, readonly, strong) NSDictionary *styles;
  80. /**
  81. * @abstract The component's pseudoClassStyles.
  82. */
  83. @property (nonatomic, readonly, strong) NSDictionary *pseudoClassStyles;
  84. /**
  85. * @abstract The component's attributes.
  86. */
  87. @property (nonatomic, readonly, strong) NSDictionary *attributes;
  88. /**
  89. * @abstract The component's events.
  90. */
  91. @property (nonatomic, readonly, strong) NSArray *events;
  92. /**
  93. * @abstract The reference to
  94. */
  95. @property (nonatomic, readonly, weak, nullable) WXSDKInstance *weexInstance;
  96. /**
  97. * @abstract The component's subcomponents.
  98. */
  99. @property (nonatomic, readonly, strong, nullable) NSArray<WXComponent *> *subcomponents;
  100. /**
  101. * @abstract The component's supercomponent.
  102. */
  103. @property (nonatomic, readonly, weak, nullable) WXComponent *supercomponent;
  104. ///--------------------------------------
  105. /// @name Layout
  106. ///--------------------------------------
  107. /**
  108. * @abstract Return the calculated frame.
  109. *
  110. * @warning Subclasses must not override this.
  111. */
  112. @property(nonatomic, readonly, assign) CGRect calculatedFrame;
  113. /**
  114. * @abstract Tell if component's view frame will keep synchronized with calculatedFrame.
  115. * Default Value is YES.
  116. */
  117. @property(nonatomic, assign) BOOL isViewFrameSyncWithCalculated;
  118. ///**
  119. // * @abstract Return the calculated absolute position.
  120. // *
  121. // * @warning Subclasses must not override this.
  122. // */
  123. //@property(nonatomic, assign) CGPoint absolutePosition;
  124. /**
  125. * @abstract Invalidates the component's layout and marks it as needing an update.
  126. *
  127. * @discussion You can call this method to indicate that the layout of a component has changed and must be updated. Weex typically calls this method automatically when the layout-related styles change or when subcomponents are added or removed.
  128. *
  129. */
  130. - (void)setNeedsLayout;
  131. /**
  132. * @abstract Returns a Boolean indicating whether the component has been marked as needing a layout update.
  133. *
  134. * @return YES if the component has been marked as requiring a layout update.
  135. *
  136. */
  137. - (BOOL)needsLayout;
  138. /**
  139. * @abstract return a measure block for measure component's layout
  140. *
  141. * constrainedSize: The maximum size the receiver should fit in.
  142. *
  143. * @return A block which will ask the component to measure and return the size that best fits for a constrained size.
  144. *
  145. * @discussion Subclasses can override this method to perform their own layout behaviour. Weex will use the returned block to measure the component's layout, ignoring its own layout mechanism.
  146. *
  147. */
  148. - (nullable CGSize (^)(CGSize constrainedSize))measureBlock;
  149. /**
  150. * @abstract Called on main thread when the component has just laid out.
  151. */
  152. - (void)layoutDidFinish;
  153. /**
  154. * @abstract Update component's CSS style values for external components.
  155. * Could be called in any thread and will be scheduled to component thread.
  156. */
  157. - (void)updateLayoutStyles:(NSDictionary*)styles;
  158. ///--------------------------------------
  159. /// @name View Management
  160. ///--------------------------------------
  161. /**
  162. * @abstract The view that the component manages.
  163. *
  164. * @discussion If you access this property and its value is currently nil, the component automatically calls the loadView method and returns the resulting view.
  165. *
  166. * @warning It must be on accessed on the main thread.Subclasses must not override this;
  167. */
  168. @property(nonatomic, readonly, strong) UIView *view;
  169. /**
  170. * @abstract The layer that the component manages.
  171. *
  172. * @discussion The layer property is also lazily initialized, similar to the view property.
  173. *
  174. * @warning It must be on accessed on the main thread. Subclasses must not override this;
  175. */
  176. @property(nonatomic, readonly, strong) CALayer *layer;
  177. /**
  178. * @abstract Creates the view that the component manages.
  179. *
  180. * @return View to be created
  181. *
  182. * @discussion This method loads or creates a view and assigns it to the view property. This is where subclasses should create their custom view hierarchy. Should never be called directly.The method is called on the main thread.
  183. *
  184. * @warning Your custom implementation of this method should not call super
  185. */
  186. - (UIView *)loadView;
  187. /**
  188. * @abstract Returns a Boolean value indicating whether the view is currently loaded.
  189. */
  190. - (BOOL)isViewLoaded;
  191. /**
  192. * @abstract Called before the load of component'€™s view .
  193. *
  194. * @discussion This is before -loadView. The method is called on the main thread.
  195. */
  196. - (void)viewWillLoad;
  197. /**
  198. * @abstract Called after the component'€™s view is loaded and set.
  199. *
  200. * @discussion This is after -loadView. This is the best time to perform additional initialization like adding gesture recognizers to the view.The method is called on the main thread.
  201. */
  202. - (void)viewDidLoad;
  203. /**
  204. * @abstract Called just before releasing the component'€™s view.The method is called on the main thread.
  205. */
  206. - (void)viewWillUnload;
  207. /**
  208. * @abstract Called when the component'€™s view is released.The method is called on the main thread.
  209. */
  210. - (void)viewDidUnload;
  211. /**
  212. * @abstract Inserts a subview at the specified index.
  213. *
  214. * @param subcomponent The subcomponent whose view will be inserted in the component's view.
  215. * @param index The index in the array of the subcomponents property at which to insert the view. subcomponent indices start at 0 and cannot be greater than the number of subcomponents.
  216. *
  217. * @discussion This will insert subcomponent's view to the view hierachy by default, it can be overrided to change the view hierachy. The method is called on the main thread.
  218. */
  219. - (void)insertSubview:(WXComponent *)subcomponent atIndex:(NSInteger)index;
  220. /**
  221. * @abstract Tells the component that a subcomponent's view is about to be removed.
  222. *
  223. * @discussion The method is called on the main thread.
  224. */
  225. - (void)willRemoveSubview:(WXComponent *)component;
  226. /**
  227. * @abstract Remove the component's view from its superview.
  228. *
  229. * @discussion The method is called on the main thread.
  230. */
  231. - (void)removeFromSuperview;
  232. /**
  233. * @abstract Moves the subview to a new super component in the scene. The node maintains its current position in scene coordinates.
  234. *
  235. * @param newSupercomponent An WXComponent object to move the component to
  236. * @param index The index in the array of the subcomponents property at which to insert the view. subcomponent indices start at 0 and cannot be greater than the number of subcomponents.
  237. */
  238. - (void)moveToSuperview:(WXComponent *)newSupercomponent atIndex:(NSUInteger)index;
  239. ///--------------------------------------
  240. /// @name Events
  241. ///--------------------------------------
  242. /**
  243. * @abstract Fire an event to the component in Javascript.
  244. *
  245. * @param eventName The name of the event to fire
  246. * @param params The parameters to fire with
  247. **/
  248. - (void)fireEvent:(NSString *)eventName params:(nullable NSDictionary *)params;
  249. /**
  250. * @abstract Fire an event to the component and tell Javascript which value has been changed.
  251. * Used for two-way data binding.
  252. *
  253. * @param eventName The name of the event to fire
  254. * @param params The parameters to fire with
  255. * @param domChanges The values has been changed, used for two-way data binding.
  256. **/
  257. - (void)fireEvent:(NSString *)eventName params:(nullable NSDictionary *)params domChanges:(nullable NSDictionary *)domChanges;
  258. ///--------------------------------------
  259. /// @name Updating
  260. ///--------------------------------------
  261. /**
  262. * @abstract Called when component's style are updated
  263. *
  264. * @param styles The updated style dictionary
  265. * @discussion It can be overrided to handle specific style updating. The method is called on the main thread.
  266. **/
  267. - (void)updateStyles:(NSDictionary *)styles;
  268. /**
  269. * @abstract Called when component's style are reset
  270. *
  271. * @param styles The reset style's elements
  272. * @discussion It can be overrided to handle specific style reseting. The method is called on the main thread.
  273. **/
  274. - (void)resetStyles:(NSArray *)styles;
  275. /**
  276. * @abstract Called when component's attributes are updated
  277. *
  278. * @param attributes The updated attributes dictionary
  279. * @discussion It can be overrided to handle specific attribute updating. The method is called on the main thread.
  280. **/
  281. - (void)updateAttributes:(NSDictionary *)attributes;
  282. /**
  283. * @abstract Called when adding an event to the component
  284. *
  285. * @param eventName The added event's name
  286. * @discussion It can be overrided to handle specific event adding. The method is called on the main thread.
  287. **/
  288. - (void)addEvent:(NSString *)eventName;
  289. /**
  290. * @abstract Called when removing an event from the component
  291. *
  292. * @param eventName The removed event's name
  293. * @discussion It can be overrided to handle specific event removing. The method is called on the main thread.
  294. **/
  295. - (void)removeEvent:(NSString *)eventName;
  296. /**
  297. * @abstract Update component's transform with native transform struct
  298. **/
  299. - (void)setNativeTransform:(CGAffineTransform)transform;
  300. ///--------------------------------------
  301. /// @name Display
  302. ///--------------------------------------
  303. @property (nonatomic, assign) WXDisplayType displayType;
  304. /**
  305. * @abstract Marks the view as needing display. The method should be called on the main thread.
  306. * @discussion You can use this method to notify the system that your component's contents need to be redrawn. This method makes a note of the request and returns immediately. The component is not actually redrawn until the next drawing cycle, at which point all invalidated components are updated.
  307. *
  308. */
  309. - (void)setNeedsDisplay;
  310. /**
  311. * @abstract Returns a Boolean indicating whether the component needs to be drawn by `drawRect:`
  312. */
  313. - (BOOL)needsDrawRect;
  314. /**
  315. * @abstract Draws the component’s image within the passed-in rectangle.
  316. * @parameter rect The rectangle which is the entire visible bounds of your component.
  317. * @return A UIImage containing the contents of the current bitmap graphics context.
  318. * @discussion
  319. * Subclasses that use technologies such as Core Graphics and UIKit to draw their own component’s content should override this method and implement their drawing code there. You do not need to override this method if your component sets its content in superclass's way.
  320. * By the time this method is called, UIKit has configured the drawing environment appropriately for your view and you can simply call whatever drawing methods and functions you need to render your content. Specifically, Weex creates and configures a graphics context for drawing and adjusts the transform of that context so that its origin matches the origin of your components’s bounds rectangle. You can get a reference to the graphics context using the `UIGraphicsGetCurrentContext` function, but do not establish a strong reference to the graphics context because it can change between calls to the drawRect: method.
  321. * If you already have an image that represents the content of the component, then you should just return the image and do no drawing, otherwise you should draw your content in the current context and return nil.
  322. * You should never call this method directly yourself. To invalidate part of your component's content, and thus cause that portion to be redrawn, call the `setNeedsDisplay` method instead.
  323. */
  324. - (UIImage *)drawRect:(CGRect)rect;
  325. /**
  326. * @abstract Called when a component finishes drawing its content.
  327. * @discussion Do not call this method directly. Weex calls this method at appropriate times to finish updating the component's content.
  328. * Subclasses can override this method to perform additional work on components that were rendered.
  329. */
  330. - (void)didFinishDrawingLayer:(BOOL)success;
  331. /**
  332. * readyToRender, do not use it, will be deprecated soon
  333. */
  334. - (void)readyToRender;
  335. /**
  336. * @abstract trigger display if you do not have a WXLayer
  337. */
  338. - (void)triggerDisplay;
  339. /**
  340. * @abstract Creates a graphics context with the specified bounds, the context will be used for `drawRect:` in compositing environment
  341. * @discussion You can override this method to use your own graphics context.
  342. */
  343. - (CGContextRef)beginDrawContext:(CGRect)bounds;
  344. /**
  345. * @abstract Removes the current graphics context and returns an image based on the contents of the current graphics context.
  346. * @discussion You can override this method to use your own graphics context. The image will be set to layer, if your drawing system do not have layer and do not need image, returning nil is fine.
  347. */
  348. - (UIImage *)endDrawContext:(CGContextRef)context;
  349. /**
  350. * @abstract Return a shapelayer when compoent need border radius.(Especially video components)
  351. *
  352. * @discussion You can add this shadelayer to your view.layer attached to component.
  353. *
  354. */
  355. - (CAShapeLayer *)drawBorderRadiusMaskLayer:(CGRect)rect;
  356. ///--------------------------------------
  357. /// @name Data Binding
  358. ///--------------------------------------
  359. /**
  360. * @abstract Update binding data for the component
  361. * @parameter binding data to update
  362. */
  363. - (void)updateBindingData:(NSDictionary *)data;
  364. @end
  365. @interface WXComponent (Deprecated)
  366. typedef UIImage * _Nonnull(^WXDisplayBlock)(CGRect bounds, BOOL(^isCancelled)(void));
  367. typedef void(^WXDisplayCompletionBlock)(CALayer *layer, BOOL finished);
  368. /**
  369. * @abstract Return a block to be called to draw layer.
  370. *
  371. * @discussion The block returned will be called on any thread.
  372. *
  373. */
  374. - (WXDisplayBlock)displayBlock DEPRECATED_MSG_ATTRIBUTE("use drawRect: method instead.");
  375. /**
  376. * @abstract Return a block to be called while drawing is finished.
  377. *
  378. * @discussion The block returned will be called on main thread.
  379. *
  380. */
  381. - (WXDisplayCompletionBlock)displayCompletionBlock DEPRECATED_MSG_ATTRIBUTE("use didFinishDrawingLayer: method instead.");
  382. @end
  383. @interface UIView (WXComponent)
  384. @property (nonatomic, weak) WXComponent *wx_component;
  385. @property (nonatomic, weak) NSString *wx_ref;
  386. @end
  387. @interface CALayer (WXComponent)
  388. @property (nonatomic, weak) WXComponent *wx_component;
  389. @end
  390. NS_ASSUME_NONNULL_END