use super::{Context, Id};
use crate::components::text_block::BlockState;
use crate::time::Instant;
use crate::Vec2;
use std::{collections::HashMap, ops::Range};
use winit::keyboard::{KeyCode, ModifiersState};
mod drag;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Endpoint {
pub item: Id,
pub byte: usize,
pub ord: usize,
}
impl Endpoint {
fn key(&self) -> (usize, usize) {
(self.ord, self.byte)
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Selection {
pub scope: Id,
pub anchor: Endpoint,
pub head: Endpoint,
}
impl Selection {
pub fn is_empty(&self) -> bool {
self.anchor.key() == self.head.key() && self.anchor.item == self.head.item
}
pub fn ordered(&self) -> (Endpoint, Endpoint) {
if self.anchor.key() <= self.head.key() {
(self.anchor, self.head)
} else {
(self.head, self.anchor)
}
}
}
#[derive(Default)]
pub(crate) struct Scope {
pub order: Vec<Id>,
pub previous: Vec<Id>,
pub frame: u64,
pub separator: String,
pub copy_urls: bool,
}
struct Presses {
item: Id,
position: crate::Vec2,
time: Instant,
count: u8,
}
#[derive(Default)]
pub(crate) struct SelectionState {
pub items: HashMap<Id, BlockState>,
pub scopes: HashMap<Id, Scope>,
pub owners: HashMap<Id, (Id, Id)>,
pub selection: Option<Selection>,
pub stack: Vec<Id>,
pub drag: Option<drag::Drag>,
presses: Option<Presses>,
pressed: Option<(Id, Vec2, ModifiersState)>,
released: Option<Id>,
}
impl Context {
pub fn selected_text(&self) -> Option<String> {
let selection = self.selection.selection?;
(!selection.is_empty()).then(|| self.selection_text(&selection))?
}
pub fn clear_selection(&mut self) {
if self.selection.selection.take().is_some() {
self.selection.drag = None;
self.request_repaint();
}
}
pub(crate) fn selection_scope_id(&self, item: Id) -> Id {
self.selection.stack.last().copied().unwrap_or(item)
}
pub(crate) fn selection_enter(&mut self, scope: Id, item: Id) -> usize {
let frame = self.frame;
let scope = self.selection.scopes.entry(scope).or_default();
if scope.frame != frame {
scope.frame = frame;
scope.previous = std::mem::take(&mut scope.order);
}
scope.order.push(item);
scope.order.len() - 1
}
pub(crate) fn selection_range(
&self,
scope: Id,
item: Id,
ord: usize,
len: usize,
) -> Range<usize> {
let Some(selection) = self.selection.selection.filter(|s| s.scope == scope) else {
return 0..0;
};
let (lo, hi) = selection.ordered();
let start = if item == lo.item {
lo.byte.min(len)
} else if ord > lo.ord {
0
} else {
return 0..0;
};
let end = if item == hi.item {
hi.byte.min(len)
} else if ord < hi.ord {
len
} else {
return 0..0;
};
start.min(end)..end
}
pub(crate) fn selection_begin(&mut self, scope: Id, endpoint: Endpoint) {
self.selection.selection = Some(Selection {
scope,
anchor: endpoint,
head: endpoint,
});
self.request_repaint();
}
pub(crate) fn selection_set(&mut self, scope: Id, item: Id, ord: usize, range: Range<usize>) {
let at = |byte| Endpoint { item, byte, ord };
self.selection.selection = Some(Selection {
scope,
anchor: at(range.start),
head: at(range.end),
});
self.request_repaint();
}
pub(crate) fn selection_extend(&mut self, scope: Id, head: Endpoint) {
match self
.selection
.selection
.as_mut()
.filter(|s| s.scope == scope)
{
Some(selection) if selection.head != head => {
selection.head = head;
self.request_repaint();
}
Some(_) => {}
None => self.selection_begin(scope, head),
}
}
pub(crate) fn selection_touch(&mut self, item: Id, ord: usize) {
if let Some(selection) = self.selection.selection.as_mut() {
for end in [&mut selection.anchor, &mut selection.head] {
if end.item == item {
end.ord = ord;
}
}
}
}
pub(crate) fn selection_press(&mut self, item: Id, pointer: Vec2, extend: bool) -> u8 {
let now = Instant::now();
let count = self.selection.presses.as_ref().map_or(1, |last| {
let near = (pointer - last.position).length_squared() <= 16.0;
let fast =
now.saturating_duration_since(last.time) <= std::time::Duration::from_millis(500);
if last.item == item && near && fast && !extend {
last.count % 3 + 1
} else {
1
}
});
self.selection.presses = Some(Presses {
item,
position: pointer,
time: now,
count,
});
count
}
pub(crate) fn selection_select_all(&mut self, scope: Id) -> bool {
let Some(scope_info) = self.selection.scopes.get(&scope) else {
return false;
};
let items: Vec<(usize, Id, usize)> = scope_info
.order
.iter()
.enumerate()
.filter_map(|(i, id)| Some((i, *id, self.selection.items.get(id)?.text.len())))
.collect();
let (Some(first), Some(last)) = (items.first(), items.last()) else {
return false;
};
self.selection.selection = Some(Selection {
scope,
anchor: Endpoint {
item: first.1,
byte: 0,
ord: first.0,
},
head: Endpoint {
item: last.1,
byte: last.2,
ord: last.0,
},
});
self.request_repaint();
true
}
fn selection_text(&self, selection: &Selection) -> Option<String> {
let scope = self.selection.scopes.get(&selection.scope)?;
let (lo, hi) = selection.ordered();
let first = scope
.order
.iter()
.position(|id| *id == lo.item)
.unwrap_or(lo.ord);
let last = scope
.order
.iter()
.position(|id| *id == hi.item)
.unwrap_or(hi.ord);
let mut out = String::new();
let mut any = false;
for id in scope.order.iter().take(last + 1).skip(first) {
let Some(item) = self.selection.items.get(id) else {
continue;
};
let start = if *id == lo.item { lo.byte } else { 0 };
let end = if *id == hi.item {
hi.byte
} else {
item.text.len()
};
if any {
out.push_str(&scope.separator);
}
any = true;
out.push_str(&item.copy_range(start..end.max(start), scope.copy_urls));
}
Some(out)
}
pub(crate) fn selection_copy(&mut self) -> bool {
let Some(text) = self.selected_text().filter(|text| !text.is_empty()) else {
return false;
};
self.copy_text(text).is_ok()
}
pub(super) fn selection_key(&mut self, code: KeyCode) -> bool {
let mods = self.input.modifiers;
let command = mods.control_key() || mods.super_key();
let Some((_, scope)) = self
.interaction
.focused
.and_then(|id| self.selection.owners.get(&id).copied())
else {
return false;
};
match code {
KeyCode::KeyA if command => self.selection_select_all(scope),
KeyCode::KeyC | KeyCode::Insert if command && !mods.shift_key() => {
self.selection_copy()
}
_ => false,
}
}
pub(super) fn finish_selection(&mut self) {
let frame = self.frame;
let state = &mut self.selection;
state.items.retain(|_, item| item.last_frame == frame);
state.scopes.retain(|_, scope| scope.frame == frame);
let items = &state.items;
state
.owners
.retain(|_, (owner, _)| items.contains_key(owner));
if state
.selection
.is_some_and(|s| !state.scopes.contains_key(&s.scope))
{
state.selection = None;
state.drag = None;
}
state.pressed = None;
state.released = None;
if state.drag.is_some() && self.interaction.capture.is_none() {
state.drag = None;
}
}
pub(crate) fn link_hovered(&self, id: Id) -> bool {
self.interaction
.capture
.is_none_or(|capture| capture.hit.id == id)
&& self
.input
.pointer
.and_then(|pointer| self.hit_test(pointer))
.is_some_and(|hit| hit.id == id)
}
pub(crate) fn press_point(&self, id: Id) -> Option<(Vec2, ModifiersState)> {
let (target, point, mods) = self
.selection
.pressed
.filter(|(target, ..)| *target == id)?;
let transform = self.visuals.input(target);
Some((transform.inverse().point(point), mods))
}
pub(super) fn selection_note_press(&mut self, id: Id, point: Vec2) {
self.selection.pressed = Some((id, point, self.input.modifiers));
}
pub(super) fn selection_note_release(&mut self, id: Id) {
if self.selection.drag.as_ref().is_some_and(|d| d.owner == id)
|| self
.selection
.pressed
.is_some_and(|(target, ..)| target == id)
{
self.selection.released = Some(id);
}
}
pub(crate) fn selection_active(&self, scope: Id) -> bool {
self.input.focused
&& (self.selection_dragging(scope)
|| self
.interaction
.focused
.and_then(|id| self.selection.owners.get(&id))
.is_some_and(|(_, s)| *s == scope))
}
pub(super) fn selection_holds(&self, id: Id) -> bool {
self.selection.drag.as_ref().is_some_and(|d| d.owner == id)
}
pub(crate) fn selection_dragged_from(&self, id: Id) -> bool {
let Some(drag) = self.selection.drag.as_ref().filter(|d| d.owner == id) else {
return false;
};
self.input
.pointer
.is_some_and(|p| (p - drag.origin).length_squared() > 16.0)
}
pub(crate) fn pixel_scale(&self) -> f32 {
self.scale
}
pub(super) fn selection_press_elsewhere(&mut self, hit: Option<super::HitRegion>) {
let owned = hit.is_some_and(|hit| self.selection.owners.contains_key(&hit.id));
if !owned && !self.popups.is_active() {
self.clear_selection();
}
}
}