cosmic-greeter/src/greeter.rs

1034 lines
41 KiB
Rust
Raw Normal View History

2023-10-05 17:47:23 -06:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::app::{message, Command, Core, Settings};
use cosmic::{
executor,
2024-02-06 10:58:34 -07:00
iced::{
self, alignment,
event::{
self,
wayland::{Event as WaylandEvent, LayerEvent, OutputEvent},
},
2024-02-06 15:03:07 -07:00
futures::{self, SinkExt},
2024-02-06 10:58:34 -07:00
subscription,
wayland::{
actions::layer_surface::{IcedMargin, IcedOutput, SctkLayerSurfaceSettings},
layer_surface::{
destroy_layer_surface, get_layer_surface, Anchor, KeyboardInteractivity, Layer,
},
},
Length, Subscription,
},
iced_runtime::core::window::Id as SurfaceId,
style, widget, Element,
};
2024-02-06 15:03:07 -07:00
use cosmic_greeter_daemon::{UserData, WallpaperData};
2023-10-05 17:47:23 -06:00
use greetd_ipc::{codec::SyncCodec, AuthMessageType, Request, Response};
2024-02-06 15:03:07 -07:00
use std::{collections::HashMap, env, error::Error, fs, io, path::Path, process, sync::Arc};
2023-11-21 15:48:22 -07:00
use tokio::{net::UnixStream, time};
2024-02-06 10:58:34 -07:00
use wayland_client::{protocol::wl_output::WlOutput, Proxy};
2024-02-06 15:03:07 -07:00
use zbus::{dbus_proxy, Connection};
#[dbus_proxy(
interface = "com.system76.CosmicGreeter",
default_service = "com.system76.CosmicGreeter",
default_path = "/com/system76/CosmicGreeter"
)]
trait Greeter {
async fn get_user_data(&self) -> Result<String, zbus::Error>;
}
async fn user_data_dbus() -> Result<Vec<UserData>, Box<dyn Error>> {
let connection = Connection::system().await?;
// `dbus_proxy` macro creates `MyGreaterProxy` based on `Notifications` trait.
let proxy = GreeterProxy::new(&connection).await?;
let reply = proxy.get_user_data().await?;
2023-10-05 17:47:23 -06:00
2024-02-06 15:03:07 -07:00
let user_datas: Vec<UserData> = ron::from_str(&reply)?;
Ok(user_datas)
}
fn user_data_fallback() -> Vec<UserData> {
2023-10-05 17:47:23 -06:00
// The pwd::Passwd method is unsafe (but not labelled as such) due to using global state (libc pwent functions).
2024-02-06 15:03:07 -07:00
/* unsafe */
{
2023-10-05 17:47:23 -06:00
pwd::Passwd::iter()
.filter(|user| {
if user.uid < 1000 {
// Skip system accounts
return false;
}
match Path::new(&user.shell).file_name().and_then(|x| x.to_str()) {
// Skip shell ending in false
Some("false") => false,
// Skip shell ending in nologin
Some("nologin") => false,
_ => true,
}
})
.map(|user| {
2023-10-06 10:44:05 -06:00
//TODO: use accountsservice
2023-10-05 17:47:23 -06:00
let icon_path = Path::new("/var/lib/AccountsService/icons").join(&user.name);
let icon_opt = if icon_path.is_file() {
match fs::read(&icon_path) {
2024-02-06 15:03:07 -07:00
Ok(icon_data) => Some(icon_data),
2023-10-05 17:47:23 -06:00
Err(err) => {
log::error!("failed to read {:?}: {:?}", icon_path, err);
None
}
}
} else {
None
};
2024-02-06 15:03:07 -07:00
UserData {
uid: user.uid,
name: user.name,
full_name_opt: user
.gecos
.map(|gecos| gecos.split(',').next().unwrap_or_default().to_string()),
icon_opt,
wallpapers_opt: None,
}
2023-10-05 17:47:23 -06:00
})
.collect()
2024-02-06 15:03:07 -07:00
}
}
pub fn main() -> Result<(), Box<dyn Error>> {
let mut user_datas = match futures::executor::block_on(async { user_data_dbus().await }) {
Ok(ok) => ok,
Err(err) => {
log::error!("failed to load user data from daemon: {}", err);
user_data_fallback()
}
2023-10-05 17:47:23 -06:00
};
2024-02-06 15:03:07 -07:00
// Sort user data by uid
user_datas.sort_by(|a, b| a.uid.cmp(&b.uid));
2024-02-06 10:32:56 -07:00
enum SessionType {
X11,
Wayland,
}
2023-10-05 17:47:23 -06:00
//TODO: allow custom directories?
let session_dirs = &[
2024-02-06 10:32:56 -07:00
(
Path::new("/usr/share/wayland-sessions"),
SessionType::Wayland,
),
(Path::new("/usr/share/xsessions"), SessionType::X11),
2023-10-05 17:47:23 -06:00
];
let sessions = {
let mut sessions = HashMap::new();
2024-02-06 10:32:56 -07:00
for (session_dir, session_type) in session_dirs {
2023-10-05 17:47:23 -06:00
let read_dir = match fs::read_dir(&session_dir) {
Ok(ok) => ok,
Err(err) => {
log::warn!(
"failed to read session directory {:?}: {:?}",
session_dir,
err
);
continue;
}
};
for dir_entry_res in read_dir {
let dir_entry = match dir_entry_res {
Ok(ok) => ok,
Err(err) => {
log::warn!(
"failed to read session directory {:?} entry: {:?}",
session_dir,
err
);
continue;
}
};
let entry = match freedesktop_entry_parser::parse_entry(dir_entry.path()) {
Ok(ok) => ok,
Err(err) => {
log::warn!(
"failed to read session file {:?}: {:?}",
dir_entry.path(),
err
);
continue;
}
};
let name = match entry.section("Desktop Entry").attr("Name") {
Some(some) => some,
None => {
log::warn!(
"failed to read session file {:?}: no Desktop Entry/Name attribute",
dir_entry.path()
);
continue;
}
};
let exec = match entry.section("Desktop Entry").attr("Exec") {
Some(some) => some,
None => {
log::warn!(
"failed to read session file {:?}: no Desktop Entry/Exec attribute",
dir_entry.path()
);
continue;
}
};
2024-02-06 10:32:56 -07:00
let mut command = match session_type {
SessionType::X11 => {
//TODO: xinit may be better, but more complicated to set up
vec![
"startx".to_string(),
"/usr/bin/env".to_string(),
"XDG_SESSION_TYPE=x11".to_string(),
]
}
SessionType::Wayland => {
vec![
"/usr/bin/env".to_string(),
"XDG_SESSION_TYPE=wayland".to_string(),
]
}
2024-02-06 10:26:53 -07:00
};
match shlex::split(exec) {
2024-02-06 10:32:56 -07:00
Some(args) => {
for arg in args {
2024-02-06 10:26:53 -07:00
command.push(arg)
}
}
2023-10-05 17:47:23 -06:00
None => {
log::warn!(
"failed to parse session file {:?} Exec field {:?}",
dir_entry.path(),
exec
);
continue;
}
};
2024-02-06 10:26:53 -07:00
log::warn!("session {} using command {:?}", name, command);
match sessions.insert(name.to_string(), command) {
2023-10-05 17:47:23 -06:00
Some(some) => {
2024-02-06 10:26:53 -07:00
log::warn!("session {} overwrote old command {:?}", name, some);
2023-10-05 17:47:23 -06:00
}
None => {}
}
}
}
sessions
};
2024-02-06 15:03:07 -07:00
let fallback_background =
widget::image::Handle::from_memory(include_bytes!("../res/background.png"));
let flags = Flags {
2024-02-06 15:03:07 -07:00
user_datas,
sessions,
2024-02-06 15:03:07 -07:00
fallback_background,
};
2023-10-05 17:47:23 -06:00
2024-02-06 10:58:34 -07:00
let settings = Settings::default().no_main_window(true);
2023-10-05 17:47:23 -06:00
cosmic::app::run::<App>(settings, flags)?;
Ok(())
}
async fn request_message(socket: Arc<UnixStream>, request: Request) -> Message {
//TODO: handle errors
socket.writable().await.unwrap();
{
let mut bytes = Vec::<u8>::new();
request.write_to(&mut bytes).unwrap();
socket.try_write(&bytes).unwrap();
}
//TODO: handle responses at any time?
loop {
socket.readable().await.unwrap();
let mut bytes = Vec::<u8>::with_capacity(4096);
match socket.try_read_buf(&mut bytes) {
Ok(0) => break,
Ok(_count) => {
let mut cursor = io::Cursor::new(bytes);
let response = Response::read_from(&mut cursor).unwrap();
log::info!("{:?}", response);
match response {
Response::AuthMessage {
auth_message_type,
auth_message,
} => match auth_message_type {
AuthMessageType::Secret => {
return Message::Prompt(auth_message, true, Some(String::new()));
2023-10-05 17:47:23 -06:00
}
AuthMessageType::Visible => {
return Message::Prompt(auth_message, false, Some(String::new()));
2023-10-05 17:47:23 -06:00
}
//TODO: treat error type differently?
AuthMessageType::Info | AuthMessageType::Error => {
return Message::Prompt(auth_message, false, None);
2023-10-05 17:47:23 -06:00
}
},
Response::Error {
error_type: _,
description,
} => {
//TODO: use error_type?
return Message::Error(description);
}
Response::Success => match request {
Request::CreateSession { .. } => {
// User has no auth required, proceed to login
return Message::Login(socket);
}
Request::PostAuthMessageResponse { .. } => {
// All auth is completed, proceed to login
return Message::Login(socket);
}
Request::StartSession { .. } => {
// Session has been started, exit greeter
return Message::Exit;
}
Request::CancelSession => {
//TODO: restart whole process
return Message::None;
}
},
}
}
Err(err) => match err.kind() {
io::ErrorKind::WouldBlock => continue,
_ => {
log::error!("failed to read socket: {:?}", err);
break;
}
},
}
}
Message::None
}
fn request_command(socket: Arc<UnixStream>, request: Request) -> Command<Message> {
Command::perform(
async move { message::app(request_message(socket, request).await) },
|x| x,
)
}
#[derive(Clone)]
pub struct Flags {
2024-02-06 15:03:07 -07:00
user_datas: Vec<UserData>,
2023-10-05 17:47:23 -06:00
sessions: HashMap<String, Vec<String>>,
2024-02-06 15:03:07 -07:00
fallback_background: widget::image::Handle,
2023-10-05 17:47:23 -06:00
}
#[derive(Clone, Debug)]
pub enum SocketState {
/// Opening GREETD_SOCK
Pending,
/// GREETD_SOCK is open
Open(Arc<UnixStream>),
/// No GREETD_SOCK variable set
NotSet,
/// Failed to open GREETD_SOCK
Error(Arc<io::Error>),
}
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {
None,
2024-02-06 10:58:34 -07:00
OutputEvent(OutputEvent, WlOutput),
LayerEvent(LayerEvent, SurfaceId),
2023-10-05 17:47:23 -06:00
Socket(SocketState),
2024-02-06 10:58:34 -07:00
NetworkIcon(Option<&'static str>),
PowerInfo(Option<(String, f64)>),
Prompt(String, bool, Option<String>),
2023-10-05 17:47:23 -06:00
Session(String),
Username(Arc<UnixStream>, String),
Auth(Arc<UnixStream>, Option<String>),
Login(Arc<UnixStream>),
2023-11-29 08:05:09 -07:00
Suspend,
Error(String),
2023-10-05 17:47:23 -06:00
Exit,
}
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
flags: Flags,
2024-02-06 10:58:34 -07:00
surface_ids: HashMap<WlOutput, SurfaceId>,
active_surface_id_opt: Option<SurfaceId>,
2024-02-06 15:03:07 -07:00
surface_images: HashMap<SurfaceId, widget::image::Handle>,
2024-02-06 10:58:34 -07:00
surface_names: HashMap<SurfaceId, String>,
text_input_ids: HashMap<SurfaceId, widget::Id>,
network_icon_opt: Option<&'static str>,
power_info_opt: Option<(String, f64)>,
2023-10-05 17:47:23 -06:00
socket_state: SocketState,
username_opt: Option<String>,
prompt_opt: Option<(String, bool, Option<String>)>,
2023-10-05 17:47:23 -06:00
session_names: Vec<String>,
selected_session: String,
error_opt: Option<String>,
}
2024-02-06 15:03:07 -07:00
impl App {
fn update_wallpapers(&mut self) {
let username = match &self.username_opt {
Some(some) => some,
None => return,
};
let user_data = match self.flags.user_datas.iter().find(|x| &x.name == username) {
Some(some) => some,
None => return,
};
let wallpapers = match &user_data.wallpapers_opt {
Some(some) => some,
None => return,
};
for (output, surface_id) in self.surface_ids.iter() {
if self.surface_images.contains_key(surface_id) {
continue;
}
let output_name = match self.surface_names.get(surface_id) {
Some(some) => some,
None => continue,
};
log::info!("updating wallpaper for {:?}", output_name);
for (wallpaper_output_name, wallpaper_data) in wallpapers.iter() {
if wallpaper_output_name == output_name {
match wallpaper_data {
WallpaperData::Bytes(bytes) => {
let image = widget::image::Handle::from_memory(bytes.clone());
self.surface_images.insert(*surface_id, image);
//TODO: what to do about duplicates?
break;
}
WallpaperData::Color(color) => {
//TODO: support color sources
log::warn!("output {}: unsupported source {:?}", output.id(), color);
}
}
}
}
}
}
}
2023-10-05 17:47:23 -06:00
/// Implement [`cosmic::Application`] to integrate with COSMIC.
impl cosmic::Application for App {
/// Default async executor to use with the app.
type Executor = executor::Default;
/// Argument received [`cosmic::Application::new`].
type Flags = Flags;
/// Message type specific to our [`App`].
type Message = Message;
/// The unique application ID to supply to the window manager.
const APP_ID: &'static str = "com.system76.CosmicGreeter";
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
/// Creates the application, and optionally emits command on initialize.
fn init(mut core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
core.window.show_window_menu = false;
core.window.show_headerbar = false;
core.window.sharp_corners = true;
core.window.show_maximize = false;
core.window.show_minimize = false;
core.window.use_template = false;
let mut session_names: Vec<_> = flags.sessions.keys().map(|x| x.to_string()).collect();
session_names.sort();
//TODO: determine default session?
let selected_session = session_names.first().cloned().unwrap_or(String::new());
(
App {
core,
flags,
2024-02-06 10:58:34 -07:00
surface_ids: HashMap::new(),
active_surface_id_opt: None,
2024-02-06 15:03:07 -07:00
surface_images: HashMap::new(),
2024-02-06 10:58:34 -07:00
surface_names: HashMap::new(),
text_input_ids: HashMap::new(),
network_icon_opt: None,
power_info_opt: None,
2023-10-05 17:47:23 -06:00
socket_state: SocketState::Pending,
username_opt: None,
prompt_opt: None,
2023-10-05 17:47:23 -06:00
session_names,
selected_session,
error_opt: None,
},
Command::perform(
async {
message::app(Message::Socket(match env::var_os("GREETD_SOCK") {
Some(socket_path) => match UnixStream::connect(&socket_path).await {
Ok(socket) => SocketState::Open(Arc::new(socket)),
Err(err) => SocketState::Error(Arc::new(err)),
},
None => SocketState::NotSet,
}))
},
|x| x,
),
)
}
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::None => {}
2024-02-06 10:58:34 -07:00
Message::OutputEvent(output_event, output) => {
match output_event {
OutputEvent::Created(output_info_opt) => {
log::info!("output {}: created", output.id());
let surface_id = SurfaceId::unique();
match self.surface_ids.insert(output.clone(), surface_id) {
Some(old_surface_id) => {
//TODO: remove old surface?
log::warn!(
"output {}: already had surface ID {:?}",
output.id(),
old_surface_id
);
}
None => {}
}
match output_info_opt {
Some(output_info) => match output_info.name {
Some(output_name) => {
self.surface_names.insert(surface_id, output_name.clone());
2024-02-06 15:03:07 -07:00
self.surface_images.remove(&surface_id);
self.update_wallpapers();
2024-02-06 10:58:34 -07:00
}
None => {
log::warn!("output {}: no output name", output.id());
}
},
None => {
log::warn!("output {}: no output info", output.id());
}
}
let text_input_id = widget::Id::unique();
self.text_input_ids
.insert(surface_id, text_input_id.clone());
return Command::batch([
get_layer_surface(SctkLayerSurfaceSettings {
id: surface_id,
layer: Layer::Overlay,
keyboard_interactivity: KeyboardInteractivity::Exclusive,
pointer_interactivity: true,
anchor: Anchor::TOP | Anchor::BOTTOM | Anchor::LEFT | Anchor::RIGHT,
output: IcedOutput::Output(output),
namespace: "cosmic-locker".into(),
size: Some((None, None)),
margin: IcedMargin {
top: 0,
bottom: 0,
left: 0,
right: 0,
},
exclusive_zone: -1,
size_limits: iced::Limits::NONE.min_width(1.0).min_height(1.0),
}),
widget::text_input::focus(text_input_id),
]);
}
OutputEvent::Removed => {
log::info!("output {}: removed", output.id());
match self.surface_ids.remove(&output) {
Some(surface_id) => {
2024-02-06 15:03:07 -07:00
self.surface_images.remove(&surface_id);
2024-02-06 10:58:34 -07:00
self.surface_names.remove(&surface_id);
self.text_input_ids.remove(&surface_id);
return destroy_layer_surface(surface_id);
}
None => {
log::warn!("output {}: no surface found", output.id());
}
}
}
OutputEvent::InfoUpdate(output_info) => {
log::info!("output {}: info update {:#?}", output.id(), output_info);
}
}
}
Message::LayerEvent(layer_event, surface_id) => match layer_event {
LayerEvent::Focused => {
log::info!("focus surface {:?}", surface_id);
self.active_surface_id_opt = Some(surface_id);
if let Some(text_input_id) = self.text_input_ids.get(&surface_id) {
return widget::text_input::focus(text_input_id.clone());
}
}
_ => {}
},
2023-10-05 17:47:23 -06:00
Message::Socket(socket_state) => {
self.socket_state = socket_state;
2024-02-06 15:03:07 -07:00
match &self.socket_state {
SocketState::Open(socket) => {
// When socket is opened, request default user
//TODO: choose last used user
match self.flags.user_datas.first().map(|x| x.name.clone()) {
Some(username) => {
let socket = socket.clone();
return Command::perform(
async { message::app(Message::Username(socket, username)) },
|x| x,
);
}
None => {}
}
}
_ => {}
}
2023-10-05 17:47:23 -06:00
}
2024-02-06 10:58:34 -07:00
Message::NetworkIcon(network_icon_opt) => {
self.network_icon_opt = network_icon_opt;
}
Message::PowerInfo(power_info_opt) => {
self.power_info_opt = power_info_opt;
}
Message::Prompt(prompt, secret, value) => {
2024-02-06 10:58:34 -07:00
let prompt_was_none = self.prompt_opt.is_none();
self.prompt_opt = Some((prompt, secret, value));
2024-02-06 10:58:34 -07:00
if prompt_was_none {
if let Some(surface_id) = self.active_surface_id_opt {
if let Some(text_input_id) = self.text_input_ids.get(&surface_id) {
return widget::text_input::focus(text_input_id.clone());
}
}
}
2023-10-05 17:47:23 -06:00
}
Message::Session(selected_session) => {
self.selected_session = selected_session;
}
Message::Username(socket, username) => {
self.username_opt = Some(username.clone());
2024-02-06 15:03:07 -07:00
self.surface_images.clear();
self.update_wallpapers();
2023-10-05 17:47:23 -06:00
return request_command(socket, Request::CreateSession { username });
}
Message::Auth(socket, response) => {
return request_command(socket, Request::PostAuthMessageResponse { response });
}
Message::Login(socket) => {
match self.flags.sessions.get(&self.selected_session).cloned() {
Some(cmd) => {
return request_command(
socket,
Request::StartSession {
cmd,
env: Vec::new(),
},
);
}
None => todo!("session {:?} not found", self.selected_session),
}
}
2023-11-29 08:05:09 -07:00
Message::Suspend => {
#[cfg(feature = "logind")]
return Command::perform(
async move {
match crate::logind::suspend().await {
Ok(()) => message::none(),
Err(err) => {
log::error!("failed to suspend: {:?}", err);
message::app(Message::Error(err.to_string()))
}
}
},
|x| x,
);
}
Message::Error(error) => {
self.error_opt = Some(error);
}
2023-10-05 17:47:23 -06:00
Message::Exit => {
2024-02-06 10:58:34 -07:00
let mut commands = Vec::new();
for (_output, surface_id) in self.surface_ids.drain() {
2024-02-06 15:03:07 -07:00
self.surface_images.remove(&surface_id);
2024-02-06 10:58:34 -07:00
self.surface_names.remove(&surface_id);
self.text_input_ids.remove(&surface_id);
commands.push(destroy_layer_surface(surface_id));
}
commands.push(Command::perform(async { process::exit(0) }, |x| x));
return Command::batch(commands);
2023-10-05 17:47:23 -06:00
}
}
Command::none()
}
2024-02-06 10:58:34 -07:00
// Not used for layer surface window
2023-10-05 17:47:23 -06:00
fn view(&self) -> Element<Self::Message> {
2024-02-06 10:58:34 -07:00
unimplemented!()
}
/// Creates a view after each update.
fn view_window(&self, surface_id: SurfaceId) -> Element<Self::Message> {
let left_element = {
let date_time_column = {
let mut column = widget::column::with_capacity(2).padding(16.0).spacing(12.0);
let dt = chrono::Local::now();
//TODO: localized format
let date = dt.format("%A, %B %-d");
column = column
.push(widget::text::title2(format!("{}", date)).style(style::Text::Accent));
//TODO: localized format
let time = dt.format("%R");
column = column.push(
widget::text(format!("{}", time))
.size(112.0)
.style(style::Text::Accent),
);
column
};
2024-02-06 10:58:34 -07:00
let mut status_row = widget::row::with_capacity(2).padding(16.0).spacing(12.0);
if let Some(network_icon) = self.network_icon_opt {
status_row = status_row.push(widget::icon::from_name(network_icon));
}
if let Some((power_icon, power_percent)) = &self.power_info_opt {
status_row = status_row.push(iced::widget::row![
widget::icon::from_name(power_icon.clone()),
widget::text(format!("{:.0}%", power_percent)),
]);
}
//TODO: implement these buttons
let button_row = iced::widget::row![
widget::button(widget::icon::from_name(
"applications-accessibility-symbolic"
))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("input-keyboard-symbolic"))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("system-users-symbolic"))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("application-menu-symbolic"))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("system-suspend-symbolic"))
.padding(12.0)
2023-11-29 08:05:09 -07:00
.on_press(Message::Suspend),
widget::button(widget::icon::from_name("system-reboot-symbolic"))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("system-shutdown-symbolic"))
.padding(12.0)
.on_press(Message::None),
]
.padding([16.0, 0.0, 0.0, 0.0])
.spacing(8.0);
widget::container(iced::widget::column![
date_time_column,
widget::divider::horizontal::default(),
status_row,
widget::divider::horizontal::default(),
button_row,
])
.width(Length::Fill)
.align_x(alignment::Horizontal::Left)
};
let right_element = {
let mut column = widget::column::with_capacity(2)
.spacing(12.0)
.max_width(280.0);
match &self.socket_state {
SocketState::Pending => {
column = column.push(widget::text("Opening GREETD_SOCK"));
}
SocketState::Open(socket) => {
match &self.username_opt {
Some(username) => {
2024-02-06 15:03:07 -07:00
for user_data in &self.flags.user_datas {
if &user_data.name == username {
match &user_data.icon_opt {
Some(icon) => {
column = column.push(
widget::container(
2024-02-06 15:03:07 -07:00
widget::Image::new(
//TODO: cache handle
widget::image::Handle::from_memory(
icon.clone(),
),
)
.width(Length::Fixed(78.0))
.height(Length::Fixed(78.0)),
)
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
)
}
None => {}
}
2024-02-06 15:03:07 -07:00
match &user_data.full_name_opt {
Some(full_name) => {
column = column.push(
2023-10-25 10:05:13 -06:00
widget::container(widget::text::title4(full_name))
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
);
}
None => {}
}
}
2023-10-05 17:47:23 -06:00
}
}
None => {
2024-02-06 15:03:07 -07:00
let mut row = widget::row::with_capacity(self.flags.user_datas.len())
.spacing(12.0);
for user_data in &self.flags.user_datas {
let mut column = widget::column::with_capacity(2).spacing(12.0);
2024-02-06 15:03:07 -07:00
match &user_data.icon_opt {
Some(icon) => {
column = column.push(
widget::container(
2024-02-06 15:03:07 -07:00
widget::Image::new(
//TODO: cache handle
widget::image::Handle::from_memory(
icon.clone(),
),
)
.width(Length::Fixed(78.0))
.height(Length::Fixed(78.0)),
)
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
)
}
None => {}
}
2024-02-06 15:03:07 -07:00
match &user_data.full_name_opt {
Some(full_name) => {
column = column.push(
2023-10-25 10:05:13 -06:00
widget::container(widget::text::title4(full_name))
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
);
}
None => {}
}
row = row.push(
widget::MouseArea::new(
widget::cosmic_container::container(column)
.layer(cosmic::cosmic_theme::Layer::Primary)
.padding(16)
.style(cosmic::theme::Container::Card),
)
2024-02-06 15:03:07 -07:00
.on_press(
Message::Username(socket.clone(), user_data.name.clone()),
),
);
2023-10-05 17:47:23 -06:00
}
column = column.push(row);
2023-10-05 17:47:23 -06:00
}
}
match &self.prompt_opt {
Some((prompt, secret, value_opt)) => match value_opt {
Some(value) => {
2024-01-17 09:37:15 -07:00
let mut text_input =
widget::text_input(prompt.clone(), value.clone())
.leading_icon(
widget::icon::from_name("system-lock-screen-symbolic")
.into(),
)
.trailing_icon(
widget::icon::from_name("document-properties-symbolic")
.into(),
)
.on_input(|value| {
Message::Prompt(prompt.clone(), *secret, Some(value))
})
.on_submit(Message::Auth(
socket.clone(),
Some(value.clone()),
));
2024-02-06 10:58:34 -07:00
if let Some(text_input_id) = self.text_input_ids.get(&surface_id) {
text_input = text_input.id(text_input_id.clone());
}
if *secret {
text_input = text_input.password()
}
2023-10-05 17:47:23 -06:00
column = column.push(text_input);
}
None => {
column = column.push(
widget::button("Confirm")
.on_press(Message::Auth(socket.clone(), None)),
);
}
},
None => {}
2023-10-05 17:47:23 -06:00
}
}
SocketState::NotSet => {
column = column.push(widget::text("GREETD_SOCK variable not set"));
}
SocketState::Error(err) => {
column = column.push(widget::text(format!(
"Failed to open GREETD_SOCK: {:?}",
err
)))
}
2023-10-05 17:47:23 -06:00
}
if let Some(error) = &self.error_opt {
column = column.push(widget::text(error));
}
2023-10-05 17:47:23 -06:00
column = column.push(
//TODO: use button
2023-10-26 15:10:09 -07:00
iced::widget::pick_list(
&self.session_names,
Some(self.selected_session.clone()),
Message::Session,
),
);
widget::container(column)
.align_x(alignment::Horizontal::Center)
.width(Length::Fill)
};
2023-10-05 17:47:23 -06:00
crate::image_container::ImageContainer::new(
widget::container(
widget::cosmic_container::container(
iced::widget::row![left_element, right_element]
.align_items(alignment::Alignment::Center),
)
.layer(cosmic::cosmic_theme::Layer::Background)
.padding(16)
.style(cosmic::theme::Container::Custom(Box::new(
|theme: &cosmic::Theme| {
// Use background appearance as the base
let mut appearance = widget::container::StyleSheet::appearance(
theme,
&cosmic::theme::Container::Background,
);
appearance.border_radius = 16.0.into();
appearance
},
)))
.width(Length::Fixed(800.0)),
)
.padding([32.0, 0.0, 0.0, 0.0])
.width(Length::Fill)
.height(Length::Fill)
.align_x(alignment::Horizontal::Center)
.align_y(alignment::Vertical::Top)
.style(cosmic::theme::Container::Transparent),
)
2024-02-06 15:03:07 -07:00
.image(match self.surface_images.get(&surface_id) {
Some(some) => some.clone(),
None => self.flags.fallback_background.clone(),
})
2023-10-06 20:24:03 -06:00
.content_fit(iced::ContentFit::Cover)
.into()
2023-10-05 17:47:23 -06:00
}
2023-11-21 15:48:22 -07:00
2024-02-06 10:58:34 -07:00
fn subscription(&self) -> Subscription<Self::Message> {
2023-11-21 15:48:22 -07:00
struct HeartbeatSubscription;
2024-02-06 10:58:34 -07:00
//TODO: just use one vec for all subscriptions
let mut extra_suscriptions = Vec::with_capacity(2);
#[cfg(feature = "networkmanager")]
{
extra_suscriptions.push(
crate::networkmanager::subscription()
.map(|icon_opt| Message::NetworkIcon(icon_opt)),
);
}
#[cfg(feature = "upower")]
{
extra_suscriptions
.push(crate::upower::subscription().map(|info_opt| Message::PowerInfo(info_opt)));
}
Subscription::batch([
event::listen_with(|event, _| match event {
iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(
wayland_event,
)) => match wayland_event {
WaylandEvent::Output(output_event, output) => {
Some(Message::OutputEvent(output_event, output))
}
WaylandEvent::Layer(layer_event, _surface, surface_id) => {
Some(Message::LayerEvent(layer_event, surface_id))
}
_ => None,
},
_ => None,
}),
subscription::channel(
std::any::TypeId::of::<HeartbeatSubscription>(),
16,
|mut msg_tx| async move {
loop {
// Send heartbeat once a second to update time
//TODO: only send this when needed
msg_tx.send(Message::None).await.unwrap();
time::sleep(time::Duration::new(1, 0)).await;
}
},
),
Subscription::batch(extra_suscriptions),
])
2023-11-21 15:48:22 -07:00
}
2023-10-05 17:47:23 -06:00
}