diff --git a/src/ui/euph/links.rs b/src/ui/euph/links.rs index 9ccdc51..22bc67d 100644 --- a/src/ui/euph/links.rs +++ b/src/ui/euph/links.rs @@ -2,13 +2,12 @@ use std::io; use crossterm::style::Stylize; use linkify::{LinkFinder, LinkKind}; -use toss::{Style, Styled}; +use toss::widgets::{BoxedAsync, Text}; +use toss::{Style, Styled, WidgetExt}; use crate::ui::input::{key, InputEvent, KeyBindingsList}; -use crate::ui::widgets::list::ListState; -use crate::ui::widgets::popup::Popup; -use crate::ui::widgets::text::Text; -use crate::ui::widgets::BoxedWidget; +use crate::ui::widgets2::{ListState, Popup}; +use crate::ui::UiError; pub struct LinksState { links: Vec, @@ -39,34 +38,39 @@ impl LinksState { } } - pub fn widget(&self) -> BoxedWidget { + pub fn widget(&mut self) -> BoxedAsync<'_, UiError> { let style_selected = Style::new().black().on_white(); - let mut list = self.list.widget().focus(true); + let mut list = self.list.widget(); + if self.links.is_empty() { list.add_unsel(Text::new(("No links found", Style::new().grey().italic()))) } + for (id, link) in self.links.iter().enumerate() { - let (line_normal, line_selected) = if let Some(number_key) = NUMBER_KEYS.get(id) { - ( - Styled::new(format!("[{number_key}]"), Style::new().dark_grey().bold()) - .then_plain(" ") - .then_plain(link), + #[allow(clippy::collapsible_else_if)] + let text = if list.state().selected() == Some(&id) { + if let Some(number_key) = NUMBER_KEYS.get(id) { Styled::new(format!("[{number_key}]"), style_selected.bold()) .then(" ", style_selected) - .then(link, style_selected), - ) + .then(link, style_selected) + } else { + Styled::new(format!(" {link}"), style_selected) + } } else { - ( - Styled::new_plain(format!(" {link}")), - Styled::new(format!(" {link}"), style_selected), - ) + if let Some(number_key) = NUMBER_KEYS.get(id) { + Styled::new(format!("[{number_key}]"), Style::new().dark_grey().bold()) + .then_plain(" ") + .then_plain(link) + } else { + Styled::new_plain(format!(" {link}")) + } }; - list.add_sel(id, Text::new(line_normal), Text::new(line_selected)); + list.add_sel(id, Text::new(text)); } - Popup::new(list).title("Links").build() + Popup::new(list, "Links").boxed_async() } fn open_link_by_id(&self, id: usize) -> EventResult { @@ -87,8 +91,8 @@ impl LinksState { } fn open_link(&self) -> EventResult { - if let Some(id) = self.list.cursor() { - self.open_link_by_id(id) + if let Some(id) = self.list.selected() { + self.open_link_by_id(*id) } else { EventResult::Handled } diff --git a/src/ui/euph/room.rs b/src/ui/euph/room.rs index c611362..17462fa 100644 --- a/src/ui/euph/room.rs +++ b/src/ui/euph/room.rs @@ -227,7 +227,7 @@ impl EuphRoom { State::Auth(editor) => layers.push(auth::widget(editor)), State::Nick(editor) => layers.push(nick::widget(editor)), State::Account(account) => layers.push(account.widget()), - State::Links(links) => layers.push(WidgetWrapper::new(links.widget()).boxed_async()), + State::Links(links) => layers.push(links.widget()), State::InspectMessage(message) => layers.push(inspect::message_widget(message)), State::InspectSession(session) => layers.push(inspect::session_widget(session)), }