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
impl ViewContext
Sourcepub fn register_font(&self, data: impl Into<FontBlob>) -> Vec<String>
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());Sourcepub fn spawn(&self, future: impl Future<Output = ()> + 'static)
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.
Sourcepub fn wake_sender(&self) -> WakeSender
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.
Sourcepub fn request_redraw(&self)
pub fn request_redraw(&self)
Request a redraw for this view’s window.
Sourcepub fn close_window(&self)
pub fn close_window(&self)
Close this view’s window.
Sourcepub fn fps(&self) -> f64
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()));
}
});Sourcepub fn fps_cell(&self) -> Rc<Cell<f64>>
pub fn fps_cell(&self) -> Rc<Cell<f64>>
Returns the shared FPS counter — callers may read or write.
Sourcepub fn request_frame(&self, callback: impl FnMut(FrameInfo) -> bool + 'static)
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§
impl !Freeze for ViewContext
impl !RefUnwindSafe for ViewContext
impl !Send for ViewContext
impl !Sync for ViewContext
impl Unpin for ViewContext
impl UnsafeUnpin for ViewContext
impl !UnwindSafe for ViewContext
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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