on master help sign in
ui: popup: scroll documentation popups with c-u/c-d.
8fe4590Blaž Hrastnik
helix-term/src/application.rs+6−1
@@ -60,7 +60,11 @@ impl Application {
60 let editor = &mut self.editor;
60 let editor = &mut self.editor;
61 let compositor = &mut self.compositor;
61 let compositor = &mut self.compositor;
62
62
63− let mut cx = crate::compositor::Context { editor, executor };
63+ let mut cx = crate::compositor::Context {
64+ editor,
65+ executor,
66+ scroll: None,
67+ };
64
68
65 compositor.render(&mut cx);
69 compositor.render(&mut cx);
66 }
70 }
@@ -91,6 +95,7 @@ impl Application {
91 let mut cx = crate::compositor::Context {
95 let mut cx = crate::compositor::Context {
92 editor: &mut self.editor,
96 editor: &mut self.editor,
93 executor: &self.executor,
97 executor: &self.executor,
98+ scroll: None,
94 };
99 };
95 // Handle key events
100 // Handle key events
96 let should_redraw = match event {
101 let should_redraw = match event {
helix-term/src/compositor.rs+1
@@ -29,6 +29,7 @@ use helix_view::{Editor, View};
29pub struct Context<'a> {
29pub struct Context<'a> {
30 pub editor: &'a mut Editor,
30 pub editor: &'a mut Editor,
31 pub executor: &'static smol::Executor<'static>,
31 pub executor: &'static smol::Executor<'static>,
32+ pub scroll: Option<usize>,
32}
33}
33
34
34pub trait Component {
35pub trait Component {
helix-term/src/ui/markdown.rs+3−2
@@ -112,8 +112,9 @@ impl Component for Markdown {
112
112
113 let contents = Text::from(lines);
113 let contents = Text::from(lines);
114
114
115− let par = Paragraph::new(contents).wrap(Wrap { trim: false });
115+ let par = Paragraph::new(contents)
116− // .scroll(x, y) offsets
116+ .wrap(Wrap { trim: false })
117+ .scroll((cx.scroll.unwrap_or_default() as u16, 0));
117
118
118 let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
119 let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
119 par.render(area, surface);
120 par.render(area, surface);
helix-term/src/ui/popup.rs+27
@@ -19,6 +19,7 @@ pub struct Popup {
19 contents: Box<dyn Component>,
19 contents: Box<dyn Component>,
20 position: Option<Position>,
20 position: Option<Position>,
21 size: (u16, u16),
21 size: (u16, u16),
22+ scroll: usize,
22}
23}
23
24
24impl Popup {
25impl Popup {
@@ -29,12 +30,21 @@ impl Popup {
29 contents,
30 contents,
30 position: None,
31 position: None,
31 size: (0, 0),
32 size: (0, 0),
33+ scroll: 0,
32 }
34 }
33 }
35 }
34
36
35 pub fn set_position(&mut self, pos: Option<Position>) {
37 pub fn set_position(&mut self, pos: Option<Position>) {
36 self.position = pos;
38 self.position = pos;
37 }
39 }
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+ }
38}
48}
39
49
40impl Component for Popup {
50impl Component for Popup {
@@ -64,6 +74,21 @@ impl Component for Popup {
64 code: KeyCode::Char('c'),
74 code: KeyCode::Char('c'),
65 modifiers: KeyModifiers::CONTROL,
75 modifiers: KeyModifiers::CONTROL,
66 } => close_fn,
76 } => 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+ }
67 _ => self.contents.handle_event(event, cx),
92 _ => self.contents.handle_event(event, cx),
68 }
93 }
69 // for some events, we want to process them but send ignore, specifically all input except
94 // for some events, we want to process them but send ignore, specifically all input except
@@ -85,6 +110,8 @@ impl Component for Popup {
85 use tui::text::Text;
110 use tui::text::Text;
86 use tui::widgets::{Paragraph, Widget, Wrap};
111 use tui::widgets::{Paragraph, Widget, Wrap};
87
112
113+ cx.scroll = Some(self.scroll);
114+
88 let position = self
115 let position = self
89 .position
116 .position
90 .or_else(|| cx.editor.cursor_position())
117 .or_else(|| cx.editor.cursor_position())