renamed txt_cursor to caret

This commit is contained in:
Anton-4 2020-12-30 17:21:21 +01:00
parent dcee932495
commit fb66312a69
4 changed files with 56 additions and 56 deletions

View file

@ -1,6 +1,6 @@
use crate::tea::model::Model;
use crate::tea::update::{
move_txt_cursor_down, move_txt_cursor_left, move_txt_cursor_right, move_txt_cursor_up,
move_caret_down, move_caret_left, move_caret_right, move_caret_up,
};
use winit::event::{ElementState, ModifiersState, VirtualKeyCode};
@ -18,43 +18,43 @@ pub fn handle_keydown(
match virtual_keycode {
Left => {
let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_left(
model.txt_cursor_pos,
let (new_caret_pos, new_selection_opt) = move_caret_left(
model.caret_pos,
model.selection_opt,
modifiers.shift(),
&model.lines,
);
model.txt_cursor_pos = new_txt_cursor_pos;
model.caret_pos = new_caret_pos;
model.selection_opt = new_selection_opt;
}
Up => {
let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_up(
model.txt_cursor_pos,
let (new_caret_pos, new_selection_opt) = move_caret_up(
model.caret_pos,
model.selection_opt,
modifiers.shift(),
&model.lines,
);
model.txt_cursor_pos = new_txt_cursor_pos;
model.caret_pos = new_caret_pos;
model.selection_opt = new_selection_opt;
}
Right => {
let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_right(
model.txt_cursor_pos,
let (new_caret_pos, new_selection_opt) = move_caret_right(
model.caret_pos,
model.selection_opt,
modifiers.shift(),
&model.lines,
);
model.txt_cursor_pos = new_txt_cursor_pos;
model.caret_pos = new_caret_pos;
model.selection_opt = new_selection_opt;
}
Down => {
let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_down(
model.txt_cursor_pos,
let (new_caret_pos, new_selection_opt) = move_caret_down(
model.caret_pos,
model.selection_opt,
modifiers.shift(),
&model.lines,
);
model.txt_cursor_pos = new_txt_cursor_pos;
model.caret_pos = new_caret_pos;
model.selection_opt = new_selection_opt;
}
Copy => {
@ -122,7 +122,7 @@ pub fn handle_keydown(
// Escape | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | F13 | F14 | F15
// | F16 | F17 | F18 | F19 | F20 | F21 | F22 | F23 | F24 | Snapshot | Scroll | Pause
// | Insert | Home | Delete | End | PageDown | PageUp | Left | Up | Right | Down | Compose
// | txt_cursor | Numlock | AbntC1 | AbntC2 | Ax | Calculator | Capital | Convert | Kana
// | caret | Numlock | AbntC1 | AbntC2 | Ax | Calculator | Capital | Convert | Kana
// | Kanji | LAlt | LBracket | LControl | LShift | LWin | Mail | MediaSelect | PlayPause
// | Power | PrevTrack | MediaStop | Mute | MyComputer | NavigateForward
// | NavigateBackward | NextTrack | NoConvert | OEM102 | RAlt | Sysrq | RBracket

View file

@ -214,7 +214,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
let glyph_bounds_rects = queue_all_text(
&size,
&ed_model.lines,
ed_model.txt_cursor_pos,
ed_model.caret_pos,
&mut glyph_brush,
);
@ -371,7 +371,7 @@ fn create_render_pipeline(
fn queue_all_text(
size: &winit::dpi::PhysicalSize<u32>,
lines: &[String],
txt_cursor_pos: Position,
caret_pos: Position,
glyph_brush: &mut wgpu_glyph::GlyphBrush<()>,
) -> Vec<Vec<Rect>> {
let area_bounds = (size.width as f32, size.height as f32).into();
@ -394,18 +394,18 @@ fn queue_all_text(
..Default::default()
};
let txt_cursor_pos_label = Text {
let caret_pos_label = Text {
position: (30.0, 530.0).into(),
area_bounds,
color: (0.4666, 0.2, 1.0, 1.0).into(),
text: format!("Ln {}, Col {}", txt_cursor_pos.line, txt_cursor_pos.column),
text: format!("Ln {}, Col {}", caret_pos.line, caret_pos.column),
size: 30.0,
..Default::default()
};
text::queue_text_draw(&main_label, glyph_brush);
text::queue_text_draw(&txt_cursor_pos_label, glyph_brush);
text::queue_text_draw(&caret_pos_label, glyph_brush);
text::queue_text_draw(&code_text, glyph_brush)
}
@ -423,8 +423,8 @@ fn update_text_state(ed_model: &mut model::Model, received_char: &char) {
} else if ed_model.lines.len() > 1 {
ed_model.lines.pop();
}
ed_model.txt_cursor_pos = update::move_txt_cursor_left(
ed_model.txt_cursor_pos,
ed_model.caret_pos = update::move_caret_left(
ed_model.caret_pos,
None,
false,
&ed_model.lines,
@ -441,8 +441,8 @@ fn update_text_state(ed_model: &mut model::Model, received_char: &char) {
last_line.push(*received_char)
}
ed_model.lines.push(String::new());
ed_model.txt_cursor_pos = Position {
line: ed_model.txt_cursor_pos.line + 1,
ed_model.caret_pos = Position {
line: ed_model.caret_pos.line + 1,
column: 0,
};
@ -454,7 +454,7 @@ fn update_text_state(ed_model: &mut model::Model, received_char: &char) {
if let Some(last_line) = ed_model.lines.last_mut() {
last_line.push(*received_char);
ed_model.txt_cursor_pos = Position {
ed_model.caret_pos = Position {
line: nr_lines - 1,
column: last_line.len(),
};

View file

@ -3,14 +3,14 @@ use std::cmp::Ordering;
#[derive(Debug)]
pub struct Model {
pub lines: Vec<String>,
pub txt_cursor_pos: Position,
pub caret_pos: Position,
pub selection_opt: Option<RawSelection>,
}
pub fn init_model() -> Model {
Model {
lines: vec![String::new()],
txt_cursor_pos: Position { line: 0, column: 0 },
caret_pos: Position { line: 0, column: 0 },
selection_opt: None,
}
}

View file

@ -2,14 +2,14 @@ use crate::tea::model::{Position, RawSelection};
use crate::text::is_newline;
use std::cmp::{max, min};
pub fn move_txt_cursor_left(
old_txt_cursor_pos: Position,
pub fn move_caret_left(
old_caret_pos: Position,
old_selection_opt: Option<RawSelection>,
shift_pressed: bool,
lines: &[String],
) -> (Position, Option<RawSelection>) {
let old_line_nr = old_txt_cursor_pos.line;
let old_col_nr = old_txt_cursor_pos.column;
let old_line_nr = old_caret_pos.line;
let old_col_nr = old_caret_pos.column;
let (line_nr, col_nr) = if old_col_nr == 0 {
if old_line_nr == 0 {
@ -23,7 +23,7 @@ pub fn move_txt_cursor_left(
(old_line_nr, old_col_nr - 1)
};
let new_txt_cursor_pos = Position {
let new_caret_pos = Position {
line: line_nr,
column: col_nr,
};
@ -55,17 +55,17 @@ pub fn move_txt_cursor_left(
None
};
(new_txt_cursor_pos, new_selection_opt)
(new_caret_pos, new_selection_opt)
}
pub fn move_txt_cursor_right(
old_txt_cursor_pos: Position,
pub fn move_caret_right(
old_caret_pos: Position,
old_selection_opt: Option<RawSelection>,
shift_pressed: bool,
lines: &[String],
) -> (Position, Option<RawSelection>) {
let old_line_nr = old_txt_cursor_pos.line;
let old_col_nr = old_txt_cursor_pos.column;
let old_line_nr = old_caret_pos.line;
let old_col_nr = old_caret_pos.column;
let (line_nr, col_nr) = if let Some(curr_line) = lines.get(old_line_nr) {
if let Some(last_char) = curr_line.chars().last() {
@ -87,7 +87,7 @@ pub fn move_txt_cursor_right(
unreachable!()
};
let new_txt_cursor_pos = Position {
let new_caret_pos = Position {
line: line_nr,
column: col_nr,
};
@ -119,17 +119,17 @@ pub fn move_txt_cursor_right(
None
};
(new_txt_cursor_pos, new_selection_opt)
(new_caret_pos, new_selection_opt)
}
pub fn move_txt_cursor_up(
old_txt_cursor_pos: Position,
pub fn move_caret_up(
old_caret_pos: Position,
old_selection_opt: Option<RawSelection>,
shift_pressed: bool,
lines: &[String],
) -> (Position, Option<RawSelection>) {
let old_line_nr = old_txt_cursor_pos.line;
let old_col_nr = old_txt_cursor_pos.column;
let old_line_nr = old_caret_pos.line;
let old_col_nr = old_caret_pos.column;
let (line_nr, col_nr) = if old_line_nr == 0 {
(old_line_nr, old_col_nr)
@ -143,7 +143,7 @@ pub fn move_txt_cursor_up(
unreachable!()
};
let new_txt_cursor_pos = Position {
let new_caret_pos = Position {
line: line_nr,
column: col_nr,
};
@ -151,13 +151,13 @@ pub fn move_txt_cursor_up(
let new_selection_opt = if shift_pressed {
if let Some(old_selection) = old_selection_opt {
Some(RawSelection {
start_pos: new_txt_cursor_pos,
start_pos: new_caret_pos,
end_pos: old_selection.end_pos,
})
} else if !(old_line_nr == line_nr && old_col_nr == col_nr) {
Some(RawSelection {
start_pos: min(old_txt_cursor_pos, new_txt_cursor_pos),
end_pos: max(old_txt_cursor_pos, new_txt_cursor_pos),
start_pos: min(old_caret_pos, new_caret_pos),
end_pos: max(old_caret_pos, new_caret_pos),
})
} else {
None
@ -166,17 +166,17 @@ pub fn move_txt_cursor_up(
None
};
(new_txt_cursor_pos, new_selection_opt)
(new_caret_pos, new_selection_opt)
}
pub fn move_txt_cursor_down(
old_txt_cursor_pos: Position,
pub fn move_caret_down(
old_caret_pos: Position,
old_selection_opt: Option<RawSelection>,
shift_pressed: bool,
lines: &[String],
) -> (Position, Option<RawSelection>) {
let old_line_nr = old_txt_cursor_pos.line;
let old_col_nr = old_txt_cursor_pos.column;
let old_line_nr = old_caret_pos.line;
let old_col_nr = old_caret_pos.column;
let (line_nr, col_nr) = if old_line_nr + 1 >= lines.len() {
(old_line_nr, old_col_nr)
@ -198,7 +198,7 @@ pub fn move_txt_cursor_down(
unreachable!()
};
let new_txt_cursor_pos = Position {
let new_caret_pos = Position {
line: line_nr,
column: col_nr,
};
@ -207,12 +207,12 @@ pub fn move_txt_cursor_down(
if let Some(old_selection) = old_selection_opt {
Some(RawSelection {
start_pos: old_selection.start_pos,
end_pos: new_txt_cursor_pos,
end_pos: new_caret_pos,
})
} else if !(old_line_nr == line_nr && old_col_nr == col_nr) {
Some(RawSelection {
start_pos: min(old_txt_cursor_pos, new_txt_cursor_pos),
end_pos: max(old_txt_cursor_pos, new_txt_cursor_pos),
start_pos: min(old_caret_pos, new_caret_pos),
end_pos: max(old_caret_pos, new_caret_pos),
})
} else {
None
@ -221,5 +221,5 @@ pub fn move_txt_cursor_down(
None
};
(new_txt_cursor_pos, new_selection_opt)
(new_caret_pos, new_selection_opt)
}