mod action;
pub mod form;
use action::Action as WidgetAction;
use form::{list::item::value::Value, Form};
use crate::{
app::browser::{action::Action as BrowserAction, window::action::Action as WindowAction},
Profile,
};
use adw::{
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
AlertDialog, ResponseAppearance,
};
use gtk::{glib::Uri, prelude::IsA};
use std::rc::Rc;
const HEADING: &str = "Identity";
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 alert_dialog: AlertDialog,
}
impl Widget {
pub fn new(
action: (Rc<BrowserAction>, Rc<WindowAction>),
profile: Rc<Profile>,
auth_uri: Uri,
) -> Self {
let widget_action = Rc::new(WidgetAction::new());
let form = Rc::new(Form::new(
(action.0.clone(), action.1.clone(), widget_action.clone()),
profile,
auth_uri,
));
let alert_dialog = AlertDialog::builder()
.heading(HEADING)
.body(BODY)
.close_response(RESPONSE_CANCEL.0)
.default_response(RESPONSE_APPLY.0)
.extra_child(&form.g_box)
.build();
alert_dialog.add_responses(&[
RESPONSE_CANCEL,
RESPONSE_APPLY,
]);
alert_dialog.set_response_appearance(RESPONSE_APPLY.0, ResponseAppearance::Suggested);
widget_action.update.connect_activate({
let form = form.clone();
let alert_dialog = alert_dialog.clone();
move || {
form.update();
alert_dialog.set_response_enabled(RESPONSE_APPLY.0, form.is_applicable());
}
});
widget_action.update.activate();
Self {
form,
alert_dialog,
}
}
pub fn on_apply(&self, callback: impl Fn(Value) + 'static) {
self.alert_dialog.connect_response(Some(RESPONSE_APPLY.0), {
let form = self.form.clone();
move |this, response| {
this.set_response_enabled(response, false);
callback(form.list.selected().value_enum())
}
});
}
pub fn on_cancel(&self, callback: impl Fn() + 'static) {
self.alert_dialog
.connect_response(Some(RESPONSE_CANCEL.0), move |this, response| {
this.set_response_enabled(response, false);
callback()
});
}
pub fn present(&self, parent: Option<&impl IsA<gtk::Widget>>) {
self.alert_dialog.present(parent)
}
}