chore: optimize resource usage (#162)

* avoid string allocation

* cache image

* .

* .

* .

* fix
This commit is contained in:
reya
2025-09-23 09:03:48 +07:00
committed by GitHub
parent fb3da096f8
commit 9abcc25f32
25 changed files with 281 additions and 214 deletions

73
Cargo.lock generated
View File

@@ -510,12 +510,9 @@ dependencies = [
[[package]] [[package]]
name = "base62" name = "base62"
version = "2.2.2" version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0104d4d8d15e458f21dcd027ea350bf38e4364954909402f4da075aca8d0f136" checksum = "1adf9755786e27479693dedd3271691a92b5e242ab139cacb9fb8e7fb5381111"
dependencies = [
"rustversion",
]
[[package]] [[package]]
name = "base64" name = "base64"
@@ -1131,7 +1128,7 @@ dependencies = [
[[package]] [[package]]
name = "collections" name = "collections"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
@@ -1572,7 +1569,7 @@ dependencies = [
[[package]] [[package]]
name = "derive_refineable" name = "derive_refineable"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2499,7 +2496,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui" name = "gpui"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"as-raw-xcb-connection", "as-raw-xcb-connection",
@@ -2593,7 +2590,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui_macros" name = "gpui_macros"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
@@ -2605,7 +2602,7 @@ dependencies = [
[[package]] [[package]]
name = "gpui_tokio" name = "gpui_tokio"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"gpui", "gpui",
@@ -2825,7 +2822,7 @@ dependencies = [
[[package]] [[package]]
name = "http_client" name = "http_client"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
@@ -2845,7 +2842,7 @@ dependencies = [
[[package]] [[package]]
name = "http_client_tls" name = "http_client_tls"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"rustls", "rustls",
"rustls-platform-verifier", "rustls-platform-verifier",
@@ -3420,12 +3417,12 @@ dependencies = [
[[package]] [[package]]
name = "libloading" name = "libloading"
version = "0.8.8" version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07033963ba89ebaf1584d767badaa2e8fcec21aedea6b8c0346d487d49c28667" checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets 0.53.3", "windows-link 0.2.0",
] ]
[[package]] [[package]]
@@ -3569,9 +3566,9 @@ dependencies = [
[[package]] [[package]]
name = "lyon_geom" name = "lyon_geom"
version = "1.0.16" version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce9333c02ea4517fd31207f126124352ad59975218c114c55dbb8f9d56fd4b45" checksum = "4e16770d760c7848b0c1c2d209101e408207a65168109509f8483837a36cf2e7"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"euclid", "euclid",
@@ -3637,7 +3634,7 @@ dependencies = [
[[package]] [[package]]
name = "media" name = "media"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bindgen 0.71.1", "bindgen 0.71.1",
@@ -3875,9 +3872,9 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8"
[[package]] [[package]]
name = "normpath" name = "normpath"
version = "1.4.0" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c178369371fd7db523726931e50d430b560e3059665abc537ba3277e9274c9c4" checksum = "bf23ab2b905654b4cb177e30b629937b3868311d4e1cba859f899c041046e69b"
dependencies = [ dependencies = [
"windows-sys 0.61.0", "windows-sys 0.61.0",
] ]
@@ -4477,6 +4474,17 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "perf"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [
"collections",
"serde",
"serde_json",
"workspace-hack",
]
[[package]] [[package]]
name = "phf" name = "phf"
version = "0.11.3" version = "0.11.3"
@@ -5077,7 +5085,7 @@ dependencies = [
[[package]] [[package]]
name = "refineable" name = "refineable"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"derive_refineable", "derive_refineable",
"workspace-hack", "workspace-hack",
@@ -5231,7 +5239,7 @@ dependencies = [
[[package]] [[package]]
name = "reqwest_client" name = "reqwest_client"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
@@ -5466,7 +5474,7 @@ dependencies = [
"openssl-probe", "openssl-probe",
"rustls-pki-types", "rustls-pki-types",
"schannel", "schannel",
"security-framework 3.4.0", "security-framework 3.5.0",
] ]
[[package]] [[package]]
@@ -5503,7 +5511,7 @@ dependencies = [
"rustls-native-certs", "rustls-native-certs",
"rustls-platform-verifier-android", "rustls-platform-verifier-android",
"rustls-webpki", "rustls-webpki",
"security-framework 3.4.0", "security-framework 3.5.0",
"security-framework-sys", "security-framework-sys",
"webpki-root-certs 0.26.11", "webpki-root-certs 0.26.11",
"windows-sys 0.59.0", "windows-sys 0.59.0",
@@ -5736,9 +5744,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "3.4.0" version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b369d18893388b345804dc0007963c99b7d665ae71d275812d828c6f089640" checksum = "cc198e42d9b7510827939c9a15f5062a0c913f3371d765977e586d2fe6c16f4a"
dependencies = [ dependencies = [
"bitflags 2.9.4", "bitflags 2.9.4",
"core-foundation 0.10.1", "core-foundation 0.10.1",
@@ -5766,7 +5774,7 @@ checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749"
[[package]] [[package]]
name = "semantic_version" name = "semantic_version"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"serde", "serde",
@@ -6218,7 +6226,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "sum_tree" name = "sum_tree"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"log", "log",
@@ -7261,7 +7269,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]] [[package]]
name = "util" name = "util"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-fs", "async-fs",
@@ -7296,8 +7304,9 @@ dependencies = [
[[package]] [[package]]
name = "util_macros" name = "util_macros"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#839c216620af116459e2ba15e82f3df8c3597349" source = "git+https://github.com/zed-industries/zed#891a06c2940b7aa441aac047a98d0dce86fb39a0"
dependencies = [ dependencies = [
"perf",
"quote", "quote",
"syn 2.0.106", "syn 2.0.106",
"workspace-hack", "workspace-hack",
@@ -8382,9 +8391,9 @@ checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd"
[[package]] [[package]]
name = "xattr" name = "xattr"
version = "1.5.1" version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af3a19837351dc82ba89f8a125e22a3c475f05aba604acc023d62b2739ae2909" checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
dependencies = [ dependencies = [
"libc", "libc",
"rustix 1.1.2", "rustix 1.1.2",

View File

@@ -58,3 +58,7 @@ opt-level = "z"
lto = true lto = true
codegen-units = 1 codegen-units = 1
panic = "abort" panic = "abort"
[profile.profiling]
inherits = "release"
debug = true

View File

@@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use chrono::{Local, TimeZone}; use chrono::{Local, TimeZone};
use global::constants::IMAGE_RESIZE_SERVICE; use global::constants::IMAGE_RESIZE_SERVICE;
use gpui::{Image, ImageFormat}; use gpui::{Image, ImageFormat, SharedString, SharedUri};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use qrcode::render::svg; use qrcode::render::svg;
use qrcode::QrCode; use qrcode::QrCode;
@@ -15,87 +15,92 @@ const HOURS_IN_DAY: i64 = 24;
const DAYS_IN_MONTH: i64 = 30; const DAYS_IN_MONTH: i64 = 30;
const FALLBACK_IMG: &str = "https://image.nostr.build/c30703b48f511c293a9003be8100cdad37b8798b77a1dc3ec6eb8a20443d5dea.png"; const FALLBACK_IMG: &str = "https://image.nostr.build/c30703b48f511c293a9003be8100cdad37b8798b77a1dc3ec6eb8a20443d5dea.png";
pub trait ReadableProfile { pub trait RenderedProfile {
fn avatar_url(&self, proxy: bool) -> String; fn avatar(&self, proxy: bool) -> SharedUri;
fn display_name(&self) -> String; fn display_name(&self) -> SharedString;
} }
impl ReadableProfile for Profile { impl RenderedProfile for Profile {
fn avatar_url(&self, proxy: bool) -> String { fn avatar(&self, proxy: bool) -> SharedUri {
self.metadata() self.metadata()
.picture .picture
.as_ref() .as_ref()
.filter(|picture| !picture.is_empty()) .filter(|picture| !picture.is_empty())
.map(|picture| { .map(|picture| {
if proxy { if proxy {
format!( let url = format!(
"{IMAGE_RESIZE_SERVICE}/?url={picture}&w=100&h=100&fit=cover&mask=circle&default={FALLBACK_IMG}&n=-1" "{IMAGE_RESIZE_SERVICE}/?url={picture}&w=100&h=100&fit=cover&mask=circle&default={FALLBACK_IMG}&n=-1"
) );
SharedUri::from(url)
} else { } else {
picture.into() SharedUri::from(picture)
} }
}) })
.unwrap_or_else(|| "brand/avatar.png".into()) .unwrap_or_else(|| SharedUri::from("brand/avatar.png"))
} }
fn display_name(&self) -> String { fn display_name(&self) -> SharedString {
if let Some(display_name) = self.metadata().display_name.as_ref() { if let Some(display_name) = self.metadata().display_name.as_ref() {
if !display_name.is_empty() { if !display_name.is_empty() {
return display_name.into(); return SharedString::from(display_name);
} }
} }
if let Some(name) = self.metadata().name.as_ref() { if let Some(name) = self.metadata().name.as_ref() {
if !name.is_empty() { if !name.is_empty() {
return name.into(); return SharedString::from(name);
} }
} }
shorten_pubkey(self.public_key(), 4) SharedString::from(shorten_pubkey(self.public_key(), 4))
} }
} }
pub trait ReadableTimestamp { pub trait RenderedTimestamp {
fn to_human_time(&self) -> String; fn to_human_time(&self) -> SharedString;
fn to_ago(&self) -> String; fn to_ago(&self) -> SharedString;
} }
impl ReadableTimestamp for Timestamp { impl RenderedTimestamp for Timestamp {
fn to_human_time(&self) -> String { fn to_human_time(&self) -> SharedString {
let input_time = match Local.timestamp_opt(self.as_u64() as i64, 0) { let input_time = match Local.timestamp_opt(self.as_u64() as i64, 0) {
chrono::LocalResult::Single(time) => time, chrono::LocalResult::Single(time) => time,
_ => return "9999".into(), _ => return SharedString::from("9999"),
}; };
let now = Local::now(); let now = Local::now();
let input_date = input_time.date_naive(); let input_date = input_time.date_naive();
let now_date = now.date_naive(); let now_date = now.date_naive();
let yesterday_date = (now - chrono::Duration::days(1)).date_naive(); let yesterday_date = (now - chrono::Duration::days(1)).date_naive();
let time_format = input_time.format("%H:%M %p"); let time_format = input_time.format("%H:%M %p");
match input_date { match input_date {
date if date == now_date => format!("Today at {time_format}"), date if date == now_date => SharedString::from(format!("Today at {time_format}")),
date if date == yesterday_date => format!("Yesterday at {time_format}"), date if date == yesterday_date => {
_ => format!("{}, {time_format}", input_time.format("%d/%m/%y")), SharedString::from(format!("Yesterday at {time_format}"))
}
_ => SharedString::from(format!("{}, {time_format}", input_time.format("%d/%m/%y"))),
} }
} }
fn to_ago(&self) -> String { fn to_ago(&self) -> SharedString {
let input_time = match Local.timestamp_opt(self.as_u64() as i64, 0) { let input_time = match Local.timestamp_opt(self.as_u64() as i64, 0) {
chrono::LocalResult::Single(time) => time, chrono::LocalResult::Single(time) => time,
_ => return "1m".into(), _ => return SharedString::from("1m"),
}; };
let now = Local::now(); let now = Local::now();
let duration = now.signed_duration_since(input_time); let duration = now.signed_duration_since(input_time);
match duration { match duration {
d if d.num_seconds() < SECONDS_IN_MINUTE => NOW.into(), d if d.num_seconds() < SECONDS_IN_MINUTE => SharedString::from(NOW),
d if d.num_minutes() < MINUTES_IN_HOUR => format!("{}m", d.num_minutes()), d if d.num_minutes() < MINUTES_IN_HOUR => {
d if d.num_hours() < HOURS_IN_DAY => format!("{}h", d.num_hours()), SharedString::from(format!("{}m", d.num_minutes()))
d if d.num_days() < DAYS_IN_MONTH => format!("{}d", d.num_days()), }
_ => input_time.format("%b %d").to_string(), d if d.num_hours() < HOURS_IN_DAY => SharedString::from(format!("{}h", d.num_hours())),
d if d.num_days() < DAYS_IN_MONTH => SharedString::from(format!("{}d", d.num_days())),
_ => SharedString::from(input_time.format("%b %d").to_string()),
} }
} }
} }

View File

@@ -1,14 +0,0 @@
use nostr_connect::prelude::*;
#[derive(Debug, Clone)]
pub struct CoopAuthUrlHandler;
impl AuthUrlHandler for CoopAuthUrlHandler {
fn on_auth_url(&self, auth_url: Url) -> BoxedFuture<Result<()>> {
Box::pin(async move {
log::info!("Received Auth URL: {auth_url}");
webbrowser::open(auth_url.as_str())?;
Ok(())
})
}
}

View File

@@ -1,6 +1,5 @@
pub mod debounced_delay; pub mod debounced_delay;
pub mod display; pub mod display;
pub mod event; pub mod event;
pub mod handle_auth;
pub mod nip05; pub mod nip05;
pub mod nip96; pub mod nip96;

View File

@@ -62,5 +62,5 @@ oneshot.workspace = true
flume.workspace = true flume.workspace = true
webbrowser.workspace = true webbrowser.workspace = true
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
indexset = "0.12.3" indexset = "0.12.3"
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }

View File

@@ -1,10 +1,24 @@
use std::sync::Mutex; use std::sync::Mutex;
use gpui::{actions, App}; use gpui::{actions, App};
use nostr_connect::prelude::*;
actions!(coop, [ReloadMetadata, DarkMode, Settings, Logout, Quit]); actions!(coop, [ReloadMetadata, DarkMode, Settings, Logout, Quit]);
actions!(sidebar, [Reload, RelayStatus]); actions!(sidebar, [Reload, RelayStatus]);
#[derive(Debug, Clone)]
pub struct CoopAuthUrlHandler;
impl AuthUrlHandler for CoopAuthUrlHandler {
fn on_auth_url(&self, auth_url: Url) -> BoxedFuture<Result<()>> {
Box::pin(async move {
log::info!("Received Auth URL: {auth_url}");
webbrowser::open(auth_url.as_str())?;
Ok(())
})
}
}
pub fn load_embedded_fonts(cx: &App) { pub fn load_embedded_fonts(cx: &App) {
let asset_source = cx.asset_source(); let asset_source = cx.asset_source();
let font_paths = asset_source.list("fonts").unwrap(); let font_paths = asset_source.list("fonts").unwrap();

View File

@@ -7,7 +7,7 @@ use std::time::Duration;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use auto_update::AutoUpdater; use auto_update::AutoUpdater;
use client_keys::ClientKeys; use client_keys::ClientKeys;
use common::display::ReadableProfile; use common::display::RenderedProfile;
use common::event::EventUtils; use common::event::EventUtils;
use global::constants::{ use global::constants::{
ACCOUNT_IDENTIFIER, BOOTSTRAP_RELAYS, DEFAULT_SIDEBAR_WIDTH, METADATA_BATCH_LIMIT, ACCOUNT_IDENTIFIER, BOOTSTRAP_RELAYS, DEFAULT_SIDEBAR_WIDTH, METADATA_BATCH_LIMIT,
@@ -16,9 +16,9 @@ use global::constants::{
use global::{app_state, nostr_client, AuthRequest, Notice, SignalKind, UnwrappingStatus}; use global::{app_state, nostr_client, AuthRequest, Notice, SignalKind, UnwrappingStatus};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, px, rems, App, AppContext, AsyncWindowContext, Axis, Context, Entity, InteractiveElement, deferred, div, px, rems, App, AppContext, AsyncWindowContext, Axis, Context, Entity,
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
Subscription, Task, WeakEntity, Window, StatefulInteractiveElement, Styled, Subscription, Task, WeakEntity, Window,
}; };
use i18n::{shared_t, t}; use i18n::{shared_t, t};
use itertools::Itertools; use itertools::Itertools;
@@ -70,13 +70,13 @@ pub struct ChatSpace {
dock: Entity<DockArea>, dock: Entity<DockArea>,
// All authentication requests // All authentication requests
auth_requests: HashMap<RelayUrl, AuthRequest>, auth_requests: Entity<HashMap<RelayUrl, AuthRequest>>,
// Local state to determine if the user has set up NIP-17 relays // Local state to determine if the user has set up NIP-17 relays
nip17_relays: bool, nip17_relays: bool,
// All subscriptions for observing the app state // All subscriptions for observing the app state
_subscriptions: SmallVec<[Subscription; 3]>, _subscriptions: SmallVec<[Subscription; 4]>,
// All long running tasks // All long running tasks
_tasks: SmallVec<[Task<()>; 5]>, _tasks: SmallVec<[Task<()>; 5]>,
@@ -90,10 +90,18 @@ impl ChatSpace {
let title_bar = cx.new(|_| TitleBar::new()); let title_bar = cx.new(|_| TitleBar::new());
let dock = cx.new(|cx| DockArea::new(window, cx)); let dock = cx.new(|cx| DockArea::new(window, cx));
let auth_requests = cx.new(|_| HashMap::new());
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
let mut tasks = smallvec![]; let mut tasks = smallvec![];
subscriptions.push(
// Automatically sync theme with system appearance
window.observe_window_appearance(|window, cx| {
Theme::sync_system_appearance(Some(window), cx);
}),
);
subscriptions.push( subscriptions.push(
// Observe the client keys and show an alert modal if they fail to initialize // Observe the client keys and show an alert modal if they fail to initialize
cx.observe_in(&client_keys, window, |this, keys, window, cx| { cx.observe_in(&client_keys, window, |this, keys, window, cx| {
@@ -183,7 +191,7 @@ impl ChatSpace {
Self { Self {
dock, dock,
title_bar, title_bar,
auth_requests: HashMap::new(), auth_requests,
nip17_relays: true, nip17_relays: true,
_subscriptions: subscriptions, _subscriptions: subscriptions,
_tasks: tasks, _tasks: tasks,
@@ -954,28 +962,33 @@ impl ChatSpace {
} }
fn reopen_auth_request(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn reopen_auth_request(&mut self, window: &mut Window, cx: &mut Context<Self>) {
for (_, request) in self.auth_requests.clone().into_iter() { for (_, request) in self.auth_requests.read(cx).clone() {
self.open_auth_request(request, window, cx); self.open_auth_request(request, window, cx);
} }
} }
fn push_auth_request(&mut self, req: &AuthRequest, cx: &mut Context<Self>) { fn push_auth_request(&mut self, req: &AuthRequest, cx: &mut Context<Self>) {
self.auth_requests.insert(req.url.clone(), req.to_owned()); self.auth_requests.update(cx, |this, cx| {
this.insert(req.url.clone(), req.to_owned());
cx.notify(); cx.notify();
});
} }
fn sending_auth_request(&mut self, challenge: &str, cx: &mut Context<Self>) { fn sending_auth_request(&mut self, challenge: &str, cx: &mut Context<Self>) {
for (_, req) in self.auth_requests.iter_mut() { self.auth_requests.update(cx, |this, cx| {
for (_, req) in this.iter_mut() {
if req.challenge == challenge { if req.challenge == challenge {
req.sending = true; req.sending = true;
cx.notify(); cx.notify();
} }
} }
});
} }
fn is_sending_auth_request(&self, challenge: &str, _cx: &App) -> bool { fn is_sending_auth_request(&self, challenge: &str, cx: &App) -> bool {
if let Some(req) = self if let Some(req) = self
.auth_requests .auth_requests
.read(cx)
.iter() .iter()
.find(|(_, req)| req.challenge == challenge) .find(|(_, req)| req.challenge == challenge)
{ {
@@ -986,8 +999,10 @@ impl ChatSpace {
} }
fn remove_auth_request(&mut self, challenge: &str, cx: &mut Context<Self>) { fn remove_auth_request(&mut self, challenge: &str, cx: &mut Context<Self>) {
self.auth_requests.retain(|_, r| r.challenge != challenge); self.auth_requests.update(cx, |this, cx| {
this.retain(|_, r| r.challenge != challenge);
cx.notify(); cx.notify();
});
} }
fn set_onboarding_layout(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn set_onboarding_layout(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -1007,7 +1022,7 @@ impl ChatSpace {
window: &mut Window, window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
let panel = Arc::new(account::init(profile, secret, cx)); let panel = Arc::new(account::init(profile, secret, window, cx));
let center = DockItem::panel(panel); let center = DockItem::panel(panel);
self.dock.update(cx, |this, cx| { self.dock.update(cx, |this, cx| {
@@ -1269,7 +1284,7 @@ impl ChatSpace {
.w_full() .w_full()
.child(compose_button()) .child(compose_button())
.when(status != &UnwrappingStatus::Complete, |this| { .when(status != &UnwrappingStatus::Complete, |this| {
this.child( this.child(deferred(
h_flex() h_flex()
.px_2() .px_2()
.h_6() .h_6()
@@ -1278,7 +1293,7 @@ impl ChatSpace {
.rounded_full() .rounded_full()
.bg(cx.theme().surface_background) .bg(cx.theme().surface_background)
.child(shared_t!("loading.label")), .child(shared_t!("loading.label")),
) ))
}) })
} }
@@ -1291,7 +1306,7 @@ impl ChatSpace {
let proxy = AppSettings::get_proxy_user_avatars(cx); let proxy = AppSettings::get_proxy_user_avatars(cx);
let updating = AutoUpdater::read_global(cx).status.is_updating(); let updating = AutoUpdater::read_global(cx).status.is_updating();
let updated = AutoUpdater::read_global(cx).status.is_updated(); let updated = AutoUpdater::read_global(cx).status.is_updated();
let auth_requests = self.auth_requests.len(); let auth_requests = self.auth_requests.read(cx).len();
h_flex() h_flex()
.gap_1() .gap_1()
@@ -1356,7 +1371,7 @@ impl ChatSpace {
.reverse() .reverse()
.transparent() .transparent()
.icon(IconName::CaretDown) .icon(IconName::CaretDown)
.child(Avatar::new(profile.avatar_url(proxy)).size(rems(1.49))) .child(Avatar::new(profile.avatar(proxy)).size(rems(1.49)))
.popup_menu(|this, _window, _cx| { .popup_menu(|this, _window, _cx| {
this.menu(t!("user.dark_mode"), Box::new(DarkMode)) this.menu(t!("user.dark_mode"), Box::new(DarkMode))
.menu(t!("user.settings"), Box::new(Settings)) .menu(t!("user.settings"), Box::new(Settings))
@@ -1479,6 +1494,7 @@ impl Render for ChatSpace {
} }
div() div()
.id(SharedString::from("chatspace"))
.on_action(cx.listener(Self::on_settings)) .on_action(cx.listener(Self::on_settings))
.on_action(cx.listener(Self::on_dark_mode)) .on_action(cx.listener(Self::on_dark_mode))
.on_action(cx.listener(Self::on_sign_out)) .on_action(cx.listener(Self::on_sign_out))

View File

@@ -8,7 +8,6 @@ use gpui::{
TitlebarOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind, TitlebarOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind,
WindowOptions, WindowOptions,
}; };
use theme::Theme;
use ui::Root; use ui::Root;
use crate::actions::{load_embedded_fonts, quit, Quit}; use crate::actions::{load_embedded_fonts, quit, Quit};
@@ -79,13 +78,6 @@ fn main() {
// Bring the app to the foreground // Bring the app to the foreground
cx.activate(true); cx.activate(true);
// Automatically sync theme with system appearance
window
.observe_window_appearance(|window, cx| {
Theme::sync_system_appearance(Some(window), cx);
})
.detach();
// Root Entity // Root Entity
cx.new(|cx| { cx.new(|cx| {
// Initialize the tokio runtime // Initialize the tokio runtime

View File

@@ -2,15 +2,15 @@ use std::time::Duration;
use anyhow::Error; use anyhow::Error;
use client_keys::ClientKeys; use client_keys::ClientKeys;
use common::display::ReadableProfile; use common::display::RenderedProfile;
use common::handle_auth::CoopAuthUrlHandler;
use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT}; use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT};
use global::{app_state, nostr_client, SignalKind}; use global::{app_state, nostr_client, SignalKind};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, relative, rems, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter, div, relative, rems, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter,
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, SharedString, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render,
StatefulInteractiveElement, Styled, Task, WeakEntity, Window, RetainAllImageCache, SharedString, StatefulInteractiveElement, Styled, Subscription, Task,
WeakEntity, Window,
}; };
use i18n::{shared_t, t}; use i18n::{shared_t, t};
use nostr_connect::prelude::*; use nostr_connect::prelude::*;
@@ -26,10 +26,16 @@ use ui::notification::Notification;
use ui::popup_menu::PopupMenu; use ui::popup_menu::PopupMenu;
use ui::{h_flex, v_flex, ContextModal, Sizable, StyledExt}; use ui::{h_flex, v_flex, ContextModal, Sizable, StyledExt};
use crate::actions::CoopAuthUrlHandler;
use crate::chatspace::ChatSpace; use crate::chatspace::ChatSpace;
pub fn init(profile: Profile, secret: String, cx: &mut App) -> Entity<Account> { pub fn init(
cx.new(|cx| Account::new(secret, profile, cx)) profile: Profile,
secret: String,
window: &mut Window,
cx: &mut App,
) -> Entity<Account> {
cx.new(|cx| Account::new(secret, profile, window, cx))
} }
pub struct Account { pub struct Account {
@@ -38,17 +44,32 @@ pub struct Account {
is_bunker: bool, is_bunker: bool,
is_extension: bool, is_extension: bool,
loading: bool, loading: bool,
// Panel
name: SharedString, name: SharedString,
focus_handle: FocusHandle, focus_handle: FocusHandle,
image_cache: Entity<RetainAllImageCache>,
_subscriptions: SmallVec<[Subscription; 1]>,
_tasks: SmallVec<[Task<()>; 1]>, _tasks: SmallVec<[Task<()>; 1]>,
} }
impl Account { impl Account {
fn new(secret: String, profile: Profile, cx: &mut App) -> Self { fn new(secret: String, profile: Profile, window: &mut Window, cx: &mut Context<Self>) -> Self {
let is_bunker = secret.starts_with("bunker://"); let is_bunker = secret.starts_with("bunker://");
let is_extension = secret.starts_with("extension"); let is_extension = secret.starts_with("extension");
let mut subscriptions = smallvec![];
subscriptions.push(
// Clear the local state when user closes the account panel
cx.on_release_in(window, move |this, window, cx| {
this.stored_secret.clear();
this.image_cache.update(cx, |this, cx| {
this.clear(window, cx);
});
}),
);
Self { Self {
profile, profile,
is_bunker, is_bunker,
@@ -57,6 +78,8 @@ impl Account {
loading: false, loading: false,
name: "Account".into(), name: "Account".into(),
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
image_cache: RetainAllImageCache::new(cx),
_subscriptions: subscriptions,
_tasks: smallvec![], _tasks: smallvec![],
} }
} }
@@ -298,6 +321,7 @@ impl Focusable for Account {
impl Render for Account { impl Render for Account {
fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex() v_flex()
.image_cache(self.image_cache.clone())
.relative() .relative()
.size_full() .size_full()
.gap_10() .gap_10()
@@ -353,7 +377,7 @@ impl Render for Account {
) )
}) })
.when(!self.loading, |this| { .when(!self.loading, |this| {
let avatar = self.profile.avatar_url(true); let avatar = self.profile.avatar(true);
let name = self.profile.display_name(); let name = self.profile.display_name();
this.child( this.child(
@@ -370,6 +394,8 @@ impl Render for Account {
.child( .child(
div() div()
.when(self.is_bunker, |this| { .when(self.is_bunker, |this| {
let label = SharedString::from("Nostr Connect");
this.child( this.child(
div() div()
.py_0p5() .py_0p5()
@@ -380,10 +406,12 @@ impl Render for Account {
cx.theme().secondary_foreground, cx.theme().secondary_foreground,
) )
.rounded_full() .rounded_full()
.child("Nostr Connect"), .child(label),
) )
}) })
.when(self.is_extension, |this| { .when(self.is_extension, |this| {
let label = SharedString::from("Extension");
this.child( this.child(
div() div()
.py_0p5() .py_0p5()
@@ -394,7 +422,7 @@ impl Render for Account {
cx.theme().secondary_foreground, cx.theme().secondary_foreground,
) )
.rounded_full() .rounded_full()
.child("Extension"), .child(label),
) )
}), }),
), ),

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use common::display::{ReadableProfile, ReadableTimestamp}; use common::display::{RenderedProfile, RenderedTimestamp};
use common::nip96::nip96_upload; use common::nip96::nip96_upload;
use global::{app_state, nostr_client}; use global::{app_state, nostr_client};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
@@ -8,7 +8,7 @@ use gpui::{
div, img, list, px, red, relative, rems, svg, white, Action, AnyElement, App, AppContext, div, img, list, px, red, relative, rems, svg, white, Action, AnyElement, App, AppContext,
ClipboardItem, Context, Element, Entity, EventEmitter, Flatten, FocusHandle, Focusable, ClipboardItem, Context, Element, Entity, EventEmitter, Flatten, FocusHandle, Focusable,
InteractiveElement, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit, InteractiveElement, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit,
ParentElement, PathPromptOptions, Render, RetainAllImageCache, SharedString, ParentElement, PathPromptOptions, Render, RetainAllImageCache, SharedString, SharedUri,
StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, Window, StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
@@ -729,7 +729,7 @@ impl Chat {
.flex() .flex()
.gap_3() .gap_3()
.when(!hide_avatar, |this| { .when(!hide_avatar, |this| {
this.child(Avatar::new(author.avatar_url(proxy)).size(rems(2.))) this.child(Avatar::new(author.avatar(proxy)).size(rems(2.)))
}) })
.child( .child(
v_flex() v_flex()
@@ -748,7 +748,7 @@ impl Chat {
.text_color(cx.theme().text) .text_color(cx.theme().text)
.child(author.display_name()), .child(author.display_name()),
) )
.child(div().child(message.created_at.to_human_time())) .child(message.created_at.to_human_time())
.when_some(is_sent_success, |this, status| { .when_some(is_sent_success, |this, status| {
this.when(status, |this| { this.when(status, |this| {
this.child(self.render_message_sent(&id, cx)) this.child(self.render_message_sent(&id, cx))
@@ -784,14 +784,12 @@ impl Chat {
.child(self.render_actions(&id, cx)) .child(self.render_actions(&id, cx))
.on_mouse_down( .on_mouse_down(
MouseButton::Middle, MouseButton::Middle,
cx.listener(move |this, _event, _window, cx| { cx.listener(move |this, _, _window, cx| {
this.copy_message(&id, cx); this.copy_message(&id, cx);
}), }),
) )
.on_double_click(cx.listener({ .on_double_click(cx.listener(move |this, _, _window, cx| {
move |this, _event, _window, cx| {
this.reply_to(&id, cx); this.reply_to(&id, cx);
}
})) }))
.hover(|this| this.bg(cx.theme().surface_background)) .hover(|this| this.bg(cx.theme().surface_background))
.into_any_element() .into_any_element()
@@ -828,7 +826,7 @@ impl Chat {
.w_full() .w_full()
.text_ellipsis() .text_ellipsis()
.line_clamp(1) .line_clamp(1)
.child(message.content.clone()), .child(SharedString::from(&message.content)),
) )
.hover(|this| this.bg(cx.theme().elevated_surface_background)) .hover(|this| this.bg(cx.theme().elevated_surface_background))
.on_click({ .on_click({
@@ -902,7 +900,7 @@ impl Chat {
let registry = Registry::read_global(cx); let registry = Registry::read_global(cx);
let profile = registry.get_person(&report.receiver, cx); let profile = registry.get_person(&report.receiver, cx);
let name = profile.display_name(); let name = profile.display_name();
let avatar = profile.avatar_url(true); let avatar = profile.avatar(true);
v_flex() v_flex()
.gap_2() .gap_2()
@@ -1094,15 +1092,12 @@ impl Chat {
} }
fn render_attachment(&self, url: &Url, cx: &Context<Self>) -> impl IntoElement { fn render_attachment(&self, url: &Url, cx: &Context<Self>) -> impl IntoElement {
let url = url.clone();
let path: SharedString = url.to_string().into();
div() div()
.id(SharedString::from(url.to_string())) .id(SharedString::from(url.to_string()))
.relative() .relative()
.w_16() .w_16()
.child( .child(
img(path.clone()) img(SharedUri::from(url.to_string()))
.size_16() .size_16()
.shadow_lg() .shadow_lg()
.rounded(cx.theme().radius) .rounded(cx.theme().radius)
@@ -1121,9 +1116,12 @@ impl Chat {
.bg(red()) .bg(red())
.child(Icon::new(IconName::Close).size_2().text_color(white())), .child(Icon::new(IconName::Close).size_2().text_color(white())),
) )
.on_click(cx.listener(move |this, _, window, cx| { .on_click({
let url = url.clone();
cx.listener(move |this, _, window, cx| {
this.remove_attachment(&url, window, cx); this.remove_attachment(&url, window, cx);
})) })
})
} }
fn render_attachment_list( fn render_attachment_list(
@@ -1188,7 +1186,7 @@ impl Chat {
.text_sm() .text_sm()
.text_ellipsis() .text_ellipsis()
.line_clamp(1) .line_clamp(1)
.child(text.content.clone()), .child(SharedString::from(&text.content)),
) )
} else { } else {
div() div()
@@ -1405,9 +1403,7 @@ impl Render for Chat {
.px_3() .px_3()
.py_2() .py_2()
.child( .child(
div() v_flex()
.flex()
.flex_col()
.gap_1p5() .gap_1p5()
.children(self.render_attachment_list(window, cx)) .children(self.render_attachment_list(window, cx))
.children(self.render_reply_list(window, cx)) .children(self.render_reply_list(window, cx))

View File

@@ -2,7 +2,7 @@ use gpui::{
div, App, AppContext, Context, Entity, IntoElement, ParentElement, Render, SharedString, div, App, AppContext, Context, Entity, IntoElement, ParentElement, Render, SharedString,
Styled, Window, Styled, Window,
}; };
use i18n::t; use i18n::{shared_t, t};
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::input::{InputState, TextInput}; use ui::input::{InputState, TextInput};
use ui::{v_flex, Sizable}; use ui::{v_flex, Sizable};
@@ -41,7 +41,7 @@ impl Render for Subject {
div() div()
.text_sm() .text_sm()
.text_color(cx.theme().text_muted) .text_color(cx.theme().text_muted)
.child(SharedString::new(t!("subject.title"))), .child(shared_t!("subject.title")),
) )
.child(TextInput::new(&self.input).small()) .child(TextInput::new(&self.input).small())
.child( .child(
@@ -49,7 +49,7 @@ impl Render for Subject {
.text_xs() .text_xs()
.italic() .italic()
.text_color(cx.theme().text_placeholder) .text_color(cx.theme().text_placeholder)
.child(SharedString::new(t!("subject.help_text"))), .child(shared_t!("subject.help_text")),
) )
} }
} }

View File

@@ -2,15 +2,15 @@ use std::ops::Range;
use std::time::Duration; use std::time::Duration;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use common::display::{ReadableProfile, TextUtils}; use common::display::{RenderedProfile, TextUtils};
use common::nip05::nip05_profile; use common::nip05::nip05_profile;
use global::constants::BOOTSTRAP_RELAYS; use global::constants::BOOTSTRAP_RELAYS;
use global::{app_state, nostr_client}; use global::{app_state, nostr_client};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, px, relative, rems, uniform_list, App, AppContext, Context, Entity, InteractiveElement, div, px, relative, rems, uniform_list, App, AppContext, Context, Entity, InteractiveElement,
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, IntoElement, ParentElement, Render, RetainAllImageCache, SharedString,
Subscription, Task, Window, StatefulInteractiveElement, Styled, Subscription, Task, Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use i18n::{shared_t, t}; use i18n::{shared_t, t};
@@ -110,7 +110,8 @@ pub struct Compose {
/// Error message /// Error message
error_message: Entity<Option<SharedString>>, error_message: Entity<Option<SharedString>>,
_subscriptions: SmallVec<[Subscription; 1]>, image_cache: Entity<RetainAllImageCache>,
_subscriptions: SmallVec<[Subscription; 2]>,
_tasks: SmallVec<[Task<()>; 1]>, _tasks: SmallVec<[Task<()>; 1]>,
} }
@@ -161,6 +162,15 @@ impl Compose {
}), }),
); );
subscriptions.push(
// Clear the image cache when sidebar is closed
cx.on_release_in(window, move |this, window, cx| {
this.image_cache.update(cx, |this, cx| {
this.clear(window, cx);
})
}),
);
subscriptions.push( subscriptions.push(
// Handle Enter event for user input // Handle Enter event for user input
cx.subscribe_in( cx.subscribe_in(
@@ -179,6 +189,7 @@ impl Compose {
user_input, user_input,
error_message, error_message,
contacts, contacts,
image_cache: RetainAllImageCache::new(cx),
_subscriptions: subscriptions, _subscriptions: subscriptions,
_tasks: tasks, _tasks: tasks,
} }
@@ -389,7 +400,7 @@ impl Compose {
h_flex() h_flex()
.gap_1p5() .gap_1p5()
.text_sm() .text_sm()
.child(Avatar::new(profile.avatar_url(proxy)).size(rems(1.75))) .child(Avatar::new(profile.avatar(proxy)).size(rems(1.75)))
.child(profile.display_name()), .child(profile.display_name()),
) )
.when(contact.selected, |this| { .when(contact.selected, |this| {
@@ -417,6 +428,7 @@ impl Render for Compose {
let contacts = self.contacts.read(cx); let contacts = self.contacts.read(cx);
v_flex() v_flex()
.image_cache(self.image_cache.clone())
.gap_2() .gap_2()
.child( .child(
div() div()

View File

@@ -8,7 +8,7 @@ use gpui::{
div, img, App, AppContext, Context, Entity, Flatten, IntoElement, ParentElement, div, img, App, AppContext, Context, Entity, Flatten, IntoElement, ParentElement,
PathPromptOptions, Render, SharedString, Styled, Task, Window, PathPromptOptions, Render, SharedString, Styled, Task, Window,
}; };
use i18n::t; use i18n::{shared_t, t};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use settings::AppSettings; use settings::AppSettings;
use smol::fs; use smol::fs;
@@ -260,7 +260,7 @@ impl Render for EditProfile {
.flex_col() .flex_col()
.gap_1() .gap_1()
.text_sm() .text_sm()
.child(SharedString::new(t!("profile.label_name"))) .child(shared_t!("profile.label_name"))
.child(TextInput::new(&self.name_input).small()), .child(TextInput::new(&self.name_input).small()),
) )
.child( .child(
@@ -269,7 +269,7 @@ impl Render for EditProfile {
.flex_col() .flex_col()
.gap_1() .gap_1()
.text_sm() .text_sm()
.child(SharedString::new(t!("profile.label_website"))) .child(shared_t!("profile.label_website"))
.child(TextInput::new(&self.website_input).small()), .child(TextInput::new(&self.website_input).small()),
) )
.child( .child(
@@ -278,7 +278,7 @@ impl Render for EditProfile {
.flex_col() .flex_col()
.gap_1() .gap_1()
.text_sm() .text_sm()
.child(SharedString::new(t!("profile.label_bio"))) .child(shared_t!("profile.label_bio"))
.child(TextInput::new(&self.bio_input).small()), .child(TextInput::new(&self.bio_input).small()),
) )
} }

View File

@@ -1,7 +1,6 @@
use std::time::Duration; use std::time::Duration;
use client_keys::ClientKeys; use client_keys::ClientKeys;
use common::handle_auth::CoopAuthUrlHandler;
use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT}; use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT};
use global::nostr_client; use global::nostr_client;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
@@ -19,6 +18,8 @@ use ui::input::{InputEvent, InputState, TextInput};
use ui::popup_menu::PopupMenu; use ui::popup_menu::PopupMenu;
use ui::{v_flex, ContextModal, Disableable, Sizable, StyledExt}; use ui::{v_flex, ContextModal, Disableable, Sizable, StyledExt};
use crate::actions::CoopAuthUrlHandler;
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Login> { pub fn init(window: &mut Window, cx: &mut App) -> Entity<Login> {
Login::new(window, cx) Login::new(window, cx)
} }

View File

@@ -1,4 +1,4 @@
use common::display::ReadableProfile; use common::display::RenderedProfile;
use gpui::http_client::Url; use gpui::http_client::Url;
use gpui::{ use gpui::{
div, px, relative, rems, App, AppContext, Context, Entity, InteractiveElement, IntoElement, div, px, relative, rems, App, AppContext, Context, Entity, InteractiveElement, IntoElement,
@@ -141,7 +141,7 @@ impl Render for Preferences {
h_flex() h_flex()
.id("user") .id("user")
.gap_2() .gap_2()
.child(Avatar::new(profile.avatar_url(proxy)).size(rems(2.4))) .child(Avatar::new(profile.avatar(proxy)).size(rems(2.4)))
.child( .child(
div() div()
.flex_1() .flex_1()

View File

@@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use common::display::{shorten_pubkey, ReadableProfile, ReadableTimestamp}; use common::display::{shorten_pubkey, RenderedProfile, RenderedTimestamp};
use common::nip05::nip05_verify; use common::nip05::nip05_verify;
use global::constants::BOOTSTRAP_RELAYS; use global::constants::BOOTSTRAP_RELAYS;
use global::nostr_client; use global::nostr_client;
@@ -202,9 +202,7 @@ impl Screening {
.hover(|this| { .hover(|this| {
this.bg(cx.theme().elevated_surface_background) this.bg(cx.theme().elevated_surface_background)
}) })
.child( .child(Avatar::new(contact.avatar(true)).size(rems(1.75)))
Avatar::new(contact.avatar_url(true)).size(rems(1.75)),
)
.child(contact.display_name()), .child(contact.display_name()),
); );
} }
@@ -234,7 +232,7 @@ impl Render for Screening {
.items_center() .items_center()
.justify_center() .justify_center()
.text_center() .text_center()
.child(Avatar::new(self.profile.avatar_url(proxy)).size(rems(4.))) .child(Avatar::new(self.profile.avatar(proxy)).size(rems(4.)))
.child( .child(
div() div()
.font_semibold() .font_semibold()

View File

@@ -3,7 +3,7 @@ use std::rc::Rc;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, rems, App, ClickEvent, InteractiveElement, IntoElement, ParentElement as _, RenderOnce, div, rems, App, ClickEvent, InteractiveElement, IntoElement, ParentElement as _, RenderOnce,
SharedString, StatefulInteractiveElement, Styled, Window, SharedString, SharedUri, StatefulInteractiveElement, Styled, Window,
}; };
use i18n::t; use i18n::t;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
@@ -24,7 +24,7 @@ pub struct RoomListItem {
room_id: Option<u64>, room_id: Option<u64>,
public_key: Option<PublicKey>, public_key: Option<PublicKey>,
name: Option<SharedString>, name: Option<SharedString>,
avatar: Option<SharedString>, avatar: Option<SharedUri>,
created_at: Option<SharedString>, created_at: Option<SharedString>,
kind: Option<RoomKind>, kind: Option<RoomKind>,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
@@ -60,7 +60,7 @@ impl RoomListItem {
self self
} }
pub fn avatar(mut self, avatar: impl Into<SharedString>) -> Self { pub fn avatar(mut self, avatar: impl Into<SharedUri>) -> Self {
self.avatar = Some(avatar.into()); self.avatar = Some(avatar.into());
self self
} }

View File

@@ -4,13 +4,13 @@ use std::time::Duration;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use common::debounced_delay::DebouncedDelay; use common::debounced_delay::DebouncedDelay;
use common::display::{ReadableTimestamp, TextUtils}; use common::display::{RenderedTimestamp, TextUtils};
use global::constants::{BOOTSTRAP_RELAYS, SEARCH_RELAYS}; use global::constants::{BOOTSTRAP_RELAYS, SEARCH_RELAYS};
use global::{app_state, nostr_client, UnwrappingStatus}; use global::{app_state, nostr_client, UnwrappingStatus};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, relative, uniform_list, AnyElement, App, AppContext, Context, Entity, EventEmitter, deferred, div, relative, uniform_list, AnyElement, App, AppContext, Context, Entity,
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render,
RetainAllImageCache, SharedString, Styled, Subscription, Task, Window, RetainAllImageCache, SharedString, Styled, Subscription, Task, Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
@@ -56,7 +56,7 @@ pub struct Sidebar {
focus_handle: FocusHandle, focus_handle: FocusHandle,
image_cache: Entity<RetainAllImageCache>, image_cache: Entity<RetainAllImageCache>,
#[allow(dead_code)] #[allow(dead_code)]
subscriptions: SmallVec<[Subscription; 2]>, subscriptions: SmallVec<[Subscription; 3]>,
} }
impl Sidebar { impl Sidebar {
@@ -77,23 +77,30 @@ impl Sidebar {
let registry = Registry::global(cx); let registry = Registry::global(cx);
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
subscriptions.push(cx.subscribe_in( subscriptions.push(
&registry, // Clear the image cache when sidebar is closed
window, cx.on_release_in(window, move |this, window, cx| {
move |this, _, event, _window, cx| { this.image_cache.update(cx, |this, cx| {
this.clear(window, cx);
})
}),
);
subscriptions.push(
// Subscribe for registry new events
cx.subscribe_in(&registry, window, move |this, _, event, _window, cx| {
if let RegistryEvent::NewRequest(kind) = event { if let RegistryEvent::NewRequest(kind) = event {
this.indicator.update(cx, |this, cx| { this.indicator.update(cx, |this, cx| {
*this = Some(kind.to_owned()); *this = Some(kind.to_owned());
cx.notify(); cx.notify();
}); });
} }
}, }),
)); );
subscriptions.push(cx.subscribe_in( subscriptions.push(
&find_input, // Subscribe for find input events
window, cx.subscribe_in(&find_input, window, |this, _state, event, window, cx| {
|this, _state, event, window, cx| {
match event { match event {
InputEvent::PressEnter { .. } => this.search(window, cx), InputEvent::PressEnter { .. } => this.search(window, cx),
InputEvent::Change(text) => { InputEvent::Change(text) => {
@@ -112,8 +119,8 @@ impl Sidebar {
} }
_ => {} _ => {}
} }
}, }),
)); );
Self { Self {
name: "Sidebar".into(), name: "Sidebar".into(),
@@ -744,9 +751,9 @@ impl Render for Sidebar {
.tooltip(t!("sidebar.all_conversations_tooltip")) .tooltip(t!("sidebar.all_conversations_tooltip"))
.when_some(self.indicator.read(cx).as_ref(), |this, kind| { .when_some(self.indicator.read(cx).as_ref(), |this, kind| {
this.when(kind == &RoomKind::Ongoing, |this| { this.when(kind == &RoomKind::Ongoing, |this| {
this.child( this.child(deferred(
div().size_1().rounded_full().bg(cx.theme().cursor), div().size_1().rounded_full().bg(cx.theme().cursor),
) ))
}) })
}) })
.small() .small()
@@ -765,9 +772,9 @@ impl Render for Sidebar {
.tooltip(t!("sidebar.requests_tooltip")) .tooltip(t!("sidebar.requests_tooltip"))
.when_some(self.indicator.read(cx).as_ref(), |this, kind| { .when_some(self.indicator.read(cx).as_ref(), |this, kind| {
this.when(kind != &RoomKind::Ongoing, |this| { this.when(kind != &RoomKind::Ongoing, |this| {
this.child( this.child(deferred(
div().size_1().rounded_full().bg(cx.theme().cursor), div().size_1().rounded_full().bg(cx.theme().cursor),
) ))
}) })
}) })
.small() .small()
@@ -809,7 +816,7 @@ impl Render for Sidebar {
.when(!loading && total_rooms == 0, |this| { .when(!loading && total_rooms == 0, |this| {
this.map(|this| { this.map(|this| {
if self.filter(&RoomKind::Ongoing, cx) { if self.filter(&RoomKind::Ongoing, cx) {
this.child( this.child(deferred(
v_flex() v_flex()
.py_2() .py_2()
.gap_1p5() .gap_1p5()
@@ -830,9 +837,9 @@ impl Render for Sidebar {
.line_height(relative(1.25)) .line_height(relative(1.25))
.child(shared_t!("sidebar.no_conversations_label")), .child(shared_t!("sidebar.no_conversations_label")),
), ),
) ))
} else { } else {
this.child( this.child(deferred(
v_flex() v_flex()
.py_2() .py_2()
.gap_1p5() .gap_1p5()
@@ -853,7 +860,7 @@ impl Render for Sidebar {
.line_height(relative(1.25)) .line_height(relative(1.25))
.child(shared_t!("sidebar.no_requests_label")), .child(shared_t!("sidebar.no_requests_label")),
), ),
) ))
} }
}) })
}) })

View File

@@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use common::display::ReadableProfile; use common::display::RenderedProfile;
use common::nip05::nip05_verify; use common::nip05::nip05_verify;
use global::nostr_client; use global::nostr_client;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
@@ -128,9 +128,8 @@ impl UserProfile {
impl Render for UserProfile { impl Render for UserProfile {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let proxy = AppSettings::get_proxy_user_avatars(cx); let proxy = AppSettings::get_proxy_user_avatars(cx);
let bech32 = self.profile.public_key().to_bech32().unwrap();
let Ok(bech32) = self.profile.public_key().to_bech32(); let shared_bech32 = SharedString::from(bech32);
let shared_bech32 = SharedString::new(bech32);
v_flex() v_flex()
.gap_4() .gap_4()
@@ -140,7 +139,7 @@ impl Render for UserProfile {
.items_center() .items_center()
.justify_center() .justify_center()
.text_center() .text_center()
.child(Avatar::new(self.profile.avatar_url(proxy)).size(rems(4.))) .child(Avatar::new(self.profile.avatar(proxy)).size(rems(4.)))
.child( .child(
v_flex() v_flex()
.child( .child(
@@ -194,7 +193,7 @@ impl Render for UserProfile {
div() div()
.block() .block()
.text_color(cx.theme().text_muted) .text_color(cx.theme().text_muted)
.child("Public Key:"), .child(SharedString::from("Public Key:")),
) )
.child( .child(
h_flex() h_flex()
@@ -245,7 +244,8 @@ impl Render for UserProfile {
self.profile self.profile
.metadata() .metadata()
.about .about
.unwrap_or(t!("profile.no_bio").to_string()), .map(SharedString::from)
.unwrap_or(shared_t!("profile.no_bio")),
), ),
), ),
) )

View File

@@ -95,7 +95,7 @@ impl Render for Welcome {
div() div()
.font_semibold() .font_semibold()
.text_color(cx.theme().text_muted) .text_color(cx.theme().text_muted)
.child("coop on nostr"), .child(SharedString::from("coop on nostr")),
) )
.child( .child(
div() div()

View File

@@ -29,10 +29,10 @@ macro_rules! init {
#[macro_export] #[macro_export]
macro_rules! shared_t { macro_rules! shared_t {
($key:expr) => { ($key:expr) => {
SharedString::new(t!($key)) SharedString::from(t!($key))
}; };
($key:expr, $($param:ident = $value:expr),+) => { ($key:expr, $($param:ident = $value:expr),+) => {
SharedString::new(t!($key, $($param = $value),+)) SharedString::from(t!($key, $($param = $value),+))
}; };
} }

View File

@@ -4,11 +4,11 @@ use std::hash::{Hash, Hasher};
use std::time::Duration; use std::time::Duration;
use anyhow::Error; use anyhow::Error;
use common::display::ReadableProfile; use common::display::RenderedProfile;
use common::event::EventUtils; use common::event::EventUtils;
use global::constants::SEND_RETRY; use global::constants::SEND_RETRY;
use global::{app_state, nostr_client}; use global::{app_state, nostr_client};
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task}; use gpui::{App, AppContext, Context, EventEmitter, SharedString, SharedUri, Task};
use itertools::Itertools; use itertools::Itertools;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
@@ -267,22 +267,22 @@ impl Room {
} }
/// Gets the display name for the room /// Gets the display name for the room
pub fn display_name(&self, cx: &App) -> String { pub fn display_name(&self, cx: &App) -> SharedString {
if let Some(subject) = self.subject.clone() { if let Some(subject) = self.subject.clone() {
subject SharedString::from(subject)
} else { } else {
self.merge_name(cx) self.merged_name(cx)
} }
} }
/// Gets the display image for the room /// Gets the display image for the room
pub fn display_image(&self, proxy: bool, cx: &App) -> String { pub fn display_image(&self, proxy: bool, cx: &App) -> SharedUri {
if let Some(picture) = self.picture.as_ref() { if let Some(picture) = self.picture.as_ref() {
picture.clone() SharedUri::from(picture)
} else if !self.is_group() { } else if !self.is_group() {
self.first_member(cx).avatar_url(proxy) self.first_member(cx).avatar(proxy)
} else { } else {
"brand/group.png".into() SharedUri::from("brand/group.png")
} }
} }
@@ -295,7 +295,7 @@ impl Room {
} }
/// Merge the names of the first two members of the room. /// Merge the names of the first two members of the room.
pub(crate) fn merge_name(&self, cx: &App) -> String { pub(crate) fn merged_name(&self, cx: &App) -> SharedString {
let registry = Registry::read_global(cx); let registry = Registry::read_global(cx);
if self.is_group() { if self.is_group() {
@@ -308,7 +308,7 @@ impl Room {
let mut name = profiles let mut name = profiles
.iter() .iter()
.take(2) .take(2)
.map(|p| p.display_name()) .map(|p| p.name())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
@@ -316,7 +316,7 @@ impl Room {
name = format!("{}, +{}", name, profiles.len() - 2); name = format!("{}, +{}", name, profiles.len() - 2);
} }
name SharedString::from(name)
} else { } else {
self.first_member(cx).display_name() self.first_member(cx).display_name()
} }

View File

@@ -194,7 +194,7 @@ impl Root {
} }
} }
// Render Notification layer. /// Render Notification layer.
pub fn render_notification_layer( pub fn render_notification_layer(
window: &mut Window, window: &mut Window,
cx: &mut App, cx: &mut App,

View File

@@ -1,7 +1,7 @@
use std::ops::Range; use std::ops::Range;
use std::sync::Arc; use std::sync::Arc;
use common::display::ReadableProfile; use common::display::RenderedProfile;
use gpui::{ use gpui::{
AnyElement, AnyView, App, ElementId, HighlightStyle, InteractiveText, IntoElement, AnyElement, AnyView, App, ElementId, HighlightStyle, InteractiveText, IntoElement,
SharedString, StyledText, UnderlineStyle, Window, SharedString, StyledText, UnderlineStyle, Window,