LCOV - code coverage report
Current view: top level - widgets/uicore - flex.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 22 22
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package uicore
       2              : 
       3              : import (
       4              :         "image"
       5              :         "sync"
       6              : 
       7              :         "github.com/hajimehoshi/ebiten/v2"
       8              :         "github.com/yohamta/furex/v2"
       9              : )
      10              : 
      11              : // layoutProbe は furex から確定 frame を受け取るための捨て描画先。furex は frame を
      12              : // 描画コールバック経由でしか渡さないため、1×1 の画像へ空描画して記録させる。
      13              : // ハンドラは画像へ何も描かないので読み書きが無く、全コンテナで共有できる。
      14              : // 生成に ebiten の描画コンテキストが要るので遅延して一度だけ作る。
      15              : var (
      16              :         probeOnce sync.Once
      17              :         probe     *ebiten.Image
      18              : )
      19              : 
      20              : func layoutProbe() *ebiten.Image {
      21            2 :         probeOnce.Do(func() { probe = ebiten.NewImage(1, 1) })
      22            1 :         return probe
      23              : }
      24              : 
      25              : // frameTo は furex.Drawer を実装し、確定 frame を widget の Layout へ流す。
      26              : // furex はレイアウト計算にだけ使い、描画は保持ツリーが Canvas 経由で行う。
      27              : type frameTo struct{ w Widget }
      28              : 
      29              : // Draw は furex.Drawer を実装する。画面へは何も描かず、frame の記録だけを行う。
      30              : func (f frameTo) Draw(_ *ebiten.Image, frame image.Rectangle, _ *furex.View) {
      31            1 :         if f.w != nil {
      32            1 :                 f.w.Layout(frame)
      33            1 :         }
      34              : }
      35              : 
      36              : // FlexItem は縦 flex の1行。W が nil の行は間隔だけを占めるスペーサ。
      37              : // Grow が真の行は余り高さを吸収し、後続の行を下端へ押し付ける。
      38              : type FlexItem struct {
      39              :         W      Widget
      40              :         Height int
      41              :         Grow   bool
      42              : }
      43              : 
      44              : // FlexColumn は items を flexbox の縦方向で inner に配置し、各 widget の矩形を確定する。
      45              : // 幅は既定の AlignItemStretch で inner いっぱいに伸びる。Container の固定高の積み上げと
      46              : // 違い、Grow の行で余り高さを吸収できる。画面の枠組みがフッタを下端へ固定するのに使う。
      47              : func FlexColumn(inner image.Rectangle, items []FlexItem) {
      48            1 :         root := &furex.View{
      49            1 :                 Left:      inner.Min.X,
      50            1 :                 Top:       inner.Min.Y,
      51            1 :                 Width:     inner.Dx(),
      52            1 :                 Height:    inner.Dy(),
      53            1 :                 Direction: furex.Column,
      54            1 :         }
      55            1 :         for _, it := range items {
      56            1 :                 child := &furex.View{Handler: frameTo{w: it.W}}
      57            1 :                 if it.Grow {
      58            1 :                         child.Grow = 1
      59            1 :                 } else {
      60            1 :                         child.Height = it.Height
      61            1 :                 }
      62            1 :                 root.AddChild(child)
      63              :         }
      64            1 :         root.Layout()
      65            1 :         root.Draw(layoutProbe())
      66              : }
        

Generated by: LCOV version 2.0-1