Extract auth dialog into module
This commit is contained in:
parent
9ad550f98c
commit
878467835e
3 changed files with 106 additions and 54 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod auth;
|
||||||
mod nick;
|
mod nick;
|
||||||
mod nick_list;
|
mod nick_list;
|
||||||
mod popup;
|
mod popup;
|
||||||
|
|
|
||||||
90
src/ui/euph/auth.rs
Normal file
90
src/ui/euph/auth.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crossterm::event::KeyCode;
|
||||||
|
use crossterm::style::{ContentStyle, Stylize};
|
||||||
|
use euphoxide::conn::{Joining, Status};
|
||||||
|
use parking_lot::FairMutex;
|
||||||
|
use toss::terminal::Terminal;
|
||||||
|
|
||||||
|
use crate::euph::Room;
|
||||||
|
use crate::ui::input::{key, InputEvent, KeyBindingsList, KeyEvent};
|
||||||
|
use crate::ui::util;
|
||||||
|
use crate::ui::widgets::cursor::Cursor;
|
||||||
|
use crate::ui::widgets::editor::EditorState;
|
||||||
|
use crate::ui::widgets::padding::Padding;
|
||||||
|
use crate::ui::widgets::popup::Popup;
|
||||||
|
use crate::ui::widgets::text::Text;
|
||||||
|
use crate::ui::widgets::BoxedWidget;
|
||||||
|
|
||||||
|
use super::room::RoomStatus;
|
||||||
|
|
||||||
|
pub fn new() -> EditorState {
|
||||||
|
EditorState::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stable(status: &RoomStatus) -> bool {
|
||||||
|
matches!(
|
||||||
|
status,
|
||||||
|
RoomStatus::Connected(Status::Joining(Joining {
|
||||||
|
bounce: Some(_),
|
||||||
|
..
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn widget() -> BoxedWidget {
|
||||||
|
Popup::new(
|
||||||
|
Padding::new(Cursor::new(Text::new((
|
||||||
|
"<hidden>",
|
||||||
|
ContentStyle::default().grey().italic(),
|
||||||
|
))))
|
||||||
|
.left(1),
|
||||||
|
)
|
||||||
|
.title("Enter password")
|
||||||
|
.inner_padding(false)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_key_bindings(bindings: &mut KeyBindingsList) {
|
||||||
|
bindings.binding("esc", "abort");
|
||||||
|
bindings.binding("enter", "authenticate");
|
||||||
|
util::list_editor_key_bindings(bindings, |_| true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum EventResult {
|
||||||
|
NotHandled,
|
||||||
|
Handled,
|
||||||
|
ResetState,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_input_event(
|
||||||
|
terminal: &mut Terminal,
|
||||||
|
crossterm_lock: &Arc<FairMutex<()>>,
|
||||||
|
event: &InputEvent,
|
||||||
|
room: &Option<Room>,
|
||||||
|
editor: &EditorState,
|
||||||
|
) -> EventResult {
|
||||||
|
match event {
|
||||||
|
key!(Esc) => EventResult::ResetState,
|
||||||
|
key!(Enter) => {
|
||||||
|
if let Some(room) = &room {
|
||||||
|
let _ = room.auth(editor.text());
|
||||||
|
}
|
||||||
|
EventResult::ResetState
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if util::handle_editor_input_event(
|
||||||
|
editor,
|
||||||
|
terminal,
|
||||||
|
crossterm_lock,
|
||||||
|
event,
|
||||||
|
|_| true,
|
||||||
|
false,
|
||||||
|
) {
|
||||||
|
EventResult::Handled
|
||||||
|
} else {
|
||||||
|
EventResult::NotHandled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,20 +17,18 @@ use crate::store::MsgStore;
|
||||||
use crate::ui::chat::{ChatState, Reaction};
|
use crate::ui::chat::{ChatState, Reaction};
|
||||||
use crate::ui::input::{key, InputEvent, KeyBindingsList, KeyEvent};
|
use crate::ui::input::{key, InputEvent, KeyBindingsList, KeyEvent};
|
||||||
use crate::ui::widgets::border::Border;
|
use crate::ui::widgets::border::Border;
|
||||||
use crate::ui::widgets::cursor::Cursor;
|
|
||||||
use crate::ui::widgets::editor::EditorState;
|
use crate::ui::widgets::editor::EditorState;
|
||||||
use crate::ui::widgets::join::{HJoin, Segment, VJoin};
|
use crate::ui::widgets::join::{HJoin, Segment, VJoin};
|
||||||
use crate::ui::widgets::layer::Layer;
|
use crate::ui::widgets::layer::Layer;
|
||||||
use crate::ui::widgets::list::ListState;
|
use crate::ui::widgets::list::ListState;
|
||||||
use crate::ui::widgets::padding::Padding;
|
use crate::ui::widgets::padding::Padding;
|
||||||
use crate::ui::widgets::popup::Popup;
|
|
||||||
use crate::ui::widgets::text::Text;
|
use crate::ui::widgets::text::Text;
|
||||||
use crate::ui::widgets::BoxedWidget;
|
use crate::ui::widgets::BoxedWidget;
|
||||||
use crate::ui::{util, UiEvent};
|
use crate::ui::UiEvent;
|
||||||
use crate::vault::EuphVault;
|
use crate::vault::EuphVault;
|
||||||
|
|
||||||
use super::popup::RoomPopup;
|
use super::popup::RoomPopup;
|
||||||
use super::{nick, nick_list};
|
use super::{auth, nick, nick_list};
|
||||||
|
|
||||||
enum State {
|
enum State {
|
||||||
Normal,
|
Normal,
|
||||||
|
|
@ -155,17 +153,7 @@ impl EuphRoom {
|
||||||
|
|
||||||
fn stabilize_state(&mut self, status: &RoomStatus) {
|
fn stabilize_state(&mut self, status: &RoomStatus) {
|
||||||
match &self.state {
|
match &self.state {
|
||||||
State::Auth(_)
|
State::Auth(_) if !auth::stable(status) => self.state = State::Normal,
|
||||||
if !matches!(
|
|
||||||
status,
|
|
||||||
RoomStatus::Connected(Status::Joining(Joining {
|
|
||||||
bounce: Some(_),
|
|
||||||
..
|
|
||||||
}))
|
|
||||||
) =>
|
|
||||||
{
|
|
||||||
self.state = State::Normal
|
|
||||||
}
|
|
||||||
State::Nick(_) if !nick::stable(status) => self.state = State::Normal,
|
State::Nick(_) if !nick::stable(status) => self.state = State::Normal,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -190,7 +178,7 @@ impl EuphRoom {
|
||||||
|
|
||||||
match &self.state {
|
match &self.state {
|
||||||
State::Normal => {}
|
State::Normal => {}
|
||||||
State::Auth(_) => layers.push(Self::auth_widget()),
|
State::Auth(_) => layers.push(auth::widget()),
|
||||||
State::Nick(editor) => layers.push(nick::widget(editor)),
|
State::Nick(editor) => layers.push(nick::widget(editor)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -201,19 +189,6 @@ impl EuphRoom {
|
||||||
Layer::new(layers).into()
|
Layer::new(layers).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn auth_widget() -> BoxedWidget {
|
|
||||||
Popup::new(
|
|
||||||
Padding::new(Cursor::new(Text::new((
|
|
||||||
"<hidden>",
|
|
||||||
ContentStyle::default().grey().italic(),
|
|
||||||
))))
|
|
||||||
.left(1),
|
|
||||||
)
|
|
||||||
.title("Enter password")
|
|
||||||
.inner_padding(false)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn widget_without_nick_list(&self, status: &RoomStatus) -> BoxedWidget {
|
async fn widget_without_nick_list(&self, status: &RoomStatus) -> BoxedWidget {
|
||||||
VJoin::new(vec![
|
VJoin::new(vec![
|
||||||
Segment::new(Border::new(
|
Segment::new(Border::new(
|
||||||
|
|
@ -309,11 +284,7 @@ impl EuphRoom {
|
||||||
bindings.empty();
|
bindings.empty();
|
||||||
self.chat.list_key_bindings(bindings, can_compose).await;
|
self.chat.list_key_bindings(bindings, can_compose).await;
|
||||||
}
|
}
|
||||||
State::Auth(_) => {
|
State::Auth(_) => auth::list_key_bindings(bindings),
|
||||||
bindings.binding("esc", "abort");
|
|
||||||
bindings.binding("enter", "authenticate");
|
|
||||||
util::list_editor_key_bindings(bindings, |_| true, false);
|
|
||||||
}
|
|
||||||
State::Nick(_) => nick::list_key_bindings(bindings),
|
State::Nick(_) => nick::list_key_bindings(bindings),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -360,7 +331,7 @@ impl EuphRoom {
|
||||||
Some(Status::Joining(Joining {
|
Some(Status::Joining(Joining {
|
||||||
bounce: Some(_), ..
|
bounce: Some(_), ..
|
||||||
})) if matches!(event, key!('a') | key!('A')) => {
|
})) if matches!(event, key!('a') | key!('A')) => {
|
||||||
self.state = State::Auth(EditorState::new());
|
self.state = State::Auth(auth::new());
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Some(Status::Joined(joined)) if matches!(event, key!('n') | key!('N')) => {
|
Some(Status::Joined(joined)) if matches!(event, key!('n') | key!('N')) => {
|
||||||
|
|
@ -376,27 +347,17 @@ impl EuphRoom {
|
||||||
.handled()
|
.handled()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Auth(ed) => match event {
|
State::Auth(editor) => {
|
||||||
key!(Esc) => {
|
match auth::handle_input_event(terminal, crossterm_lock, event, &self.room, editor)
|
||||||
self.state = State::Normal;
|
{
|
||||||
true
|
auth::EventResult::NotHandled => false,
|
||||||
}
|
auth::EventResult::Handled => true,
|
||||||
key!(Enter) => {
|
auth::EventResult::ResetState => {
|
||||||
if let Some(room) = &self.room {
|
self.state = State::Normal;
|
||||||
let _ = room.auth(ed.text());
|
true
|
||||||
}
|
}
|
||||||
self.state = State::Normal;
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
_ => util::handle_editor_input_event(
|
}
|
||||||
ed,
|
|
||||||
terminal,
|
|
||||||
crossterm_lock,
|
|
||||||
event,
|
|
||||||
|_| true,
|
|
||||||
false,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
State::Nick(editor) => {
|
State::Nick(editor) => {
|
||||||
match nick::handle_input_event(terminal, crossterm_lock, event, &self.room, editor)
|
match nick::handle_input_event(terminal, crossterm_lock, event, &self.room, editor)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue