跳转到内容

提示消息

在窗口边缘显示一条短消息,几秒后消失:默认在右下角,也可以用 set_toast_position 换位置。

ui.toast

基本用法

if ui.button("Show toast").outline().clicked() {
ui.toast("Saved").success();
}

色调和说明

if ui.button("Show error").outline().clicked() {
ui.toast("Upload failed")
.description("Check your connection and try again.")
.danger();
}

同时显示多条

// Each has a key of its own, so a new one joins the stack. Point at the stack to open it.
if ui.button("Add toast").outline().clicked() {
self.toasts += 1;
let toast = ui.toast(format_args!("Message {}", self.toasts));
toast.description("Point at the stack to open it.").key(self.toasts);
}

带一个操作按钮

if ui.button("Delete note").outline().clicked() {
ui.toast("Note deleted").action("Undo", Command::Undo);
}
// True once, in the frame the toast's button is pressed.
if ui.action(Command::Undo) {
ui.toast("Note restored").success();
}

显示位置

ui.grid_cols(3).gap(8).children(|ui| {
for (name, position) in [
("Top left", ToastPosition::TopLeft),
("Top center", ToastPosition::TopCenter),
("Top right", ToastPosition::TopRight),
("Bottom left", ToastPosition::BottomLeft),
("Bottom center", ToastPosition::BottomCenter),
("Bottom right", ToastPosition::BottomRight),
] {
if ui.button(name).outline().w_fill().clicked() {
// For this toast and the ones after it.
ui.set_toast_position(position);
ui.toast(name);
}
}
});

关闭前一直显示

ui.row().gap(8).children(|ui| {
if ui.button("Start").outline().clicked() {
// The key names the toast, to update or close it later.
ui.toast("Exporting…").key("export").persistent();
}
if ui.button("Finish").outline().clicked() {
ui.close_toast("export");
}
});

接口

设置说明
.description(text: impl Display)第二行文字,字号较小,用次要色。
.tone(tone: Tone)设置色调:决定图标的颜色;没有另设图标时,也决定用哪个图标。
.primary()强调色。
.success()成功色。
.warning()警告色。
.danger()危险色。屏幕阅读器会立即读出这条提示消息。
.icon(icon: IconName)文字前的图标。
.action(label: impl Display, action: A)添加一个按钮(如“撤销”),用来触发 action 并关闭提示消息。
.key(key: impl Hash)用 key 而不是文字来标识提示消息。
.duration(duration: Duration)提示消息的显示时长:默认 4 秒,带操作时 8 秒。
.persistent()提示消息一直显示,直到用关闭按钮或 Ui::close_toast 关闭。