forked from reya/gpui-native-kit
add toast and alert overlays to the shell
This commit is contained in:
@@ -6,8 +6,12 @@ use gpui::{
|
||||
AnyElement, App, AppContext as _, Context, ElementId, Entity, InteractiveElement as _,
|
||||
IntoElement, ParentElement as _, Pixels, RenderOnce, Styled as _, Window, div, px,
|
||||
};
|
||||
#[cfg(feature = "alert")]
|
||||
use gpui_native_core::alert::{Alert, Alerts, AlertsView};
|
||||
use gpui_native_core::button::{Button, ButtonVariant};
|
||||
use gpui_native_core::common::{Sizable as _, h_flex, v_flex};
|
||||
#[cfg(feature = "toast")]
|
||||
use gpui_native_core::toast::{Toast, Toasts, ToastsView};
|
||||
use gpui_native_data::sidebar::Sidebar;
|
||||
use gpui_native_icons::IconRole;
|
||||
use gpui_native_theme::{ActiveTheme as _, Size};
|
||||
@@ -29,16 +33,24 @@ pub struct LayoutState {
|
||||
right_visible: bool,
|
||||
left_width: Pixels,
|
||||
right_width: Pixels,
|
||||
#[cfg(feature = "toast")]
|
||||
toasts: Entity<Toasts>,
|
||||
#[cfg(feature = "alert")]
|
||||
alerts: Entity<Alerts>,
|
||||
}
|
||||
|
||||
impl LayoutState {
|
||||
/// Creates the state: left sidebar visible, right sidebar hidden.
|
||||
pub fn new(cx: &mut App) -> Entity<Self> {
|
||||
cx.new(|_| Self {
|
||||
cx.new(|cx| Self {
|
||||
left_visible: true,
|
||||
right_visible: false,
|
||||
left_width: px(DEFAULT_SIDEBAR_WIDTH),
|
||||
right_width: px(DEFAULT_SIDEBAR_WIDTH),
|
||||
#[cfg(feature = "toast")]
|
||||
toasts: cx.new(|_| Toasts::default()),
|
||||
#[cfg(feature = "alert")]
|
||||
alerts: cx.new(|_| Alerts::default()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -91,6 +103,42 @@ impl LayoutState {
|
||||
(self.right_width - delta).clamp(px(MIN_SIDEBAR_WIDTH), px(MAX_SIDEBAR_WIDTH));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Raises a toast in the shell's overlay. Returns its id.
|
||||
#[cfg(feature = "toast")]
|
||||
pub fn push_toast(&mut self, toast: Toast, cx: &mut Context<Self>) -> usize {
|
||||
self.toasts.update(cx, |toasts, cx| toasts.push(toast, cx))
|
||||
}
|
||||
|
||||
/// Dismisses one toast by id.
|
||||
#[cfg(feature = "toast")]
|
||||
pub fn dismiss_toast(&mut self, id: usize, cx: &mut Context<Self>) {
|
||||
self.toasts.update(cx, |toasts, cx| toasts.dismiss(id, cx));
|
||||
}
|
||||
|
||||
/// Dismisses the toast on screen.
|
||||
#[cfg(feature = "toast")]
|
||||
pub fn clear_toasts(&mut self, cx: &mut Context<Self>) {
|
||||
self.toasts.update(cx, |toasts, cx| toasts.clear(cx));
|
||||
}
|
||||
|
||||
/// Pushes an alert onto the shell's stack. Returns its id.
|
||||
#[cfg(feature = "alert")]
|
||||
pub fn push_alert(&mut self, alert: Alert, cx: &mut Context<Self>) -> usize {
|
||||
self.alerts.update(cx, |alerts, cx| alerts.push(alert, cx))
|
||||
}
|
||||
|
||||
/// Dismisses one alert by id, running its dismiss handler.
|
||||
#[cfg(feature = "alert")]
|
||||
pub fn dismiss_alert(&mut self, id: usize, cx: &mut Context<Self>) {
|
||||
self.alerts.update(cx, |alerts, cx| alerts.dismiss(id, cx));
|
||||
}
|
||||
|
||||
/// Dismisses every alert, running each entry's `on_dismiss` handler.
|
||||
#[cfg(feature = "alert")]
|
||||
pub fn clear_alerts(&mut self, cx: &mut Context<Self>) {
|
||||
self.alerts.update(cx, |alerts, cx| alerts.clear(cx));
|
||||
}
|
||||
}
|
||||
|
||||
/// Which sidebar a region or toggle belongs to.
|
||||
@@ -241,8 +289,22 @@ impl RenderOnce for Shell {
|
||||
))
|
||||
});
|
||||
|
||||
// Wraps the layout in a window frame.
|
||||
frame(layout, window, cx)
|
||||
// Wraps the layout in a window frame
|
||||
let mut shell = frame(layout, window, cx);
|
||||
|
||||
#[cfg(feature = "toast")]
|
||||
{
|
||||
let toasts = state.read(cx).toasts.clone();
|
||||
shell = shell.child(ToastsView::new((id.clone(), "toasts"), toasts));
|
||||
}
|
||||
|
||||
#[cfg(feature = "alert")]
|
||||
{
|
||||
let alerts = state.read(cx).alerts.clone();
|
||||
shell = shell.child(AlertsView::new((id, "alerts"), alerts));
|
||||
}
|
||||
|
||||
shell
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user