2
2
3
3
mod utils;
4
4
5
+ use std:: cell:: RefCell ;
6
+ use std:: rc:: Rc ;
7
+
5
8
use js_sys:: Function ;
6
9
use js_sys:: Reflect ;
7
10
use wasm_bindgen:: prelude:: * ;
@@ -363,14 +366,18 @@ pub struct Layout {
363
366
364
367
#[ wasm_bindgen]
365
368
impl Layout {
366
- fn new ( layout : stretch:: result:: Layout ) -> Layout {
369
+ fn new ( allocator : & Allocator , node : stretch:: node:: Node ) -> Layout {
370
+ let stretch = allocator. stretch . borrow ( ) ;
371
+ let layout = stretch. layout ( node) . unwrap ( ) ;
372
+ let children = stretch. children ( node) . unwrap ( ) ;
373
+
367
374
Layout {
368
375
width : layout. size . width ,
369
376
height : layout. size . height ,
370
377
x : layout. location . x ,
371
378
y : layout. location . y ,
372
- childCount : layout . children . len ( ) ,
373
- children : layout . children . into_iter ( ) . map ( Layout :: new) . collect ( ) ,
379
+ childCount : children. len ( ) ,
380
+ children : children. into_iter ( ) . map ( |child| Layout :: new ( allocator , child ) ) . collect ( ) ,
374
381
}
375
382
}
376
383
@@ -380,8 +387,23 @@ impl Layout {
380
387
}
381
388
}
382
389
390
+ #[ wasm_bindgen]
391
+ #[ derive( Clone ) ]
392
+ pub struct Allocator {
393
+ stretch : Rc < RefCell < stretch:: node:: Stretch > > ,
394
+ }
395
+
396
+ #[ wasm_bindgen]
397
+ impl Allocator {
398
+ #[ wasm_bindgen( constructor) ]
399
+ pub fn new ( ) -> Allocator {
400
+ Allocator { stretch : Rc :: new ( RefCell :: new ( stretch:: node:: Stretch :: new ( ) ) ) }
401
+ }
402
+ }
403
+
383
404
#[ wasm_bindgen]
384
405
pub struct Node {
406
+ allocator : Allocator ,
385
407
node : stretch:: node:: Node ,
386
408
style : JsValue ,
387
409
@@ -392,60 +414,72 @@ pub struct Node {
392
414
#[ wasm_bindgen]
393
415
impl Node {
394
416
#[ wasm_bindgen( constructor) ]
395
- pub fn new ( style : & JsValue ) -> Node {
396
- Node { node : stretch:: node:: Node :: new ( parse_style ( & style) , vec ! [ ] ) , style : style. clone ( ) , childCount : 0 }
417
+ pub fn new ( allocator : & Allocator , style : & JsValue ) -> Node {
418
+ Node {
419
+ allocator : allocator. clone ( ) ,
420
+ node : allocator. stretch . borrow_mut ( ) . new_node ( parse_style ( & style) , vec ! [ ] ) . unwrap ( ) ,
421
+ style : style. clone ( ) ,
422
+ childCount : 0 ,
423
+ }
397
424
}
398
425
399
426
#[ wasm_bindgen( js_name = setMeasure) ]
400
427
pub fn set_measure ( & mut self , measure : & JsValue ) {
401
428
let measure = Function :: from ( measure. clone ( ) ) ;
402
429
403
- self . node . set_measure ( Some ( Box :: new ( move |constraints| {
404
- let widthConstraint = if let stretch:: number:: Number :: Defined ( val) = constraints. width {
405
- val. into ( )
406
- } else {
407
- JsValue :: UNDEFINED
408
- } ;
409
-
410
- let heightConstaint = if let stretch:: number:: Number :: Defined ( val) = constraints. height {
411
- val. into ( )
412
- } else {
413
- JsValue :: UNDEFINED
414
- } ;
415
-
416
- if let Ok ( result) = measure. call2 ( & JsValue :: UNDEFINED , & widthConstraint, & heightConstaint) {
417
- let width = get_f32 ( & result, "width" ) ;
418
- let height = get_f32 ( & result, "height" ) ;
419
-
420
- if width. is_some ( ) && height. is_some ( ) {
421
- return Ok ( stretch:: geometry:: Size { width : width. unwrap ( ) , height : height. unwrap ( ) } ) ;
422
- }
423
- }
430
+ self . allocator
431
+ . stretch
432
+ . borrow_mut ( )
433
+ . set_measure (
434
+ self . node ,
435
+ Some ( Box :: new ( move |constraints| {
436
+ let widthConstraint = if let stretch:: number:: Number :: Defined ( val) = constraints. width {
437
+ val. into ( )
438
+ } else {
439
+ JsValue :: UNDEFINED
440
+ } ;
441
+
442
+ let heightConstaint = if let stretch:: number:: Number :: Defined ( val) = constraints. height {
443
+ val. into ( )
444
+ } else {
445
+ JsValue :: UNDEFINED
446
+ } ;
447
+
448
+ if let Ok ( result) = measure. call2 ( & JsValue :: UNDEFINED , & widthConstraint, & heightConstaint) {
449
+ let width = get_f32 ( & result, "width" ) ;
450
+ let height = get_f32 ( & result, "height" ) ;
451
+
452
+ if width. is_some ( ) && height. is_some ( ) {
453
+ return Ok ( stretch:: geometry:: Size { width : width. unwrap ( ) , height : height. unwrap ( ) } ) ;
454
+ }
455
+ }
424
456
425
- Err ( Box :: new ( "Failed in javascript" ) )
426
- } ) ) ) ;
457
+ Err ( Box :: new ( "Failed in javascript" ) )
458
+ } ) ) ,
459
+ )
460
+ . unwrap ( ) ;
427
461
}
428
462
429
463
#[ wasm_bindgen( js_name = addChild) ]
430
464
pub fn add_child ( & mut self , child : & Node ) {
431
- self . node . add_child ( & child. node ) ;
465
+ self . allocator . stretch . borrow_mut ( ) . add_child ( self . node , child. node ) . unwrap ( ) ;
432
466
self . childCount += 1 ;
433
467
}
434
468
435
469
#[ wasm_bindgen( js_name = removeChild) ]
436
470
pub fn remove_child ( & mut self , child : & Node ) {
437
- self . node . remove_child ( & child. node ) ;
471
+ self . allocator . stretch . borrow_mut ( ) . remove_child ( self . node , child. node ) . unwrap ( ) ;
438
472
self . childCount -= 1 ;
439
473
}
440
474
441
475
#[ wasm_bindgen( js_name = replaceChildAtIndex) ]
442
476
pub fn replace_child_at_index ( & mut self , index : usize , child : & Node ) {
443
- self . node . replace_child_at_index ( index, & child. node ) ;
477
+ self . allocator . stretch . borrow_mut ( ) . replace_child_at_index ( self . node , index, child. node ) . unwrap ( ) ;
444
478
}
445
479
446
480
#[ wasm_bindgen( js_name = removeChildAtIndex) ]
447
481
pub fn remove_child_at_index ( & mut self , index : usize ) {
448
- self . node . remove_child_at_index ( index) ;
482
+ self . allocator . stretch . borrow_mut ( ) . remove_child_at_index ( self . node , index) . unwrap ( ) ;
449
483
self . childCount -= 1 ;
450
484
}
451
485
@@ -456,25 +490,28 @@ impl Node {
456
490
457
491
#[ wasm_bindgen( js_name = setStyle) ]
458
492
pub fn set_style ( & mut self , style : & JsValue ) {
459
- self . node . set_style ( parse_style ( style) ) ;
493
+ self . allocator . stretch . borrow_mut ( ) . set_style ( self . node , parse_style ( style) ) . unwrap ( ) ;
460
494
self . style = style. clone ( ) ;
461
495
}
462
496
463
497
#[ wasm_bindgen( js_name = markDirty) ]
464
498
pub fn mark_dirty ( & mut self ) {
465
- self . node . mark_dirty ( )
499
+ self . allocator . stretch . borrow_mut ( ) . mark_dirty ( self . node ) . unwrap ( )
466
500
}
467
501
468
502
#[ wasm_bindgen( js_name = isDirty) ]
469
503
pub fn is_dirty ( & self ) -> bool {
470
- self . node . dirty ( )
504
+ self . allocator . stretch . borrow ( ) . dirty ( self . node ) . unwrap ( )
471
505
}
472
506
473
507
#[ wasm_bindgen( js_name = computeLayout) ]
474
- pub fn compute_layout ( & self , size : & JsValue ) -> Layout {
475
- Layout :: new (
476
- self . node
477
- . compute_layout ( stretch:: geometry:: Size {
508
+ pub fn compute_layout ( & mut self , size : & JsValue ) -> Layout {
509
+ self . allocator
510
+ . stretch
511
+ . borrow_mut ( )
512
+ . compute_layout (
513
+ self . node ,
514
+ stretch:: geometry:: Size {
478
515
width : if let Some ( val) = get_f32 ( size, "width" ) {
479
516
stretch:: number:: Number :: Defined ( val)
480
517
} else {
@@ -485,9 +522,10 @@ impl Node {
485
522
} else {
486
523
stretch:: number:: Number :: Undefined
487
524
} ,
488
- } )
489
- . unwrap ( ) ,
490
- )
525
+ } ,
526
+ )
527
+ . unwrap ( ) ;
528
+ Layout :: new ( & self . allocator , self . node )
491
529
}
492
530
}
493
531
0 commit comments