Proc macros for components, views

This commit is contained in:
2026-03-21 20:50:13 -04:00
parent 497dff987e
commit 437b318ad9
6 changed files with 625 additions and 5 deletions

View File

@@ -6,9 +6,14 @@ edition = "2024"
[dependencies]
ruin_reactivity = { path = "../reactivity" }
ruin_runtime = { package = "ruin-runtime", path = "../runtime" }
ruin_app_proc_macros = { package = "ruin-app-proc-macros", path = "../ruin_app_proc_macros" }
ruin_ui = { path = "../ui" }
ruin_ui_platform_wayland = { path = "../ui_platform_wayland" }
[[example]]
name = "00_bootstrap_and_counter_raw"
path = "example/00_bootstrap_and_counter_raw.rs"
[[example]]
name = "00_bootstrap_and_counter"
path = "example/00_bootstrap_and_counter.rs"

View File

@@ -0,0 +1,95 @@
use ruin_app::prelude::*;
use ruin_ui::Border;
#[ruin_runtime::async_main]
async fn main() -> ruin_app::Result<()> {
let title = "RUIN Counter";
App::new()
.window(
Window::new()
.title(title)
.app_id("dev.ruin.counter")
.size(960.0, 640.0),
)
.mount(view! {
CounterApp(title = title) {}
})
.run()
.await
}
#[component]
fn CounterApp(title: &'static str) -> impl IntoView {
let count = use_signal(|| 0_i32);
let doubled = use_memo({
let count = count.clone();
move || count.get() * 2
});
use_window_title({
let count = count.clone();
move || format!("{title} ({})", count.get())
});
view! {
column(gap = 16.0, padding = 24.0) {
text(role = TextRole::Heading(1), size = 32.0, weight = FontWeight::Semibold) {
title
}
CounterActions(count = count.clone()) {}
block(
padding = 16.0,
gap = 8.0,
background = surfaces::raised(),
border_radius = 12.0,
border = Border::new(2.0, Color::rgb(0, 0, 0))
) {
text(size = 18.0) { "count = "; count.clone() }
text(color = colors::muted()) { "double = "; doubled.clone() }
}
}
}
}
#[component]
fn CounterActions(count: Signal<i32>) -> impl IntoView {
view! {
row(gap = 8.0) {
button(
on_press = {
let count = count.clone();
move |_| {
count.update(|value| *value -= 1);
}
},
) {
"-1"
}
button(
on_press = {
let count = count.clone();
move |_| {
let _ = count.set(0);
}
},
) {
"Reset"
}
button(
on_press = {
let count = count.clone();
move |_| {
count.update(|value| *value += 1);
}
},
) {
"+1"
}
}
}
}

View File

@@ -12,13 +12,15 @@ use std::rc::Rc;
use ruin_reactivity::effect;
use ruin_ui::{
Color, CursorIcon, Edges, Element, ElementId, InteractionTree, LayoutSnapshot, PlatformEvent,
PointerButton, PointerEvent, PointerRouter, RoutedPointerEvent, RoutedPointerEventKind,
TextFontFamily, TextSpan, TextSpanWeight, TextStyle, TextSystem, UiSize, WindowController,
WindowSpec, WindowUpdate, layout_snapshot_with_text_system,
Border, Color, CursorIcon, Edges, Element, ElementId, InteractionTree, LayoutSnapshot,
PlatformEvent, PointerButton, PointerEvent, PointerRouter, RoutedPointerEvent,
RoutedPointerEventKind, TextFontFamily, TextSpan, TextSpanWeight, TextStyle, TextSystem,
UiSize, WindowController, WindowSpec, WindowUpdate, layout_snapshot_with_text_system,
};
use ruin_ui_platform_wayland::start_wayland_ui;
pub use ruin_app_proc_macros::{component, view};
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
#[derive(Clone, Debug)]
@@ -564,6 +566,11 @@ impl ContainerBuilder {
self
}
pub fn border(mut self, border: Border) -> Self {
self.element = self.element.border(border.width, border.color);
self
}
pub fn width(mut self, width: f32) -> Self {
self.element = self.element.width(width);
self
@@ -949,7 +956,8 @@ pub mod prelude {
pub use crate::{
App, ButtonBuilder, Children, Component, ContainerBuilder, FontWeight, IntoEdges, IntoView,
Memo, Mountable, Result, Signal, TextBuilder, TextChildren, TextRole, View, Window, block,
button, colors, column, row, surfaces, text, use_memo, use_signal, use_window_title,
button, colors, column, component, row, surfaces, text, use_memo, use_signal,
use_window_title, view,
};
pub use ruin_ui::{
Color, CursorIcon, Edges, Element, ElementId, PointerButton, PointerEventKind,