Skip to content

Commit

Permalink
UI refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Oct 21, 2023
1 parent 9aa63fa commit 1594e09
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 242 deletions.
90 changes: 44 additions & 46 deletions src/greeter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
collections::HashMap,
convert::TryInto,
env,
error::Error,
Expand All @@ -25,6 +24,12 @@ use zeroize::Zeroize;
use crate::{
info::{get_issue, get_last_session, get_last_session_path, get_last_user_name, get_last_user_session, get_last_user_session_path, get_last_user_username, get_min_max_uids, get_users},
power::PowerOption,
ui::{
common::menu::Menu,
power::Power,
sessions::{Session, SessionType},
users::User,
},
};

const DEFAULT_LOCALE: Locale = Locale::en_US;
Expand Down Expand Up @@ -60,34 +65,6 @@ pub enum Mode {
Processing,
}

#[derive(SmartDefault, Debug, Copy, Clone, PartialEq)]
pub enum SessionType {
X11,
Wayland,
TTY,
#[default]
None,
}

impl SessionType {
pub fn to_xdg_session_type(&self) -> &'static str {
match self {
SessionType::X11 => "x11",
SessionType::Wayland => "wayland",
SessionType::TTY => "tty",
SessionType::None => "unspecified",
}
}
}

#[derive(SmartDefault, Clone)]
pub struct Session {
pub name: String,
pub command: String,
pub session_type: SessionType,
pub path: Option<PathBuf>,
}

#[derive(SmartDefault)]
pub struct Greeter {
#[default(DEFAULT_LOCALE)]
Expand All @@ -100,18 +77,14 @@ pub struct Greeter {
pub previous_mode: Mode,
pub cursor_offset: i16,

pub users: Vec<(String, Option<String>)>,
pub selected_user: usize,
pub users: Menu<User>,
pub command: Option<String>,
pub new_command: String,
pub session_path: Option<PathBuf>,
pub session_paths: Vec<(PathBuf, SessionType)>,
pub sessions: Vec<Session>,
pub selected_session: usize,
pub sessions: Menu<Session>,
pub xsession_wrapper: Option<String>,

pub selected_power_option: usize,

pub username: String,
pub username_mask: Option<String>,
pub prompt: Option<String>,
Expand All @@ -129,7 +102,7 @@ pub struct Greeter {
pub greeting: Option<String>,
pub message: Option<String>,

pub power_commands: HashMap<PowerOption, String>,
pub power_commands: Menu<Power>,
pub power_command: Option<Command>,
pub power_command_notify: Arc<Notify>,
pub power_setsid: bool,
Expand All @@ -150,10 +123,21 @@ impl Greeter {
let mut greeter = Self::default();

greeter.set_locale();

greeter.power_commands = Menu {
title: fl!("title_power"),
options: Default::default(),
selected: 0,
};

greeter.parse_options().await;
greeter.sessions = crate::info::get_sessions(&greeter).unwrap_or_default();
greeter.sessions = Menu {
title: fl!("title_session"),
options: crate::info::get_sessions(&greeter).unwrap_or_default(),
selected: 0,
};

if let Some(Session { command, .. }) = greeter.sessions.get(0) {
if let Some(Session { command, .. }) = greeter.sessions.options.get(0) {
if greeter.command.is_none() {
greeter.command = Some(command.clone());
}
Expand Down Expand Up @@ -186,7 +170,12 @@ impl Greeter {
}
}

greeter.selected_session = greeter.sessions.iter().position(|Session { path, .. }| path.as_deref() == greeter.session_path.as_deref()).unwrap_or(0);
greeter.sessions.selected = greeter
.sessions
.options
.iter()
.position(|Session { path, .. }| path.as_deref() == greeter.session_path.as_deref())
.unwrap_or(0);

greeter
}
Expand Down Expand Up @@ -391,7 +380,11 @@ impl Greeter {
process::exit(1);
}

self.users = get_users(min_uid, max_uid);
self.users = Menu {
title: fl!("title_users"),
options: get_users(min_uid, max_uid),
selected: 0,
}
}

if self.config().opt_present("remember-session") && self.config().opt_present("remember-user-session") {
Expand Down Expand Up @@ -428,12 +421,17 @@ impl Greeter {
self.greeting = get_issue();
}

if let Some(command) = self.config().opt_str("power-shutdown") {
self.power_commands.insert(PowerOption::Shutdown, command);
}
if let Some(command) = self.config().opt_str("power-reboot") {
self.power_commands.insert(PowerOption::Reboot, command);
}
self.power_commands.options.push(Power {
action: PowerOption::Shutdown,
label: fl!("shutdown"),
command: self.config().opt_str("power-shutdown"),
});

self.power_commands.options.push(Power {
action: PowerOption::Reboot,
label: fl!("reboot"),
command: self.config().opt_str("power-reboot"),
});

self.power_setsid = !self.config().opt_present("power-no-setsid");

Expand Down
14 changes: 10 additions & 4 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ use ini::Ini;
use lazy_static::lazy_static;
use nix::sys::utsname;

use crate::{Greeter, Session, SessionType};
use crate::{
ui::{
sessions::{Session, SessionType},
users::User,
},
Greeter,
};

const LAST_USER_USERNAME: &str = "/var/cache/tuigreet/lastuser";
const LAST_USER_NAME: &str = "/var/cache/tuigreet/lastuser-name";
Expand Down Expand Up @@ -147,13 +153,13 @@ pub fn write_last_user_session(username: &str, session: &str) {
let _ = fs::write(format!("{LAST_SESSION}-{username}"), session);
}

pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<(String, Option<String>)> {
pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<User> {
match File::open("/etc/passwd") {
Err(_) => vec![],
Ok(file) => {
let file = BufReader::new(file);

let users: Vec<(String, Option<String>)> = file
let users: Vec<User> = file
.lines()
.filter_map(|line| {
line
Expand All @@ -176,7 +182,7 @@ pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<(String, Option<String>)> {
.ok()
.flatten()
.filter(|(uid, _, _)| uid >= &min_uid && uid <= &max_uid)
.map(|(_, username, name)| (username, name))
.map(|(_, username, name)| User { username, name })
})
.collect();

Expand Down
5 changes: 3 additions & 2 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use tokio::sync::{

use crate::{
info::{write_last_user_session, write_last_user_session_path, write_last_username},
AuthStatus, Greeter, Mode, Session, SessionType,
ui::sessions::{Session, SessionType},
AuthStatus, Greeter, Mode,
};

#[derive(Clone)]
Expand Down Expand Up @@ -121,7 +122,7 @@ impl Ipc {
greeter.done = true;
greeter.mode = Mode::Processing;

let session = greeter.sessions.get(greeter.selected_session).filter(|s| &s.command == command);
let session = greeter.sessions.options.get(greeter.sessions.selected).filter(|s| &s.command == command);
let mut command = command.clone();
let mut env = vec![];

Expand Down
49 changes: 28 additions & 21 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
info::{delete_last_session_path, get_last_user_session, get_last_user_session_path, write_last_session, write_last_session_path},
ipc::Ipc,
power::power,
ui::POWER_OPTIONS,
Greeter, Mode, Session,
ui::{sessions::Session, users::User},
Greeter, Mode,
};

pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc) -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -84,40 +84,40 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc

KeyEvent { code: KeyCode::Up, .. } => {
if let Mode::Users = greeter.mode {
if greeter.selected_user > 0 {
greeter.selected_user -= 1;
if greeter.users.selected > 0 {
greeter.users.selected -= 1;
}
}

if let Mode::Sessions = greeter.mode {
if greeter.selected_session > 0 {
greeter.selected_session -= 1;
if greeter.sessions.selected > 0 {
greeter.sessions.selected -= 1;
}
}

if let Mode::Power = greeter.mode {
if greeter.selected_power_option > 0 {
greeter.selected_power_option -= 1;
if greeter.power_commands.selected > 0 {
greeter.power_commands.selected -= 1;
}
}
}

KeyEvent { code: KeyCode::Down, .. } => {
if let Mode::Users = greeter.mode {
if greeter.selected_user < greeter.users.len() - 1 {
greeter.selected_user += 1;
if greeter.users.selected < greeter.users.options.len() - 1 {
greeter.users.selected += 1;
}
}

if let Mode::Sessions = greeter.mode {
if greeter.selected_session < greeter.sessions.len() - 1 {
greeter.selected_session += 1;
if greeter.sessions.selected < greeter.sessions.options.len() - 1 {
greeter.sessions.selected += 1;
}
}

if let Mode::Power = greeter.mode {
if greeter.selected_power_option < POWER_OPTIONS.len() - 1 {
greeter.selected_power_option += 1;
if greeter.power_commands.selected < greeter.power_commands.options.len() - 1 {
greeter.power_commands.selected += 1;
}
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc
}

Mode::Command => {
greeter.selected_session = 0;
greeter.sessions.selected = 0;
greeter.command = Some(greeter.new_command.clone());

if greeter.remember_session {
Expand All @@ -188,9 +188,9 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc
}

Mode::Users => {
let username = greeter.users.get(greeter.selected_user).cloned();
let username = greeter.users.options.get(greeter.users.selected).cloned();

if let Some((username, name)) = username {
if let Some(User { username, name }) = username {
greeter.username = username;
greeter.username_mask = name;
}
Expand All @@ -199,7 +199,7 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc
}

Mode::Sessions => {
let session = greeter.sessions.get(greeter.selected_session).cloned();
let session = greeter.sessions.options.get(greeter.sessions.selected).cloned();

if let Some(Session { path, command, .. }) = session {
if greeter.remember_session {
Expand All @@ -218,8 +218,10 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc
}

Mode::Power => {
if let Some((option, _)) = POWER_OPTIONS.get(greeter.selected_power_option) {
power(&mut greeter, *option);
let power_command = greeter.power_commands.options.get(greeter.power_commands.selected).cloned();

if let Some(command) = power_command {
power(&mut greeter, command.action);
}

greeter.mode = greeter.previous_mode;
Expand Down Expand Up @@ -304,7 +306,12 @@ async fn validate_username(greeter: &mut Greeter, ipc: &Ipc) {

if greeter.remember_user_session {
if let Ok(last_session) = get_last_user_session_path(&greeter.username) {
greeter.selected_session = greeter.sessions.iter().position(|Session { path, .. }| path.as_deref() == Some(last_session.as_ref())).unwrap_or(0);
greeter.sessions.selected = greeter
.sessions
.options
.iter()
.position(|Session { path, .. }| path.as_deref() == Some(last_session.as_ref()))
.unwrap_or(0);
}

if let Ok(command) = get_last_user_session(&greeter.username) {
Expand Down
Loading

0 comments on commit 1594e09

Please sign in to comment.