Skip to main content

Context

Struct Context 

Source
pub struct Context {
    pub input: Input,
    pub theme: Theme,
    pub text: TextEngine,
    pub time: f64,
    pub dt: f32,
    pub frame: u64,
    pub screen: Rect,
    pub pixels_per_point: f32,
    pub clear_color: Color,
    pub cursor: Cursor,
    pub host_commands: Vec<HostCommand>,
    pub ime_area: Option<Rect>,
    /* private fields */
}
Expand description

The state of one window. Every widget reads and writes it.

App::update gets it each frame. Most code reaches it through Ui::ctx.

Fields§

§input: Input

Input of the current frame.

§theme: Theme

Appearance and accessibility settings.

§text: TextEngine

Text layout and fonts.

§time: f64

Seconds since the app started.

§dt: f32

Seconds since the last frame, at most 1/24 so an idle gap does not turn into one big animation step.

§frame: u64

Number of the current frame.

§screen: Rect

The window, in points. Its top left corner is (0, 0).

§pixels_per_point: f32

Physical pixels per point, from the display scale.

§clear_color: Color

Color behind everything.

§cursor: Cursor

Mouse cursor for this frame. Widgets set it while hovered.

§host_commands: Vec<HostCommand>

Requests to the window host, handled after the frame.

§ime_area: Option<Rect>

Caret of the focused text field, where an input method shows its candidates. The field sets it every frame it has focus.

Implementations§

Source§

impl Context

Source

pub fn new() -> Self

Creates the state of a window with the default theme.

Source

pub fn begin_frame(&mut self, size: Vec2, ppp: f32, time: f64, events: &[Event])

Starts a frame of size points at pixels_per_point, time seconds after the start, with the input events. The window host and Headless call it for you.

Source

pub fn end_frame(&mut self)

Ends the frame and builds the draw list. The window host and Headless call it for you.

Source

pub fn request_repaint(&mut self)

Asks for another frame as soon as possible. Call it every frame while something animates. An idle window draws nothing.

Source

pub fn wants_repaint(&self) -> bool

Returns true if another frame was asked for.

Source

pub fn request_relayout(&mut self)

Tells the host that sizes used for layout this frame were guesses. The frame runs again before it is shown.

Source

pub fn wants_relayout(&self) -> bool

Returns true if the frame should run again before it is shown.

Source

pub fn set_tilt(&mut self, tilt: Vec2)

Sets the tilt of the device in radians. It turns the highlights on glass.

Source

pub fn add_scroll_energy(&mut self, v: f32)

Adds to the movement that briefly turns the highlights while scrolling.

Source

pub fn get<T: Clone + Default + 'static>(&mut self, id: Id) -> T

Returns a copy of the state stored for id, or the default if there is none.

Source

pub fn try_get<T: Clone + 'static>(&mut self, id: Id) -> Option<T>

Returns a copy of the state stored for id, if there is any.

Source

pub fn take<T: Default + 'static>(&mut self, id: Id) -> T

Moves the state out, leaving nothing stored. Cheaper than Self::get for large state. Put it back with Self::set.

Source

pub fn set<T: 'static>(&mut self, id: Id, value: T)

Stores state for id. State that is not used for a while is dropped.

Source

pub fn animate(&mut self, id: Id, target: f32, tuning: Tuning) -> f32

Moves a value stored under id towards target on a spring and returns it. Keeps asking for frames until it arrives.

Source

pub fn animate_bool(&mut self, id: Id, on: bool, tuning: Tuning) -> f32

Like Self::animate, towards 1 when on and 0 when off.

Source

pub fn animate_vec2(&mut self, id: Id, target: Vec2, tuning: Tuning) -> Spring2d

Like Self::animate, for a point. Returns the spring, so you can read its velocity too.

Source

pub fn tuning(&self, tuning: Tuning) -> Tuning

Returns tuning as it should be used: unchanged, or short and without overshoot when Theme::reduce_motion is on.

Source

pub fn layer(&mut self, id: Id, order: Order) -> usize

Returns the index of the layer id, created on first use. Layers draw in Order, and within an order the one used or clicked last is on top.

Source

pub fn painter(&mut self, layer: usize, clip: Rect) -> Painter<'_>

Returns a painter for layer that clips to clip.

Source

pub fn background(&mut self) -> Painter<'_>

Returns a painter for the whole window, behind all widgets.

Source

pub fn interact( &mut self, id: Id, rect: Rect, clip: Rect, layer: usize, sense: Sense, ) -> Response

Registers rect on layer for input and returns what happened to it. Parts outside clip do not react. Only the topmost widget under the pointer is hovered.

Source

pub fn focusable(&mut self, id: Id)

Lets id take keyboard focus with Tab. Call it every frame the widget is visible.

Source

pub fn set_focus(&mut self, id: Option<Id>)

Gives keyboard focus to id, or takes it away with None.

Source

pub fn shows_focus(&self, id: Id) -> bool

Returns true if id has focus from the keyboard, which is when the focus ring shows.

Source

pub fn scroll_area(&mut self, id: Id, rect: Rect, layer: usize) -> Vec2

Registers a scroll area and returns the wheel movement it receives, on both axes.

Source

pub fn scroll_area_axes( &mut self, id: Id, rect: Rect, layer: usize, x: bool, y: bool, ) -> Vec2

Registers a scroll area that scrolls on the given axes and returns the wheel movement it receives. Movement on the other axis goes to the scroll area around it, so a horizontal row inside a page leaves vertical scrolling to the page.

Source

pub fn is_active(&self, id: Id) -> bool

Returns true while id is being pressed or dragged.

Source

pub fn grab(&mut self, id: Id)

Makes id the target of the current press or drag.

Source

pub fn any_active(&self) -> bool

Returns true while any widget is being pressed or dragged.

Source

pub fn backdrop(&mut self, id: Id, adaptive: bool) -> Backdrop

Returns how the glass element id should look over what is behind it. With adaptive set, small elements switch between light and dark with the backdrop. Large surfaces keep the theme appearance.

Source

pub fn prepare_glass( &mut self, id: Id, glass: &mut Glass, adaptive: bool, ) -> Backdrop

Adapts glass to what is behind it and has the backdrop measured for the next frame. Call it before painting glass you build yourself.

Source

pub fn set_accessibility(&mut self, on: bool)

Turns collecting the accessibility tree on or off. The window host turns it on when a screen reader asks for the tree.

Source

pub fn accessibility(&self) -> bool

Returns true while the accessibility tree is collected.

Source

pub fn access_node( &mut self, id: Id, role: Role, rect: Rect, layer: usize, ) -> Option<&mut Node>

Describes an element for screen readers. Returns None while none is listening, so building labels costs nothing then. Ui::access fills in the layer for you.

Source

pub fn access_label(&mut self, id: Id, label: impl Into<String>)

Labels the node id described this frame, for widgets without text of their own, such as sliders.

Source

pub fn access_label_since(&mut self, from: usize, label: &str)

Labels every node described since from (an index into Self::access_nodes) that has no label yet. Settings rows use it to name the switch or slider at their end.

Source

pub fn access_nodes(&self) -> &[Node]

Returns the nodes described so far this frame.

Source

pub fn access_requested(&self, id: Id, action: &Action) -> bool

Returns true if a screen reader asked for action on id this frame.

Source

pub fn take_access_text(&mut self, id: Id) -> Option<String>

Returns the text a screen reader wants to put into the field id.

Source

pub fn take_access_number(&mut self, id: Id) -> Option<f64>

Returns the value a screen reader wants to give the slider or stepper id.

Source

pub fn access_update(&self, title: &str) -> TreeUpdate

Returns the full accessibility tree of the last frame for AccessKit.

Source

pub fn morph(&mut self, key: Id, view: Id, rect: Rect) -> Rect

Moves an element from the view that showed it before to the view that shows it now, such as a cover that grows from a grid into a detail page.

Call it every frame the element is drawn, with the same key from every view, the view that draws it, and where that view puts it. Draw the element at the returned rectangle. While the view stays the same, the result is rect. When the view changes, it travels from the old place to rect on a spring. An element that was not drawn for 20 seconds starts over where it is.

let key = Id::new(("cover", 7));
let (view, place) = if open {
    (Id::new("detail"), rect(40.0, 40.0, 320.0, 320.0))
} else {
    (Id::new("grid"), ui.allocate(vec2(120.0, 120.0)))
};
let shown = ui.ctx.morph(key, view, place);
ui.painter().image(shown, 14.0, image, rect(0.0, 0.0, 1.0, 1.0), Color::WHITE);
Source

pub fn is_morphing(&self, key: Id) -> bool

Returns true while the element key travels between views.

Source

pub fn start_drag( &mut self, source: Id, payload: Box<dyn Any>, grab: Vec2, size: Vec2, )

Picks up payload. Ui::drag_source calls it for you.

Source

pub fn dragged_id(&self) -> Option<Id>

Returns the id of the widget being dragged.

Source

pub fn dragged<P: Clone + 'static>(&self) -> Option<P>

Returns the payload being dragged, if it has type P.

Source

pub fn drag_rect(&self) -> Option<Rect>

Returns where the dragged item is drawn: at the pointer, keeping the offset where it was grabbed.

Source

pub fn drag_over<P: 'static>(&self, rect: Rect) -> bool

Returns true while a payload of type P is dragged over rect.

Source

pub fn take_drop<P: 'static>(&mut self, rect: Rect) -> Option<P>

Returns the payload when it is released over rect. Only the first caller gets it.

Source

pub fn add_image(&mut self, width: u32, height: u32, rgba: Vec<u8>) -> ImageId

Adds an image of width by height pixels in RGBA order, 8 bits per channel, and returns its id. Draw it with Ui::image or Painter::image.

Source

pub fn remove_image(&mut self, id: ImageId)

Frees an image added with Self::add_image. Drawing it afterwards shows nothing.

Source

pub fn copy_text(&mut self, text: impl Into<String>)

Puts text on the clipboard.

Source

pub fn measure(&mut self, text: &str, size: f32, weight: Weight) -> Vec2

Returns the size of text in one line at size points.

Source

pub fn toast(&mut self, icon: Option<Icon>, text: impl Into<String>)

Shows a short message at the top of the window for a few seconds.

Source

pub fn popup_open(&self, id: Id) -> bool

Returns true if the popup id is open.

Source

pub fn set_popup(&mut self, id: Id, open: bool)

Opens or closes the popup id.

Source

pub fn toggle_popup(&mut self, id: Id)

Opens the popup id if it is closed, and closes it if it is open.

Source

pub fn pressed_outside(&self, rect: Rect) -> bool

Returns true if the pointer was pressed this frame outside rect.

Source

pub fn pointer_or(&self, fallback: Vec2) -> Vec2

Returns the pointer position, or fallback while the pointer is outside the window.

Trait Implementations§

Source§

impl Default for Context

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more