pub struct Response {
pub ctx: CtxRef,
pub layer_id: LayerId,
pub id: Id,
pub rect: Rect,
pub sense: Sense,
/* private fields */
}Expand description
The result of adding a widget to a Ui.
A Response lets you know whether or not a widget is being hovered, clicked or dragged.
It also lets you easily show a tooltip on hover.
Whenever something gets added to a Ui, a Response object is returned.
[ui.add] returns a Response, as does [ui.button], and all similar shortcuts.
Fields§
§ctx: CtxRefUsed for optionally showing a tooltip and checking for more interactions.
layer_id: LayerIdWhich layer the widget is part of.
id: IdThe Id of the widget/area this response pertains.
rect: RectThe area of the screen we are talking about.
sense: SenseThe senses (click and/or drag) that the widget was interested in (if any).
Implementations§
Source§impl Response
impl Response
Sourcepub fn clicked(&self) -> bool
pub fn clicked(&self) -> bool
Returns true if this widget was clicked this frame by the primary button.
Note that the widget must be sensing clicks with Sense::click.
crate::Button senses clicks; crate::Label does not (unless you call crate::Label::sense).
You can use Self::interact to sense more things after adding a widget.
Sourcepub fn clicked_by(&self, button: PointerButton) -> bool
pub fn clicked_by(&self, button: PointerButton) -> bool
Returns true if this widget was clicked this frame by the given button.
Sourcepub fn secondary_clicked(&self) -> bool
pub fn secondary_clicked(&self) -> bool
Returns true if this widget was clicked this frame by the secondary mouse button (e.g. the right mouse button).
Sourcepub fn middle_clicked(&self) -> bool
pub fn middle_clicked(&self) -> bool
Returns true if this widget was clicked this frame by the middle mouse button.
Sourcepub fn double_clicked(&self) -> bool
pub fn double_clicked(&self) -> bool
Returns true if this widget was double-clicked this frame by the primary button.
Sourcepub fn double_clicked_by(&self, button: PointerButton) -> bool
pub fn double_clicked_by(&self, button: PointerButton) -> bool
Returns true if this widget was double-clicked this frame by the given button.
Sourcepub fn clicked_elsewhere(&self) -> bool
pub fn clicked_elsewhere(&self) -> bool
true if there was a click outside this widget this frame.
Sourcepub fn enabled(&self) -> bool
pub fn enabled(&self) -> bool
Was the widget enabled? If false, there was no interaction attempted and the widget should be drawn in a gray disabled look.
Sourcepub fn hovered(&self) -> bool
pub fn hovered(&self) -> bool
The pointer is hovering above this widget or the widget was clicked/tapped this frame.
Sourcepub fn has_focus(&self) -> bool
pub fn has_focus(&self) -> bool
This widget has the keyboard focus (i.e. is receiving key presses).
Sourcepub fn gained_focus(&self) -> bool
pub fn gained_focus(&self) -> bool
True if this widget has keyboard focus this frame, but didn’t last frame.
Sourcepub fn lost_focus(&self) -> bool
pub fn lost_focus(&self) -> bool
The widget had keyboard focus and lost it,
either because the user pressed tab or clicked somewhere else,
or (in case of a crate::TextEdit) because the user pressed enter.
let response = ui.text_edit_singleline(&mut my_text);
if response.lost_focus() && ui.input().key_pressed(egui::Key::Enter) {
do_request(&my_text);
}pub fn lost_kb_focus(&self) -> bool
Sourcepub fn request_focus(&self)
pub fn request_focus(&self)
Request that this widget get keyboard focus.
Sourcepub fn surrender_focus(&self)
pub fn surrender_focus(&self)
Surrender keyboard focus for this widget.
Sourcepub fn dragged(&self) -> bool
pub fn dragged(&self) -> bool
The widgets is being dragged.
To find out which button(s), query crate::PointerState::button_down
(ui.input().pointer.button_down(…)).
Note that the widget must be sensing drags with Sense::drag.
crate::DragValue senses drags; crate::Label does not (unless you call crate::Label::sense).
You can use Self::interact to sense more things after adding a widget.
pub fn dragged_by(&self, button: PointerButton) -> bool
Sourcepub fn drag_started(&self) -> bool
pub fn drag_started(&self) -> bool
Did a drag on this widgets begin this frame?
Sourcepub fn drag_released(&self) -> bool
pub fn drag_released(&self) -> bool
The widget was being dragged, but now it has been released.
Sourcepub fn drag_delta(&self) -> Vec2
pub fn drag_delta(&self) -> Vec2
If dragged, how many points were we dragged and in what direction?
Sourcepub fn interact_pointer_pos(&self) -> Option<Pos2>
pub fn interact_pointer_pos(&self) -> Option<Pos2>
Where the pointer (mouse/touch) were when when this widget was clicked or dragged.
None if the widget is not being interacted with.
Sourcepub fn hover_pos(&self) -> Option<Pos2>
pub fn hover_pos(&self) -> Option<Pos2>
If it is a good idea to show a tooltip, where is pointer? None if the pointer is outside the response area.
Is the pointer button currently down on this widget? This is true if the pointer is pressing down or dragging a widget
Sourcepub fn changed(&self) -> bool
pub fn changed(&self) -> bool
What the underlying data changed?
e.g. the slider was dragged, text was entered in a TextEdit etc.
Always false for something like a Button.
Can sometimes be true even though the data didn’t changed
(e.g. if the user entered a character and erased it the same frame).
This is not set if the view of the data was changed.
For instance, moving the cursor in a TextEdit does not set this to true.
Sourcepub fn mark_changed(&mut self)
pub fn mark_changed(&mut self)
Report the data shown by this widget changed.
This must be called by widgets that represent some mutable data, e.g. checkboxes, sliders etc.
This should be called when the content changes, but not when the view does.
So we call this when the text of a crate::TextEdit, but not when the cursors changes.
Sourcepub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Response
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Response
Show this UI if the widget was hovered (i.e. a tooltip).
The text will not be visible if the widget is not enabled.
For that, use Self::on_disabled_hover_ui instead.
If you call this multiple times the tooltips will stack underneath the previous ones.
Sourcepub fn on_disabled_hover_ui(
self,
add_contents: impl FnOnce(&mut Ui),
) -> Response
pub fn on_disabled_hover_ui( self, add_contents: impl FnOnce(&mut Ui), ) -> Response
Show this UI when hovering if the widget is disabled.
Sourcepub fn on_hover_ui_at_pointer(
self,
add_contents: impl FnOnce(&mut Ui),
) -> Response
pub fn on_hover_ui_at_pointer( self, add_contents: impl FnOnce(&mut Ui), ) -> Response
Like on_hover_ui, but show the ui next to cursor.
Sourcepub fn on_hover_text(self, text: impl ToString) -> Response
pub fn on_hover_text(self, text: impl ToString) -> Response
Show this text if the widget was hovered (i.e. a tooltip).
The text will not be visible if the widget is not enabled.
For that, use Self::on_disabled_hover_text instead.
If you call this multiple times the tooltips will stack underneath the previous ones.
Sourcepub fn on_disabled_hover_text(self, text: impl ToString) -> Response
pub fn on_disabled_hover_text(self, text: impl ToString) -> Response
Show this text when hovering if the widget is disabled.
pub fn tooltip_text(self, text: impl ToString) -> Response
on_hover_text instead.Sourcepub fn on_hover_cursor(self, cursor: CursorIcon) -> Response
pub fn on_hover_cursor(self, cursor: CursorIcon) -> Response
When hovered, use this icon for the mouse cursor.
Sourcepub fn interact(&self, sense: Sense) -> Response
pub fn interact(&self, sense: Sense) -> Response
Check for more interactions (e.g. sense clicks on a Response returned from a label).
Note that this call will not add any hover-effects to the widget, so when possible
it is better to give the widget a Sense instead, e.g. using [Label::sense].
let response = ui.label("hello");
assert!(!response.clicked()); // labels don't sense clicks by default
let response = response.interact(egui::Sense::click());
if response.clicked() { /* … */ }Sourcepub fn scroll_to_me(&self, align: Align)
pub fn scroll_to_me(&self, align: Align)
Move the scroll to this UI with the specified alignment.
egui::ScrollArea::auto_sized().show(ui, |ui| {
for i in 0..1000 {
let response = ui.button(format!("Button {}", i));
if response.clicked() {
response.scroll_to_me(Align::Center);
}
}
});Sourcepub fn widget_info(&self, make_info: impl Fn() -> WidgetInfo)
pub fn widget_info(&self, make_info: impl Fn() -> WidgetInfo)
For accessibility.
Call after interacting and potential calls to Self::mark_changed.
Trait Implementations§
Source§impl BitOr for Response
To summarize the response from many widgets you can use this pattern:
impl BitOr for 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::new(&mut v.x)) | ui.add(DragValue::new(&mut v.y))
}Now draw_vec2(ui, foo).hovered is true if either DragValue were hovered.
Source§impl BitOrAssign for Response
To summarize the response from many widgets you can use this pattern:
impl BitOrAssign for Response
To summarize the response from many widgets you can use this pattern:
let mut response = ui.add(widget_a);
response |= ui.add(widget_b);
response |= ui.add(widget_c);
if response.hovered() { ui.label("You hovered at least one of the widgets"); }Source§fn bitor_assign(&mut self, rhs: Response)
fn bitor_assign(&mut self, rhs: Response)
|= operation. Read moreAuto Trait Implementations§
impl Freeze for Response
impl !RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl !UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more