Skip to content

Commit

Permalink
Minimise the need to batch operations in PlainEditor (#192)
Browse files Browse the repository at this point in the history
The current design of `PlainEditor` and `PlainEditorTransaction` is that
if the editor is accessible, the inner layout is always valid and
up-to-date (barring a caught panic in the `transact` method). This is
not ideal for Masonry, because it forces event handling to recreate the
full layout even if the old version could be reused; this potential
reuse is especially prevalent in updates driven by Xilem.

This does bring in another change from Masonry, which is `StyleSet`;
this is a set of style properties, differentiated by their kind. It can
contain at most one font size, line height, etc.
This has some potentially unexpected behaviour with `FontStack`s, as in
the other APIs they "combine", but in this format they. However, this
combining behaviour is not documented as far as I can see, so I think
it's reasonable.
This implemented using a hashmap of discriminants in the enum.
The current API of `Arc<[StyleProperty]>` is horrendously unergonomic.

To reflect this change, I've renamed `PlainEditorTxn` to
`PlainEditorDriver`. Suggestions for an alternative name are welcome.

---------

Co-authored-by: Tom Churchman <[email protected]>
  • Loading branch information
DJMcNab and tomcur authored Dec 5, 2024
1 parent dbb38bd commit 211878f
Show file tree
Hide file tree
Showing 10 changed files with 819 additions and 310 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ This release has an [MSRV] of 1.75.

- `Generation` on `PlainEditor` to help implement lazy drawing. ([#143] by [@xorgy])

### Changed

### Parley

- Breaking change: `PlainEditor`'s semantics are no longer transactional ([#192][] by [@DJMcNab][])

## [0.2.0] - 2024-10-10

Expand Down Expand Up @@ -76,6 +81,7 @@ This release has an [MSRV] of 1.70.
[MSRV]: README.md#minimum-supported-rust-version-msrv

[@dfrg]: https://github.com/dfrg
[@DJMcNab]: https://github.com/DJMcNab
[@nicoburns]: https://github.com/nicoburns
[@waywardmonkeys]: https://github.com/waywardmonkeys
[@xorgy]: https://github.com/xorgy
Expand All @@ -93,6 +99,7 @@ This release has an [MSRV] of 1.70.
[#126]: https://github.com/linebender/parley/pull/126
[#129]: https://github.com/linebender/parley/pull/129
[#143]: https://github.com/linebender/parley/pull/143
[#192]: https://github.com/linebender/parley/pull/192

[Unreleased]: https://github.com/linebender/parley/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/linebender/parley/releases/tag/v0.2.0
Expand Down
25 changes: 7 additions & 18 deletions examples/vello_editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ mod access_ids;
use access_ids::{TEXT_INPUT_ID, WINDOW_ID};

mod text;
use parley::{GenericFamily, StyleProperty};

const WINDOW_TITLE: &str = "Vello Text Editor";

Expand Down Expand Up @@ -114,15 +113,10 @@ impl ApplicationHandler<accesskit_winit::Event> for SimpleVelloApp<'_> {
let access_adapter =
accesskit_winit::Adapter::with_event_loop_proxy(&window, self.event_loop_proxy.clone());
window.set_visible(true);
window.set_ime_allowed(true);

let size = window.inner_size();

self.editor.transact(|txn| {
txn.set_scale(1.0);
txn.set_width(Some(size.width as f32 - 2f32 * text::INSET));
txn.set_text(text::LOREM);
});

// Create a vello Surface
let surface_future = {
let surface = self
Expand Down Expand Up @@ -236,15 +230,9 @@ impl ApplicationHandler<accesskit_winit::Event> for SimpleVelloApp<'_> {
WindowEvent::Resized(size) => {
self.context
.resize_surface(&mut render_state.surface, size.width, size.height);
self.editor.transact(|txn| {
txn.set_scale(1.0);
txn.set_width(Some(size.width as f32 - 2f32 * text::INSET));
txn.set_default_style(Arc::new([
StyleProperty::FontSize(32.0),
StyleProperty::LineHeight(1.2),
GenericFamily::SystemUi.into(),
]));
});
let editor = self.editor.editor();
editor.set_scale(1.0);
editor.set_width(Some(size.width as f32 - 2f32 * text::INSET));
render_state.window.request_redraw();
}

Expand Down Expand Up @@ -352,7 +340,7 @@ fn main() -> Result<()> {
renderers: vec![],
state: RenderState::Suspended(None),
scene: Scene::new(),
editor: text::Editor::default(),
editor: text::Editor::new(text::LOREM),
last_drawn_generation: Default::default(),
event_loop_proxy: event_loop.create_proxy(),
};
Expand All @@ -361,7 +349,8 @@ fn main() -> Result<()> {
event_loop
.run_app(&mut app)
.expect("Couldn't run event loop");
print!("{}", app.editor.text());
let text = app.editor.text();
print!("{text}");
Ok(())
}

Expand Down
Loading

0 comments on commit 211878f

Please sign in to comment.