Skip to content

Commit

Permalink
Always close user menu when user is selected (#131).
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Apr 27, 2024
1 parent f65c706 commit f988ed1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum Mode {
#[default]
Username,
Password,
Action,
Users,
Command,
Sessions,
Expand Down
3 changes: 3 additions & 0 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ impl Ipc {
AuthMessageType::Info => {
greeter.remove_prompt();

greeter.previous_mode = greeter.mode;
greeter.mode = Mode::Action;

if let Some(message) = &mut greeter.message {
message.push('\n');
message.push_str(auth_message.trim_end());
Expand Down
10 changes: 6 additions & 4 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, input: KeyEvent, ipc: Ipc) ->
greeter.username = MaskedString::from(username, name);
}

greeter.mode = greeter.previous_mode;

validate_username(&mut greeter, &ipc).await;
}

Expand Down Expand Up @@ -271,7 +273,7 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, input: KeyEvent, ipc: Ipc) ->
greeter.mode = greeter.previous_mode;
}

Mode::Processing => {}
_ => {}
},

// Do not handle any other controls keybindings
Expand All @@ -296,7 +298,7 @@ async fn insert_key(greeter: &mut Greeter, c: char) {
Mode::Username => &greeter.username.value,
Mode::Password => &greeter.buffer,
Mode::Command => &greeter.buffer,
Mode::Users | Mode::Sessions | Mode::Power | Mode::Processing => return,
_ => return,
};

let index = (value.chars().count() as i16 + greeter.cursor_offset) as usize;
Expand All @@ -322,7 +324,7 @@ async fn delete_key(greeter: &mut Greeter, key: KeyCode) {
Mode::Username => &greeter.username.value,
Mode::Password => &greeter.buffer,
Mode::Command => &greeter.buffer,
Mode::Users | Mode::Sessions | Mode::Power | Mode::Processing => return,
_ => return,
};

let index = match key {
Expand All @@ -341,7 +343,7 @@ async fn delete_key(greeter: &mut Greeter, key: KeyCode) {
Mode::Username => greeter.username.value = value,
Mode::Password => greeter.buffer = value,
Mode::Command => greeter.buffer = value,
Mode::Users | Mode::Sessions | Mode::Power | Mode::Processing => return,
_ => return,
};

if let KeyCode::Delete = key {
Expand Down
12 changes: 7 additions & 5 deletions src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn
let (message, message_height) = get_message_height(greeter, container_padding, 1);
let (greeting, greeting_height) = get_greeting_height(greeter, container_padding, 0);

let should_display_answer = greeter.mode == Mode::Password;

let constraints = [
Constraint::Length(greeting_height), // Greeting
Constraint::Length(1), // Username
Constraint::Length(if greeter.mode == Mode::Username { 0 } else { prompt_padding }), // Prompt padding
Constraint::Length(if greeter.mode == Mode::Username { 0 } else { 1 }), // Answer
Constraint::Length(greeting_height), // Greeting
Constraint::Length(1), // Username
Constraint::Length(if should_display_answer { prompt_padding } else { 0 }), // Prompt padding
Constraint::Length(if should_display_answer { 1 } else { 0 }), // Answer
];

let chunks = Layout::default().direction(Direction::Vertical).constraints(constraints.as_ref()).split(frame);
Expand Down Expand Up @@ -67,7 +69,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn
let username_value = Paragraph::new(username_value_text);

match greeter.mode {
Mode::Username | Mode::Password => {
Mode::Username | Mode::Password | Mode::Action => {
f.render_widget(username_label, chunks[USERNAME_INDEX]);

if !greeter.user_menu || !greeter.username.value.is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion src/ui/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn should_hide_cursor(greeter: &Greeter) -> bool {
|| greeter.mode == Mode::Sessions
|| greeter.mode == Mode::Power
|| greeter.mode == Mode::Processing
|| greeter.mode == Mode::Action
}

// Computes the height of the main window where we display content, depending on
Expand All @@ -37,7 +38,7 @@ pub fn get_height(greeter: &Greeter) -> u16 {
let prompt_padding = greeter.prompt_padding();

let initial = match greeter.mode {
Mode::Username | Mode::Command => (2 * container_padding) + 1,
Mode::Username | Mode::Action | Mode::Command => (2 * container_padding) + 1,
Mode::Password => match greeter.prompt {
Some(_) => (2 * container_padding) + prompt_padding + 2,
None => (2 * container_padding) + 1,
Expand Down

0 comments on commit f988ed1

Please sign in to comment.