Saved accounts on the login screen show their profile picture, kept on disk beside the account and dropped on forget or removal
Browse files at this commitf2d084f9f0905061ec6316154a48cbf2b3254cbdParent: 23c2ec5- fix
- saved accounts on the login screen show their profile picture, kept on disk beside the account and dropped on forget or removal
4 changed files +70 -7
- crates/client/src/accounts.rs +34 -2
- crates/client/src/gui.rs +15 -0
- crates/client/src/gui/auth.rs +9 -3
- crates/client/src/lib.rs +12 -2
crates/client/src/accounts.rsmodified+34 -2
| @@ -1,12 +1,16 @@ | |||
| 1 | +use std::sync::Arc; | ||
| 2 | + | ||
| 1 | use common::model::{Id, User}; | 3 | use common::model::{Id, User}; |
| 4 | +use gpui_kit::{Image, ImageFormat}; | ||
| 2 | 5 | ||
| 3 | -use crate::{read_config, write_config}; | 6 | +use crate::{read_config, read_config_bytes, remove_config, write_config}; |
| 4 | 7 | ||
| 5 | -#[derive(Debug, Clone, PartialEq, Eq)] | 8 | +#[derive(Debug, Clone)] |
| 6 | pub struct Account { | 9 | pub struct Account { |
| 7 | pub user_id: Id, | 10 | pub user_id: Id, |
| 8 | pub username: String, | 11 | pub username: String, |
| 9 | pub token: String, | 12 | pub token: String, |
| 13 | + pub picture: Option<Arc<Image>>, | ||
| 10 | } | 14 | } |
| 11 | 15 | ||
| 12 | #[derive(Debug, Clone, Default)] | 16 | #[derive(Debug, Clone, Default)] |
| @@ -33,6 +37,7 @@ impl Accounts { | |||
| 33 | user_id: user.id, | 37 | user_id: user.id, |
| 34 | username: user.username.clone(), | 38 | username: user.username.clone(), |
| 35 | token, | 39 | token, |
| 40 | + picture: picture(&self.server, user.id), | ||
| 36 | }); | 41 | }); |
| 37 | self.active = Some(user.id); | 42 | self.active = Some(user.id); |
| 38 | save(self); | 43 | save(self); |
| @@ -58,7 +63,24 @@ impl Accounts { | |||
| 58 | save(self); | 63 | save(self); |
| 59 | } | 64 | } |
| 60 | 65 | ||
| 66 | + pub fn set_picture(&mut self, user_id: Id, bytes: Option<Vec<u8>>) { | ||
| 67 | + let name = picture_file(&self.server, user_id); | ||
| 68 | + match &bytes { | ||
| 69 | + Some(bytes) => write_config(&name, bytes), | ||
| 70 | + None => remove_config(&name), | ||
| 71 | + } | ||
| 72 | + if let Some(account) = self | ||
| 73 | + .list | ||
| 74 | + .iter_mut() | ||
| 75 | + .find(|account| account.user_id == user_id) | ||
| 76 | + { | ||
| 77 | + account.picture = | ||
| 78 | + bytes.map(|bytes| Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes))); | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 61 | pub fn forget(&mut self, user_id: Id) { | 82 | pub fn forget(&mut self, user_id: Id) { |
| 83 | + remove_config(&picture_file(&self.server, user_id)); | ||
| 62 | self.remove(user_id); | 84 | self.remove(user_id); |
| 63 | if self.active == Some(user_id) { | 85 | if self.active == Some(user_id) { |
| 64 | self.active = None; | 86 | self.active = None; |
| @@ -85,6 +107,15 @@ fn file(server: &str) -> String { | |||
| 85 | format!("accounts-{server}") | 107 | format!("accounts-{server}") |
| 86 | } | 108 | } |
| 87 | 109 | ||
| 110 | +fn picture_file(server: &str, user_id: Id) -> String { | ||
| 111 | + format!("{}-{user_id}.jpg", file(server)) | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +fn picture(server: &str, user_id: Id) -> Option<Arc<Image>> { | ||
| 115 | + read_config_bytes(&picture_file(server, user_id)) | ||
| 116 | + .map(|bytes| Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes))) | ||
| 117 | +} | ||
| 118 | + | ||
| 88 | pub fn load(server: &str) -> Accounts { | 119 | pub fn load(server: &str) -> Accounts { |
| 89 | let mut accounts = Accounts { | 120 | let mut accounts = Accounts { |
| 90 | server: server.to_string(), | 121 | server: server.to_string(), |
| @@ -103,6 +134,7 @@ pub fn load(server: &str) -> Accounts { | |||
| 103 | user_id, | 134 | user_id, |
| 104 | username: username.to_string(), | 135 | username: username.to_string(), |
| 105 | token: token.to_string(), | 136 | token: token.to_string(), |
| 137 | + picture: picture(server, user_id), | ||
| 106 | }); | 138 | }); |
| 107 | } | 139 | } |
| 108 | } | 140 | } |
crates/client/src/gui.rsmodified+15 -0
| @@ -947,6 +947,13 @@ impl Yumush { | |||
| 947 | yumush.chat.avatar_requests.remove(&avatar_id); | 947 | yumush.chat.avatar_requests.remove(&avatar_id); |
| 948 | if let Ok(bytes) = result { | 948 | if let Ok(bytes) = result { |
| 949 | style::picture(avatar_id, &bytes); | 949 | style::picture(avatar_id, &bytes); |
| 950 | + if let Some(me) = yumush | ||
| 951 | + .user | ||
| 952 | + .as_ref() | ||
| 953 | + .filter(|me| me.avatar_id == Some(avatar_id)) | ||
| 954 | + { | ||
| 955 | + yumush.accounts.set_picture(me.id, Some(bytes.clone())); | ||
| 956 | + } | ||
| 950 | yumush.chat.avatars.insert( | 957 | yumush.chat.avatars.insert( |
| 951 | avatar_id, | 958 | avatar_id, |
| 952 | Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes)), | 959 | Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes)), |
| @@ -959,6 +966,14 @@ impl Yumush { | |||
| 959 | } | 966 | } |
| 960 | 967 | ||
| 961 | pub fn request_user_avatar(&mut self, user: &User, cx: &mut Context<Self>) { | 968 | pub fn request_user_avatar(&mut self, user: &User, cx: &mut Context<Self>) { |
| 969 | + if user.avatar_id.is_none() | ||
| 970 | + && self | ||
| 971 | + .accounts | ||
| 972 | + .get(user.id) | ||
| 973 | + .is_some_and(|account| account.picture.is_some()) | ||
| 974 | + { | ||
| 975 | + self.accounts.set_picture(user.id, None); | ||
| 976 | + } | ||
| 962 | if let Some(avatar_id) = user.avatar_id { | 977 | if let Some(avatar_id) = user.avatar_id { |
| 963 | self.request_avatar(avatar_id, cx); | 978 | self.request_avatar(avatar_id, cx); |
| 964 | } | 979 | } |
crates/client/src/gui/auth.rsmodified+9 -3
| @@ -293,8 +293,14 @@ impl Yumush { | |||
| 293 | .items_center() | 293 | .items_center() |
| 294 | .gap(px(space::SM)) | 294 | .gap(px(space::SM)) |
| 295 | .rounded(px(radius::MD)) | 295 | .rounded(px(radius::MD)) |
| 296 | - .child( | 296 | + .child(match &account.picture { |
| 297 | - badge( | 297 | + Some(picture) => div() |
| 298 | + .size(px(size::CHIP)) | ||
| 299 | + .flex_shrink_0() | ||
| 300 | + .rounded_full() | ||
| 301 | + .overflow_hidden() | ||
| 302 | + .child(gpui_kit::img(picture.clone()).size_full()), | ||
| 303 | + None => badge( | ||
| 298 | palette, | 304 | palette, |
| 299 | account.user_id, | 305 | account.user_id, |
| 300 | &account.username, | 306 | &account.username, |
| @@ -302,7 +308,7 @@ impl Yumush { | |||
| 302 | text(text::CAPTION), | 308 | text(text::CAPTION), |
| 303 | ) | 309 | ) |
| 304 | .rounded_full(), | 310 | .rounded_full(), |
| 305 | - ) | 311 | + }) |
| 306 | .child(div().flex_1().truncate().child(account.username.clone())) | 312 | .child(div().flex_1().truncate().child(account.username.clone())) |
| 307 | .child( | 313 | .child( |
| 308 | clickable( | 314 | clickable( |
crates/client/src/lib.rsmodified+12 -2
| @@ -35,7 +35,17 @@ pub fn read_config(name: &str) -> Option<String> { | |||
| 35 | fs::read_to_string(config_file(name)?).ok() | 35 | fs::read_to_string(config_file(name)?).ok() |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | -pub fn write_config(name: &str, content: &str) { | 38 | +pub fn read_config_bytes(name: &str) -> Option<Vec<u8>> { |
| 39 | + fs::read(config_file(name)?).ok() | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +pub fn remove_config(name: &str) { | ||
| 43 | + if let Some(path) = config_file(name) { | ||
| 44 | + let _ = fs::remove_file(path); | ||
| 45 | + } | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +pub fn write_config(name: &str, content: impl AsRef<[u8]>) { | ||
| 39 | let Some(path) = config_file(name) else { | 49 | let Some(path) = config_file(name) else { |
| 40 | return; | 50 | return; |
| 41 | }; | 51 | }; |
| @@ -47,7 +57,7 @@ pub fn write_config(name: &str, content: &str) { | |||
| 47 | options.write(true).create(true).truncate(true); | 57 | options.write(true).create(true).truncate(true); |
| 48 | #[cfg(unix)] | 58 | #[cfg(unix)] |
| 49 | std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); | 59 | std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); |
| 50 | - options.open(&path)?.write_all(content.as_bytes()) | 60 | + options.open(&path)?.write_all(content.as_ref()) |
| 51 | }); | 61 | }); |
| 52 | 62 | ||
| 53 | if let Err(error) = result { | 63 | if let Err(error) = result { |