Capture hover events on tabs

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2021-04-27 09:23:44 -06:00
parent fc4b7e2a2a
commit 3d1e44ca29
5 changed files with 29 additions and 22 deletions

View file

@ -23,6 +23,7 @@ pub use event_handler::*;
pub use flex::*; pub use flex::*;
pub use label::*; pub use label::*;
pub use line_box::*; pub use line_box::*;
pub use mouse_event_handler::*;
pub use new::*; pub use new::*;
pub use stack::*; pub use stack::*;
pub use svg::*; pub use svg::*;

View file

@ -17,11 +17,11 @@ pub struct MouseState {
} }
impl MouseEventHandler { impl MouseEventHandler {
pub fn new<Tag: 'static>( pub fn new<Tag, F>(id: usize, ctx: &AppContext, render_child: F) -> Self
id: usize, where
ctx: &AppContext, Tag: 'static,
render_child: impl FnOnce(MouseState) -> ElementBox, F: FnOnce(MouseState) -> ElementBox,
) -> Self { {
let state = ctx.value::<Tag, _>(id); let state = ctx.value::<Tag, _>(id);
let child = state.map(ctx, |state| render_child(*state)); let child = state.map(ctx, |state| render_child(*state));
Self { state, child } Self { state, child }
@ -30,7 +30,6 @@ impl MouseEventHandler {
impl Element for MouseEventHandler { impl Element for MouseEventHandler {
type LayoutState = (); type LayoutState = ();
type PaintState = (); type PaintState = ();
fn layout( fn layout(
@ -72,7 +71,6 @@ impl Element for MouseEventHandler {
let mouse_in = bounds.contains_point(*position); let mouse_in = bounds.contains_point(*position);
if state.hovered != mouse_in { if state.hovered != mouse_in {
state.hovered = mouse_in; state.hovered = mouse_in;
log::info!("hovered {}", state.hovered);
// ctx.notify(); // ctx.notify();
true true
} else { } else {
@ -81,7 +79,6 @@ impl Element for MouseEventHandler {
} }
Event::LeftMouseDown { position, .. } => { Event::LeftMouseDown { position, .. } => {
if bounds.contains_point(*position) { if bounds.contains_point(*position) {
log::info!("clicked");
state.clicked = true; state.clicked = true;
// ctx.notify(); // ctx.notify();
true true
@ -91,7 +88,6 @@ impl Element for MouseEventHandler {
} }
Event::LeftMouseUp { .. } => { Event::LeftMouseUp { .. } => {
if state.clicked { if state.clicked {
log::info!("unclicked");
state.clicked = false; state.clicked = false;
// ctx.notify(); // ctx.notify();
true true

View file

@ -82,6 +82,10 @@ unsafe fn build_classes() {
sel!(mouseUp:), sel!(mouseUp:),
handle_view_event as extern "C" fn(&Object, Sel, id), handle_view_event as extern "C" fn(&Object, Sel, id),
); );
decl.add_method(
sel!(mouseMoved:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method( decl.add_method(
sel!(mouseDragged:), sel!(mouseDragged:),
handle_view_event as extern "C" fn(&Object, Sel, id), handle_view_event as extern "C" fn(&Object, Sel, id),

View file

@ -474,6 +474,7 @@ impl Element for BufferElement {
precise, precise,
} => self.scroll(*position, *delta, *precise, layout, paint, ctx), } => self.scroll(*position, *delta, *precise, layout, paint, ctx),
Event::KeyDown { chars, .. } => self.key_down(chars, ctx), Event::KeyDown { chars, .. } => self.key_down(chars, ctx),
_ => false,
} }
} else { } else {
false false

View file

@ -176,14 +176,14 @@ impl Pane {
ctx.emit(Event::Split(direction)); ctx.emit(Event::Split(direction));
} }
fn render_tabs(&self, app: &AppContext) -> ElementBox { fn render_tabs(&self, ctx: &AppContext) -> ElementBox {
let settings = smol::block_on(self.settings.read()); let settings = smol::block_on(self.settings.read());
let border_color = ColorU::from_u32(0xdbdbdcff); let border_color = ColorU::from_u32(0xdbdbdcff);
let mut row = Flex::row(); let mut row = Flex::row();
let last_item_ix = self.items.len() - 1; let last_item_ix = self.items.len() - 1;
for (ix, item) in self.items.iter().enumerate() { for (ix, item) in self.items.iter().enumerate() {
let title = item.title(app); let title = item.title(ctx);
let mut border = Border::new(1.0, border_color); let mut border = Border::new(1.0, border_color);
border.left = ix > 0; border.left = ix > 0;
@ -204,7 +204,7 @@ impl Pane {
LineBox::new( LineBox::new(
settings.ui_font_family, settings.ui_font_family,
settings.ui_font_size, settings.ui_font_size,
Align::new(Self::render_modified_icon(item.is_dirty(app))) Align::new(Self::render_modified_icon(item.is_dirty(ctx)))
.right() .right()
.boxed(), .boxed(),
) )
@ -224,9 +224,12 @@ impl Pane {
container = container.with_background_color(ColorU::from_u32(0xeaeaebff)); container = container.with_background_color(ColorU::from_u32(0xeaeaebff));
} }
enum Tab {}
row.add_child( row.add_child(
Expanded::new( Expanded::new(
1.0, 1.0,
MouseEventHandler::new::<Tab, _>(0, ctx, |mouse_state| {
ConstrainedBox::new( ConstrainedBox::new(
EventHandler::new(container.boxed()) EventHandler::new(container.boxed())
.on_mouse_down(move |ctx| { .on_mouse_down(move |ctx| {
@ -237,6 +240,8 @@ impl Pane {
) )
.with_min_width(80.0) .with_min_width(80.0)
.with_max_width(264.0) .with_max_width(264.0)
.boxed()
})
.boxed(), .boxed(),
) )
.named("tab"), .named("tab"),