switch to Rc<RefCell<...>> for widget storage
This commit is contained in:
@@ -75,7 +75,7 @@ impl TextBuilderOutput for TextOutput {
|
||||
builder.attrs.line_height,
|
||||
));
|
||||
let hint = builder.hint.get(ui);
|
||||
let font_system = &mut ui.data.text.font_system;
|
||||
let font_system = &mut ui.data.text.borrow_mut().font_system;
|
||||
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
let mut text = Text {
|
||||
content: builder.content.into(),
|
||||
@@ -101,8 +101,9 @@ impl TextBuilderOutput for TextEditOutput {
|
||||
let mut text = TextEdit::new(
|
||||
TextView::new(buf, builder.attrs, builder.hint.get(ui)),
|
||||
builder.output.single_line,
|
||||
ui.data.text.clone(),
|
||||
);
|
||||
let font_system = &mut ui.data.text.font_system;
|
||||
let font_system = &mut ui.data.text.borrow_mut().font_system;
|
||||
text.buf
|
||||
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
builder.attrs.apply(font_system, &mut text.buf, None);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::prelude::*;
|
||||
use cosmic_text::{Affinity, Attrs, Cursor, FontSystem, LayoutRun, Motion};
|
||||
use cosmic_text::{Affinity, Attrs, Cursor, LayoutRun, Motion};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use winit::{
|
||||
event::KeyEvent,
|
||||
@@ -13,17 +13,19 @@ pub struct TextEdit {
|
||||
selection: TextSelection,
|
||||
history: Vec<(String, TextSelection)>,
|
||||
double_hit: Option<Cursor>,
|
||||
data: TextData,
|
||||
pub single_line: bool,
|
||||
}
|
||||
|
||||
impl TextEdit {
|
||||
pub fn new(view: TextView, single_line: bool) -> Self {
|
||||
pub fn new(view: TextView, single_line: bool, data: TextData) -> Self {
|
||||
Self {
|
||||
view,
|
||||
selection: Default::default(),
|
||||
history: Default::default(),
|
||||
double_hit: None,
|
||||
single_line,
|
||||
data,
|
||||
}
|
||||
}
|
||||
pub fn select_content(&self, start: Cursor, end: Cursor) -> String {
|
||||
@@ -42,6 +44,369 @@ impl TextEdit {
|
||||
str
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take(&mut self) -> String {
|
||||
let text = self
|
||||
.buf
|
||||
.lines
|
||||
.drain(..)
|
||||
.map(|l| l.into_text())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
self.set("");
|
||||
text
|
||||
}
|
||||
|
||||
pub fn set(&mut self, text: &str) {
|
||||
let text = self.string(text);
|
||||
self.view.buf.set_text(
|
||||
&mut self.data.borrow_mut().font_system,
|
||||
&text,
|
||||
&Attrs::new(),
|
||||
SHAPING,
|
||||
None,
|
||||
);
|
||||
self.selection.clear();
|
||||
}
|
||||
|
||||
pub fn motion(&mut self, motion: Motion, select: bool) {
|
||||
if let TextSelection::Pos(cursor) = self.selection
|
||||
&& let Some(new) = self.buf_motion(cursor, motion)
|
||||
{
|
||||
self.selection = if select {
|
||||
TextSelection::Span {
|
||||
start: cursor,
|
||||
end: new,
|
||||
}
|
||||
} else {
|
||||
TextSelection::Pos(new)
|
||||
};
|
||||
} else if let TextSelection::Span { start, end } = self.selection {
|
||||
if select {
|
||||
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||
self.selection = TextSelection::Span { start, end: cursor };
|
||||
}
|
||||
} else {
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
match motion {
|
||||
Motion::Left | Motion::LeftWord => self.selection = TextSelection::Pos(start),
|
||||
Motion::Right | Motion::RightWord => self.selection = TextSelection::Pos(end),
|
||||
_ => {
|
||||
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||
self.selection = TextSelection::Pos(cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
fn string(&self, text: &str) -> String {
|
||||
if self.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;
|
||||
};
|
||||
self.insert_inner(first, true);
|
||||
for line in lines {
|
||||
self.newline();
|
||||
self.insert_inner(line, true);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_span(&mut self) -> bool {
|
||||
if let TextSelection::Span { start, end } = self.selection {
|
||||
self.delete_between(start, end);
|
||||
let (start, _) = sort_cursors(start, end);
|
||||
self.selection = TextSelection::Pos(start);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_between(&mut self, start: Cursor, end: Cursor) {
|
||||
let lines = &mut self.view.buf.lines;
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
if start.line == end.line {
|
||||
let line = &mut lines[start.line];
|
||||
let text = line.text();
|
||||
let text = text[..start.index].to_string() + &text[end.index..];
|
||||
edit_line(line, text);
|
||||
} else {
|
||||
// start
|
||||
let start_text = lines[start.line].text()[..start.index].to_string();
|
||||
let end_text = &lines[end.line].text()[end.index..];
|
||||
let text = start_text + end_text;
|
||||
edit_line(&mut lines[start.line], text);
|
||||
}
|
||||
// between
|
||||
let range = (start.line + 1)..=end.line;
|
||||
if !range.is_empty() {
|
||||
lines.splice(range, None);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_inner(&mut self, text: &str, mov: bool) {
|
||||
self.clear_span();
|
||||
if let TextSelection::Pos(cursor) = self.selection {
|
||||
let line = &mut self.view.buf.lines[cursor.line];
|
||||
let mut line_text = line.text().to_string();
|
||||
line_text.insert_str(cursor.index, text);
|
||||
edit_line(line, line_text);
|
||||
if mov {
|
||||
for _ in 0..text.chars().count() {
|
||||
self.motion(Motion::Right, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn newline(&mut self) {
|
||||
if self.single_line {
|
||||
return;
|
||||
}
|
||||
self.clear_span();
|
||||
if let TextSelection::Pos(cursor) = &mut self.selection {
|
||||
let lines = &mut self.view.buf.lines;
|
||||
let line = &mut lines[cursor.line];
|
||||
let new = line.split_off(cursor.index);
|
||||
cursor.line += 1;
|
||||
lines.insert(cursor.line, new);
|
||||
cursor.index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self, word: bool) {
|
||||
if !self.clear_span()
|
||||
&& let TextSelection::Pos(cursor) = &mut self.selection
|
||||
&& (cursor.index != 0 || cursor.line != 0)
|
||||
{
|
||||
self.motion(if word { Motion::LeftWord } else { Motion::Left }, false);
|
||||
self.delete(word);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, word: bool) {
|
||||
if self.clear_span() {
|
||||
return;
|
||||
}
|
||||
if let TextSelection::Pos(cursor) = &mut self.selection {
|
||||
if word {
|
||||
let start = *cursor;
|
||||
if let Some(end) = self.buf_motion(start, Motion::RightWord) {
|
||||
self.delete_between(start, end);
|
||||
}
|
||||
} else {
|
||||
let lines = &mut self.view.buf.lines;
|
||||
let line = &mut lines[cursor.line];
|
||||
if cursor.index == line.text().len() {
|
||||
if cursor.line == lines.len() - 1 {
|
||||
return;
|
||||
}
|
||||
let add = lines.remove(cursor.line + 1).into_text();
|
||||
let line = &mut lines[cursor.line];
|
||||
let mut cur = line.text().to_string();
|
||||
cur.push_str(&add);
|
||||
edit_line(line, cur);
|
||||
} else {
|
||||
let mut text = line.text().to_string();
|
||||
text.remove(cursor.index);
|
||||
edit_line(line, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn buf_motion(&mut self, cursor: Cursor, motion: Motion) -> Option<Cursor> {
|
||||
self.view
|
||||
.buf
|
||||
.cursor_motion(
|
||||
&mut self.data.borrow_mut().font_system,
|
||||
cursor,
|
||||
None,
|
||||
motion,
|
||||
)
|
||||
.map(|r| r.0)
|
||||
}
|
||||
|
||||
pub fn select_word_at(&mut self, cursor: Cursor) {
|
||||
if let (Some(start), Some(end)) = (
|
||||
self.buf_motion(cursor, Motion::LeftWord),
|
||||
self.buf_motion(cursor, Motion::RightWord),
|
||||
) {
|
||||
self.selection = TextSelection::Span { start, end };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_line_at(&mut self, cursor: Cursor) {
|
||||
let end = self.buf.lines[cursor.line].text().len();
|
||||
self.selection = TextSelection::Span {
|
||||
start: Cursor::new(cursor.line, 0),
|
||||
end: Cursor::new(cursor.line, end),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, pos: Vec2, size: Vec2, drag: bool, recent: bool) {
|
||||
let pos = pos - self.region().top_left().to_abs(size);
|
||||
let hit = self.buf.hit(pos.x, pos.y);
|
||||
let sel = &mut self.selection;
|
||||
match sel {
|
||||
TextSelection::None => {
|
||||
if !drag && let Some(hit) = hit {
|
||||
*sel = TextSelection::Pos(hit)
|
||||
}
|
||||
}
|
||||
TextSelection::Pos(pos) => match (hit, drag) {
|
||||
(None, false) => *sel = TextSelection::None,
|
||||
(None, true) => (),
|
||||
(Some(hit), false) => {
|
||||
if recent && hit == *pos {
|
||||
self.double_hit = Some(hit);
|
||||
return self.select_word_at(hit);
|
||||
} else {
|
||||
*pos = hit
|
||||
}
|
||||
}
|
||||
(Some(end), true) => *sel = TextSelection::Span { start: *pos, end },
|
||||
},
|
||||
TextSelection::Span { start, end } => match (hit, drag) {
|
||||
(None, false) => *sel = TextSelection::None,
|
||||
(None, true) => *sel = TextSelection::Pos(*start),
|
||||
(Some(hit), false) => {
|
||||
if recent
|
||||
&& let Some(double) = self.double_hit
|
||||
&& double == hit
|
||||
{
|
||||
return self.select_line_at(hit);
|
||||
} else {
|
||||
*sel = TextSelection::Pos(hit)
|
||||
}
|
||||
}
|
||||
(Some(hit), true) => *end = hit,
|
||||
},
|
||||
}
|
||||
if let TextSelection::Span { start, end } = sel
|
||||
&& start == end
|
||||
{
|
||||
*sel = TextSelection::Pos(*start);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deselect(&mut self) {
|
||||
self.selection = TextSelection::None;
|
||||
}
|
||||
|
||||
pub fn apply_event(&mut self, event: &KeyEvent, modifiers: &Modifiers) -> TextInputResult {
|
||||
let old = (self.content(), self.selection);
|
||||
let mut undo = false;
|
||||
let res = self.apply_event_inner(event, modifiers, &mut undo);
|
||||
if undo && let Some((old, selection)) = self.history.pop() {
|
||||
self.set(&old);
|
||||
self.selection = selection;
|
||||
} else if self.content() != old.0 {
|
||||
self.history.push(old);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn apply_event_inner(
|
||||
&mut self,
|
||||
event: &KeyEvent,
|
||||
modifiers: &Modifiers,
|
||||
undo: &mut bool,
|
||||
) -> TextInputResult {
|
||||
match &event.logical_key {
|
||||
Key::Named(named) => match named {
|
||||
NamedKey::Backspace => self.backspace(modifiers.control),
|
||||
NamedKey::Delete => self.delete(modifiers.control),
|
||||
NamedKey::Space => self.insert(" "),
|
||||
NamedKey::Enter => {
|
||||
if modifiers.shift {
|
||||
self.newline();
|
||||
} else {
|
||||
return TextInputResult::Submit;
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowRight => {
|
||||
if modifiers.control {
|
||||
self.motion(Motion::RightWord, modifiers.shift)
|
||||
} else {
|
||||
self.motion(Motion::Right, modifiers.shift)
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowLeft => {
|
||||
if modifiers.control {
|
||||
self.motion(Motion::LeftWord, modifiers.shift)
|
||||
} else {
|
||||
self.motion(Motion::Left, modifiers.shift)
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowUp => self.motion(Motion::Up, modifiers.shift),
|
||||
NamedKey::ArrowDown => self.motion(Motion::Down, modifiers.shift),
|
||||
NamedKey::Escape => {
|
||||
self.deselect();
|
||||
return TextInputResult::Unfocus;
|
||||
}
|
||||
_ => return TextInputResult::Unused,
|
||||
},
|
||||
Key::Character(text) => {
|
||||
if modifiers.control {
|
||||
match text.as_str() {
|
||||
"v" => return TextInputResult::Paste,
|
||||
"c" => {
|
||||
if let TextSelection::Span { start, end } = self.selection {
|
||||
let content = self.select_content(start, end);
|
||||
return TextInputResult::Copy(content);
|
||||
}
|
||||
}
|
||||
"x" => {
|
||||
if let TextSelection::Span { start, end } = self.selection {
|
||||
let content = self.select_content(start, end);
|
||||
self.clear_span();
|
||||
return TextInputResult::Copy(content);
|
||||
}
|
||||
}
|
||||
"a" => {
|
||||
if !self.buf.lines[0].text().is_empty() || self.buf.lines.len() > 1 {
|
||||
let lines = &self.buf.lines;
|
||||
let last_line = lines.len() - 1;
|
||||
let last_idx = lines[last_line].text().len();
|
||||
self.selection = TextSelection::Span {
|
||||
start: Cursor::new(0, 0),
|
||||
end: Cursor::new(last_line, last_idx),
|
||||
};
|
||||
}
|
||||
}
|
||||
"z" => {
|
||||
*undo = true;
|
||||
}
|
||||
_ => self.insert(text),
|
||||
}
|
||||
} else {
|
||||
self.insert(text);
|
||||
}
|
||||
}
|
||||
_ => return TextInputResult::Unused,
|
||||
}
|
||||
TextInputResult::Used
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for TextEdit {
|
||||
@@ -175,373 +540,6 @@ fn cursor_pos(cursor: Cursor, buf: &TextBuffer) -> Option<Vec2> {
|
||||
prev
|
||||
}
|
||||
|
||||
pub struct TextEditCtx<'a> {
|
||||
pub text: &'a mut TextEdit,
|
||||
pub font_system: &'a mut FontSystem,
|
||||
}
|
||||
|
||||
impl<'a> TextEditCtx<'a> {
|
||||
pub fn take(&mut self) -> String {
|
||||
let text = self
|
||||
.text
|
||||
.buf
|
||||
.lines
|
||||
.drain(..)
|
||||
.map(|l| l.into_text())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
self.text
|
||||
.buf
|
||||
.set_text(self.font_system, "", &Attrs::new(), SHAPING, None);
|
||||
self.text.selection.clear();
|
||||
text
|
||||
}
|
||||
|
||||
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);
|
||||
self.text.selection.clear();
|
||||
}
|
||||
|
||||
pub fn motion(&mut self, motion: Motion, select: bool) {
|
||||
if let TextSelection::Pos(cursor) = self.text.selection
|
||||
&& let Some(new) = self.buf_motion(cursor, motion)
|
||||
{
|
||||
if select {
|
||||
self.text.selection = TextSelection::Span {
|
||||
start: cursor,
|
||||
end: new,
|
||||
};
|
||||
} else {
|
||||
self.text.selection = TextSelection::Pos(new);
|
||||
}
|
||||
} else if let TextSelection::Span { start, end } = self.text.selection {
|
||||
if select {
|
||||
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||
self.text.selection = TextSelection::Span { start, end: cursor };
|
||||
}
|
||||
} else {
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
let sel = &mut self.text.selection;
|
||||
match motion {
|
||||
Motion::Left | Motion::LeftWord => *sel = TextSelection::Pos(start),
|
||||
Motion::Right | Motion::RightWord => *sel = TextSelection::Pos(end),
|
||||
_ => {
|
||||
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||
self.text.selection = TextSelection::Pos(cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
self.insert_inner(first, true);
|
||||
for line in lines {
|
||||
self.newline();
|
||||
self.insert_inner(line, true);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_span(&mut self) -> bool {
|
||||
if let TextSelection::Span { start, end } = self.text.selection {
|
||||
self.delete_between(start, end);
|
||||
let (start, _) = sort_cursors(start, end);
|
||||
self.text.selection = TextSelection::Pos(start);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_between(&mut self, start: Cursor, end: Cursor) {
|
||||
let lines = &mut self.text.view.buf.lines;
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
if start.line == end.line {
|
||||
let line = &mut lines[start.line];
|
||||
let text = line.text();
|
||||
let text = text[..start.index].to_string() + &text[end.index..];
|
||||
edit_line(line, text);
|
||||
} else {
|
||||
// start
|
||||
let start_text = lines[start.line].text()[..start.index].to_string();
|
||||
let end_text = &lines[end.line].text()[end.index..];
|
||||
let text = start_text + end_text;
|
||||
edit_line(&mut lines[start.line], text);
|
||||
}
|
||||
// between
|
||||
let range = (start.line + 1)..=end.line;
|
||||
if !range.is_empty() {
|
||||
lines.splice(range, None);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_inner(&mut self, text: &str, mov: bool) {
|
||||
self.clear_span();
|
||||
if let TextSelection::Pos(cursor) = &mut self.text.selection {
|
||||
let line = &mut self.text.view.buf.lines[cursor.line];
|
||||
let mut line_text = line.text().to_string();
|
||||
line_text.insert_str(cursor.index, text);
|
||||
edit_line(line, line_text);
|
||||
if mov {
|
||||
for _ in 0..text.chars().count() {
|
||||
self.motion(Motion::Right, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
let line = &mut lines[cursor.line];
|
||||
let new = line.split_off(cursor.index);
|
||||
cursor.line += 1;
|
||||
lines.insert(cursor.line, new);
|
||||
cursor.index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self, word: bool) {
|
||||
if !self.clear_span()
|
||||
&& let TextSelection::Pos(cursor) = &mut self.text.selection
|
||||
&& (cursor.index != 0 || cursor.line != 0)
|
||||
{
|
||||
self.motion(if word { Motion::LeftWord } else { Motion::Left }, false);
|
||||
self.delete(word);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, word: bool) {
|
||||
if !self.clear_span()
|
||||
&& let TextSelection::Pos(cursor) = &mut self.text.selection
|
||||
{
|
||||
if word {
|
||||
let start = *cursor;
|
||||
if let Some(end) = self.buf_motion(start, Motion::RightWord) {
|
||||
self.delete_between(start, end);
|
||||
}
|
||||
} else {
|
||||
let lines = &mut self.text.view.buf.lines;
|
||||
let line = &mut lines[cursor.line];
|
||||
if cursor.index == line.text().len() {
|
||||
if cursor.line == lines.len() - 1 {
|
||||
return;
|
||||
}
|
||||
let add = lines.remove(cursor.line + 1).into_text();
|
||||
let line = &mut lines[cursor.line];
|
||||
let mut cur = line.text().to_string();
|
||||
cur.push_str(&add);
|
||||
edit_line(line, cur);
|
||||
} else {
|
||||
let mut text = line.text().to_string();
|
||||
text.remove(cursor.index);
|
||||
edit_line(line, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn buf_motion(&mut self, cursor: Cursor, motion: Motion) -> Option<Cursor> {
|
||||
self.text
|
||||
.buf
|
||||
.cursor_motion(self.font_system, cursor, None, motion)
|
||||
.map(|r| r.0)
|
||||
}
|
||||
|
||||
pub fn select_word_at(&mut self, cursor: Cursor) {
|
||||
if let (Some(start), Some(end)) = (
|
||||
self.buf_motion(cursor, Motion::LeftWord),
|
||||
self.buf_motion(cursor, Motion::RightWord),
|
||||
) {
|
||||
self.text.selection = TextSelection::Span { start, end };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_line_at(&mut self, cursor: Cursor) {
|
||||
let end = self.text.buf.lines[cursor.line].text().len();
|
||||
self.text.selection = TextSelection::Span {
|
||||
start: Cursor::new(cursor.line, 0),
|
||||
end: Cursor::new(cursor.line, end),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, pos: Vec2, size: Vec2, drag: bool, recent: bool) {
|
||||
let pos = pos - self.text.region().top_left().to_abs(size);
|
||||
let hit = self.text.buf.hit(pos.x, pos.y);
|
||||
let sel = &mut self.text.selection;
|
||||
match sel {
|
||||
TextSelection::None => {
|
||||
if !drag && let Some(hit) = hit {
|
||||
*sel = TextSelection::Pos(hit)
|
||||
}
|
||||
}
|
||||
TextSelection::Pos(pos) => match (hit, drag) {
|
||||
(None, false) => *sel = TextSelection::None,
|
||||
(None, true) => (),
|
||||
(Some(hit), false) => {
|
||||
if recent && hit == *pos {
|
||||
self.text.double_hit = Some(hit);
|
||||
return self.select_word_at(hit);
|
||||
} else {
|
||||
*pos = hit
|
||||
}
|
||||
}
|
||||
(Some(end), true) => *sel = TextSelection::Span { start: *pos, end },
|
||||
},
|
||||
TextSelection::Span { start, end } => match (hit, drag) {
|
||||
(None, false) => *sel = TextSelection::None,
|
||||
(None, true) => *sel = TextSelection::Pos(*start),
|
||||
(Some(hit), false) => {
|
||||
if recent
|
||||
&& let Some(double) = self.text.double_hit
|
||||
&& double == hit
|
||||
{
|
||||
return self.select_line_at(hit);
|
||||
} else {
|
||||
*sel = TextSelection::Pos(hit)
|
||||
}
|
||||
}
|
||||
(Some(hit), true) => *end = hit,
|
||||
},
|
||||
}
|
||||
if let TextSelection::Span { start, end } = sel
|
||||
&& start == end
|
||||
{
|
||||
*sel = TextSelection::Pos(*start);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deselect(&mut self) {
|
||||
self.text.selection = TextSelection::None;
|
||||
}
|
||||
|
||||
pub fn apply_event(&mut self, event: &KeyEvent, modifiers: &Modifiers) -> TextInputResult {
|
||||
let old = (self.text.content(), self.text.selection);
|
||||
let mut undo = false;
|
||||
let res = self.apply_event_inner(event, modifiers, &mut undo);
|
||||
if undo && let Some((old, selection)) = self.text.history.pop() {
|
||||
self.set(&old);
|
||||
self.text.selection = selection;
|
||||
} else if self.text.content() != old.0 {
|
||||
self.text.history.push(old);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn apply_event_inner(
|
||||
&mut self,
|
||||
event: &KeyEvent,
|
||||
modifiers: &Modifiers,
|
||||
undo: &mut bool,
|
||||
) -> TextInputResult {
|
||||
match &event.logical_key {
|
||||
Key::Named(named) => match named {
|
||||
NamedKey::Backspace => self.backspace(modifiers.control),
|
||||
NamedKey::Delete => self.delete(modifiers.control),
|
||||
NamedKey::Space => self.insert(" "),
|
||||
NamedKey::Enter => {
|
||||
if modifiers.shift {
|
||||
self.newline();
|
||||
} else {
|
||||
return TextInputResult::Submit;
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowRight => {
|
||||
if modifiers.control {
|
||||
self.motion(Motion::RightWord, modifiers.shift)
|
||||
} else {
|
||||
self.motion(Motion::Right, modifiers.shift)
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowLeft => {
|
||||
if modifiers.control {
|
||||
self.motion(Motion::LeftWord, modifiers.shift)
|
||||
} else {
|
||||
self.motion(Motion::Left, modifiers.shift)
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowUp => self.motion(Motion::Up, modifiers.shift),
|
||||
NamedKey::ArrowDown => self.motion(Motion::Down, modifiers.shift),
|
||||
NamedKey::Escape => {
|
||||
self.deselect();
|
||||
return TextInputResult::Unfocus;
|
||||
}
|
||||
_ => return TextInputResult::Unused,
|
||||
},
|
||||
Key::Character(text) => {
|
||||
if modifiers.control {
|
||||
match text.as_str() {
|
||||
"v" => return TextInputResult::Paste,
|
||||
"c" => {
|
||||
if let TextSelection::Span { start, end } = self.text.selection {
|
||||
let content = self.text.select_content(start, end);
|
||||
return TextInputResult::Copy(content);
|
||||
}
|
||||
}
|
||||
"x" => {
|
||||
if let TextSelection::Span { start, end } = self.text.selection {
|
||||
let content = self.text.select_content(start, end);
|
||||
self.clear_span();
|
||||
return TextInputResult::Copy(content);
|
||||
}
|
||||
}
|
||||
"a" => {
|
||||
if !self.text.buf.lines[0].text().is_empty()
|
||||
|| self.text.buf.lines.len() > 1
|
||||
{
|
||||
let lines = &self.text.buf.lines;
|
||||
let last_line = lines.len() - 1;
|
||||
let last_idx = lines[last_line].text().len();
|
||||
self.text.selection = TextSelection::Span {
|
||||
start: Cursor::new(0, 0),
|
||||
end: Cursor::new(last_line, last_idx),
|
||||
};
|
||||
}
|
||||
}
|
||||
"z" => {
|
||||
*undo = true;
|
||||
}
|
||||
_ => self.insert(text),
|
||||
}
|
||||
} else {
|
||||
self.insert(text);
|
||||
}
|
||||
}
|
||||
_ => return TextInputResult::Unused,
|
||||
}
|
||||
TextInputResult::Used
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Modifiers {
|
||||
pub shift: bool,
|
||||
|
||||
@@ -21,11 +21,11 @@ pub struct TextView {
|
||||
// cache
|
||||
tex: Option<RenderedText>,
|
||||
width: Option<f32>,
|
||||
pub hint: Option<WidgetId>,
|
||||
pub hint: Option<WidgetRef>,
|
||||
}
|
||||
|
||||
impl TextView {
|
||||
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetId>) -> Self {
|
||||
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetRef>) -> Self {
|
||||
Self {
|
||||
attrs: attrs.into(),
|
||||
buf: buf.into(),
|
||||
@@ -67,9 +67,12 @@ impl TextView {
|
||||
return tex.clone();
|
||||
}
|
||||
self.width = width;
|
||||
let font_system = &mut ctx.text.font_system;
|
||||
self.attrs.apply(font_system, &mut self.buf, width);
|
||||
self.buf.shape_until_scroll(font_system, false);
|
||||
let mut text_data = ctx.text.borrow_mut();
|
||||
self.attrs
|
||||
.apply(&mut text_data.font_system, &mut self.buf, width);
|
||||
self.buf
|
||||
.shape_until_scroll(&mut text_data.font_system, false);
|
||||
drop(text_data);
|
||||
let tex = ctx.draw_text(&mut self.buf, &self.attrs);
|
||||
self.tex = Some(tex.clone());
|
||||
self.attrs.changed = false;
|
||||
@@ -136,7 +139,7 @@ impl Text {
|
||||
if self.content.changed {
|
||||
self.content.changed = false;
|
||||
self.view.buf.set_text(
|
||||
&mut ctx.text.font_system,
|
||||
&mut ctx.text.borrow_mut().font_system,
|
||||
&self.content,
|
||||
&Attrs::new().family(self.view.attrs.family),
|
||||
SHAPING,
|
||||
|
||||
Reference in New Issue
Block a user