Skip to content

Commit

Permalink
More builder options for Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Feb 5, 2024
1 parent 59cacfc commit b9663e3
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cursive-core/src/views/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,47 @@ pub enum DialogFocus {
Button(usize),
}

#[cfg(feature = "builder")]
impl crate::builder::Resolvable for DialogFocus {
fn from_config(
config: &crate::builder::Config,
context: &crate::builder::Context,
) -> Result<Self, crate::builder::Error> {
use crate::builder::{Config, Context, Error, Object};
fn _from_config(config: &Config, context: &Context) -> Option<DialogFocus> {
// The config can be either:
// A string: content
// An object: button: i
if let Ok(string) = context.resolve::<String>(&config) {
if string == "Content" || string == "content" {
return Some(DialogFocus::Content);
} else {
return None;
}
}

if let Ok(obj) = context.resolve::<Object>(&config) {
let (key, value) = obj.iter().next()?;

if key != "Button" {
return None;
}
let i = context.resolve(value).ok()?;
return Some(DialogFocus::Button(i));
}

None
}

_from_config(config, context).ok_or_else(|| {
Error::invalid_config(
r#"Expected either the string "Content", or a object with {Button: i}."#,
config,
)
})
}
}

struct ChildButton {
button: LastSizeView<Button>,
offset: Cell<Vec2>,
Expand Down Expand Up @@ -920,11 +961,19 @@ crate::raw_recipe!(Dialog, |config, context| {
dialog.set_title::<String>(title);
}

if let Some(title_position) = context.resolve(&config["title_position"])? {
dialog.set_title_position(title_position);
}

let content: Option<BoxedView> = context.resolve(&config["content"])?;
if let Some(content) = content {
dialog.set_content(content);
}

if let Some(padding) = context.resolve(&config["padding"])? {
dialog.set_padding(padding);
}

struct Btn {
key: String,
value: std::sync::Arc<dyn Fn(&mut Cursive) + Send + Sync>,
Expand Down Expand Up @@ -953,6 +1002,10 @@ crate::raw_recipe!(Dialog, |config, context| {
dialog.add_button_with_cb(btn.key, btn.value);
}

if let Some(focus) = context.resolve(&config["focus"])? {
dialog.set_focus(focus);
}

Ok(dialog)
});

Expand Down

0 comments on commit b9663e3

Please sign in to comment.