use super::super::{Button, CursorIcon, NoAction, Style, Ui, UiNode};
pub struct ButtonBuilder<'a, A = NoAction> {
ui: &'a mut Ui<A>,
button: Option<Button<A>>,
}
impl<'a, A: Clone> ButtonBuilder<'a, A> {
pub(crate) fn new(ui: &'a mut Ui<A>, text: impl Into<String>) -> Self {
Self {
ui,
button: Some(Button::new(text)),
}
}
pub fn id(mut self, id: impl Into<String>) -> Self {
if let Some(button) = self.button.as_mut() {
button.id = Some(id.into());
}
self
}
pub fn key(mut self, key: impl Into<String>) -> Self {
if let Some(button) = self.button.as_mut() {
button.key = Some(key.into());
}
self
}
pub fn class(mut self, class: impl Into<String>) -> Self {
if let Some(button) = self.button.as_mut() {
button.class = Some(class.into());
}
self
}
pub fn customMinimumSize(mut self, width: u32, height: u32) -> Self {
if let Some(button) = self.button.as_mut() {
button.customMinimumSize = Some((width, height));
}
self
}
pub fn style(mut self, configure: impl FnOnce(&mut Style)) -> Self {
if let Some(button) = self.button.as_mut() {
configure(&mut button.style);
}
self
}
pub fn onClick(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.click = Some(action);
}
self
}
pub fn onHover(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.hover = Some(action);
}
self
}
pub fn onUnhover(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.unhover = Some(action);
}
self
}
pub fn onFocus(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.focus = Some(action);
}
self
}
pub fn onBlur(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.blur = Some(action);
}
self
}
pub fn onPointerDown(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.pointerDown = Some(action);
}
self
}
pub fn onPointerUp(mut self, action: A) -> Self {
if let Some(button) = self.button.as_mut() {
button.interactions.pointerUp = Some(action);
}
self
}
pub fn cursor(mut self, cursor: CursorIcon) -> Self {
if let Some(button) = self.button.as_mut() {
button.cursor = Some(cursor);
}
self
}
}
impl<A> Drop for ButtonBuilder<'_, A> {
fn drop(&mut self) {
if let Some(button) = self.button.take() {
self.ui.nodes.push(UiNode::Button(button));
}
}
}