Skip to content

Commit 6660906

Browse files
authored
Rethinking transitions between views (#130)
* Fix user command view not to retain previous view * Fix detail view to close with Confirm action * Open detail view via Confirm action in user command view
1 parent 2fc92cd commit 6660906

5 files changed

Lines changed: 47 additions & 128 deletions

File tree

src/app.rs

Lines changed: 38 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,27 @@ impl App<'_> {
349349
}
350350

351351
fn open_detail(&mut self) {
352-
if let View::List(ref mut view) = self.view {
353-
let commit_list_state = view.take_list_state();
354-
let selected = commit_list_state.selected_commit_hash().clone();
355-
let (commit, changes) = self.repository.commit_detail(&selected);
356-
let refs = self
357-
.repository
358-
.refs(&selected)
359-
.into_iter()
360-
.cloned()
361-
.collect();
362-
self.view = View::of_detail(
363-
commit_list_state,
364-
commit,
365-
changes,
366-
refs,
367-
self.ctx.clone(),
368-
self.tx.clone(),
369-
);
370-
}
352+
let commit_list_state = match self.view {
353+
View::List(ref mut view) => view.take_list_state(),
354+
View::UserCommand(ref mut view) => view.take_list_state(),
355+
_ => return,
356+
};
357+
let selected = commit_list_state.selected_commit_hash().clone();
358+
let (commit, changes) = self.repository.commit_detail(&selected);
359+
let refs = self
360+
.repository
361+
.refs(&selected)
362+
.into_iter()
363+
.cloned()
364+
.collect();
365+
self.view = View::of_detail(
366+
commit_list_state,
367+
commit,
368+
changes,
369+
refs,
370+
self.ctx.clone(),
371+
self.tx.clone(),
372+
);
371373
}
372374

373375
fn close_detail(&mut self) {
@@ -384,79 +386,28 @@ impl App<'_> {
384386
}
385387

386388
fn open_user_command(&mut self, user_command_number: usize) {
387-
if let View::List(ref mut view) = self.view {
388-
let commit_list_state = view.take_list_state();
389-
let selected = commit_list_state.selected_commit_hash().clone();
390-
let (commit, _) = self.repository.commit_detail(&selected);
391-
self.view = View::of_user_command_from_list(
392-
commit_list_state,
393-
commit,
394-
user_command_number,
395-
self.app_status.view_area,
396-
self.ctx.clone(),
397-
self.tx.clone(),
398-
);
399-
} else if let View::Detail(ref mut view) = self.view {
400-
let commit_list_state = view.take_list_state();
401-
let selected = commit_list_state.selected_commit_hash().clone();
402-
let (commit, _) = self.repository.commit_detail(&selected);
403-
self.view = View::of_user_command_from_detail(
404-
commit_list_state,
405-
commit,
406-
user_command_number,
407-
self.app_status.view_area,
408-
self.ctx.clone(),
409-
self.tx.clone(),
410-
);
411-
} else if let View::UserCommand(ref mut view) = self.view {
412-
let commit_list_state = view.take_list_state();
413-
let selected = commit_list_state.selected_commit_hash().clone();
414-
let (commit, _) = self.repository.commit_detail(&selected);
415-
if view.before_view_is_list() {
416-
self.view = View::of_user_command_from_list(
417-
commit_list_state,
418-
commit,
419-
user_command_number,
420-
self.app_status.view_area,
421-
self.ctx.clone(),
422-
self.tx.clone(),
423-
);
424-
} else {
425-
self.view = View::of_user_command_from_detail(
426-
commit_list_state,
427-
commit,
428-
user_command_number,
429-
self.app_status.view_area,
430-
self.ctx.clone(),
431-
self.tx.clone(),
432-
);
433-
}
434-
}
389+
let commit_list_state = match self.view {
390+
View::List(ref mut view) => view.take_list_state(),
391+
View::Detail(ref mut view) => view.take_list_state(),
392+
View::UserCommand(ref mut view) => view.take_list_state(),
393+
_ => return,
394+
};
395+
let selected = commit_list_state.selected_commit_hash().clone();
396+
let (commit, _) = self.repository.commit_detail(&selected);
397+
self.view = View::of_user_command(
398+
commit_list_state,
399+
commit,
400+
user_command_number,
401+
self.app_status.view_area,
402+
self.ctx.clone(),
403+
self.tx.clone(),
404+
);
435405
}
436406

437407
fn close_user_command(&mut self) {
438408
if let View::UserCommand(ref mut view) = self.view {
439409
let commit_list_state = view.take_list_state();
440-
let selected = commit_list_state.selected_commit_hash().clone();
441-
let (commit, changes) = self.repository.commit_detail(&selected);
442-
let refs = self
443-
.repository
444-
.refs(&selected)
445-
.into_iter()
446-
.cloned()
447-
.collect();
448-
if view.before_view_is_list() {
449-
self.view = View::of_list(commit_list_state, self.ctx.clone(), self.tx.clone());
450-
} else {
451-
self.view = View::of_detail(
452-
commit_list_state,
453-
commit,
454-
changes,
455-
refs,
456-
self.ctx.clone(),
457-
self.tx.clone(),
458-
);
459-
}
410+
self.view = View::of_list(commit_list_state, self.ctx.clone(), self.tx.clone());
460411
}
461412
}
462413

src/view/detail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a> DetailView<'a> {
114114
UserEvent::HelpToggle => {
115115
self.tx.send(AppEvent::OpenHelp);
116116
}
117-
UserEvent::Cancel | UserEvent::Close => {
117+
UserEvent::Confirm | UserEvent::Cancel | UserEvent::Close => {
118118
self.tx.send(AppEvent::ClearDetail); // hack: reset the rendering of the image area
119119
self.tx.send(AppEvent::CloseDetail);
120120
}

src/view/help.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ fn build_lines(
275275
let (list_key_lines, list_value_lines) = build_block_lines("Commit List:", list_helps, color_theme, keybind);
276276

277277
let mut detail_helps = vec![
278-
(vec![UserEvent::Cancel, UserEvent::Close], "Close commit details".into()),
278+
(vec![UserEvent::Cancel, UserEvent::Close, UserEvent::Confirm], "Close commit details".into()),
279279
(vec![UserEvent::NavigateDown], "Scroll down".into()),
280280
(vec![UserEvent::NavigateUp], "Scroll up".into()),
281281
(vec![UserEvent::PageDown], "Scroll page down".into()),
@@ -318,6 +318,7 @@ fn build_lines(
318318
(vec![UserEvent::SelectDown], "Select older commit".into()),
319319
(vec![UserEvent::SelectUp], "Select newer commit".into()),
320320
(vec![UserEvent::GoToParent], "Select parent commit".into()),
321+
(vec![UserEvent::Confirm], "Show commit details".into()),
321322
];
322323
user_command_helps.extend(user_command_view_toggle_helps);
323324
let (user_command_key_lines, user_command_value_lines) = build_block_lines("User Command:", user_command_helps, color_theme, keybind);

src/view/user_command.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ use crate::{
2020
},
2121
};
2222

23-
#[derive(Debug)]
24-
pub enum UserCommandViewBeforeView {
25-
List,
26-
Detail,
27-
}
28-
2923
#[derive(Debug)]
3024
pub struct UserCommandView<'a> {
3125
commit_list_state: Option<CommitListState<'a>>,
@@ -36,7 +30,6 @@ pub struct UserCommandView<'a> {
3630

3731
ctx: Rc<AppContext>,
3832
tx: Sender,
39-
before_view: UserCommandViewBeforeView,
4033
clear: bool,
4134
}
4235

@@ -48,7 +41,6 @@ impl<'a> UserCommandView<'a> {
4841
view_area: Rect,
4942
ctx: Rc<AppContext>,
5043
tx: Sender,
51-
before_view: UserCommandViewBeforeView,
5244
) -> UserCommandView<'a> {
5345
let user_command_output_lines =
5446
build_user_command_output_lines(&commit, user_command_number, view_area, ctx.clone())
@@ -64,7 +56,6 @@ impl<'a> UserCommandView<'a> {
6456
user_command_output_lines,
6557
ctx,
6658
tx,
67-
before_view,
6859
clear: false,
6960
}
7061
}
@@ -130,6 +121,9 @@ impl<'a> UserCommandView<'a> {
130121
self.tx.send(AppEvent::OpenUserCommand(n));
131122
}
132123
}
124+
UserEvent::Confirm => {
125+
self.tx.send(AppEvent::OpenDetail);
126+
}
133127
UserEvent::Cancel | UserEvent::Close => {
134128
self.close();
135129
}
@@ -217,10 +211,6 @@ impl<'a> UserCommandView<'a> {
217211
self.clear = true;
218212
}
219213

220-
pub fn before_view_is_list(&self) -> bool {
221-
matches!(self.before_view, UserCommandViewBeforeView::List)
222-
}
223-
224214
fn close(&self) {
225215
self.tx.send(AppEvent::ClearUserCommand); // hack: reset the rendering of the image area
226216
self.tx.send(AppEvent::CloseUserCommand);

src/view/views.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ use crate::{
77
event::{Sender, UserEventWithCount},
88
git::{Commit, FileChange, Ref},
99
view::{
10-
detail::DetailView,
11-
help::HelpView,
12-
list::ListView,
13-
refs::RefsView,
14-
user_command::{UserCommandView, UserCommandViewBeforeView},
10+
detail::DetailView, help::HelpView, list::ListView, refs::RefsView,
11+
user_command::UserCommandView,
1512
},
1613
widget::commit_list::CommitListState,
1714
};
@@ -76,7 +73,7 @@ impl<'a> View<'a> {
7673
)))
7774
}
7875

79-
pub fn of_user_command_from_list(
76+
pub fn of_user_command(
8077
commit_list_state: CommitListState<'a>,
8178
commit: Commit,
8279
user_command_number: usize,
@@ -91,26 +88,6 @@ impl<'a> View<'a> {
9188
view_area,
9289
ctx,
9390
tx,
94-
UserCommandViewBeforeView::List,
95-
)))
96-
}
97-
98-
pub fn of_user_command_from_detail(
99-
commit_list_state: CommitListState<'a>,
100-
commit: Commit,
101-
user_command_number: usize,
102-
view_area: Rect,
103-
ctx: Rc<AppContext>,
104-
tx: Sender,
105-
) -> Self {
106-
View::UserCommand(Box::new(UserCommandView::new(
107-
commit_list_state,
108-
commit,
109-
user_command_number,
110-
view_area,
111-
ctx,
112-
tx,
113-
UserCommandViewBeforeView::Detail,
11491
)))
11592
}
11693

0 commit comments

Comments
 (0)