refactoring for home and end keys, better Modifiers abstraction

This commit is contained in:
Anton-4 2021-02-27 19:52:28 +01:00
parent d153e580a1
commit fe916f0d03
7 changed files with 611 additions and 536 deletions

View file

@ -408,20 +408,20 @@ fn begin_render_pass<'a>(
fn queue_editor_text( fn queue_editor_text(
size: &PhysicalSize<u32>, size: &PhysicalSize<u32>,
_editor_lines: &str, editor_lines: &str,
caret_pos: TextPos, caret_pos: TextPos,
config: &Config, config: &Config,
glyph_brush: &mut GlyphBrush<()>, glyph_brush: &mut GlyphBrush<()>,
) { ) {
let area_bounds = (size.width as f32, size.height as f32).into(); let area_bounds = (size.width as f32, size.height as f32).into();
// let code_text = Text { let code_text = Text {
// position: CODE_TXT_XY.into(), position: CODE_TXT_XY.into(),
// area_bounds, area_bounds,
// text: editor_lines, text: editor_lines,
// size: settings.code_font_size, size: config.code_font_size,
// ..Default::default() ..Default::default()
// }; };
let s = format!("Ln {}, Col {}", caret_pos.line, caret_pos.column); let s = format!("Ln {}, Col {}", caret_pos.line, caret_pos.column);
let text = s.as_str(); let text = s.as_str();
@ -438,7 +438,7 @@ fn queue_editor_text(
queue_text_draw(&caret_pos_label, glyph_brush); queue_text_draw(&caret_pos_label, glyph_brush);
// TODO convert to ast and render with render_ast::render_expr2 // TODO convert to ast and render with render_ast::render_expr2
//queue_code_text_draw(&code_text, &ed_theme.syntax_high_map, settings, glyph_brush); queue_text_draw(&code_text, glyph_brush);
} }
fn _queue_no_file_text( fn _queue_no_file_text(

View file

@ -12,6 +12,7 @@ mod editor;
mod graphics; mod graphics;
pub mod lang; //TODO remove pub for unused warnings pub mod lang; //TODO remove pub for unused warnings
mod ui; mod ui;
mod window;
use std::io; use std::io;
use std::path::Path; use std::path::Path;

File diff suppressed because it is too large Load diff

View file

@ -2,7 +2,7 @@ use super::selection::validate_selection;
use super::selection::Selection; use super::selection::Selection;
use super::text_pos::TextPos; use super::text_pos::TextPos;
use crate::ui::ui_error::UIResult; use crate::ui::ui_error::UIResult;
use winit::event::ModifiersState; use crate::window::keyboard_input::Modifiers;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct CaretWSelect { pub struct CaretWSelect {
@ -31,12 +31,12 @@ impl CaretWSelect {
} }
} }
pub fn move_caret_w_mods(&mut self, new_pos: TextPos, mods: &ModifiersState) -> UIResult<()> { pub fn move_caret_w_mods(&mut self, new_pos: TextPos, mods: &Modifiers) -> UIResult<()> {
let caret_pos = self.caret_pos; let caret_pos = self.caret_pos;
// one does not simply move the caret // one does not simply move the caret
let valid_sel_opt = if new_pos != caret_pos { let valid_sel_opt = if new_pos != caret_pos {
if mods.shift() { if mods.shift {
if let Some(old_sel) = self.selection_opt { if let Some(old_sel) = self.selection_opt {
if new_pos < old_sel.start_pos { if new_pos < old_sel.start_pos {
if caret_pos > old_sel.start_pos { if caret_pos > old_sel.start_pos {
@ -55,7 +55,6 @@ impl CaretWSelect {
} else if new_pos < caret_pos { } else if new_pos < caret_pos {
mk_some_sel(old_sel.start_pos, new_pos)? mk_some_sel(old_sel.start_pos, new_pos)?
} else { } else {
// TODO should this return none?
None None
} }
} else if new_pos < self.caret_pos { } else if new_pos < self.caret_pos {

View file

@ -5,9 +5,10 @@ use crate::ui::text::{
text_pos::TextPos, text_pos::TextPos,
}; };
use crate::ui::ui_error::UIResult; use crate::ui::ui_error::UIResult;
use crate::window::keyboard_input::Modifiers;
use bumpalo::collections::String as BumpString; use bumpalo::collections::String as BumpString;
use bumpalo::Bump; use bumpalo::Bump;
use winit::event::{ModifiersState, VirtualKeyCode}; use winit::event::{VirtualKeyCode};
pub trait Lines { pub trait Lines {
fn get_line(&self, line_nr: usize) -> UIResult<&str>; fn get_line(&self, line_nr: usize) -> UIResult<&str>;
@ -27,13 +28,17 @@ pub trait SelectableLines {
fn set_caret(&mut self, caret_pos: TextPos); fn set_caret(&mut self, caret_pos: TextPos);
fn move_caret_left(&mut self, shift_pressed: bool) -> UIResult<()>; fn move_caret_left(&mut self, modifiers: &Modifiers) -> UIResult<()>;
fn move_caret_right(&mut self, shift_pressed: bool) -> UIResult<()>; fn move_caret_right(&mut self, modifiers: &Modifiers) -> UIResult<()>;
fn move_caret_up(&mut self, shift_pressed: bool) -> UIResult<()>; fn move_caret_up(&mut self, modifiers: &Modifiers) -> UIResult<()>;
fn move_caret_down(&mut self, shift_pressed: bool) -> UIResult<()>; fn move_caret_down(&mut self, modifiers: &Modifiers) -> UIResult<()>;
fn move_caret_home(&mut self, modifiers: &Modifiers) -> UIResult<()>;
fn move_caret_end(&mut self, modifiers: &Modifiers) -> UIResult<()>;
fn get_selection(&self) -> Option<Selection>; fn get_selection(&self) -> Option<Selection>;
@ -48,6 +53,12 @@ pub trait SelectableLines {
fn select_all(&mut self) -> UIResult<()>; fn select_all(&mut self) -> UIResult<()>;
fn last_text_pos(&self) -> TextPos; fn last_text_pos(&self) -> TextPos;
fn handle_key_down(
&mut self,
modifiers: &Modifiers,
virtual_keycode: VirtualKeyCode,
) -> UIResult<()>;
} }
pub trait MutSelectableLines { pub trait MutSelectableLines {
@ -61,10 +72,4 @@ pub trait MutSelectableLines {
fn pop_char(&mut self) -> UIResult<()>; fn pop_char(&mut self) -> UIResult<()>;
fn del_selection(&mut self) -> UIResult<()>; fn del_selection(&mut self) -> UIResult<()>;
fn handle_key_down(
&mut self,
modifiers: &ModifiersState,
virtual_keycode: VirtualKeyCode,
) -> UIResult<()>;
} }

View file

@ -0,0 +1,44 @@
pub struct Modifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub logo: bool,
}
impl Default for Modifiers {
fn default() -> Self {
Self {
shift: false,
ctrl: false,
alt: false,
logo: false
}
}
}
pub fn no_mods() -> Modifiers {
Modifiers {
shift: false,
ctrl: false,
alt: false,
logo: false
}
}
pub fn from_winit(winit_mods: &winit::event::ModifiersState) -> Modifiers {
Modifiers {
shift: winit_mods.shift(),
ctrl: winit_mods.ctrl(),
alt: winit_mods.alt(),
logo: winit_mods.logo(),
}
}
pub fn shift_pressed() -> Modifiers {
Modifiers {
shift: true,
..Default::default()
}
}

1
editor/src/window/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod keyboard_input;