1- /* Copyright (c) 2024 Bangle.js contributors. See the file LICENSE for copying permission. */
1+ /* Copyright (c) 2025 Bangle.js contributors. See the file LICENSE for copying permission. */
22
33// See Layout.md for documentation
44
@@ -233,47 +233,6 @@ Layout.prototype.forgetLazyState = function () {
233233 this . rects = { } ;
234234} ;
235235
236- Layout . prototype . layout = function ( l ) {
237- // l = current layout element
238- var floor = Math . floor , cb = {
239- "h" : function ( l ) { "ram" ;
240- var acc_w = l . x + ( 0 | l . pad ) ,
241- accfillx = 0 ,
242- fillx = l . c && l . c . reduce ( ( a , l ) => a + ( 0 | l . fillx ) , 0 ) ;
243- if ( ! fillx ) { acc_w += ( l . w - l . _w ) >> 1 ; fillx = 1 ; }
244- var x = acc_w ;
245- l . c . forEach ( c => {
246- c . x = 0 | x ;
247- acc_w += c . _w ;
248- accfillx += 0 | c . fillx ;
249- x = acc_w + floor ( accfillx * ( l . w - l . _w ) / fillx ) ;
250- c . w = 0 | ( x - c . x ) ;
251- c . h = 0 | ( c . filly ? l . h - ( l . pad << 1 ) : c . _h ) ;
252- c . y = 0 | ( l . y + ( 0 | l . pad ) + ( ( 1 + ( 0 | c . valign ) ) * ( l . h - ( l . pad << 1 ) - c . h ) >> 1 ) ) ;
253- if ( c . c ) cb [ c . type ] ( c ) ;
254- } ) ;
255- } ,
256- "v" : function ( l ) { "ram" ;
257- var acc_h = l . y + ( 0 | l . pad ) ,
258- accfilly = 0 ,
259- filly = l . c && l . c . reduce ( ( a , l ) => a + ( 0 | l . filly ) , 0 ) ;
260- if ( ! filly ) { acc_h += ( l . h - l . _h ) >> 1 ; filly = 1 ; }
261- var y = acc_h ;
262- l . c . forEach ( c => {
263- c . y = 0 | y ;
264- acc_h += c . _h ;
265- accfilly += 0 | c . filly ;
266- y = acc_h + floor ( accfilly * ( l . h - l . _h ) / filly ) ;
267- c . h = 0 | ( y - c . y ) ;
268- c . w = 0 | ( c . fillx ? l . w - ( l . pad << 1 ) : c . _w ) ;
269- c . x = 0 | ( l . x + ( 0 | l . pad ) + ( ( 1 + ( 0 | c . halign ) ) * ( l . w - ( l . pad << 1 ) - c . w ) >> 1 ) ) ;
270- if ( c . c ) cb [ c . type ] ( c ) ;
271- } ) ;
272- }
273- } ;
274- if ( cb [ l . type ] ) cb [ l . type ] ( l ) ;
275- } ;
276-
277236Layout . prototype . debug = function ( l , c ) {
278237 if ( ! l ) l = this . _l ;
279238 c = c || 1 ;
@@ -287,7 +246,7 @@ Layout.prototype.debug = function(l,c) {
287246Layout . prototype . update = function ( ) {
288247 delete this . updateNeeded ;
289248 var gfx = g , max = Math . max , rnd = Math . round ; // define locally, because this is faster
290- // update sizes
249+ // update sizes. We first set _w/_h as the minimum sizes (working from the 'leaves' of the tree up)
291250 function updateMin ( l ) { "ram" ;
292251 cb [ l . type ] ( l ) ;
293252 if ( l . r & 1 ) { // rotation
@@ -327,20 +286,20 @@ Layout.prototype.update = function() {
327286 } , "h" : function ( l ) { "ram" ;
328287 l . c . forEach ( updateMin ) ;
329288 l . _h = l . c . reduce ( ( a , b ) => max ( a , b . _h ) , 0 ) ;
330- l . _w = l . c . reduce ( ( a , b ) => a + b . _w , 0 ) ;
289+ l . __w = l . _w = l . c . reduce ( ( a , b ) => a + b . _w , 0 ) ; // set __w as minimum width which'll ignore .width
331290 if ( l . fillx == null && l . c . some ( c => c . fillx ) ) l . fillx = 1 ;
332291 if ( l . filly == null && l . c . some ( c => c . filly ) ) l . filly = 1 ;
333292 } , "v" : function ( l ) { "ram" ;
334293 l . c . forEach ( updateMin ) ;
335- l . _h = l . c . reduce ( ( a , b ) => a + b . _h , 0 ) ;
294+ l . __h = l . _h = l . c . reduce ( ( a , b ) => a + b . _h , 0 ) ; // set __h as minimum height which'll ignore .height
336295 l . _w = l . c . reduce ( ( a , b ) => max ( a , b . _w ) , 0 ) ;
337296 if ( l . fillx == null && l . c . some ( c => c . fillx ) ) l . fillx = 1 ;
338297 if ( l . filly == null && l . c . some ( c => c . filly ) ) l . filly = 1 ;
339298 }
340299 } ;
341300 var l = this . _l ;
342301 updateMin ( l ) ;
343- delete cb ;
302+ // set outer dimensions
344303 if ( l . fillx || l . filly ) { // fill all
345304 l . w = Bangle . appRect . w ;
346305 l . h = Bangle . appRect . h ;
@@ -352,8 +311,50 @@ Layout.prototype.update = function() {
352311 l . x = ( Bangle . appRect . w - l . w ) >> 1 ;
353312 l . y = Bangle . appRect . y + ( ( Bangle . appRect . h - l . h ) >> 1 ) ;
354313 }
355- // layout children
356- this . layout ( l ) ;
314+ // Now work from top down using minimum _w/_h to set real w/h values
315+ var floor = Math . floor ;
316+ cb = {
317+ "h" : function ( l ) { "ram" ;
318+ var acc_w = l . x + ( 0 | l . pad ) ,
319+ accfillx = 0 ,
320+ fillx = l . c && l . c . reduce ( ( a , l ) => a + ( 0 | l . fillx ) , 0 ) ;
321+ if ( ! fillx ) { acc_w += ( l . w - ( l . pad << 1 ) - l . __w ) >> 1 ; fillx = 1 ; }
322+ var x = acc_w ;
323+ l . c . forEach ( c => {
324+ c . x = 0 | x ;
325+ acc_w += c . _w ;
326+ accfillx += 0 | c . fillx ;
327+ x = acc_w + floor ( accfillx * ( l . w - l . _w ) / fillx ) ;
328+ c . w = 0 | ( x - c . x ) ;
329+ c . h = 0 | ( c . filly ? l . h - ( l . pad << 1 ) : c . _h ) ;
330+ c . y = 0 | ( l . y + ( 0 | l . pad ) + ( ( 1 + ( 0 | c . valign ) ) * ( l . h - ( l . pad << 1 ) - c . h ) >> 1 ) ) ;
331+ if ( c . c ) cb [ c . type ] ( c ) ;
332+ delete c . _w ; delete c . _h ; // free memory
333+ } ) ;
334+ delete l . __w ;
335+ } ,
336+ "v" : function ( l ) { "ram" ;
337+ var acc_h = l . y + ( 0 | l . pad ) ,
338+ accfilly = 0 ,
339+ filly = l . c && l . c . reduce ( ( a , l ) => a + ( 0 | l . filly ) , 0 ) ;
340+ if ( ! filly ) { acc_h += ( l . h - ( l . pad << 1 ) - l . __h ) >> 1 ; filly = 1 ; }
341+ var y = acc_h ;
342+ l . c . forEach ( c => {
343+ c . y = 0 | y ;
344+ acc_h += c . _h ;
345+ accfilly += 0 | c . filly ;
346+ y = acc_h + floor ( accfilly * ( l . h - l . _h ) / filly ) ;
347+ c . h = 0 | ( y - c . y ) ;
348+ c . w = 0 | ( c . fillx ? l . w - ( l . pad << 1 ) : c . _w ) ;
349+ c . x = 0 | ( l . x + ( 0 | l . pad ) + ( ( 1 + ( 0 | c . halign ) ) * ( l . w - ( l . pad << 1 ) - c . w ) >> 1 ) ) ;
350+ if ( c . c ) cb [ c . type ] ( c ) ;
351+ delete c . _w ; delete c . _h ; // free memory
352+ } ) ;
353+ delete l . __h ;
354+ }
355+ } ;
356+ if ( cb [ l . type ] ) cb [ l . type ] ( l ) ;
357+ delete l . _w ; delete l . _h ; // free memory
357358} ;
358359
359360Layout . prototype . clear = function ( l ) {
@@ -363,4 +364,4 @@ Layout.prototype.clear = function(l) {
363364 g . clearRect ( l . x , l . y , l . x + l . w - 1 , l . y + l . h - 1 ) ;
364365} ;
365366
366- exports = Layout ;
367+ exports = Layout ;
0 commit comments