Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor rework #170

Merged
merged 20 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/vello_editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use winit::event::*;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::Window;

// #[path = "text2.rs"]
mod text;
use parley::{GenericFamily, StyleProperty};

Expand Down Expand Up @@ -227,7 +226,7 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
base_color: Color::rgb8(30, 30, 30), // Background color
width,
height,
antialiasing_method: AaConfig::Msaa16,
antialiasing_method: AaConfig::Area,
},
)
.expect("failed to render to surface");
Expand Down
2 changes: 2 additions & 0 deletions examples/vello_editor/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl Editor {
WindowEvent::MouseInput { state, button, .. } => {
if button == winit::event::MouseButton::Left {
self.pointer_down = state.is_pressed();
self.cursor_reset();
if self.pointer_down {
let now = Instant::now();
if let Some(last) = self.last_click_time.take() {
Expand Down Expand Up @@ -284,6 +285,7 @@ impl Editor {
self.cursor_pos = (position.x as f32 - INSET, position.y as f32 - INSET);
// macOS seems to generate a spurious move after selecting word?
if self.pointer_down && prev_pos != self.cursor_pos {
self.cursor_reset();
let cursor_pos = self.cursor_pos;
self.transact(|txn| txn.extend_selection_to_point(cursor_pos.0, cursor_pos.1));
// println!("Active text: {:?}", self.active_text());
Expand Down
39 changes: 37 additions & 2 deletions parley/src/layout/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl<'a, B: Brush> Cluster<'a, B> {
}
}
}
path.cluster(layout)
None
// path.cluster(layout)
}

/// Returns the cluster and affinity for the given layout and point.
Expand Down Expand Up @@ -61,6 +62,40 @@ impl<'a, B: Brush> Cluster<'a, B> {
Some((path.cluster(layout)?, Affinity::default()))
}

/// Returns the cluster and affinity for the given layout and point.
pub fn from_point2(layout: &'a Layout<B>, x: f32, y: f32) -> Option<(Self, bool)> {
let mut path = ClusterPath::default();
if let Some((line_index, line)) = layout.line_for_offset(y) {
path.line_index = line_index as u32;
let mut offset = line.metrics().offset;
let last_run_index = line.len().saturating_sub(1);
for (run_index, run) in line.runs().enumerate() {
let is_last_run = run_index == last_run_index;
let run_advance = run.advance();
path.run_index = run_index as u32;
path.logical_index = 0;
if x > offset + run_advance && !is_last_run {
offset += run_advance;
continue;
}
let last_cluster_index = run.cluster_range().len().saturating_sub(1);
for (visual_index, cluster) in run.visual_clusters().enumerate() {
let is_last_cluster = is_last_run && visual_index == last_cluster_index;
path.logical_index =
run.visual_to_logical(visual_index).unwrap_or_default() as u32;
let cluster_advance = cluster.advance();
let edge = offset;
offset += cluster_advance;
if x > offset && !is_last_cluster {
continue;
}
return Some((path.cluster(layout)?, x <= edge + cluster_advance * 0.5));
}
}
}
Some((path.cluster(layout)?, true))
}

/// Returns the line that contains the cluster.
pub fn line(&self) -> Line<'a, B> {
self.run.layout.get(self.run.line_index as usize).unwrap()
Expand Down Expand Up @@ -328,7 +363,7 @@ impl<'a, B: Brush> Cluster<'a, B> {
/// on the containing line.
pub fn visual_offset(&self) -> Option<f32> {
let line = self.path.line(self.run.layout)?;
let mut offset = 0.0;
let mut offset = line.metrics().offset;
for run_index in 0..=self.path.run_index() {
let run = line.run(run_index)?;
if run_index != self.path.run_index() {
Expand Down
Loading
Loading