Simplify text editing state changes

This commit is contained in:
iris committed 2026-09-13 02:42:28 -04:00
1 parent 5fe7c3b6df
commit dc008441e4
2 files changed
+20 -30

No files matched your search

+16 -26
View File
@@ -111,7 +111,7 @@ impl<'a> TextEditCtx<'a> {
self.text.view.buf.layout()
}
fn refresh(&mut self) {
fn clamp_selection_to_layout(&mut self) {
if let Some(sel) = self.text.selection {
let layout = self.layout();
self.text.selection = Some(sel.refresh(layout));
@@ -119,8 +119,8 @@ impl<'a> TextEditCtx<'a> {
}
pub fn take(&mut self) -> String {
let text = self.text.view.buf.text().to_string();
self.set("");
let text = std::mem::take(self.text.view.buf.edit());
self.text.selection = None;
text
}
@@ -219,9 +219,8 @@ impl<'a> TextEditCtx<'a> {
if end == 0 {
return;
}
let start = {
let layout = self.layout();
if word {
let start = if word {
sel.focus().previous_logical_word(layout).index()
} else {
let Some(cluster) = sel.focus().logical_clusters(layout)[0] else {
@@ -236,7 +235,6 @@ impl<'a> TextEditCtx<'a> {
.next_back()
.map_or(range.start, |(start, _)| start)
}
}
};
self.delete_range(start, end);
}
@@ -252,9 +250,8 @@ impl<'a> TextEditCtx<'a> {
if start >= self.text.view.buf.text().len() {
return;
}
let end = {
let layout = self.layout();
if word {
let end = if word {
sel.focus().next_logical_word(layout).index()
} else {
let clusters = sel.focus().logical_clusters(layout);
@@ -262,17 +259,11 @@ impl<'a> TextEditCtx<'a> {
return;
};
cluster.text_range().end
}
};
self.delete_range(start, end);
}
fn delete_range(&mut self, start: usize, end: usize) {
let len = self.text.view.buf.text().len();
let (start, end) = (start.min(end).min(len), start.max(end).min(len));
if start == end {
return;
}
self.text.view.buf.edit().replace_range(start..end, "");
self.text.view.buf.changed = true;
self.set_caret(start);
@@ -294,32 +285,31 @@ impl<'a> TextEditCtx<'a> {
let prev_sel = self.text.selection;
let prev_hit = self.text.double_hit;
let outcome = {
let layout = self.layout();
if drag {
prev_sel.map(|sel| (Some(sel.extend_to_point(layout, pos.x, pos.y)), prev_hit))
let (selection, double_hit) = if drag {
let Some(selection) = prev_sel else {
return;
};
(selection.extend_to_point(layout, pos.x, pos.y), prev_hit)
} else {
let hit = Selection::from_point(layout, pos.x, pos.y);
let index = hit.focus().index();
// Successive clicks at one index select the word, then the line.
Some(if recent && prev_hit == Some(index) {
(Some(Selection::line_from_point(layout, pos.x, pos.y)), None)
if recent && prev_hit == Some(index) {
(Selection::line_from_point(layout, pos.x, pos.y), None)
} else if recent && prev_sel.map(|s| s.focus().index()) == Some(index) {
(
Some(Selection::word_from_point(layout, pos.x, pos.y)),
Selection::word_from_point(layout, pos.x, pos.y),
Some(index),
)
} else {
(Some(hit), None)
})
(hit, None)
}
};
if let Some((selection, double_hit)) = outcome {
self.text.selection = selection;
self.text.selection = Some(selection);
self.text.double_hit = double_hit;
}
}
pub fn deselect(&mut self) {
self.text.selection = None;
@@ -334,7 +324,7 @@ impl<'a> TextEditCtx<'a> {
if let Some((old, selection)) = self.text.history.pop() {
self.set(&old);
self.text.selection = selection;
self.refresh();
self.clamp_selection_to_layout();
}
} else if self.text.view.buf.text() != old.0 {
self.text.history.push(old);
+4 -4
View File
@@ -23,7 +23,7 @@ pub struct TextView {
}
impl TextView {
fn is_blank(&self) -> bool {
fn is_empty(&self) -> bool {
self.buf.is_empty()
}
@@ -76,7 +76,7 @@ impl TextView {
self.tex.as_ref()
}
pub fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
if self.is_blank()
if self.is_empty()
&& let Some(hint) = &self.hint
{
ctx.width(hint)
@@ -85,7 +85,7 @@ impl TextView {
}
}
pub fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
if self.is_blank()
if self.is_empty()
&& let Some(hint) = &self.hint
{
ctx.height(hint)
@@ -96,7 +96,7 @@ impl TextView {
pub fn draw(&mut self, painter: &mut Painter) -> UiRegion {
let tex = self.render(&mut painter.size_ctx());
let region = tex.size.align(self.align);
if self.is_blank()
if self.is_empty()
&& let Some(hint) = &self.hint
{
painter.widget(hint);