wintf 0.0.3

Windows Tategaki Framework - Rust UI library with Japanese vertical text support
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use super::*;

#[test]
fn test_phase_tunnel() {
    let phase = Phase::Tunnel(42);
    assert!(phase.is_tunnel());
    assert!(!phase.is_bubble());
    assert_eq!(*phase.value(), 42);
}

#[test]
fn test_phase_bubble() {
    let phase = Phase::Bubble("test");
    assert!(!phase.is_tunnel());
    assert!(phase.is_bubble());
    assert_eq!(*phase.value(), "test");
}

#[test]
fn test_phase_clone() {
    let phase = Phase::Tunnel(100);
    let cloned = phase.clone();
    assert_eq!(*cloned.value(), 100);
}

#[test]
fn test_handler_component_size() {
    // ハンドラコンポーネントは fn ポインタのサイズのみ
    use std::mem::size_of;
    assert_eq!(
        size_of::<OnPointerPressed>(),
        size_of::<PointerEventHandler>()
    );
    assert_eq!(
        size_of::<OnPointerMoved>(),
        size_of::<PointerEventHandler>()
    );
}

#[test]
fn test_build_bubble_path_single_entity() {
    let mut world = World::new();
    let entity = world.spawn_empty().id();

    let path = build_bubble_path(&world, entity);
    assert_eq!(path.len(), 1);
    assert_eq!(path[0], entity);
}

#[test]
fn test_build_bubble_path_hierarchy() {
    let mut world = World::new();
    let root = world.spawn_empty().id();
    let child = world.spawn(ChildOf(root)).id();
    let grandchild = world.spawn(ChildOf(child)).id();

    let path = build_bubble_path(&world, grandchild);
    assert_eq!(path.len(), 3);
    assert_eq!(path[0], grandchild);
    assert_eq!(path[1], child);
    assert_eq!(path[2], root);
}

#[test]
fn test_dispatch_with_no_handlers() {
    let mut world = World::new();
    let entity = world.spawn(PointerState::default()).id();

    // ハンドラなしでもパニックしない
    dispatch_pointer_events(&mut world);

    // エンティティがまだ存在することを確認
    assert!(world.get_entity(entity).is_ok());
}

#[test]
fn test_dispatch_with_handler() {
    use std::sync::atomic::{AtomicU32, Ordering};
    static CALL_COUNT: AtomicU32 = AtomicU32::new(0);

    fn test_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_bubble() {
            CALL_COUNT.fetch_add(1, Ordering::SeqCst);
        }
        false // 伝播続行
    }

    CALL_COUNT.store(0, Ordering::SeqCst);

    let mut world = World::new();
    let entity = world
        .spawn((PointerState::default(), OnPointerMoved(test_handler)))
        .id();

    dispatch_pointer_events(&mut world);

    // Bubble フェーズでハンドラが呼ばれたことを確認
    assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
    assert!(world.get_entity(entity).is_ok());
}

#[test]
fn test_dispatch_stop_propagation() {
    use std::sync::atomic::{AtomicU32, Ordering};
    static TUNNEL_COUNT: AtomicU32 = AtomicU32::new(0);
    static BUBBLE_COUNT: AtomicU32 = AtomicU32::new(0);

    fn stopping_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_tunnel() {
            TUNNEL_COUNT.fetch_add(1, Ordering::SeqCst);
            false // Tunnel では停止しない
        } else {
            BUBBLE_COUNT.fetch_add(1, Ordering::SeqCst);
            true // Bubble で停止
        }
    }

    fn never_called_in_bubble_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_bubble() {
            BUBBLE_COUNT.fetch_add(100, Ordering::SeqCst); // 呼ばれたら大きな値を加算
        }
        false
    }

    TUNNEL_COUNT.store(0, Ordering::SeqCst);
    BUBBLE_COUNT.store(0, Ordering::SeqCst);

    let mut world = World::new();
    let root = world
        .spawn(OnPointerMoved(never_called_in_bubble_handler))
        .id();
    let child = world
        .spawn((
            ChildOf(root),
            PointerState::default(),
            OnPointerMoved(stopping_handler),
        ))
        .id();

    dispatch_pointer_events(&mut world);

    // Tunnel: root(無) → child(stopping_handler) → 1回
    // Bubble: child(stopping_handler) → 停止 → root は呼ばれない
    assert_eq!(TUNNEL_COUNT.load(Ordering::SeqCst), 1);
    assert_eq!(BUBBLE_COUNT.load(Ordering::SeqCst), 1);
    assert!(world.get_entity(child).is_ok());
}

#[test]
fn test_phase_value_on_bubble() {
    // value() は Tunnel/Bubble いずれでもデータを返す(既存は Tunnel のみ)
    let phase = Phase::Bubble(7);
    assert_eq!(*phase.value(), 7);
    assert!(phase.is_bubble());
    assert!(!phase.is_tunnel());
}

#[test]
fn test_dispatch_tunnel_order_is_root_to_sender() {
    // Tunnel フェーズは root → sender の順で呼ばれることを記録順で検証する
    use std::sync::Mutex;
    static ORDER: Mutex<Vec<u8>> = Mutex::new(Vec::new());

    fn root_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_tunnel() {
            ORDER.lock().unwrap().push(0); // root
        }
        false
    }
    fn child_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_tunnel() {
            ORDER.lock().unwrap().push(1); // child(sender)
        }
        false
    }

    ORDER.lock().unwrap().clear();

    let mut world = World::new();
    let root = world.spawn(OnPointerMoved(root_handler)).id();
    let _child = world
        .spawn((
            ChildOf(root),
            PointerState::default(),
            OnPointerMoved(child_handler),
        ))
        .id();

    dispatch_pointer_events(&mut world);

    // Tunnel は root(0) → child(1) の順
    assert_eq!(
        *ORDER.lock().unwrap(),
        vec![0, 1],
        "Tunnel は root→sender 順"
    );
}

#[test]
fn test_dispatch_pressed_gating_requires_main_button() {
    // OnPointerPressed は left/right/middle のいずれかが down のときのみ発火する。
    // XButton1/XButton2 だけでは発火しない(dispatch/mod.rs:231 のゲート条件)。
    use std::sync::atomic::{AtomicU32, Ordering};
    static PRESSED_COUNT: AtomicU32 = AtomicU32::new(0);

    fn pressed_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_bubble() {
            PRESSED_COUNT.fetch_add(1, Ordering::SeqCst);
        }
        false
    }

    // ケース1: XButton1 のみ down → 発火しない
    PRESSED_COUNT.store(0, Ordering::SeqCst);
    {
        let mut world = World::new();
        world.spawn((
            PointerState {
                xbutton1_down: true,
                ..Default::default()
            },
            OnPointerPressed(pressed_handler),
        ));
        dispatch_pointer_events(&mut world);
        assert_eq!(
            PRESSED_COUNT.load(Ordering::SeqCst),
            0,
            "XButton のみでは Pressed 不発火"
        );
    }

    // ケース2: Left down → 発火
    PRESSED_COUNT.store(0, Ordering::SeqCst);
    {
        let mut world = World::new();
        world.spawn((
            PointerState {
                left_down: true,
                ..Default::default()
            },
            OnPointerPressed(pressed_handler),
        ));
        dispatch_pointer_events(&mut world);
        assert_eq!(
            PRESSED_COUNT.load(Ordering::SeqCst),
            1,
            "Left down で Pressed 発火"
        );
    }
}

#[test]
fn test_dispatch_clears_button_state_after_dispatch() {
    // dispatch 後にボタン状態・double_click・解放の旗がクリアされる(次フレーム再発火防止、
    // dispatch/mod.rs の末尾のクリア)。位置・修飾キーはクリアされない。
    let mut world = World::new();
    let e = world
        .spawn(PointerState {
            left_down: true,
            right_down: true,
            middle_down: true,
            xbutton1_down: true,
            xbutton2_down: true,
            double_click: super::super::DoubleClick::Left,
            released: super::super::ButtonReleased {
                left: true,
                right: true,
                middle: true,
                xbutton1: true,
                xbutton2: true,
            },
            shift_down: true, // 修飾キーは保持されることを確認
            ..Default::default()
        })
        .id();

    dispatch_pointer_events(&mut world);

    let s = world.get::<PointerState>(e).unwrap();
    assert!(
        !s.left_down && !s.right_down && !s.middle_down,
        "主ボタンクリア"
    );
    assert!(!s.xbutton1_down && !s.xbutton2_down, "XButton クリア");
    assert_eq!(
        s.double_click,
        super::super::DoubleClick::None,
        "double_click クリア"
    );
    assert_eq!(
        s.released,
        super::super::ButtonReleased::default(),
        "解放の旗は 1 フレーム限りでクリア"
    );
    assert!(!s.released.any(), "any() も偽に戻る");
    assert!(s.shift_down, "修飾キーは dispatch でクリアされない");
}

#[test]
fn test_dispatch_released_only_fires_released_handler_once() {
    // 解放の旗だけが立った tick では OnPointerReleased が Bubble で 1 回配られる。
    use std::sync::atomic::{AtomicU32, Ordering};
    static RELEASED_COUNT: AtomicU32 = AtomicU32::new(0);

    fn released_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_bubble() {
            RELEASED_COUNT.fetch_add(1, Ordering::SeqCst);
        }
        false
    }

    RELEASED_COUNT.store(0, Ordering::SeqCst);

    let mut world = World::new();
    world.spawn((
        PointerState {
            released: super::super::ButtonReleased {
                right: true,
                ..Default::default()
            },
            ..Default::default()
        },
        OnPointerReleased(released_handler),
    ));

    dispatch_pointer_events(&mut world);

    assert_eq!(
        RELEASED_COUNT.load(Ordering::SeqCst),
        1,
        "解放の旗で Released が 1 回配られる"
    );
}

#[test]
fn test_dispatch_released_flag_absent_does_not_fire() {
    // 旗が 1 つも立っていなければ Released は配られない(any() のゲート)。
    use std::sync::atomic::{AtomicU32, Ordering};
    static RELEASED_COUNT: AtomicU32 = AtomicU32::new(0);

    fn released_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        _ev: &Phase<PointerState>,
    ) -> bool {
        RELEASED_COUNT.fetch_add(1, Ordering::SeqCst);
        false
    }

    RELEASED_COUNT.store(0, Ordering::SeqCst);

    let mut world = World::new();
    world.spawn((PointerState::default(), OnPointerReleased(released_handler)));

    dispatch_pointer_events(&mut world);

    assert_eq!(
        RELEASED_COUNT.load(Ordering::SeqCst),
        0,
        "旗なしでは Released 不発火"
    );
}

#[test]
fn test_dispatch_same_tick_press_and_release_delivers_both() {
    // 同じ tick に押下と解放が入った場合、Pressed と Released の両方が届く。
    // 記録順で「押下の配送の後に解放」も固定する。
    use std::sync::Mutex;
    static ORDER: Mutex<Vec<u8>> = Mutex::new(Vec::new());

    fn pressed_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_bubble() {
            ORDER.lock().unwrap().push(0);
        }
        false
    }
    fn released_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        if ev.is_bubble() {
            ORDER.lock().unwrap().push(1);
        }
        false
    }

    ORDER.lock().unwrap().clear();

    let mut world = World::new();
    world.spawn((
        PointerState {
            right_down: true,
            released: super::super::ButtonReleased {
                right: true,
                ..Default::default()
            },
            ..Default::default()
        },
        OnPointerPressed(pressed_handler),
        OnPointerReleased(released_handler),
    ));

    dispatch_pointer_events(&mut world);

    assert_eq!(
        *ORDER.lock().unwrap(),
        vec![0, 1],
        "同 tick の押下+解放は Pressed→Released の順で両方届く"
    );
}

#[test]
fn test_dispatch_released_handler_receives_tunnel_then_bubble() {
    // Released も既存の配送経路(Tunnel: root→sender、Bubble: sender→root)に乗る。
    use std::sync::Mutex;
    static ORDER: Mutex<Vec<&'static str>> = Mutex::new(Vec::new());

    fn root_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        ORDER
            .lock()
            .unwrap()
            .push(if ev.is_tunnel() { "root-t" } else { "root-b" });
        false
    }
    fn child_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        ev: &Phase<PointerState>,
    ) -> bool {
        ORDER
            .lock()
            .unwrap()
            .push(if ev.is_tunnel() { "child-t" } else { "child-b" });
        false
    }

    ORDER.lock().unwrap().clear();

    let mut world = World::new();
    let root = world.spawn(OnPointerReleased(root_handler)).id();
    world.spawn((
        ChildOf(root),
        PointerState {
            released: super::super::ButtonReleased {
                left: true,
                ..Default::default()
            },
            ..Default::default()
        },
        OnPointerReleased(child_handler),
    ));

    dispatch_pointer_events(&mut world);

    assert_eq!(
        *ORDER.lock().unwrap(),
        vec!["root-t", "child-t", "child-b", "root-b"],
        "Released は Tunnel(root→sender)→ Bubble(sender→root)"
    );
}

#[test]
fn test_dispatch_event_for_handler_guards_deleted_entity() {
    // path 内のエンティティが存在しない場合、dispatch_event_for_handler は
    // パニックせず静かに終了する(存在チェック、dispatch/mod.rs:165/181)。
    use std::sync::atomic::{AtomicU32, Ordering};
    static CALL: AtomicU32 = AtomicU32::new(0);

    fn counting_handler(
        _world: &mut World,
        _sender: Entity,
        _entity: Entity,
        _ev: &Phase<PointerState>,
    ) -> bool {
        CALL.fetch_add(1, Ordering::SeqCst);
        false
    }

    CALL.store(0, Ordering::SeqCst);

    let mut world = World::new();
    let real = world
        .spawn((PointerState::default(), OnPointerMoved(counting_handler)))
        .id();
    // despawn して dangling になったエンティティ ID を path 先頭に混ぜる
    let ghost = world.spawn_empty().id();
    world.despawn(ghost);

    let event = PointerState::default();
    // path = [real, ghost] : Tunnel は rev で ghost→real。先頭の ghost で存在チェックに
    // 引っかかり即 return するため、real のハンドラには到達しない。
    dispatch_event_for_handler::<PointerState, OnPointerMoved>(
        &mut world,
        real,
        &[real, ghost],
        &event,
        |h| h.0,
    );

    // ghost に到達した時点で return するため、real のハンドラは呼ばれない。
    // パニックしないことが主眼(存在チェックの特性化)。
    assert_eq!(
        CALL.load(Ordering::SeqCst),
        0,
        "削除済みエンティティで静かに終了(real ハンドラ未到達)"
    );
    assert!(world.get_entity(real).is_ok());
}