use egui::RichText;
use winit::{
dpi::Size,
window::{Icon, WindowAttributes},
};
use crate::create_window;
#[derive(Default, Debug)]
pub struct DialogLine {
line: Vec<RichText>,
}
impl DialogLine {
pub fn new() -> Self {
Self::default()
}
pub fn with(mut self, txt: impl Into<RichText>) -> Self {
self.line.push(txt.into());
self
}
}
#[derive(Default, Debug)]
pub struct Dialog {
texts: Vec<Vec<RichText>>,
buttons: Vec<RichText>,
attrs: WindowAttributes,
}
impl Dialog {
pub fn new() -> Self {
Self::default()
}
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.attrs.title = title.into();
self
}
pub fn with_window_attributes(mut self, attrs: WindowAttributes) -> Self {
self.attrs = attrs;
self
}
pub fn window_attributes(&self) -> &WindowAttributes {
&self.attrs
}
pub fn with_rich_line(mut self, line: DialogLine) -> Self {
self.texts.push(line.line);
self
}
pub fn with_line(mut self, line: impl Into<RichText>) -> Self {
self.texts.push(vec![line.into()]);
self
}
pub fn with_button(mut self, btn: impl Into<RichText>) -> Self {
self.buttons.push(btn.into());
self
}
pub fn with_min_size(mut self, sz: impl Into<Size>) -> Self {
self.attrs.min_inner_size = Some(sz.into());
self
}
pub fn with_size(mut self, sz: impl Into<Size>) -> Self {
self.attrs.inner_size = Some(sz.into());
self
}
pub fn with_max_size(mut self, sz: impl Into<Size>) -> Self {
self.attrs.max_inner_size = Some(sz.into());
self
}
pub fn with_decorations(mut self, decorations: bool) -> Self {
self.attrs.decorations = decorations;
self
}
pub fn with_window_icon(mut self, icon: Option<Icon>) -> Self {
self.attrs.window_icon = icon;
self
}
pub fn with_resizeable(mut self, resizeable: bool) -> Self {
self.attrs.resizable = resizeable;
self
}
pub fn show(self) -> Result<Option<usize>, crate::Error> {
create_window(
|ui| {
for line in &self.texts {
ui.horizontal(|ui| {
for txt in line {
ui.label(txt.clone());
}
});
}
ui.horizontal(|ui| {
for (i, btn) in self.buttons.iter().enumerate() {
if ui.button(btn.clone()).clicked() {
return Some(i);
}
}
None
})
.inner
},
self.attrs,
)
}
}