pub struct Response {
    pub ctx: Context,
    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: Context

Used for optionally showing a tooltip and checking for more interactions.

layer_id: LayerId

Which layer the widget is part of.

id: Id

The Id of the widget/area this response pertains.

rect: Rect

The area of the screen we are talking about.

sense: Sense

The senses (click and/or drag) that the widget was interested in (if any).

Implementations

Returns true if this widget was clicked this frame by the primary button.

A click is registered when the mouse or touch is released within a certain amount of time and distance from when and where it was pressed.

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.

Returns true if this widget was clicked this frame by the given button.

Returns true if this widget was clicked this frame by the secondary mouse button (e.g. the right mouse button).

Returns true if this widget was clicked this frame by the middle mouse button.

Returns true if this widget was double-clicked this frame by the primary button.

Returns true if this widget was triple-clicked this frame by the primary button.

Returns true if this widget was double-clicked this frame by the given button.

Returns true if this widget was triple-clicked this frame by the given button.

true if there was a click outside this widget this frame.

Was the widget enabled? If false, there was no interaction attempted and the widget should be drawn in a gray disabled look.

The pointer is hovering above this widget or the widget was clicked/tapped this frame.

Note that this is slightly different from checking response.rect.contains(pointer_pos). For one, the hover rectangle is slightly larger, by half of the current item spacing (to make it easier to click things). But hovered also checks that no other area is covering this response rectangle.

This widget has the keyboard focus (i.e. is receiving key presses).

This function only returns true if the UI as a whole (e.g. window) also has the keyboard focus. That makes this function suitable for style choices, e.g. a thicker border around focused widgets.

True if this widget has keyboard focus this frame, but didn’t last frame.

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);
}

Request that this widget get keyboard focus.

Surrender keyboard focus for this widget.

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.

Did a drag on this widgets begin this frame?

The widget was being dragged, but now it has been released.

If dragged, how many points were we dragged and in what direction?

Where the pointer (mouse/touch) were when when this widget was clicked or dragged. None if the widget is not being interacted with.

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

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.

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.

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.

Show this UI when hovering if the widget is disabled.

Like on_hover_ui, but show the ui next to cursor.

Like on_hover_text, but show the text next to cursor.

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.

Show this text when hovering if the widget is disabled.

When hovered, use this icon for the mouse cursor.

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 crate::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() { /* … */ }

Adjust the scroll position until this UI becomes visible.

If align is None, it’ll scroll enough to bring the UI into view.

See also: Ui::scroll_to_cursor, Ui::scroll_to_rect. Ui::scroll_with_delta.

egui::ScrollArea::vertical().show(ui, |ui| {
    for i in 0..1000 {
        let response = ui.button("Scroll to me");
        if response.clicked() {
            response.scroll_to_me(Some(egui::Align::Center));
        }
    }
});

For accessibility.

Call after interacting and potential calls to Self::mark_changed.

Response to secondary clicks (right-clicks) by showing the given menu.

let response = ui.add(Label::new("Right-click me!").sense(Sense::click()));
response.context_menu(|ui| {
    if ui.button("Close the menu").clicked() {
        ui.close_menu();
    }
});

See also: Ui::menu_button and Ui::close_menu.

A logical “or” operation. For instance a.union(b).hovered means “was either a or b hovered?”.

The resulting Self::id will come from the first (self) argument.

Trait Implementations

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.

The resulting type after applying the | operator.
Performs the | operation. Read more

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"); }
Performs the |= operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.