Migrate auth popup to AsyncWidget

This commit is contained in:
Joscha 2023-04-13 01:51:52 +02:00
parent e358e2184e
commit 03766802fd
2 changed files with 16 additions and 18 deletions

View file

@ -1,26 +1,27 @@
use toss::Terminal; use toss::widgets::{BoxedAsync, EditorState};
use toss::{Terminal, WidgetExt};
use crate::euph::Room; use crate::euph::Room;
use crate::ui::input::{key, InputEvent, KeyBindingsList}; use crate::ui::input::{key, InputEvent, KeyBindingsList};
use crate::ui::util; use crate::ui::widgets2::Popup;
use crate::ui::widgets::editor::EditorState; use crate::ui::{util2, UiError};
use crate::ui::widgets::popup::Popup;
use crate::ui::widgets::BoxedWidget;
pub fn new() -> EditorState { pub fn new() -> EditorState {
EditorState::new() EditorState::new()
} }
pub fn widget(editor: &EditorState) -> BoxedWidget { pub fn widget(editor: &mut EditorState) -> BoxedAsync<'_, UiError> {
Popup::new(editor.widget().hidden()) Popup::new(
.title("Enter password") editor.widget().with_hidden_default_placeholder(),
.build() "Enter password",
)
.boxed_async()
} }
pub fn list_key_bindings(bindings: &mut KeyBindingsList) { pub fn list_key_bindings(bindings: &mut KeyBindingsList) {
bindings.binding("esc", "abort"); bindings.binding("esc", "abort");
bindings.binding("enter", "authenticate"); bindings.binding("enter", "authenticate");
util::list_editor_key_bindings(bindings, |_| true); util2::list_editor_key_bindings(bindings, |_| true);
} }
pub enum EventResult { pub enum EventResult {
@ -33,18 +34,18 @@ pub fn handle_input_event(
terminal: &mut Terminal, terminal: &mut Terminal,
event: &InputEvent, event: &InputEvent,
room: &Option<Room>, room: &Option<Room>,
editor: &EditorState, editor: &mut EditorState,
) -> EventResult { ) -> EventResult {
match event { match event {
key!(Esc) => EventResult::ResetState, key!(Esc) => EventResult::ResetState,
key!(Enter) => { key!(Enter) => {
if let Some(room) = &room { if let Some(room) = &room {
let _ = room.auth(editor.text()); let _ = room.auth(editor.text().to_string());
} }
EventResult::ResetState EventResult::ResetState
} }
_ => { _ => {
if util::handle_editor_input_event(editor, terminal, event, |_| true) { if util2::handle_editor_input_event(editor, terminal, event, |_| true) {
EventResult::Handled EventResult::Handled
} else { } else {
EventResult::NotHandled EventResult::NotHandled

View file

@ -16,7 +16,6 @@ use crate::euph;
use crate::macros::logging_unwrap; use crate::macros::logging_unwrap;
use crate::ui::chat::{ChatState, Reaction}; use crate::ui::chat::{ChatState, Reaction};
use crate::ui::input::{key, InputEvent, KeyBindingsList}; use crate::ui::input::{key, InputEvent, KeyBindingsList};
use crate::ui::widgets::editor::EditorState as OldEditorState;
use crate::ui::widgets::WidgetWrapper; use crate::ui::widgets::WidgetWrapper;
use crate::ui::widgets2::ListState; use crate::ui::widgets2::ListState;
use crate::ui::{util2, UiError, UiEvent}; use crate::ui::{util2, UiError, UiEvent};
@ -36,7 +35,7 @@ enum Focus {
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
enum State { enum State {
Normal, Normal,
Auth(OldEditorState), Auth(EditorState),
Nick(EditorState), Nick(EditorState),
Account(AccountUiState), Account(AccountUiState),
Links(LinksState), Links(LinksState),
@ -225,9 +224,7 @@ impl EuphRoom {
match &mut self.state { match &mut self.state {
State::Normal => {} State::Normal => {}
State::Auth(editor) => { State::Auth(editor) => layers.push(auth::widget(editor)),
layers.push(WidgetWrapper::new(auth::widget(editor)).boxed_async())
}
State::Nick(editor) => layers.push(nick::widget(editor)), State::Nick(editor) => layers.push(nick::widget(editor)),
State::Account(account) => { State::Account(account) => {
layers.push(WidgetWrapper::new(account.widget()).boxed_async()) layers.push(WidgetWrapper::new(account.widget()).boxed_async())