Skip to main content

ViewContext

Struct ViewContext 

Source
pub struct ViewContext { /* private fields */ }
Expand description

The user-facing API inside a view.

Passed to the view’s init closure. Provides access to:

  • The document (DOM tree, via FrameWidget)
  • The cross-thread sender (for giving to background tasks)
  • The platform host (for requesting redraws, setting title, etc.)
  • The window identity (which window this view belongs to)

§Example

app.window(WindowConfig::default(), |ctx| {
    let doc = ctx.document();
    let btn = doc.create::<HtmlButtonElement>();
    btn.set_text("Hello!");
    doc.root().append(btn);
});

Implementations§

Source§

impl ViewContext

Source

pub fn document(&self) -> &Document

Read-only access to the document.

Source

pub fn register_font(&self, data: impl Into<FontBlob>) -> Vec<String>

Register custom font data (TTF/OTF/TTC bytes) into the font system.

Chrome equivalent: document.fonts.add(new FontFace(...)). After registration, the font’s family name is available for CSS font-family matching. The family name is auto-detected from the font file’s name table.

Accepts &'static [u8] (zero-copy for include_bytes!()), Vec<u8> (runtime-loaded), or Arc<[u8]> (pre-shared).

Returns the registered family names.

§Example
// Static — zero copy:
ctx.register_font(include_bytes!("../assets/Cairo.ttf") as &[u8]);

// Runtime — from a file:
ctx.register_font(std::fs::read("font.ttf").unwrap());
Source

pub fn spawn(&self, future: impl Future<Output = ()> + 'static)

Spawn a !Send async task on the view thread’s executor.

The future runs on the view thread — it can safely capture and mutate DOM handles across .await points. No Arc, no Mutex, no WakeSender needed.

ctx.spawn(async move {
    kozan_platform::time::sleep(Duration::from_millis(500)).await;
    card.set_style(activated_style());
});

If called during the init closure the future is queued and started on the first scheduler tick. If called later (e.g. from an event handler posted via WakeSender) it is spawned into the executor immediately — use WakeSender::post for that case.

Source

pub fn wake_sender(&self) -> WakeSender

Get a clone of the cross-thread sender.

Give this to background threads so they can send results back to this view’s scheduler.

Source

pub fn request_redraw(&self)

Request a redraw for this view’s window.

Source

pub fn set_title(&self, title: &str)

Set the window title.

Source

pub fn close_window(&self)

Close this view’s window.

Source

pub fn window_id(&self) -> WindowId

The WindowId this view belongs to.

Source

pub fn fps(&self) -> f64

Current FPS — updated each frame by the scheduler.

Returns 0.0 on the first frame. Use this to build FPS overlays.

let fps_rc = ctx.fps_cell();
ctx.spawn(async move {
    loop {
        sleep(Duration::from_millis(200)).await;
        label.set_text(&format!("{:.0} FPS", fps_rc.get()));
    }
});
Source

pub fn fps_cell(&self) -> Rc<Cell<f64>>

Returns the shared FPS counter — callers may read or write.

Source

pub fn request_frame(&self, callback: impl FnMut(FrameInfo) -> bool + 'static)

Register a frame callback — like requestAnimationFrame.

Returns bool: true = keep for next frame, false = one-shot.

// One-shot:
ctx.request_frame(|_info| { do_something(); false });

// Render loop (like requestAnimationFrame in a loop):
ctx.request_frame(move |info| {
    fps_label.set_content(format!("{:.0} FPS", info.fps));
    true // keep running
});

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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> ErasedDestructor for T
where T: 'static,