tui: Select correct list item on issue page
This commit is contained in:
parent
dd16ed780e
commit
c2b6fefb8e
|
|
@ -162,8 +162,8 @@ impl ViewPage for IssuePage {
|
|||
context: &Context,
|
||||
theme: &Theme,
|
||||
) -> Result<()> {
|
||||
let (id, issue) = &self.issue;
|
||||
let list = widget::issue::list(context, theme, (*id, issue)).to_boxed();
|
||||
let (id, issue) = self.issue.clone();
|
||||
let list = widget::issue::list(context, theme, (id, issue)).to_boxed();
|
||||
let shortcuts = widget::common::shortcuts(
|
||||
theme,
|
||||
vec![
|
||||
|
|
|
|||
|
|
@ -288,6 +288,12 @@ impl ListItem for IssueItem {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialEq for IssueItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_patch_state(state: &PatchState) -> (String, Color) {
|
||||
match state {
|
||||
PatchState::Open { conflicts: _ } => (" ● ".into(), Color::Green),
|
||||
|
|
|
|||
|
|
@ -25,11 +25,8 @@ pub struct ItemState {
|
|||
}
|
||||
|
||||
impl ItemState {
|
||||
pub fn new(len: usize) -> Self {
|
||||
Self {
|
||||
selected: Some(0),
|
||||
len,
|
||||
}
|
||||
pub fn new(selected: Option<usize>, len: usize) -> Self {
|
||||
Self { selected, len }
|
||||
}
|
||||
|
||||
pub fn selected(&self) -> Option<usize> {
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ where
|
|||
items: items.to_vec(),
|
||||
header,
|
||||
widths,
|
||||
state: ItemState::new(items.len()),
|
||||
state: ItemState::new(Some(0), items.len()),
|
||||
theme,
|
||||
}
|
||||
}
|
||||
|
|
@ -230,7 +230,7 @@ where
|
|||
/// A list component that can display [`ListItem`]'s.
|
||||
pub struct List<V>
|
||||
where
|
||||
V: ListItem + Clone,
|
||||
V: ListItem + Clone + PartialEq,
|
||||
{
|
||||
/// Items held by this list.
|
||||
items: Vec<V>,
|
||||
|
|
@ -242,12 +242,17 @@ where
|
|||
|
||||
impl<V> List<V>
|
||||
where
|
||||
V: ListItem + Clone,
|
||||
V: ListItem + Clone + PartialEq,
|
||||
{
|
||||
pub fn new(items: &[V], theme: Theme) -> Self {
|
||||
pub fn new(items: &[V], selected: Option<V>, theme: Theme) -> Self {
|
||||
let selected = match selected {
|
||||
Some(item) => items.iter().position(|i| i == &item),
|
||||
None => Some(0),
|
||||
};
|
||||
|
||||
Self {
|
||||
items: items.to_vec(),
|
||||
state: ItemState::new(items.len()),
|
||||
state: ItemState::new(selected, items.len()),
|
||||
theme,
|
||||
}
|
||||
}
|
||||
|
|
@ -255,7 +260,7 @@ where
|
|||
|
||||
impl<V> WidgetComponent for List<V>
|
||||
where
|
||||
V: ListItem + Clone,
|
||||
V: ListItem + Clone + PartialEq,
|
||||
{
|
||||
fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) {
|
||||
use tuirealm::tui::widgets::{List, ListItem};
|
||||
|
|
|
|||
|
|
@ -22,9 +22,9 @@ pub struct LargeList {
|
|||
}
|
||||
|
||||
impl LargeList {
|
||||
pub fn new(context: &Context, theme: &Theme) -> Self {
|
||||
pub fn new(context: &Context, theme: &Theme, selected: Option<(IssueId, Issue)>) -> Self {
|
||||
let repo = context.repository();
|
||||
let issues = cob::issue::all(repo).unwrap_or(vec![]);
|
||||
let issues = cob::issue::all(repo).unwrap_or_default();
|
||||
|
||||
let mut items = issues
|
||||
.iter()
|
||||
|
|
@ -34,7 +34,10 @@ impl LargeList {
|
|||
items.sort_by(|a, b| b.timestamp().cmp(a.timestamp()));
|
||||
items.sort_by(|a, b| a.state().cmp(b.state()));
|
||||
|
||||
let list = Widget::new(List::new(&items, theme.clone()))
|
||||
let selected =
|
||||
selected.map(|(id, issue)| IssueItem::from((context.profile(), repo, id, issue)));
|
||||
|
||||
let list = Widget::new(List::new(&items, selected, theme.clone()))
|
||||
.highlight(theme.colors.item_list_highlighted_bg);
|
||||
|
||||
let container = common::labeled_container(theme, "Issues", list.to_boxed());
|
||||
|
|
@ -57,8 +60,8 @@ impl WidgetComponent for LargeList {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn list(context: &Context, theme: &Theme, _issue: (IssueId, &Issue)) -> Widget<LargeList> {
|
||||
let list = LargeList::new(context, theme);
|
||||
pub fn list(context: &Context, theme: &Theme, issue: (IssueId, Issue)) -> Widget<LargeList> {
|
||||
let list = LargeList::new(context, theme, Some(issue));
|
||||
Widget::new(list)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue