feat(widgets): add icon widget

This commit is contained in:
Liu
2026-10-17 21:16:50 +08:00
parent de31a3bc74
commit 92b46eee10
18 changed files with 1111 additions and 13 deletions
+16 -2
View File
@@ -10,7 +10,11 @@ npm install @lcui/fluent-icons
## Choosing icons
Search and select icons on [flicon.io](https://www.flicon.io/). For example, the zoom-in icon is typically named "Zoom In" in English.
Search and select icons in the browser below. For example, the zoom-in icon is typically named "Zoom In" in English.
<IconBrowser />
Fluent UI System Icons provides direction-related metadata, but its icon data has no category field, so the detail panel does not show category tags.
fluentui-system-icons icons are available in sizes like 16, 20, and 24, named in the format "icon name + size + style". `@lcui/fluent-icons` uses the naming format "icon name + style", and when the style is Regular it can be omitted.
@@ -22,8 +26,18 @@ import { ZoomIn } from "@lcui/fluent-icons";
<ZoomIn />
```
The default size is 20. If the icon size is fixed and you want better rendering, specify the size parameter:
The default glyph is regular 24; when an icon has no regular 24 glyph, the closest available regular size is used (ties choose the smaller size). `size` selects a design glyph size and, when explicitly set, also sets its display size. `fontSize` changes only the rendered size without changing glyph selection. If neither is set, the icon inherits the surrounding text size, so `className` or `style` can also control the display size:
```tsx
<ZoomIn size={32} />
```
In C, create a Fluent icon with the convenience constructor. In XML, omitting `library` defaults to Fluent; specify it only when using another icon library:
```c
ui_widget_t *icon = ui_create_fluent_icon("dismiss");
```
```xml
<icon name="dismiss" />
```
@@ -0,0 +1,41 @@
# Icon 组件设计
## 目标
`icon` 是继承 Text 的基础图标部件,为 `@lcui/fluent-icons` 及未来的图标字体包提供 C、XML 与 TSX 一致的承载节点。它不携带字体文件;字体资源始终由应用依赖的图标包和 `@lcui/cli` 打包。
## 类与字体资源
- Fluent font class:Regular 使用 `fui-icon`,Filled 使用 `fui-icon fui-icon-filled`;分别来自 `icons.css` 与 `icons-filled.css`,负责指定 font-family。
- Fluent glyph class:图标名使用 `fui-icon-<name>`,filled modifier 使用 `fui-icon-filled`,设计尺寸使用 `fui-icon-glyph-<N>`;图标包生成的 scoped CSS 将这些类组合映射到 Unicode codepoint。
- 图标名 `filled` 和匹配 `glyph-<digits>` 的名称为保留名,不能与 modifier token 冲突。
- Lucide 基类为 `lucide-icon`,图标名和设计尺寸分别使用 `lucide-icon-<name>`、`lucide-icon-glyph-<N>`;目前只支持 regular,不使用 filled 风格。
- 应用必须导入图标包的 CSS。未导入时 LCUI 按普通文本字体显示 glyph;核心库不探测字体、不打印警告、不渲染专用占位方框。
## API
```c
typedef enum ui_icon_library {
UI_ICON_LIBRARY_FLUENT,
UI_ICON_LIBRARY_LUCIDE,
} ui_icon_library_t;
ui_widget_t *ui_create_icon(void);
ui_widget_t *ui_create_fluent_icon(const char *name);
void ui_icon_set_library(ui_widget_t *icon, ui_icon_library_t library);
void ui_icon_set_name(ui_widget_t *icon, const char *name);
void ui_icon_set_size(ui_widget_t *icon, int size);
void ui_icon_set_filled(ui_widget_t *icon, bool filled);
```
- `ui_icon_set_library()`、`ui_icon_set_name()`、`ui_icon_set_size()` 和 `ui_icon_set_filled()` 共同生成库对应的 font class 与 semantic class。Fluent regular 图标例如为 `fui-icon fui-icon-access-time`;Filled 追加 `fui-icon-filled`,非默认设计尺寸追加 `fui-icon-glyph-N`。Lucide 使用 `lucide-icon` 和 `lucide-icon-access-time`,并固定选择 regular glyph;传入 filled 时不输出 filled class。更新时移除部件自身添加的旧 class;CSS `content` 由 Text 的样式更新链路写入渲染层。
- 新建部件默认 Fluent、regular,内部 `size=0` 表示自动选择,不生成 glyph-size class;基础 CSS 规则提供 regular 24,若缺少 24 则使用包生成器选定的最近可用尺寸。显式 `size=24` 同样使用基础规则;正数且非 24 的尺寸才生成 glyph-size class。核心库不维护图标尺寸表。
- `ui_create_fluent_icon(name)` 是 Fluent 常用情形的快捷构造函数,等价于创建默认 Fluent Icon 并设置 name;需要 filled、size 或其它库时继续使用 setter。
- `@lcui/fluent-icons` 的 TSX `Icon` 包装组件可以继续使用 `iconMap` 按 `convert.js` 规则选择更合适的实际尺寸,但该映射不复制进 LCUI 核心库。
- XML 使用 `library="fluent"`、`name="access-time"`、`size="24"` 和 `filled="false"` 属性,或直接使用等价的 namespaced Fluent classes。C 使用上述 setter;prototype 解析 `library`、`name`、`size`、`filled` 属性。Lucide 不使用 filled。
## 边界与验证
- 不绑定 `@lcui/fluent-icons`、未来 `@lcui/lucide-icons` 的 TTF、CSS 或图标全量映射到 LCUI 动态库。
- 不内建图标映射或码点;各图标包的 CSS 是 glyph class 与 `content` 的唯一来源。所有 glyph selectors 都必须由库基类限定。LCUI Icon 不再缓存独立的 `style_class` 或旧格式 `glyph_class`:只缓存并替换实际添加的 namespaced name/modifier classes。Lucide 使用独立 `lucide-icon` font class,目前只选择 regular per-icon glyph class,不暴露 filled variant。
- 测试工厂、Text 继承、library/filled class 切换和 glyph class 替换。CSS content 到文本层的更新由现有 Text 测试路径覆盖。
+16 -2
View File
@@ -10,7 +10,11 @@ npm install @lcui/fluent-icons
## 选取图标
在 [flicon.io](https://www.flicon.io/) 网站中搜索和选取图标。以放大图标为例,英文名通常是 Zoom In。
你可以在下面的图标库视图中搜索并选择图标。以放大图标为例,英文名通常是 Zoom In。
<IconBrowser />
Fluent UI System Icons 提供方向相关的元数据,但图标数据没有分类字段,因此详情面板不显示分类标签。
fluentui-system-icons 的图标有 16、20、24 等几种尺寸可选,命名方式是"图标名+尺寸+风格"。`@lcui/fluent-icons` 的命名方式是"图标名+风格",当风格为 Regular 时可以省略它。
@@ -22,8 +26,18 @@ import { ZoomIn } from "@lcui/fluent-icons";
<ZoomIn />
```
默认尺寸是 20。如果图标尺寸固定且希望更好的渲染效果,可以指定 size 参数:
默认选择 regular 24 字形;如果没有 24,则选择最接近的可用 regular 尺寸,距离相同时选较小尺寸。`size` 选择设计字形尺寸,并在显式设置时同步设置显示字号;`fontSize` 只改变显示字号、不改变字形选择。未显式设置时图标会继承周围的文字字号,因此也可以通过 `className` 或 `style` 调整显示大小:
```tsx
<ZoomIn size={32} />
```
C 代码可用 Fluent 快捷构造函数创建图标。XML 中省略 `library` 时默认使用 Fluent;只有使用其它图标库时才需要指定该属性:
```c
ui_widget_t *icon = ui_create_fluent_icon("dismiss");
```
```xml
<icon name="dismiss" />
```
@@ -0,0 +1,283 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <LCUI.h>
#include "icon-browser.tsx.h"
#include "icon-browser.h"
typedef struct {
icon_browser_react_t base;
char *search;
char *selected_name;
char *selected_component;
char *selected_class;
char *selected_size_class;
bool selected_filled_only;
int selected_size;
int search_timer_id;
} icon_browser_t;
static icon_browser_t *icon_browser_get(ui_widget_t *w)
{
while (w && !ui_widget_has_class(w, "icon-library-browser")) {
w = w->parent;
}
return w ? ui_widget_get_data(w, icon_browser_proto) : NULL;
}
static bool icon_browser_name_matches(const char *name, const char *query)
{
const unsigned char *name_char;
const unsigned char *query_char;
if (!query || !query[0]) {
return true;
}
for (; *name; ++name) {
name_char = (const unsigned char *)name;
query_char = (const unsigned char *)query;
while (*name_char && *query_char &&
tolower(*name_char) == tolower(*query_char)) {
++name_char;
++query_char;
}
if (!*query_char) {
return true;
}
}
return false;
}
static void icon_browser_apply_search(void *arg)
{
icon_browser_t *that = arg;
ui_widget_t *item;
const char *name;
size_t i;
that->search_timer_id = 0;
for (i = 0; (item = ui_widget_get_child(that->base.refs.icon_list, i));
++i) {
name = ui_widget_get_attr(item, "name");
ui_widget_set_style_string(
item, "display",
icon_browser_name_matches(name, that->search) ? "flex"
: "none");
}
}
static void icon_browser_on_search_change(ui_widget_t *w, ui_event_t *e,
void *arg)
{
icon_browser_t *that = icon_browser_get(w);
wchar_t wide_search[256] = { 0 };
char search[1024] = { 0 };
size_t wide_length;
wide_length = ui_textinput_get_text_w(w, 0, 255, wide_search);
wide_search[wide_length] = L'\0';
encode_utf8(search, wide_search, sizeof(search));
free(that->search);
that->search = y_strdup(search);
if (that->search_timer_id) {
ptk_clear_timeout(that->search_timer_id);
}
that->search_timer_id =
ptk_set_timeout(1000, icon_browser_apply_search, that);
}
static void icon_browser_on_select(ui_widget_t *w, ui_event_t *e, void *arg)
{
icon_browser_t *that = icon_browser_get(w);
const char *name = ui_widget_get_attr(w, "name");
const char *component = ui_widget_get_attr(w, "data-component-name");
const char *filled_only = ui_widget_get_attr(w, "data-filled-only");
const char *preview_size = ui_widget_get_attr(w, "data-preview-size");
char class_name[256];
char size_class[64];
free(that->selected_name);
that->selected_name = y_strdup(name);
free(that->selected_component);
that->selected_component = y_strdup(component);
that->selected_filled_only =
filled_only && strcmp(filled_only, "true") == 0;
that->selected_size = preview_size ? atoi(preview_size) : 24;
ui_text_set_content(that->base.refs.detail_name, name);
if (that->selected_class) {
ui_widget_remove_class(that->base.refs.detail_icon,
that->selected_class);
free(that->selected_class);
}
if (that->selected_size_class) {
ui_widget_remove_class(that->base.refs.detail_icon,
that->selected_size_class);
free(that->selected_size_class);
that->selected_size_class = NULL;
}
ui_widget_remove_class(that->base.refs.detail_icon, "fui-icon-filled");
snprintf(class_name, sizeof(class_name), "fui-icon-%s", name);
that->selected_class = y_strdup(class_name);
ui_widget_add_class(that->base.refs.detail_icon, that->selected_class);
if (that->selected_filled_only) {
ui_widget_add_class(that->base.refs.detail_icon,
"fui-icon-filled");
}
if (that->selected_size != 24) {
snprintf(size_class, sizeof(size_class), "fui-icon-glyph-%d",
that->selected_size);
that->selected_size_class = y_strdup(size_class);
ui_widget_add_class(that->base.refs.detail_icon,
that->selected_size_class);
}
ui_widget_set_style_string(that->base.refs.detail, "display", "flex");
}
static void icon_browser_on_close(ui_widget_t *w, ui_event_t *e, void *arg)
{
icon_browser_t *that = icon_browser_get(w);
ui_widget_set_style_string(that->base.refs.detail, "display", "none");
}
static void icon_browser_copy_text(const char *text)
{
size_t text_length = strlen(text);
wchar_t *wide_text = malloc(sizeof(wchar_t) * (text_length + 1));
size_t wide_length = decode_utf8(wide_text, text, text_length + 1);
ptk_clipboard_set_text(wide_text, wide_length);
free(wide_text);
}
static void icon_browser_on_copy(ui_widget_t *w, ui_event_t *e, void *arg)
{
icon_browser_t *that = icon_browser_get(w);
const char *format = ui_widget_get_attr(w, "data-copy-format");
char snippet[1024];
if (!that->selected_name) {
return;
}
if (strcmp(format, "c") == 0) {
snprintf(snippet, sizeof(snippet),
"ui_widget_t *icon = ui_create_fluent_icon(\"%s\");",
that->selected_name);
if (that->selected_filled_only) {
snprintf(snippet + strlen(snippet),
sizeof(snippet) - strlen(snippet),
"\nui_icon_set_filled(icon, true);");
}
if (that->selected_size != 24) {
snprintf(snippet + strlen(snippet),
sizeof(snippet) - strlen(snippet),
"\nui_icon_set_size(icon, %d);",
that->selected_size);
}
snprintf(snippet + strlen(snippet),
sizeof(snippet) - strlen(snippet),
"\nui_widget_append(parent, icon);");
} else if (strcmp(format, "jsx") == 0) {
if (that->selected_filled_only && that->selected_size != 24) {
snprintf(
snippet, sizeof(snippet),
"import { %s } from \"@lcui/fluent-icons\";\n\n"
"<%s size={%d} fontSize={24} />",
that->selected_component, that->selected_component,
that->selected_size);
} else {
snprintf(
snippet, sizeof(snippet),
"import { %s } from \"@lcui/fluent-icons\";\n\n"
"<%s />",
that->selected_component, that->selected_component);
}
} else {
if (that->selected_filled_only && that->selected_size != 24) {
snprintf(snippet, sizeof(snippet),
"<icon name=\"%s\" "
"filled=\"true\" size=\"%d\" />",
that->selected_name, that->selected_size);
} else if (that->selected_filled_only) {
snprintf(snippet, sizeof(snippet),
"<icon name=\"%s\" "
"filled=\"true\" />",
that->selected_name);
} else {
snprintf(snippet, sizeof(snippet),
"<icon name=\"%s\" />", that->selected_name);
}
}
icon_browser_copy_text(snippet);
}
static void icon_browser_update_locale(icon_browser_t *that, const char *locale)
{
bool is_english = locale && strcmp(locale, "en") == 0;
ui_textinput_set_placeholder(
that->base.refs.search,
is_english ? "Search icons..." : "搜索图标名称...");
ui_text_set_content(that->base.refs.copy_c,
is_english ? "Copy C code" : "复制 C 代码");
ui_text_set_content(that->base.refs.copy_jsx,
is_english ? "Copy JSX" : "复制 JSX");
ui_text_set_content(that->base.refs.copy_xml,
is_english ? "Copy XML" : "复制 XML");
}
static void icon_browser_on_set_attr(ui_widget_t *w, const char *name,
const char *value)
{
icon_browser_t *that = ui_widget_get_data(w, icon_browser_proto);
if (strcmp(name, "locale") == 0) {
icon_browser_update_locale(that, value);
}
}
static void icon_browser_init(ui_widget_t *w)
{
icon_browser_t *that =
ui_widget_add_data(w, icon_browser_proto, sizeof(icon_browser_t));
icon_browser_react_init(w);
that->search = y_strdup("");
that->selected_name = NULL;
that->selected_component = NULL;
that->selected_class = y_strdup("fui-icon-checkmark");
that->selected_size_class = NULL;
that->selected_filled_only = false;
that->selected_size = 24;
that->search_timer_id = 0;
ui_widget_set_style_string(that->base.refs.detail, "display", "none");
}
static void icon_browser_destroy(ui_widget_t *w)
{
icon_browser_t *that = ui_widget_get_data(w, icon_browser_proto);
if (that->search_timer_id) {
ptk_clear_timeout(that->search_timer_id);
}
free(that->search);
free(that->selected_name);
free(that->selected_component);
free(that->selected_class);
free(that->selected_size_class);
icon_browser_react_destroy(w);
}
ui_widget_t *ui_create_icon_browser(void)
{
return ui_create_widget_with_prototype(icon_browser_proto);
}
void ui_register_icon_browser(void)
{
icon_browser_init_prototype();
icon_browser_proto->init = icon_browser_init;
icon_browser_proto->destroy = icon_browser_destroy;
icon_browser_proto->setattr = icon_browser_on_set_attr;
}
@@ -0,0 +1,84 @@
.icon-library-browser {
position: relative;
@apply my-4 min-h-[480px];
}
.icon-library-search {
@apply mb-3 w-full;
}
.icon-library-scroll-area {
@apply h-[480px];
}
.icon-library-list {
@apply flex flex-wrap gap-2;
}
.icon-library-item {
@apply flex h-14 w-14 shrink-0 items-center justify-center rounded-lg bg-gray-100 cursor-pointer;
}
.icon-library-item:hover {
@apply bg-gray-200;
}
.icon-library-detail {
position: absolute;
z-index: 10;
bottom: 0;
left: 0;
right: 0;
@apply flex min-h-[160px] w-full items-center gap-4 rounded-xl border border-gray-200 bg-white p-4;
}
.icon-library-preview {
@apply flex h-32 w-32 shrink-0 items-center justify-center rounded-lg bg-gray-100;
}
.icon-library-detail-content {
@apply flex min-w-0 h-full flex-1 flex-col gap-4 justify-between;
}
.icon-library-detail-header {
@apply flex justify-between gap-3;
}
.icon-library-detail-name {
@apply text-lg font-semibold;
}
.icon-library-close {
@apply p-0 flex h-8 w-8 items-center justify-center;
}
.icon-library-actions {
@apply flex flex-wrap gap-2;
}
/* Vertical centering nudges for the icons on this page (measured ink
* offsets at this page's sizes; see the text-* buckets in global.css
* for icons that carry them -- these use private classes, so nudge
* here instead). The base `icon { position: relative; }` lives in
* global.css. */
/* grid icons render at font-size 24: ink sits 0.5-1px above center */
.icon-library-item .fui-icon {
top: 1px;
line-height: 28px;
position: relative;
}
/* 128px: ink sits ~5-6.5px above line center (top: 6px), and the
* default 1.43em line height is kept on purpose -- leading-none would
* shrink the line box to 128px and push the baseline to y=122, which
* clips full-bleed glyphs (bmp.top up to 124+) at the line-box edge
* before any widget offset can help. The 183px line box overflows
* the 128x128 preview, but the ink (rows 32..138) stays well inside
* the visible window after flex centering. */
.icon-library-preview .icon-library-detail-icon {
top: 6px;
position: relative;
@apply text-[116px] leading-[128px];
}
@@ -0,0 +1,9 @@
#include <ui.h>
void ui_register_icon_browser(void);
ui_widget_t *ui_create_icon_browser(void);
void icon_browser_update(ui_widget_t *w);
void ui_load_icon_browser_resources(void);
@@ -0,0 +1,128 @@
import {
Button,
ScrollArea,
ScrollAreaContent,
Scrollbar,
Text,
TextInput,
Widget,
} from "@lcui/react";
import { Dismiss, DismissCircle, Icon, iconMap } from "@lcui/fluent-icons";
import "./icon-browser.css";
const iconNames = Object.keys(iconMap).sort();
function getFilledPreviewSize(name: keyof typeof iconMap) {
const sizes = Object.keys(iconMap[name].filled).map(Number);
return sizes.reduce((best, size) => {
const distance = Math.abs(size - 24);
const bestDistance = Math.abs(best - 24);
return distance < bestDistance || (distance === bestDistance && size < best)
? size
: best;
});
}
function getIconComponentName(name: keyof typeof iconMap) {
const componentName = name
.split("-")
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join("");
return Object.keys(iconMap[name].regular).length > 0
? componentName
: `${componentName}Filled`;
}
function hasRegularStyle(name: keyof typeof iconMap) {
return Object.keys(iconMap[name].regular).length > 0;
}
interface IconBrowserProps {
locale?: "en" | "zh-CN";
}
export default function IconBrowser(_props: IconBrowserProps) {
return (
<Widget className="icon-library-browser">
<TextInput
$ref="search"
className="icon-library-search"
placeholder="搜索图标名称..."
onChange="icon_browser_on_search_change"
/>
<ScrollArea className="icon-library-scroll-area">
<ScrollAreaContent>
<Widget $ref="icon_list" className="icon-library-list">
{iconNames.map((name) => (
<Widget
key={name}
className="icon-library-item"
name={name}
data-filled-only={hasRegularStyle(name) ? "false" : "true"}
data-component-name={getIconComponentName(name)}
data-preview-size={
hasRegularStyle(name) ? 24 : getFilledPreviewSize(name)
}
onClick="icon_browser_on_select"
>
<Icon
name={name}
filled={!hasRegularStyle(name)}
size={hasRegularStyle(name) ? 24 : getFilledPreviewSize(name)}
fontSize={24}
/>
</Widget>
))}
</Widget>
</ScrollAreaContent>
<Scrollbar />
</ScrollArea>
<Widget $ref="detail" className="icon-library-detail">
<Widget className="icon-library-preview">
<Icon
$ref="detail_icon"
name="checkmark"
className="icon-library-detail-icon"
/>
</Widget>
<Widget className="icon-library-detail-content">
<Widget className="icon-library-detail-header">
<Text $ref="detail_name" className="icon-library-detail-name" />
<Button
className="icon-library-close"
onClick="icon_browser_on_close"
>
<Dismiss fontSize={16} />
</Button>
</Widget>
<Widget className="icon-library-actions">
<Button
$ref="copy_c"
className="icon-library-copy-button"
data-copy-format="c"
onClick="icon_browser_on_copy"
>
复制 C 代码
</Button>
<Button
$ref="copy_jsx"
className="icon-library-copy-button"
data-copy-format="jsx"
onClick="icon_browser_on_copy"
>
复制 JSX
</Button>
<Button
$ref="copy_xml"
className="icon-library-copy-button"
data-copy-format="xml"
onClick="icon_browser_on_copy"
>
复制 XML
</Button>
</Widget>
</Widget>
</Widget>
</Widget>
);
}
@@ -1,5 +1,6 @@
import { Text, Widget } from "@lcui/react";
import CodeBlockCopy from "../../../components/code-block-copy";
import IconBrowser from "../../../components/icon-browser";
export default function IconLibraryDocPage() {
return (
@@ -20,7 +21,9 @@ export default function IconLibraryDocPage() {
</Widget>
<Widget className="doc-section">
<h2>Choosing icons</h2>
<p>{`Search and select icons on flicon.io. For example, the zoom-in icon is typically named "Zoom In" in English.`}</p>
<p>{`Search and select icons in the browser below. For example, the zoom-in icon is typically named "Zoom In" in English.`}</p>
<IconBrowser locale="en" />
<p>{`Fluent UI System Icons provides direction-related metadata, but its icon data has no category field, so the detail panel does not show category tags.`}</p>
<p>{`fluentui-system-icons icons are available in sizes like 16, 20, and 24, named in the format "icon name + size + style". [bgcolor=#eee] @lcui/fluent-icons [/bgcolor] uses the naming format "icon name + style", and when the style is Regular it can be omitted.`}</p>
</Widget>
<Widget className="doc-section">
@@ -36,7 +39,7 @@ export default function IconLibraryDocPage() {
<Text className="code-line">{`[color=#116329]<[color=#0550ae]ZoomIn[/color] />[/color]`}</Text>
</Widget>
</Widget>
<p>{`The default size is 20. If the icon size is fixed and you want better rendering, specify the size parameter:`}</p>
<p>{`The default glyph is regular 24; when an icon has no regular 24 glyph, the closest available regular size is used (ties choose the smaller size). [bgcolor=#eee] size [/bgcolor] selects a design glyph size and, when explicitly set, also sets its display size. [bgcolor=#eee] fontSize [/bgcolor] changes only the rendered size without changing glyph selection. If neither is set, the icon inherits the surrounding text size, so [bgcolor=#eee] className [/bgcolor] or [bgcolor=#eee] style [/bgcolor] can also control the display size:`}</p>
<Widget className="code-block" data-language="tsx" data-source="en/devtools/icon-library/2">
<Widget className="code-block-header">
<Text className="code-block-title">TypeScript</Text>
@@ -46,6 +49,25 @@ export default function IconLibraryDocPage() {
<Text className="code-line">{`<[color=#8250df]ZoomIn[/color] size={[color=#0550ae]32[/color]} />`}</Text>
</Widget>
</Widget>
<p>{`In C, create a Fluent icon with the convenience constructor. In XML, omitting [bgcolor=#eee] library [/bgcolor] defaults to Fluent; specify it only when using another icon library:`}</p>
<Widget className="code-block" data-language="c" data-source="en/devtools/icon-library/3">
<Widget className="code-block-header">
<Text className="code-block-title">C</Text>
</Widget>
<Widget className="code-block-body">
<CodeBlockCopy />
<Text className="code-line">{`[color=#953800]ui_widget_t[/color] *icon = ui_create_fluent_icon([color=#0a3069]"dismiss"[/color]);`}</Text>
</Widget>
</Widget>
<Widget className="code-block" data-language="xml" data-source="en/devtools/icon-library/4">
<Widget className="code-block-header">
<Text className="code-block-title">XML</Text>
</Widget>
<Widget className="code-block-body">
<CodeBlockCopy />
<Text className="code-line">{`[color=#116329]<[color=#0550ae]icon[/color] [color=#0550ae]name[/color]=[color=#0a3069]"dismiss"[/color] />[/color]`}</Text>
</Widget>
</Widget>
</Widget>
</Widget>
);
+2 -1
View File
@@ -1,7 +1,8 @@
import { PropsWithChildren, ScrollArea, ScrollAreaContent, Scrollbar } from "@lcui/react";
import Navbar from "./components/navbar";
import Sidebar from "./components/sidebar";
import "@lcui/fluent-icons/dist/style.css";
import "@lcui/fluent-icons/dist/icons.css";
import "@lcui/fluent-icons/dist/icons-filled.css";
import "./global.css";
export default function AppLayout({ children }: PropsWithChildren) {
@@ -1,5 +1,6 @@
import { Text, Widget } from "@lcui/react";
import CodeBlockCopy from "../../../components/code-block-copy";
import IconBrowser from "../../../components/icon-browser";
export default function IconLibraryDocPage() {
return (
@@ -20,7 +21,9 @@ export default function IconLibraryDocPage() {
</Widget>
<Widget className="doc-section">
<h2>选取图标</h2>
<p>{`在 flicon.io 网站中搜索和选取图标。以放大图标为例,英文名通常是 Zoom In。`}</p>
<p>{`你可以在下面的图标库视图中搜索并选择图标。以放大图标为例,英文名通常是 Zoom In。`}</p>
<IconBrowser locale="zh-CN" />
<p>{`Fluent UI System Icons 提供方向相关的元数据,但图标数据没有分类字段,因此详情面板不显示分类标签。`}</p>
<p>{`fluentui-system-icons 的图标有 16、20、24 等几种尺寸可选,命名方式是"图标名+尺寸+风格"。[bgcolor=#eee] @lcui/fluent-icons [/bgcolor] 的命名方式是"图标名+风格",当风格为 Regular 时可以省略它。`}</p>
</Widget>
<Widget className="doc-section">
@@ -36,7 +39,7 @@ export default function IconLibraryDocPage() {
<Text className="code-line">{`[color=#116329]<[color=#0550ae]ZoomIn[/color] />[/color]`}</Text>
</Widget>
</Widget>
<p>{`默认尺寸是 20。如果图标尺寸固定且希望更好的渲染效果,可以指定 size 参数:`}</p>
<p>{`默认选择 regular 24 字形;如果没有 24,则选择最接近的可用 regular 尺寸,距离相同时选较小尺寸。[bgcolor=#eee] size [/bgcolor] 选择设计字形尺寸,并在显式设置时同步设置显示字号;[bgcolor=#eee] fontSize [/bgcolor] 只改变显示字号、不改变字形选择。未显式设置时图标会继承周围的文字字号,因此也可以通过 [bgcolor=#eee] className [/bgcolor] 或 [bgcolor=#eee] style [/bgcolor] 调整显示大小:`}</p>
<Widget className="code-block" data-language="tsx" data-source="zh-CN/devtools/icon-library/2">
<Widget className="code-block-header">
<Text className="code-block-title">TypeScript</Text>
@@ -46,6 +49,25 @@ export default function IconLibraryDocPage() {
<Text className="code-line">{`<[color=#8250df]ZoomIn[/color] size={[color=#0550ae]32[/color]} />`}</Text>
</Widget>
</Widget>
<p>{`C 代码可用 Fluent 快捷构造函数创建图标。XML 中省略 [bgcolor=#eee] library [/bgcolor] 时默认使用 Fluent;只有使用其它图标库时才需要指定该属性:`}</p>
<Widget className="code-block" data-language="c" data-source="zh-CN/devtools/icon-library/3">
<Widget className="code-block-header">
<Text className="code-block-title">C</Text>
</Widget>
<Widget className="code-block-body">
<CodeBlockCopy />
<Text className="code-line">{`[color=#953800]ui_widget_t[/color] *icon = ui_create_fluent_icon([color=#0a3069]"dismiss"[/color]);`}</Text>
</Widget>
</Widget>
<Widget className="code-block" data-language="xml" data-source="zh-CN/devtools/icon-library/4">
<Widget className="code-block-header">
<Text className="code-block-title">XML</Text>
</Widget>
<Widget className="code-block-body">
<CodeBlockCopy />
<Text className="code-line">{`[color=#116329]<[color=#0550ae]icon[/color] [color=#0550ae]name[/color]=[color=#0a3069]"dismiss"[/color] />[/color]`}</Text>
</Widget>
</Widget>
</Widget>
</Widget>
);
@@ -202,7 +202,7 @@ async function main(): Promise<void> {
}
});
} else {
console.log("\n[done] Build complete → tools/doc-viewer/app/");
console.log("\n[done] Build complete → examples/doc-viewer/app/");
}
}
@@ -486,6 +486,14 @@ function renderSectionContent(
continue;
}
if (trimmed === "<IconBrowser />") {
out.push(
`${indent}<IconBrowser locale="${locale === "en" ? "en" : "zh-CN"}" />`,
);
i++;
continue;
}
// ### heading inside section content
const h3Match = line.match(/^### (.+)$/);
if (h3Match) {
@@ -931,10 +939,15 @@ function buildContentPageTsx(
const allImports = [ftImportBlock].filter(Boolean).join("\n");
const importsBlock = allImports ? `${allImports}\n` : "";
const iconBrowserImport = parsed.sections.some((section) =>
section.content.includes("<IconBrowser />"),
)
? `import IconBrowser from "${relativeImport(pageDir, join(examplesOutDir, "..", "components", "icon-browser"))}";\n`
: "";
return `import { Text, Widget } from "@lcui/react";
import CodeBlockCopy from "${ccPath}";
${importsBlock}
${iconBrowserImport}${importsBlock}
export default function ${componentName}() {
return (
<Widget className="doc-page">
+1
View File
@@ -24,3 +24,4 @@
#include "widgets/checkbox.h"
#include "widgets/radio_group.h"
#include "widgets/field.h"
#include "widgets/icon.h"
+35
View File
@@ -0,0 +1,35 @@
/*
* include/LCUI/widgets/icon.h: -- LCUI's Icon widget
*
* Copyright (c) 2026, Liu Chao <hello@lcui.dev> All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* This file is part of LCUI, distributed under the MIT LICENSE file in the
* LICENSE.TXT file in the root directory of this source tree.
*/
#ifndef LCUI_INCLUDE_WIDGETS_ICON_H
#define LCUI_INCLUDE_WIDGETS_ICON_H
#include <LCUI/common.h>
#include <ui/base.h>
LCUI_BEGIN_HEADER
typedef enum ui_icon_library {
UI_ICON_LIBRARY_FLUENT,
UI_ICON_LIBRARY_LUCIDE,
} ui_icon_library_t;
LCUI_API ui_widget_t *ui_create_icon(void);
LCUI_API ui_widget_t *ui_create_fluent_icon(const char *name);
LCUI_API void ui_icon_set_library(ui_widget_t *icon, ui_icon_library_t library);
LCUI_API void ui_icon_set_name(ui_widget_t *icon, const char *name);
LCUI_API void ui_icon_set_size(ui_widget_t *icon, int size);
LCUI_API void ui_icon_set_filled(ui_widget_t *icon, bool filled);
LCUI_API void ui_register_icon(void);
LCUI_END_HEADER
#endif
+3 -2
View File
@@ -19,10 +19,11 @@ void lcui_widgets_init(void)
ui_register_canvas();
ui_register_anchor();
ui_register_button();
ui_register_select();
ui_register_progress();
ui_register_icon();
ui_register_scrollarea();
ui_register_scrollbar();
ui_register_select();
ui_register_progress();
ui_register_textcaret();
ui_register_textinput();
ui_register_router_link();
+223
View File
@@ -0,0 +1,223 @@
/*
* src/widgets/icon.c: -- Icon widget
*
* Copyright (c) 2026, Liu Chao <hello@lcui.dev> All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* This file is part of LCUI, distributed under the MIT LICENSE file in the
* LICENSE.TXT file in the root directory of this source tree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <LCUI/widgets/icon.h>
#include <ui/css.h>
#include <ui/prototype.h>
typedef struct ui_icon {
char *name;
char *name_class;
char *filled_class;
char *size_class;
int size;
bool filled;
ui_icon_library_t library;
} ui_icon_t;
static ui_widget_prototype_t *ui_icon_prototype;
/* Fluent icon glyphs have degenerate vertical metrics (hhea ascender = 1em,
* descender = 0), and pandagl centers the line box by the font ascent/descent
* box, so the ink renders slightly above the visual center (~0.5-2.5px
* depending on the font size). Nudge fluent icons down 1px rather than
* patching the font or the renderer. */
static const char *ui_icon_css = "\
icon,\
icon.fui-icon {\
top: 1px;\
display: inline-block;\
position: relative;\
line-height: 1;\
}";
static const char *ui_icon_library_names[] = {
"fluent",
"lucide",
};
static const char *ui_icon_library_classes[] = {
"fui-icon",
"lucide-icon",
};
static bool ui_icon_parse_bool(const char *value)
{
return value &&
(strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 ||
strcmp(value, "on") == 0 || strcmp(value, "1") == 0);
}
static void ui_icon_remove_class(ui_widget_t *icon, char **class_name)
{
if (*class_name) {
ui_widget_remove_class(icon, *class_name);
free(*class_name);
*class_name = NULL;
}
}
static char *ui_icon_create_class(const char *prefix, const char *name)
{
size_t length = strlen(prefix) + strlen(name) + 1;
char *class_name = malloc(length);
snprintf(class_name, length, "%s%s", prefix, name);
return class_name;
}
static void ui_icon_update_classes(ui_widget_t *icon)
{
ui_icon_t *that = ui_widget_get_data(icon, ui_icon_prototype);
const char *font_class = ui_icon_library_classes[that->library];
const char *name_prefix;
const char *size_prefix;
char size[16];
ui_widget_remove_class(icon, "fui-icon");
ui_widget_remove_class(icon, "lucide-icon");
ui_icon_remove_class(icon, &that->name_class);
ui_icon_remove_class(icon, &that->filled_class);
ui_icon_remove_class(icon, &that->size_class);
ui_widget_add_class(icon, font_class);
if (!that->name || !that->name[0]) {
return;
}
name_prefix = that->library == UI_ICON_LIBRARY_FLUENT ? "fui-icon-"
: "lucide-icon-";
size_prefix = that->library == UI_ICON_LIBRARY_FLUENT
? "fui-icon-glyph-"
: "lucide-icon-glyph-";
that->name_class = ui_icon_create_class(name_prefix, that->name);
ui_widget_add_class(icon, that->name_class);
if (that->library == UI_ICON_LIBRARY_FLUENT && that->filled) {
that->filled_class = y_strdup("fui-icon-filled");
ui_widget_add_class(icon, that->filled_class);
}
if (that->size > 0 && that->size != 24) {
snprintf(size, sizeof(size), "%d", that->size);
that->size_class = ui_icon_create_class(size_prefix, size);
ui_widget_add_class(icon, that->size_class);
}
}
static void ui_icon_update_name(ui_widget_t *icon, const char *name)
{
ui_icon_t *that = ui_widget_get_data(icon, ui_icon_prototype);
free(that->name);
that->name = name ? y_strdup(name) : NULL;
ui_icon_update_classes(icon);
}
static void ui_icon_on_set_attr(ui_widget_t *icon, const char *name,
const char *value)
{
ui_icon_t *that = ui_widget_get_data(icon, ui_icon_prototype);
if (strcmp(name, "library") == 0) {
that->library = value && strcmp(value, "lucide") == 0
? UI_ICON_LIBRARY_LUCIDE
: UI_ICON_LIBRARY_FLUENT;
ui_icon_update_classes(icon);
} else if (strcmp(name, "name") == 0) {
ui_icon_update_name(icon, value);
} else if (strcmp(name, "size") == 0) {
that->size = value ? atoi(value) : 0;
if (that->size < 1) {
that->size = 0;
}
ui_icon_update_classes(icon);
} else if (strcmp(name, "filled") == 0) {
that->filled = ui_icon_parse_bool(value);
ui_icon_update_classes(icon);
}
}
static void ui_icon_on_init(ui_widget_t *icon)
{
ui_icon_t *that;
ui_icon_prototype->proto->init(icon);
that = ui_widget_add_data(icon, ui_icon_prototype, sizeof(ui_icon_t));
that->name = NULL;
that->name_class = NULL;
that->filled_class = NULL;
that->size_class = NULL;
that->size = 0;
that->filled = false;
that->library = UI_ICON_LIBRARY_FLUENT;
ui_icon_update_classes(icon);
}
static void ui_icon_on_destroy(ui_widget_t *icon)
{
ui_icon_t *that = ui_widget_get_data(icon, ui_icon_prototype);
free(that->name);
free(that->name_class);
free(that->filled_class);
free(that->size_class);
ui_icon_prototype->proto->destroy(icon);
}
ui_widget_t *ui_create_icon(void)
{
return ui_create_widget_with_prototype(ui_icon_prototype);
}
ui_widget_t *ui_create_fluent_icon(const char *name)
{
ui_widget_t *icon = ui_create_icon();
ui_icon_set_name(icon, name);
return icon;
}
void ui_icon_set_library(ui_widget_t *icon, ui_icon_library_t library)
{
if (library < UI_ICON_LIBRARY_FLUENT ||
library > UI_ICON_LIBRARY_LUCIDE) {
library = UI_ICON_LIBRARY_FLUENT;
}
ui_widget_set_attr(icon, "library", ui_icon_library_names[library]);
}
void ui_icon_set_name(ui_widget_t *icon, const char *name)
{
ui_widget_set_attr(icon, "name", name ? name : "");
}
void ui_icon_set_size(ui_widget_t *icon, int size)
{
char value[16];
snprintf(value, sizeof(value), "%d", size);
ui_widget_set_attr(icon, "size", value);
}
void ui_icon_set_filled(ui_widget_t *icon, bool filled)
{
ui_widget_set_attr(icon, "filled", filled ? "true" : "false");
}
void ui_register_icon(void)
{
ui_icon_prototype = ui_create_widget_prototype("icon", "text");
ui_icon_prototype->init = ui_icon_on_init;
ui_icon_prototype->destroy = ui_icon_on_destroy;
ui_icon_prototype->setattr = ui_icon_on_set_attr;
ui_load_css_string(ui_icon_css, __FILE__);
}
+205
View File
@@ -0,0 +1,205 @@
/*
* tests/integration/test_icon.c: -- Icon widget tests
*
* Copyright (c) 2026, Liu Chao <hello@lcui.dev> All rights reserved.
*
* SPDX-License-Identifier: MIT
*/
#include <LCUI.h>
#include <ctest-custom.h>
static void test_defaults(void)
{
ui_widget_t *icon = ui_create_icon();
ctest_equal_bool("should inherit the text prototype",
ui_check_widget_type(icon, "text"), true);
ctest_equal_bool("should use the shared Fluent font class",
ui_widget_has_class(icon, "fui-icon"), true);
ctest_equal_bool("should omit the old Fluent regular font class",
ui_widget_has_class(icon, "fui-icon-regular"), false);
ui_widget_destroy(icon);
}
static void test_fluent_factory(void)
{
ui_widget_t *icon = ui_create_fluent_icon("checkmark");
ctest_equal_bool("should create a Fluent icon with its name class",
ui_widget_has_class(icon, "fui-icon-checkmark"), true);
ctest_equal_bool("should use the shared Fluent font class",
ui_widget_has_class(icon, "fui-icon"), true);
ctest_equal_bool("should use automatic design size by default",
ui_widget_has_class(icon, "fui-icon-glyph-0"), false);
ui_widget_destroy(icon);
}
static void test_glyph_class(void)
{
ui_widget_t *icon = ui_create_icon();
ui_icon_set_name(icon, "access-time");
ctest_equal_bool("should add the namespaced Fluent icon class",
ui_widget_has_class(icon, "fui-icon-access-time"),
true);
ctest_equal_bool("should omit a glyph size class for the default size",
ui_widget_has_class(icon, "fui-icon-glyph-24"), false);
ctest_equal_bool("should omit the unnamespaced icon name class",
ui_widget_has_class(icon, "access-time"), false);
ctest_equal_bool("should omit the legacy regular glyph class",
ui_widget_has_class(icon, "access-time-24-regular"),
false);
ui_icon_set_size(icon, 20);
ctest_equal_bool("should remove the previous glyph class",
ui_widget_has_class(icon, "access-time-24-regular"),
false);
ctest_equal_bool("should omit the legacy size glyph class",
ui_widget_has_class(icon, "access-time-20-regular"),
false);
ctest_equal_bool("should add the requested glyph size class",
ui_widget_has_class(icon, "fui-icon-glyph-20"), true);
ctest_equal_bool("should omit the unnamespaced glyph size class",
ui_widget_has_class(icon, "size-20"), false);
ui_icon_set_size(icon, 0);
ctest_equal_bool(
"should remove the glyph size class when size is automatic",
ui_widget_has_class(icon, "fui-icon-glyph-20"), false);
ctest_equal_bool("should omit a zero glyph size class",
ui_widget_has_class(icon, "fui-icon-glyph-0"), false);
ui_widget_destroy(icon);
}
static void test_library_and_fill(void)
{
ui_widget_t *icon = ui_create_icon();
ui_icon_set_name(icon, "access-time");
ui_icon_set_filled(icon, true);
ctest_equal_bool("should keep the namespaced Fluent icon class",
ui_widget_has_class(icon, "fui-icon-access-time"),
true);
ctest_equal_bool("should add the namespaced filled modifier",
ui_widget_has_class(icon, "fui-icon-filled"), true);
ctest_equal_bool("should omit the unnamespaced filled modifier",
ui_widget_has_class(icon, "filled"), false);
ctest_equal_bool("should keep the shared Fluent font class",
ui_widget_has_class(icon, "fui-icon"), true);
ctest_equal_bool("should omit the old Fluent regular font class",
ui_widget_has_class(icon, "fui-icon-regular"), false);
ctest_equal_bool("should replace the regular glyph class",
ui_widget_has_class(icon, "access-time-24-regular"),
false);
ctest_equal_bool("should omit the legacy filled glyph class",
ui_widget_has_class(icon, "access-time-24-filled"),
false);
ui_icon_set_size(icon, 20);
ctest_equal_bool("should add glyph size class while filled",
ui_widget_has_class(icon, "fui-icon-glyph-20"), true);
ctest_equal_bool("should omit the legacy filled size glyph class",
ui_widget_has_class(icon, "access-time-20-filled"),
false);
ui_widget_destroy(icon);
}
static void test_lucide_class(void)
{
ui_widget_t *icon = ui_create_icon();
ui_icon_set_library(icon, UI_ICON_LIBRARY_LUCIDE);
ui_icon_set_name(icon, "access-time");
ui_icon_set_size(icon, 20);
ui_icon_set_filled(icon, true);
ctest_equal_bool("should use the Lucide base class",
ui_widget_has_class(icon, "lucide-icon"), true);
ctest_equal_bool("should use a namespaced Lucide icon class",
ui_widget_has_class(icon, "lucide-icon-access-time"),
true);
ctest_equal_bool("should use a namespaced Lucide glyph size class",
ui_widget_has_class(icon, "lucide-icon-glyph-20"),
true);
ctest_equal_bool("should omit the old Lucide base class",
ui_widget_has_class(icon, "lui-icon-regular"), false);
ctest_equal_bool("should ignore filled for Lucide",
ui_widget_has_class(icon, "filled"), false);
ctest_equal_bool("should omit the old Lucide glyph class",
ui_widget_has_class(icon, "access-time-20-regular"),
false);
ctest_equal_bool("should omit a filled Lucide glyph class",
ui_widget_has_class(icon, "access-time-20-filled"),
false);
ui_widget_destroy(icon);
}
static wchar_t get_icon_content(ui_widget_t *icon)
{
wchar_t content[16] = { 0 };
ui_text_get_content_w(icon, content, 16);
return content[0];
}
static void test_content_selection(void)
{
static const char css[] =
".fui-icon { content: \"\\e000\"; }\n"
".fui-icon.fui-icon-checkmark { content: \"\\e001\"; }\n"
".fui-icon.fui-icon-checkmark.fui-icon-filled { content: "
"\"\\e101\"; }\n"
".fui-icon.fui-icon-checkmark.fui-icon-glyph-48 { content: "
"\"\\e002\"; }\n"
".fui-icon.fui-icon-checkmark.fui-icon-filled.fui-icon-glyph-48 { "
"content: \"\\e102\"; }\n";
ui_widget_t *icon = ui_create_icon();
ui_icon_set_name(icon, "checkmark");
ui_root_append(icon);
ui_load_css_string(css, "fluent-icon.css");
ui_update();
ctest_equal_int("should use the base regular content",
get_icon_content(icon), 0xe001);
ui_icon_set_filled(icon, true);
ui_update();
ctest_equal_int("should switch content with the filled class",
get_icon_content(icon), 0xe101);
ui_icon_set_size(icon, 48);
ui_update();
ctest_equal_int("should select the filled size glyph",
get_icon_content(icon), 0xe102);
ui_icon_set_filled(icon, false);
ui_update();
ctest_equal_int("should select the regular size glyph",
get_icon_content(icon), 0xe002);
ui_icon_set_size(icon, 0);
ui_update();
ctest_equal_bool("should omit a glyph size class in automatic mode",
ui_widget_has_class(icon, "fui-icon-glyph-0"), false);
ctest_equal_int("should use the base glyph in automatic size mode",
get_icon_content(icon), 0xe001);
ui_widget_destroy(icon);
}
void test_icon(void)
{
lcui_init();
ctest_describe("icon defaults", test_defaults);
ctest_describe("fluent icon factory", test_fluent_factory);
ctest_describe("icon glyph class", test_glyph_class);
ctest_describe("icon library and fill", test_library_and_fill);
ctest_describe("icon Lucide classes", test_lucide_class);
ctest_describe("icon CSS content", test_content_selection);
lcui_destroy();
}
+2
View File
@@ -36,6 +36,7 @@ extern void test_radio_group(void);
extern void test_portal(void);
extern void test_select(void);
extern void test_cursor(void);
extern void test_icon(void);
static const ctest_suite_t suites[] = {
{ "settings", test_settings },
@@ -62,6 +63,7 @@ static const ctest_suite_t suites[] = {
{ "portal", test_portal },
{ "select", test_select },
{ "cursor", test_cursor },
{ "icon", test_icon },
{ NULL, NULL }
};