Button 按钮
触发一个明确动作,例如保存表单、提交查询、打开对话框。Primary 用于主要操作,Danger 表示具有破坏性的动作。
在线体验
点击预览按钮,观察计数变化;Disabled 按钮不会改变状态。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
"fmt"
)
var clicks int
func page(c *ui.Context) {
if ui.Button(c, "Click me").Primary().Clicked() {
clicks++
}
ui.Text(c, fmt.Sprintf("Clicks: %d", clicks))
}常用参数与方法
| 参数 / 方法 | 说明 |
|---|---|
Primary() | 主要操作的强调样式 |
Danger() | 危险操作样式,可与 Primary 组合 |
Disabled(bool) | 禁用交互 |
Loading(bool) | 显示加载状态并阻止重复点击 |
Small() / Large() | 设置控件尺寸 |
Clicked() bool | 读取当前帧是否发生点击 |
使用说明
使用 Tab 聚焦后,Enter 或空格可以触发按钮。异步操作开始后可使用 Loading(true) 阻止重复提交。
更多场景
官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。
实现与更多示例
全部示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
完整 API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Button
func Button(c *Context, label string) *ButtonWButton adds a button.
返回 *ButtonW,可链式调用的方法见 ButtonW 方法。
ui.IconButton
func IconButton(c *Context, ic *icon.Icon, label string) *IconButtonWIconButton adds an icon button; label is used for tests and accessibility.
返回 *IconButtonW,可链式调用的方法见 IconButtonW 方法。
ButtonW 方法
ui.ButtonW:ButtonW is a button under construction; chain modifiers then query.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name read by screen readers. | A11yLabel(s string) *ButtonW |
Appearance | Appearance sets how the button wears its color (Ant v6 variant, plus Element's plain). | Appearance(a ButtonAppearance) *ButtonW |
AutoInsertSpace | AutoInsertSpace sets whether a label of exactly two Chinese characters is drawn with a space between them ("确定" shows as "确 定"; Ant / Element ConfigProvider button.autoInsertSpace). | AutoInsertSpace(on bool) *ButtonW |
Block | Block stretches the button to the container width. | Block() *ButtonW |
Circle | Circle makes an icon-only round button. | Circle() *ButtonW |
Clicked | Clicked reports activation (never when disabled or loading). | Clicked() bool |
Color | Color sets the button color (Ant v6 color): a role, an Ant preset or a hex color ("#13c2c2"). | Color(col ButtonColor) *ButtonW |
Danger | Danger() *ButtonW | |
Dashed | Dashed() *ButtonW | |
Disabled | Disabled disables the button when on. | Disabled(on bool) *ButtonW |
Ghost | Ghost() *ButtonW | |
Hovered | Hovered reports hover. | Hovered() bool |
ID | ID returns the widget ID. | ID() ID |
Icon | Icon puts an icon before the label. | Icon(ic *icon.Icon) *ButtonW |
IconPlacement | IconPlacement places the icon at the inline "start" of the label (default) or at its "end" (Ant v6 iconPlacement, which replaces iconPosition). | IconPlacement(p string) *ButtonW |
IconRight | IconRight puts the icon after the label (Icon(ic).IconPlacement("end")). | IconRight(ic *icon.Icon) *ButtonW |
Info | Info makes a solid info button (Element type="info"). | Info() *ButtonW |
Large | Large() *ButtonW | |
Layout | Layout applies layout options. | Layout(opts ...layout.Option) *ButtonW |
Link | Link() *ButtonW | |
Loading | Loading shows a spinner and blocks clicks. | Loading(on bool) *ButtonW |
LoadingDelay | LoadingDelay postpones the loading state (Ant loading.delay): after Loading(true) the button stays clickable and shows no spinner until d has passed; a loading that ends sooner never shows. | LoadingDelay(d time.Duration) *ButtonW |
LoadingIcon | LoadingIcon replaces the loading spinner with ic, drawn spinning (Ant loading.icon, Element loading-icon). | LoadingIcon(ic *icon.Icon) *ButtonW |
Plain | Plain uses Element's plain style: a light tint of the color with a tinted border, filled with the color under the pointer. | Plain() *ButtonW |
Primary | Primary() *ButtonW | |
Round | Round() *ButtonW | |
Secondary | Secondary makes a neutral filled button (shadcn "secondary"). | Secondary() *ButtonW |
Size | Size sets the control size. | Size(s ControlSize) *ButtonW |
Small | Small() *ButtonW | |
Success | Success makes a solid success button (Element type="success"); add Plain for the plain style. | Success() *ButtonW |
Text | Text() *ButtonW | |
Variant | Variant sets the variant explicitly. | Variant(v ButtonVariant) *ButtonW |
Warning | Warning makes a solid warning button (Element type="warning"). | Warning() *ButtonW |
IconButtonW 方法
ui.IconButtonW:IconButtonW is a borderless square icon button (close buttons, toolbar toggles, calendar navigation).
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel replaces the accessible name (by default the label argument). | A11yLabel(s string) *IconButtonW |
Clicked | Clicked reports activation. | Clicked() bool |
Color | Color fixes the icon color. | Color(col theme.Color) *IconButtonW |
Hovered | Hovered reports hover. | Hovered() bool |
NoHoverBg | NoHoverBg disables the hover background. | NoHoverBg() *IconButtonW |
Size | Size sets the square box size and the icon size. | Size(box, iconSize float32) *IconButtonW |
Icon 参数类型
icon.Icon:Icon is a vector (or bitmap, see FromImage) icon. Declare custom icons with a composite literal, New or FromImage:
var Branch = &icon.Icon{Name: "app:branch", Box: [4]float32{0, 0, 1024, 1024},
Parts: []icon.Part{{D: "M..."}}}
The exported fields must not change once the icon is first drawn.
| 字段 | 说明 | 类型 |
|---|---|---|
Name | string | |
Box | Box is the view box (min x, min y, width, height) Parts are drawn in, like an SVG viewBox. | [4]float32 |
Width | Width is the stroke width of stroked Parts in Box units. | float32 |
Parts | Parts are painted in order. | []Part |
Dir | Dir marks a directional icon (an arrow, a chevron, text lines that start at the left): it mirrors horizontally in right-to-left layouts. | bool |
Icon 的方法:
| 方法 | 说明 | 签名 |
|---|---|---|
Directional | Directional marks ic as directional, so it mirrors horizontally when drawn for a right-to-left layout (see DrawOpts.RTL), and returns ic. | Directional() *Icon |
IsDirectional | IsDirectional reports whether ic mirrors in right-to-left layouts. | IsDirectional() bool |
IsImage | IsImage reports whether ic is a bitmap icon made by FromImage. | IsImage() bool |