From 10574c34d63b429d73c1eae9310ddfa56dfa2771 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 11 Sep 2020 10:19:04 +0200 Subject: [PATCH] make it easy to combine Response:s with | and |= overloads --- egui/src/types.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/egui/src/types.rs b/egui/src/types.rs index 4301c00b..54f668d6 100644 --- a/egui/src/types.rs +++ b/egui/src/types.rs @@ -111,10 +111,12 @@ 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)); Self { - ctx: self.ctx, + ctx: other.ctx, rect: self.rect.union(other.rect), sense: self.sense.union(other.sense), 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?