mod action;
pub mod form;
use action::Action;
use form::{list::item::value::Value, Form};
use crate::profile::Profile;
use adw::{
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
AlertDialog, ResponseAppearance,
};
use gtk::prelude::IsA;
use std::rc::Rc;
const HEADING: &str = "Ident";
const BODY: &str = "Select identity certificate";
const RESPONSE_APPLY: (&str, &str) = ("apply", "Apply");
const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel");
pub struct Widget {
pub form: Rc<Form>,
pub gobject: AlertDialog,
}
impl Widget {
pub fn new(profile: Rc<Profile>) -> Self {
let action = Rc::new(Action::new());
let form = Rc::new(Form::new(profile, action.clone()));
let gobject = AlertDialog::builder()
.heading(HEADING)
.body(BODY)
.close_response(RESPONSE_CANCEL.0)
.default_response(RESPONSE_APPLY.0)
.extra_child(&form.gobject)
.build();
gobject.add_responses(&[
RESPONSE_CANCEL,
RESPONSE_APPLY,
]);
gobject.set_response_appearance(RESPONSE_APPLY.0, ResponseAppearance::Suggested);
gobject.set_response_appearance(RESPONSE_CANCEL.0, ResponseAppearance::Destructive);
action.update.connect_activate({
let form = form.clone();
let gobject = gobject.clone();
move || {
gobject.set_response_enabled(RESPONSE_APPLY.0, form.is_valid());
}
});
Self {
form,
gobject,
}
}
pub fn on_apply(&self, callback: impl Fn(Value) + 'static) {
self.gobject.connect_response(Some(RESPONSE_APPLY.0), {
let form = self.form.clone();
move |this, response| {
this.set_response_enabled(response, false);
callback(form.list.selected())
}
});
}
pub fn present(&self, parent: Option<&impl IsA<gtk::Widget>>) {
self.gobject.present(parent)
}
}