pub trait View {
// Required method
fn render(&self, ctx: &mut RenderContext<'_>);
// Provided methods
fn widget_type(&self) -> &'static str { ... }
fn id(&self) -> Option<&str> { ... }
fn classes(&self) -> &[String] { ... }
fn children(&self) -> &[Box<dyn View>] { ... }
fn meta(&self) -> WidgetMeta { ... }
}Expand description
The core trait for all renderable components
Every widget in Revue implements the View trait, which provides:
- Rendering via
render() - CSS selector support via
id(),classes(), andwidget_type() - Child exposure for container widgets via
children() - Metadata generation via
meta()
§Implementing View
At minimum, you only need to implement render:
use revue::prelude::*;
struct MyWidget {
text: String,
}
impl View for MyWidget {
fn render(&self, ctx: &mut RenderContext) {
ctx.draw_text(0, 0, &self.text, Color::WHITE);
}
}§CSS Selector Support
Widgets can be styled via CSS using three selector types:
-
Type selector - All widgets of a given type
MyWidget { color: red; } -
ID selector - A specific widget (unique identifier)
ⓘwidget.id("my-special-widget");#my-special-widget { color: blue; } -
Class selector - Widgets with a specific class
ⓘwidget.class("primary").class("active");.primary { color: green; } .active { font-weight: bold; }
§Container Widgets
Container widgets (like Stack, Grid) should override children()
to expose their children for DOM traversal:
impl View for MyContainer {
fn children(&self) -> &[Box<dyn View>] {
&self.children
}
// ...
}§View Trait Object
View can be used as a trait object (Box<dyn View>) for dynamic polymorphism:
fn render_any_widget(widget: Box<dyn View>, ctx: &mut RenderContext) {
widget.render(ctx);
}Required Methods§
Sourcefn render(&self, ctx: &mut RenderContext<'_>)
fn render(&self, ctx: &mut RenderContext<'_>)
Render the view to the given context
The RenderContext provides drawing primitives like:
draw_text()- Render text at a positionfill()- Fill a region with a character/colordraw_border()- Draw bordersclear()- Clear a region
§Example
fn render(&self, ctx: &mut RenderContext) {
ctx.clear(ctx.area());
ctx.draw_text(0, 0, &self.label, Color::WHITE);
}Provided Methods§
Sourcefn widget_type(&self) -> &'static str
fn widget_type(&self) -> &'static str
Get widget type name (for CSS type selectors)
The default implementation extracts the type name from the Rust type.
For example, MyApp::MyWidget becomes "MyWidget".
Override this if you want a custom type name for CSS matching:
fn widget_type(&self) -> &'static str {
"button" // Always match as "button" regardless of Rust type
}Sourcefn id(&self) -> Option<&str>
fn id(&self) -> Option<&str>
Get element ID (for CSS #id selectors)
Returns None by default. Override to provide a unique ID:
struct MyWidget {
id: Option<String>,
}
impl View for MyWidget {
fn id(&self) -> Option<&str> {
self.id.as_deref()
}
}Sourcefn classes(&self) -> &[String]
fn classes(&self) -> &[String]
Get CSS classes (for CSS .class selectors)
Returns an empty slice by default. Override to provide classes:
struct MyWidget {
classes: Vec<String>,
}
impl View for MyWidget {
fn classes(&self) -> &[String] {
&self.classes
}
}Sourcefn children(&self) -> &[Box<dyn View>]
fn children(&self) -> &[Box<dyn View>]
Get child views (for container widgets)
Container widgets (Stack, Grid, etc.) should override this to expose their children, enabling the DOM builder to traverse the full widget tree.
The returned slice should contain boxed trait objects to enable heterogeneous child collections.
§Example
struct MyContainer {
children: Vec<Box<dyn View>>,
}
impl View for MyContainer {
fn children(&self) -> &[Box<dyn View>] {
&self.children
}
}Sourcefn meta(&self) -> WidgetMeta
fn meta(&self) -> WidgetMeta
Get widget metadata for DOM
This method combines widget_type(), id(), and classes() into
a WidgetMeta struct used by the DOM builder. You typically don’t need
to override this.
Trait Implementations§
Source§impl View for Box<dyn View>
Implement View for Box<dyn View> to allow boxed views to be used as children
impl View for Box<dyn View>
Implement View for Box<dyn View> to allow boxed views to be used as children
Source§fn render(&self, ctx: &mut RenderContext<'_>)
fn render(&self, ctx: &mut RenderContext<'_>)
Source§fn widget_type(&self) -> &'static str
fn widget_type(&self) -> &'static str
Source§fn meta(&self) -> WidgetMeta
fn meta(&self) -> WidgetMeta
Implementations on Foreign Types§
Source§impl View for Box<dyn View>
Implement View for Box<dyn View> to allow boxed views to be used as children
impl View for Box<dyn View>
Implement View for Box<dyn View> to allow boxed views to be used as children