Skip to content

Commit

Permalink
Properly handle row repositioning
Browse files Browse the repository at this point in the history
  • Loading branch information
afishhh committed Nov 29, 2024
1 parent f028154 commit bc86bec
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 191 deletions.
8 changes: 4 additions & 4 deletions crates/egui/src/text_selection/accesskit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ pub fn update_accesskit_for_text_widget(
};

ctx.with_accessibility_parent(parent_id, || {
for (row_index, (row, offset)) in galley.rows.iter().enumerate() {
for (row_index, row) in galley.rows.iter().enumerate() {
let row_id = parent_id.with(row_index);
ctx.accesskit_node_builder(row_id, |builder| {
builder.set_role(accesskit::Role::TextRun);
let rect = row.rect.translate(offset.to_vec2() + galley_pos.to_vec2());
let rect = row.rect().translate(galley_pos.to_vec2());
builder.set_bounds(accesskit::Rect {
x0: rect.min.x.into(),
y0: rect.min.y.into(),
Expand Down Expand Up @@ -74,14 +74,14 @@ pub fn update_accesskit_for_text_widget(
let old_len = value.len();
value.push(glyph.chr);
character_lengths.push((value.len() - old_len) as _);
character_positions.push(glyph.pos.x - row.rect.min.x);
character_positions.push(glyph.pos.x - row.pos.x);
character_widths.push(glyph.advance_width);
}

if row.ends_with_newline {
value.push('\n');
character_lengths.push(1);
character_positions.push(row.rect.max.x - row.rect.min.x);
character_positions.push(row.size.x);
character_widths.push(0.0);
}
word_lengths.push((character_lengths.len() - last_word_start) as _);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/text_selection/cursor_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn ccursor_from_accesskit_text_position(
position: &accesskit::TextPosition,
) -> Option<CCursor> {
let mut total_length = 0usize;
for (i, (row, _)) in galley.rows.iter().enumerate() {
for (i, row) in galley.rows.iter().enumerate() {
let row_id = id.with(i);
if row_id.accesskit_id() == position.node {
return Some(CCursor {
Expand Down
8 changes: 4 additions & 4 deletions crates/egui/src/text_selection/label_text_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ impl LabelSelectionState {
if let epaint::Shape::Text(text_shape) = &mut shape.shape {
let galley = Arc::make_mut(&mut text_shape.galley);
for row_selection in row_selections {
if let Some((row, _)) =
if let Some(placed_row) =
galley.rows.get_mut(row_selection.row)
{
let row = Arc::make_mut(row);
let row = Arc::make_mut(&mut placed_row.row);
for vertex_index in row_selection.vertex_indices {
if let Some(vertex) = row
.visuals
Expand Down Expand Up @@ -662,8 +662,8 @@ fn selected_text(galley: &Galley, cursor_range: &CursorRange) -> String {
}

fn estimate_row_height(galley: &Galley) -> f32 {
if let Some((row, _)) = galley.rows.first() {
row.rect.height()
if let Some(placed_row) = galley.rows.first() {
placed_row.height()
} else {
galley.size().y
}
Expand Down
19 changes: 10 additions & 9 deletions crates/egui/src/text_selection/visuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@ pub fn paint_text_selection(
let max = max.rcursor;

for ri in min.row..=max.row {
let (row, _) = &mut galley.rows[ri];
let row = Arc::make_mut(row);
let placed_row = &mut galley.rows[ri];

let left = if ri == min.row {
row.x_offset(min.column)
placed_row.x_offset(min.column)
} else {
row.rect.left()
0.0
};
let right = if ri == max.row {
row.x_offset(max.column)
placed_row.x_offset(max.column)
} else {
let newline_size = if row.ends_with_newline {
row.height() / 2.0 // visualize that we select the newline
let newline_size = if placed_row.ends_with_newline {
placed_row.height() / 2.0 // visualize that we select the newline
} else {
0.0
};
row.rect.right() + newline_size
placed_row.size.x + newline_size
};

let rect = Rect::from_min_max(pos2(left, row.min_y()), pos2(right, row.max_y()));
let rect = Rect::from_min_max(pos2(left, 0.0), pos2(right, placed_row.size.y));

let row = Arc::make_mut(&mut placed_row.row);
let mesh = &mut row.visuals.mesh;

// Time to insert the selection rectangle into the row mesh.
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ impl WidgetText {
Self::RichText(text) => text.font_height(fonts, style),
Self::LayoutJob(job) => job.font_height(fonts),
Self::Galley(galley) => {
if let Some((row, _)) = galley.rows.first() {
row.height()
if let Some(placed_row) = galley.rows.first() {
placed_row.height()
} else {
galley.size().y
}
Expand Down
9 changes: 3 additions & 6 deletions crates/egui/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,10 @@ impl Label {
let pos = pos2(ui.max_rect().left(), ui.cursor().top());
assert!(!galley.rows.is_empty(), "Galleys are never empty");
// collect a response from many rows:
let rect = galley.rows[0]
.0
.rect
.translate(galley.rows[0].1.to_vec2() + pos.to_vec2());
let rect = galley.rows[0].rect().translate(pos.to_vec2());
let mut response = ui.allocate_rect(rect, sense);
for (row, offset) in galley.rows.iter().skip(1) {
let rect = row.rect.translate(offset.to_vec2() + pos.to_vec2());
for placed_row in galley.rows.iter().skip(1) {
let rect = placed_row.rect().translate(pos.to_vec2());
response |= ui.allocate_rect(rect, sense);
}
(pos, galley, response)
Expand Down
5 changes: 2 additions & 3 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,8 @@ impl Shape {

// Scale text:
let galley = Arc::make_mut(&mut text_shape.galley);
for (row, offset) in &mut galley.rows {
let row = Arc::make_mut(row);
*offset = *offset * transform.scaling;
for placed_row in &mut galley.rows {
let row = Arc::make_mut(&mut placed_row.row);
row.visuals.mesh_bounds = transform.scaling * row.visuals.mesh_bounds;
for v in &mut row.visuals.mesh.vertices {
v.pos = Pos2::new(transform.scaling * v.pos.x, transform.scaling * v.pos.y);
Expand Down
4 changes: 2 additions & 2 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub fn adjust_colors(

if !galley.is_empty() {
let galley = std::sync::Arc::make_mut(galley);
for (row, _) in &mut galley.rows {
let row = Arc::make_mut(row);
for placed_row in &mut galley.rows {
let row = Arc::make_mut(&mut placed_row.row);
for vertex in &mut row.visuals.mesh.vertices {
adjust_color(&mut vertex.color);
}
Expand Down
12 changes: 4 additions & 8 deletions crates/epaint/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ impl AllocInfo {
pub fn from_galley(galley: &Galley) -> Self {
Self::from_slice(galley.text().as_bytes())
+ Self::from_slice(&galley.rows)
+ galley
.rows
.iter()
.map(|(row, _)| Self::from_galley_row(row))
.sum()
+ galley.rows.iter().map(Self::from_galley_row).sum()
}

fn from_galley_row(row: &crate::text::Row) -> Self {
fn from_galley_row(row: &crate::text::PlacedRow) -> Self {
Self::from_mesh(&row.visuals.mesh) + Self::from_slice(&row.glyphs)
}

Expand Down Expand Up @@ -217,8 +213,8 @@ impl PaintStats {
self.shape_text += AllocInfo::from_galley(&text_shape.galley);

for row in &text_shape.galley.rows {
self.text_shape_indices += AllocInfo::from_slice(&row.0.visuals.mesh.indices);
self.text_shape_vertices += AllocInfo::from_slice(&row.0.visuals.mesh.vertices);
self.text_shape_indices += AllocInfo::from_slice(&row.visuals.mesh.indices);
self.text_shape_vertices += AllocInfo::from_slice(&row.visuals.mesh.vertices);
}
}
Shape::Mesh(mesh) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,12 +1778,12 @@ impl Tessellator {

let rotator = Rot2::from_angle(*angle);

for (row, row_pos) in &galley.rows {
for row in &galley.rows {
if row.visuals.mesh.is_empty() {
continue;
}

let final_pos = galley_pos + row_pos.to_vec2();
let final_pos = galley_pos + row.pos.to_vec2();

let mut row_rect = row.visuals.mesh_bounds;
if *angle != 0.0 {
Expand Down
23 changes: 18 additions & 5 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,21 @@ impl GalleyCache {
let current_offset = emath::vec2(0.0, merged_galley.rect.height());
merged_galley
.rows
.extend(galley.rows.iter().map(|(row, prev_offset)| {
merged_galley.mesh_bounds =
merged_galley.mesh_bounds.union(row.visuals.mesh_bounds);

(row.clone(), *prev_offset + current_offset)
.extend(galley.rows.iter().map(|placed_row| {
let new_pos = placed_row.pos + current_offset;
merged_galley.mesh_bounds = merged_galley
.mesh_bounds
.union(placed_row.visuals.mesh_bounds.translate(new_pos.to_vec2()));

super::PlacedRow {
row: placed_row.row.clone(),
pos: new_pos,
ends_with_newline: placed_row.ends_with_newline,
}
}));
if let Some(last) = merged_galley.rows.last_mut() {
last.ends_with_newline = true;
}
merged_galley.rect = merged_galley
.rect
.union(galley.rect.translate(current_offset));
Expand All @@ -822,6 +831,10 @@ impl GalleyCache {
}
}

if let Some(last) = merged_galley.rows.last_mut() {
last.ends_with_newline = false;
}

merged_galley
}

Expand Down
Loading

0 comments on commit bc86bec

Please sign in to comment.