use std::sync::Arc;
use super::{
button::{Button, ButtonPart, ButtonSize, ButtonVariant},
icons,
message_box::{autofocus, wrapping},
names,
part_style::{Length, PartStyle},
popover::{Popover, PopoverPart},
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::{IconChoice, glyph, shown_icon},
theme::{Themed, current_theme},
};
use crate::{
app::{
ctx::{CollisionStrategy, Placement},
events::KeyboardEvent,
theme::{PaletteColor, Theme, TypographyStyle},
},
components::{Column, Row},
core::{ElementRef, Signal},
layout::{Alignment, layout_kind::Justify, text_style::FontWeight},
node::{Element, SpacingValue, dimension::Dimension},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PopconfirmPart {
Panel,
Body,
Content,
Header,
Icon,
Text,
Title,
Description,
Footer,
CancelButton,
CancelLabel,
ConfirmButton,
ConfirmLabel,
}
names!(PopconfirmPart {
Panel => "panel",
Body => "body",
Content => "content",
Header => "header",
Icon => "icon",
Text => "text",
Title => "title",
Description => "description",
Footer => "footer",
CancelButton => "cancel_button",
CancelLabel => "cancel_label",
ConfirmButton => "confirm_button",
ConfirmLabel => "confirm_label",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PopconfirmState {
Destructive,
}
names!(PopconfirmState {
Destructive => "destructive",
});
pub type PopconfirmRenderer = Arc<dyn Fn(PopconfirmView) -> Element + Send + Sync>;
type Handler = Arc<dyn Fn() + Send + Sync>;
pub struct Popconfirm {
open: Signal<bool>,
anchor: ElementRef,
trigger: Option<Element>,
icon: Option<IconChoice>,
title: Option<Arc<str>>,
description: Option<Arc<str>>,
confirm: (Arc<str>, Option<Handler>),
cancel: (Arc<str>, Option<Handler>),
destructive: bool,
placement: Placement,
offset: Option<SpacingValue>,
collision: Option<CollisionStrategy>,
width: Option<Length>,
label: Option<Arc<str>>,
inline: bool,
dismiss_on_escape: bool,
dismiss_on_outside_press: bool,
on_dismiss: Vec<Handler>,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Popconfirm {
pub fn new(open: Signal<bool>, anchor: impl Into<ElementRef>) -> Self {
Self {
open,
anchor: anchor.into(),
trigger: None,
icon: None,
title: None,
description: None,
confirm: (super::current_locale().common.yes.clone(), None),
cancel: (super::current_locale().common.no.clone(), None),
destructive: false,
placement: Placement::BottomStart,
offset: None,
collision: None,
width: None,
label: None,
inline: false,
dismiss_on_escape: true,
dismiss_on_outside_press: true,
on_dismiss: Vec::new(),
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn trigger(mut self, trigger: impl Into<Element>) -> Self {
self.trigger = Some(trigger.into());
self
}
pub fn icon(mut self, icon: impl Into<IconChoice>) -> Self {
self.icon = Some(icon.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 confirm(mut self, label: &str, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.confirm = (Arc::from(label), Some(Arc::new(handler)));
self
}
pub fn cancel(mut self, label: &str, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.cancel = (Arc::from(label), Some(Arc::new(handler)));
self
}
pub fn destructive(mut self, destructive: bool) -> Self {
self.destructive = destructive;
self
}
pub fn placement(mut self, placement: Placement) -> Self {
self.placement = placement;
self
}
pub fn offset(mut self, offset: impl Into<SpacingValue>) -> Self {
self.offset = Some(offset.into());
self
}
pub fn collision(mut self, collision: CollisionStrategy) -> Self {
self.collision = Some(collision);
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = Some(width.into());
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn on_dismiss(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_dismiss.push(Arc::new(handler));
self
}
pub fn dismiss_on_escape(mut self, dismiss: bool) -> Self {
self.dismiss_on_escape = dismiss;
self
}
pub fn dismiss_on_outside_press(mut self, dismiss: bool) -> Self {
self.dismiss_on_outside_press = dismiss;
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 case = Case {
variant: None,
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states,
}
.state(PopconfirmState::Destructive, self.destructive);
let mut rules = Recipe::new();
if let Some(width) = self.width {
rules = rules.base(PopconfirmPart::Panel, |s| s.width(width));
}
let rules = rules.extend(&self.rules);
let recipe = theme
.resolve::<Popconfirm>(&case, Some(&rules))
.owned_by(self.id.as_ref());
let rules = recipe
.rules_for(PopconfirmPart::Panel, PopoverPart::Panel)
.extend(&recipe.rules_for(PopconfirmPart::Body, PopoverPart::Body));
let view = PopconfirmView {
icon: shown_icon(self.icon, icons::WARNING),
title: self.title.clone(),
description: self.description.clone(),
confirm_label: self.confirm.0,
cancel_label: self.cancel.0,
destructive: self.destructive,
recipe,
open: self.open.clone(),
on_confirm: self.confirm.1,
on_cancel: self.cancel.1.clone(),
on_dismiss: self.on_dismiss.clone(),
dismiss_on_escape: self.dismiss_on_escape,
};
let content = match theme.renderer::<Popconfirm>() {
Some(renderer) => renderer(view),
None => view.render(),
};
let mut popover = Popover::new(self.open, self.anchor)
.alert(true)
.placement(self.placement)
.inline(self.inline)
.dismiss_on_escape(self.dismiss_on_escape)
.dismiss_on_outside_press(self.dismiss_on_outside_press)
.rules(&rules)
.child(content);
let (on_cancel, on_dismiss) = (self.cancel.1, self.on_dismiss);
if on_cancel.is_some() || !on_dismiss.is_empty() {
popover = popover.on_dismiss(move || dismissed(on_cancel.as_ref(), &on_dismiss));
}
if let Some(trigger) = self.trigger {
popover = popover.trigger(trigger);
}
if let Some(offset) = self.offset {
popover = popover.offset(offset);
}
if let Some(collision) = self.collision {
popover = popover.collision(collision);
}
if let Some(name) = self.label.as_ref().or(self.title.as_ref()) {
popover = popover.label(name);
}
if let Some(description) = &self.description {
popover = popover.described(description);
}
if let Some(id) = self.id {
popover = popover.id(id);
}
if let Some(key) = self.key {
popover = popover.key(key);
}
popover.build(theme)
}
}
impl From<Popconfirm> for Element {
fn from(popconfirm: Popconfirm) -> Self {
popconfirm.build(¤t_theme())
}
}
pub struct PopconfirmView {
pub icon: Option<Name>,
pub title: Option<Arc<str>>,
pub description: Option<Arc<str>>,
pub confirm_label: Arc<str>,
pub cancel_label: Arc<str>,
pub destructive: bool,
pub recipe: ResolvedRecipe,
open: Signal<bool>,
on_confirm: Option<Handler>,
on_cancel: Option<Handler>,
on_dismiss: Vec<Handler>,
dismiss_on_escape: bool,
}
impl PopconfirmView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn confirmer(&self) -> impl Fn() + Send + Sync + Clone + 'static {
answer(self.open.clone(), self.on_confirm.clone())
}
pub fn canceller(&self) -> impl Fn() + Send + Sync + Clone + 'static {
answer(self.open.clone(), self.on_cancel.clone())
}
pub fn dismisser(&self) -> impl Fn() + Send + Sync + Clone + 'static {
let (open, on_cancel, on_dismiss) = (self.open.clone(), self.on_cancel.clone(), self.on_dismiss.clone());
move || {
open.set(false);
dismissed(on_cancel.as_ref(), &on_dismiss);
}
}
fn confirm_takes_focus(&self) -> bool {
!self.destructive
}
pub fn header(&self) -> Row {
let mut text = Column::new()
.flex(1.0)
.align_items(Alignment::Stretch)
.part(&self.part(PopconfirmPart::Text));
for (part, content) in [
(PopconfirmPart::Title, &self.title),
(PopconfirmPart::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::Start)
.part(&self.part(PopconfirmPart::Header));
if let Some(icon) = self.icon {
header = header.child(glyph(&self.part(PopconfirmPart::Icon), icon, false));
}
header.child(text)
}
pub fn cancel(&self) -> Element {
let rules = self
.recipe
.rules_for(PopconfirmPart::CancelButton, ButtonPart::Root)
.extend(&self.recipe.rules_for(PopconfirmPart::CancelLabel, ButtonPart::Label));
let cancel = self.canceller();
let button = Button::new(&self.cancel_label)
.variant(ButtonVariant::Outline)
.size(ButtonSize::Xs)
.rules(&rules)
.on_click(move |_| cancel())
.build(self.theme());
if self.confirm_takes_focus() {
button
} else {
autofocus(button)
}
}
pub fn confirm(&self) -> Element {
let rules = self
.recipe
.rules_for(PopconfirmPart::ConfirmButton, ButtonPart::Root)
.extend(&self.recipe.rules_for(PopconfirmPart::ConfirmLabel, ButtonPart::Label));
let confirm = self.confirmer();
let button = Button::new(&self.confirm_label)
.variant(if self.destructive {
ButtonVariant::Destructive
} else {
ButtonVariant::Primary
})
.size(ButtonSize::Xs)
.rules(&rules)
.on_click(move |_| confirm())
.build(self.theme());
if self.confirm_takes_focus() {
autofocus(button)
} else {
button
}
}
pub fn footer(&self) -> Row {
Row::new()
.width(Dimension::Pct(100.0))
.wrap()
.justify(Justify::End)
.align_items(Alignment::Center)
.overflow_visible()
.part(&self.part(PopconfirmPart::Footer))
.child(self.cancel())
.child(self.confirm())
}
pub fn content(&self) -> Column {
let content = Column::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Stretch)
.overflow_visible()
.part(&self.part(PopconfirmPart::Content));
if !self.dismiss_on_escape {
return content;
}
let dismiss = self.dismisser();
content.on_key_down(move |event: KeyboardEvent| {
if event.focus_within && event.key == "Escape" && !event.default_prevented() {
event.prevent_default();
dismiss();
}
})
}
pub fn render(self) -> Element {
self.content().child(self.header()).child(self.footer()).into()
}
}
fn answer(open: Signal<bool>, handler: Option<Handler>) -> impl Fn() + Send + Sync + Clone + 'static {
move || {
open.set(false);
if let Some(handler) = &handler {
handler();
}
}
}
fn dismissed(on_cancel: Option<&Handler>, on_dismiss: &[Handler]) {
if let Some(on_cancel) = on_cancel {
on_cancel();
}
for handler in on_dismiss {
handler();
}
}
impl crate::ui::Styled for Popconfirm {
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 Popconfirm {
const NAME: &'static str = "popconfirm";
type Renderer = PopconfirmRenderer;
fn parts() -> crate::ui::Declared {
PopconfirmPart::declared()
}
fn states() -> crate::ui::Declared {
PopconfirmState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::{Destructive, Foreground, MutedForeground, Warning};
use PopconfirmPart::*;
Recipe::new()
.base(Panel, |s| s.width(250.0).padding_x(8.0).padding_y(8.0))
.base(Body, |s| s.padding_x(4.0).padding_y(4.0))
.base(Content, |s| s.gap(12.0))
.base(Header, |s| s.gap(8.0))
.base(Icon, |s| s.square(16.0).foreground(Warning))
.base(Text, |s| s.gap(2.0))
.base(Title, |s| {
s.typography(TypographyStyle::Body)
.font_size(13.0)
.weight(FontWeight::Medium)
.foreground(Foreground)
})
.base(Description, |s| {
s.typography(TypographyStyle::Description)
.font_size(12.0)
.foreground(MutedForeground)
})
.base(Footer, |s| s.gap(8.0))
.base(CancelLabel, |s| s.font_size(13.0))
.base(ConfirmLabel, |s| s.font_size(13.0))
.state(PopconfirmState::Destructive, Icon, |s| s.foreground(Destructive))
}
}