simpleui-py 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simpleui/__init__.py +73 -0
- simpleui/components/__init__.py +28 -0
- simpleui/components/alert.py +168 -0
- simpleui/components/button.py +259 -0
- simpleui/components/card.py +130 -0
- simpleui/components/input.py +264 -0
- simpleui/components/layout.py +174 -0
- simpleui/components/progress.py +168 -0
- simpleui/components/switch.py +91 -0
- simpleui/components/tag.py +121 -0
- simpleui/core.py +1055 -0
- simpleui/py.typed +1 -0
- simpleui/utils.py +184 -0
- simpleui_py-1.0.0.dist-info/METADATA +54 -0
- simpleui_py-1.0.0.dist-info/RECORD +19 -0
- simpleui_py-1.0.0.dist-info/WHEEL +5 -0
- simpleui_py-1.0.0.dist-info/entry_points.txt +2 -0
- simpleui_py-1.0.0.dist-info/licenses/LICENSE +21 -0
- simpleui_py-1.0.0.dist-info/top_level.txt +1 -0
simpleui/__init__.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SimpleUI - Fluent 风格 Python UI 组件库
|
|
3
|
+
简约设计,无限可能
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
__version__ = "1.0.0"
|
|
7
|
+
__author__ = "SimpleUI Team"
|
|
8
|
+
|
|
9
|
+
# 从 core 导入核心功能
|
|
10
|
+
from .core import (
|
|
11
|
+
Component,
|
|
12
|
+
Theme,
|
|
13
|
+
ThemeType,
|
|
14
|
+
render_html,
|
|
15
|
+
export_html,
|
|
16
|
+
set_theme,
|
|
17
|
+
get_theme,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
# 从 components 导入所有组件
|
|
21
|
+
from .components import (
|
|
22
|
+
Button,
|
|
23
|
+
ButtonGroup,
|
|
24
|
+
Input,
|
|
25
|
+
TextArea,
|
|
26
|
+
Card,
|
|
27
|
+
Switch,
|
|
28
|
+
Tag,
|
|
29
|
+
Progress,
|
|
30
|
+
Alert,
|
|
31
|
+
Container,
|
|
32
|
+
Row,
|
|
33
|
+
Column,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
# 从 utils 导入工具函数
|
|
37
|
+
from .utils import preview, serve, demo, playground
|
|
38
|
+
|
|
39
|
+
# 导出列表
|
|
40
|
+
__all__ = [
|
|
41
|
+
# 版本信息
|
|
42
|
+
"__version__",
|
|
43
|
+
"__author__",
|
|
44
|
+
|
|
45
|
+
# 核心类
|
|
46
|
+
"Component",
|
|
47
|
+
"Theme",
|
|
48
|
+
"ThemeType",
|
|
49
|
+
"render_html",
|
|
50
|
+
"export_html",
|
|
51
|
+
"set_theme",
|
|
52
|
+
"get_theme",
|
|
53
|
+
|
|
54
|
+
# 组件
|
|
55
|
+
"Button",
|
|
56
|
+
"ButtonGroup",
|
|
57
|
+
"Input",
|
|
58
|
+
"TextArea",
|
|
59
|
+
"Card",
|
|
60
|
+
"Switch",
|
|
61
|
+
"Tag",
|
|
62
|
+
"Progress",
|
|
63
|
+
"Alert",
|
|
64
|
+
"Container",
|
|
65
|
+
"Row",
|
|
66
|
+
"Column",
|
|
67
|
+
|
|
68
|
+
# 工具函数
|
|
69
|
+
"preview",
|
|
70
|
+
"serve",
|
|
71
|
+
"demo",
|
|
72
|
+
"playground",
|
|
73
|
+
]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SimpleUI 组件模块
|
|
3
|
+
导出所有 UI 组件
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .button import Button, ButtonGroup
|
|
7
|
+
from .input import Input, TextArea
|
|
8
|
+
from .card import Card
|
|
9
|
+
from .switch import Switch
|
|
10
|
+
from .tag import Tag
|
|
11
|
+
from .progress import Progress
|
|
12
|
+
from .alert import Alert
|
|
13
|
+
from .layout import Container, Row, Column
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"Button",
|
|
17
|
+
"ButtonGroup",
|
|
18
|
+
"Input",
|
|
19
|
+
"TextArea",
|
|
20
|
+
"Card",
|
|
21
|
+
"Switch",
|
|
22
|
+
"Tag",
|
|
23
|
+
"Progress",
|
|
24
|
+
"Alert",
|
|
25
|
+
"Container",
|
|
26
|
+
"Row",
|
|
27
|
+
"Column",
|
|
28
|
+
]
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""
|
|
2
|
+
进度条组件
|
|
3
|
+
Fluent 风格进度条,支持多种状态
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Optional, Literal
|
|
7
|
+
from ..core import Component
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Progress(Component):
|
|
11
|
+
"""
|
|
12
|
+
Fluent 风格进度条组件
|
|
13
|
+
|
|
14
|
+
支持线性进度条和圆形进度条,支持多种状态颜色。
|
|
15
|
+
|
|
16
|
+
参数:
|
|
17
|
+
value: 当前进度值 (0-100)
|
|
18
|
+
max_value: 最大值
|
|
19
|
+
label: 标签文本
|
|
20
|
+
show_value: 是否显示数值
|
|
21
|
+
status: 状态颜色
|
|
22
|
+
striped: 是否显示条纹
|
|
23
|
+
height: 高度
|
|
24
|
+
|
|
25
|
+
示例:
|
|
26
|
+
Progress(value=75, label="下载进度")
|
|
27
|
+
Progress(value=100, status="success")
|
|
28
|
+
Progress.circle(value=70)
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
# 状态对应的CSS类
|
|
32
|
+
STATUS_CLASSES = {
|
|
33
|
+
"default": "",
|
|
34
|
+
"success": "sui-progress-success",
|
|
35
|
+
"warning": "sui-progress-warning",
|
|
36
|
+
"danger": "sui-progress-danger",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
def __init__(
|
|
40
|
+
self,
|
|
41
|
+
value: float = 0,
|
|
42
|
+
max_value: float = 100,
|
|
43
|
+
label: Optional[str] = None,
|
|
44
|
+
show_value: bool = True,
|
|
45
|
+
status: Literal["default", "success", "warning", "danger"] = "default",
|
|
46
|
+
striped: bool = False,
|
|
47
|
+
animated: bool = False,
|
|
48
|
+
height: str = "8px",
|
|
49
|
+
**kwargs
|
|
50
|
+
):
|
|
51
|
+
super().__init__(**kwargs)
|
|
52
|
+
self._max_value = max_value
|
|
53
|
+
self.value = max(0, min(value, self._max_value))
|
|
54
|
+
self.label = label
|
|
55
|
+
self.show_value = show_value
|
|
56
|
+
self.status = status
|
|
57
|
+
self.striped = striped
|
|
58
|
+
self.animated = animated
|
|
59
|
+
self.height = height
|
|
60
|
+
|
|
61
|
+
def _get_progress_classes(self) -> str:
|
|
62
|
+
"""获取进度条CSS类"""
|
|
63
|
+
classes = ["sui-progress"]
|
|
64
|
+
|
|
65
|
+
if self.status in self.STATUS_CLASSES:
|
|
66
|
+
classes.append(self.STATUS_CLASSES[self.status])
|
|
67
|
+
|
|
68
|
+
return " ".join(classes)
|
|
69
|
+
|
|
70
|
+
def _get_percentage(self) -> float:
|
|
71
|
+
"""计算百分比"""
|
|
72
|
+
if self._max_value == 0:
|
|
73
|
+
return 0
|
|
74
|
+
return (self.value / self._max_value) * 100
|
|
75
|
+
|
|
76
|
+
def render(self) -> str:
|
|
77
|
+
"""渲染进度条"""
|
|
78
|
+
html_parts = ['<div class="sui-progress-wrapper">']
|
|
79
|
+
|
|
80
|
+
# 标签和数值
|
|
81
|
+
if self.label or self.show_value:
|
|
82
|
+
html_parts.append('<div class="sui-progress-label">')
|
|
83
|
+
|
|
84
|
+
if self.label:
|
|
85
|
+
html_parts.append(f'<span>{self.label}</span>')
|
|
86
|
+
else:
|
|
87
|
+
html_parts.append('<span></span>')
|
|
88
|
+
|
|
89
|
+
if self.show_value:
|
|
90
|
+
html_parts.append(f'<span>{round(self._get_percentage())}%</span>')
|
|
91
|
+
|
|
92
|
+
html_parts.append('</div>')
|
|
93
|
+
|
|
94
|
+
# 进度条
|
|
95
|
+
percentage = self._get_percentage()
|
|
96
|
+
html_parts.append(f'<div class="{self._get_progress_classes()}" style="height:{self.height}">')
|
|
97
|
+
|
|
98
|
+
bar_style = f"width:{percentage}%"
|
|
99
|
+
if self.striped:
|
|
100
|
+
bar_style += ";background-image: linear-gradient(45deg, rgba(255,255,255,0.15) 25%, transparent 25%, transparent 50%, rgba(255,255,255,0.15) 50%, rgba(255,255,255,0.15) 75%, transparent 75%, transparent);background-size: 1rem 1rem"
|
|
101
|
+
|
|
102
|
+
html_parts.append(f'<div class="sui-progress-bar" style="{bar_style}"></div>')
|
|
103
|
+
html_parts.append('</div>')
|
|
104
|
+
|
|
105
|
+
html_parts.append('</div>')
|
|
106
|
+
|
|
107
|
+
return "".join(html_parts)
|
|
108
|
+
|
|
109
|
+
def set_value(self, value: float) -> "Progress":
|
|
110
|
+
"""设置当前值"""
|
|
111
|
+
self.value = max(0, min(value, self._max_value))
|
|
112
|
+
return self
|
|
113
|
+
|
|
114
|
+
def set_status(self, status: Literal["default", "success", "warning", "danger"]) -> "Progress":
|
|
115
|
+
"""设置状态"""
|
|
116
|
+
self.status = status
|
|
117
|
+
return self
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def circle(
|
|
121
|
+
value: float = 0,
|
|
122
|
+
max_value: float = 100,
|
|
123
|
+
size: int = 80,
|
|
124
|
+
stroke_width: int = 6,
|
|
125
|
+
status: Literal["default", "success", "warning", "danger"] = "default",
|
|
126
|
+
) -> str:
|
|
127
|
+
"""
|
|
128
|
+
创建圆形进度条
|
|
129
|
+
|
|
130
|
+
参数:
|
|
131
|
+
value: 当前进度值
|
|
132
|
+
max_value: 最大值
|
|
133
|
+
size: 尺寸
|
|
134
|
+
stroke_width: 线条宽度
|
|
135
|
+
status: 状态
|
|
136
|
+
|
|
137
|
+
返回:
|
|
138
|
+
HTML字符串
|
|
139
|
+
"""
|
|
140
|
+
percentage = (value / max_value) * 100 if max_value > 0 else 0
|
|
141
|
+
radius = (size - stroke_width) / 2
|
|
142
|
+
circumference = 2 * 3.14159 * radius
|
|
143
|
+
stroke_dashoffset = circumference * (1 - percentage / 100)
|
|
144
|
+
|
|
145
|
+
colors = {
|
|
146
|
+
"default": "url(#progress-gradient)",
|
|
147
|
+
"success": "#43e97b",
|
|
148
|
+
"warning": "#fbbf24",
|
|
149
|
+
"danger": "#ef4444",
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
color = colors.get(status, colors["default"])
|
|
153
|
+
|
|
154
|
+
return f'''
|
|
155
|
+
<div class="sui-progress-circle" style="width:{size}px;height:{size}px;position:relative">
|
|
156
|
+
<svg width="{size}" height="{size}" style="transform:rotate(-90deg)">
|
|
157
|
+
<defs>
|
|
158
|
+
<linearGradient id="progress-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
|
|
159
|
+
<stop offset="0%" stop-color="#667eea"/>
|
|
160
|
+
<stop offset="100%" stop-color="#764ba2"/>
|
|
161
|
+
</linearGradient>
|
|
162
|
+
</defs>
|
|
163
|
+
<circle cx="{size/2}" cy="{size/2}" r="{radius}" fill="none" stroke="var(--bg-card)" stroke-width="{stroke_width}"/>
|
|
164
|
+
<circle cx="{size/2}" cy="{size/2}" r="{radius}" fill="none" stroke="{color}" stroke-width="{stroke_width}" stroke-linecap="round" stroke-dasharray="{circumference}" stroke-dashoffset="{stroke_dashoffset}" style="transition:stroke-dashoffset 0.5s ease"/>
|
|
165
|
+
</svg>
|
|
166
|
+
<span style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;font-size:1rem;font-weight:700;color:var(--text-primary)">{round(percentage)}%</span>
|
|
167
|
+
</div>
|
|
168
|
+
'''
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""
|
|
2
|
+
按钮组件
|
|
3
|
+
Fluent 风格按钮,支持多种变体和尺寸
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Optional, Literal, Union
|
|
7
|
+
from ..core import Component
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Button(Component):
|
|
11
|
+
"""
|
|
12
|
+
Fluent 风格按钮组件
|
|
13
|
+
|
|
14
|
+
支持6种变体:primary, secondary, outline, ghost, danger, success
|
|
15
|
+
支持3种尺寸:sm, md, lg
|
|
16
|
+
支持图标、加载状态、禁用状态
|
|
17
|
+
|
|
18
|
+
参数:
|
|
19
|
+
text: 按钮文本
|
|
20
|
+
variant: 按钮变体
|
|
21
|
+
size: 按钮尺寸
|
|
22
|
+
icon: 图标SVG路径
|
|
23
|
+
icon_position: 图标位置
|
|
24
|
+
loading: 是否显示加载状态
|
|
25
|
+
disabled: 是否禁用
|
|
26
|
+
href: 链接地址(设置后渲染为<a>标签)
|
|
27
|
+
onclick: 点击事件处理函数
|
|
28
|
+
|
|
29
|
+
示例:
|
|
30
|
+
Button("点击我", variant="primary")
|
|
31
|
+
Button("删除", variant="danger", icon=Button.ICONS["delete"])
|
|
32
|
+
Button.icon_button("plus")
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
# 变体对应的CSS类
|
|
36
|
+
VARIANT_CLASSES = {
|
|
37
|
+
"primary": "sui-btn-primary",
|
|
38
|
+
"secondary": "sui-btn-secondary",
|
|
39
|
+
"outline": "sui-btn-outline",
|
|
40
|
+
"ghost": "sui-btn-ghost",
|
|
41
|
+
"danger": "sui-btn-danger",
|
|
42
|
+
"success": "sui-btn-success",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
# 尺寸对应的CSS类
|
|
46
|
+
SIZE_CLASSES = {
|
|
47
|
+
"sm": "sui-btn-sm",
|
|
48
|
+
"md": "",
|
|
49
|
+
"lg": "sui-btn-lg",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
# 预设图标
|
|
53
|
+
ICONS = {
|
|
54
|
+
"plus": '<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>',
|
|
55
|
+
"minus": '<path d="M19 13H5v-2h14v2z"/>',
|
|
56
|
+
"download": '<path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/>',
|
|
57
|
+
"upload": '<path d="M9 16h6v-6h4l-7-7-7 7h4zm-4 2h14v2H5z"/>',
|
|
58
|
+
"check": '<path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>',
|
|
59
|
+
"close": '<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>',
|
|
60
|
+
"edit": '<path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/>',
|
|
61
|
+
"delete": '<path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/>',
|
|
62
|
+
"search": '<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>',
|
|
63
|
+
"settings": '<path d="M19.14 12.94c.04-.31.06-.63.06-.94 0-.31-.02-.63-.06-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.04.31-.06.63-.06.94s.02.63.06.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z"/>',
|
|
64
|
+
"refresh": '<path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>',
|
|
65
|
+
"arrow_right": '<path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z"/>',
|
|
66
|
+
"arrow_left": '<path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"/>',
|
|
67
|
+
"play": '<path d="M8 5v14l11-7z"/>',
|
|
68
|
+
"pause": '<path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/>',
|
|
69
|
+
"mail": '<path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z"/>',
|
|
70
|
+
"user": '<path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>',
|
|
71
|
+
"heart": '<path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>',
|
|
72
|
+
"star": '<path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/>',
|
|
73
|
+
"home": '<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>',
|
|
74
|
+
"folder": '<path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"/>',
|
|
75
|
+
"file": '<path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 16H8v-2h8v2zm0-4H8v-2h8v2zm-3-5V3.5L18.5 9H13z"/>',
|
|
76
|
+
"copy": '<path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/>',
|
|
77
|
+
"link": '<path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>',
|
|
78
|
+
"external": '<path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"/>',
|
|
79
|
+
"info": '<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/>',
|
|
80
|
+
"warning": '<path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z"/>',
|
|
81
|
+
"error": '<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>',
|
|
82
|
+
"help": '<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z"/>',
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
text: Optional[str] = None,
|
|
88
|
+
variant: Literal["primary", "secondary", "outline", "ghost", "danger", "success"] = "primary",
|
|
89
|
+
size: Literal["sm", "md", "lg"] = "md",
|
|
90
|
+
icon: Optional[str] = None,
|
|
91
|
+
icon_position: Literal["left", "right"] = "left",
|
|
92
|
+
loading: bool = False,
|
|
93
|
+
disabled: bool = False,
|
|
94
|
+
href: Optional[str] = None,
|
|
95
|
+
onclick: Optional[str] = None,
|
|
96
|
+
**kwargs
|
|
97
|
+
):
|
|
98
|
+
"""
|
|
99
|
+
初始化按钮
|
|
100
|
+
|
|
101
|
+
参数:
|
|
102
|
+
text: 按钮文本
|
|
103
|
+
variant: 变体
|
|
104
|
+
size: 尺寸
|
|
105
|
+
icon: 图标SVG路径
|
|
106
|
+
icon_position: 图标位置
|
|
107
|
+
loading: 加载状态
|
|
108
|
+
disabled: 禁用状态
|
|
109
|
+
href: 链接地址
|
|
110
|
+
onclick: 点击事件
|
|
111
|
+
"""
|
|
112
|
+
super().__init__(**kwargs)
|
|
113
|
+
self.text = text
|
|
114
|
+
self.variant = variant
|
|
115
|
+
self.size = size
|
|
116
|
+
self.icon = icon
|
|
117
|
+
self.icon_position = icon_position
|
|
118
|
+
self.loading = loading
|
|
119
|
+
self.disabled = disabled
|
|
120
|
+
self.href = href
|
|
121
|
+
self.onclick = onclick
|
|
122
|
+
|
|
123
|
+
def _get_base_classes(self) -> str:
|
|
124
|
+
"""获取CSS类名"""
|
|
125
|
+
classes = ["sui-btn"]
|
|
126
|
+
|
|
127
|
+
# 变体类
|
|
128
|
+
if self.variant in self.VARIANT_CLASSES:
|
|
129
|
+
classes.append(self.VARIANT_CLASSES[self.variant])
|
|
130
|
+
|
|
131
|
+
# 尺寸类
|
|
132
|
+
if self.size in self.SIZE_CLASSES:
|
|
133
|
+
classes.append(self.SIZE_CLASSES[self.size])
|
|
134
|
+
|
|
135
|
+
# 加载状态
|
|
136
|
+
if self.loading:
|
|
137
|
+
classes.append("sui-btn-loading")
|
|
138
|
+
|
|
139
|
+
# 用户自定义类
|
|
140
|
+
if self.class_name:
|
|
141
|
+
classes.append(self.class_name)
|
|
142
|
+
|
|
143
|
+
return " ".join(classes)
|
|
144
|
+
|
|
145
|
+
def _get_attributes_str(self) -> str:
|
|
146
|
+
"""获取属性字符串"""
|
|
147
|
+
attrs = super()._get_attributes_str()
|
|
148
|
+
|
|
149
|
+
# 禁用状态
|
|
150
|
+
if self.disabled or self.loading:
|
|
151
|
+
attrs += " disabled"
|
|
152
|
+
|
|
153
|
+
# 点击事件
|
|
154
|
+
if self.onclick and not self.disabled:
|
|
155
|
+
attrs += f' onclick="{self.onclick}"'
|
|
156
|
+
|
|
157
|
+
return attrs
|
|
158
|
+
|
|
159
|
+
def _render_icon(self) -> str:
|
|
160
|
+
"""渲染图标"""
|
|
161
|
+
if not self.icon:
|
|
162
|
+
return ""
|
|
163
|
+
|
|
164
|
+
icon_html = f'<svg viewBox="0 0 24 24" fill="currentColor" width="16" height="16">{self.icon}</svg>'
|
|
165
|
+
|
|
166
|
+
if self.icon_position == "right":
|
|
167
|
+
return f'<span>{self.text or ""}</span>{icon_html}'
|
|
168
|
+
else:
|
|
169
|
+
return f'{icon_html}<span>{self.text or ""}</span>'
|
|
170
|
+
|
|
171
|
+
def render(self) -> str:
|
|
172
|
+
"""渲染按钮为HTML"""
|
|
173
|
+
# 内容
|
|
174
|
+
if self.icon:
|
|
175
|
+
content = self._render_icon()
|
|
176
|
+
else:
|
|
177
|
+
content = self.text or ""
|
|
178
|
+
|
|
179
|
+
# 子组件
|
|
180
|
+
content += self._render_children()
|
|
181
|
+
|
|
182
|
+
# 渲染为链接或按钮
|
|
183
|
+
if self.href:
|
|
184
|
+
return f'<a href="{self.href}" {self._get_attributes_str()}>{content}</a>'
|
|
185
|
+
|
|
186
|
+
return f'<button {self._get_attributes_str()}>{content}</button>'
|
|
187
|
+
|
|
188
|
+
@classmethod
|
|
189
|
+
def icon_button(
|
|
190
|
+
cls,
|
|
191
|
+
icon: str,
|
|
192
|
+
variant: str = "primary",
|
|
193
|
+
size: str = "md",
|
|
194
|
+
**kwargs
|
|
195
|
+
) -> "Button":
|
|
196
|
+
"""
|
|
197
|
+
创建图标按钮
|
|
198
|
+
|
|
199
|
+
参数:
|
|
200
|
+
icon: 图标名称或SVG路径
|
|
201
|
+
variant: 变体
|
|
202
|
+
size: 尺寸
|
|
203
|
+
|
|
204
|
+
返回:
|
|
205
|
+
Button实例
|
|
206
|
+
|
|
207
|
+
示例:
|
|
208
|
+
Button.icon_button("plus")
|
|
209
|
+
Button.icon_button("search", variant="secondary")
|
|
210
|
+
"""
|
|
211
|
+
# 获取图标SVG
|
|
212
|
+
icon_svg = cls.ICONS.get(icon, icon)
|
|
213
|
+
|
|
214
|
+
return cls(
|
|
215
|
+
variant=variant,
|
|
216
|
+
size=size,
|
|
217
|
+
icon=icon_svg,
|
|
218
|
+
class_name="sui-btn-icon " + kwargs.pop("class_name", ""),
|
|
219
|
+
**kwargs
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class ButtonGroup(Component):
|
|
224
|
+
"""
|
|
225
|
+
按钮组组件
|
|
226
|
+
|
|
227
|
+
将多个按钮组合在一起显示。
|
|
228
|
+
|
|
229
|
+
参数:
|
|
230
|
+
buttons: 按钮列表
|
|
231
|
+
|
|
232
|
+
示例:
|
|
233
|
+
group = ButtonGroup([
|
|
234
|
+
Button("左", variant="primary"),
|
|
235
|
+
Button("中", variant="primary"),
|
|
236
|
+
Button("右", variant="primary"),
|
|
237
|
+
])
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
def __init__(
|
|
241
|
+
self,
|
|
242
|
+
buttons: Optional[list] = None,
|
|
243
|
+
**kwargs
|
|
244
|
+
):
|
|
245
|
+
super().__init__(**kwargs)
|
|
246
|
+
if buttons:
|
|
247
|
+
for btn in buttons:
|
|
248
|
+
self.add_child(btn)
|
|
249
|
+
|
|
250
|
+
def _get_base_classes(self) -> str:
|
|
251
|
+
return f"sui-btn-group {self.class_name}".strip()
|
|
252
|
+
|
|
253
|
+
def add_button(self, button: Button) -> "ButtonGroup":
|
|
254
|
+
"""添加按钮"""
|
|
255
|
+
self.add_child(button)
|
|
256
|
+
return self
|
|
257
|
+
|
|
258
|
+
def render(self) -> str:
|
|
259
|
+
return f'<div {self._get_attributes_str()}>{self._render_children()}</div>'
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""
|
|
2
|
+
卡片组件
|
|
3
|
+
Fluent 风格卡片,支持图片和多种变体
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Optional, Literal, Union
|
|
7
|
+
from ..core import Component
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Card(Component):
|
|
11
|
+
"""
|
|
12
|
+
Fluent 风格卡片组件
|
|
13
|
+
|
|
14
|
+
支持图片、多种变体、交互效果。
|
|
15
|
+
|
|
16
|
+
参数:
|
|
17
|
+
title: 标题
|
|
18
|
+
text: 描述文本
|
|
19
|
+
image: 图片URL或背景色
|
|
20
|
+
image_height: 图片高度
|
|
21
|
+
variant: 变体
|
|
22
|
+
footer: 底部内容
|
|
23
|
+
onclick: 点击事件
|
|
24
|
+
|
|
25
|
+
示例:
|
|
26
|
+
Card(title="标题", text="描述")
|
|
27
|
+
Card(title="标题", image="var(--gradient-1)")
|
|
28
|
+
Card(title="标题", variant="elevated")
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
# 变体对应的CSS类
|
|
32
|
+
VARIANT_CLASSES = {
|
|
33
|
+
"default": "",
|
|
34
|
+
"elevated": "sui-card-elevated",
|
|
35
|
+
"outlined": "sui-card-outlined",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
title: Optional[str] = None,
|
|
41
|
+
text: Optional[str] = None,
|
|
42
|
+
image: Optional[str] = None,
|
|
43
|
+
image_height: str = "120px",
|
|
44
|
+
variant: Literal["default", "elevated", "outlined"] = "default",
|
|
45
|
+
footer: Optional[Union[str, Component]] = None,
|
|
46
|
+
onclick: Optional[str] = None,
|
|
47
|
+
**kwargs
|
|
48
|
+
):
|
|
49
|
+
super().__init__(**kwargs)
|
|
50
|
+
self.title = title
|
|
51
|
+
self.text = text
|
|
52
|
+
self.image = image
|
|
53
|
+
self.image_height = image_height
|
|
54
|
+
self.variant = variant
|
|
55
|
+
self.footer = footer
|
|
56
|
+
self.onclick = onclick
|
|
57
|
+
|
|
58
|
+
def _get_base_classes(self) -> str:
|
|
59
|
+
"""获取CSS类名"""
|
|
60
|
+
classes = ["sui-card"]
|
|
61
|
+
|
|
62
|
+
if self.variant in self.VARIANT_CLASSES:
|
|
63
|
+
classes.append(self.VARIANT_CLASSES[self.variant])
|
|
64
|
+
|
|
65
|
+
if self.onclick:
|
|
66
|
+
classes.append("sui-card-interactive")
|
|
67
|
+
|
|
68
|
+
if self.class_name:
|
|
69
|
+
classes.append(self.class_name)
|
|
70
|
+
|
|
71
|
+
return " ".join(classes)
|
|
72
|
+
|
|
73
|
+
def _get_attributes_str(self) -> str:
|
|
74
|
+
"""获取属性字符串"""
|
|
75
|
+
attrs = super()._get_attributes_str()
|
|
76
|
+
|
|
77
|
+
if self.onclick:
|
|
78
|
+
attrs += f' onclick="{self.onclick}"'
|
|
79
|
+
|
|
80
|
+
return attrs
|
|
81
|
+
|
|
82
|
+
def render(self) -> str:
|
|
83
|
+
"""渲染卡片"""
|
|
84
|
+
html_parts = [f'<div {self._get_attributes_str()}>']
|
|
85
|
+
|
|
86
|
+
# 图片
|
|
87
|
+
if self.image:
|
|
88
|
+
if self.image.startswith("http") or self.image.startswith("/"):
|
|
89
|
+
html_parts.append(f'<img class="sui-card-img" src="{self.image}" alt="{self.title or ""}">')
|
|
90
|
+
else:
|
|
91
|
+
html_parts.append(f'<div class="sui-card-img" style="background:{self.image};height:{self.image_height}"></div>')
|
|
92
|
+
|
|
93
|
+
# 内容
|
|
94
|
+
html_parts.append('<div class="sui-card-body">')
|
|
95
|
+
|
|
96
|
+
if self.title:
|
|
97
|
+
html_parts.append(f'<h4 class="sui-card-title">{self.title}</h4>')
|
|
98
|
+
|
|
99
|
+
if self.text:
|
|
100
|
+
html_parts.append(f'<p class="sui-card-text">{self.text}</p>')
|
|
101
|
+
|
|
102
|
+
# 子组件
|
|
103
|
+
html_parts.append(self._render_children())
|
|
104
|
+
|
|
105
|
+
html_parts.append('</div>')
|
|
106
|
+
|
|
107
|
+
# 底部
|
|
108
|
+
if self.footer:
|
|
109
|
+
html_parts.append('<div class="sui-card-footer">')
|
|
110
|
+
if isinstance(self.footer, Component):
|
|
111
|
+
html_parts.append(self.footer.render())
|
|
112
|
+
else:
|
|
113
|
+
html_parts.append(str(self.footer))
|
|
114
|
+
html_parts.append('</div>')
|
|
115
|
+
|
|
116
|
+
html_parts.append('</div>')
|
|
117
|
+
|
|
118
|
+
return "".join(html_parts)
|
|
119
|
+
|
|
120
|
+
def set_image(self, image: str, height: Optional[str] = None) -> "Card":
|
|
121
|
+
"""设置图片"""
|
|
122
|
+
self.image = image
|
|
123
|
+
if height:
|
|
124
|
+
self.image_height = height
|
|
125
|
+
return self
|
|
126
|
+
|
|
127
|
+
def set_footer(self, footer: Union[str, Component]) -> "Card":
|
|
128
|
+
"""设置底部"""
|
|
129
|
+
self.footer = footer
|
|
130
|
+
return self
|