Skip to content

Commit 907ad28

Browse files
committed
Use parley selection
1 parent 5fd6488 commit 907ad28

File tree

2 files changed

+36
-53
lines changed

2 files changed

+36
-53
lines changed

internal/core/textlayout/sharedparley.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ std::thread_local! {
2828

2929
#[derive(Debug, Default, PartialEq, Clone, Copy)]
3030
pub struct Brush {
31-
pub is_selected: bool,
3231
pub stroke: Option<TextStrokeStyle>,
3332
}
3433

@@ -38,7 +37,6 @@ pub struct LayoutOptions {
3837
pub horizontal_align: TextHorizontalAlignment,
3938
pub vertical_align: TextVerticalAlignment,
4039
pub stroke: Option<TextStrokeStyle>,
41-
pub selection: Option<std::ops::Range<usize>>,
4240
pub font_request: Option<FontRequest>,
4341
pub text_wrap: TextWrap,
4442
pub text_overflow: TextOverflow,
@@ -52,7 +50,6 @@ impl Default for LayoutOptions {
5250
horizontal_align: TextHorizontalAlignment::Left,
5351
vertical_align: TextVerticalAlignment::Top,
5452
stroke: None,
55-
selection: None,
5653
font_request: None,
5754
text_wrap: TextWrap::WordWrap,
5855
text_overflow: TextOverflow::Clip,
@@ -104,16 +101,7 @@ pub fn layout(text: &str, scale_factor: f32, options: LayoutOptions) -> Layout {
104101
todo!();
105102
}
106103

107-
builder.push_default(parley::StyleProperty::Brush(Brush {
108-
stroke: options.stroke,
109-
..Default::default()
110-
}));
111-
if let Some(selection) = options.selection {
112-
builder.push(
113-
parley::StyleProperty::Brush(Brush { stroke: options.stroke, is_selected: true }),
114-
selection,
115-
);
116-
}
104+
builder.push_default(parley::StyleProperty::Brush(Brush { stroke: options.stroke }));
117105

118106
builder.build(text)
119107
});

internal/renderers/femtovg/itemrenderer.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ fn draw_glyphs<R: femtovg::Renderer + TextureImporter>(
199199
layout: &sharedparley::Layout,
200200
canvas: &mut Canvas<R>,
201201
paint: &femtovg::Paint,
202-
selection_foreground_color: Color,
203-
selection_background_color: Color,
204202
) {
205203
for line in layout.lines() {
206204
for item in line.items() {
@@ -213,30 +211,6 @@ fn draw_glyphs<R: femtovg::Renderer + TextureImporter>(
213211

214212
let brush = glyph_run.style().brush;
215213

216-
let mut paint = paint.clone();
217-
if brush.is_selected {
218-
if let Some(start) = glyph_run.positioned_glyphs().next() {
219-
let selection_rect = PhysicalRect::new(
220-
PhysicalPoint::new(
221-
start.x,
222-
start.y + layout.y_offset - run.font_size(),
223-
),
224-
PhysicalSize::new(
225-
glyph_run.advance(),
226-
run.font_size() + run.metrics().descent,
227-
),
228-
);
229-
canvas.fill_path(
230-
&rect_to_path(selection_rect),
231-
&femtovg::Paint::color(to_femtovg_color(
232-
&selection_background_color,
233-
)),
234-
);
235-
}
236-
237-
paint.set_color(to_femtovg_color(&selection_foreground_color));
238-
}
239-
240214
let glyphs = glyph_run.positioned_glyphs().map(|glyph: parley::Glyph| {
241215
femtovg::PositionedGlyph {
242216
x: glyph.x,
@@ -248,15 +222,15 @@ fn draw_glyphs<R: femtovg::Renderer + TextureImporter>(
248222

249223
match brush.stroke {
250224
Some(i_slint_core::items::TextStrokeStyle::Outside) => {
251-
canvas.stroke_glyphs(glyphs.clone(), &paint).unwrap();
252-
canvas.fill_glyphs(glyphs, &paint).unwrap();
225+
canvas.stroke_glyphs(glyphs.clone(), paint).unwrap();
226+
canvas.fill_glyphs(glyphs, paint).unwrap();
253227
}
254228
Some(i_slint_core::items::TextStrokeStyle::Center) => {
255-
canvas.fill_glyphs(glyphs.clone(), &paint).unwrap();
256-
canvas.stroke_glyphs(glyphs, &paint).unwrap();
229+
canvas.fill_glyphs(glyphs.clone(), paint).unwrap();
230+
canvas.stroke_glyphs(glyphs, paint).unwrap();
257231
}
258232
None => {
259-
canvas.fill_glyphs(glyphs, &paint).unwrap();
233+
canvas.fill_glyphs(glyphs, paint).unwrap();
260234
}
261235
}
262236
}
@@ -463,7 +437,7 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
463437

464438
let mut canvas = self.canvas.borrow_mut();
465439

466-
draw_glyphs(&layout, &mut canvas, &paint, Color::default(), Color::default());
440+
draw_glyphs(&layout, &mut canvas, &paint);
467441
}
468442

469443
fn draw_text_input(
@@ -517,19 +491,40 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
517491
max_physical_width: Some(width * self.scale_factor),
518492
max_height: Some(height),
519493
vertical_align: text_input.vertical_alignment(),
520-
selection: Some(min_select..max_select),
521494
font_request: Some(font_request),
522495
..Default::default()
523496
},
524497
);
525-
draw_glyphs(
526-
&layout,
527-
&mut canvas,
528-
&paint,
529-
text_input.selection_foreground_color(),
530-
text_input.selection_background_color(),
498+
499+
let selection = parley::layout::cursor::Selection::new(
500+
parley::layout::cursor::Cursor::from_byte_index(
501+
&layout,
502+
min_select,
503+
Default::default(),
504+
),
505+
parley::layout::cursor::Cursor::from_byte_index(
506+
&layout,
507+
max_select,
508+
Default::default(),
509+
),
531510
);
532511

512+
selection.geometry_with(&layout, |rect, _| {
513+
let mut cursor_rect = femtovg::Path::new();
514+
cursor_rect.rect(
515+
rect.min_x() as _,
516+
rect.min_y() as f32 + layout.y_offset,
517+
rect.width() as _,
518+
rect.height() as _,
519+
);
520+
canvas.fill_path(
521+
&cursor_rect,
522+
&femtovg::Paint::color(to_femtovg_color(&text_input.selection_background_color())),
523+
);
524+
});
525+
526+
draw_glyphs(&layout, &mut canvas, &paint);
527+
533528
if cursor_visible {
534529
let cursor = parley::layout::cursor::Cursor::from_byte_index(
535530
&layout,
@@ -980,7 +975,7 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
980975
let layout = sharedparley::layout(string, self.scale_factor.get(), Default::default());
981976
let paint = femtovg::Paint::color(to_femtovg_color(&color));
982977
let mut canvas = self.canvas.borrow_mut();
983-
draw_glyphs(&layout, &mut canvas, &paint, color, color);
978+
draw_glyphs(&layout, &mut canvas, &paint);
984979
}
985980

986981
fn draw_image_direct(&mut self, image: i_slint_core::graphics::Image) {

0 commit comments

Comments
 (0)