egui/text_selection/
visuals.rsuse std::sync::Arc;
use crate::{pos2, vec2, Galley, Painter, Rect, Ui, Visuals};
use super::CursorRange;
#[derive(Clone, Debug)]
pub struct RowVertexIndices {
pub row: usize,
pub vertex_indices: [u32; 6],
}
pub fn paint_text_selection(
galley: &mut Arc<Galley>,
visuals: &Visuals,
cursor_range: &CursorRange,
mut new_vertex_indices: Option<&mut Vec<RowVertexIndices>>,
) {
if cursor_range.is_empty() {
return;
}
let galley: &mut Galley = Arc::make_mut(galley);
let color = visuals.selection.bg_fill;
let [min, max] = cursor_range.sorted_cursors();
let min = min.rcursor;
let max = max.rcursor;
for ri in min.row..=max.row {
let row = &mut galley.rows[ri];
let left = if ri == min.row {
row.x_offset(min.column)
} else {
row.rect.left()
};
let right = if ri == max.row {
row.x_offset(max.column)
} else {
let newline_size = if row.ends_with_newline {
row.height() / 2.0 } else {
0.0
};
row.rect.right() + newline_size
};
let rect = Rect::from_min_max(pos2(left, row.min_y()), pos2(right, row.max_y()));
let mesh = &mut row.visuals.mesh;
let glyph_index_start = row.visuals.glyph_index_start;
let num_indices_before = mesh.indices.len();
mesh.add_colored_rect(rect, color);
assert_eq!(num_indices_before + 6, mesh.indices.len());
let selection_triangles = [
mesh.indices[num_indices_before],
mesh.indices[num_indices_before + 1],
mesh.indices[num_indices_before + 2],
mesh.indices[num_indices_before + 3],
mesh.indices[num_indices_before + 4],
mesh.indices[num_indices_before + 5],
];
for i in (glyph_index_start..num_indices_before).rev() {
mesh.indices.swap(i, i + 6);
}
mesh.indices[glyph_index_start..glyph_index_start + 6]
.clone_from_slice(&selection_triangles);
row.visuals.mesh_bounds = mesh.calc_bounds();
if let Some(new_vertex_indices) = &mut new_vertex_indices {
new_vertex_indices.push(RowVertexIndices {
row: ri,
vertex_indices: selection_triangles,
});
}
}
}
pub fn paint_cursor_end(painter: &Painter, visuals: &Visuals, cursor_rect: Rect) {
let stroke = visuals.text_cursor.stroke;
let top = cursor_rect.center_top();
let bottom = cursor_rect.center_bottom();
painter.line_segment([top, bottom], (stroke.width, stroke.color));
if false {
let extrusion = 3.0;
let width = 1.0;
painter.line_segment(
[top - vec2(extrusion, 0.0), top + vec2(extrusion, 0.0)],
(width, stroke.color),
);
painter.line_segment(
[bottom - vec2(extrusion, 0.0), bottom + vec2(extrusion, 0.0)],
(width, stroke.color),
);
}
}
pub fn paint_text_cursor(
ui: &Ui,
painter: &Painter,
primary_cursor_rect: Rect,
time_since_last_edit: f64,
) {
if ui.visuals().text_cursor.blink {
let on_duration = ui.visuals().text_cursor.on_duration;
let off_duration = ui.visuals().text_cursor.off_duration;
let total_duration = on_duration + off_duration;
let time_in_cycle = (time_since_last_edit % (total_duration as f64)) as f32;
let wake_in = if time_in_cycle < on_duration {
paint_cursor_end(painter, ui.visuals(), primary_cursor_rect);
on_duration - time_in_cycle
} else {
total_duration - time_in_cycle
};
ui.ctx().request_repaint_after_secs(wake_in);
} else {
paint_cursor_end(painter, ui.visuals(), primary_cursor_rect);
}
}