Skip to content

Commit

Permalink
glib: Re-introduce an event propagation specific type
Browse files Browse the repository at this point in the history
As the semantics of ControlFlow don't match 1:1 with the event
propagations
See gtk-rs/gtk4-rs#1435
  • Loading branch information
bilelmoussaoui committed Jul 25, 2023
1 parent 751e1dd commit d636359
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion glib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use self::{
},
signal::{
signal_handler_block, signal_handler_disconnect, signal_handler_unblock,
signal_stop_emission_by_name, SignalHandlerId,
signal_stop_emission_by_name, Propagation, SignalHandlerId,
},
types::{ILong, Pointer, StaticType, StaticTypeExt, Type, ULong},
value::{BoxedValue, SendValue, ToSendValue, ToValue, Value},
Expand Down
78 changes: 78 additions & 0 deletions glib/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,81 @@ pub fn signal_has_handler_pending<T: ObjectType>(
))
}
}

// rustdoc-stripper-ignore-next
/// Whether an event was handled or not.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Propagation {
// Event was handled, don't propagate
Stop,
// Event was not handled, propagate
Proceed,
}

impl Propagation {
// rustdoc-stripper-ignore-next
/// Returns `true` if this is a `Stop` variant.
pub fn is_stop(&self) -> bool {
matches!(self, Self::Stop)
}

// rustdoc-stripper-ignore-next
/// Returns `true` if this is a `Stop` variant.
pub fn is_proceed(&self) -> bool {
matches!(self, Self::Proceed)
}
}

impl From<bool> for Propagation {
fn from(c: bool) -> Self {
if c {
Self::Proceed
} else {
Self::Stop
}
}
}

impl From<Propagation> for bool {
fn from(c: Propagation) -> Self {
match c {
Propagation::Stop => false,
Propagation::Proceed => true,
}
}
}

#[doc(hidden)]
impl IntoGlib for Propagation {
type GlibType = ffi::gboolean;

#[inline]
fn into_glib(self) -> ffi::gboolean {
bool::from(self).into_glib()
}
}

#[doc(hidden)]
impl FromGlib<ffi::gboolean> for Propagation {
#[inline]
unsafe fn from_glib(value: ffi::gboolean) -> Self {
bool::from_glib(value).into()
}
}

impl crate::ToValue for Propagation {
fn to_value(&self) -> crate::Value {
bool::from(*self).to_value()
}

fn value_type(&self) -> crate::Type {
<bool as crate::StaticType>::static_type()
}
}

impl From<Propagation> for crate::Value {
#[inline]
fn from(v: Propagation) -> Self {
bool::from(v).into()
}
}

0 comments on commit d636359

Please sign in to comment.