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: InputInput of the current frame.
theme: ThemeAppearance and accessibility settings.
text: TextEngineText layout and fonts.
time: f64Seconds since the app started.
dt: f32Seconds since the last frame, at most 1/24 so an idle gap does not turn into one big animation step.
frame: u64Number of the current frame.
screen: RectThe window, in points. Its top left corner is (0, 0).
pixels_per_point: f32Physical pixels per point, from the display scale.
clear_color: ColorColor behind everything.
cursor: CursorMouse 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
impl Context
Sourcepub fn begin_frame(&mut self, size: Vec2, ppp: f32, time: f64, events: &[Event])
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.
Sourcepub fn end_frame(&mut self)
pub fn end_frame(&mut self)
Ends the frame and builds the draw list. The window host and
Headless call it for you.
Sourcepub fn request_repaint(&mut self)
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.
Sourcepub fn wants_repaint(&self) -> bool
pub fn wants_repaint(&self) -> bool
Returns true if another frame was asked for.
Sourcepub fn request_relayout(&mut self)
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.
Sourcepub fn wants_relayout(&self) -> bool
pub fn wants_relayout(&self) -> bool
Returns true if the frame should run again before it is shown.
Sourcepub fn set_tilt(&mut self, tilt: Vec2)
pub fn set_tilt(&mut self, tilt: Vec2)
Sets the tilt of the device in radians. It turns the highlights on glass.
Sourcepub fn add_scroll_energy(&mut self, v: f32)
pub fn add_scroll_energy(&mut self, v: f32)
Adds to the movement that briefly turns the highlights while scrolling.
Sourcepub fn get<T: Clone + Default + 'static>(&mut self, id: Id) -> T
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.
Sourcepub fn try_get<T: Clone + 'static>(&mut self, id: Id) -> Option<T>
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.
Sourcepub fn set<T: 'static>(&mut self, id: Id, value: T)
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.
Sourcepub fn animate(&mut self, id: Id, target: f32, tuning: Tuning) -> f32
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.
Sourcepub fn animate_bool(&mut self, id: Id, on: bool, tuning: Tuning) -> f32
pub fn animate_bool(&mut self, id: Id, on: bool, tuning: Tuning) -> f32
Like Self::animate, towards 1 when on and 0 when off.
Sourcepub fn animate_vec2(&mut self, id: Id, target: Vec2, tuning: Tuning) -> Spring2d
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.
Sourcepub fn tuning(&self, tuning: Tuning) -> Tuning
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.
Sourcepub fn layer(&mut self, id: Id, order: Order) -> usize
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.
Sourcepub fn painter(&mut self, layer: usize, clip: Rect) -> Painter<'_>
pub fn painter(&mut self, layer: usize, clip: Rect) -> Painter<'_>
Returns a painter for layer that clips to clip.
Sourcepub fn background(&mut self) -> Painter<'_>
pub fn background(&mut self) -> Painter<'_>
Returns a painter for the whole window, behind all widgets.
Sourcepub fn interact(
&mut self,
id: Id,
rect: Rect,
clip: Rect,
layer: usize,
sense: Sense,
) -> Response
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.
Sourcepub fn focusable(&mut self, id: Id)
pub fn focusable(&mut self, id: Id)
Lets id take keyboard focus with Tab. Call it every frame the widget is
visible.
Sourcepub fn set_focus(&mut self, id: Option<Id>)
pub fn set_focus(&mut self, id: Option<Id>)
Gives keyboard focus to id, or takes it away with None.
Sourcepub fn shows_focus(&self, id: Id) -> bool
pub fn shows_focus(&self, id: Id) -> bool
Returns true if id has focus from the keyboard, which is when the focus
ring shows.
Sourcepub fn scroll_area(&mut self, id: Id, rect: Rect, layer: usize) -> Vec2
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.
Sourcepub fn scroll_area_axes(
&mut self,
id: Id,
rect: Rect,
layer: usize,
x: bool,
y: bool,
) -> Vec2
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.
Sourcepub fn any_active(&self) -> bool
pub fn any_active(&self) -> bool
Returns true while any widget is being pressed or dragged.
Sourcepub fn backdrop(&mut self, id: Id, adaptive: bool) -> Backdrop
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.
Sourcepub fn prepare_glass(
&mut self,
id: Id,
glass: &mut Glass,
adaptive: bool,
) -> Backdrop
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.
Sourcepub fn set_accessibility(&mut self, on: bool)
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.
Sourcepub fn accessibility(&self) -> bool
pub fn accessibility(&self) -> bool
Returns true while the accessibility tree is collected.
Sourcepub fn access_node(
&mut self,
id: Id,
role: Role,
rect: Rect,
layer: usize,
) -> Option<&mut Node>
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.
Sourcepub fn access_label(&mut self, id: Id, label: impl Into<String>)
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.
Sourcepub fn access_label_since(&mut self, from: usize, label: &str)
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.
Sourcepub fn access_nodes(&self) -> &[Node]
pub fn access_nodes(&self) -> &[Node]
Returns the nodes described so far this frame.
Sourcepub fn access_requested(&self, id: Id, action: &Action) -> bool
pub fn access_requested(&self, id: Id, action: &Action) -> bool
Returns true if a screen reader asked for action on id this frame.
Sourcepub fn take_access_text(&mut self, id: Id) -> Option<String>
pub fn take_access_text(&mut self, id: Id) -> Option<String>
Returns the text a screen reader wants to put into the field id.
Sourcepub fn take_access_number(&mut self, id: Id) -> Option<f64>
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.
Sourcepub fn access_update(&self, title: &str) -> TreeUpdate
pub fn access_update(&self, title: &str) -> TreeUpdate
Returns the full accessibility tree of the last frame for AccessKit.
Sourcepub fn morph(&mut self, key: Id, view: Id, rect: Rect) -> Rect
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);Sourcepub fn is_morphing(&self, key: Id) -> bool
pub fn is_morphing(&self, key: Id) -> bool
Returns true while the element key travels between views.
Sourcepub fn start_drag(
&mut self,
source: Id,
payload: Box<dyn Any>,
grab: Vec2,
size: Vec2,
)
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.
Sourcepub fn dragged_id(&self) -> Option<Id>
pub fn dragged_id(&self) -> Option<Id>
Returns the id of the widget being dragged.
Sourcepub fn dragged<P: Clone + 'static>(&self) -> Option<P>
pub fn dragged<P: Clone + 'static>(&self) -> Option<P>
Returns the payload being dragged, if it has type P.
Sourcepub fn drag_rect(&self) -> Option<Rect>
pub fn drag_rect(&self) -> Option<Rect>
Returns where the dragged item is drawn: at the pointer, keeping the offset where it was grabbed.
Sourcepub fn drag_over<P: 'static>(&self, rect: Rect) -> bool
pub fn drag_over<P: 'static>(&self, rect: Rect) -> bool
Returns true while a payload of type P is dragged over rect.
Sourcepub fn take_drop<P: 'static>(&mut self, rect: Rect) -> Option<P>
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.
Sourcepub fn add_image(&mut self, width: u32, height: u32, rgba: Vec<u8>) -> ImageId
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.
Sourcepub fn remove_image(&mut self, id: ImageId)
pub fn remove_image(&mut self, id: ImageId)
Frees an image added with Self::add_image. Drawing it afterwards shows
nothing.
Sourcepub fn measure(&mut self, text: &str, size: f32, weight: Weight) -> Vec2
pub fn measure(&mut self, text: &str, size: f32, weight: Weight) -> Vec2
Returns the size of text in one line at size points.
Sourcepub fn toast(&mut self, icon: Option<Icon>, text: impl Into<String>)
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.
Sourcepub fn popup_open(&self, id: Id) -> bool
pub fn popup_open(&self, id: Id) -> bool
Returns true if the popup id is open.
Sourcepub fn toggle_popup(&mut self, id: Id)
pub fn toggle_popup(&mut self, id: Id)
Opens the popup id if it is closed, and closes it if it is open.
Sourcepub fn pressed_outside(&self, rect: Rect) -> bool
pub fn pressed_outside(&self, rect: Rect) -> bool
Returns true if the pointer was pressed this frame outside rect.
Sourcepub fn pointer_or(&self, fallback: Vec2) -> Vec2
pub fn pointer_or(&self, fallback: Vec2) -> Vec2
Returns the pointer position, or fallback while the pointer is outside
the window.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Context
impl !RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl !UnwindSafe for Context
impl Forget for Context
impl Move for Context
impl Unpin for Context
impl UnsafeUnpin for Context
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.