use std::sync::Arc;
use super::{
button::{Button, ButtonPart, ButtonSize, ButtonVariant},
dialog::{Dialog, DialogPart},
message_box::{DisclosureParts, autofocus, disclosure, disclosure_recipe, wrapping},
names,
part_style::{Length, PartStyle},
progress::{Progress, ProgressFormat, ProgressPart, ProgressSize},
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::glyph,
theme::{Themed, current_theme},
};
use crate::{
app::{
events::KeyboardEvent,
theme::{PaletteColor, Theme, TypographyStyle},
},
components::{Column, Row},
core::Signal,
layout::{Alignment, layout_kind::Justify, text_style::FontWeight},
node::{Element, dimension::Dimension, node_kind::TextOverflow},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ProgressDialogPart {
Veil,
Panel,
Content,
Header,
Icon,
Text,
Title,
Description,
Progress,
Meta,
Count,
Status,
Footer,
DetailTrigger,
DetailChevron,
DetailLabel,
DetailPanel,
Detail,
Actions,
ActionButton,
ActionLabel,
CancelButton,
CancelLabel,
}
names!(ProgressDialogPart {
Veil => "veil",
Panel => "panel",
Content => "content",
Header => "header",
Icon => "icon",
Text => "text",
Title => "title",
Description => "description",
Progress => "progress",
Meta => "meta",
Count => "count",
Status => "status",
Footer => "footer",
DetailTrigger => "detail_trigger",
DetailChevron => "detail_chevron",
DetailLabel => "detail_label",
DetailPanel => "detail_panel",
Detail => "detail",
Actions => "actions",
ActionButton => "action_button",
ActionLabel => "action_label",
CancelButton => "cancel_button",
CancelLabel => "cancel_label",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ProgressDialogState {
Indeterminate,
DetailOpen,
}
names!(ProgressDialogState {
Indeterminate => "indeterminate",
DetailOpen => "detail_open",
});
pub type ProgressDialogRenderer = Arc<dyn Fn(ProgressDialogView) -> Element + Send + Sync>;
type Handler = Arc<dyn Fn() + Send + Sync>;
pub struct ProgressDialog {
open: Signal<bool>,
icon: Option<Name>,
title: Option<Arc<str>>,
description: Option<Arc<str>>,
label: Option<Arc<str>>,
value: Option<f32>,
max: f32,
format: Option<ProgressFormat>,
status: Option<Arc<str>>,
detail: Option<(Arc<str>, Signal<bool>)>,
detail_label: Arc<str>,
actions: Vec<(Arc<str>, Handler)>,
cancel_label: Arc<str>,
on_cancel: Vec<Handler>,
inline: bool,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl ProgressDialog {
pub fn new(open: Signal<bool>) -> Self {
Self {
open,
icon: None,
title: None,
description: None,
label: None,
value: None,
max: 1.0,
format: None,
status: None,
detail: None,
detail_label: super::current_locale().common.details.clone(),
actions: Vec::new(),
cancel_label: super::current_locale().common.cancel.clone(),
on_cancel: Vec::new(),
inline: false,
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn icon(mut self, name: impl Into<Name>) -> Self {
self.icon = Some(name.into());
self
}
pub fn title(mut self, title: &str) -> Self {
self.title = Some(Arc::from(title));
self
}
pub fn description(mut self, description: &str) -> Self {
self.description = Some(Arc::from(description));
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn value(mut self, value: impl Into<f64>) -> Self {
self.value = Some(value.into() as f32);
self
}
pub fn max(mut self, max: impl Into<f64>) -> Self {
self.max = max.into() as f32;
self
}
pub fn format(mut self, format: impl Fn(f32, f32) -> String + Send + Sync + 'static) -> Self {
self.format = Some(Arc::new(format));
self
}
pub fn status(mut self, status: &str) -> Self {
self.status = Some(Arc::from(status));
self
}
pub fn detail(mut self, detail: &str, open: Signal<bool>) -> Self {
self.detail = Some((Arc::from(detail), open));
self
}
pub fn detail_label(mut self, label: &str) -> Self {
self.detail_label = Arc::from(label);
self
}
pub fn button(mut self, label: &str, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.actions.push((Arc::from(label), Arc::new(handler)));
self
}
pub fn on_cancel(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_cancel.push(Arc::new(handler));
self
}
pub fn cancel_label(mut self, label: &str) -> Self {
self.cancel_label = Arc::from(label);
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
let width = width.into();
self.rules = self.rules.base(ProgressDialogPart::Panel, |s| s.width(width));
self
}
pub fn inline(mut self, inline: bool) -> Self {
self.inline = inline;
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(PartStyle) -> PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub fn build(self, theme: &Theme) -> Element {
let detail_open = self.detail.as_ref().is_some_and(|(_, open)| open.get());
let case = Case {
variant: None,
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states,
}
.state(ProgressDialogState::Indeterminate, self.value.is_none())
.state(ProgressDialogState::DetailOpen, detail_open);
let view = ProgressDialogView {
icon: self.icon,
title: self.title,
description: self.description,
label: self.label,
value: self.value,
max: self.max,
status: self.status,
detail: self.detail.as_ref().map(|(detail, _)| detail.clone()),
detail_label: self.detail_label,
detail_open,
actions: self.actions.iter().map(|(label, _)| label.clone()).collect(),
cancel_label: (!self.on_cancel.is_empty()).then_some(self.cancel_label),
inline: self.inline,
recipe: theme
.resolve::<ProgressDialog>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
open: self.open,
format: self.format,
detail_signal: self.detail.map(|(_, open)| open),
handlers: self.actions.into_iter().map(|(_, handler)| handler).collect(),
on_cancel: self.on_cancel,
id: self.id,
key: self.key,
};
match theme.renderer::<ProgressDialog>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<ProgressDialog> for Element {
fn from(dialog: ProgressDialog) -> Self {
dialog.build(¤t_theme())
}
}
pub struct ProgressDialogView {
pub icon: Option<Name>,
pub title: Option<Arc<str>>,
pub description: Option<Arc<str>>,
pub label: Option<Arc<str>>,
pub value: Option<f32>,
pub max: f32,
pub status: Option<Arc<str>>,
pub detail: Option<Arc<str>>,
pub detail_label: Arc<str>,
pub detail_open: bool,
pub actions: Vec<Arc<str>>,
pub cancel_label: Option<Arc<str>>,
pub inline: bool,
pub recipe: ResolvedRecipe,
open: Signal<bool>,
format: Option<ProgressFormat>,
detail_signal: Option<Signal<bool>>,
handlers: Vec<Handler>,
on_cancel: Vec<Handler>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl ProgressDialogView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn open(&self) -> &Signal<bool> {
&self.open
}
pub fn fraction(&self) -> Option<f32> {
let value = self.value?;
Some(if self.max > 0.0 {
value.clamp(0.0, self.max) / self.max
} else {
0.0
})
}
pub fn count(&self) -> Option<String> {
let fraction = self.fraction()?;
Some(match &self.format {
Some(format) => format(self.value?.clamp(0.0, self.max.max(0.0)), self.max),
None => format!("{}%", (fraction * 100.0).round() as i32),
})
}
pub fn header(&self) -> Row {
let mut text = Column::new()
.flex(1.0)
.align_items(Alignment::Stretch)
.part(&self.part(ProgressDialogPart::Text));
for (part, content) in [
(ProgressDialogPart::Title, &self.title),
(ProgressDialogPart::Description, &self.description),
] {
if let Some(content) = content {
text = text.child(wrapping(&self.part(part), content));
}
}
let mut header = Row::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Center)
.part(&self.part(ProgressDialogPart::Header));
if let Some(icon) = self.icon {
header = header.child(glyph(&self.part(ProgressDialogPart::Icon), icon, false));
}
header.child(text)
}
pub fn progress(&self) -> Element {
let progress = match self.value {
Some(value) => Progress::new(value).max(self.max),
None => Progress::indeterminate(),
};
progress
.size(ProgressSize::Md)
.rules(&self.recipe.rules_for(ProgressDialogPart::Progress, ProgressPart::Root))
.build(self.theme())
}
pub fn meta(&self) -> Option<Row> {
let count = self.count();
if count.is_none() && self.status.is_none() {
return None;
}
let mut meta = Row::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Center)
.part(&self.part(ProgressDialogPart::Meta));
if let Some(count) = &count {
let part = self.part(ProgressDialogPart::Count);
meta = meta.child(Row::new().part(&part).child(part.text(count).nowrap()));
}
if let Some(status) = &self.status {
let part = self.part(ProgressDialogPart::Status);
meta = meta.child(
Row::new()
.flex_full(1.0, 1.0, Some(0.0))
.min_width(Dimension::Px(0.0))
.justify(Justify::End)
.part(&part)
.child(part.text(status).nowrap().text_overflow(TextOverflow::Elipsis)),
);
}
Some(meta)
}
pub fn canceller(&self) -> Option<impl Fn() + Send + Sync + Clone + 'static> {
if self.on_cancel.is_empty() {
return None;
}
let handlers = self.on_cancel.clone();
Some(move || {
for handler in &handlers {
handler();
}
})
}
fn button(&self, label: &str, parts: (ProgressDialogPart, ProgressDialogPart), handler: Handler) -> Element {
let rules = self
.recipe
.rules_for(parts.0, ButtonPart::Root)
.extend(&self.recipe.rules_for(parts.1, ButtonPart::Label));
Button::new(label)
.variant(ButtonVariant::Outline)
.size(ButtonSize::Sm)
.rules(&rules)
.on_click(move |_| handler())
.build(self.theme())
}
pub fn actions(&self) -> Row {
let mut actions = Row::new()
.align_items(Alignment::Center)
.overflow_visible()
.part(&self.part(ProgressDialogPart::Actions));
for (label, handler) in self.actions.iter().zip(&self.handlers) {
actions = actions.child(self.button(
label,
(ProgressDialogPart::ActionButton, ProgressDialogPart::ActionLabel),
handler.clone(),
));
}
if let (Some(label), Some(cancel)) = (&self.cancel_label, self.canceller()) {
actions = actions.child(autofocus(self.button(
label,
(ProgressDialogPart::CancelButton, ProgressDialogPart::CancelLabel),
Arc::new(cancel),
)));
}
actions
}
pub fn detail(&self) -> Option<(crate::components::Button, Column)> {
let (detail, open) = (self.detail.as_ref()?, self.detail_signal.as_ref()?);
Some(disclosure(
&self.recipe,
detail_parts(),
&self.detail_label,
detail,
open,
))
}
pub fn footer(&self, trigger: Option<crate::components::Button>) -> Option<Row> {
let actions = (!self.actions.is_empty() || self.cancel_label.is_some()).then(|| self.actions());
if trigger.is_none() && actions.is_none() {
return None;
}
let mut footer = Row::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Center)
.justify(if trigger.is_some() {
Justify::SpaceBetween
} else {
Justify::End
})
.overflow_visible()
.part(&self.part(ProgressDialogPart::Footer));
if let Some(trigger) = trigger {
footer = footer.child(trigger);
}
if let Some(actions) = actions {
footer = footer.child(actions);
}
Some(footer)
}
pub fn content(&self) -> Column {
let mut content = Column::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Stretch)
.overflow_visible()
.part(&self.part(ProgressDialogPart::Content));
if let Some(cancel) = self.canceller() {
content = content.on_key_down(move |event: KeyboardEvent| {
if event.focus_within && event.key == "Escape" && !event.default_prevented() {
event.prevent_default();
cancel();
}
});
}
content
}
pub fn dialog(&self, content: impl Into<Element>) -> Element {
let rules = self
.recipe
.rules_for(ProgressDialogPart::Panel, DialogPart::Panel)
.extend(&self.recipe.rules_for(ProgressDialogPart::Veil, DialogPart::Veil));
let mut dialog = Dialog::new(self.open.clone())
.rules(&rules)
.inline(self.inline)
.dismiss_on_escape(false)
.dismiss_on_veil(false)
.child(content);
if let Some(label) = self.label.as_ref().or(self.title.as_ref()) {
dialog = dialog.label(label);
}
if let Some(description) = &self.description {
dialog = dialog.described(description);
}
if let Some(id) = &self.id {
dialog = dialog.id(id.clone());
}
if let Some(key) = &self.key {
dialog = dialog.key(key.clone());
}
dialog.build(self.theme())
}
pub fn render(self) -> Element {
let mut content = self.content().child(self.header()).child(self.progress());
if let Some(meta) = self.meta() {
content = content.child(meta);
}
let (trigger, panel) = match self.detail() {
Some((trigger, panel)) => (Some(trigger), Some(panel)),
None => (None, None),
};
let mut bottom = Column::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Stretch)
.overflow_visible();
if let Some(footer) = self.footer(trigger) {
bottom = bottom.child(footer);
}
if let Some(panel) = panel {
bottom = bottom.child(panel);
}
self.dialog(content.child(bottom))
}
}
fn detail_parts() -> DisclosureParts {
use ProgressDialogPart::*;
DisclosureParts {
trigger: DetailTrigger.into(),
icon: DetailChevron.into(),
label: DetailLabel.into(),
panel: DetailPanel.into(),
content: Detail.into(),
}
}
impl crate::ui::Styled for ProgressDialog {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for ProgressDialog {
const NAME: &'static str = "progress_dialog";
const ROOT: &'static str = "panel";
type Renderer = ProgressDialogRenderer;
fn parts() -> crate::ui::Declared {
ProgressDialogPart::declared()
}
fn states() -> crate::ui::Declared {
ProgressDialogState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::{Foreground, MutedForeground};
use ProgressDialogPart::*;
let recipe = Recipe::new()
.base(Panel, |s| {
s.width(420.0)
.max_width(Length::Fill)
.padding_x(16.0)
.padding_y(16.0)
})
.base(Content, |s| s.gap(12.0))
.base(Header, |s| s.gap(12.0))
.base(Icon, |s| s.square(20.0).foreground(Foreground))
.base(Text, |s| s.gap(2.0))
.base(Title, |s| {
s.typography(TypographyStyle::Body)
.font_size(15.0)
.weight(FontWeight::SemiBold)
.foreground(Foreground)
})
.base(Description, |s| {
s.typography(TypographyStyle::Description)
.font_size(12.0)
.foreground(MutedForeground)
})
.base(Progress, |s| s.width(Length::Fill))
.base(Meta, |s| s.gap(8.0))
.base(Count, |s| {
s.typography(TypographyStyle::Mono)
.font_size(11.0)
.foreground(MutedForeground)
})
.base(Status, |s| {
s.typography(TypographyStyle::Mono)
.font_size(11.0)
.foreground(MutedForeground)
})
.base(Footer, |s| s.gap(8.0).padding_top(4.0))
.base(Actions, |s| s.gap(8.0))
.base(ActionButton, |s| s.padding_x(14.0))
.base(CancelButton, |s| s.padding_x(14.0));
disclosure_recipe(recipe, &detail_parts(), ProgressDialogState::DetailOpen.into())
}
}