Use popup widget builder
This commit is contained in:
parent
4094ba3e3d
commit
6c637390e4
3 changed files with 44 additions and 58 deletions
|
|
@ -1,19 +1,16 @@
|
|||
use crossterm::style::{ContentStyle, Stylize};
|
||||
use toss::styled::Styled;
|
||||
|
||||
use crate::ui::widgets::background::Background;
|
||||
use crate::ui::widgets::border::Border;
|
||||
use crate::ui::widgets::float::Float;
|
||||
use crate::ui::widgets::layer::Layer;
|
||||
use crate::ui::widgets::padding::Padding;
|
||||
use crate::ui::widgets::popup::Popup;
|
||||
use crate::ui::widgets::text::Text;
|
||||
use crate::ui::widgets::BoxedWidget;
|
||||
|
||||
pub enum Popup {
|
||||
pub enum RoomPopup {
|
||||
ServerError { description: String, reason: String },
|
||||
}
|
||||
|
||||
impl Popup {
|
||||
impl RoomPopup {
|
||||
fn server_error_widget(description: &str, reason: &str) -> BoxedWidget {
|
||||
let border_style = ContentStyle::default().red().bold();
|
||||
let text = Styled::new_plain(description)
|
||||
|
|
@ -21,15 +18,10 @@ impl Popup {
|
|||
.then("Reason:", ContentStyle::default().bold())
|
||||
.then_plain(" ")
|
||||
.then_plain(reason);
|
||||
Layer::new(vec![
|
||||
Border::new(Background::new(Padding::new(Text::new(text)).horizontal(1)))
|
||||
.style(border_style)
|
||||
.into(),
|
||||
Float::new(Padding::new(Text::new(("Error", border_style))).horizontal(1))
|
||||
.horizontal(0.5)
|
||||
.into(),
|
||||
])
|
||||
.into()
|
||||
Popup::new(Text::new(text))
|
||||
.title(("Error", border_style))
|
||||
.border(border_style)
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn widget(&self) -> BoxedWidget {
|
||||
|
|
|
|||
|
|
@ -21,17 +21,17 @@ use crate::ui::widgets::background::Background;
|
|||
use crate::ui::widgets::border::Border;
|
||||
use crate::ui::widgets::editor::EditorState;
|
||||
use crate::ui::widgets::empty::Empty;
|
||||
use crate::ui::widgets::float::Float;
|
||||
use crate::ui::widgets::join::{HJoin, Segment, VJoin};
|
||||
use crate::ui::widgets::layer::Layer;
|
||||
use crate::ui::widgets::list::{List, ListState};
|
||||
use crate::ui::widgets::padding::Padding;
|
||||
use crate::ui::widgets::popup::Popup;
|
||||
use crate::ui::widgets::text::Text;
|
||||
use crate::ui::widgets::BoxedWidget;
|
||||
use crate::ui::{util, UiEvent};
|
||||
use crate::vault::EuphVault;
|
||||
|
||||
use super::popup::Popup;
|
||||
use super::popup::RoomPopup;
|
||||
|
||||
enum State {
|
||||
Normal,
|
||||
|
|
@ -45,7 +45,7 @@ pub struct EuphRoom {
|
|||
room: Option<euph::Room>,
|
||||
|
||||
state: State,
|
||||
popups: VecDeque<Popup>,
|
||||
popups: VecDeque<RoomPopup>,
|
||||
|
||||
chat: ChatState<euph::SmallMessage, EuphVault>,
|
||||
last_msg_sent: Option<oneshot::Receiver<Snowflake>>,
|
||||
|
|
@ -155,21 +155,7 @@ impl EuphRoom {
|
|||
|
||||
match &self.state {
|
||||
State::Normal => {}
|
||||
State::ChooseNick(ed) => layers.push(
|
||||
Float::new(Border::new(Background::new(VJoin::new(vec![
|
||||
Segment::new(Padding::new(Text::new("Choose nick")).horizontal(1)),
|
||||
Segment::new(
|
||||
Padding::new(
|
||||
ed.widget()
|
||||
.highlight(|s| Styled::new(s, euph::nick_style(s))),
|
||||
)
|
||||
.left(1),
|
||||
),
|
||||
]))))
|
||||
.horizontal(0.5)
|
||||
.vertical(0.5)
|
||||
.into(),
|
||||
),
|
||||
State::ChooseNick(editor) => layers.push(Self::choose_nick_widget(editor)),
|
||||
}
|
||||
|
||||
for popup in &self.popups {
|
||||
|
|
@ -179,6 +165,16 @@ impl EuphRoom {
|
|||
Layer::new(layers).into()
|
||||
}
|
||||
|
||||
fn choose_nick_widget(editor: &EditorState) -> BoxedWidget {
|
||||
let editor = editor
|
||||
.widget()
|
||||
.highlight(|s| Styled::new(s, euph::nick_style(s)));
|
||||
Popup::new(Padding::new(editor).left(1))
|
||||
.title("Choose nick")
|
||||
.inner_padding(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
async fn widget_without_nick_list(&self, status: &Option<Option<Status>>) -> BoxedWidget {
|
||||
VJoin::new(vec![
|
||||
Segment::new(Border::new(
|
||||
|
|
@ -470,7 +466,7 @@ impl EuphRoom {
|
|||
|
||||
pub fn handle_euph_room_event(&mut self, event: EuphRoomEvent) -> bool {
|
||||
match event {
|
||||
EuphRoomEvent::Connected | EuphRoomEvent::Disconnected => true,
|
||||
EuphRoomEvent::Connected | EuphRoomEvent::Disconnected | EuphRoomEvent::Stopped => true,
|
||||
EuphRoomEvent::Packet(packet) => match packet.content {
|
||||
Ok(data) => self.handle_euph_data(data),
|
||||
Err(reason) => self.handle_euph_error(packet.r#type, reason),
|
||||
|
|
@ -521,7 +517,7 @@ impl EuphRoom {
|
|||
_ => return false,
|
||||
};
|
||||
let description = format!("Failed to {action}.");
|
||||
self.popups.push_front(Popup::ServerError {
|
||||
self.popups.push_front(RoomPopup::ServerError {
|
||||
description,
|
||||
reason,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -16,14 +16,12 @@ use crate::vault::Vault;
|
|||
|
||||
use super::euph::room::EuphRoom;
|
||||
use super::input::{key, InputEvent, KeyBindingsList, KeyEvent};
|
||||
use super::widgets::background::Background;
|
||||
use super::widgets::border::Border;
|
||||
use super::widgets::editor::EditorState;
|
||||
use super::widgets::float::Float;
|
||||
use super::widgets::join::{HJoin, Segment, VJoin};
|
||||
use super::widgets::layer::Layer;
|
||||
use super::widgets::list::{List, ListState};
|
||||
use super::widgets::padding::Padding;
|
||||
use super::widgets::popup::Popup;
|
||||
use super::widgets::text::Text;
|
||||
use super::widgets::BoxedWidget;
|
||||
use super::{util, UiEvent};
|
||||
|
|
@ -104,29 +102,29 @@ impl Rooms {
|
|||
.widget()
|
||||
.await
|
||||
}
|
||||
State::Connect(ed) => {
|
||||
let room_style = ContentStyle::default().bold().blue();
|
||||
Layer::new(vec![
|
||||
self.rooms_widget().await,
|
||||
Float::new(Border::new(Background::new(VJoin::new(vec![
|
||||
Segment::new(Padding::new(Text::new("Connect to")).horizontal(1)),
|
||||
Segment::new(
|
||||
Padding::new(HJoin::new(vec![
|
||||
Segment::new(Text::new(("&", room_style))),
|
||||
Segment::new(ed.widget().highlight(|s| Styled::new(s, room_style))),
|
||||
]))
|
||||
.left(1),
|
||||
),
|
||||
]))))
|
||||
.horizontal(0.5)
|
||||
.vertical(0.5)
|
||||
.into(),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
State::Connect(editor) => Layer::new(vec![
|
||||
self.rooms_widget().await,
|
||||
Self::new_room_widget(editor),
|
||||
])
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn new_room_widget(editor: &EditorState) -> BoxedWidget {
|
||||
let room_style = ContentStyle::default().bold().blue();
|
||||
let editor = editor.widget().highlight(|s| Styled::new(s, room_style));
|
||||
Popup::new(
|
||||
Padding::new(HJoin::new(vec![
|
||||
Segment::new(Text::new(("&", room_style))),
|
||||
Segment::new(editor).priority(0),
|
||||
]))
|
||||
.left(1),
|
||||
)
|
||||
.title("Connect to")
|
||||
.inner_padding(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn format_pbln(joined: &Joined) -> String {
|
||||
let mut p = 0_usize;
|
||||
let mut b = 0_usize;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue