Close tabs when X is clicked

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo
2021-04-28 10:00:45 -06:00
co-authored by Antonio Scandurra
parent bc1f758b66
commit b5463dcd1e
2 changed files with 29 additions and 6 deletions
+15 -1
View File
@@ -8,6 +8,7 @@ use serde_json::json;
pub struct MouseEventHandler {
state: ValueHandle<MouseState>,
child: ElementBox,
click_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
}
#[derive(Clone, Copy, Debug, Default)]
@@ -28,8 +29,14 @@ impl MouseEventHandler {
Self {
state: state_handle,
child,
click_handler: None,
}
}
pub fn on_click(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
self.click_handler = Some(Box::new(handler));
self
}
}
impl Element for MouseEventHandler {
@@ -70,6 +77,8 @@ impl Element for MouseEventHandler {
_: &mut Self::PaintState,
ctx: &mut EventContext,
) -> bool {
let click_handler = self.click_handler.as_mut();
let handled_in_child = self.child.dispatch_event(event, ctx);
self.state.update(ctx.app, |state| match event {
@@ -92,10 +101,15 @@ impl Element for MouseEventHandler {
handled_in_child
}
}
Event::LeftMouseUp { .. } => {
Event::LeftMouseUp { position, .. } => {
if !handled_in_child && state.clicked {
state.clicked = false;
ctx.notify();
if let Some(handler) = click_handler {
if bounds.contains_point(*position) {
handler(ctx);
}
}
true
} else {
handled_in_child