allow scrolling syntax text (see #725)

This commit is contained in:
Stephan Dilly 2021-05-23 12:37:32 +02:00
parent eab8fc202b
commit 6decd7a638
2 changed files with 48 additions and 19 deletions

View file

@ -31,6 +31,11 @@ const FOLDER_ICON_COLLAPSED: &str = "\u{25b8}"; //▸
const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾ const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾
const EMPTY_STR: &str = ""; const EMPTY_STR: &str = "";
enum Focus {
Tree,
File,
}
pub struct RevisionFilesComponent { pub struct RevisionFilesComponent {
queue: Queue, queue: Queue,
title: String, title: String,
@ -43,6 +48,7 @@ pub struct RevisionFilesComponent {
scroll_top: Cell<usize>, scroll_top: Cell<usize>,
revision: Option<CommitId>, revision: Option<CommitId>,
visible: bool, visible: bool,
focus: Focus,
key_config: SharedKeyConfig, key_config: SharedKeyConfig,
} }
@ -67,6 +73,7 @@ impl RevisionFilesComponent {
files: Vec::new(), files: Vec::new(),
revision: None, revision: None,
visible: false, visible: false,
focus: Focus::Tree,
key_config, key_config,
} }
} }
@ -276,18 +283,26 @@ impl Component for RevisionFilesComponent {
) -> Result<EventState> { ) -> Result<EventState> {
if self.is_visible() { if self.is_visible() {
if let Event::Key(key) = event { if let Event::Key(key) = event {
let is_tree_focused =
matches!(self.focus, Focus::Tree);
if key == self.key_config.exit_popup { if key == self.key_config.exit_popup {
self.hide(); self.hide();
} else if is_tree_focused
&& tree_nav(&mut self.tree, &self.key_config, key)
{
self.selection_changed();
} else if key == self.key_config.blame { } else if key == self.key_config.blame {
if self.blame() { if self.blame() {
self.hide(); self.hide();
} }
} else if tree_nav( } else if key == self.key_config.move_right {
&mut self.tree, if is_tree_focused {
&self.key_config, self.focus = Focus::File;
key, } else {
) { self.focus = Focus::Tree;
self.selection_changed(); }
} else if !is_tree_focused {
self.current_file.event(event)?;
} }
} }

View file

@ -13,8 +13,9 @@ use asyncgit::{
AsyncNotification, CWD, AsyncNotification, CWD,
}; };
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
use crossterm::event::Event;
use itertools::Either; use itertools::Either;
use std::{convert::From, path::Path}; use std::{cell::Cell, convert::From, path::Path};
use tui::{ use tui::{
backend::Backend, backend::Backend,
layout::Rect, layout::Rect,
@ -27,7 +28,8 @@ pub struct SyntaxTextComponent {
current_file: Option<(String, Either<ui::SyntaxText, String>)>, current_file: Option<(String, Either<ui::SyntaxText, String>)>,
async_highlighting: async_highlighting:
AsyncSingleJob<AsyncSyntaxJob, AsyncNotification>, AsyncSingleJob<AsyncSyntaxJob, AsyncNotification>,
_key_config: SharedKeyConfig, key_config: SharedKeyConfig,
scroll_top: Cell<u16>,
} }
impl SyntaxTextComponent { impl SyntaxTextComponent {
@ -42,7 +44,8 @@ impl SyntaxTextComponent {
AsyncNotification::SyntaxHighlighting, AsyncNotification::SyntaxHighlighting,
), ),
current_file: None, current_file: None,
_key_config: key_config, scroll_top: Cell::new(0),
key_config,
} }
} }
@ -115,14 +118,16 @@ impl DrawableComponent for SyntaxTextComponent {
f: &mut Frame<B>, f: &mut Frame<B>,
area: Rect, area: Rect,
) -> Result<()> { ) -> Result<()> {
let content = let text = self.current_file.as_ref().map_or_else(
Paragraph::new(self.current_file.as_ref().map_or_else( || Text::from(""),
|| Text::from(""), |(_, content)| match content {
|(_, content)| match content { Either::Left(syn) => syn.into(),
Either::Left(syn) => syn.into(), Either::Right(s) => Text::from(s.as_str()),
Either::Right(s) => Text::from(s.as_str()), },
}, );
))
let content = Paragraph::new(text)
.scroll((self.scroll_top.get(), 0))
.wrap(Wrap { trim: false }); .wrap(Wrap { trim: false });
f.render_widget(content, area); f.render_widget(content, area);
@ -142,9 +147,18 @@ impl Component for SyntaxTextComponent {
fn event( fn event(
&mut self, &mut self,
_event: crossterm::event::Event, event: crossterm::event::Event,
) -> Result<EventState> { ) -> Result<EventState> {
//TODO: scrolling if let Event::Key(key) = event {
if key == self.key_config.move_down {
self.scroll_top
.set(self.scroll_top.get().saturating_add(1));
} else if key == self.key_config.move_up {
self.scroll_top
.set(self.scroll_top.get().saturating_sub(1));
}
}
Ok(EventState::NotConsumed) Ok(EventState::NotConsumed)
} }
} }