Migrate links popup to AsyncWidget
This commit is contained in:
parent
f69d88bf4a
commit
31c8453a83
2 changed files with 27 additions and 23 deletions
|
|
@ -2,13 +2,12 @@ use std::io;
|
||||||
|
|
||||||
use crossterm::style::Stylize;
|
use crossterm::style::Stylize;
|
||||||
use linkify::{LinkFinder, LinkKind};
|
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::input::{key, InputEvent, KeyBindingsList};
|
||||||
use crate::ui::widgets::list::ListState;
|
use crate::ui::widgets2::{ListState, Popup};
|
||||||
use crate::ui::widgets::popup::Popup;
|
use crate::ui::UiError;
|
||||||
use crate::ui::widgets::text::Text;
|
|
||||||
use crate::ui::widgets::BoxedWidget;
|
|
||||||
|
|
||||||
pub struct LinksState {
|
pub struct LinksState {
|
||||||
links: Vec<String>,
|
links: Vec<String>,
|
||||||
|
|
@ -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 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() {
|
if self.links.is_empty() {
|
||||||
list.add_unsel(Text::new(("No links found", Style::new().grey().italic())))
|
list.add_unsel(Text::new(("No links found", Style::new().grey().italic())))
|
||||||
}
|
}
|
||||||
|
|
||||||
for (id, link) in self.links.iter().enumerate() {
|
for (id, link) in self.links.iter().enumerate() {
|
||||||
let (line_normal, line_selected) = if let Some(number_key) = NUMBER_KEYS.get(id) {
|
#[allow(clippy::collapsible_else_if)]
|
||||||
(
|
let text = if list.state().selected() == Some(&id) {
|
||||||
Styled::new(format!("[{number_key}]"), Style::new().dark_grey().bold())
|
if let Some(number_key) = NUMBER_KEYS.get(id) {
|
||||||
.then_plain(" ")
|
|
||||||
.then_plain(link),
|
|
||||||
Styled::new(format!("[{number_key}]"), style_selected.bold())
|
Styled::new(format!("[{number_key}]"), style_selected.bold())
|
||||||
.then(" ", style_selected)
|
.then(" ", style_selected)
|
||||||
.then(link, style_selected),
|
.then(link, style_selected)
|
||||||
)
|
} else {
|
||||||
|
Styled::new(format!(" {link}"), style_selected)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
(
|
if let Some(number_key) = NUMBER_KEYS.get(id) {
|
||||||
Styled::new_plain(format!(" {link}")),
|
Styled::new(format!("[{number_key}]"), Style::new().dark_grey().bold())
|
||||||
Styled::new(format!(" {link}"), style_selected),
|
.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 {
|
fn open_link_by_id(&self, id: usize) -> EventResult {
|
||||||
|
|
@ -87,8 +91,8 @@ impl LinksState {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_link(&self) -> EventResult {
|
fn open_link(&self) -> EventResult {
|
||||||
if let Some(id) = self.list.cursor() {
|
if let Some(id) = self.list.selected() {
|
||||||
self.open_link_by_id(id)
|
self.open_link_by_id(*id)
|
||||||
} else {
|
} else {
|
||||||
EventResult::Handled
|
EventResult::Handled
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -227,7 +227,7 @@ impl EuphRoom {
|
||||||
State::Auth(editor) => layers.push(auth::widget(editor)),
|
State::Auth(editor) => layers.push(auth::widget(editor)),
|
||||||
State::Nick(editor) => layers.push(nick::widget(editor)),
|
State::Nick(editor) => layers.push(nick::widget(editor)),
|
||||||
State::Account(account) => layers.push(account.widget()),
|
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::InspectMessage(message) => layers.push(inspect::message_widget(message)),
|
||||||
State::InspectSession(session) => layers.push(inspect::session_widget(session)),
|
State::InspectSession(session) => layers.push(inspect::session_widget(session)),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue