slint-pixel 0.3.0

可复用像素风 Slint 1.18 组件库:16×16 像素画板 widget + 像素标题栏(跨平台)
Documentation
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import { PixelTheme } from "pixel_theme.slint";
// ============================================================================
// slint-pixel · 进阶像素风组件(无系统依赖,纯 Slint 自绘,跨平台)
//
// 组件清单:
//   PixelRadioButton   单选框(独立使用,in-out checked)
//   PixelRadioGroup    单选框组(互斥,数据驱动 options)
//   PixelComboBox      下拉选择框(PopupWindow,owner 可控制 open)
//   PixelMenu          下拉菜单(按钮触发 + 选项列表,"---" 项渲染分隔线)
//   PixelTableView     表格(表头 + 数据行 + 选中行回调)
//   PixelScrollPanel   滚动面板(像素滚动条:滚轮 + 拖拽滑块,内容 @children)
//   PixelCollapsible   可折叠面板(标题栏点击展开/收起,内容 @children)
//   PixelAccordion     手风琴(数据驱动 titles/contents,互斥展开)
//   PixelSidebar       侧边栏(导航项 + 选中态 + 内容区 @children)
//
// 所有组件主题色均为 in 属性,宿主可在 .slint 里覆盖。
// ============================================================================

// ===== 通用弹出列表(内部组件,供 ComboBox / Menu 复用) =====
component PixelPopupList inherits Rectangle {
    in property <[string]> items: [];
    in property <int> current: -1;
    in property <length> item-height: 28px;
    in property <color> face: PixelTheme.bg;
    in property <color> face-hover: PixelTheme.hover;
    in property <color> face-current: PixelTheme.accent;
    in property <color> edge: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    callback selected(index: int);

    Rectangle {
        width: 100%; height: root.height;
        background: transparent;
        border-radius: PixelTheme.radius;
        Rectangle {
            x: 0px; y: 0px;
            width: parent.width; height: parent.height;
            background: root.face;
            border-width: PixelTheme.border-width;
            border-radius: PixelTheme.radius;
            border-color: root.edge;
            VerticalLayout {
                padding: 2px;
                spacing: 0px;
                for item[i] in root.items : Rectangle {
                    height: root.item-height;
                    Rectangle {
                        visible: item == "---";
                        x: 6px; y: 13px;
                        width: parent.width - 12px; height: PixelTheme.border-width;
                        background: root.edge;
                    }
                    Rectangle {
                        visible: item != "---";
                        background: root.current == i ? root.face-current
                            : (item-ta.has-hover ? root.face-hover : transparent);
                        border-radius: PixelTheme.radius-sm;
                        Text {
                            x: 10px; y: 0px;
                            width: parent.width - 20px; height: parent.height;
                            text: item;
                            color: root.current == i ? PixelTheme.on-accent : root.text-color;
                            font-size: 13px;
                            vertical-alignment: center;
                        }
                    }
                    item-ta := TouchArea {
                        visible: item != "---";
                        width: 100%; height: 100%;
                        clicked => { root.selected(i); }
                        mouse-cursor: pointer;
                    }
                }
            }
        }
    }
}

// ===== 单选框 =====
export component PixelRadioButton inherits Rectangle {
    in-out property <bool> checked: false;
    in property <string> text: "";
    in property <bool> enabled: true;
    in property <color> face: PixelTheme.hover;
    in property <color> face-checked: PixelTheme.accent;
    in property <color> border: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> dot-color: PixelTheme.text;
    callback toggled;
    height: 24px;
    opacity: root.enabled ? 1.0 : 0.5;

    HorizontalLayout {
        spacing: 8px;
        Rectangle {
            width: 22px; height: 22px;
            background: root.shadow;
            border-radius: PixelTheme.radius-sm;
            Rectangle {
                x: ta.pressed ? 1px : 0px;
                y: ta.pressed ? 1px : 0px;
                width: 22px; height: 22px;
                background: root.checked ? root.face-checked : root.face;
                border-width: PixelTheme.border-width;
                border-radius: PixelTheme.radius-sm;
                border-color: root.checked ? root.face-checked : root.border;
                Rectangle {
                    visible: root.checked;
                    x: 7px; y: 7px; width: 8px; height: 8px;
                    background: root.checked ? PixelTheme.on-accent : root.dot-color;
                }
                ta := TouchArea {
                    enabled: root.enabled;
                    clicked => {
                        root.checked = true;
                        root.toggled();
                    }
                    mouse-cursor: pointer;
                }
            }
        }
        Text {
            text: root.text;
            color: root.text-color;
            vertical-alignment: center;
            font-size: 13px;
        }
    }
}

// ===== 单选框组(互斥,数据驱动) =====
export component PixelRadioGroup inherits Rectangle {
    in property <[string]> options: [];
    in-out property <int> index: 0;
    in property <bool> enabled: true;
    in property <color> face: PixelTheme.hover;
    in property <color> face-checked: PixelTheme.accent;
    in property <color> border: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> dot-color: PixelTheme.text;
    callback selected(index: int);

    VerticalLayout {
        spacing: 8px;
        for opt[i] in root.options : Rectangle {
            height: 24px;
            opacity: root.enabled ? 1.0 : 0.5;
            HorizontalLayout {
                spacing: 8px;
                Rectangle {
                    width: 22px; height: 22px;
                    background: root.shadow;
                    border-radius: PixelTheme.radius-sm;
                    Rectangle {
                        x: item-ta.pressed ? 1px : 0px;
                        y: item-ta.pressed ? 1px : 0px;
                        width: 22px; height: 22px;
                        background: root.index == i ? root.face-checked : root.face;
                        border-width: PixelTheme.border-width;
                        border-radius: PixelTheme.radius-sm;
                        border-color: root.index == i ? root.face-checked : root.border;
                        Rectangle {
                            visible: root.index == i;
                            x: 7px; y: 7px; width: 8px; height: 8px;
                            background: root.index == i ? PixelTheme.on-accent : root.dot-color;
                        }
                    }
                }
                Text {
                    text: opt;
                    color: root.text-color;
                    vertical-alignment: center;
                    font-size: 13px;
                }
            }
            item-ta := TouchArea {
                enabled: root.enabled;
                width: 100%; height: 100%;
                clicked => {
                    root.index = i;
                    root.selected(i);
                }
                mouse-cursor: pointer;
            }
        }
    }
}

// ===== 下拉选择框 =====
export component PixelComboBox inherits Rectangle {
    in property <[string]> model: [];
    in-out property <int> index: -1;
    in-out property <bool> open: false;
    in property <string> placeholder: "请选择";
    in property <bool> enabled: true;
    in property <color> face: PixelTheme.bg;
    in property <color> border: PixelTheme.border-soft;
    in property <color> border-focus: PixelTheme.accent;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> dim: PixelTheme.dim;
    in property <color> item-face: PixelTheme.bg;
    in property <color> item-hover: PixelTheme.hover;
    in property <color> item-current: PixelTheme.accent;
    in property <color> item-border: PixelTheme.edge;
    callback selected(index: int);
    height: 32px;
    opacity: root.enabled ? 1.0 : 0.5;

    property <bool> has-selection: root.index >= 0 && root.index < root.model.length;

    init => {
        if root.open {
            popup.show();
        }
    }

    Rectangle {
        width: 100%; height: root.height;
        background: transparent;
        border-radius: PixelTheme.radius;
        Rectangle {
            x: 0px; y: 0px;
            width: parent.width; height: parent.height;
            background: root.face;
            border-width: PixelTheme.border-width; // 与输入框一致:统一 2px 主题描边
            border-radius: PixelTheme.radius;
            border-color: popup.is-open ? root.border-focus : root.border;
            Text {
                x: 8px; y: 0px;
                width: parent.width - 36px; height: parent.height;
                text: root.has-selection ? root.model[root.index] : root.placeholder;
                color: root.has-selection ? root.text-color : root.dim;
                font-size: 13px;
                vertical-alignment: center;
            }
            Text {
                x: parent.width - 26px; y: 0px;
                width: 20px; height: parent.height;
                text: popup.is-open ? "▲" : "▼";
                color: root.dim;
                font-size: 11px;
                horizontal-alignment: center;
                vertical-alignment: center;
            }
            ta := TouchArea {
                enabled: root.enabled;
                clicked => {
                    if root.open {
                        popup.close();
                    } else {
                        popup.show();
                    }
                    root.open = !root.open;
                }
                mouse-cursor: pointer;
            }
        }
    }

    popup := PopupWindow {
        x: 0px; y: root.height + 2px;
        width: root.width;
        height: root.model.length * 28px + 10px;
        close-policy: close-on-click-outside;
        PixelPopupList {
            width: 100%; height: 100%;
            items: root.model;
            current: root.index;
            face: root.item-face;
            face-hover: root.item-hover;
            face-current: root.item-current;
            edge: root.item-border;
            shadow: root.shadow;
            text-color: root.text-color;
            selected(i) => {
                root.index = i;
                root.open = false;
                root.selected(i);
                popup.close();
            }
        }
    }
}

// ===== 下拉菜单 =====
export component PixelMenu inherits Rectangle {
    in property <[string]> items: [];
    in-out property <int> index: -1;
    in-out property <bool> open: false;
    in property <string> button-text: "菜单";
    in property <bool> enabled: true;
    in property <color> face: PixelTheme.hover;
    in property <color> border: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> dim: PixelTheme.dim;
    in property <color> item-face: PixelTheme.bg;
    in property <color> item-hover: PixelTheme.hover;
    in property <color> item-current: PixelTheme.accent;
    in property <color> item-border: PixelTheme.edge;
    callback selected(index: int);
    width: 120px;
    height: 32px;
    opacity: root.enabled ? 1.0 : 0.5;

    init => {
        if root.open {
            popup.show();
        }
    }

    Rectangle {
        width: 100%; height: root.height;
        background: transparent;
        border-radius: PixelTheme.radius;
        Rectangle {
            x: 0px; y: 0px;
            width: parent.width; height: parent.height;
            background: root.face;
            border-width: PixelTheme.border-width;
            border-radius: PixelTheme.radius;
            border-color: root.border;
            Text {
                x: 10px; y: 0px;
                width: parent.width - 36px; height: parent.height;
                text: root.button-text;
                color: root.text-color;
                font-weight: 700;
                font-size: 13px;
                vertical-alignment: center;
            }
            Text {
                x: parent.width - 26px; y: 0px;
                width: 20px; height: parent.height;
                text: popup.is-open ? "▲" : "▼";
                color: root.dim;
                font-size: 11px;
                horizontal-alignment: center;
                vertical-alignment: center;
            }
            ta := TouchArea {
                enabled: root.enabled;
                clicked => {
                    if root.open {
                        popup.close();
                    } else {
                        popup.show();
                    }
                    root.open = !root.open;
                }
                mouse-cursor: pointer;
            }
        }
    }

    popup := PopupWindow {
        x: 0px; y: root.height + 2px;
        width: 180px;
        height: root.items.length * 28px + 10px;
        close-policy: close-on-click-outside;
        PixelPopupList {
            width: 100%; height: 100%;
            items: root.items;
            current: root.index;
            face: root.item-face;
            face-hover: root.item-hover;
            face-current: root.item-current;
            edge: root.item-border;
            shadow: root.shadow;
            text-color: root.text-color;
            selected(i) => {
                root.index = i;
                root.open = false;
                root.selected(i);
                popup.close();
            }
        }
    }
}
// ===== 表格 =====
export component PixelTableView inherits Rectangle {
    in property <[string]> headers: [];
    in property <[[string]]> rows: [];
    in-out property <int> selected-row: -1;
    in property <bool> enabled: true;
    in property <length> header-height: 28px;
    in property <length> row-height: 26px;
    in property <color> face: PixelTheme.bg;
    in property <color> header-face: PixelTheme.panel;
    in property <color> row-even: PixelTheme.bg;
    in property <color> row-odd: PixelTheme.bg;
    in property <color> row-selected: PixelTheme.accent;
    in property <color> border: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> dim: PixelTheme.dim;
    callback row-clicked(row: int);
    opacity: root.enabled ? 1.0 : 0.5;

    // 内容总高(表头 + 数据行 + 上下边框),自适应展开,避免在布局里塌缩被 clip
    property <length> body-height: root.header-height + root.row-height * root.rows.length + 4px;
    preferred-height: root.body-height;
    min-height: root.body-height;

    Rectangle {
        width: 100%; height: 100%;
        background: transparent;
        border-radius: PixelTheme.radius;
        Rectangle {
            x: 0px; y: 0px;
            width: parent.width; height: parent.height;
            background: root.face;
            border-width: PixelTheme.border-width;
            border-radius: PixelTheme.radius;
            border-color: root.border;
            clip: true;

            // 表头
            Rectangle {
                x: 0px; y: 0px;
                width: parent.width; height: root.header-height;
                background: root.header-face;
                Rectangle {
                    x: 0px; y: parent.height - PixelTheme.border-width;
                    width: parent.width; height: PixelTheme.border-width;
                    background: root.border;
                }
                HorizontalLayout {
                    padding: 6px;
                    spacing: 4px;
                    for h in root.headers : Text {
                        text: h;
                        color: root.dim;
                        font-weight: 700;
                        font-size: 12px;
                        vertical-alignment: center;
                        horizontal-alignment: center;
                    }
                }
            }

            // 数据行
            VerticalLayout {
                y: root.header-height;
                padding: 0px;
                spacing: 0px;
                for r[i] in root.rows : Rectangle {
                    height: root.row-height;
                    background: root.selected-row == i ? root.row-selected
                        : (Math.mod(i, 2) == 0 ? root.row-even : root.row-odd);
                    border-radius: PixelTheme.radius-sm;
                    HorizontalLayout {
                        padding: 6px;
                        spacing: 4px;
                        for cell in r : Text {
                            text: cell;
                            color: root.selected-row == i ? PixelTheme.on-accent : root.text-color;
                            font-size: 12px;
                            vertical-alignment: center;
                            horizontal-alignment: center;
                        }
                    }
                    row-ta := TouchArea {
                        enabled: root.enabled;
                        width: 100%; height: 100%;
                        clicked => {
                            root.selected-row = i;
                            root.row-clicked(i);
                        }
                        mouse-cursor: pointer;
                    }
                }
            }
        }
    }
}

// ===== 滚动面板(基于 Slint 内置 ScrollView) =====
// 纯 Slint 无法在运行时测量"内容自然高度"(子项总被视口高度约束),
// 因此滚动逻辑复用 Slint 内置 ScrollView(滚轮/拖拽/触控均原生支持);
// 滚动条外观跟随当前活动样式(fluent 等),边框/底色按像素主题着色。
import { ScrollView } from "std-widgets.slint";

export component PixelScrollPanel inherits Rectangle {
    in property <color> panel-bg: PixelTheme.bg;
    in property <color> edge: PixelTheme.edge;
    in property <length> initial-scroll-y: 0px;
    background: root.panel-bg;
    border-width: PixelTheme.border-width;
    border-radius: PixelTheme.radius;
    border-color: root.edge;
    clip: true;

    ScrollView {
        x: 2px; y: 2px;
        width: parent.width - 4px;
        height: parent.height - 4px;
        // Slint 1.18 起 Flickable/ScrollView 的 viewport-* 更名为 content-*(旧名仅作 deprecated 别名)
        content-y: -root.initial-scroll-y;
        // 内容直接放入 ScrollView(fluent 同款:ScrollView 自身包 Flickable)
        @children
    }
}

// ===== 可折叠面板 =====
export component PixelCollapsible inherits VerticalLayout {
    in property <string> title: "";
    in-out property <bool> expanded: false;
    in property <bool> enabled: true;
    in property <color> header-face: PixelTheme.panel;
    in property <color> header-hover: PixelTheme.hover;
    in property <color> body: PixelTheme.bg;
    in property <color> edge: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> highlight: PixelTheme.accent;
    callback toggled(expanded: bool);
    opacity: root.enabled ? 1.0 : 0.5;
    spacing: 0px;

    // 标题栏与内容区共享一个外框(避免两框相贴出 4px 缝);框内标题下是 2px 分隔线
    Rectangle {
        width: 100%;
        height: 32px + cl-body.height;
        border-width: PixelTheme.border-width;
        border-radius: PixelTheme.radius;
        border-color: root.edge;
        background: root.header-face;
        clip: true;

        // 标题栏
        Rectangle {
            width: 100%;
            height: 32px;
            background: header-ta.has-hover ? root.header-hover : transparent;
            Text {
                x: 10px; y: 0px;
                width: parent.width - 40px; height: parent.height;
                text: root.title;
                color: root.text-color;
                font-weight: 700;
                font-size: 13px;
                vertical-alignment: center;
            }
            Text {
                x: parent.width - 30px; y: 0px;
                width: 24px; height: parent.height;
                text: root.expanded ? "▼" : "▶";
                color: root.text-color;
                font-size: 11px;
                horizontal-alignment: center;
                vertical-alignment: center;
            }
            // 标题下分隔线(常显 2px:折叠态作框内分隔,展开态分隔内容区)
            Rectangle {
                x: 0px; y: parent.height - PixelTheme.border-width;
                width: parent.width; height: PixelTheme.border-width;
                background: root.edge;
            }
            header-ta := TouchArea {
                enabled: root.enabled;
                width: 100%; height: 100%;
                clicked => {
                    root.expanded = !root.expanded;
                    root.toggled(root.expanded);
                }
                mouse-cursor: pointer;
            }
        }

        // 内容区(收起时高度 0 + 裁剪,带展开动画)
        cl-body := Rectangle {
            y: 32px;
            width: 100%;
            height: root.expanded ? max(body-content.preferred-height, 40px) : 0px;
            background: root.body;
            clip: true;
            animate height { duration: 160ms; easing: ease-out; }
            body-content := VerticalLayout {
                x: 0px;
                width: 100%;
                @children
            }
        }
    }
}

// ===== 手风琴(互斥展开,数据驱动) =====
export component PixelAccordion inherits VerticalLayout {
    in property <[string]> titles: [];
    in property <[string]> contents: [];
    in-out property <int> active: -1;
    in property <bool> enabled: true;
    in property <color> header-face: PixelTheme.panel;
    in property <color> header-hover: PixelTheme.hover;
    in property <color> body: PixelTheme.bg;
    in property <color> edge: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> highlight: PixelTheme.accent;
    callback toggled(index: int);
    opacity: root.enabled ? 1.0 : 0.5;
    spacing: 4px;

    for t[i] in root.titles : Rectangle {
        width: 100%;
        // 标题栏与内容区共享一个外框(避免两框相贴出 4px 缝);框内标题下是 2px 分隔线
        height: 32px + acc-body.height;
        border-width: PixelTheme.border-width;
        border-radius: PixelTheme.radius;
        border-color: root.edge;
        background: root.header-face;
        clip: true;

        // 标题栏
        Rectangle {
            width: 100%;
            height: 32px;
            background: panel-ta.has-hover ? root.header-hover : transparent;
            Text {
                x: 10px; y: 0px;
                width: parent.width - 40px; height: parent.height;
                text: t;
                color: root.text-color;
                font-weight: 700;
                font-size: 13px;
                vertical-alignment: center;
            }
            Text {
                x: parent.width - 30px; y: 0px;
                width: 24px; height: parent.height;
                text: root.active == i ? "▼" : "▶";
                color: root.text-color;
                font-size: 11px;
                horizontal-alignment: center;
                vertical-alignment: center;
            }
            // 标题下分隔线(常显 2px)
            Rectangle {
                x: 0px; y: parent.height - PixelTheme.border-width;
                width: parent.width; height: PixelTheme.border-width;
                background: root.edge;
            }
            panel-ta := TouchArea {
                enabled: root.enabled;
                width: 100%; height: 100%;
                clicked => {
                    root.active = root.active == i ? -1 : i;
                    root.toggled(root.active);
                }
                mouse-cursor: pointer;
            }
        }

        // 内容区
        acc-body := Rectangle {
            y: 32px;
            width: 100%;
            height: root.active == i ? max(acc-body-content.preferred-height, 40px) : 0px;
            background: root.body;
            clip: true;
            animate height { duration: 160ms; easing: ease-out; }
            acc-body-content := VerticalLayout {
                x: 0px;
                width: 100%;
                Text {
                    x: 10px;
                    width: parent.width - 20px;
                    text: root.contents[i];
                    color: root.text-color;
                    font-size: 13px;
                    wrap: word-wrap;
                }
            }
        }
    }
}

// ===== 侧边栏 =====
export component PixelSidebar inherits Rectangle {
    in property <[string]> items: [];
    in-out property <int> active: 0;
    in property <string> title: "";
    in property <bool> enabled: true;
    in property <length> sidebar-width: 170px;
    in property <color> sidebar-bg: PixelTheme.bg;
    in property <color> item-hover: PixelTheme.hover;
    in property <color> item-active: PixelTheme.accent;
    in property <color> content-bg: PixelTheme.bg;
    in property <color> edge: PixelTheme.edge;
    in property <color> shadow: PixelTheme.shadow;
    in property <color> text-color: PixelTheme.text;
    in property <color> dim: PixelTheme.dim;
    in property <color> highlight: PixelTheme.accent;
    callback selected(index: int);
    opacity: root.enabled ? 1.0 : 0.5;

    HorizontalLayout {
        spacing: 0px;

        // 侧栏
        Rectangle {
            width: root.sidebar-width;
            background: root.sidebar-bg;
            Rectangle {
                x: parent.width - PixelTheme.border-width; y: 0px;
                width: PixelTheme.border-width; height: parent.height;
                background: root.shadow;
                border-radius: PixelTheme.radius;
            }
            VerticalLayout {
                padding: 10px;
                spacing: 4px;
                Text {
                    text: root.title;
                    color: root.text-color;
                    font-weight: 800;
                    letter-spacing: 1px;
                    font-size: 13px;
                }
                Rectangle { height: PixelTheme.border-width; background: root.edge; }
                for item[i] in root.items : Rectangle {
                    height: 32px;
                    background: root.active == i ? root.item-active
                        : (item-ta.has-hover ? root.item-hover : transparent);
                    border-radius: PixelTheme.radius-sm;
                    // 选中指示条
                    Rectangle {
                        visible: root.active == i;
                        x: 0px; y: 0px;
                        width: PixelTheme.border-width; height: 32px;
                        background: root.highlight;
                        border-radius: PixelTheme.radius-sm;
                    }
                    Text {
                        x: 12px; y: 0px;
                        width: parent.width - 20px; height: parent.height;
                        text: item;
                        color: root.active == i ? PixelTheme.on-accent : root.text-color;
                        font-size: 13px;
                        vertical-alignment: center;
                    }
                    item-ta := TouchArea {
                        enabled: root.enabled;
                        width: 100%; height: 100%;
                        clicked => {
                            root.active = i;
                            root.selected(i);
                        }
                        mouse-cursor: pointer;
                    }
                }
            }
        }

        // 内容区
        Rectangle {
            background: root.content-bg;
            @children
        }
    }
}