use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::ops::Range;
use std::rc::Rc;
use std::time::{Duration, Instant};
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, AppContext as _, Bounds, ClipboardItem, Context, CursorStyle, DispatchPhase,
Div, Element, ElementId, Entity, FocusHandle, Global, GlobalElementId, HitboxBehavior, Hsla,
InspectorElementId, InteractiveElement, IntoElement, KeyDownEvent, LayoutId, MouseButton,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, RenderOnce,
ScrollHandle, SharedString, Styled, StyledText, TextLayout, Window, anchored, canvas, deferred,
div, fill, point, px,
};
use super::{Menu, MenuItem};
use crate::theme::{Theme, alpha};
type Key = (u64, u32);
type Spot = (Key, usize);
type EdgeScroll = Rc<dyn Fn(Pixels, &mut Window, &mut App)>;
const EDGE_TICK: Duration = Duration::from_millis(16);
struct Block {
layout: TextLayout,
bounds: Bounds<Pixels>,
visible: Bounds<Pixels>,
text: SharedString,
joint: SharedString,
whole: Option<SharedString>,
frame: u64,
}
pub struct TextSelection {
focus: FocusHandle,
blocks: BTreeMap<Key, Block>,
counts: HashMap<u64, u32>,
frame: u64,
anchor: Option<Spot>,
head: Option<Spot>,
dragging: bool,
pointer: Point<Pixels>,
viewport: Option<Bounds<Pixels>>,
edge_scrolling: bool,
edge_scroll: Option<EdgeScroll>,
painted: Option<Instant>,
shared: bool,
menu: Option<(Point<Pixels>, Key)>,
}
impl TextSelection {
fn new(shared: bool, cx: &mut Context<Self>) -> Self {
Self {
focus: cx.focus_handle(),
blocks: BTreeMap::new(),
counts: HashMap::new(),
frame: 0,
anchor: None,
head: None,
dragging: false,
pointer: Point::default(),
viewport: None,
edge_scrolling: false,
edge_scroll: None,
painted: None,
shared,
menu: None,
}
}
pub fn of(id: impl Into<ElementId>, window: &mut Window, cx: &mut App) -> Entity<Self> {
window.use_keyed_state(id, cx, |_, cx| Self::new(false, cx))
}
pub fn on_edge_scroll(&mut self, scroll: impl Fn(Pixels, &mut Window, &mut App) + 'static) {
self.edge_scroll = Some(Rc::new(scroll));
}
pub fn edge_scroll_handle(&mut self, handle: ScrollHandle) {
self.on_edge_scroll(move |delta, window, _| {
let offset = handle.offset();
let top = (offset.y + delta).clamp(-handle.max_offset().height, px(0.));
handle.set_offset(point(offset.x, top));
window.refresh();
});
}
pub fn shared(cx: &mut App) -> Entity<Self> {
cx.new(|cx| Self::new(true, cx))
}
fn range(&self) -> Option<(Spot, Spot)> {
let (a, h) = (self.anchor?, self.head?);
(a != h).then(|| if a <= h { (a, h) } else { (h, a) })
}
fn block_range(&self, key: Key) -> Option<Range<usize>> {
let ((sk, so), (ek, eo)) = self.range()?;
let text = &self.blocks.get(&key)?.text;
if key < sk || key > ek {
return None;
}
let start = if key == sk { floor_char(text, so) } else { 0 };
let end = if key == ek {
floor_char(text, eo)
} else {
text.len()
};
(start < end).then_some(start..end)
}
pub fn selected_text(&self) -> String {
let Some(((sk, _), (ek, _))) = self.range() else {
return String::new();
};
let mut out = String::new();
let mut last_owner = None;
for (&key, block) in self.blocks.range(sk..=ek) {
if let Some(range) = self.block_range(key) {
if !out.is_empty() {
out.push_str(if last_owner == Some(key.0) {
&block.joint
} else {
"\n\n"
});
}
out.push_str(&block.text[range]);
last_owner = Some(key.0);
}
}
out
}
fn whole_text(&self, key: Key) -> String {
if let Some(whole) = self.blocks.get(&key).and_then(|b| b.whole.clone()) {
return whole.to_string();
}
let mut out = String::new();
for (k, block) in self.blocks.range((key.0, 0)..=(key.0, u32::MAX)) {
if k.1 > 0 {
out.push_str(&block.joint);
}
out.push_str(&block.text);
}
out
}
fn select_all(&mut self) -> bool {
let (Some((&first, _)), Some((&last, block))) =
(self.blocks.first_key_value(), self.blocks.last_key_value())
else {
return false;
};
self.anchor = Some((first, 0));
self.head = Some((last, block.text.len()));
true
}
fn shown(&self) -> impl Iterator<Item = (&Key, &Block)> {
let frame = self.frame;
self.blocks.iter().filter(move |(_, b)| b.frame == frame)
}
fn spot_at(&self, pos: Point<Pixels>) -> Option<Spot> {
let mut last = None;
for (&key, block) in self.shown() {
if pos.y < block.bounds.top() {
return Some((key, 0));
}
if pos.y <= block.bounds.bottom() {
return Some((key, index_at(block, pos)));
}
last = Some((key, block.text.len()));
}
last
}
fn clear(&mut self) -> bool {
let had = self.range().is_some();
self.anchor = None;
self.head = None;
self.dragging = false;
had
}
fn restart(&mut self, owner: u64) {
self.counts.remove(&owner);
self.blocks.retain(|k, _| k.0 != owner);
}
}
fn index_at(block: &Block, pos: Point<Pixels>) -> usize {
let ix = match block.layout.index_for_position(pos) {
Ok(ix) | Err(ix) => ix,
};
floor_char(&block.text, ix)
}
fn floor_char(text: &str, mut ix: usize) -> usize {
ix = ix.min(text.len());
while !text.is_char_boundary(ix) {
ix -= 1;
}
ix
}
pub fn word_at(text: &str, ix: usize) -> Range<usize> {
let ix = floor_char(text, ix);
let is_word = |c: char| c.is_alphanumeric() || c == '_';
let at = text[ix..].chars().next();
let on_word = at.is_some_and(is_word)
|| (at.is_none() && text[..ix].chars().next_back().is_some_and(is_word));
if !on_word {
return ix..at.map_or(ix, |c| ix + c.len_utf8());
}
let start = text[..ix]
.char_indices()
.rev()
.take_while(|(_, c)| is_word(*c))
.last()
.map_or(ix, |(i, _)| i);
let end = text[ix..]
.char_indices()
.find(|(_, c)| !is_word(*c))
.map_or(text.len(), |(i, _)| ix + i);
start..end
}
#[derive(Clone)]
pub struct Selection {
pub state: Entity<TextSelection>,
pub owner: u64,
}
thread_local! {
static SCOPE: RefCell<Vec<Selection>> = const { RefCell::new(Vec::new()) };
}
pub fn current_selection() -> Option<Selection> {
SCOPE.with(|s| s.borrow().last().cloned())
}
pub struct SelectionScope {
scope: Selection,
child: Option<AnyElement>,
}
impl SelectionScope {
pub fn new(state: Entity<TextSelection>, owner: u64, child: impl IntoElement) -> Self {
Self {
scope: Selection { state, owner },
child: Some(child.into_any_element()),
}
}
}
impl IntoElement for SelectionScope {
type Element = Self;
fn into_element(self) -> Self {
self
}
}
impl Element for SelectionScope {
type RequestLayoutState = AnyElement;
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, AnyElement) {
let mut child = self.child.take().expect("laid out once");
SCOPE.with(|s| s.borrow_mut().push(self.scope.clone()));
let id = child.request_layout(window, cx);
SCOPE.with(|s| s.borrow_mut().pop());
(id, child)
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
child: &mut AnyElement,
window: &mut Window,
cx: &mut App,
) {
child.prepaint(window, cx);
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
child: &mut AnyElement,
_: &mut (),
window: &mut Window,
cx: &mut App,
) {
let owner = self.scope.owner;
self.scope.state.update(cx, |s, _| s.restart(owner));
child.paint(window, cx);
}
}
pub struct TextMenuRequest {
pub at: Point<Pixels>,
pub selected: Option<String>,
pub all: String,
pub select_all: Rc<dyn Fn(&mut App)>,
}
type TextMenuHandler = Rc<dyn Fn(TextMenuRequest, &mut Window, &mut App)>;
struct TextMenu {
labels: Rc<dyn Fn() -> [SharedString; 3]>,
native: Option<TextMenuHandler>,
}
impl Global for TextMenu {}
pub fn set_text_menu(
cx: &mut App,
labels: impl Fn() -> [SharedString; 3] + 'static,
native: Option<impl Fn(TextMenuRequest, &mut Window, &mut App) + 'static>,
) {
cx.set_global(TextMenu {
labels: Rc::new(labels),
native: native.map(|f| Rc::new(f) as TextMenuHandler),
});
}
fn menu_labels(cx: &App) -> [SharedString; 3] {
cx.try_global::<TextMenu>()
.map(|m| (m.labels)())
.unwrap_or_else(|| ["Copy".into(), "Copy all".into(), "Select all".into()])
}
fn open_menu(
state: &Entity<TextSelection>,
key: Key,
at: Point<Pixels>,
window: &mut Window,
cx: &mut App,
) {
let native = cx.try_global::<TextMenu>().and_then(|m| m.native.clone());
let Some(native) = native else {
state.update(cx, |s, cx| {
s.menu = Some((at, key));
cx.notify();
});
return;
};
let sel = state.read(cx);
let selected = Some(sel.selected_text()).filter(|t| !t.is_empty());
let all = sel.whole_text(key);
let target = state.clone();
let select_all: Rc<dyn Fn(&mut App)> = Rc::new(move |cx: &mut App| {
target.update(cx, |s, cx| {
if s.select_all() {
cx.notify();
}
})
});
native(
TextMenuRequest {
at,
selected,
all,
select_all,
},
window,
cx,
);
}
fn drawn_menu(state: &Entity<TextSelection>, at: Point<Pixels>, key: Key, cx: &App) -> AnyElement {
let [copy, copy_all, select_all] = menu_labels(cx);
let sel = state.read(cx);
let selected = sel.selected_text();
let all = sel.whole_text(key);
let close = |state: &Entity<TextSelection>| {
let state = state.clone();
move |cx: &mut App| {
state.update(cx, |s, cx| {
s.menu = None;
cx.notify();
})
}
};
let mut menu = Menu::new("text-menu").width(200.).stagger(false);
if !selected.is_empty() {
let done = close(state);
menu = menu.item(MenuItem::new("text-copy", copy).icon("copy").on_click(
move |_, _, cx| {
cx.write_to_clipboard(ClipboardItem::new_string(selected.clone()));
done(cx);
},
));
}
let done = close(state);
menu = menu.item(
MenuItem::new("text-copy-all", copy_all)
.icon("copy")
.on_click(move |_, _, cx| {
cx.write_to_clipboard(ClipboardItem::new_string(all.clone()));
done(cx);
}),
);
let target = state.clone();
menu = menu.item(
MenuItem::new("text-select-all", select_all).on_click(move |_, _, cx| {
target.update(cx, |s, cx| {
s.select_all();
s.menu = None;
cx.notify();
})
}),
);
let done = close(state);
deferred(
anchored()
.position(at)
.snap_to_window_with_margin(px(8.))
.child(
div()
.occlude()
.on_mouse_down_out(move |_, _, cx| done(cx))
.child(menu),
),
)
.with_priority(1)
.into_any_element()
}
pub fn selection_root(state: &Entity<TextSelection>, root: Div, cx: &App) -> Div {
let focus = state.read(cx).focus.clone();
let menu = state.read(cx).menu;
let keys_state = state.clone();
let paint_state = state.clone();
root.relative()
.track_focus(&focus)
.on_key_down(move |event: &KeyDownEvent, _, cx| on_key(&keys_state, event, cx))
.child(
canvas(
|_, _, _| {},
move |bounds, _, window, cx| {
let mask = window.content_mask().bounds;
paint_state.update(cx, |s, _| {
s.frame += 1;
s.viewport = Some(if s.shared {
bounds.intersect(&mask)
} else {
mask
});
s.painted = Some(Instant::now());
if !s.shared {
s.restart(0);
}
});
follow(&paint_state, window);
},
)
.absolute()
.top_0()
.left_0()
.size_full(),
)
.when_some(menu, |root, (at, key)| {
root.child(drawn_menu(state, at, key, cx))
})
}
fn on_key(state: &Entity<TextSelection>, event: &KeyDownEvent, cx: &mut App) {
let keys = &event.keystroke;
let key = keys.key.as_str();
if keys.modifiers.secondary() && key == "c" {
let text = state.read(cx).selected_text();
if !text.is_empty() {
cx.write_to_clipboard(ClipboardItem::new_string(text));
cx.stop_propagation();
}
} else if keys.modifiers.secondary() && key == "a" {
state.update(cx, |s, cx| {
if s.select_all() {
cx.notify();
}
});
cx.stop_propagation();
} else if key == "escape" {
let had = state.update(cx, |s, cx| {
let had = s.clear() || s.menu.take().is_some();
if had {
cx.notify();
}
had
});
if had {
cx.stop_propagation();
}
}
}
fn follow(state: &Entity<TextSelection>, window: &mut Window) {
let s = state.clone();
window.on_mouse_event(move |event: &MouseDownEvent, phase, _, cx| {
if phase != DispatchPhase::Capture {
return;
}
let inside = s
.read(cx)
.shown()
.any(|(_, b)| b.visible.contains(&event.position));
if !inside {
s.update(cx, |sel, cx| {
if sel.clear() {
cx.notify();
}
});
}
});
let s = state.clone();
window.on_mouse_event(move |event: &MouseMoveEvent, phase, window, cx| {
if phase != DispatchPhase::Capture || !s.read(cx).dragging {
return;
}
if event.pressed_button != Some(MouseButton::Left) {
s.update(cx, |sel, _| sel.dragging = false);
return;
}
s.update(cx, |sel, cx| {
sel.pointer = event.position;
let head = sel.spot_at(event.position);
if head.is_some() && head != sel.head {
sel.head = head;
cx.notify();
}
});
if s.read(cx).edge_scroll.is_some()
&& edge_step(&s, cx).is_some()
&& !s.read(cx).edge_scrolling
{
edge_scroll(&s, window, cx);
}
});
let s = state.clone();
window.on_mouse_event(move |event: &MouseUpEvent, phase, window, cx| {
if phase != DispatchPhase::Capture
|| event.button != MouseButton::Left
|| !s.read(cx).dragging
{
return;
}
s.update(cx, |sel, _| sel.dragging = false);
if s.read(cx).range().is_some() {
let focus = s.read(cx).focus.clone();
window.focus(&focus);
}
});
}
fn edge_step(state: &Entity<TextSelection>, cx: &App) -> Option<Pixels> {
let s = state.read(cx);
let view = s.viewport?;
if !s.dragging {
return None;
}
let (mut top, mut bottom) = (view.top(), view.bottom());
if !s.shared {
for (_, b) in s.shown() {
top = if b.bounds.top() < top {
b.bounds.top()
} else {
top
};
bottom = if b.bounds.bottom() > bottom {
b.bounds.bottom()
} else {
bottom
};
}
}
let pos = s.pointer;
let speed = |past: Pixels| (past / px(4.)).clamp(2., 40.);
if pos.y < view.top() && (s.shared || top < view.top()) {
Some(px(speed(view.top() - pos.y)))
} else if pos.y > view.bottom() && (s.shared || bottom > view.bottom()) {
Some(px(-speed(pos.y - view.bottom())))
} else {
None
}
}
fn edge_scroll(state: &Entity<TextSelection>, window: &mut Window, cx: &mut App) {
state.update(cx, |s, _| s.edge_scrolling = true);
let handle = window.window_handle();
let state = state.clone();
cx.spawn(async move |cx| {
loop {
cx.background_executor().timer(EDGE_TICK).await;
let more = handle.update(cx, |_, window, cx| {
if state
.read(cx)
.painted
.is_none_or(|t| t.elapsed() > EDGE_TICK * 8)
{
state.update(cx, |s, _| s.dragging = false);
return false;
}
let (Some(dy), Some(scroll)) =
(edge_step(&state, cx), state.read(cx).edge_scroll.clone())
else {
return false;
};
scroll(dy, window, cx);
state.update(cx, |s, cx| {
let head = s.spot_at(s.pointer);
if head.is_some() && head != s.head {
s.head = head;
cx.notify();
}
});
true
});
if !more.unwrap_or(false) {
break;
}
}
let _ = state.update(cx, |s, _| s.edge_scrolling = false);
})
.detach();
}
pub fn selectable_block(
group: &Selection,
layout: TextLayout,
source: SharedString,
joint: SharedString,
whole: Option<SharedString>,
inner: AnyElement,
color: Hsla,
) -> AnyElement {
let state = group.state.clone();
let owner = group.owner;
div()
.relative()
.child(
canvas(
|bounds, window, _| window.insert_hitbox(bounds, HitboxBehavior::Normal),
move |bounds, hitbox, window, cx| {
window.set_cursor_style(CursorStyle::IBeam, &hitbox);
let visible = window.content_mask().bounds.intersect(&bounds);
let (key, selected) = state.update(cx, |s, _| {
let n = s.counts.entry(owner).or_default();
let key = (owner, *n);
*n += 1;
let frame = s.frame;
s.blocks.insert(
key,
Block {
layout: layout.clone(),
bounds,
visible,
text: source.clone(),
joint: joint.clone(),
whole: whole.clone(),
frame,
},
);
(key, s.block_range(key))
});
if let Some(range) = selected {
paint_range(&layout, bounds, range, color, window);
}
press(&state, key, hitbox, window);
},
)
.absolute()
.top_0()
.left_0()
.size_full(),
)
.child(inner)
.into_any_element()
}
pub fn group_for(id: ElementId, window: &mut Window, cx: &mut App) -> (Selection, bool) {
match current_selection() {
Some(scope) => (scope, false),
None => (
Selection {
state: TextSelection::of(id, window, cx),
owner: 0,
},
true,
),
}
}
fn paint_range(
layout: &TextLayout,
bounds: Bounds<Pixels>,
range: Range<usize>,
color: Hsla,
window: &mut Window,
) {
let (Some(a), Some(b)) = (
layout.position_for_index(range.start),
layout.position_for_index(range.end),
) else {
return;
};
let lh = layout.line_height();
let rect = |x0: Pixels, y0: Pixels, x1: Pixels, y1: Pixels| {
fill(Bounds::from_corners(point(x0, y0), point(x1, y1)), color)
};
if a.y == b.y {
window.paint_quad(rect(a.x, a.y, b.x, a.y + lh));
return;
}
window.paint_quad(rect(a.x, a.y, bounds.right(), a.y + lh));
if b.y > a.y + lh {
window.paint_quad(rect(bounds.left(), a.y + lh, bounds.right(), b.y));
}
window.paint_quad(rect(bounds.left(), b.y, b.x, b.y + lh));
}
fn press(state: &Entity<TextSelection>, key: Key, hitbox: gpui::Hitbox, window: &mut Window) {
let s = state.clone();
window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
if phase != DispatchPhase::Capture || !hitbox.is_hovered(window) {
return;
}
if event.button == MouseButton::Right {
open_menu(&s, key, event.position, window, cx);
return;
}
if event.button != MouseButton::Left {
return;
}
s.update(cx, |sel, cx| {
let Some(block) = sel.blocks.get(&key) else {
return;
};
let ix = index_at(block, event.position);
let len = block.text.len();
sel.pointer = event.position;
if event.modifiers.shift && sel.anchor.is_some() {
sel.head = Some((key, ix));
sel.dragging = true;
} else {
let range = match event.click_count {
0 | 1 => ix..ix,
2 => word_at(&block.text, ix),
_ => 0..len,
};
sel.anchor = Some((key, range.start));
sel.head = Some((key, range.end));
sel.dragging = event.click_count <= 1;
}
sel.menu = None;
cx.notify();
});
if s.read(cx).range().is_some() {
let focus = s.read(cx).focus.clone();
window.focus(&focus);
}
});
}
#[derive(IntoElement)]
pub struct SelectableText {
id: ElementId,
text: SharedString,
}
impl SelectableText {
pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
text: text.into(),
}
}
}
impl RenderOnce for SelectableText {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let color = alpha(Theme::of(cx).primary, 0.3);
let (group, own) = group_for(self.id, window, cx);
let styled = StyledText::new(self.text.clone());
let layout = styled.layout().clone();
let block = selectable_block(
&group,
layout,
self.text,
SharedString::default(),
None,
styled.into_any_element(),
color,
);
let root = if own {
selection_root(&group.state, div(), cx)
} else {
div()
};
root.child(block)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn double_click_takes_the_word() {
let text = "hola, señor_x mundo";
assert_eq!(&text[word_at(text, 1)], "hola");
assert_eq!(&text[word_at(text, 8)], "señor_x");
assert_eq!(&text[word_at(text, 4)], ",");
assert_eq!(&text[word_at(text, text.len())], "mundo");
}
}