use gpui::{
App, Context, Entity, FontWeight, Render, Window, WindowBounds, WindowKind, WindowOptions,
canvas, div, prelude::*, px, size,
};
use crate::containers::DialogButton;
use crate::controls::{ButtonVariant, text_field};
use crate::keyboard::{self, Dismiss};
use crate::lighting;
use crate::palette::Palette;
use crate::state::{ControlHost, ControlState};
use crate::text_input::InputStyle;
use crate::text_input::{self, TextInput};
const WIDTH: f32 = 420.0;
const PADDING: f32 = 20.0;
const GAP: f32 = 14.0;
const TITLE_HEIGHT: f32 = 22.0;
const LINE_HEIGHT: f32 = 18.0;
const FIELD_HEIGHT: f32 = 32.0;
const BUTTON_HEIGHT: f32 = 32.0;
const CHARS_PER_LINE: f32 = 55.0;
fn estimated_height(message: &str, field: bool) -> f32 {
let lines = message
.split('\n')
.map(|line| {
(line.chars().count() as f32 / CHARS_PER_LINE)
.ceil()
.max(1.0)
})
.sum::<f32>();
let message = if message.is_empty() {
0.0
} else {
lines * LINE_HEIGHT + GAP
};
let field = if field { FIELD_HEIGHT + GAP } else { 0.0 };
2.0 * PADDING + TITLE_HEIGHT + GAP + message + field + BUTTON_HEIGHT
}
enum Kind {
Prompt(String),
Confirm,
Alert,
Choose,
}
enum Outcome {
Cancelled,
Text(String),
Button(usize),
}
type Answer = Box<dyn FnOnce(Outcome, &mut App)>;
struct Dialog {
controls: ControlState,
palette: Palette,
title: String,
message: String,
buttons: Vec<String>,
cancel: bool,
input: Option<Entity<TextInput>>,
answer: Option<Answer>,
}
impl ControlHost for Dialog {
fn control_state(&self) -> &ControlState {
&self.controls
}
fn control_state_mut(&mut self) -> &mut ControlState {
&mut self.controls
}
}
impl Dialog {
fn finish(&mut self, chosen: Option<usize>, window: &mut Window, cx: &mut Context<Self>) {
let Some(answer) = self.answer.take() else {
return;
};
let outcome = match chosen {
None => Outcome::Cancelled,
Some(button) => match &self.input {
Some(input) => Outcome::Text(input.read(cx).text()),
None => Outcome::Button(button),
},
};
window.remove_window();
answer(outcome, cx);
}
fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.finish(Some(self.buttons.len().saturating_sub(1)), window, cx);
}
}
impl Render for Dialog {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let palette = self.palette;
let input = self.input.clone();
let title = self.title.clone();
let message = self.message.clone();
let last = self.buttons.len().saturating_sub(1);
let mut buttons: Vec<DialogButton<Dialog>> = Vec::with_capacity(self.buttons.len() + 1);
if self.cancel {
buttons.push(DialogButton::new(
"dialog-cancel",
"Cancel",
ButtonVariant::Soft,
|this: &mut Dialog, window, cx| this.finish(None, window, cx),
));
}
for (n, label) in self.buttons.iter().enumerate() {
buttons.push(DialogButton::new(
("dialog-button", n),
label.clone(),
if n == last {
ButtonVariant::Primary
} else {
ButtonVariant::Soft
},
move |this: &mut Dialog, window, cx| this.finish(Some(n), window, cx),
));
}
let footer = div()
.flex()
.flex_wrap()
.justify_end()
.gap(px(8.0))
.children(buttons.into_iter().map(|spec| {
let on_click = spec.on_click;
div()
.min_w(px(110.0))
.flex_none()
.child(crate::controls::button(
spec.id,
&spec.label,
spec.variant,
spec.enabled,
palette,
cx,
move |this, window, cx| on_click(this, window, cx),
))
}));
let fit = canvas(
|bounds, window, _| {
let wanted = f32::from(bounds.size.height) + 2.0 * PADDING;
if (f32::from(window.viewport_size().height) - wanted).abs() > 0.5 {
window.resize(size(px(WIDTH), px(wanted)));
}
},
|_, _, _, _| {},
)
.absolute()
.top_0()
.left_0()
.size_full();
keyboard::root(div().id("dialog-window"), self, cx)
.size_full()
.p(px(PADDING))
.flex()
.flex_col()
.font_family(crate::typography::ui_font())
.text_size(px(13.0))
.text_color(palette.text_primary)
.bg(palette.field_surface)
.on_action(cx.listener(|this, _: &Dismiss, window, cx| {
this.finish(None, window, cx);
}))
.on_action(cx.listener(|this, _: &text_input::Enter, window, cx| {
this.submit(window, cx);
}))
.on_key_down(cx.listener(|this, event: &gpui::KeyDownEvent, window, cx| {
if keyboard::key(event) == Some(keyboard::Key::Activate) {
cx.stop_propagation();
this.submit(window, cx);
}
}))
.child(
div()
.relative()
.flex_none()
.w_full()
.flex()
.flex_col()
.gap(px(GAP))
.child(fit)
.child(
div()
.text_size(px(17.0))
.font_weight(FontWeight::SEMIBOLD)
.child(title),
)
.when(!message.is_empty(), |el| {
el.child(div().text_color(palette.text_secondary).child(message))
})
.children(
input
.as_ref()
.map(|input| text_field(input, palette, window, cx)),
)
.child(footer),
)
.shadow(lighting::panel(palette.is_dark))
}
}
pub fn prompt(
cx: &mut App,
palette: Palette,
title: impl Into<String>,
message: impl Into<String>,
initial: impl Into<String>,
button: impl Into<String>,
on_answer: impl FnOnce(Option<String>, &mut App) + 'static,
) {
open(
cx,
palette,
title.into(),
message.into(),
Kind::Prompt(initial.into()),
vec![button.into()],
Box::new(move |outcome, cx| {
on_answer(
match outcome {
Outcome::Text(text) => Some(text),
_ => None,
},
cx,
)
}),
);
}
pub fn confirm(
cx: &mut App,
palette: Palette,
title: impl Into<String>,
message: impl Into<String>,
button: impl Into<String>,
on_answer: impl FnOnce(bool, &mut App) + 'static,
) {
open(
cx,
palette,
title.into(),
message.into(),
Kind::Confirm,
vec![button.into()],
Box::new(move |outcome, cx| on_answer(matches!(outcome, Outcome::Button(_)), cx)),
);
}
pub fn choose(
cx: &mut App,
palette: Palette,
title: impl Into<String>,
message: impl Into<String>,
buttons: Vec<String>,
on_answer: impl FnOnce(Option<usize>, &mut App) + 'static,
) {
open(
cx,
palette,
title.into(),
message.into(),
Kind::Choose,
buttons,
Box::new(move |outcome, cx| {
on_answer(
match outcome {
Outcome::Button(n) => Some(n),
_ => None,
},
cx,
)
}),
);
}
pub fn alert(cx: &mut App, palette: Palette, title: impl Into<String>, message: impl Into<String>) {
open(
cx,
palette,
title.into(),
message.into(),
Kind::Alert,
vec!["OK".into()],
Box::new(|_, _| {}),
);
}
fn open(
cx: &mut App,
palette: Palette,
title: String,
message: String,
kind: Kind,
buttons: Vec<String>,
answer: Answer,
) {
let cancel = !matches!(kind, Kind::Alert);
let input = match kind {
Kind::Prompt(initial) => Some(cx.new(|cx| {
let mut input = TextInput::new(cx, "", false, InputStyle::from_palette(palette, 13.0));
input.set_text(&initial, cx);
input
})),
Kind::Confirm | Kind::Alert | Kind::Choose => None,
};
let height = estimated_height(&message, input.is_some());
let options = WindowOptions {
window_bounds: Some(WindowBounds::centered(size(px(WIDTH), px(height)), cx)),
kind: WindowKind::Dialog,
titlebar: None,
is_resizable: false,
is_minimizable: false,
..Default::default()
};
let focus = input
.as_ref()
.map(|input| input.read(cx).focus_handle.clone());
let mut answer = Some(answer);
let result = cx.open_window(options, |window, cx| {
let controls = ControlState::default();
match &focus {
Some(focus) => window.focus(focus, cx),
None => controls.focus_root(window, cx),
}
cx.new(|_| Dialog {
controls,
palette,
title,
message,
buttons,
cancel,
input,
answer: answer.take(),
})
});
if result.is_err()
&& let Some(answer) = answer
{
answer(Outcome::Cancelled, cx);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_first_guess_counts_lines_and_rows() {
let chrome = 2.0 * PADDING + TITLE_HEIGHT + GAP + BUTTON_HEIGHT;
assert_eq!(estimated_height("", false), chrome);
assert_eq!(estimated_height("Short", false), chrome + LINE_HEIGHT + GAP);
let long = "a".repeat(120);
assert_eq!(
estimated_height(&long, false),
chrome + 3.0 * LINE_HEIGHT + GAP
);
assert_eq!(
estimated_height("One\nTwo", true),
chrome + 2.0 * LINE_HEIGHT + GAP + FIELD_HEIGHT + GAP
);
}
}