Migrate nick popup to AsyncWidget

This commit is contained in:
Joscha 2023-04-13 01:43:12 +02:00
parent c7cbd9856b
commit e358e2184e
2 changed files with 17 additions and 23 deletions

View file

@ -1,26 +1,22 @@
use euphoxide::conn::Joined;
use toss::{Style, Terminal};
use toss::widgets::{BoxedAsync, EditorState};
use toss::{Style, Terminal, WidgetExt};
use crate::euph::{self, Room};
use crate::ui::input::{key, InputEvent, KeyBindingsList};
use crate::ui::util;
use crate::ui::widgets::editor::EditorState;
use crate::ui::widgets::padding::Padding;
use crate::ui::widgets::popup::Popup;
use crate::ui::widgets::BoxedWidget;
use crate::ui::widgets2::Popup;
use crate::ui::{util2, UiError};
pub fn new(joined: Joined) -> EditorState {
EditorState::with_initial_text(joined.session.name)
}
pub fn widget(editor: &EditorState) -> BoxedWidget {
let editor = editor
pub fn widget(editor: &mut EditorState) -> BoxedAsync<'_, UiError> {
let inner = editor
.widget()
.highlight(|s| euph::style_nick_exact(s, Style::new()));
Popup::new(Padding::new(editor).left(1))
.title("Choose nick")
.inner_padding(false)
.build()
.with_highlight(|s| euph::style_nick_exact(s, Style::new()));
Popup::new(inner, "Choose nick").boxed_async()
}
fn nick_char(c: char) -> bool {
@ -30,7 +26,7 @@ fn nick_char(c: char) -> bool {
pub fn list_key_bindings(bindings: &mut KeyBindingsList) {
bindings.binding("esc", "abort");
bindings.binding("enter", "set nick");
util::list_editor_key_bindings(bindings, nick_char);
util2::list_editor_key_bindings(bindings, nick_char);
}
pub enum EventResult {
@ -43,18 +39,18 @@ pub fn handle_input_event(
terminal: &mut Terminal,
event: &InputEvent,
room: &Option<Room>,
editor: &EditorState,
editor: &mut EditorState,
) -> EventResult {
match event {
key!(Esc) => EventResult::ResetState,
key!(Enter) => {
if let Some(room) = &room {
let _ = room.nick(editor.text());
let _ = room.nick(editor.text().to_string());
}
EventResult::ResetState
}
_ => {
if util::handle_editor_input_event(editor, terminal, event, nick_char) {
if util2::handle_editor_input_event(editor, terminal, event, nick_char) {
EventResult::Handled
} else {
EventResult::NotHandled

View file

@ -8,7 +8,7 @@ use euphoxide::conn::{self, Joined, Joining, SessionInfo};
use parking_lot::FairMutex;
use tokio::sync::oneshot::error::TryRecvError;
use tokio::sync::{mpsc, oneshot};
use toss::widgets::{BoxedAsync, Join2, Layer, Text};
use toss::widgets::{BoxedAsync, EditorState, Join2, Layer, Text};
use toss::{AsyncWidget, Style, Styled, Terminal, WidgetExt};
use crate::config;
@ -37,7 +37,7 @@ enum Focus {
enum State {
Normal,
Auth(OldEditorState),
Nick(OldEditorState),
Nick(EditorState),
Account(AccountUiState),
Links(LinksState),
InspectMessage(Message),
@ -223,14 +223,12 @@ impl EuphRoom {
let mut layers = vec![chat];
match &self.state {
match &mut self.state {
State::Normal => {}
State::Auth(editor) => {
layers.push(WidgetWrapper::new(auth::widget(editor)).boxed_async())
}
State::Nick(editor) => {
layers.push(WidgetWrapper::new(nick::widget(editor)).boxed_async())
}
State::Nick(editor) => layers.push(nick::widget(editor)),
State::Account(account) => {
layers.push(WidgetWrapper::new(account.widget()).boxed_async())
}