make it easy to combine Response:s with | and |= overloads

This commit is contained in:
Emil Ernerfeldt 2020-09-11 10:19:04 +02:00
parent 13060d495b
commit 10574c34d6

View file

@ -111,10 +111,12 @@ impl Response {
} }
impl Response { impl Response {
pub fn union(self, other: Self) -> Self { /// A logical "or" operation.
/// For instance `a.union(b).hovered` means "was either a or b hovered?".
pub fn union(&self, other: Self) -> Self {
assert!(Arc::ptr_eq(&self.ctx, &other.ctx)); assert!(Arc::ptr_eq(&self.ctx, &other.ctx));
Self { Self {
ctx: self.ctx, ctx: other.ctx,
rect: self.rect.union(other.rect), rect: self.rect.union(other.rect),
sense: self.sense.union(other.sense), sense: self.sense.union(other.sense),
hovered: self.hovered || other.hovered, hovered: self.hovered || other.hovered,
@ -126,6 +128,37 @@ impl Response {
} }
} }
/// To summarize the response from many widgets you can use this pattern:
///
/// ```
/// use egui::*;
/// fn draw_vec2(ui: &mut Ui, v: &mut Vec2) -> Response {
/// ui.add(DragValue::f32(&mut v.x)) | ui.add(DragValue::f32(&mut v.y))
/// }
/// ```
///
/// Now `draw_vec2(ui, foo).hovered` is true if either `DragValue` were hovered.
impl std::ops::BitOr for Response {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
self.union(rhs)
}
}
/// To summarize the response from many widgets you can use this pattern:
///
/// ``` ignore
/// let mut response = ui.add(some_widget);
/// response |= ui.add(some_other_widget);
/// response |= ui.add(some_widget);
/// if response.active { ui.label("You are interacting with one of the widgets"); }
/// ```
impl std::ops::BitOrAssign for Response {
fn bitor_assign(&mut self, rhs: Self) {
*self = self.union(rhs);
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/// What sort of interaction is a widget sensitive to? /// What sort of interaction is a widget sensitive to?