Skip to main content

GpuSurface

Struct GpuSurface 

Source
pub struct GpuSurface(/* private fields */);
Expand description

A surface the app draws on with its own GPU API (wgpu, Vulkan, Metal, Direct3D), at its own pace and on its own thread, as it would draw on a window of its own: an NSView backed by a CAMetalLayer on AppKit. It has no natural size; the layout sizes it.

on_ready gets its SurfaceHandle once the native surface exists, and on_resize its size in pixels whenever that or its scale changes; the handle’s size() has it too, for a render thread. The handle keeps the native surface alive after the widget is gone, so drop it (and the GPU surface made on it) when the app is done presenting.

The surface sits above the window’s own content, as the platform layers it. Its label is its accessible name.

With on_input it takes keys and the pointer: a click or Tab focuses it, and it gets every key the window doesn’t take first for its menus’ shortcuts, Tab too (Control+Tab leaves it on AppKit and GTK, as it leaves a text view). pointer_lock hides and holds the cursor and reports its moves; keyboard_grab takes the system’s shortcuts and the app’s own as well. The platform ends both when the window stops being the active one (the grab also when the surface loses focus), and sets their signals back to false: the app locks again, say, on the next click. cursor is the cursor over it: the platform’s, none, or the app’s own image.

ⓘ
GpuSurface::new()
    .label("Machine")
    .on_ready(move |surface| renderer.send(Message::Surface(surface)))
    .on_resize(move |size| renderer.send(Message::Resize(size)))
    .on_input(move |input| machine.send(Message::Input(input)))
    .pointer_lock(captured)
    .keyboard_grab(captured)

Implementations§

Source§

impl GpuSurface

Source

pub fn new() -> GpuSurface

Source

pub fn label(self, label: impl IntoValue<String>) -> GpuSurface

Its accessible name: what the app draws there.

Source

pub fn on_window_active(self, handler: impl Fn(bool) + 'static) -> GpuSurface

The window it’s in became the active one, or stopped being it. The surface keeps its focus through this: a caret it paints should stop anyway, as a text field’s does.

Source

pub fn on_ready(self, handler: impl Fn(SurfaceHandle) + 'static) -> GpuSurface

The native surface exists: make the GPU surface on it. Called once.

Source

pub fn on_resize(self, handler: impl Fn(SurfaceSize) + 'static) -> GpuSurface

Its size in pixels, or its scale, changed: configure the GPU surface for it.

Source

pub fn on_input(self, handler: impl Fn(SurfaceInput) + 'static) -> GpuSurface

Takes keys and the pointer, and reports them: a key down or up, the pointer’s moves, buttons and scrolling over it, and while it’s locked, how far it moved. Keys down when it loses focus, or its window stops being the active one, are reported released then.

Source

pub fn pointer_lock(self, locked: Signal<bool>) -> GpuSurface

Hides and holds the cursor while locked is true, reporting its moves as SurfaceInput::Motion, and before the host’s acceleration as SurfaceInput::RawMotion. The platform ends it when the window stops being the active one, and locked goes back to false.

Source

pub fn cursor(self, cursor: impl IntoValue<Cursor>) -> GpuSurface

The pointer’s cursor over it, while it isn’t locked: the platform’s own, none, or an image of the app’s (the one a machine gives its pointer, say).

Source

pub fn text_input(self, on: impl IntoValue<bool>) -> GpuSurface

Takes text, as a text field does, while on is true: keys report what they type (SurfaceInput::Key’s text), and the platform’s input methods are on while it has focus, committing as SurfaceInput::Text. With on_input, which reports them.

Source

pub fn input_method(self, on: impl IntoValue<bool>) -> GpuSurface

Whether the platform’s input methods are on while it takes text, as they are unless on is false: off, keys still report what they type (layouts, dead keys, AltGr), but nothing composes. For a modal editor, whose keys outside insert mode are commands.

Source

pub fn text_cursor(self, caret: impl IntoValue<Option<Rect>>) -> GpuSurface

Where the caret is, in points from its top left, while it takes text: the input method’s candidate window opens by it.

Source

pub fn transparent(self, on: impl IntoValue<bool>) -> GpuSurface

Lets the window’s backdrop show through it while on is true, where what the app presents isn’t opaque: AppKit puts its behind-window vibrancy under it, and WinUI’s windows have Mica already; GTK and Kirigami have no backdrop, so the window’s own background shows. The app presents with alpha, premultiplied as the compositor reads it (wgpu’s PostMultiplied mode on Metal, a composition swapchain with PreMultiplied on Direct3D 12).

Source

pub fn keyboard_grab(self, grabbed: Signal<bool>) -> GpuSurface

Takes every key while grabbed is true, the system’s shortcuts (as far as the platform lets an app) and the window’s own too, and focuses the surface. The platform ends it when the surface or its window loses focus, and grabbed goes back to false.

Trait Implementations§

Source§

impl Default for GpuSurface

Source§

fn default() -> GpuSurface

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

impl ElementBuilder for GpuSurface

Source§

fn element(&mut self) -> &mut Element

Source§

fn style(self, f: impl FnOnce(&mut Style)) -> Self

Escape hatch: edit the style directly (static).
Source§

fn style_with(self, f: impl Fn(&mut Style) + 'static) -> Self

Escape hatch: edit the style reactively. f re-runs whenever the signals it reads change, and must set every field it cares about.
Source§

fn width(self, value: impl IntoValue<Length>) -> Self

Source§

fn height(self, value: impl IntoValue<Length>) -> Self

Source§

fn min_width(self, value: impl IntoValue<Length>) -> Self

Source§

fn min_height(self, value: impl IntoValue<Length>) -> Self

Source§

fn max_width(self, value: impl IntoValue<Length>) -> Self

Source§

fn max_height(self, value: impl IntoValue<Length>) -> Self

Source§

fn aspect_ratio(self, value: impl IntoValue<f32>) -> Self

Source§

fn padding(self, value: impl IntoValue<Length>) -> Self

Source§

fn padding_x(self, value: impl IntoValue<Length>) -> Self

Source§

fn padding_y(self, value: impl IntoValue<Length>) -> Self

Source§

fn padding_start(self, value: impl IntoValue<Length>) -> Self

Source§

fn padding_end(self, value: impl IntoValue<Length>) -> Self

Source§

fn padding_top(self, value: impl IntoValue<Length>) -> Self

Source§

fn padding_bottom(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin_x(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin_y(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin_start(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin_end(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin_top(self, value: impl IntoValue<Length>) -> Self

Source§

fn margin_bottom(self, value: impl IntoValue<Length>) -> Self

Source§

fn grow(self, value: impl IntoValue<f32>) -> Self

Source§

fn shrink(self, value: impl IntoValue<f32>) -> Self

Source§

fn basis(self, value: impl IntoValue<Length>) -> Self

Source§

fn align_self(self, value: impl IntoValue<Align>) -> Self

Source§

fn justify_self(self, value: impl IntoValue<Align>) -> Self

Alignment inside a grid cell along the inline axis.
Source§

fn top(self, value: impl IntoValue<Length>) -> Self

Inset from the top of the containing block (with absolute()).
Source§

fn bottom(self, value: impl IntoValue<Length>) -> Self

Source§

fn start(self, value: impl IntoValue<Length>) -> Self

Inset from the inline start (left in LTR, right in RTL).
Source§

fn end(self, value: impl IntoValue<Length>) -> Self

Source§

fn grid_column(self, value: impl IntoValue<GridPlacement>) -> Self

Source§

fn grid_row(self, value: impl IntoValue<GridPlacement>) -> Self

Source§

fn direction(self, value: impl IntoValue<TextDirection>) -> Self

Source§

fn hidden(self, value: impl IntoValue<bool>) -> Self

Not laid out, not visible, not focusable, not in the a11y tree. Un-hiding restores whatever display the node had.
Source§

fn size( self, width: impl IntoValue<Length>, height: impl IntoValue<Length>, ) -> Self

Source§

fn absolute(self) -> Self

Takes the node out of flow; position it with top/start/….
Source§

fn tooltip(self, text: impl IntoValue<String>) -> Self

A tooltip: text the platform shows, its own way, when the pointer rests on the widget, and assistive technology reads as its description. Empty: none.
Source§

fn context_menu(self, entries: impl MenuEntries) -> Self

A context menu: the platform shows it its own way, on a right-click, a long press or the menu key, and over children without one of their own. Takes items, separators and submenus, as a Menu does, with the same reactive titles, states and check marks: Read more
Source§

fn context_menu_with<E>(self, entries: impl Fn() -> E + 'static) -> Self
where E: MenuEntries,

A context menu built by entries, and built again whenever what it reads changes, as Menu::children_with builds its entries.
Source§

fn context_menu_on_open<E>(self, entries: impl Fn() -> E + 'static) -> Self
where E: MenuEntries,

A context menu built by entries each time the platform opens it, for a menu that depends on what’s selected or under the pointer at that moment: a table row’s menu acting on the selection. What it reads is read then, not followed.
Source§

fn a11y_label(self, label: impl IntoValue<String>) -> Self

Source§

fn a11y_description(self, description: impl IntoValue<String>) -> Self

Source§

fn a11y_role(self, role: Role) -> Self

Source§

fn a11y_hidden(self) -> Self

Source§

fn on_focus(self, handler: impl Fn() + 'static) -> Self

Called when this control gains keyboard focus.
Source§

fn on_blur(self, handler: impl Fn() + 'static) -> Self

Called when this control loses keyboard focus.
Source§

fn tab_index(self, index: u32) -> Self

Moves this control ahead in the Tab order. Controls with a tab index come first (lowest first); the rest follow in reading order. Prefer arranging the tree in the order users should visit it.
Source§

fn node_ref(self, node: NodeRef) -> Self

Points node at this widget’s node, for use_size and to focus it from code (NodeRef::focus).
Source§

fn test_id(self, id: impl Into<String>) -> Self

Last-resort handle for tests; prefer finding nodes by role and name.
Source§

impl View for GpuSurface

Source§

fn build(self, ui: &Ui) -> NodeId

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<V> Children for V
where V: View,

Source§

fn into_views(self, out: &mut Vec<AnyView>)

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.