single line textedit

This commit is contained in:
2025-11-22 22:04:46 -05:00
parent 14a9da0553
commit ee0616885f
3 changed files with 34 additions and 13 deletions

View File

@@ -13,15 +13,17 @@ pub struct TextEdit {
selection: TextSelection,
history: Vec<(String, TextSelection)>,
double_hit: Option<Cursor>,
pub single_line: bool,
}
impl TextEdit {
pub fn new(view: TextView) -> Self {
pub fn new(view: TextView, single_line: bool) -> Self {
Self {
view,
selection: Default::default(),
history: Default::default(),
double_hit: None,
single_line,
}
}
pub fn select_content(&self, start: Cursor, end: Cursor) -> String {
@@ -196,9 +198,10 @@ impl<'a> TextEditCtx<'a> {
}
pub fn set(&mut self, text: &str) {
let text = self.string(text);
self.text
.buf
.set_text(self.font_system, text, &Attrs::new(), SHAPING, None);
.set_text(self.font_system, &text, &Attrs::new(), SHAPING, None);
self.text.selection.clear();
}
@@ -236,13 +239,23 @@ impl<'a> TextEditCtx<'a> {
}
pub fn replace(&mut self, len: usize, text: &str) {
let text = self.string(text);
for _ in 0..len {
self.delete(false);
}
self.insert_inner(text, false);
self.insert_inner(&text, false);
}
fn string(&self, text: &str) -> String {
if self.text.single_line {
text.replace('\n', "")
} else {
text.to_string()
}
}
pub fn insert(&mut self, text: &str) {
let text = self.string(text);
let mut lines = text.split('\n');
let Some(first) = lines.next() else {
return;
@@ -303,6 +316,9 @@ impl<'a> TextEditCtx<'a> {
}
pub fn newline(&mut self) {
if self.text.single_line {
return;
}
self.clear_span();
if let TextSelection::Pos(cursor) = &mut self.text.selection {
let lines = &mut self.text.view.buf.lines;