use crate::{
enums::*,
traits::{GroupExt, WidgetBase},
};
use wasm_bindgen::JsCast;
use wasm_bindgen::UnwrapThrowExt;
pub trait WidgetExt: WidgetBase {
fn add_callback<F: 'static + FnMut(&Self)>(&self, event: Event, mut cb: F)
where
Self: Sized,
{
self.inner().add_callback(event, move |w| unsafe {
cb(&Self::from_widget(w));
});
}
fn set_id(&self, id: &str) {
self.inner().set_id(id);
}
fn with_id(self, id: &str) -> Self
where
Self: Sized,
{
self.set_id(id);
self
}
fn set_class(&self, class: &str) {
self.inner().set_class_name(class);
}
fn with_class(self, class: &str) -> Self
where
Self: Sized,
{
self.set_class(class);
self
}
fn class(&self) -> String {
self.inner().class_name()
}
fn set_label(&self, title: &str) {
self.inner().set_text_content(Some(title))
}
fn with_label(self, title: &str) -> Self
where
Self: Sized,
{
self.set_label(title);
self
}
fn set_pos(&self, x: i32, y: i32) {
let inner = self.inner();
inner.set_style(Style::Left, &x.to_string());
inner.set_style(Style::Top, &y.to_string());
}
fn set_size(&self, width: i32, height: i32) {
let inner = self.inner();
inner.set_style(Style::Width, &width.to_string());
inner.set_style(Style::Height, &height.to_string());
}
fn with_size(self, w: i32, h: i32) -> Self
where
Self: Sized,
{
self.set_size(w, h);
self
}
fn redraw(&self) {
self.hide();
self.show();
}
fn show(&self) {
self.inner().set_style(Style::Display, "block");
}
fn hide(&self) {
self.inner().set_style(Style::Display, "none");
}
fn x(&self) -> i32 {
self.inner().style(Style::Left).parse().unwrap_throw()
}
fn y(&self) -> i32 {
self.inner().style(Style::Top).parse().unwrap_throw()
}
fn w(&self) -> i32 {
self.inner().client_width()
}
fn h(&self) -> i32 {
self.inner().client_height()
}
fn label(&self) -> Option<String> {
self.inner().text_content()
}
fn color(&self) -> Color {
Color::from_hex_str(&self.inner().style(Style::BackgroundColor)).unwrap_throw()
}
fn set_color(&self, color: Color) {
self.inner()
.set_style(Style::BackgroundColor, &color.to_str())
}
fn set_label_color(&self, color: Color) {
self.inner().set_style(Style::Color, &color.to_str())
}
fn label_color(&self) -> Color {
Color::from_hex_str(&self.inner().style(Style::Color)).unwrap_throw()
}
fn set_label_size(&self, size: u8) {
self.inner().set_style(Style::FontSize, &size.to_string());
}
fn label_size(&self) -> u8 {
self.inner().style(Style::FontSize).parse().unwrap_throw()
}
fn set_label_font(&self, font: &str) {
self.inner().set_style(Style::Font, font);
}
fn label_font(&self) -> String {
self.inner().style(Style::Font)
}
fn do_callback(&self, event: Event) {
let c = self.inner();
let elem: &web_sys::EventTarget = c.dyn_ref().unwrap_throw();
elem.dispatch_event(&web_sys::Event::new(&event.to_str()).unwrap_throw())
.unwrap_throw();
}
fn set_margin(&self, size: i32) {
self.inner().set_style(Style::Margin, &size.to_string());
}
fn margin(&self) -> i32 {
self.inner().style(Style::Margin).parse().unwrap_throw()
}
fn set_padding(&self, size: i32) {
self.inner().set_style(Style::Padding, &size.to_string());
}
fn padding(&self) -> i32 {
self.inner().style(Style::Padding).parse().unwrap_throw()
}
fn set_frame(&self, frame: FrameType) {
match frame {
FrameType::FlatBox => {
self.inner().set_style(Style::Border, "none");
self.inner().set_style(Style::BorderRadius, "0px");
}
FrameType::RFlatBox => {
self.inner().set_style(Style::Border, "none");
self.inner().set_style(Style::BorderRadius, "8px");
}
FrameType::RoundBox => {
self.inner().set_style(Style::Border, "none");
self.inner().set_style(Style::BorderRadius, "50%");
}
FrameType::FlatFrame => {
self.inner().set_style(Style::BorderRadius, "0px");
}
FrameType::RFlatFrame => {
self.inner().set_style(Style::BorderRadius, "8px");
}
FrameType::RoundFrame => {
self.inner().set_style(Style::BorderRadius, "50%");
}
}
}
fn parent(&self) -> Option<Box<dyn GroupExt>> {
if let Some(parent) = self.inner().parent_element() {
Some(Box::new(unsafe {
crate::group::Group::from_widget(&crate::widget::Widget::from_elem(parent))
}))
} else {
None
}
}
}