Expand description
A modal dialog component for displaying critical information or actions.
This module provides dialog_provider
, a component that renders content in a modal
overlay. When active, the dialog sits on top of the primary UI, blocks interactions
with the content behind it (via a “scrim”), and can be dismissed by user actions
like pressing the Escape
key or clicking the scrim.
§Key Components
dialog_provider
: The main function that wraps your UI to provide dialog capabilities.DialogProviderState
: A state object you create and manage to control the dialog’s visibility using itsopen()
andclose()
methods.DialogProviderArgs
: Configuration for the provider, including the visualstyle
of the scrim and the mandatoryon_close_request
callback.DialogStyle
: Defines the scrim’s appearance, eitherMaterial
(a simple dark overlay) orGlass
(a blurred, translucent effect).
§Usage
The dialog_provider
acts as a wrapper around your main content. It takes the main
content and the dialog content as separate closures.
- Create State: In your application’s state, create an
Arc<RwLock<DialogProviderState>>
. - Wrap Content: Call
dialog_provider
at a high level in your component tree. - Provide Content: Pass two closures to
dialog_provider
:main_content
: Renders the UI that is always visible.dialog_content
: Renders the content of the dialog box itself. This closure receives anf32
alpha value for animating its appearance.
- Control Visibility: From an event handler (e.g., a button’s
on_click
), calldialog_state.write().open()
to show the dialog. - Handle Closing: The
on_close_request
callback you provide is responsible for callingdialog_state.write().close()
to dismiss the dialog.
§Example
use std::sync::Arc;
use parking_lot::RwLock;
use tessera_ui::{tessera, Renderer};
use tessera_ui_basic_components::{
dialog::{dialog_provider, DialogProviderArgsBuilder, DialogProviderState},
button::{button, ButtonArgsBuilder},
ripple_state::RippleState,
text::{text, TextArgsBuilder},
};
// Define an application state.
#[derive(Default)]
struct AppState {
dialog_state: Arc<RwLock<DialogProviderState>>,
ripple_state: Arc<RippleState>,
}
#[tessera]
fn app(state: Arc<RwLock<AppState>>) {
let dialog_state = state.read().dialog_state.clone();
// Use the dialog_provider.
dialog_provider(
DialogProviderArgsBuilder::default()
// Provide a callback to handle close requests.
.on_close_request(Arc::new({
let dialog_state = dialog_state.clone();
move || dialog_state.write().close()
}))
.build()
.unwrap(),
dialog_state.clone(),
// Define the main content.
move || {
button(
ButtonArgsBuilder::default()
.on_click(Arc::new({
let dialog_state = dialog_state.clone();
move || dialog_state.write().open()
}))
.build()
.unwrap(),
state.read().ripple_state.clone(),
|| text(TextArgsBuilder::default().text("Show Dialog".to_string()).build().unwrap())
);
},
// Define the dialog content.
|alpha| {
text(TextArgsBuilder::default().text("This is a dialog!".to_string()).build().unwrap());
}
);
}
Structs§
- Dialog
Provider Args - Arguments for the
dialog_provider
component. - Dialog
Provider Args Builder - Builder for
DialogProviderArgs
. - Dialog
Provider State
Enums§
- Dialog
Provider Args Builder Error - Error type for DialogProviderArgsBuilder
- Dialog
Style - Defines the visual style of the dialog’s scrim.
Functions§
- dialog_
provider - A provider component that manages the rendering and event flow for a modal dialog.