on master help sign in
ui: popup: scroll documentation popups with c-u/c-d.
commit8fe459066605a794f8dceedaf872f71ffd4c750a authorBlaž Hrastnik <blaz@mxxn.io> dateMon 8 Mar 2021, 17:32 (6 years ago) parentddcf515
4 files changed+37−3
helix-term/src/application.rs+6−1 helix-term/src/compositor.rs+1 helix-term/src/ui/markdown.rs+3−2 helix-term/src/ui/popup.rs+27
helix-term/src/application.rs+6−1
@@ -60,7 +60,11 @@ impl Application {
6060 let editor = &mut self.editor;
6161 let compositor = &mut self.compositor;
6262
63− let mut cx = crate::compositor::Context { editor, executor };
63+ let mut cx = crate::compositor::Context {
64+ editor,
65+ executor,
66+ scroll: None,
67+ };
6468
6569 compositor.render(&mut cx);
6670 }
@@ -91,6 +95,7 @@ impl Application {
9195 let mut cx = crate::compositor::Context {
9296 editor: &mut self.editor,
9397 executor: &self.executor,
98+ scroll: None,
9499 };
95100 // Handle key events
96101 let should_redraw = match event {
helix-term/src/compositor.rs+1
@@ -29,6 +29,7 @@ use helix_view::{Editor, View};
2929pub struct Context<'a> {
3030 pub editor: &'a mut Editor,
3131 pub executor: &'static smol::Executor<'static>,
32+ pub scroll: Option<usize>,
3233}
3334
3435pub trait Component {
helix-term/src/ui/markdown.rs+3−2
@@ -112,8 +112,9 @@ impl Component for Markdown {
112112
113113 let contents = Text::from(lines);
114114
115− let par = Paragraph::new(contents).wrap(Wrap { trim: false });
116− // .scroll(x, y) offsets
115+ let par = Paragraph::new(contents)
116+ .wrap(Wrap { trim: false })
117+ .scroll((cx.scroll.unwrap_or_default() as u16, 0));
117118
118119 let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
119120 par.render(area, surface);
helix-term/src/ui/popup.rs+27
@@ -19,6 +19,7 @@ pub struct Popup {
1919 contents: Box<dyn Component>,
2020 position: Option<Position>,
2121 size: (u16, u16),
22+ scroll: usize,
2223}
2324
2425impl Popup {
@@ -29,12 +30,21 @@ impl Popup {
2930 contents,
3031 position: None,
3132 size: (0, 0),
33+ scroll: 0,
3234 }
3335 }
3436
3537 pub fn set_position(&mut self, pos: Option<Position>) {
3638 self.position = pos;
3739 }
40+
41+ pub fn scroll(&mut self, offset: usize, direction: bool) {
42+ if direction {
43+ self.scroll += offset;
44+ } else {
45+ self.scroll = self.scroll.saturating_sub(offset);
46+ }
47+ }
3848}
3949
4050impl Component for Popup {
@@ -64,6 +74,21 @@ impl Component for Popup {
6474 code: KeyCode::Char('c'),
6575 modifiers: KeyModifiers::CONTROL,
6676 } => close_fn,
77+
78+ KeyEvent {
79+ code: KeyCode::Char('d'),
80+ modifiers: KeyModifiers::CONTROL,
81+ } => {
82+ self.scroll(self.size.1 as usize / 2, true);
83+ return EventResult::Consumed(None);
84+ }
85+ KeyEvent {
86+ code: KeyCode::Char('u'),
87+ modifiers: KeyModifiers::CONTROL,
88+ } => {
89+ self.scroll(self.size.1 as usize / 2, false);
90+ return EventResult::Consumed(None);
91+ }
6792 _ => self.contents.handle_event(event, cx),
6893 }
6994 // for some events, we want to process them but send ignore, specifically all input except
@@ -85,6 +110,8 @@ impl Component for Popup {
85110 use tui::text::Text;
86111 use tui::widgets::{Paragraph, Widget, Wrap};
87112
113+ cx.scroll = Some(self.scroll);
114+
88115 let position = self
89116 .position
90117 .or_else(|| cx.editor.cursor_position())