use kas::prelude::*;
use kas::theme::MarkStyle;
use std::fmt::Debug;
#[impl_self]
mod Mark {
#[derive(Debug)]
#[widget]
pub struct Mark {
core: widget_core!(),
style: MarkStyle,
label: String,
}
impl Self {
pub fn new(style: MarkStyle, label: impl ToString) -> Self {
Mark {
core: Default::default(),
style,
label: label.to_string(),
}
}
#[inline]
pub fn mark(&self) -> MarkStyle {
self.style
}
#[inline]
pub fn set_mark(&mut self, mark: MarkStyle) {
self.style = mark;
}
}
impl Layout for Self {
fn size_rules(&mut self, cx: &mut SizeCx, axis: AxisInfo) -> SizeRules {
cx.feature(self.style.into(), axis)
}
fn draw(&self, mut draw: DrawCx) {
draw.mark(self.rect(), self.style);
}
}
impl Tile for Self {
fn tooltip(&self) -> Option<&str> {
Some(&self.label)
}
fn role(&self, cx: &mut dyn RoleCx) -> Role<'_> {
cx.set_label(&self.label);
Role::Indicator
}
}
}
#[impl_self]
mod MarkButton {
#[derive(Debug)]
#[widget]
pub struct MarkButton<M: Clone + Debug + 'static> {
core: widget_core!(),
style: MarkStyle,
label: String,
msg: M,
}
impl Self {
pub fn new_msg(style: MarkStyle, label: impl ToString, msg: M) -> Self {
MarkButton {
core: Default::default(),
style,
label: label.to_string(),
msg,
}
}
}
impl Layout for Self {
fn size_rules(&mut self, cx: &mut SizeCx, axis: AxisInfo) -> SizeRules {
cx.feature(self.style.into(), axis)
.with_stretch(Stretch::Low)
}
fn draw(&self, mut draw: DrawCx) {
draw.mark(self.rect(), self.style);
}
}
impl Tile for Self {
fn tooltip(&self) -> Option<&str> {
Some(&self.label)
}
fn role(&self, cx: &mut dyn RoleCx) -> Role<'_> {
cx.set_label(&self.label);
Role::Button
}
}
impl Events for Self {
const REDRAW_ON_MOUSE_OVER: bool = true;
type Data = ();
fn handle_event(&mut self, cx: &mut EventCx, _: &Self::Data, event: Event) -> IsUsed {
event.on_click(cx, self.id(), |cx| cx.push(self.msg.clone()))
}
fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
if let Some(kas::messages::Activate(code)) = cx.try_pop() {
cx.push(self.msg.clone());
cx.depress_with_key(&self, code);
}
}
}
}