Skip to content

Commit

Permalink
Add basic action-based editor, minorly improved from vello_editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
xorgy committed Oct 7, 2024
1 parent e5370ce commit 509bb23
Show file tree
Hide file tree
Showing 5 changed files with 635 additions and 251 deletions.
27 changes: 21 additions & 6 deletions examples/vello_editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use winit::window::Window;

// #[path = "text2.rs"]
mod text;
use parley::layout::editor::PlainEditorOp;

// Simple struct to hold the state of the renderer
pub struct ActiveRenderState<'s> {
Expand Down Expand Up @@ -47,7 +48,7 @@ struct SimpleVelloApp<'s> {
scene: Scene,

// Our text state object
editor: text::Editor,
editor: text::Editor<'s>,
}

impl ApplicationHandler for SimpleVelloApp<'_> {
Expand All @@ -70,7 +71,12 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
wgpu::PresentMode::AutoVsync,
);
let surface = pollster::block_on(surface_future).expect("Error creating surface");
self.editor.update_layout(size.width as _, 1.0);

self.editor.transact([
PlainEditorOp::SetScale(1.0),
PlainEditorOp::SetWidth(size.width as f32 - 2f32 * text::INSET),
PlainEditorOp::SetText(text::LOREM.into()),
]);

// Create a vello Renderer for the surface (using its device id)
self.renderers
Expand Down Expand Up @@ -107,7 +113,7 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
_ => return,
};

self.editor.handle_event(&event);
self.editor.handle_event(event.clone());
render_state.window.request_redraw();
// render_state
// .window
Expand All @@ -122,7 +128,17 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
self.context
.resize_surface(&mut render_state.surface, size.width, size.height);
render_state.window.request_redraw();
self.editor.update_layout(size.width as _, 1.0);
self.editor.transact([
PlainEditorOp::SetScale(1.0),
PlainEditorOp::SetWidth(size.width as f32 - 2f32 * text::INSET),
PlainEditorOp::SetDefaultStyle(Arc::new([
parley::style::StyleProperty::FontSize(32.0),
parley::style::StyleProperty::LineHeight(1.2),
parley::style::StyleProperty::FontStack(parley::style::FontStack::Source(
"system-ui",
)),
])),
]);
}

// This is where all the rendering happens
Expand Down Expand Up @@ -189,13 +205,12 @@ fn main() -> Result<()> {
editor: text::Editor::default(),
};

app.editor.set_text(text::LOREM);

// Create and run a winit event loop
let event_loop = EventLoop::new()?;
event_loop
.run_app(&mut app)
.expect("Couldn't run event loop");
print!("{}", app.editor.text());
Ok(())
}

Expand Down
Loading

0 comments on commit 509bb23

Please sign in to comment.