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
}
}
}