Skip to content

MessageBox 信息提示 ​

用一个命令式对话框告知用户结果或状态,只提供一个“确定”按钮,不需要维护打开状态。

何时使用 ​

  • 操作完成后需要用户确认知晓,例如保存成功、导入完成或请求失败。
  • 需要用状态图标区分信息、成功、警告和错误时使用 MessageBox。
  • 需要用户做“确定 / 取消”选择时用 Confirm 确认框;需要输入内容时用 Prompt 输入框;短暂且无需确认的反馈用 Message 全局提示。

在线体验 ​

MessageBox 信息提示 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    if ui.Button(c, "提交订单").Primary().Clicked() {
        ui.MessageBox(c, ui.ConfirmOptions{
            Title:   "订单已提交",
            Content: "我们会尽快为你发货。",
            Status:  "success",
        })
    }
}

调用必须放在按钮 Clicked() 等一次性事件分支中;对话框会在全局浮层中显示,点击确定后关闭。

常见用法 ​

使用状态图标 ​

Status 支持 info、success、warning 和 error,用于匹配结果的语义;未设置时默认为 info。

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    if ui.Button(c, "检查连接").Clicked() {
        ui.MessageBox(c, ui.ConfirmOptions{
            Title:   "服务暂时不可用",
            Content: "请检查网络后重试。",
            Status:  "error",
            OkText:  "知道了",
        })
    }
}

居中、拖动和自定义图标 ​

Center 让图标、标题、内容和按钮居中,Draggable 允许拖动标题,Icon 可以换成图标库中的图标。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/icon"
)

func page(c *ui.Context) {
    if ui.Button(c, "显示收藏结果").Clicked() {
        ui.MessageBox(c, ui.ConfirmOptions{
            Title:     "收藏成功",
            Content:   "可以拖动标题移动这个对话框。",
            Status:    "success",
            Center:    true,
            Draggable: true,
            Icon:      icon.StarFilled,
        })
    }
}

在回调中更新页面状态 ​

OnOk 在 UI 线程执行;保存返回的 *ConfirmRef 后还可以通过 Action 判断用户的关闭方式,或用 IsOpen 观察当前状态。

go
import "github.com/tangoui/tango/ui"

var (
    messageRef *ui.ConfirmRef
    result     string
)

func page(c *ui.Context) {
    if ui.Button(c, "打开提示").Clicked() {
        messageRef = ui.MessageBox(c, ui.ConfirmOptions{
            Title: "同步完成",
            OnOk:  func(d *ui.ConfirmRef) { result = "已确认,动作:" + string(d.Action()) },
        })
    }
    if messageRef != nil && messageRef.IsOpen() {
        ui.Text(c, "提示正在显示").Secondary()
    }
    ui.Text(c, result).Secondary()
}

自定义宽度与遮罩关闭 ​

Width 控制对话框宽度;MaskClosable 允许点击遮罩关闭,关闭会按取消路径处理。信息提示通常保留默认的单按钮行为。

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    if ui.Button(c, "查看详细说明").Clicked() {
        ui.MessageBox(c, ui.ConfirmOptions{
            Title:        "导入完成",
            Content:      "已导入 128 条记录,点击遮罩或确定都可以关闭。",
            Status:       "info",
            Width:        520,
            MaskClosable: true,
            OnCancel:     func() { /* 记录用户关闭了提示 */ },
        })
    }
}

主题与样式 ​

MessageBox 与 Confirm、Prompt、Modal 共用 theme.Components.Modal 令牌。Ant Design 风格默认在窗口顶部展示,状态图标位于内容左侧;Element Plus 和 shadcn 风格使用各自的对话框布局,后者默认居中。可以通过 ConfigProvider.Defaults("Confirm", ui.Props{...}) 配置默认 width、okText 等值,选项对象中的显式值优先。

注意事项 ​

  • 只在事件中调用:不要在页面函数每帧无条件调用 MessageBox,否则会不断创建新对话框。
  • 单按钮语义:MessageBox 只有确定按钮;需要用户在两个动作间选择时改用 Confirm。
  • 回调线程:OnOk、OnCancel 和 OnClose 在 UI 线程执行,可以直接更新页面状态。
  • 关闭方式:关闭按钮、Esc 和显式启用的遮罩关闭都走取消回调。NoKeyboard: true 禁止 Esc 关闭。启用 DistinguishCancelAndClose 时,这些关闭方式会调用 OnClose。
  • 焦点与无障碍:打开后焦点进入模态对话框并被限制在其中,关闭后恢复到打开它的控件。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

MessageBox 信息提示

gallery 演示 MessageBox 的具名 Render 函数,源码 page_more_confirm.go#L16。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.MessageBox ​

go
func MessageBox(c *Context, o ConfirmOptions) *ConfirmRef

MessageBox opens an information dialog with a single OK button (Ant Modal.info/success/warning/error, Element ElMessageBox.alert). Set o.Status to pick the icon.

返回 *ConfirmRef,可链式调用的方法见 ConfirmRef 方法。

ConfirmRef 方法 ​

ui.ConfirmRef:ConfirmRef is a handle to an open dialog.

方法说明签名
ActionAction reports how the dialog was closed by the user ("" while open or after Close).Action() ConfirmAction
CloseClose closes the dialog without calling any callback.Close()
IsOpenIsOpen reports whether the dialog is still shown.IsOpen() bool
LoadingLoading shows (or clears) the spinner on the OK button.Loading(on bool)
ValueValue returns the Prompt input.Value() string

ConfirmOptions 参数类型 ​

ui.ConfirmOptions:ConfirmOptions configure Confirm, MessageBox and Prompt.

字段说明类型
Titlestring
Contentstring
StatusStatus picks the leading icon: "info", "success", "warning", "error" or "none".string
OkTextdefault "确定" ("知道了" for Ant MessageBox)string
CancelTextdefault "取消"string
Dangerred OK button (destructive actions)bool
Widthfloat32
MaskClosableMaskClosable closes the dialog (as a cancel) on a mask click.bool
OnOkOnOk runs on the UI thread when OK is activated.func(d *ConfirmRef)
OnCancelOnCancel runs on Cancel, the close button, Escape or a mask click.func()
DistinguishCancelAndCloseDistinguishCancelAndClose tells the close button, Escape and a mask click apart from Cancel: they run OnClose (not OnCancel) and ConfirmRef.Action reports ConfirmActionClose.bool
OnCloseOnClose runs when the dialog is closed without Cancel or OK (close button, Escape, mask) and DistinguishCancelAndClose is set.func()
CenterCenter centers the icon, title, content and buttons.bool
DraggableDraggable lets the user move the dialog by dragging its title.bool
IconIcon replaces the status icon; it is drawn in the status color (the primary color for Status "none").*icon.Icon
AutoFocusButtonAutoFocusButton picks the button focused when the dialog opens: "" or "ok" (default), "cancel", or "none" (antd autoFocusButton, null for none).string
NoKeyboardNoKeyboard keeps Escape from closing the dialog (antd keyboard false, Element close-on-press-escape false).bool
InputValuePrompt only.string
InputPlaceholderstring
ValidateValidate returns an error message ("" = valid); OK is refused while it fails.func(v string) string

相关链接 ​

以 MIT 许可发布