Generalize Text element to let you add arbitrary scene primitives for runs of text
This commit is contained in:
parent
66d4cb8c14
commit
8eb9c6563a
2 changed files with 67 additions and 75 deletions
|
@ -2,8 +2,8 @@ use gpui::{
|
||||||
color::Color,
|
color::Color,
|
||||||
elements::Text,
|
elements::Text,
|
||||||
fonts::{HighlightStyle, TextStyle},
|
fonts::{HighlightStyle, TextStyle},
|
||||||
platform::MouseButton,
|
platform::{CursorStyle, MouseButton},
|
||||||
AnyElement, Element, MouseRegion,
|
AnyElement, CursorRegion, Element, MouseRegion,
|
||||||
};
|
};
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use simplelog::SimpleLogger;
|
use simplelog::SimpleLogger;
|
||||||
|
@ -61,13 +61,19 @@ impl gpui::View for TextView {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.with_highlights(vec![(17..26, underline), (34..40, underline)])
|
.with_highlights(vec![(17..26, underline), (34..40, underline)])
|
||||||
.with_mouse_regions(vec![(17..26), (34..40)], move |ix, bounds| {
|
.with_custom_runs(vec![(17..26), (34..40)], move |ix, bounds, scene, _| {
|
||||||
MouseRegion::new::<Self>(view_id, ix, bounds).on_click::<Self, _>(
|
scene.push_cursor_region(CursorRegion {
|
||||||
MouseButton::Left,
|
bounds,
|
||||||
move |_, _, _| {
|
style: CursorStyle::PointingHand,
|
||||||
eprintln!("clicked link {ix}");
|
});
|
||||||
},
|
scene.push_mouse_region(
|
||||||
)
|
MouseRegion::new::<Self>(view_id, ix, bounds).on_click::<Self, _>(
|
||||||
|
MouseButton::Left,
|
||||||
|
move |_, _, _| {
|
||||||
|
eprintln!("clicked link {ix}");
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.into_any()
|
.into_any()
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,9 @@ use crate::{
|
||||||
vector::{vec2f, Vector2F},
|
vector::{vec2f, Vector2F},
|
||||||
},
|
},
|
||||||
json::{ToJson, Value},
|
json::{ToJson, Value},
|
||||||
platform::CursorStyle,
|
|
||||||
text_layout::{Line, RunStyle, ShapedBoundary},
|
text_layout::{Line, RunStyle, ShapedBoundary},
|
||||||
CursorRegion, Element, FontCache, MouseRegion, SceneBuilder, SizeConstraint, TextLayoutCache,
|
AppContext, Element, FontCache, SceneBuilder, SizeConstraint, TextLayoutCache, View,
|
||||||
View, ViewContext,
|
ViewContext,
|
||||||
};
|
};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
@ -20,9 +19,9 @@ pub struct Text {
|
||||||
style: TextStyle,
|
style: TextStyle,
|
||||||
soft_wrap: bool,
|
soft_wrap: bool,
|
||||||
highlights: Option<Box<[(Range<usize>, HighlightStyle)]>>,
|
highlights: Option<Box<[(Range<usize>, HighlightStyle)]>>,
|
||||||
mouse_runs: Option<(
|
custom_runs: Option<(
|
||||||
Box<[Range<usize>]>,
|
Box<[Range<usize>]>,
|
||||||
Box<dyn FnMut(usize, RectF) -> MouseRegion>,
|
Box<dyn FnMut(usize, RectF, &mut SceneBuilder, &mut AppContext)>,
|
||||||
)>,
|
)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +38,7 @@ impl Text {
|
||||||
style,
|
style,
|
||||||
soft_wrap: true,
|
soft_wrap: true,
|
||||||
highlights: None,
|
highlights: None,
|
||||||
mouse_runs: None,
|
custom_runs: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,12 +55,12 @@ impl Text {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_mouse_regions(
|
pub fn with_custom_runs(
|
||||||
mut self,
|
mut self,
|
||||||
runs: impl Into<Box<[Range<usize>]>>,
|
runs: impl Into<Box<[Range<usize>]>>,
|
||||||
build_mouse_region: impl 'static + FnMut(usize, RectF) -> MouseRegion,
|
callback: impl 'static + FnMut(usize, RectF, &mut SceneBuilder, &mut AppContext),
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.mouse_runs = Some((runs.into(), Box::new(build_mouse_region)));
|
self.custom_runs = Some((runs.into(), Box::new(callback)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,17 +175,18 @@ impl<V: View> Element<V> for Text {
|
||||||
) -> Self::PaintState {
|
) -> Self::PaintState {
|
||||||
let mut origin = bounds.origin();
|
let mut origin = bounds.origin();
|
||||||
let empty = Vec::new();
|
let empty = Vec::new();
|
||||||
|
let mut callback = |_, _, _: &mut SceneBuilder, _: &mut AppContext| {};
|
||||||
|
|
||||||
let mouse_runs;
|
let mouse_runs;
|
||||||
let mut build_mouse_region;
|
let custom_run_callback;
|
||||||
if let Some((runs, build_region)) = &mut self.mouse_runs {
|
if let Some((runs, build_region)) = &mut self.custom_runs {
|
||||||
mouse_runs = runs.iter();
|
mouse_runs = runs.iter();
|
||||||
build_mouse_region = Some(build_region);
|
custom_run_callback = build_region.as_mut();
|
||||||
} else {
|
} else {
|
||||||
mouse_runs = [].iter();
|
mouse_runs = [].iter();
|
||||||
build_mouse_region = None;
|
custom_run_callback = &mut callback;
|
||||||
}
|
}
|
||||||
let mut mouse_runs = mouse_runs.enumerate().peekable();
|
let mut custom_runs = mouse_runs.enumerate().peekable();
|
||||||
|
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
for (ix, line) in layout.shaped_lines.iter().enumerate() {
|
for (ix, line) in layout.shaped_lines.iter().enumerate() {
|
||||||
|
@ -214,13 +214,13 @@ impl<V: View> Element<V> for Text {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the mouse regions
|
// Paint any custom runs that intersect this line.
|
||||||
let end_offset = offset + line.len();
|
let end_offset = offset + line.len();
|
||||||
if let Some((mut mouse_run_ix, mut mouse_run_range)) = mouse_runs.peek().cloned() {
|
if let Some((custom_run_ix, custom_run_range)) = custom_runs.peek().cloned() {
|
||||||
if mouse_run_range.start < end_offset {
|
if custom_run_range.start < end_offset {
|
||||||
let mut current_mouse_run = None;
|
let mut current_custom_run = None;
|
||||||
if mouse_run_range.start <= offset {
|
if custom_run_range.start <= offset {
|
||||||
current_mouse_run = Some((mouse_run_ix, origin));
|
current_custom_run = Some((custom_run_ix, custom_run_range.end, origin));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut glyph_origin = origin;
|
let mut glyph_origin = origin;
|
||||||
|
@ -237,81 +237,67 @@ impl<V: View> Element<V> for Text {
|
||||||
glyph_origin.set_x(glyph_origin.x() + glyph.position.x() - prev_position);
|
glyph_origin.set_x(glyph_origin.x() + glyph.position.x() - prev_position);
|
||||||
prev_position = glyph.position.x();
|
prev_position = glyph.position.x();
|
||||||
|
|
||||||
|
// If we've reached a soft wrap position, move down one line. If there
|
||||||
|
// is a custom run in-progress, paint it.
|
||||||
if wrap_boundaries
|
if wrap_boundaries
|
||||||
.peek()
|
.peek()
|
||||||
.map_or(false, |b| b.run_ix == run_ix && b.glyph_ix == glyph_ix)
|
.map_or(false, |b| b.run_ix == run_ix && b.glyph_ix == glyph_ix)
|
||||||
{
|
{
|
||||||
if let Some((mouse_run_ix, mouse_region_start)) = &mut current_mouse_run
|
if let Some((run_ix, _, run_origin)) = &mut current_custom_run {
|
||||||
{
|
|
||||||
let bounds = RectF::from_points(
|
let bounds = RectF::from_points(
|
||||||
*mouse_region_start,
|
*run_origin,
|
||||||
glyph_origin + vec2f(0., layout.line_height),
|
glyph_origin + vec2f(0., layout.line_height),
|
||||||
);
|
);
|
||||||
scene.push_cursor_region(CursorRegion {
|
custom_run_callback(*run_ix, bounds, scene, cx);
|
||||||
bounds,
|
*run_origin =
|
||||||
style: CursorStyle::PointingHand,
|
|
||||||
});
|
|
||||||
scene.push_mouse_region((build_mouse_region.as_mut().unwrap())(
|
|
||||||
*mouse_run_ix,
|
|
||||||
bounds,
|
|
||||||
));
|
|
||||||
*mouse_region_start =
|
|
||||||
vec2f(origin.x(), glyph_origin.y() + layout.line_height);
|
vec2f(origin.x(), glyph_origin.y() + layout.line_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
wrap_boundaries.next();
|
wrap_boundaries.next();
|
||||||
glyph_origin = vec2f(origin.x(), glyph_origin.y() + layout.line_height);
|
glyph_origin = vec2f(origin.x(), glyph_origin.y() + layout.line_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
if offset + glyph.index == mouse_run_range.start {
|
// If we've reached the end of the current custom run, paint it.
|
||||||
current_mouse_run = Some((mouse_run_ix, glyph_origin));
|
if let Some((run_ix, run_end_offset, run_origin)) = current_custom_run {
|
||||||
}
|
if offset + glyph.index == run_end_offset {
|
||||||
if offset + glyph.index == mouse_run_range.end {
|
current_custom_run.take();
|
||||||
if let Some((mouse_run_ix, mouse_region_start)) =
|
|
||||||
current_mouse_run.take()
|
|
||||||
{
|
|
||||||
let bounds = RectF::from_points(
|
let bounds = RectF::from_points(
|
||||||
mouse_region_start,
|
run_origin,
|
||||||
glyph_origin + vec2f(0., layout.line_height),
|
glyph_origin + vec2f(0., layout.line_height),
|
||||||
);
|
);
|
||||||
scene.push_cursor_region(CursorRegion {
|
custom_run_callback(run_ix, bounds, scene, cx);
|
||||||
bounds,
|
custom_runs.next();
|
||||||
style: CursorStyle::PointingHand,
|
|
||||||
});
|
|
||||||
scene.push_mouse_region((build_mouse_region.as_mut().unwrap())(
|
|
||||||
mouse_run_ix,
|
|
||||||
bounds,
|
|
||||||
));
|
|
||||||
mouse_runs.next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(next) = mouse_runs.peek() {
|
if let Some((_, run_range)) = custom_runs.peek() {
|
||||||
mouse_run_ix = next.0;
|
if run_range.start >= end_offset {
|
||||||
mouse_run_range = next.1;
|
|
||||||
if mouse_run_range.start >= end_offset {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if mouse_run_range.start == offset + glyph.index {
|
if run_range.start == offset + glyph.index {
|
||||||
current_mouse_run = Some((mouse_run_ix, glyph_origin));
|
current_custom_run =
|
||||||
|
Some((run_ix, run_range.end, glyph_origin));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we've reached the start of a new custom run, start tracking it.
|
||||||
|
if let Some((run_ix, run_range)) = custom_runs.peek() {
|
||||||
|
if offset + glyph.index == run_range.start {
|
||||||
|
current_custom_run = Some((*run_ix, run_range.end, glyph_origin));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((mouse_run_ix, mouse_region_start)) = current_mouse_run {
|
// If a custom run extends beyond the end of the line, paint it.
|
||||||
|
if let Some((run_ix, run_end_offset, run_origin)) = current_custom_run {
|
||||||
let line_end = glyph_origin + vec2f(line.width() - prev_position, 0.);
|
let line_end = glyph_origin + vec2f(line.width() - prev_position, 0.);
|
||||||
let bounds = RectF::from_points(
|
let bounds = RectF::from_points(
|
||||||
mouse_region_start,
|
run_origin,
|
||||||
line_end + vec2f(0., layout.line_height),
|
line_end + vec2f(0., layout.line_height),
|
||||||
);
|
);
|
||||||
scene.push_cursor_region(CursorRegion {
|
custom_run_callback(run_ix, bounds, scene, cx);
|
||||||
bounds,
|
if end_offset == run_end_offset {
|
||||||
style: CursorStyle::PointingHand,
|
custom_runs.next();
|
||||||
});
|
}
|
||||||
scene.push_mouse_region((build_mouse_region.as_mut().unwrap())(
|
|
||||||
mouse_run_ix,
|
|
||||||
bounds,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue