Skip to repository content174 lines · 5.7 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T04:31:35.972Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
view_example_main.rs
1#![cfg_attr(target_family = "wasm", no_main)]
2
3//! View example — composing a text input from the `View` primitives.
4//!
5//! The whole point: a text input is deceptively complicated, and `View` makes it
6//! easy to compose one. Three pieces, each shown in its own section:
7//!
8//! * `Editor` — the workhorse entity: cursor, blink, focus, keyboard, and a
9//! specialized text renderer. All the hard parts live here.
10//! * `String` — the data plane. `editor.text(cx)` / `value.read(cx)` get it out.
11//! * `Input` / `TextArea` — the shaping layer. Each takes a `String` (and grows
12//! the editor internally) OR an `Editor` (so you can read the cursor).
13//!
14//! Run: `cargo run -p gpui --example view_example`
15
16mod example_editor;
17mod example_input;
18mod example_text_area;
19
20#[cfg(test)]
21mod example_tests;
22
23use example_editor::Editor;
24use example_input::Input;
25use example_text_area::TextArea;
26
27use gpui::{
28 App, Bounds, Context, Div, Entity, IntoElement, KeyBinding, Render, SharedString, Window,
29 WindowBounds, WindowOptions, actions, div, hsla, prelude::*, px, rgb, size,
30};
31use gpui_platform::application;
32
33actions!(
34 view_example,
35 [Backspace, Delete, Left, Right, Home, End, Enter, Quit]
36);
37
38/// A tiny stateless view that reads an editor's cursor and is composed *beside*
39/// the thing editing it — two views over one entity, zero wiring.
40#[derive(IntoElement)]
41struct CursorReadout {
42 editor: Entity<Editor>,
43}
44
45impl CursorReadout {
46 fn new(editor: Entity<Editor>) -> Self {
47 Self { editor }
48 }
49}
50
51impl gpui::RenderOnce for CursorReadout {
52 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
53 let cursor = self.editor.read(cx).cursor;
54 div()
55 .text_sm()
56 .text_color(hsla(0., 0., 0.45, 1.))
57 .child(SharedString::from(format!("cursor @ {cursor}")))
58 }
59}
60
61struct ViewExample;
62
63impl ViewExample {
64 fn new() -> Self {
65 Self
66 }
67}
68
69impl Render for ViewExample {
70 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
71 // The data plane: plain strings, allocated at the top by the hook.
72 let name = window.use_state(cx, |_, _| String::new());
73 let email = window.use_state(cx, |_, _| String::from("me@example.com"));
74 let bio = window.use_state(cx, |_, _| String::new());
75 // Editors that own their own string internally — no extra wiring up top.
76 let notes = window.use_state(cx, |window, cx| Editor::new("multi\nline", window, cx));
77 let owned = window.use_state(cx, |window, cx| Editor::new("editable", window, cx));
78
79 div()
80 .flex()
81 .flex_col()
82 .size_full()
83 .bg(rgb(0xf0f0f0))
84 .p(px(24.))
85 .gap(px(24.))
86 .child(
87 section("Inputs — from a String (cursor stays internal)")
88 .child(Input::new(name).width(px(320.)))
89 .child(
90 Input::new(email)
91 .width(px(320.))
92 .color(hsla(0., 0., 0.3, 1.)),
93 ),
94 )
95 .child(
96 section("Input — from an Editor (read its cursor beside it)").child(
97 div()
98 .flex()
99 .items_center()
100 .gap(px(12.))
101 .child(Input::editor(owned.clone()).width(px(320.)))
102 .child(CursorReadout::new(owned)),
103 ),
104 )
105 .child(
106 section("Text areas — from a String, or from an Editor")
107 .child(TextArea::new(bio, 3))
108 .child(
109 div()
110 .flex()
111 .items_start()
112 .gap(px(12.))
113 .child(TextArea::editor(notes.clone(), 3).color(hsla(
114 250. / 360.,
115 0.7,
116 0.4,
117 1.,
118 )))
119 .child(CursorReadout::new(notes)),
120 ),
121 )
122 }
123}
124
125/// A labeled vertical section.
126fn section(title: &str) -> Div {
127 div().flex().flex_col().gap(px(8.)).child(
128 div()
129 .text_sm()
130 .text_color(hsla(0., 0., 0.3, 1.))
131 .child(SharedString::from(title.to_string())),
132 )
133}
134
135fn run_example() {
136 application().run(|cx: &mut App| {
137 let bounds = Bounds::centered(None, size(px(560.0), px(480.0)), cx);
138 cx.bind_keys([
139 KeyBinding::new("backspace", Backspace, None),
140 KeyBinding::new("delete", Delete, None),
141 KeyBinding::new("left", Left, None),
142 KeyBinding::new("right", Right, None),
143 KeyBinding::new("home", Home, None),
144 KeyBinding::new("end", End, None),
145 KeyBinding::new("enter", Enter, None),
146 KeyBinding::new("cmd-q", Quit, None),
147 ]);
148
149 cx.open_window(
150 WindowOptions {
151 window_bounds: Some(WindowBounds::Windowed(bounds)),
152 ..Default::default()
153 },
154 |_, cx| cx.new(|_| ViewExample::new()),
155 )
156 .unwrap();
157
158 cx.on_action(|_: &Quit, cx| cx.quit());
159 cx.activate(true);
160 });
161}
162
163#[cfg(not(target_family = "wasm"))]
164fn main() {
165 run_example();
166}
167
168#[cfg(target_family = "wasm")]
169#[wasm_bindgen::prelude::wasm_bindgen(start)]
170pub fn start() {
171 gpui_platform::web_init();
172 run_example();
173}
174