use crate::tokens::DESIGN_TOKENS;
use egui::{CursorIcon, Response, Ui};
pub trait ResponseExt {
fn on_click(&self, action: impl FnOnce()) -> &Self;
fn on_double_click(&self, action: impl FnOnce()) -> &Self;
fn on_triple_click(&self, action: impl FnOnce()) -> &Self;
fn on_secondary_click(&self, action: impl FnOnce()) -> &Self;
fn on_middle_click(&self, action: impl FnOnce()) -> &Self;
fn on_hover(&self, action: impl FnOnce()) -> &Self;
fn on_hover_start(&self, action: impl FnOnce()) -> &Self;
fn is_double_clicked(&self) -> bool;
fn is_middle_clicked(&self) -> bool;
fn is_secondary_clicked(&self) -> bool;
fn drag_started(&self) -> bool;
fn drag_stopped(&self) -> bool;
fn highlight(&self, ui: &mut Ui) -> &Self;
fn show_tooltip_on_hover(&self, text: impl Into<String>) -> &Self;
fn set_cursor_icon(&self, icon: CursorIcon) -> &Self;
}
impl ResponseExt for Response {
fn on_click(&self, action: impl FnOnce()) -> &Self {
if self.clicked() {
action();
}
self
}
fn on_double_click(&self, action: impl FnOnce()) -> &Self {
if self.double_clicked() {
action();
}
self
}
fn on_triple_click(&self, action: impl FnOnce()) -> &Self {
if self.triple_clicked() {
action();
}
self
}
fn on_secondary_click(&self, action: impl FnOnce()) -> &Self {
if self.secondary_clicked() {
action();
}
self
}
fn on_middle_click(&self, action: impl FnOnce()) -> &Self {
if self.middle_clicked() {
action();
}
self
}
fn on_hover(&self, action: impl FnOnce()) -> &Self {
if self.hovered() {
action();
}
self
}
fn on_hover_start(&self, action: impl FnOnce()) -> &Self {
if self.hovered() && !self.ctx.input(|i| i.pointer.any_pressed()) {
action();
}
self
}
fn is_double_clicked(&self) -> bool {
self.double_clicked()
}
fn is_middle_clicked(&self) -> bool {
self.middle_clicked()
}
fn is_secondary_clicked(&self) -> bool {
self.secondary_clicked()
}
fn drag_started(&self) -> bool {
self.drag_started_by(egui::PointerButton::Primary)
}
fn drag_stopped(&self) -> bool {
self.drag_stopped_by(egui::PointerButton::Primary)
}
fn highlight(&self, ui: &mut Ui) -> &Self {
let rect = self.rect;
let stroke = egui::Stroke::new(
DESIGN_TOKENS.stroke.thick,
DESIGN_TOKENS.semantic.status.caution,
);
ui.painter()
.rect_stroke(rect, 0.0, stroke, egui::StrokeKind::Outside);
self
}
fn show_tooltip_on_hover(&self, text: impl Into<String>) -> &Self {
let _ = self.clone().on_hover_text(text.into());
self
}
fn set_cursor_icon(&self, icon: CursorIcon) -> &Self {
if self.hovered() {
self.ctx.set_cursor_icon(icon);
}
self
}
}