Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import type { Layer } from "../Layer";
 
export interface RenderContext {
  x: number; // relative [0,1]
  y: number; // relative [0,1]
  width: number; // relative [0,1]
  height: number; // relative [0,1]
}
 
export interface LayerMProps {
  isVisible: () => boolean;
  width: () => number;
  height: () => number;
}
 
function toAbs(ctx: RenderContext, target: Layer) {
  return {
    x: ctx.x * target.width,
    y: ctx.y * target.height,
    width: ctx.width * target.width,
    height: ctx.height * target.height,
  };
}
 
/**
 * LayerM is a monadic, pure functional wrapper for layer composition.
 * It only stores a render function and a props getter, all state is captured in closures.
 */
export class LayerM {
  private renderFn: (target: Layer, ctx: RenderContext) => void;
  public props: LayerMProps;
 
  private constructor(renderFn: (target: Layer, ctx: RenderContext) => void, props: LayerMProps) {
    this.renderFn = renderFn;
    this.props = props;
  }
 
  /**
   * Lift a Layer to a LayerM
   */
  public static lift(layer: Layer): LayerM {
    const props: LayerMProps = {
      isVisible: () => layer.isVisible,
      width: () => (layer.isVisible ? layer.width : 0),
      height: () => (layer.isVisible ? layer.height : 0),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      if (!layer.isVisible) return;
      const abs = toAbs(ctx, target);
      layer.drawToRegion(target, abs.x, abs.y, abs.width, abs.height);
    }, props);
  }
 
  /**
   * Monadic bind operation
   */
  public bind(fn: (renderFn: (target: Layer, ctx: RenderContext) => void, props: LayerMProps) => LayerM): LayerM {
    return fn(this.renderFn, this.props);
  }
 
  /**
   * Monadic map operation
   */
  public map(
    fn: (
      renderFn: (target: Layer, ctx: RenderContext) => void,
      props: LayerMProps,
    ) => [(target: Layer, ctx: RenderContext) => void, LayerMProps],
  ): LayerM {
    const [newRenderFn, newProps] = fn(this.renderFn, this.props);
    return new LayerM(newRenderFn, newProps);
  }
 
  /**
   * Place this layer on top of another layer
   */
  public onTopOf(other: LayerM): LayerM {
    const props: LayerMProps = {
      isVisible: () => this.props.isVisible() || other.props.isVisible(),
      width: () => Math.max(this.props.width(), other.props.width()),
      height: () => Math.max(this.props.height(), other.props.height()),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      other.renderTo(target, ctx);
      this.renderTo(target, ctx);
    }, props);
  }
 
  /**
   * Stack this layer above another layer
   */
  public above(other: LayerM): LayerM {
    const props: LayerMProps = {
      isVisible: () => this.props.isVisible() || other.props.isVisible(),
      width: () => Math.max(this.props.width(), other.props.width()),
      height: () => this.props.height() + other.props.height(),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      const visibleLayers = [this, other].filter((l) => l.isVisible());
      const propsArr = visibleLayers.map((l) => l.props);
      const totalHeight = propsArr.reduce((sum, p) => sum + p.height(), 0);
      let yOffset = ctx.y;
      for (let i = 0; i < visibleLayers.length; i++) {
        const hRel = (propsArr[i].height() / totalHeight) * ctx.height;
        visibleLayers[i].renderTo(target, {
          x: ctx.x,
          y: yOffset,
          width: ctx.width,
          height: hRel,
        });
        yOffset += hRel;
      }
    }, props);
  }
 
  /**
   * Stack this layer below another layer
   */
  public below(other: LayerM): LayerM {
    return other.above(this);
  }
 
  /**
   * Stack multiple layers vertically, mapping each child's height to a proportional region of ctx.height
   */
  public static vStack(layers: LayerM[]): LayerM {
    if (layers.length === 0) {
      throw new Error("Cannot create empty vStack");
    }
    const propsArr = layers.map((l) => l.props);
    const props: LayerMProps = {
      isVisible: () => propsArr.some((p) => p.isVisible()),
      width: () => Math.max(...propsArr.map((p) => p.width())),
      height: () => propsArr.reduce((sum, p) => sum + p.height(), 0),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      const visibleLayers = layers.filter((l) => l.isVisible());
      const propsArr = visibleLayers.map((l) => l.props);
      const totalHeight = propsArr.reduce((sum, p) => sum + p.height(), 0);
      let yOffset = ctx.y;
      for (let i = 0; i < visibleLayers.length; i++) {
        const hRel = (propsArr[i].height() / totalHeight) * ctx.height;
        visibleLayers[i].renderTo(target, {
          x: ctx.x,
          y: yOffset,
          width: ctx.width,
          height: hRel,
        });
        yOffset += hRel;
      }
    }, props);
  }
 
  /**
   * Stack multiple layers horizontally, mapping each child's width to a proportional region of ctx.width
   */
  public static hStack(layers: LayerM[]): LayerM {
    if (layers.length === 0) {
      throw new Error("Cannot create empty hStack");
    }
    const propsArr = layers.map((l) => l.props);
    const props: LayerMProps = {
      isVisible: () => propsArr.some((p) => p.isVisible()),
      width: () => propsArr.reduce((sum, p) => sum + p.width(), 0),
      height: () => Math.max(...propsArr.map((p) => p.height())),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      const visibleLayers = layers.filter((l) => l.isVisible());
      const propsArr = visibleLayers.map((l) => l.props);
      const totalWidth = propsArr.reduce((sum, p) => sum + p.width(), 0);
      let xOffset = ctx.x;
      for (let i = 0; i < visibleLayers.length; i++) {
        const wRel = (propsArr[i].width() / totalWidth) * ctx.width;
        visibleLayers[i].renderTo(target, {
          x: xOffset,
          y: ctx.y,
          width: wRel,
          height: ctx.height,
        });
        xOffset += wRel;
      }
    }, props);
  }
 
  /**
   * Overlay multiple layers
   */
  public static overlay(layers: LayerM[]): LayerM {
    if (layers.length === 0) {
      throw new Error("Cannot create empty overlay");
    }
    const propsArr = layers.map((l) => l.props);
    const props: LayerMProps = {
      isVisible: () => propsArr.some((p) => p.isVisible()),
      width: () => Math.max(...propsArr.map((p) => p.width())),
      height: () => Math.max(...propsArr.map((p) => p.height())),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      for (const layer of layers) {
        layer.renderTo(target, ctx);
      }
    }, props);
  }
 
  /**
   * Render this layer to a target layer at a given region
   * If ctx is not provided, render to the full target region (0,0,1,1)
   */
  public renderTo(target: Layer, ctx: RenderContext = { x: 0, y: 0, width: 1, height: 1 }): void {
    this.renderFn(target, ctx);
  }
 
  /**
   * Returns true if the layer or any child is visible (deferred)
   */
  public isVisible(): boolean {
    return this.props.isVisible();
  }
 
  /**
   * Returns the dimensions of the layer (deferred)
   */
  public getDimensions(): { width: number; height: number } {
    return {
      width: this.props.width(),
      height: this.props.height(),
    };
  }
 
  /**
   * Conditional composition: if condition is true, use thenLayer, else use elseLayer.
   */
  public static ifM(cond: boolean | (() => boolean), thenLayer: LayerM, elseLayer: LayerM): LayerM {
    const condFn = typeof cond === "function" ? cond : () => cond;
    const props: LayerMProps = {
      isVisible: () => (condFn() ? thenLayer.props.isVisible() : elseLayer.props.isVisible()),
      width: () => (condFn() ? thenLayer.props.width() : elseLayer.props.width()),
      height: () => (condFn() ? thenLayer.props.height() : elseLayer.props.height()),
    };
    return new LayerM((target: Layer, ctx: RenderContext) => {
      if (condFn()) {
        thenLayer.renderTo(target, ctx);
      } else {
        elseLayer.renderTo(target, ctx);
      }
    }, props);
  }
 
  /**
   * Shift the rendered region by absolute pixel values dx and dy (relative to the target's size),
   * and reduce the available width/height accordingly.
   */
  public shift(dx: number, dy: number): LayerM {
    const props = this.props;
    return new LayerM((target: Layer, ctx: RenderContext) => {
      const pixelRatio = (target as any).pixelRatio || window.devicePixelRatio || 1;
      const relDx = (dx * pixelRatio) / target.width;
      const relDy = (dy * pixelRatio) / target.height;
      this.renderTo(target, {
        x: ctx.x + relDx,
        y: ctx.y + relDy,
        width: ctx.width - relDx,
        height: ctx.height - relDy,
      });
    }, props);
  }
}