Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: disable default view events #646

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/animations/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use floem::{
animate::Animation,
event::EventListener as EL,
kurbo::Stroke,
peniko::Color,
reactive::{RwSignal, SignalGet, Trigger},
unit::DurationUnitExt,
Expand Down
1 change: 0 additions & 1 deletion examples/view-transition/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use floem::{
animate::Animation,
kurbo::Stroke,
peniko::Color,
reactive::{RwSignal, SignalGet, SignalUpdate},
style::Style,
Expand Down
1 change: 0 additions & 1 deletion examples/view-transition/src/music_player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use floem::{
animate::Animation,
kurbo::Stroke,
peniko::{Brush, Color},
reactive::{RwSignal, SignalGet, SignalUpdate},
style::{ScaleX, ScaleY, Style, Transition},
Expand Down
514 changes: 237 additions & 277 deletions src/context.rs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,24 @@ impl ViewId {
self.add_update_message(UpdateMessage::RemoveKeyboardNavigable { id: *self });
}

/// Disables the default view behavior for the specified event.
///
/// Children will still see the event, but the view event function will not be called nor the event listeners on the view
pub fn disable_default_event(&self, event: EventListener) {
self.state()
.borrow_mut()
.disable_default_events
.insert(event);
}

/// Re-enables the default view behavior for a previously disabled event.
pub fn remove_disable_default_event(&self, event: EventListener) {
self.state()
.borrow_mut()
.disable_default_events
.remove(&event);
}

/// Mark this view as a view that can be dragged
///
/// You can customize the apearance of a view while dragging in the style
Expand Down
3 changes: 3 additions & 0 deletions src/view_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
},
};
use bitflags::bitflags;
use im::HashSet;
use peniko::kurbo::{Point, Rect};
use smallvec::SmallVec;
use std::{cell::RefCell, collections::HashMap, marker::PhantomData, rc::Rc};
Expand Down Expand Up @@ -172,6 +173,7 @@ pub struct ViewState {
pub(crate) last_pointer_down: Option<PointerInputEvent>,
pub(crate) is_hidden_state: IsHiddenState,
pub(crate) num_waiting_animations: u16,
pub(crate) disable_default_events: HashSet<EventListener>,
pub(crate) debug_name: SmallVec<[String; 1]>,
}

Expand Down Expand Up @@ -202,6 +204,7 @@ impl ViewState {
window_origin: Point::ZERO,
is_hidden_state: IsHiddenState::None,
num_waiting_animations: 0,
disable_default_events: HashSet::new(),
debug_name: Default::default(),
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ pub trait Decorators: IntoView<V = Self::DV> + Sized {
view
}

/// Dynamically controls whether the default view behavior for an event should be disabled.
/// When disable is true, children will still see the event, but the view event function will not be called nor
/// the event listeners on the view.
///
/// # Reactivity
/// This function is reactive and will re-run the disable function automatically in response to changes in signals
fn disable_default_event(
self,
disable: impl Fn() -> (EventListener, bool) + 'static,
) -> Self::DV {
let view = self.into_view();
let id = view.id();
create_effect(move |_| {
let (event, disable) = disable();
if disable {
id.disable_default_event(event);
} else {
id.remove_disable_default_event(event);
}
});
view
}

fn draggable(self) -> Self::DV {
let view = self.into_view();
view.id().draggable();
Expand Down
Loading