yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use crate::gui::glyph::Fill as _; |
| 2 | use crate::lang::{Label as _, w}; |
| 3 | use common::model::{Hue, Id}; |
| 4 | use std::{collections::HashMap, fmt::Display, rc::Rc, sync::Arc, time::Instant}; |
| 5 | |
| 6 | use common::{ |
| 7 | call::{GetAvatar, GetSettings, MarkRead}, |
| 8 | constant::{MAXIMUM_AVATAR_BYTES, MAXIMUM_MESSAGE_LENGTH}, |
| 9 | error::Error, |
| 10 | event::Event, |
| 11 | model::{ |
| 12 | BackupInfo, CallLog, Community, CommunityKind, Message, Presence, Rank, Sanction, |
| 13 | ServerSettings, Unread, User, mentions, |
| 14 | }, |
| 15 | }; |
| 16 | use gpui_kit::{ |
| 17 | AnyElement, AnyWindowHandle, App, AppContext, AsyncWindowContext, Context, Div, ElementId, |
| 18 | Entity, FontWeight, Hsla, Image, ImageFormat, InteractiveElement, IntoElement, KeyBinding, |
| 19 | KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, ParentElement, Pixels, Rems, Render, |
| 20 | RenderOnce, Stateful, StatefulInteractiveElement, Styled, WeakEntity, Window, actions, |
| 21 | assets::IconName, |
| 22 | base::Selectable, |
| 23 | component::{ |
| 24 | Icon, Sizable, Theme, ThemeRegistry, WindowExt, |
| 25 | input::{Input, InputEvent, InputState}, |
| 26 | notification::Notification, |
| 27 | spinner::Spinner, |
| 28 | }, |
| 29 | div, img, |
| 30 | prelude::FluentBuilder as _, |
| 31 | px, rems, |
| 32 | }; |
| 33 | use jiff::{Timestamp, tz::TimeZone}; |
| 34 | use tokio::sync::mpsc; |
| 35 | |
| 36 | use crate::{ |
| 37 | accounts::{self, Accounts}, |
| 38 | marks::{self, Mark}, |
| 39 | net::{Fail, LinkError, NetworkEvent, NetworkHandle}, |
| 40 | settings::{Scale, Settings}, |
| 41 | }; |
| 42 | |
| 43 | mod account_list; |
| 44 | mod auth; |
| 45 | mod builder; |
| 46 | mod call_bar; |
| 47 | mod camera_view; |
| 48 | mod chat; |
| 49 | mod community_card; |
| 50 | mod community_settings; |
| 51 | mod composer; |
| 52 | mod dock; |
| 53 | mod forward; |
| 54 | pub mod gallery; |
| 55 | pub mod glyph; |
| 56 | mod jump; |
| 57 | mod media_view; |
| 58 | mod message_actions; |
| 59 | mod parts; |
| 60 | mod poll; |
| 61 | mod profile; |
| 62 | mod settings_page; |
| 63 | mod share_view; |
| 64 | mod sidebar; |
| 65 | mod snippet; |
| 66 | mod social; |
| 67 | mod stream_stats; |
| 68 | pub mod style; |
| 69 | mod surface; |
| 70 | mod tow; |
| 71 | mod update_view; |
| 72 | mod voice_note; |
| 73 | mod voice_settings; |
| 74 | mod voice_stats; |
| 75 | mod wave; |
| 76 | mod widget; |
| 77 | |
| 78 | use auth::Auth; |
| 79 | use chat::{Chat, View}; |
| 80 | use style::{Tokens, radius, size, space}; |
| 81 | use voice_settings::VoiceControls; |
| 82 | |
| 83 | actions!(yumush, [FocusFind, CloseSettings]); |
| 84 | |
| 85 | pub fn text(size: f32) -> Rems { |
| 86 | rems(size / style::text::UI) |
| 87 | } |
| 88 | |
| 89 | #[derive(Clone, Copy)] |
| 90 | pub struct Palette { |
| 91 | pub window: Hsla, |
| 92 | pub panel: Hsla, |
| 93 | pub field: Hsla, |
| 94 | pub border: Hsla, |
| 95 | pub text: Hsla, |
| 96 | pub body: Hsla, |
| 97 | pub muted: Hsla, |
| 98 | pub hover: Hsla, |
| 99 | pub accent: Hsla, |
| 100 | pub on_accent: Hsla, |
| 101 | pub ok: Hsla, |
| 102 | pub danger: Hsla, |
| 103 | pub alert: Hsla, |
| 104 | pub screen: Hsla, |
| 105 | pub camera: Hsla, |
| 106 | pub dark: bool, |
| 107 | pub tokens: Tokens, |
| 108 | } |
| 109 | |
| 110 | impl Palette { |
| 111 | pub fn tinted(self, id: Id) -> Hsla { |
| 112 | self.tokens.person(id) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub fn palette(cx: &App) -> Palette { |
| 117 | let tokens = Tokens::of(cx); |
| 118 | Palette { |
| 119 | window: tokens.ground(), |
| 120 | panel: tokens.ground(), |
| 121 | field: tokens.raise(), |
| 122 | border: tokens.thread(), |
| 123 | text: tokens.paper(), |
| 124 | body: tokens.paper(), |
| 125 | muted: tokens.dim(), |
| 126 | hover: tokens.hov(), |
| 127 | accent: tokens.paper(), |
| 128 | on_accent: tokens.ground(), |
| 129 | ok: tokens.hue(Hue::Mint), |
| 130 | danger: tokens.bad(), |
| 131 | alert: tokens.bad(), |
| 132 | screen: tokens.hue(Hue::Sky), |
| 133 | camera: tokens.hue(Hue::Amber), |
| 134 | dark: tokens.dark(), |
| 135 | tokens, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | const FONT: &str = "Onest"; |
| 140 | const MONO_FONT: &str = "Adwaita Mono"; |
| 141 | |
| 142 | pub fn init(cx: &mut App) { |
| 143 | let fonts = [ |
| 144 | include_bytes!("../assets/fonts/Onest-Regular.ttf").as_slice(), |
| 145 | include_bytes!("../assets/fonts/Onest-Medium.ttf"), |
| 146 | include_bytes!("../assets/fonts/Onest-SemiBold.ttf"), |
| 147 | include_bytes!("../assets/fonts/Onest-Bold.ttf"), |
| 148 | include_bytes!("../assets/fonts/AdwaitaMono-Regular.ttf"), |
| 149 | include_bytes!("../assets/fonts/AdwaitaMono-Bold.ttf"), |
| 150 | ]; |
| 151 | let fonts = fonts.into_iter().map(std::borrow::Cow::Borrowed).collect(); |
| 152 | if let Err(error) = cx.text_system().add_fonts(fonts) { |
| 153 | tracing::warn!("fonts: {error}"); |
| 154 | } |
| 155 | let mut theme = Theme::default(); |
| 156 | theme.font_family = FONT.into(); |
| 157 | theme.mono_font_family = MONO_FONT.into(); |
| 158 | cx.set_global(theme); |
| 159 | gpui_kit::init(cx); |
| 160 | let registry = ThemeRegistry::global(cx); |
| 161 | let (light, dark) = ( |
| 162 | registry.default_light_theme().clone(), |
| 163 | registry.default_dark_theme().clone(), |
| 164 | ); |
| 165 | let theme = Theme::global_mut(cx); |
| 166 | theme.light_theme = light; |
| 167 | theme.dark_theme = dark; |
| 168 | } |
| 169 | |
| 170 | pub fn sync_theme(cx: &mut App) { |
| 171 | let palette = palette(cx); |
| 172 | let font_size = cx.global::<Scale>().0.pixels(); |
| 173 | let theme = Theme::global_mut(cx); |
| 174 | theme.font_family = FONT.into(); |
| 175 | theme.mono_font_family = MONO_FONT.into(); |
| 176 | let colors = &mut theme.colors; |
| 177 | |
| 178 | colors.background = palette.panel; |
| 179 | colors.foreground = palette.text; |
| 180 | colors.popover = palette.panel; |
| 181 | colors.popover_foreground = palette.text; |
| 182 | colors.border = palette.border; |
| 183 | colors.input = palette.border; |
| 184 | colors.muted = palette.field; |
| 185 | colors.muted_foreground = palette.muted; |
| 186 | colors.accent = palette.hover; |
| 187 | colors.accent_foreground = palette.text; |
| 188 | colors.list_hover = palette.hover; |
| 189 | colors.secondary = palette.field; |
| 190 | colors.secondary_foreground = palette.text; |
| 191 | colors.primary = palette.accent; |
| 192 | colors.primary_hover = palette.accent; |
| 193 | colors.primary_active = palette.accent; |
| 194 | colors.primary_foreground = palette.on_accent; |
| 195 | colors.ring = palette.accent; |
| 196 | colors.caret = palette.accent; |
| 197 | colors.danger = palette.danger; |
| 198 | colors.slider_bar = palette.text; |
| 199 | colors.slider_thumb = palette.text; |
| 200 | theme.tokens = (&theme.colors).into(); |
| 201 | theme.radius = px(radius::SM); |
| 202 | theme.radius_lg = px(radius::LG); |
| 203 | theme.font_size = font_size; |
| 204 | theme.focus_ring = false; |
| 205 | } |
| 206 | |
| 207 | const FIND_KEY: &str = "ctrl-k"; |
| 208 | pub const JUMP_HINT: &str = "Ctrl K"; |
| 209 | |
| 210 | pub fn badge(palette: Palette, id: Id, name: &str, size: Pixels, font: Rems) -> Div { |
| 211 | let initial = name |
| 212 | .chars() |
| 213 | .next() |
| 214 | .unwrap_or('?') |
| 215 | .to_uppercase() |
| 216 | .to_string(); |
| 217 | |
| 218 | div() |
| 219 | .size(size) |
| 220 | .flex_shrink_0() |
| 221 | .flex() |
| 222 | .items_center() |
| 223 | .justify_center() |
| 224 | .bg(palette.tinted(id)) |
| 225 | .text_color(palette.on_accent) |
| 226 | .text_size(font) |
| 227 | .font_weight(FontWeight::SEMIBOLD) |
| 228 | .child(initial) |
| 229 | } |
| 230 | |
| 231 | pub fn avatar( |
| 232 | palette: Palette, |
| 233 | user: &User, |
| 234 | size: Pixels, |
| 235 | font: Rems, |
| 236 | avatars: &HashMap<Id, Arc<Image>>, |
| 237 | ) -> Div { |
| 238 | let Some(image) = user.avatar_id.as_ref().and_then(|id| avatars.get(id)) else { |
| 239 | return badge(palette, user.id, &user.username, size, font).rounded_full(); |
| 240 | }; |
| 241 | |
| 242 | div() |
| 243 | .size(size) |
| 244 | .flex_shrink_0() |
| 245 | .rounded_full() |
| 246 | .overflow_hidden() |
| 247 | .child(img(image.clone()).size_full().rounded_full()) |
| 248 | } |
| 249 | |
| 250 | pub fn picture_of(community: &Community, avatars: &HashMap<Id, Arc<Image>>) -> Option<Arc<Image>> { |
| 251 | let avatar_id = match &community.kind { |
| 252 | CommunityKind::Group => community.avatar_id, |
| 253 | CommunityKind::Direct { peer } => peer.avatar_id, |
| 254 | CommunityKind::Notes => None, |
| 255 | }; |
| 256 | avatar_id.and_then(|id| avatars.get(&id)).cloned() |
| 257 | } |
| 258 | |
| 259 | pub struct Zoom { |
| 260 | image: Arc<Image>, |
| 261 | round: bool, |
| 262 | name: String, |
| 263 | } |
| 264 | |
| 265 | impl Zoom { |
| 266 | pub fn new(image: Option<Arc<Image>>, round: bool, name: String) -> Option<Self> { |
| 267 | image.map(|image| Self { image, round, name }) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | pub fn zoomable( |
| 272 | entity: &Entity<Yumush>, |
| 273 | id: impl Into<ElementId>, |
| 274 | face: Div, |
| 275 | zoom: Option<Zoom>, |
| 276 | ) -> Stateful<Div> { |
| 277 | let face = face.id(id); |
| 278 | let Some(zoom) = zoom.map(Rc::new) else { |
| 279 | return face; |
| 280 | }; |
| 281 | let entity = entity.clone(); |
| 282 | face.cursor_pointer().on_click(move |_, _, cx| { |
| 283 | let zoom = zoom.clone(); |
| 284 | entity.update(cx, |yumush, cx| { |
| 285 | yumush.zoom = Some(zoom); |
| 286 | cx.notify(); |
| 287 | }); |
| 288 | }) |
| 289 | } |
| 290 | |
| 291 | fn zoom_view(zoom: &Zoom, entity: &Entity<Yumush>, t: Tokens) -> Stateful<Div> { |
| 292 | let entity = entity.clone(); |
| 293 | let picture = img(zoom.image.clone()).size(px(size::MEDIA)); |
| 294 | div() |
| 295 | .id("zoom") |
| 296 | .absolute() |
| 297 | .inset_0() |
| 298 | .occlude() |
| 299 | .bg(t.scrim()) |
| 300 | .flex() |
| 301 | .flex_col() |
| 302 | .items_center() |
| 303 | .justify_center() |
| 304 | .gap(px(space::LG)) |
| 305 | .on_click(move |_, _, cx| { |
| 306 | entity.update(cx, |yumush, cx| { |
| 307 | yumush.zoom = None; |
| 308 | cx.notify(); |
| 309 | }) |
| 310 | }) |
| 311 | .child(if zoom.round { |
| 312 | picture.rounded_full() |
| 313 | } else { |
| 314 | picture.rounded(px(radius::LG)) |
| 315 | }) |
| 316 | .child(parts::words( |
| 317 | zoom.name.clone(), |
| 318 | parts::Text::Title, |
| 319 | parts::Ink::Paper, |
| 320 | t, |
| 321 | )) |
| 322 | } |
| 323 | |
| 324 | pub fn community_name(community: &Community) -> &str { |
| 325 | match &community.kind { |
| 326 | CommunityKind::Group => &community.name, |
| 327 | CommunityKind::Direct { peer } if community.name.is_empty() => &peer.username, |
| 328 | CommunityKind::Direct { .. } => &community.name, |
| 329 | CommunityKind::Notes => w().gui.notes, |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | pub fn community_icon( |
| 334 | palette: Palette, |
| 335 | community: &Community, |
| 336 | size: Pixels, |
| 337 | font: Rems, |
| 338 | avatars: &HashMap<Id, Arc<Image>>, |
| 339 | ) -> Div { |
| 340 | match &community.kind { |
| 341 | CommunityKind::Group => match community.avatar_id.as_ref().and_then(|id| avatars.get(id)) { |
| 342 | Some(image) => div() |
| 343 | .size(size) |
| 344 | .flex_shrink_0() |
| 345 | .rounded(px(radius::SM)) |
| 346 | .overflow_hidden() |
| 347 | .child(img(image.clone()).size_full().rounded(px(radius::SM))), |
| 348 | None => { |
| 349 | badge(palette, community.id, &community.name, size, font).rounded(px(radius::SM)) |
| 350 | } |
| 351 | }, |
| 352 | CommunityKind::Direct { peer } => avatar(palette, peer, size, font, avatars), |
| 353 | CommunityKind::Notes => { |
| 354 | badge(palette, community.id, w().gui.notes, size, font).rounded(px(radius::SM)) |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | pub const KB: f64 = 1024.0; |
| 360 | pub const MB: f64 = KB * 1024.0; |
| 361 | const GB: f64 = MB * 1024.0; |
| 362 | |
| 363 | pub const CLOCK: &str = "%H:%M"; |
| 364 | pub const CLOCK_SECONDS: &str = "%H:%M:%S"; |
| 365 | |
| 366 | pub fn storage(bytes: u64) -> String { |
| 367 | let bytes = bytes as f64; |
| 368 | if bytes >= GB { |
| 369 | format!("{:.1} GB", bytes / GB) |
| 370 | } else if bytes >= MB { |
| 371 | format!("{:.1} MB", bytes / MB) |
| 372 | } else { |
| 373 | format!("{:.1} KB", bytes / KB) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | pub fn millis_time(millis: u64, format: &str) -> String { |
| 378 | let Ok(timestamp) = Timestamp::from_millisecond(millis as i64) else { |
| 379 | return String::new(); |
| 380 | }; |
| 381 | |
| 382 | let zoned = timestamp.to_zoned_datetime(TimeZone::system()); |
| 383 | zoned |
| 384 | .strftime(&month_named(format, zoned.month())) |
| 385 | .to_string() |
| 386 | } |
| 387 | |
| 388 | pub fn month_named(format: &str, month: i8) -> String { |
| 389 | let name = crate::lang::w() |
| 390 | .common |
| 391 | .months |
| 392 | .split(' ') |
| 393 | .nth(month as usize - 1) |
| 394 | .unwrap_or_default(); |
| 395 | format.replace("%b", name) |
| 396 | } |
| 397 | |
| 398 | pub use parts::motion; |
| 399 | |
| 400 | pub fn hoverable(id: impl Into<ElementId>, element: Div, palette: Palette) -> Stateful<Div> { |
| 401 | hoverable_with(id, element, palette.hover) |
| 402 | } |
| 403 | |
| 404 | pub fn hoverable_with(id: impl Into<ElementId>, element: Div, hover: Hsla) -> Stateful<Div> { |
| 405 | element |
| 406 | .id(id) |
| 407 | .cursor_pointer() |
| 408 | .hover(move |style| style.bg(hover)) |
| 409 | } |
| 410 | |
| 411 | pub fn clickable( |
| 412 | id: impl Into<ElementId>, |
| 413 | palette: Palette, |
| 414 | on_click: impl Fn(&mut Window, &mut App) + 'static, |
| 415 | ) -> Stateful<Div> { |
| 416 | hoverable(id, div(), palette).on_click(move |_, window, cx| on_click(window, cx)) |
| 417 | } |
| 418 | |
| 419 | pub fn entity_action<T: 'static>( |
| 420 | entity: &Entity<T>, |
| 421 | run: impl Fn(&mut T, &mut Window, &mut Context<T>) + 'static, |
| 422 | ) -> impl Fn(&mut Window, &mut App) + 'static { |
| 423 | let entity = entity.clone(); |
| 424 | |
| 425 | move |window, cx| entity.update(cx, |state, cx| run(state, window, cx)) |
| 426 | } |
| 427 | |
| 428 | pub struct Tip(&'static str); |
| 429 | |
| 430 | impl Render for Tip { |
| 431 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 432 | let palette = palette(cx); |
| 433 | div() |
| 434 | .px(px(space::SM)) |
| 435 | .py(px(space::XS)) |
| 436 | .rounded(px(radius::SM)) |
| 437 | .border_1() |
| 438 | .border_color(palette.border) |
| 439 | .bg(palette.field) |
| 440 | .text_color(palette.text) |
| 441 | .font_family(FONT) |
| 442 | .text_size(text(style::text::LABEL)) |
| 443 | .child(self.0) |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | pub trait Tooltip { |
| 448 | fn tip(self, text: &'static str) -> Self; |
| 449 | } |
| 450 | |
| 451 | impl Tooltip for Stateful<Div> { |
| 452 | fn tip(self, text: &'static str) -> Self { |
| 453 | self.tooltip(move |_, cx| cx.new(|_| Tip(text)).into()) |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | #[derive(IntoElement)] |
| 458 | pub struct Trigger(AnyElement); |
| 459 | |
| 460 | impl Trigger { |
| 461 | pub fn new(content: impl IntoElement) -> Self { |
| 462 | Self(content.into_any_element()) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | impl Selectable for Trigger { |
| 467 | fn selected(self, _: bool) -> Self { |
| 468 | self |
| 469 | } |
| 470 | |
| 471 | fn is_selected(&self) -> bool { |
| 472 | false |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | impl RenderOnce for Trigger { |
| 477 | fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { |
| 478 | self.0 |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | pub fn rank_tag(rank: Option<Rank>, palette: Palette) -> Option<Div> { |
| 483 | let tag = div() |
| 484 | .flex_shrink_0() |
| 485 | .text_size(text(style::text::CAPTION)) |
| 486 | .text_color(palette.muted); |
| 487 | match rank? { |
| 488 | Rank::Member => None, |
| 489 | Rank::Builder => Some( |
| 490 | tag.text_color(palette.accent) |
| 491 | .child(Icon::new(IconName::Shield).size(px(size::ICON_SM))), |
| 492 | ), |
| 493 | rank => Some(tag.child(rank.label())), |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | pub fn presence_color(presence: Presence, palette: Palette) -> Hsla { |
| 498 | match presence { |
| 499 | Presence::Online => palette.ok, |
| 500 | Presence::Busy => palette.danger, |
| 501 | Presence::Offline => palette.muted, |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | pub fn presence_avatar( |
| 506 | avatar: impl IntoElement, |
| 507 | presence: Presence, |
| 508 | size: Pixels, |
| 509 | palette: Palette, |
| 510 | ) -> Div { |
| 511 | let dot = if size > px(size::CHIP) { |
| 512 | px(space::SM + space::XXS) |
| 513 | } else { |
| 514 | px(space::SM) |
| 515 | }; |
| 516 | |
| 517 | div().relative().flex_shrink_0().child(avatar).child( |
| 518 | div() |
| 519 | .absolute() |
| 520 | .right(px(-space::XXS)) |
| 521 | .bottom(px(-space::XXS)) |
| 522 | .size(dot) |
| 523 | .rounded_full() |
| 524 | .border_2() |
| 525 | .border_color(palette.window) |
| 526 | .bg(presence_color(presence, palette)), |
| 527 | ) |
| 528 | } |
| 529 | |
| 530 | pub fn bind_keys(cx: &mut App) { |
| 531 | cx.bind_keys([ |
| 532 | KeyBinding::new(FIND_KEY, FocusFind, None), |
| 533 | KeyBinding::new("escape", CloseSettings, None), |
| 534 | ]); |
| 535 | composer::bind_keys(cx); |
| 536 | } |
| 537 | |
| 538 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 539 | enum Route { |
| 540 | Login, |
| 541 | Register, |
| 542 | Chat, |
| 543 | } |
| 544 | |
| 545 | #[derive(Debug, Clone, PartialEq)] |
| 546 | enum Connection { |
| 547 | Connecting, |
| 548 | Connected, |
| 549 | Lost { error: String, attempts: u32 }, |
| 550 | Failed(String), |
| 551 | } |
| 552 | |
| 553 | impl Connection { |
| 554 | fn is_connected(&self) -> bool { |
| 555 | matches!(self, Self::Connected) |
| 556 | } |
| 557 | |
| 558 | fn lost(&mut self, error: &LinkError) { |
| 559 | if error.terminal() { |
| 560 | *self = Self::Failed(error.to_string()); |
| 561 | return; |
| 562 | } |
| 563 | let attempts = match self { |
| 564 | Self::Lost { attempts, .. } => *attempts + 1, |
| 565 | _ => 1, |
| 566 | }; |
| 567 | *self = Self::Lost { |
| 568 | error: error.to_string(), |
| 569 | attempts, |
| 570 | }; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | fn connection_notice(connection: &Connection, palette: Palette) -> Option<Div> { |
| 575 | let (label, color, busy) = match connection { |
| 576 | Connection::Connecting => (w().gui.connecting.to_string(), palette.muted, true), |
| 577 | Connection::Connected => return None, |
| 578 | Connection::Lost { error, attempts } => { |
| 579 | (w().gui.reconnecting(attempts, error), palette.alert, true) |
| 580 | } |
| 581 | Connection::Failed(error) => (error.clone(), palette.alert, false), |
| 582 | }; |
| 583 | Some( |
| 584 | div() |
| 585 | .h(px(size::CHIP)) |
| 586 | .flex() |
| 587 | .items_center() |
| 588 | .justify_center() |
| 589 | .gap(px(space::SM)) |
| 590 | .text_size(text(style::text::CAPTION)) |
| 591 | .text_color(color) |
| 592 | .when(busy, |row| row.child(Spinner::new().xsmall().color(color))) |
| 593 | .child(label), |
| 594 | ) |
| 595 | } |
| 596 | |
| 597 | pub struct Yumush { |
| 598 | user: Option<User>, |
| 599 | current_route: Route, |
| 600 | auth: Auth, |
| 601 | chat: Chat, |
| 602 | network: NetworkHandle, |
| 603 | connection: Connection, |
| 604 | server: Option<ServerSettings>, |
| 605 | dismissed_announcement: Option<String>, |
| 606 | settings: Settings, |
| 607 | accounts: Accounts, |
| 608 | account_filter: Entity<InputState>, |
| 609 | voice: VoiceControls, |
| 610 | focus: gpui_kit::FocusHandle, |
| 611 | window: Option<AnyWindowHandle>, |
| 612 | deferred: Vec<NetworkEvent>, |
| 613 | backups: Vec<BackupInfo>, |
| 614 | pending_backup_delete: Option<String>, |
| 615 | update: crate::update::Phase, |
| 616 | desk: builder::releases::Desk, |
| 617 | log_search: Entity<InputState>, |
| 618 | log_scroll: gpui_kit::UniformListScrollHandle, |
| 619 | share_apps: Vec<crate::share::audio::AudioApp>, |
| 620 | share_apps_seen: std::cell::Cell<Instant>, |
| 621 | share_apps_task: Option<gpui_kit::Task<()>>, |
| 622 | wordmark_flips: u32, |
| 623 | wordmark_flipped: Option<Instant>, |
| 624 | zoom: Option<Rc<Zoom>>, |
| 625 | emoji_group: usize, |
| 626 | share_tab: common::share::Kind, |
| 627 | camera_modes: Vec<common::share::Mode>, |
| 628 | camera_devices: Vec<String>, |
| 629 | share_more: bool, |
| 630 | share_busy: bool, |
| 631 | fullscreen: bool, |
| 632 | pumping: bool, |
| 633 | pumped: Instant, |
| 634 | send_order: Arc<tokio::sync::Mutex<()>>, |
| 635 | } |
| 636 | |
| 637 | impl Yumush { |
| 638 | pub fn new( |
| 639 | network: NetworkHandle, |
| 640 | mut network_event_receiver: mpsc::Receiver<NetworkEvent>, |
| 641 | settings: Settings, |
| 642 | window: &mut Window, |
| 643 | cx: &mut Context<Self>, |
| 644 | ) -> Self { |
| 645 | cx.spawn(async move |this, cx| { |
| 646 | while let Some(event) = network_event_receiver.recv().await { |
| 647 | let Ok(handle) = this.read_with(cx, |yumush, _| yumush.window) else { |
| 648 | break; |
| 649 | }; |
| 650 | let mut event = Some(event); |
| 651 | if let Some(handle) = handle { |
| 652 | let _ = handle.update(cx, |_, window, cx| { |
| 653 | if let Some(event) = event.take() { |
| 654 | let _ = this.update(cx, |yumush, cx| { |
| 655 | yumush.handle_network_event(event, Some(window), cx) |
| 656 | }); |
| 657 | } |
| 658 | }); |
| 659 | } |
| 660 | if let Some(event) = event |
| 661 | && this |
| 662 | .update(cx, |yumush, cx| { |
| 663 | yumush.handle_network_event(event, None, cx) |
| 664 | }) |
| 665 | .is_err() |
| 666 | { |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | }) |
| 671 | .detach(); |
| 672 | |
| 673 | let mut phase = crate::update::subscribe(); |
| 674 | cx.spawn(async move |this, cx| { |
| 675 | loop { |
| 676 | let now = *phase.borrow_and_update(); |
| 677 | let alive = this |
| 678 | .update(cx, |yumush, cx| { |
| 679 | yumush.update = now; |
| 680 | cx.notify(); |
| 681 | }) |
| 682 | .is_ok(); |
| 683 | if !alive || phase.changed().await.is_err() { |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | }) |
| 688 | .detach(); |
| 689 | |
| 690 | sidebar::wander(cx); |
| 691 | let voice = VoiceControls::new(&settings.voice, window, cx); |
| 692 | |
| 693 | let server = network.server(); |
| 694 | let account_filter = new_input(w().auth.find_account, window, cx); |
| 695 | let log_search = new_input(w().chat.find, window, cx); |
| 696 | cx.subscribe(&account_filter, |_, _, event: &InputEvent, cx| { |
| 697 | if matches!(event, InputEvent::Change) { |
| 698 | cx.notify(); |
| 699 | } |
| 700 | }) |
| 701 | .detach(); |
| 702 | let mut yumush = Self { |
| 703 | user: None, |
| 704 | focus: cx.focus_handle(), |
| 705 | current_route: Route::Login, |
| 706 | auth: Auth::new(&server, window, cx), |
| 707 | chat: Chat::new(window, cx), |
| 708 | network, |
| 709 | connection: Connection::Connecting, |
| 710 | server: None, |
| 711 | dismissed_announcement: None, |
| 712 | settings, |
| 713 | accounts: accounts::load(&server.address), |
| 714 | account_filter, |
| 715 | voice, |
| 716 | window: None, |
| 717 | deferred: Vec::new(), |
| 718 | backups: Vec::new(), |
| 719 | pending_backup_delete: None, |
| 720 | update: crate::update::Phase::Idle, |
| 721 | desk: builder::releases::Desk::default(), |
| 722 | log_search, |
| 723 | log_scroll: gpui_kit::UniformListScrollHandle::new(), |
| 724 | share_apps: Vec::new(), |
| 725 | share_apps_seen: std::cell::Cell::new(Instant::now()), |
| 726 | share_apps_task: None, |
| 727 | wordmark_flips: 0, |
| 728 | wordmark_flipped: None, |
| 729 | zoom: None, |
| 730 | emoji_group: 0, |
| 731 | share_tab: common::share::Kind::Screen, |
| 732 | camera_modes: Vec::new(), |
| 733 | camera_devices: Vec::new(), |
| 734 | share_more: false, |
| 735 | share_busy: false, |
| 736 | fullscreen: false, |
| 737 | pumping: false, |
| 738 | pumped: Instant::now(), |
| 739 | send_order: Default::default(), |
| 740 | }; |
| 741 | yumush.chat.camera_task = Some(camera_view::ticker(cx)); |
| 742 | yumush |
| 743 | } |
| 744 | |
| 745 | pub fn tray_state(&self) -> crate::tray::TrayState { |
| 746 | let groups = self.chat.unread_groups(); |
| 747 | let direct = self.chat.unread_direct(); |
| 748 | crate::tray::TrayState { |
| 749 | messages: groups.messages + direct.messages, |
| 750 | mentions: groups.mentions + direct.mentions, |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | pub fn attach_window(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 755 | self.window = Some(window.window_handle()); |
| 756 | |
| 757 | cx.observe_window_activation(window, |yumush, window, cx| { |
| 758 | if window.is_window_active() { |
| 759 | yumush.read_selected(cx); |
| 760 | } else { |
| 761 | yumush.network.set_talking(false); |
| 762 | } |
| 763 | }) |
| 764 | .detach(); |
| 765 | |
| 766 | for event in std::mem::take(&mut self.deferred) { |
| 767 | self.handle_network_event(event, Some(window), cx); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | pub fn window(&self) -> Option<AnyWindowHandle> { |
| 772 | self.window |
| 773 | } |
| 774 | |
| 775 | pub fn detach_window(&mut self) -> bool { |
| 776 | self.window = None; |
| 777 | self.settings.close_to_tray |
| 778 | } |
| 779 | |
| 780 | pub(super) fn enter_session( |
| 781 | &mut self, |
| 782 | user: User, |
| 783 | token: String, |
| 784 | window: &mut Window, |
| 785 | cx: &mut Context<Self>, |
| 786 | ) { |
| 787 | self.accounts.sign_in(&user, token); |
| 788 | self.request_user_avatar(&user, cx); |
| 789 | self.chat.muted = marks::load(Mark::Mute, user.id); |
| 790 | self.chat.pinned = marks::load(Mark::Pin, user.id); |
| 791 | style::remember(&[&user]); |
| 792 | style::remember_me(user.id); |
| 793 | self.user = Some(user); |
| 794 | self.change_page(Route::Chat, window, cx); |
| 795 | self.load_communities(window, cx); |
| 796 | self.load_social(window, cx); |
| 797 | self.load_users(window, cx); |
| 798 | } |
| 799 | |
| 800 | pub(super) fn switch_account( |
| 801 | &mut self, |
| 802 | user_id: Id, |
| 803 | window: &mut Window, |
| 804 | cx: &mut Context<Self>, |
| 805 | ) { |
| 806 | let Some(account) = self.accounts.get(user_id).cloned() else { |
| 807 | return; |
| 808 | }; |
| 809 | if !self.connection.is_connected() { |
| 810 | window.push_notification(Notification::error(w().gui.not_connected), cx); |
| 811 | return; |
| 812 | } |
| 813 | if self.user.is_some() { |
| 814 | self.end_session(window, cx); |
| 815 | } |
| 816 | self.accounts.suspend(); |
| 817 | let network = self.network.clone(); |
| 818 | |
| 819 | cx.spawn_in(window, async move |this, cx| { |
| 820 | let result = network.resume(&account.token).await; |
| 821 | let _ = this.update_in(cx, |yumush, window, cx| match result { |
| 822 | Ok((user, token)) => yumush.enter_session(user, token, window, cx), |
| 823 | Err(error @ Fail::Server(Error::NotFound | Error::Unauthenticated)) => { |
| 824 | yumush.accounts.forget(account.user_id); |
| 825 | window.push_notification(Notification::error(error.to_string()), cx); |
| 826 | cx.notify(); |
| 827 | } |
| 828 | Err(error) => { |
| 829 | window.push_notification(Notification::error(error.to_string()), cx); |
| 830 | } |
| 831 | }); |
| 832 | }) |
| 833 | .detach(); |
| 834 | } |
| 835 | |
| 836 | pub(super) fn add_account(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 837 | self.accounts.detach(); |
| 838 | self.end_session(window, cx); |
| 839 | let network = self.network.clone(); |
| 840 | |
| 841 | cx.spawn_in(window, async move |this, cx| { |
| 842 | if let Err(error) = network.detach().await { |
| 843 | notify_error(&this, cx, error); |
| 844 | } |
| 845 | }) |
| 846 | .detach(); |
| 847 | } |
| 848 | |
| 849 | pub(super) fn forget_account(&mut self, user_id: Id, cx: &mut Context<Self>) { |
| 850 | self.accounts.forget(user_id); |
| 851 | cx.notify(); |
| 852 | } |
| 853 | |
| 854 | pub(super) fn toggle_mark(&mut self, mark: Mark, community_id: Id, cx: &mut Context<Self>) { |
| 855 | let Some(me) = self.user.as_ref().map(|me| me.id) else { |
| 856 | return; |
| 857 | }; |
| 858 | let ids = match mark { |
| 859 | Mark::Mute => &mut self.chat.muted, |
| 860 | Mark::Pin => &mut self.chat.pinned, |
| 861 | }; |
| 862 | if !ids.remove(&community_id) { |
| 863 | ids.insert(community_id); |
| 864 | } |
| 865 | marks::save(mark, me, ids); |
| 866 | cx.notify(); |
| 867 | } |
| 868 | |
| 869 | pub(super) fn armed(&self, armed: chat::Armed) -> bool { |
| 870 | self.chat.armed == Some(armed) |
| 871 | } |
| 872 | |
| 873 | pub(super) fn armed_label(&self, armed: chat::Armed, label: &'static str) -> &'static str { |
| 874 | if self.armed(armed) { |
| 875 | w().common.confirm |
| 876 | } else { |
| 877 | label |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | pub(super) fn arm(&mut self, armed: chat::Armed, cx: &mut Context<Self>) -> bool { |
| 882 | if self.armed(armed) { |
| 883 | self.chat.armed = None; |
| 884 | return true; |
| 885 | } |
| 886 | self.chat.armed = Some(armed); |
| 887 | cx.notify(); |
| 888 | false |
| 889 | } |
| 890 | |
| 891 | pub(super) fn disarm(&mut self, cx: &mut Context<Self>) { |
| 892 | if self.chat.armed.take().is_some() { |
| 893 | cx.notify(); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | fn logout(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 898 | let Some(user) = self.user.clone() else { |
| 899 | return; |
| 900 | }; |
| 901 | self.accounts.forget(user.id); |
| 902 | self.end_session(window, cx); |
| 903 | let network = self.network.clone(); |
| 904 | |
| 905 | cx.spawn_in(window, async move |this, cx| { |
| 906 | if let Err(error) = network.logout().await { |
| 907 | notify_error(&this, cx, error); |
| 908 | } |
| 909 | }) |
| 910 | .detach(); |
| 911 | } |
| 912 | |
| 913 | fn change_page(&mut self, route: Route, window: &mut Window, cx: &mut Context<Self>) { |
| 914 | self.current_route = route; |
| 915 | cx.notify(); |
| 916 | |
| 917 | match route { |
| 918 | Route::Login | Route::Register => self.auth.focus(window, cx), |
| 919 | Route::Chat => self.chat.focus(window, cx), |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | fn show_view(&mut self, view: View, window: &mut Window, cx: &mut Context<Self>) { |
| 924 | self.chat.section = None; |
| 925 | if self.chat.view == View::Settings && view != View::Settings { |
| 926 | self.leave_settings(); |
| 927 | } |
| 928 | if view == View::Settings { |
| 929 | self.enter_settings(window, cx); |
| 930 | let (username, status) = self |
| 931 | .user |
| 932 | .as_ref() |
| 933 | .map(|user| (user.username.clone(), user.status.clone())) |
| 934 | .unwrap_or_default(); |
| 935 | self.chat |
| 936 | .username_input |
| 937 | .update(cx, |input, cx| input.set_value(username, window, cx)); |
| 938 | self.chat |
| 939 | .status_input |
| 940 | .update(cx, |input, cx| input.fill(status, window, cx)); |
| 941 | } |
| 942 | if view == View::Builder { |
| 943 | self.load_builder(window, cx); |
| 944 | } |
| 945 | if view == View::CommunitySettings { |
| 946 | let name = self |
| 947 | .chat |
| 948 | .selected_community() |
| 949 | .map(|community| community.name.clone()) |
| 950 | .unwrap_or_default(); |
| 951 | self.chat |
| 952 | .community_name_input |
| 953 | .update(cx, |input, cx| input.set_value(name, window, cx)); |
| 954 | self.load_bans(window, cx); |
| 955 | } |
| 956 | |
| 957 | self.chat.view = view; |
| 958 | self.chat.armed = None; |
| 959 | if view == View::Chat { |
| 960 | self.chat.focus(window, cx); |
| 961 | } |
| 962 | cx.notify(); |
| 963 | } |
| 964 | |
| 965 | pub fn actor_rank(&self) -> Option<Rank> { |
| 966 | self.user.as_ref().and_then(|me| self.chat.rank_of(me)) |
| 967 | } |
| 968 | |
| 969 | pub fn community_badge( |
| 970 | &self, |
| 971 | community: &Community, |
| 972 | size: Pixels, |
| 973 | font: Rems, |
| 974 | palette: Palette, |
| 975 | ) -> Div { |
| 976 | let icon = community_icon(palette, community, size, font, &self.chat.avatars); |
| 977 | match community.kind.peer() { |
| 978 | Some(peer) => presence_avatar(icon, self.presence_of(peer.id), size, palette), |
| 979 | None => icon, |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | pub fn presence_of(&self, user_id: Id) -> Presence { |
| 984 | if self.is_me(user_id) && !self.connection.is_connected() { |
| 985 | return Presence::Offline; |
| 986 | } |
| 987 | self.chat |
| 988 | .presences |
| 989 | .get(&user_id) |
| 990 | .copied() |
| 991 | .unwrap_or(Presence::Offline) |
| 992 | } |
| 993 | |
| 994 | pub fn is_online(&self, user_id: Id) -> bool { |
| 995 | self.presence_of(user_id) != Presence::Offline |
| 996 | } |
| 997 | |
| 998 | pub fn request_avatar(&mut self, avatar_id: Id, cx: &mut Context<Self>) { |
| 999 | if self.chat.avatars.contains_key(&avatar_id) |
| 1000 | || self.chat.avatar_requests.contains(&avatar_id) |
| 1001 | { |
| 1002 | return; |
| 1003 | } |
| 1004 | self.chat.avatar_requests.insert(avatar_id); |
| 1005 | |
| 1006 | let network = self.network.clone(); |
| 1007 | |
| 1008 | cx.spawn(async move |this, cx| { |
| 1009 | let result = network.call(GetAvatar { avatar_id }).await; |
| 1010 | let _ = this.update(cx, |yumush, cx| { |
| 1011 | yumush.chat.avatar_requests.remove(&avatar_id); |
| 1012 | if let Ok(bytes) = result { |
| 1013 | style::picture(avatar_id, &bytes); |
| 1014 | if let Some(me) = yumush |
| 1015 | .user |
| 1016 | .as_ref() |
| 1017 | .filter(|me| me.avatar_id == Some(avatar_id)) |
| 1018 | { |
| 1019 | yumush.accounts.set_picture(me.id, Some(bytes.clone())); |
| 1020 | } |
| 1021 | yumush.chat.avatars.insert( |
| 1022 | avatar_id, |
| 1023 | Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes)), |
| 1024 | ); |
| 1025 | } |
| 1026 | cx.notify(); |
| 1027 | }); |
| 1028 | }) |
| 1029 | .detach(); |
| 1030 | } |
| 1031 | |
| 1032 | pub fn request_user_avatar(&mut self, user: &User, cx: &mut Context<Self>) { |
| 1033 | if user.avatar_id.is_none() |
| 1034 | && self |
| 1035 | .accounts |
| 1036 | .get(user.id) |
| 1037 | .is_some_and(|account| account.picture.is_some()) |
| 1038 | { |
| 1039 | self.accounts.set_picture(user.id, None); |
| 1040 | } |
| 1041 | if let Some(avatar_id) = user.avatar_id { |
| 1042 | self.request_avatar(avatar_id, cx); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | fn apply_pushed(&mut self, event: Event, cx: &mut Context<Self>) { |
| 1047 | match &event { |
| 1048 | Event::MemberJoined { member, .. } => self.request_user_avatar(&member.user, cx), |
| 1049 | Event::UserUpdated(user) => { |
| 1050 | if self.is_me(user.id) { |
| 1051 | self.user = Some(user.clone()); |
| 1052 | self.accounts.rename(user); |
| 1053 | if !self.is_builder() && self.chat.view == View::Builder { |
| 1054 | self.chat.view = View::Chat; |
| 1055 | } |
| 1056 | } |
| 1057 | self.request_user_avatar(user, cx); |
| 1058 | } |
| 1059 | Event::VoiceLeft { |
| 1060 | community_id, |
| 1061 | user_id, |
| 1062 | } if self.is_me(*user_id) => self.leave_voice_in(*community_id, cx), |
| 1063 | Event::VoiceMuted { |
| 1064 | user_id, |
| 1065 | muted: true, |
| 1066 | .. |
| 1067 | } if self.is_me(*user_id) => self.set_muted(true, cx), |
| 1068 | Event::ShareStopped { user_id, kind, .. } if self.is_watching(*user_id, *kind) => { |
| 1069 | self.close_viewer(cx) |
| 1070 | } |
| 1071 | Event::StatsWanted(true) => self.start_stats_push(cx), |
| 1072 | Event::StatsWanted(false) => self.chat.stats_task = None, |
| 1073 | Event::Typing { .. } => { |
| 1074 | cx.spawn(async move |this, cx| { |
| 1075 | cx.background_executor().timer(chat::TYPING_TTL).await; |
| 1076 | let _ = this.update(cx, |_, cx| cx.notify()); |
| 1077 | }) |
| 1078 | .detach(); |
| 1079 | } |
| 1080 | _ => {} |
| 1081 | } |
| 1082 | let builder = self.is_builder(); |
| 1083 | self.chat |
| 1084 | .apply_event(event, self.user.as_ref().map(|user| user.id), builder); |
| 1085 | self.request_authors(cx); |
| 1086 | self.request_social_avatars(cx); |
| 1087 | self.request_media(cx); |
| 1088 | } |
| 1089 | |
| 1090 | fn mark_read(&mut self, community_id: Id, message_id: Id, cx: &mut Context<Self>) { |
| 1091 | self.chat.mark_read(community_id, message_id); |
| 1092 | let network = self.network.clone(); |
| 1093 | |
| 1094 | cx.spawn(async move |_, _| { |
| 1095 | let _ = network |
| 1096 | .call(MarkRead { |
| 1097 | community_id, |
| 1098 | message_id, |
| 1099 | }) |
| 1100 | .await; |
| 1101 | }) |
| 1102 | .detach(); |
| 1103 | } |
| 1104 | |
| 1105 | fn read_selected(&mut self, cx: &mut Context<Self>) { |
| 1106 | let Some(community_id) = self.chat.selected_community_id else { |
| 1107 | return; |
| 1108 | }; |
| 1109 | let Some(message_id) = self.chat.messages.last().map(|message| message.id) else { |
| 1110 | return; |
| 1111 | }; |
| 1112 | if self |
| 1113 | .chat |
| 1114 | .unread |
| 1115 | .get(&community_id) |
| 1116 | .is_some_and(|unread| unread.messages > 0) |
| 1117 | { |
| 1118 | self.mark_read(community_id, message_id, cx); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | fn count_message(&mut self, message: &Message, active: bool, cx: &mut Context<Self>) { |
| 1123 | let Some(me) = self.user.as_ref() else { |
| 1124 | return; |
| 1125 | }; |
| 1126 | if message.user_id == me.id { |
| 1127 | return; |
| 1128 | } |
| 1129 | if active && self.chat.is_selected(message.community_id) { |
| 1130 | self.mark_read(message.community_id, message.id, cx); |
| 1131 | return; |
| 1132 | } |
| 1133 | let unread = self |
| 1134 | .chat |
| 1135 | .unread |
| 1136 | .entry(message.community_id) |
| 1137 | .or_insert_with(|| Unread { |
| 1138 | community_id: message.community_id, |
| 1139 | ..Default::default() |
| 1140 | }); |
| 1141 | unread.messages += 1; |
| 1142 | if mentions(&message.body, &me.username) { |
| 1143 | unread.mentions += 1; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | fn should_notify(&self, message: &Message, active: bool) -> bool { |
| 1148 | let Some(me) = self.user.as_ref() else { |
| 1149 | return false; |
| 1150 | }; |
| 1151 | let viewing = active && self.chat.is_selected(message.community_id); |
| 1152 | |
| 1153 | message.user_id != me.id |
| 1154 | && message.call.is_none() |
| 1155 | && !self.chat.muted.contains(&message.community_id) |
| 1156 | && self.presence_of(me.id) == Presence::Online |
| 1157 | && !viewing |
| 1158 | } |
| 1159 | |
| 1160 | fn starts_call(&self, community_id: Id, user_id: Id) -> bool { |
| 1161 | !self.is_me(user_id) |
| 1162 | && self.chat.voice_ids(community_id).is_empty() |
| 1163 | && self.is_direct(community_id) |
| 1164 | } |
| 1165 | |
| 1166 | fn ends_call(&self, community_id: Id, user_id: Id) -> bool { |
| 1167 | !self.is_me(user_id) |
| 1168 | && self.chat.in_voice(community_id) |
| 1169 | && self.is_direct(community_id) |
| 1170 | && self |
| 1171 | .chat |
| 1172 | .voice_ids(community_id) |
| 1173 | .iter() |
| 1174 | .all(|id| *id == user_id || self.is_me(*id)) |
| 1175 | } |
| 1176 | |
| 1177 | pub(super) fn is_direct(&self, community_id: Id) -> bool { |
| 1178 | self.chat |
| 1179 | .community(community_id) |
| 1180 | .is_some_and(|community| community.kind.is_direct()) |
| 1181 | } |
| 1182 | |
| 1183 | pub(super) fn is_me(&self, user_id: Id) -> bool { |
| 1184 | self.user.as_ref().is_some_and(|me| me.id == user_id) |
| 1185 | } |
| 1186 | |
| 1187 | fn end_session(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1188 | self.end_voice(cx); |
| 1189 | self.leave_settings(); |
| 1190 | self.stop_playing(cx); |
| 1191 | self.user = None; |
| 1192 | self.chat.reset(); |
| 1193 | self.change_page(Route::Login, window, cx); |
| 1194 | } |
| 1195 | |
| 1196 | pub(super) fn max_avatar_bytes(&self) -> u64 { |
| 1197 | self.server |
| 1198 | .as_ref() |
| 1199 | .map_or(MAXIMUM_AVATAR_BYTES as u64, |settings| { |
| 1200 | settings.max_avatar_bytes |
| 1201 | }) |
| 1202 | } |
| 1203 | |
| 1204 | pub(super) fn max_message_length(&self) -> usize { |
| 1205 | self.server |
| 1206 | .as_ref() |
| 1207 | .map_or(MAXIMUM_MESSAGE_LENGTH, |settings| { |
| 1208 | settings.max_message_length as usize |
| 1209 | }) |
| 1210 | } |
| 1211 | |
| 1212 | pub(super) fn announcement(&self) -> Option<&str> { |
| 1213 | let text = self.server.as_ref()?.announcement.as_str(); |
| 1214 | let dismissed = self.dismissed_announcement.as_deref() == Some(text); |
| 1215 | |
| 1216 | (!text.is_empty() && !dismissed).then_some(text) |
| 1217 | } |
| 1218 | |
| 1219 | pub(super) fn dismiss_announcement(&mut self, cx: &mut Context<Self>) { |
| 1220 | self.dismissed_announcement = self |
| 1221 | .server |
| 1222 | .as_ref() |
| 1223 | .map(|settings| settings.announcement.clone()); |
| 1224 | cx.notify(); |
| 1225 | } |
| 1226 | |
| 1227 | fn load_server_settings(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 1228 | let network = self.network.clone(); |
| 1229 | |
| 1230 | cx.spawn_in(window, async move |this, cx| { |
| 1231 | let Ok(settings) = network.call(GetSettings).await else { |
| 1232 | return; |
| 1233 | }; |
| 1234 | let _ = this.update(cx, |yumush, cx| { |
| 1235 | yumush.server = Some(settings); |
| 1236 | cx.notify(); |
| 1237 | }); |
| 1238 | }) |
| 1239 | .detach(); |
| 1240 | } |
| 1241 | |
| 1242 | fn handle_network_event( |
| 1243 | &mut self, |
| 1244 | event: NetworkEvent, |
| 1245 | window: Option<&mut Window>, |
| 1246 | cx: &mut Context<Self>, |
| 1247 | ) { |
| 1248 | let active = window |
| 1249 | .as_ref() |
| 1250 | .is_some_and(|window| window.is_window_active()); |
| 1251 | if let NetworkEvent::Pushed(pushed) = &event { |
| 1252 | self.before_pushed(pushed, active, cx); |
| 1253 | } |
| 1254 | |
| 1255 | let Some(window) = window else { |
| 1256 | self.handle_hidden(event, cx); |
| 1257 | return; |
| 1258 | }; |
| 1259 | |
| 1260 | match event { |
| 1261 | NetworkEvent::Connected => { |
| 1262 | self.connection = Connection::Connected; |
| 1263 | self.load_server_settings(window, cx); |
| 1264 | if self.user.is_some() { |
| 1265 | self.reload(window, cx); |
| 1266 | } else if let Some(active) = self.accounts.active { |
| 1267 | self.switch_account(active, window, cx); |
| 1268 | } |
| 1269 | } |
| 1270 | NetworkEvent::Disconnected(error) => self.connection.lost(&error), |
| 1271 | NetworkEvent::VoiceRejoined => self.refresh_voice(cx), |
| 1272 | NetworkEvent::VoiceEnded => self.chat.set_voice_off(), |
| 1273 | NetworkEvent::SessionLost => { |
| 1274 | if let Some(user) = &self.user { |
| 1275 | self.accounts.forget(user.id); |
| 1276 | } |
| 1277 | self.end_session(window, cx); |
| 1278 | } |
| 1279 | NetworkEvent::Pushed(event) => match *event { |
| 1280 | Event::MemberLeft { |
| 1281 | community_id, |
| 1282 | user_id, |
| 1283 | } if self.is_me(user_id) => { |
| 1284 | self.leave_voice_in(community_id, cx); |
| 1285 | self.chat.remove_community(community_id); |
| 1286 | } |
| 1287 | Event::Removed { |
| 1288 | community_id, |
| 1289 | community_name, |
| 1290 | sanction, |
| 1291 | reason, |
| 1292 | } => { |
| 1293 | self.leave_voice_in(community_id, cx); |
| 1294 | self.chat.remove_community(community_id); |
| 1295 | let text = match (sanction, reason) { |
| 1296 | (Sanction::Ban, Some(reason)) => { |
| 1297 | w().gui.banned_reason(community_name, reason) |
| 1298 | } |
| 1299 | (Sanction::Ban, None) => w().gui.banned(community_name), |
| 1300 | (Sanction::Kick, Some(reason)) => { |
| 1301 | w().gui.kicked_reason(community_name, reason) |
| 1302 | } |
| 1303 | (Sanction::Kick, None) => w().gui.kicked(community_name), |
| 1304 | }; |
| 1305 | window.push_notification(Notification::warning(text), cx); |
| 1306 | } |
| 1307 | Event::CommunityDeleted { community_id } => { |
| 1308 | self.leave_voice_in(community_id, cx); |
| 1309 | self.apply_pushed(Event::CommunityDeleted { community_id }, cx); |
| 1310 | } |
| 1311 | Event::ReportCreated => self.report_created(window, cx), |
| 1312 | Event::Release => self.releases_changed(window, cx), |
| 1313 | Event::SettingsUpdated(settings) => self.server = Some(settings), |
| 1314 | event => { |
| 1315 | let voice = matches!( |
| 1316 | event, |
| 1317 | Event::VoiceJoined { .. } |
| 1318 | | Event::VoiceLeft { .. } |
| 1319 | | Event::VoiceMuted { .. } |
| 1320 | ); |
| 1321 | self.apply_pushed(event, cx); |
| 1322 | if voice { |
| 1323 | self.voice_changed(window, cx); |
| 1324 | } |
| 1325 | } |
| 1326 | }, |
| 1327 | } |
| 1328 | |
| 1329 | self.update_ringing(cx); |
| 1330 | cx.notify(); |
| 1331 | } |
| 1332 | |
| 1333 | fn before_pushed(&mut self, event: &Event, active: bool, cx: &mut Context<Self>) { |
| 1334 | match event { |
| 1335 | Event::MessageCreated(message) => { |
| 1336 | if self.should_notify(message, active) { |
| 1337 | self.network.play_notification(&self.settings.voice); |
| 1338 | } |
| 1339 | self.count_message(message, active, cx); |
| 1340 | if !self |
| 1341 | .chat |
| 1342 | .communities |
| 1343 | .iter() |
| 1344 | .any(|community| community.id == message.community_id) |
| 1345 | { |
| 1346 | self.refresh_communities(cx); |
| 1347 | } |
| 1348 | if message.call == Some(CallLog::Declined) |
| 1349 | && self.chat.in_voice(message.community_id) |
| 1350 | { |
| 1351 | self.end_voice(cx); |
| 1352 | } |
| 1353 | } |
| 1354 | Event::VoiceJoined { |
| 1355 | community_id, |
| 1356 | user_id, |
| 1357 | } if self.starts_call(*community_id, *user_id) => { |
| 1358 | self.chat.ringing.insert(*community_id); |
| 1359 | } |
| 1360 | Event::VoiceLeft { |
| 1361 | community_id, |
| 1362 | user_id, |
| 1363 | } if self.ends_call(*community_id, *user_id) => { |
| 1364 | self.end_voice(cx); |
| 1365 | } |
| 1366 | _ => {} |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | fn handle_hidden(&mut self, event: NetworkEvent, cx: &mut Context<Self>) { |
| 1371 | match event { |
| 1372 | NetworkEvent::Connected => { |
| 1373 | self.connection = Connection::Connected; |
| 1374 | self.deferred.push(NetworkEvent::Connected); |
| 1375 | } |
| 1376 | NetworkEvent::Disconnected(error) => self.connection.lost(&error), |
| 1377 | NetworkEvent::VoiceRejoined => self.refresh_voice(cx), |
| 1378 | NetworkEvent::VoiceEnded => self.chat.set_voice_off(), |
| 1379 | NetworkEvent::SessionLost => self.deferred.push(NetworkEvent::SessionLost), |
| 1380 | NetworkEvent::Pushed(event) => match *event { |
| 1381 | Event::MemberLeft { .. } |
| 1382 | | Event::Removed { .. } |
| 1383 | | Event::CommunityDeleted { .. } |
| 1384 | | Event::ReportCreated => self.deferred.push(NetworkEvent::Pushed(event)), |
| 1385 | Event::SettingsUpdated(settings) => self.server = Some(settings), |
| 1386 | event => self.apply_pushed(event, cx), |
| 1387 | }, |
| 1388 | } |
| 1389 | |
| 1390 | self.update_ringing(cx); |
| 1391 | cx.notify(); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | pub fn input_dialog( |
| 1396 | title: &'static str, |
| 1397 | action: &'static str, |
| 1398 | placeholder: &'static str, |
| 1399 | window: &mut Window, |
| 1400 | cx: &mut Context<Yumush>, |
| 1401 | on_submit: impl Fn(&mut Yumush, String, &mut Window, &mut Context<Yumush>) + 'static, |
| 1402 | ) { |
| 1403 | let input = new_input(placeholder, window, cx); |
| 1404 | let on_submit = std::rc::Rc::new(on_submit); |
| 1405 | let on_enter = on_submit.clone(); |
| 1406 | |
| 1407 | cx.subscribe_in(&input, window, move |this, input, event, window, cx| { |
| 1408 | if matches!(event, InputEvent::PressEnter { .. }) { |
| 1409 | let text = input.read(cx).text().to_string(); |
| 1410 | on_enter(this, text, window, cx); |
| 1411 | } |
| 1412 | }) |
| 1413 | .detach(); |
| 1414 | |
| 1415 | let entity = cx.entity(); |
| 1416 | let dialog_input = input.clone(); |
| 1417 | window.open_dialog(cx, move |dialog, _, cx| { |
| 1418 | let palette = palette(cx); |
| 1419 | let entity = entity.clone(); |
| 1420 | let on_submit = on_submit.clone(); |
| 1421 | let button_input = dialog_input.clone(); |
| 1422 | |
| 1423 | dialog |
| 1424 | .title(title) |
| 1425 | .close_button(false) |
| 1426 | .w(px(size::POPOVER)) |
| 1427 | .child( |
| 1428 | div() |
| 1429 | .flex() |
| 1430 | .items_center() |
| 1431 | .gap(px(space::MD)) |
| 1432 | .child( |
| 1433 | widget::frame(palette, Input::new(&dialog_input).token(glyph::token)) |
| 1434 | .flex_1() |
| 1435 | .min_w_0(), |
| 1436 | ) |
| 1437 | .child(parts::button( |
| 1438 | "input_dialog_submit", |
| 1439 | action, |
| 1440 | parts::Press::Primary, |
| 1441 | palette.tokens, |
| 1442 | move |window, cx| { |
| 1443 | let text = button_input.read(cx).text().to_string(); |
| 1444 | entity.update(cx, |this, cx| on_submit(this, text, window, cx)); |
| 1445 | }, |
| 1446 | )), |
| 1447 | ) |
| 1448 | }); |
| 1449 | window.defer(cx, move |window, cx| { |
| 1450 | input.update(cx, |input, cx| input.focus(window, cx)); |
| 1451 | }); |
| 1452 | } |
| 1453 | |
| 1454 | pub fn notify_error<T: 'static>( |
| 1455 | this: &WeakEntity<T>, |
| 1456 | cx: &mut AsyncWindowContext, |
| 1457 | error: impl Display, |
| 1458 | ) { |
| 1459 | let _ = this.update_in(cx, |_, window, cx| { |
| 1460 | window.push_notification(Notification::error(error.to_string()), cx); |
| 1461 | }); |
| 1462 | } |
| 1463 | |
| 1464 | pub(super) fn new_input( |
| 1465 | placeholder: &'static str, |
| 1466 | window: &mut Window, |
| 1467 | cx: &mut Context<Yumush>, |
| 1468 | ) -> Entity<InputState> { |
| 1469 | let input = cx.new(|cx| { |
| 1470 | let mut input_state = InputState::new(window, cx); |
| 1471 | input_state.set_placeholder(placeholder, window, cx); |
| 1472 | |
| 1473 | input_state |
| 1474 | }); |
| 1475 | cx.subscribe(&input, |_, input, event: &InputEvent, cx| { |
| 1476 | if matches!(event, InputEvent::Change) { |
| 1477 | input.update(cx, |state, cx| state.mark(cx)); |
| 1478 | } |
| 1479 | }) |
| 1480 | .detach(); |
| 1481 | input |
| 1482 | } |
| 1483 | |
| 1484 | impl Render for Yumush { |
| 1485 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 1486 | crate::usage::tick(crate::usage::Group::Gui); |
| 1487 | let fullscreen = self |
| 1488 | .chat |
| 1489 | .viewer |
| 1490 | .as_ref() |
| 1491 | .is_some_and(|viewer| viewer.fullscreen); |
| 1492 | if fullscreen != self.fullscreen { |
| 1493 | self.fullscreen = fullscreen; |
| 1494 | window.toggle_fullscreen(); |
| 1495 | } |
| 1496 | if window.focused(cx).is_none() { |
| 1497 | window.focus(&self.focus, cx); |
| 1498 | } |
| 1499 | self.hide_idle_cursor(window, cx); |
| 1500 | self.schedule_pump(window, cx); |
| 1501 | let palette = palette(cx); |
| 1502 | let route = self.current_route; |
| 1503 | let required = self.update.required() && !crate::release::running(); |
| 1504 | let page = match route { |
| 1505 | _ if required => self.update_required(palette).into_any_element(), |
| 1506 | Route::Login | Route::Register => self.auth_page(route, cx).into_any_element(), |
| 1507 | Route::Chat => self.chat_page(cx).into_any_element(), |
| 1508 | }; |
| 1509 | let banner = (route == Route::Chat && !required) |
| 1510 | .then(|| connection_notice(&self.connection, palette)) |
| 1511 | .flatten(); |
| 1512 | |
| 1513 | div() |
| 1514 | .relative() |
| 1515 | .flex() |
| 1516 | .flex_col() |
| 1517 | .size_full() |
| 1518 | .bg(palette.window) |
| 1519 | .text_color(palette.text) |
| 1520 | .text_size(text(style::text::UI)) |
| 1521 | .track_focus(&self.focus) |
| 1522 | .on_action(cx.listener(|this, _: &FocusFind, window, cx| this.open_jump(window, cx))) |
| 1523 | .on_action(cx.listener(|this, _: &CloseSettings, window, cx| { |
| 1524 | if this.zoom.is_some() { |
| 1525 | this.zoom = None; |
| 1526 | cx.notify(); |
| 1527 | } else if this.chat.friends_open { |
| 1528 | this.close_friends(window, cx); |
| 1529 | } else if this.chat.goto.open { |
| 1530 | this.close_jump(window, cx); |
| 1531 | } else if this.fullscreen { |
| 1532 | this.toggle_fullscreen(cx); |
| 1533 | } else if this.chat.view != View::Chat { |
| 1534 | this.show_view(View::Chat, window, cx); |
| 1535 | } else { |
| 1536 | this.cancel_composer(window, cx); |
| 1537 | } |
| 1538 | })) |
| 1539 | .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { |
| 1540 | this.voice_key_down(event, cx); |
| 1541 | })) |
| 1542 | .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| { |
| 1543 | this.voice_key_up(event, cx); |
| 1544 | })) |
| 1545 | .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| { |
| 1546 | this.voice_modifiers(event, cx); |
| 1547 | })) |
| 1548 | .children(banner) |
| 1549 | .child(div().flex_1().min_h_0().child(page)) |
| 1550 | .children( |
| 1551 | (route == Route::Chat && !required) |
| 1552 | .then(|| self.jump_box(palette, cx)) |
| 1553 | .flatten(), |
| 1554 | ) |
| 1555 | .children( |
| 1556 | self.zoom |
| 1557 | .as_ref() |
| 1558 | .map(|zoom| zoom_view(zoom, &cx.entity(), palette.tokens)), |
| 1559 | ) |
| 1560 | } |
| 1561 | } |