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,
|
context: &Context,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (id, issue) = &self.issue;
|
let (id, issue) = self.issue.clone();
|
||||||
let list = widget::issue::list(context, theme, (*id, issue)).to_boxed();
|
let list = widget::issue::list(context, theme, (id, issue)).to_boxed();
|
||||||
let shortcuts = widget::common::shortcuts(
|
let shortcuts = widget::common::shortcuts(
|
||||||
theme,
|
theme,
|
||||||
vec![
|
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) {
|
pub fn format_patch_state(state: &PatchState) -> (String, Color) {
|
||||||
match state {
|
match state {
|
||||||
PatchState::Open { conflicts: _ } => (" ● ".into(), Color::Green),
|
PatchState::Open { conflicts: _ } => (" ● ".into(), Color::Green),
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,8 @@ pub struct ItemState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemState {
|
impl ItemState {
|
||||||
pub fn new(len: usize) -> Self {
|
pub fn new(selected: Option<usize>, len: usize) -> Self {
|
||||||
Self {
|
Self { selected, len }
|
||||||
selected: Some(0),
|
|
||||||
len,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn selected(&self) -> Option<usize> {
|
pub fn selected(&self) -> Option<usize> {
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ where
|
||||||
items: items.to_vec(),
|
items: items.to_vec(),
|
||||||
header,
|
header,
|
||||||
widths,
|
widths,
|
||||||
state: ItemState::new(items.len()),
|
state: ItemState::new(Some(0), items.len()),
|
||||||
theme,
|
theme,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -230,7 +230,7 @@ where
|
||||||
/// A list component that can display [`ListItem`]'s.
|
/// A list component that can display [`ListItem`]'s.
|
||||||
pub struct List<V>
|
pub struct List<V>
|
||||||
where
|
where
|
||||||
V: ListItem + Clone,
|
V: ListItem + Clone + PartialEq,
|
||||||
{
|
{
|
||||||
/// Items held by this list.
|
/// Items held by this list.
|
||||||
items: Vec<V>,
|
items: Vec<V>,
|
||||||
|
|
@ -242,12 +242,17 @@ where
|
||||||
|
|
||||||
impl<V> List<V>
|
impl<V> List<V>
|
||||||
where
|
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 {
|
Self {
|
||||||
items: items.to_vec(),
|
items: items.to_vec(),
|
||||||
state: ItemState::new(items.len()),
|
state: ItemState::new(selected, items.len()),
|
||||||
theme,
|
theme,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -255,7 +260,7 @@ where
|
||||||
|
|
||||||
impl<V> WidgetComponent for List<V>
|
impl<V> WidgetComponent for List<V>
|
||||||
where
|
where
|
||||||
V: ListItem + Clone,
|
V: ListItem + Clone + PartialEq,
|
||||||
{
|
{
|
||||||
fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) {
|
fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) {
|
||||||
use tuirealm::tui::widgets::{List, ListItem};
|
use tuirealm::tui::widgets::{List, ListItem};
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ pub struct LargeList {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 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
|
let mut items = issues
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -34,7 +34,10 @@ impl LargeList {
|
||||||
items.sort_by(|a, b| b.timestamp().cmp(a.timestamp()));
|
items.sort_by(|a, b| b.timestamp().cmp(a.timestamp()));
|
||||||
items.sort_by(|a, b| a.state().cmp(b.state()));
|
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);
|
.highlight(theme.colors.item_list_highlighted_bg);
|
||||||
|
|
||||||
let container = common::labeled_container(theme, "Issues", list.to_boxed());
|
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> {
|
pub fn list(context: &Context, theme: &Theme, issue: (IssueId, Issue)) -> Widget<LargeList> {
|
||||||
let list = LargeList::new(context, theme);
|
let list = LargeList::new(context, theme, Some(issue));
|
||||||
Widget::new(list)
|
Widget::new(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue