diff --git a/Cargo.lock b/Cargo.lock index e94d1ff..7a211d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,7 +155,7 @@ dependencies = [ "syntect 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "wgpu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "wgpu_glyph 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "winit 0.20.0-alpha5 (registry+https://github.com/rust-lang/crates.io-index)", + "winit 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1635,12 +1635,11 @@ dependencies = [ [[package]] name = "winit" -version = "0.20.0-alpha5" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "calloop 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "cocoa 0.19.1 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "core-graphics 0.17.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1650,6 +1649,8 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-extras 2.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1927,7 +1928,7 @@ dependencies = [ "checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9" -"checksum winit 0.20.0-alpha5 (registry+https://github.com/rust-lang/crates.io-index)" = "14fbf7c84c8c1ff9d72982cb382c1a78fbe0f7556f49b1ca98de212d018c5b82" +"checksum winit 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ba128780050481f453bec2a115b916dbc6ae79c303dee9bad8b9080bdccd4f5" "checksum wio 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" "checksum x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39697e3123f715483d311b5826e254b6f3cfebdd83cf7ef3358f579c3d68e235" diff --git a/Cargo.toml b/Cargo.toml index ddd5335..7f8d825 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,4 @@ edition = "2018" syntect = "3.3" wgpu = "0.4.0" wgpu_glyph = "0.6.0" -winit = "0.20.0-alpha5" # TODO: Update once out of alpha +winit = "0.20.0" diff --git a/src/buffer.rs b/src/buffer.rs index cd629e6..46a7d91 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -24,10 +24,7 @@ struct Location { impl Location { fn new() -> Location { - Location { - row: 0, - col: 0, - } + Location { row: 0, col: 0 } } } @@ -51,7 +48,9 @@ impl Span { } fn get_char_indices_for_line(&self, line: usize, line_length: usize) -> Option<(usize, usize)> { - if !self.contains_line(line) { return None; } + if !self.contains_line(line) { + return None; + } // 4 Cases: // Start/End line @@ -60,9 +59,9 @@ impl Span { // End line if self.start.row == self.end.row { - Some((self.start.col, self.end.col)) + Some((self.start.col, self.end.col)) } else if self.start.row == line { - Some((self.start.col, line_length)) + Some((self.start.col, line_length)) } else if self.end.row == line { Some((0, self.end.col)) } else { @@ -86,7 +85,7 @@ impl Cursor { selection_start: None, } } - + fn set_row(&mut self, row: usize) { self.location.row = row; } @@ -122,7 +121,7 @@ pub struct Buffer { scroll: f32, cursor: Cursor, dragging: bool, - size: PhysicalSize, + size: PhysicalSize, path: PathBuf, // TODO: Move those to editor? syntax_set: SyntaxSet, @@ -163,7 +162,7 @@ fn generate_highlight_info( } impl Buffer { - pub fn new(size: PhysicalSize, file_name: String) -> Buffer { + pub fn new(size: PhysicalSize, file_name: String) -> Buffer { let path = Path::new(&file_name); let file = std::fs::read_to_string(path).expect("Failed to read file."); // TODO: Not sure if just splitting '\n' is right here. @@ -194,7 +193,7 @@ impl Buffer { std::fs::write(&self.path, self.lines.join("\n")).expect("Failed to save file."); } - pub fn update_size(&mut self, size: PhysicalSize) { + pub fn update_size(&mut self, size: PhysicalSize) { self.size = size; } @@ -224,7 +223,7 @@ impl Buffer { &mut self, button: MouseButton, state: ElementState, - position: PhysicalPosition, + position: PhysicalPosition, ) { if button == MouseButton::Left { if state == ElementState::Pressed { @@ -239,7 +238,7 @@ impl Buffer { } } - pub fn handle_mouse_move(&mut self, position: PhysicalPosition) { + pub fn handle_mouse_move(&mut self, position: PhysicalPosition) { if self.dragging { if self.cursor.selection_start.is_none() { self.cursor.selection_start = Some(self.cursor.location); @@ -250,14 +249,14 @@ impl Buffer { } } - fn hit_test(&self, position: PhysicalPosition) -> Location { + fn hit_test(&self, position: PhysicalPosition) -> Location { let x_pad = 10.0; let digit_count = self.lines.len().to_string().chars().count(); let gutter_offset = x_pad + 30.0 + digit_count as f32 * (SCALE / 2.0); let abs_position = PhysicalPosition::new( - (position.x - gutter_offset as f64).max(0.0), - position.y + self.scroll as f64, + (position.x as f32 - gutter_offset).max(0.0), + position.y as f32 + self.scroll, ); let line = (abs_position.y / 40.0).floor() as usize; @@ -281,16 +280,28 @@ impl Buffer { self.cursor.set_row(self.cursor.location.row + 1); self.lines.insert(self.cursor.location.row, new_line); self.cursor.set_col_with_affinity(0); - } else if input == 127 as char { + // this is Backspace + } else if input == '\u{8}' { if self.cursor.location.col > 0 { self.lines[self.cursor.location.row].remove(self.cursor.location.col - 1); - self.cursor.set_col_with_affinity(self.cursor.location.col - 1); + self.cursor + .set_col_with_affinity(self.cursor.location.col - 1); } else if self.cursor.location.row > 0 { let remaining = self.lines.remove(self.cursor.location.row); self.cursor.set_row(self.cursor.location.row - 1); - self.cursor.set_col_with_affinity(self.lines[self.cursor.location.row].len()); + self.cursor + .set_col_with_affinity(self.lines[self.cursor.location.row].len()); self.lines[self.cursor.location.row].push_str(&remaining); } + // this is Delete + } else if input == '\u{7f}' { + if self.lines[self.cursor.location.row].len() > self.cursor.location.col { + self.lines[self.cursor.location.row].remove(self.cursor.location.col); + } + } else if input == '\t' { + // Do nothing, unless we consider how to display tab, + // because now cursor should be moved to right one character when deleting + // Also, now when there is \t in the file, it will not be displayed correctly } else { self.lines[self.cursor.location.row].insert(self.cursor.location.col, input); self.cursor.set_col(self.cursor.location.col + 1); @@ -325,9 +336,7 @@ impl Buffer { let row = (self.cursor.location.row as isize - 1) .max(0) .min(self.lines.len() as isize) as usize; - let col = self.lines[row] - .len() - .min(self.cursor.col_affinity); + let col = self.lines[row].len().min(self.cursor.col_affinity); self.cursor.set_row(row); self.cursor.set_col(col); } @@ -336,9 +345,7 @@ impl Buffer { let row = (self.cursor.location.row as isize + 1) .max(0) .min(self.lines.len() as isize - 1) as usize; - let col = self.lines[row] - .len() - .min(self.cursor.col_affinity); + let col = self.lines[row].len().min(self.cursor.col_affinity); self.cursor.set_row(row); self.cursor.set_col(col); } @@ -347,10 +354,12 @@ impl Buffer { if self.cursor.location.col == 0 { if self.cursor.location.row > 0 { self.cursor.set_row(self.cursor.location.row - 1); - self.cursor.set_col_with_affinity(self.lines[self.cursor.location.row].len()); + self.cursor + .set_col_with_affinity(self.lines[self.cursor.location.row].len()); } } else { - self.cursor.set_col_with_affinity(self.cursor.location.col - 1); + self.cursor + .set_col_with_affinity(self.cursor.location.col - 1); } } VirtualKeyCode::Right => { @@ -361,7 +370,8 @@ impl Buffer { self.cursor.set_col_with_affinity(0); } } else { - self.cursor.set_col_with_affinity(self.cursor.location.col + 1); + self.cursor + .set_col_with_affinity(self.cursor.location.col + 1); } } _ => {} @@ -371,7 +381,7 @@ impl Buffer { pub fn draw( &self, - size: PhysicalSize, + size: PhysicalSize, glyph_brush: &mut GlyphBrush<()>, rect_brush: &mut RectangleBrush, ) { @@ -411,7 +421,9 @@ impl Buffer { let mut line_no_color = [0.4, 0.4, 0.4, 1.0]; // Paint selection boxes - if let Some((start, end)) = selection_span.and_then(|span| span.get_char_indices_for_line(index, line.len())) { + if let Some((start, end)) = + selection_span.and_then(|span| span.get_char_indices_for_line(index, line.len())) + { // TODO: Gah, we should not do this. We should do a single layout pass and add some // methods that lets us query glyph locations. let layout = glyph_brush.fonts().first().unwrap().layout( diff --git a/src/editor.rs b/src/editor.rs index ec18efe..2e2c8d7 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -9,11 +9,11 @@ use winit::{ pub struct Editor { buffers: Vec, active_buffer: usize, - size: PhysicalSize, + size: PhysicalSize, } impl Editor { - pub fn new(size: PhysicalSize, file_name: String) -> Editor { + pub fn new(size: PhysicalSize, file_name: String) -> Editor { Editor { buffers: vec![Buffer::new(size, file_name)], active_buffer: 0, @@ -25,7 +25,7 @@ impl Editor { self.buffers[self.active_buffer].save(); } - pub fn update_size(&mut self, size: PhysicalSize) { + pub fn update_size(&mut self, size: PhysicalSize) { self.size = size; for buffer in &mut self.buffers { buffer.update_size(size); @@ -44,18 +44,18 @@ impl Editor { &mut self, button: MouseButton, state: ElementState, - position: PhysicalPosition, + position: PhysicalPosition, ) { self.buffers[self.active_buffer].handle_mouse_input(button, state, position); } - pub fn handle_mouse_move(&mut self, position: PhysicalPosition) { + pub fn handle_mouse_move(&mut self, position: PhysicalPosition) { self.buffers[self.active_buffer].handle_mouse_move(position); } pub fn draw( &self, - size: PhysicalSize, + size: PhysicalSize, glyph_brush: &mut GlyphBrush<()>, rect_brush: &mut RectangleBrush, ) { diff --git a/src/main.rs b/src/main.rs index 7b51181..d333509 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn main() -> Result<(), Box> { let window = WindowBuilder::new() .with_title("brewcode") .build(&event_loop)?; - let mut size = window.inner_size().to_physical(window.hidpi_factor()); + let mut size = window.inner_size(); let surface = wgpu::Surface::create(&window); let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { @@ -51,15 +51,15 @@ fn main() -> Result<(), Box> { &wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, format: render_format, - width: size.width.round() as u32, - height: size.height.round() as u32, + width: size.width, + height: size.height, present_mode: wgpu::PresentMode::Vsync, }, ); // TODO: Dynamically load fonts or something? - let inconsolata: &[u8] = - include_bytes!("/Users/connor/Library/Fonts/InconsolataGo-Regular.ttf"); + let inconsolata: &[u8] = include_bytes!("/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf"); + // /Users/connor/Library/Fonts/InconsolataGo-Regular.ttf"); let mut glyph_brush = GlyphBrushBuilder::using_font_bytes(inconsolata).build(&mut device, render_format); @@ -72,7 +72,7 @@ fn main() -> Result<(), Box> { let mut last_frame = std::time::Instant::now(); let mut modifier_pressed = false; - let mut cursor_position = PhysicalPosition::new(0.0, 0.0); + let mut cursor_position: PhysicalPosition = PhysicalPosition::new(0, 0); event_loop.run(move |event, _, control_flow| match event { Event::WindowEvent { @@ -84,17 +84,17 @@ fn main() -> Result<(), Box> { event: WindowEvent::KeyboardInput { input, .. }, .. } => { - if input.virtual_keycode == Some(VirtualKeyCode::LWin) { + if input.virtual_keycode == Some(VirtualKeyCode::LControl) { modifier_pressed = input.state == ElementState::Pressed; } match (input.virtual_keycode, input.modifiers) { // Quit - (Some(VirtualKeyCode::Q), ModifiersState { logo: true, .. }) => { + (Some(VirtualKeyCode::Q), ModifiersState::CTRL) => { *control_flow = ControlFlow::Exit } // Save - (Some(VirtualKeyCode::S), ModifiersState { logo: true, .. }) => { + (Some(VirtualKeyCode::S), ModifiersState::CTRL) => { editor.save(); } @@ -121,7 +121,7 @@ fn main() -> Result<(), Box> { event: WindowEvent::CursorMoved { position, .. }, .. } => { - cursor_position = position.to_physical(window.hidpi_factor()); + cursor_position = position; editor.handle_mouse_move(cursor_position); window.request_redraw(); } @@ -152,7 +152,7 @@ fn main() -> Result<(), Box> { event: WindowEvent::Resized(new_size), .. } => { - size = new_size.to_physical(window.hidpi_factor()); + size = new_size; editor.update_size(size); swap_chain = device.create_swap_chain( @@ -160,8 +160,8 @@ fn main() -> Result<(), Box> { &wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, format: render_format, - width: size.width.round() as u32, - height: size.height.round() as u32, + width: size.width, + height: size.height, present_mode: wgpu::PresentMode::Vsync, }, ); @@ -169,10 +169,7 @@ fn main() -> Result<(), Box> { window.request_redraw(); } - Event::WindowEvent { - event: WindowEvent::RedrawRequested, - .. - } => { + Event::RedrawRequested(_) => { let dt = last_frame.elapsed().as_millis(); let fps = 1.0 / ((dt as f32) / 1000.0); last_frame = std::time::Instant::now(); @@ -204,7 +201,7 @@ fn main() -> Result<(), Box> { &device, &mut encoder, &frame.view, - (size.width, size.height), + (size.width as f64, size.height as f64), ); glyph_brush.queue(Section { @@ -220,8 +217,8 @@ fn main() -> Result<(), Box> { &mut device, &mut encoder, &frame.view, - size.width.round() as u32, - size.height.round() as u32, + size.width, + size.height, ) .expect("Failed to draw queued text.");