use crate::graphics::{Point, Rectangle};
use crate::input::{mouse, ButtonState};
use crate::ui::core::{
Align, Element, Event, Hasher, Layout, MouseCursor, Node, Style, Widget,
};
use std::hash::Hash;
pub struct Button<'a, Message> {
state: &'a mut State,
label: String,
class: Class,
on_press: Option<Message>,
style: Style,
}
impl<'a, Message> std::fmt::Debug for Button<'a, Message>
where
Message: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Button")
.field("state", &self.state)
.field("label", &self.label)
.field("class", &self.class)
.field("on_press", &self.on_press)
.field("style", &self.style)
.finish()
}
}
impl<'a, Message> Button<'a, Message> {
pub fn new(state: &'a mut State, label: &str) -> Self {
Button {
state,
label: String::from(label),
class: Class::Primary,
on_press: None,
style: Style::default().min_width(100),
}
}
pub fn width(mut self, width: u32) -> Self {
self.style = self.style.width(width);
self
}
pub fn fill_width(mut self) -> Self {
self.style = self.style.fill_width();
self
}
pub fn align_self(mut self, align: Align) -> Self {
self.style = self.style.align_self(align);
self
}
pub fn class(mut self, class: Class) -> Self {
self.class = class;
self
}
pub fn on_press(mut self, msg: Message) -> Self {
self.on_press = Some(msg);
self
}
}
impl<'a, Message, Renderer> Widget<Message, Renderer> for Button<'a, Message>
where
Renderer: self::Renderer,
Message: Copy + std::fmt::Debug,
{
fn node(&self, _renderer: &Renderer) -> Node {
Node::new(self.style.height(50))
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
) {
match event {
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state,
}) => {
if let Some(on_press) = self.on_press {
let bounds = layout.bounds();
match state {
ButtonState::Pressed => {
self.state.is_pressed =
bounds.contains(cursor_position);
}
ButtonState::Released => {
let is_clicked = self.state.is_pressed
&& bounds.contains(cursor_position);
self.state.is_pressed = false;
if is_clicked {
messages.push(on_press);
}
}
}
}
}
_ => {}
}
}
fn draw(
&self,
renderer: &mut Renderer,
layout: Layout<'_>,
cursor_position: Point,
) -> MouseCursor {
renderer.draw(
cursor_position,
layout.bounds(),
self.state,
&self.label,
self.class,
)
}
fn hash(&self, state: &mut Hasher) {
self.style.hash(state);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State {
is_pressed: bool,
}
impl State {
pub fn new() -> State {
State::default()
}
pub fn is_pressed(&self) -> bool {
self.is_pressed
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Class {
Primary,
Secondary,
Positive,
}
pub trait Renderer {
fn draw(
&mut self,
cursor_position: Point,
bounds: Rectangle<f32>,
state: &State,
label: &str,
class: Class,
) -> MouseCursor;
}
impl<'a, Message, Renderer> From<Button<'a, Message>>
for Element<'a, Message, Renderer>
where
Renderer: self::Renderer,
Message: 'static + Copy + std::fmt::Debug,
{
fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> {
Element::new(button)
}
}