Skip to content

Commit

Permalink
Implement underlining for IME areas (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed May 7, 2024
1 parent 392c3c1 commit a0b0842
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
11 changes: 10 additions & 1 deletion crates/masonry/src/text2/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ impl<T: EditableText> TextEditor<T> {

pub fn rebuild(&mut self, fcx: &mut FontContext) {
// TODO: Add the pre-edit range as an underlined region in the text attributes
self.inner.rebuild(fcx);

self.inner.rebuild_with_attributes(fcx, |mut builder| {
if let Some(range) = self.preedit_range.as_ref() {
builder.push(
&parley::style::StyleProperty::Underline(true),
range.clone(),
);
}
builder
});
}

pub fn draw(&mut self, scene: &mut Scene, point: impl Into<Point>) {
Expand Down
19 changes: 15 additions & 4 deletions crates/masonry/src/text2/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::borrow::Cow;
use std::ops::{Deref, DerefMut, Range};

use kurbo::{Affine, Line, Point, Stroke};
use parley::context::RangedBuilder;
use parley::FontContext;
use unicode_segmentation::{GraphemeCursor, UnicodeSegmentation};
use vello::peniko::{Brush, Color};
Expand Down Expand Up @@ -210,28 +211,38 @@ impl<T: Selectable> TextWithSelection<T> {
self.needs_selection_update = true;
}

// Intentionally aliases the method on
pub fn rebuild(&mut self, fcx: &mut FontContext) {
// Intentionally aliases the method on `TextLayout`
pub fn rebuild_with_attributes(
&mut self,
fcx: &mut FontContext,
attributes: impl for<'b> FnOnce(
RangedBuilder<'b, TextBrush, &'b str>,
) -> RangedBuilder<'b, TextBrush, &'b str>,
) {
// In theory, we could be clever here and only rebuild the layout if the
// selected range was previously or currently non-zero size (i.e. there is a selected range)
if self.needs_selection_update || self.layout.needs_rebuild() {
self.layout.invalidate();
self.layout.rebuild_with_attributes(fcx, |mut builder| {
if let Some(selection) = self.selection {
let range = selection.min()..selection.max();
let range = selection.range();
if !range.is_empty() {
builder.push(
&parley::style::StyleProperty::Brush(self.highlight_brush.clone()),
range,
);
}
}
builder
attributes(builder)
});
self.needs_selection_update = false;
}
}

pub fn rebuild(&mut self, fcx: &mut FontContext) {
self.rebuild_with_attributes(fcx, |builder| builder);
}

pub fn draw(&mut self, scene: &mut Scene, point: impl Into<Point>) {
// TODO: Calculate the location for this in layout lazily?
if let Some(selection) = self.selection {
Expand Down

0 comments on commit a0b0842

Please sign in to comment.