use std::vec::Vec;
use azul_core::{
callbacks::{CoreCallbackData, Update},
dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec, NodeType, TabIndex},
refany::RefAny,
resources::{ImageRef, OptionImageRef},
};
#[allow(clippy::wildcard_imports)] use azul_css::{
dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec},
props::{
basic::{
color::ColorU,
font::{StyleFontFamily, StyleFontFamilyVec},
*,
},
layout::*,
property::{CssProperty, *},
style::*,
},
system::SystemFontType,
*,
};
use crate::callbacks::{Callback, CallbackInfo};
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum ButtonType {
#[default]
Default,
Primary,
Secondary,
Success,
Danger,
Warning,
Info,
Link,
}
impl ButtonType {
#[must_use] pub const fn class_name(&self) -> &'static str {
match self {
Self::Default => "__azul-btn-default",
Self::Primary => "__azul-btn-primary",
Self::Secondary => "__azul-btn-secondary",
Self::Success => "__azul-btn-success",
Self::Danger => "__azul-btn-danger",
Self::Warning => "__azul-btn-warning",
Self::Info => "__azul-btn-info",
Self::Link => "__azul-btn-link",
}
}
}
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub struct Button {
pub label: AzString,
pub image: OptionImageRef,
pub button_type: ButtonType,
pub container_style: CssPropertyWithConditionsVec,
pub label_style: CssPropertyWithConditionsVec,
pub image_style: CssPropertyWithConditionsVec,
pub on_click: OptionButtonOnClick,
}
pub type ButtonOnClickCallbackType = extern "C" fn(RefAny, CallbackInfo) -> Update;
impl_widget_callback!(
ButtonOnClick,
OptionButtonOnClick,
ButtonOnClickCallback,
ButtonOnClickCallbackType
);
azul_core::impl_managed_callback! {
wrapper: ButtonOnClickCallback,
info_ty: CallbackInfo,
return_ty: Update,
default_ret: Update::DoNothing,
invoker_static: BUTTON_ON_CLICK_INVOKER,
invoker_ty: AzButtonOnClickCallbackInvoker,
thunk_fn: az_button_on_click_callback_thunk,
setter_fn: AzApp_setButtonOnClickCallbackInvoker,
from_handle_fn: AzButtonOnClickCallback_createFromHostHandle,
}
const fn get_button_colors(button_type: ButtonType) -> (ColorU, ColorU, ColorU) {
match button_type {
ButtonType::Default => (
ColorU::rgb(248, 249, 250), ColorU::rgb(233, 236, 239), ColorU::rgb(218, 222, 226), ),
ButtonType::Primary => (
ColorU::bootstrap_primary(),
ColorU::bootstrap_primary_hover(),
ColorU::bootstrap_primary_active(),
),
ButtonType::Secondary => (
ColorU::bootstrap_secondary(),
ColorU::bootstrap_secondary_hover(),
ColorU::bootstrap_secondary_active(),
),
ButtonType::Success => (
ColorU::bootstrap_success(),
ColorU::bootstrap_success_hover(),
ColorU::bootstrap_success_active(),
),
ButtonType::Danger => (
ColorU::bootstrap_danger(),
ColorU::bootstrap_danger_hover(),
ColorU::bootstrap_danger_active(),
),
ButtonType::Warning => (
ColorU::bootstrap_warning(),
ColorU::bootstrap_warning_hover(),
ColorU::bootstrap_warning_active(),
),
ButtonType::Info => (
ColorU::bootstrap_info(),
ColorU::bootstrap_info_hover(),
ColorU::bootstrap_info_active(),
),
ButtonType::Link => (
ColorU::TRANSPARENT,
ColorU::TRANSPARENT,
ColorU::TRANSPARENT,
),
}
}
const fn get_button_text_color(button_type: ButtonType) -> ColorU {
match button_type {
ButtonType::Default => ColorU::rgb(33, 37, 41), ButtonType::Warning => ColorU::BLACK, ButtonType::Link => ColorU::bootstrap_link(), _ => ColorU::WHITE, }
}
fn build_button_container_style(button_type: ButtonType) -> Vec<CssPropertyWithConditions> {
return alloc::vec![
CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Block)),
CssPropertyWithConditions::simple(CssProperty::const_padding_top(LayoutPaddingTop::const_px(6))),
CssPropertyWithConditions::simple(CssProperty::const_padding_bottom(LayoutPaddingBottom::const_px(6))),
];
#[allow(unreachable_code)]
let (bg_normal, bg_hover, bg_active) = get_button_colors(button_type);
let text_color = get_button_text_color(button_type);
let focus_outline_color = ColorU::bootstrap_primary();
let mut props = Vec::with_capacity(40);
props.push(CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::InlineFlex)));
props.push(CssPropertyWithConditions::simple(CssProperty::const_flex_direction(LayoutFlexDirection::Row)));
props.push(CssPropertyWithConditions::simple(CssProperty::const_justify_content(LayoutJustifyContent::Center)));
props.push(CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)));
props.push(CssPropertyWithConditions::simple(CssProperty::align_self(LayoutAlignSelf::Start)));
props.push(CssPropertyWithConditions::simple(CssProperty::const_cursor(StyleCursor::Pointer)));
props.push(CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(0))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor { inner: text_color })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_padding_top(LayoutPaddingTop::const_px(6))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_padding_bottom(LayoutPaddingBottom::const_px(6))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_padding_left(LayoutPaddingLeft::const_px(12))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_padding_right(LayoutPaddingRight::const_px(12))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_top_left_radius(StyleBorderTopLeftRadius::const_px(4))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_top_right_radius(StyleBorderTopRightRadius::const_px(4))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_bottom_left_radius(StyleBorderBottomLeftRadius::const_px(4))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_bottom_right_radius(StyleBorderBottomRightRadius::const_px(4))));
if button_type == ButtonType::Link {
props.push(CssPropertyWithConditions::simple(CssProperty::const_background_content(
StyleBackgroundContentVec::from_const_slice(&[StyleBackgroundContent::Color(ColorU::TRANSPARENT)]),
)));
props.push(CssPropertyWithConditions::on_hover(CssProperty::TextDecoration(StyleTextDecoration::Underline.into())));
} else {
props.push(CssPropertyWithConditions::simple(CssProperty::const_background_content(
StyleBackgroundContentVec::from_vec(vec![StyleBackgroundContent::Color(bg_normal)]),
)));
let border_color = if button_type == ButtonType::Default {
ColorU::rgb(206, 212, 218)
} else {
bg_normal
};
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_top_width(LayoutBorderTopWidth::const_px(1))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_bottom_width(LayoutBorderBottomWidth::const_px(1))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_left_width(LayoutBorderLeftWidth::const_px(1))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_right_width(LayoutBorderRightWidth::const_px(1))));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_top_style(StyleBorderTopStyle { inner: BorderStyle::Solid })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_bottom_style(StyleBorderBottomStyle { inner: BorderStyle::Solid })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_left_style(StyleBorderLeftStyle { inner: BorderStyle::Solid })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_right_style(StyleBorderRightStyle { inner: BorderStyle::Solid })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_top_color(StyleBorderTopColor { inner: border_color })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_bottom_color(StyleBorderBottomColor { inner: border_color })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_left_color(StyleBorderLeftColor { inner: border_color })));
props.push(CssPropertyWithConditions::simple(CssProperty::const_border_right_color(StyleBorderRightColor { inner: border_color })));
props.push(CssPropertyWithConditions::on_hover(CssProperty::BackgroundContent(
StyleBackgroundContentVec::from_vec(vec![StyleBackgroundContent::Color(bg_hover)]).into(),
)));
if button_type == ButtonType::Default {
let hover_border = ColorU::rgb(173, 181, 189);
props.push(CssPropertyWithConditions::on_hover(CssProperty::BorderTopColor(StyleBorderTopColor { inner: hover_border }.into())));
props.push(CssPropertyWithConditions::on_hover(CssProperty::BorderBottomColor(StyleBorderBottomColor { inner: hover_border }.into())));
props.push(CssPropertyWithConditions::on_hover(CssProperty::BorderLeftColor(StyleBorderLeftColor { inner: hover_border }.into())));
props.push(CssPropertyWithConditions::on_hover(CssProperty::BorderRightColor(StyleBorderRightColor { inner: hover_border }.into())));
}
props.push(CssPropertyWithConditions::on_active(CssProperty::BackgroundContent(
StyleBackgroundContentVec::from_vec(vec![StyleBackgroundContent::Color(bg_active)]).into(),
)));
props.push(CssPropertyWithConditions::on_focus(CssProperty::BorderTopColor(StyleBorderTopColor { inner: focus_outline_color }.into())));
props.push(CssPropertyWithConditions::on_focus(CssProperty::BorderBottomColor(StyleBorderBottomColor { inner: focus_outline_color }.into())));
props.push(CssPropertyWithConditions::on_focus(CssProperty::BorderLeftColor(StyleBorderLeftColor { inner: focus_outline_color }.into())));
props.push(CssPropertyWithConditions::on_focus(CssProperty::BorderRightColor(StyleBorderRightColor { inner: focus_outline_color }.into())));
}
props
}
fn build_button_label_style() -> Vec<CssPropertyWithConditions> {
let font_family = StyleFontFamilyVec::from_vec(vec![
StyleFontFamily::SystemType(SystemFontType::Ui),
]);
vec![
CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(14))),
CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
CssPropertyWithConditions::simple(CssProperty::const_font_family(font_family)),
CssPropertyWithConditions::simple(CssProperty::user_select(StyleUserSelect::None)),
]
}
impl Button {
#[inline]
#[must_use] pub fn create(label: AzString) -> Self {
Self::with_type(label, ButtonType::Default)
}
#[inline]
#[must_use] pub fn with_type(label: AzString, button_type: ButtonType) -> Self {
let container_style = build_button_container_style(button_type);
let label_style = build_button_label_style();
Self {
label,
image: None.into(),
button_type,
on_click: None.into(),
container_style: CssPropertyWithConditionsVec::from_vec(container_style),
label_style: CssPropertyWithConditionsVec::from_vec(label_style.clone()),
image_style: CssPropertyWithConditionsVec::from_vec(label_style),
}
}
#[inline]
pub fn set_button_type(&mut self, button_type: ButtonType) {
self.button_type = button_type;
self.container_style = CssPropertyWithConditionsVec::from_vec(build_button_container_style(button_type));
}
#[inline]
#[must_use] pub fn with_button_type(mut self, button_type: ButtonType) -> Self {
self.set_button_type(button_type);
self
}
#[inline]
#[must_use]
pub fn swap_with_default(&mut self) -> Self {
let mut m = Self::create(AzString::from_const_str(""));
core::mem::swap(&mut m, self);
m
}
#[inline]
pub fn set_image(&mut self, image: ImageRef) {
self.image = Some(image).into();
}
#[inline]
pub fn set_on_click<C: Into<ButtonOnClickCallback>>(&mut self, data: RefAny, on_click: C) {
self.on_click = Some(ButtonOnClick {
refany: data,
callback: on_click.into(),
})
.into();
}
#[inline]
#[must_use]
pub fn with_on_click<C: Into<ButtonOnClickCallback>>(
mut self,
data: RefAny,
on_click: C,
) -> Self {
self.set_on_click(data, on_click);
self
}
#[inline]
#[must_use] pub fn dom(self) -> Dom {
use azul_core::{
callbacks::{CoreCallback, CoreCallbackData},
dom::{EventFilter, HoverEventFilter},
};
let callbacks = match self.on_click.into_option() {
Some(ButtonOnClick {
refany: data,
callback,
}) => vec![CoreCallbackData {
event: EventFilter::Hover(HoverEventFilter::MouseUp),
callback: CoreCallback {
cb: callback.cb as *const () as usize,
ctx: callback.ctx,
},
refany: data,
}],
None => Vec::new(),
};
let type_class = self.button_type.class_name();
let classes: Vec<IdOrClass> = vec![
Class(AzString::from("__azul-native-button")),
Class(AzString::from(type_class)),
];
let label_dom = Dom::create_text(self.label)
.with_css_props(self.label_style);
let mut button = Dom::create_node(NodeType::Button);
if let Some(image) = self.image.into_option() {
button = button.with_child(
Dom::create_image(image).with_css_props(self.image_style),
);
}
button
.with_child(label_dom)
.with_css_props(self.container_style)
.with_ids_and_classes(IdOrClassVec::from_vec(classes))
.with_callbacks(callbacks.into())
.with_tab_index(TabIndex::Auto)
}
}