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

View File

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

View File

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

View File

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

View File

@@ -1,10 +1,24 @@
use std::sync::Mutex;
use gpui::{actions, App};
use nostr_connect::prelude::*;
actions!(coop, [ReloadMetadata, DarkMode, Settings, Logout, Quit]);
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) {
let asset_source = cx.asset_source();
let font_paths = asset_source.list("fonts").unwrap();

View File

@@ -7,7 +7,7 @@ use std::time::Duration;
use anyhow::{anyhow, Error};
use auto_update::AutoUpdater;
use client_keys::ClientKeys;
use common::display::ReadableProfile;
use common::display::RenderedProfile;
use common::event::EventUtils;
use global::constants::{
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 gpui::prelude::FluentBuilder;
use gpui::{
div, px, rems, App, AppContext, AsyncWindowContext, Axis, Context, Entity, InteractiveElement,
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
Subscription, Task, WeakEntity, Window,
deferred, div, px, rems, App, AppContext, AsyncWindowContext, Axis, Context, Entity,
InteractiveElement, IntoElement, ParentElement, Render, SharedString,
StatefulInteractiveElement, Styled, Subscription, Task, WeakEntity, Window,
};
use i18n::{shared_t, t};
use itertools::Itertools;
@@ -70,13 +70,13 @@ pub struct ChatSpace {
dock: Entity<DockArea>,
// 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
nip17_relays: bool,
// All subscriptions for observing the app state
_subscriptions: SmallVec<[Subscription; 3]>,
_subscriptions: SmallVec<[Subscription; 4]>,
// All long running tasks
_tasks: SmallVec<[Task<()>; 5]>,
@@ -90,10 +90,18 @@ impl ChatSpace {
let title_bar = cx.new(|_| TitleBar::new());
let dock = cx.new(|cx| DockArea::new(window, cx));
let auth_requests = cx.new(|_| HashMap::new());
let mut subscriptions = 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(
// Observe the client keys and show an alert modal if they fail to initialize
cx.observe_in(&client_keys, window, |this, keys, window, cx| {
@@ -183,7 +191,7 @@ impl ChatSpace {
Self {
dock,
title_bar,
auth_requests: HashMap::new(),
auth_requests,
nip17_relays: true,
_subscriptions: subscriptions,
_tasks: tasks,
@@ -954,28 +962,33 @@ impl ChatSpace {
}
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);
}
}
fn push_auth_request(&mut self, req: &AuthRequest, cx: &mut Context<Self>) {
self.auth_requests.insert(req.url.clone(), req.to_owned());
cx.notify();
self.auth_requests.update(cx, |this, cx| {
this.insert(req.url.clone(), req.to_owned());
cx.notify();
});
}
fn sending_auth_request(&mut self, challenge: &str, cx: &mut Context<Self>) {
for (_, req) in self.auth_requests.iter_mut() {
if req.challenge == challenge {
req.sending = true;
cx.notify();
self.auth_requests.update(cx, |this, cx| {
for (_, req) in this.iter_mut() {
if req.challenge == challenge {
req.sending = true;
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
.auth_requests
.read(cx)
.iter()
.find(|(_, req)| req.challenge == challenge)
{
@@ -986,8 +999,10 @@ impl ChatSpace {
}
fn remove_auth_request(&mut self, challenge: &str, cx: &mut Context<Self>) {
self.auth_requests.retain(|_, r| r.challenge != challenge);
cx.notify();
self.auth_requests.update(cx, |this, cx| {
this.retain(|_, r| r.challenge != challenge);
cx.notify();
});
}
fn set_onboarding_layout(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -1007,7 +1022,7 @@ impl ChatSpace {
window: &mut Window,
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);
self.dock.update(cx, |this, cx| {
@@ -1269,7 +1284,7 @@ impl ChatSpace {
.w_full()
.child(compose_button())
.when(status != &UnwrappingStatus::Complete, |this| {
this.child(
this.child(deferred(
h_flex()
.px_2()
.h_6()
@@ -1278,7 +1293,7 @@ impl ChatSpace {
.rounded_full()
.bg(cx.theme().surface_background)
.child(shared_t!("loading.label")),
)
))
})
}
@@ -1291,7 +1306,7 @@ impl ChatSpace {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let updating = AutoUpdater::read_global(cx).status.is_updating();
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()
.gap_1()
@@ -1356,7 +1371,7 @@ impl ChatSpace {
.reverse()
.transparent()
.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| {
this.menu(t!("user.dark_mode"), Box::new(DarkMode))
.menu(t!("user.settings"), Box::new(Settings))
@@ -1479,6 +1494,7 @@ impl Render for ChatSpace {
}
div()
.id(SharedString::from("chatspace"))
.on_action(cx.listener(Self::on_settings))
.on_action(cx.listener(Self::on_dark_mode))
.on_action(cx.listener(Self::on_sign_out))

View File

@@ -8,7 +8,6 @@ use gpui::{
TitlebarOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind,
WindowOptions,
};
use theme::Theme;
use ui::Root;
use crate::actions::{load_embedded_fonts, quit, Quit};
@@ -79,13 +78,6 @@ fn main() {
// Bring the app to the foreground
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
cx.new(|cx| {
// Initialize the tokio runtime

View File

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

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
use common::display::{ReadableProfile, ReadableTimestamp};
use common::display::{RenderedProfile, RenderedTimestamp};
use common::nip96::nip96_upload;
use global::{app_state, nostr_client};
use gpui::prelude::FluentBuilder;
@@ -8,7 +8,7 @@ use gpui::{
div, img, list, px, red, relative, rems, svg, white, Action, AnyElement, App, AppContext,
ClipboardItem, Context, Element, Entity, EventEmitter, Flatten, FocusHandle, Focusable,
InteractiveElement, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit,
ParentElement, PathPromptOptions, Render, RetainAllImageCache, SharedString,
ParentElement, PathPromptOptions, Render, RetainAllImageCache, SharedString, SharedUri,
StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, Window,
};
use gpui_tokio::Tokio;
@@ -729,7 +729,7 @@ impl Chat {
.flex()
.gap_3()
.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(
v_flex()
@@ -748,7 +748,7 @@ impl Chat {
.text_color(cx.theme().text)
.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| {
this.when(status, |this| {
this.child(self.render_message_sent(&id, cx))
@@ -784,14 +784,12 @@ impl Chat {
.child(self.render_actions(&id, cx))
.on_mouse_down(
MouseButton::Middle,
cx.listener(move |this, _event, _window, cx| {
cx.listener(move |this, _, _window, cx| {
this.copy_message(&id, cx);
}),
)
.on_double_click(cx.listener({
move |this, _event, _window, cx| {
this.reply_to(&id, cx);
}
.on_double_click(cx.listener(move |this, _, _window, cx| {
this.reply_to(&id, cx);
}))
.hover(|this| this.bg(cx.theme().surface_background))
.into_any_element()
@@ -828,7 +826,7 @@ impl Chat {
.w_full()
.text_ellipsis()
.line_clamp(1)
.child(message.content.clone()),
.child(SharedString::from(&message.content)),
)
.hover(|this| this.bg(cx.theme().elevated_surface_background))
.on_click({
@@ -902,7 +900,7 @@ impl Chat {
let registry = Registry::read_global(cx);
let profile = registry.get_person(&report.receiver, cx);
let name = profile.display_name();
let avatar = profile.avatar_url(true);
let avatar = profile.avatar(true);
v_flex()
.gap_2()
@@ -1094,15 +1092,12 @@ impl Chat {
}
fn render_attachment(&self, url: &Url, cx: &Context<Self>) -> impl IntoElement {
let url = url.clone();
let path: SharedString = url.to_string().into();
div()
.id(SharedString::from(url.to_string()))
.relative()
.w_16()
.child(
img(path.clone())
img(SharedUri::from(url.to_string()))
.size_16()
.shadow_lg()
.rounded(cx.theme().radius)
@@ -1121,9 +1116,12 @@ impl Chat {
.bg(red())
.child(Icon::new(IconName::Close).size_2().text_color(white())),
)
.on_click(cx.listener(move |this, _, window, cx| {
this.remove_attachment(&url, window, cx);
}))
.on_click({
let url = url.clone();
cx.listener(move |this, _, window, cx| {
this.remove_attachment(&url, window, cx);
})
})
}
fn render_attachment_list(
@@ -1188,7 +1186,7 @@ impl Chat {
.text_sm()
.text_ellipsis()
.line_clamp(1)
.child(text.content.clone()),
.child(SharedString::from(&text.content)),
)
} else {
div()
@@ -1405,9 +1403,7 @@ impl Render for Chat {
.px_3()
.py_2()
.child(
div()
.flex()
.flex_col()
v_flex()
.gap_1p5()
.children(self.render_attachment_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,
Styled, Window,
};
use i18n::t;
use i18n::{shared_t, t};
use theme::ActiveTheme;
use ui::input::{InputState, TextInput};
use ui::{v_flex, Sizable};
@@ -41,7 +41,7 @@ impl Render for Subject {
div()
.text_sm()
.text_color(cx.theme().text_muted)
.child(SharedString::new(t!("subject.title"))),
.child(shared_t!("subject.title")),
)
.child(TextInput::new(&self.input).small())
.child(
@@ -49,7 +49,7 @@ impl Render for Subject {
.text_xs()
.italic()
.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 anyhow::{anyhow, Error};
use common::display::{ReadableProfile, TextUtils};
use common::display::{RenderedProfile, TextUtils};
use common::nip05::nip05_profile;
use global::constants::BOOTSTRAP_RELAYS;
use global::{app_state, nostr_client};
use gpui::prelude::FluentBuilder;
use gpui::{
div, px, relative, rems, uniform_list, App, AppContext, Context, Entity, InteractiveElement,
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
Subscription, Task, Window,
IntoElement, ParentElement, Render, RetainAllImageCache, SharedString,
StatefulInteractiveElement, Styled, Subscription, Task, Window,
};
use gpui_tokio::Tokio;
use i18n::{shared_t, t};
@@ -110,7 +110,8 @@ pub struct Compose {
/// Error message
error_message: Entity<Option<SharedString>>,
_subscriptions: SmallVec<[Subscription; 1]>,
image_cache: Entity<RetainAllImageCache>,
_subscriptions: SmallVec<[Subscription; 2]>,
_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(
// Handle Enter event for user input
cx.subscribe_in(
@@ -179,6 +189,7 @@ impl Compose {
user_input,
error_message,
contacts,
image_cache: RetainAllImageCache::new(cx),
_subscriptions: subscriptions,
_tasks: tasks,
}
@@ -389,7 +400,7 @@ impl Compose {
h_flex()
.gap_1p5()
.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()),
)
.when(contact.selected, |this| {
@@ -417,6 +428,7 @@ impl Render for Compose {
let contacts = self.contacts.read(cx);
v_flex()
.image_cache(self.image_cache.clone())
.gap_2()
.child(
div()

View File

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

View File

@@ -1,7 +1,6 @@
use std::time::Duration;
use client_keys::ClientKeys;
use common::handle_auth::CoopAuthUrlHandler;
use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT};
use global::nostr_client;
use gpui::prelude::FluentBuilder;
@@ -19,6 +18,8 @@ use ui::input::{InputEvent, InputState, TextInput};
use ui::popup_menu::PopupMenu;
use ui::{v_flex, ContextModal, Disableable, Sizable, StyledExt};
use crate::actions::CoopAuthUrlHandler;
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Login> {
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::{
div, px, relative, rems, App, AppContext, Context, Entity, InteractiveElement, IntoElement,
@@ -141,7 +141,7 @@ impl Render for Preferences {
h_flex()
.id("user")
.gap_2()
.child(Avatar::new(profile.avatar_url(proxy)).size(rems(2.4)))
.child(Avatar::new(profile.avatar(proxy)).size(rems(2.4)))
.child(
div()
.flex_1()

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
use std::time::Duration;
use common::display::ReadableProfile;
use common::display::RenderedProfile;
use common::nip05::nip05_verify;
use global::nostr_client;
use gpui::prelude::FluentBuilder;
@@ -128,9 +128,8 @@ impl UserProfile {
impl Render for UserProfile {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let Ok(bech32) = self.profile.public_key().to_bech32();
let shared_bech32 = SharedString::new(bech32);
let bech32 = self.profile.public_key().to_bech32().unwrap();
let shared_bech32 = SharedString::from(bech32);
v_flex()
.gap_4()
@@ -140,7 +139,7 @@ impl Render for UserProfile {
.items_center()
.justify_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(
v_flex()
.child(
@@ -194,7 +193,7 @@ impl Render for UserProfile {
div()
.block()
.text_color(cx.theme().text_muted)
.child("Public Key:"),
.child(SharedString::from("Public Key:")),
)
.child(
h_flex()
@@ -245,7 +244,8 @@ impl Render for UserProfile {
self.profile
.metadata()
.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()
.font_semibold()
.text_color(cx.theme().text_muted)
.child("coop on nostr"),
.child(SharedString::from("coop on nostr")),
)
.child(
div()

View File

@@ -29,10 +29,10 @@ macro_rules! init {
#[macro_export]
macro_rules! shared_t {
($key:expr) => {
SharedString::new(t!($key))
SharedString::from(t!($key))
};
($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 anyhow::Error;
use common::display::ReadableProfile;
use common::display::RenderedProfile;
use common::event::EventUtils;
use global::constants::SEND_RETRY;
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 nostr_sdk::prelude::*;
@@ -267,22 +267,22 @@ impl 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() {
subject
SharedString::from(subject)
} else {
self.merge_name(cx)
self.merged_name(cx)
}
}
/// 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() {
picture.clone()
SharedUri::from(picture)
} else if !self.is_group() {
self.first_member(cx).avatar_url(proxy)
self.first_member(cx).avatar(proxy)
} 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.
pub(crate) fn merge_name(&self, cx: &App) -> String {
pub(crate) fn merged_name(&self, cx: &App) -> SharedString {
let registry = Registry::read_global(cx);
if self.is_group() {
@@ -308,7 +308,7 @@ impl Room {
let mut name = profiles
.iter()
.take(2)
.map(|p| p.display_name())
.map(|p| p.name())
.collect::<Vec<_>>()
.join(", ");
@@ -316,7 +316,7 @@ impl Room {
name = format!("{}, +{}", name, profiles.len() - 2);
}
name
SharedString::from(name)
} else {
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(
window: &mut Window,
cx: &mut App,

View File

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