From 1af4b263b2a9dd4b56dd4c033823cca9eb6779ed Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Sun, 9 Oct 2022 19:19:40 -0700 Subject: [PATCH 1/2] Implemented page up and page down for the editor --- crates/editor/src/editor.rs | 32 +++++++++++++++++++++++++++----- crates/editor/src/element.rs | 2 ++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 1abe65d48287c3cd5a16d9dce97aa23ff048a513..ee795bb0eb8667931bb2e09391c7ac32bc777e16 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -451,6 +451,7 @@ pub struct Editor { leader_replica_id: Option, hover_state: HoverState, link_go_to_definition_state: LinkGoToDefinitionState, + lines: Option, _subscriptions: Vec, } @@ -1052,6 +1053,7 @@ impl Editor { leader_replica_id: None, hover_state: Default::default(), link_go_to_definition_state: Default::default(), + lines: None, _subscriptions: vec![ cx.observe(&buffer, Self::on_buffer_changed), cx.subscribe(&buffer, Self::on_buffer_event), @@ -1163,9 +1165,9 @@ impl Editor { ) { let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - if scroll_position.y() == 0. { + if scroll_position.y() <= 0. { self.scroll_top_anchor = Anchor::min(); - self.scroll_position = scroll_position; + self.scroll_position = scroll_position.max(vec2f(0., 0.)); } else { let scroll_top_buffer_offset = DisplayPoint::new(scroll_position.y() as u32, 0).to_offset(&map, Bias::Right); @@ -1186,6 +1188,10 @@ impl Editor { cx.notify(); } + fn set_lines(&mut self, lines: f32) { + self.lines = Some(lines) + } + fn set_scroll_top_anchor( &mut self, anchor: Anchor, @@ -5514,12 +5520,28 @@ impl Editor { } } - pub fn page_up(&mut self, _: &PageUp, _: &mut ViewContext) { + pub fn page_up(&mut self, _: &PageUp, cx: &mut ViewContext) { log::info!("Editor::page_up"); + let lines = match self.lines { + Some(lines) => lines, + None => return, + }; + + let cur_position = self.scroll_position(cx); + let new_pos = cur_position - vec2f(0., lines + 1.); + self.set_scroll_position(new_pos, cx); } - pub fn page_down(&mut self, _: &PageDown, _: &mut ViewContext) { - log::info!("Editor::page_down"); + pub fn page_down(&mut self, _: &PageDown, cx: &mut ViewContext) { + log::info!("Editor::page_up"); + let lines = match self.lines { + Some(lines) => lines, + None => return, + }; + + let cur_position = self.scroll_position(cx); + let new_pos = cur_position + vec2f(0., lines - 1.); + self.set_scroll_position(new_pos, cx); } pub fn fold(&mut self, _: &Fold, cx: &mut ViewContext) { diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index acf2e5887ccae44c826921174a404736e398bfe3..d1ab64d7b7755bc07bbfa2e02cba7fe2bc2339e9 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1495,6 +1495,8 @@ impl Element for EditorElement { let mut highlighted_rows = None; let mut highlighted_ranges = Vec::new(); self.update_view(cx.app, |view, cx| { + view.set_lines(size.y() / line_height); + let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx)); highlighted_rows = view.highlighted_rows(); From 1d2495d57b12b1c508ea1b86349c19e37b38d704 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Mon, 10 Oct 2022 11:38:28 -0700 Subject: [PATCH 2/2] Re-arrange how lines are set --- crates/editor/src/editor.rs | 14 ++++++-------- crates/editor/src/element.rs | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index ee795bb0eb8667931bb2e09391c7ac32bc777e16..38ca82b0d2244ed41c288e43abc4b67875107c2d 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -451,7 +451,7 @@ pub struct Editor { leader_replica_id: Option, hover_state: HoverState, link_go_to_definition_state: LinkGoToDefinitionState, - lines: Option, + visible_line_count: Option, _subscriptions: Vec, } @@ -1053,7 +1053,7 @@ impl Editor { leader_replica_id: None, hover_state: Default::default(), link_go_to_definition_state: Default::default(), - lines: None, + visible_line_count: None, _subscriptions: vec![ cx.observe(&buffer, Self::on_buffer_changed), cx.subscribe(&buffer, Self::on_buffer_event), @@ -1188,8 +1188,8 @@ impl Editor { cx.notify(); } - fn set_lines(&mut self, lines: f32) { - self.lines = Some(lines) + fn set_visible_line_count(&mut self, lines: f32) { + self.visible_line_count = Some(lines) } fn set_scroll_top_anchor( @@ -5521,8 +5521,7 @@ impl Editor { } pub fn page_up(&mut self, _: &PageUp, cx: &mut ViewContext) { - log::info!("Editor::page_up"); - let lines = match self.lines { + let lines = match self.visible_line_count { Some(lines) => lines, None => return, }; @@ -5533,8 +5532,7 @@ impl Editor { } pub fn page_down(&mut self, _: &PageDown, cx: &mut ViewContext) { - log::info!("Editor::page_up"); - let lines = match self.lines { + let lines = match self.visible_line_count { Some(lines) => lines, None => return, }; diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index d1ab64d7b7755bc07bbfa2e02cba7fe2bc2339e9..587133e9dd703b04beef2d8f51e602cfd609f446 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1422,6 +1422,8 @@ impl Element for EditorElement { let em_advance = style.text.em_advance(cx.font_cache); let overscroll = vec2f(em_width, 0.); let snapshot = self.update_view(cx.app, |view, cx| { + view.set_visible_line_count(size.y() / line_height); + let wrap_width = match view.soft_wrap_mode(cx) { SoftWrap::None => Some((MAX_LINE_LEN / 2) as f32 * em_advance), SoftWrap::EditorWidth => { @@ -1495,8 +1497,6 @@ impl Element for EditorElement { let mut highlighted_rows = None; let mut highlighted_ranges = Vec::new(); self.update_view(cx.app, |view, cx| { - view.set_lines(size.y() / line_height); - let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx)); highlighted_rows = view.highlighted_rows();