add toast

This commit is contained in:
2026-10-30 01:31:16 +07:00
parent e03b04793c
commit 64f4c3ff0c
13 changed files with 558 additions and 44 deletions
+87 -14
View File
@@ -1,3 +1,5 @@
use std::time::Duration;
use gpui::prelude::FluentBuilder as _;
use gpui::{
App, AppContext as _, Bounds, ClickEvent, Context, Entity, InteractiveElement as _,
@@ -18,6 +20,7 @@ use gpui_native_core::radio::{Radio, RadioGroup};
use gpui_native_core::scroll_area::ScrollArea;
use gpui_native_core::slider::Slider;
use gpui_native_core::switch::Switch;
use gpui_native_core::toast::{Toast, ToastSeverity, Toasts, ToastsView};
use gpui_native_core::toggle::Toggle;
use gpui_native_core::tooltip::Tooltip;
use gpui_native_icons::{IconAssets, IconRole, icon};
@@ -36,6 +39,7 @@ struct Gallery {
menu_open: bool,
dialog_open: bool,
alert_open: bool,
toasts: Entity<Toasts>,
scroll: gpui::ScrollHandle,
}
@@ -335,6 +339,68 @@ impl Render for Gallery {
})
}),
)
.child(
v_flex()
.gap_2()
.child(section_label(
"toast · native ref — NSUserNotification stack, WinUI InfoBar, Adwaita in-app notifications",
colors,
))
.child(
div().flex().gap_2().children([
{
let toasts = self.toasts.clone();
Button::new("gallery-toast-success")
.label("Success")
.variant(ButtonVariant::Secondary)
.on_click(move |_, _, cx| {
toasts.update(cx, |toasts, cx| {
toasts.push(
Toast::new("Workspace saved")
.message("All changes written to disk.")
.severity(ToastSeverity::Success)
.no_icon(),
cx,
);
});
})
},
{
let toasts = self.toasts.clone();
Button::new("gallery-toast-warning")
.label("Warning")
.variant(ButtonVariant::Secondary)
.on_click(move |_, _, cx| {
toasts.update(cx, |toasts, cx| {
toasts.push(
Toast::new("Disk almost full")
.message("1.2 GB remaining.")
.severity(ToastSeverity::Warning),
cx,
);
});
})
},
{
let toasts = self.toasts.clone();
Button::new("gallery-toast-danger")
.label("Danger (sticky)")
.variant(ButtonVariant::Destructive)
.on_click(move |_, _, cx| {
toasts.update(cx, |toasts, cx| {
toasts.push(
Toast::new("Sync failed")
.message("Will retry when online.")
.severity(ToastSeverity::Danger)
.duration(Duration::ZERO),
cx,
);
});
})
},
]),
),
)
.child(
v_flex()
.gap_2()
@@ -673,7 +739,7 @@ impl Render for Gallery {
div().flex().items_center().gap_4().children(
IconRole::all()
.iter()
.map(|role| icon(*role).size(20.0).color(colors.foreground)),
.map(|role| icon(*role).size(px(20.)).color(colors.foreground)),
),
),
);
@@ -793,6 +859,7 @@ impl Render for Gallery {
)
.child(dialog)
.child(alert)
.child(ToastsView::new("gallery-toasts", self.toasts.clone()))
}
}
@@ -872,19 +939,25 @@ fn main() {
cx.refresh_windows();
})
.detach();
cx.new(|_| Gallery {
button_clicks: 0,
checkbox_checked: false,
switch_checked: true,
plan: SharedString::from("starter"),
toggle_pressed: false,
volume: 40.0,
input_state,
popover_open: false,
menu_open: false,
dialog_open: false,
alert_open: false,
scroll: gpui::ScrollHandle::new(),
let toasts = cx.new(|_| Toasts::default());
cx.new(|cx| {
// Toast pushes and dismissals repaint the stack.
cx.observe(&toasts, |_, _, cx| cx.notify()).detach();
Gallery {
button_clicks: 0,
checkbox_checked: false,
switch_checked: true,
plan: SharedString::from("starter"),
toggle_pressed: false,
volume: 40.0,
input_state,
popover_open: false,
menu_open: false,
dialog_open: false,
alert_open: false,
toasts,
scroll: gpui::ScrollHandle::new(),
}
})
},
) {