use day_core::RNode;
use day_pieces::Decorate;
use day_spec::{PieceKind, kinds};
use jni::Env;
use jni::objects::{Global, JObject};
fn class_for_kind(kind: Option<PieceKind>) -> &'static str {
match kind {
Some(kinds::LABEL) => "android.widget.TextView",
Some(kinds::BUTTON) => "com.google.android.material.button.MaterialButton",
Some(kinds::SLIDER) => "com.google.android.material.slider.Slider",
Some(kinds::DIVIDER) => "com.google.android.material.divider.MaterialDivider",
Some(kinds::TEXT_FIELD) => "com.google.android.material.textfield.TextInputLayout",
_ => "",
}
}
pub fn with_native<R>(
node: RNode,
f: impl FnOnce(&Global<JObject<'static>>, &str, &mut Env) -> R,
) -> Option<R> {
let (handle, kind) = day_core::with_tree(|t| (t.node_handle_any(node), t.node_kind(node)));
let h = handle?.downcast::<crate::AHandle>().ok()?;
let class = class_for_kind(kind);
Some(crate::with_env(|env| f(&h.0, class, env)))
}
pub trait MaterialExt: Decorate + Sized {
fn android(
self,
f: impl FnOnce(&Global<JObject<'static>>, &str, &mut Env) + 'static,
) -> day_pieces::Decorated<Self> {
self.tweak(move |n| {
let _ = with_native(n, f);
})
}
}
impl<P: Decorate> MaterialExt for P {}