feat: simple login process
This commit is contained in:
@@ -112,6 +112,12 @@ impl Account {
|
||||
self._tasks.push(task);
|
||||
}
|
||||
|
||||
/// Set the public key of the account
|
||||
pub fn set_public_key(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
|
||||
self.public_key = Some(public_key);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Check if the account entity has a public key
|
||||
pub fn has_account(&self) -> bool {
|
||||
self.public_key.is_some()
|
||||
|
||||
@@ -16,5 +16,8 @@ pub const BOOTSTRAP_RELAYS: [&str; 5] = [
|
||||
/// Default relay for Nostr Connect
|
||||
pub const NOSTR_CONNECT_RELAY: &str = "wss://relay.nsec.app";
|
||||
|
||||
/// Default timeout for Nostr Connect (seconds)
|
||||
pub const NOSTR_CONNECT_TIMEOUT: u64 = 30;
|
||||
|
||||
/// Default width of the sidebar.
|
||||
pub const DEFAULT_SIDEBAR_WIDTH: f32 = 240.;
|
||||
|
||||
@@ -34,3 +34,5 @@ futures.workspace = true
|
||||
flume.workspace = true
|
||||
|
||||
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
||||
qrcode = "0.14.1"
|
||||
webbrowser = "1.0.6"
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
use gpui::{actions, App};
|
||||
use nostr_connect::prelude::*;
|
||||
|
||||
actions!(lume, [Quit, About, Open]);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LumeAuthUrlHandler;
|
||||
|
||||
impl AuthUrlHandler for LumeAuthUrlHandler {
|
||||
#[allow(mismatched_lifetime_syntaxes)]
|
||||
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();
|
||||
|
||||
384
crates/lume/src/panels/login.rs
Normal file
384
crates/lume/src/panels/login.rs
Normal file
@@ -0,0 +1,384 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use account::Account;
|
||||
use anyhow::{anyhow, Error};
|
||||
use common::NOSTR_CONNECT_TIMEOUT;
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
div, relative, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
||||
IntoElement, ParentElement, Render, SharedString, Styled, Subscription, Window,
|
||||
};
|
||||
use gpui_component::button::{Button, ButtonVariants};
|
||||
use gpui_component::dock::{Panel, PanelEvent};
|
||||
use gpui_component::input::{Input, InputEvent, InputState};
|
||||
use gpui_component::notification::Notification;
|
||||
use gpui_component::{v_flex, ActiveTheme, Disableable, StyledExt, WindowExt};
|
||||
use nostr_connect::prelude::*;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use state::client;
|
||||
|
||||
use crate::actions::LumeAuthUrlHandler;
|
||||
|
||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Login> {
|
||||
cx.new(|cx| Login::new(window, cx))
|
||||
}
|
||||
|
||||
pub struct Login {
|
||||
focus_handle: FocusHandle,
|
||||
|
||||
/// Input for nsec for bunker uri
|
||||
key_input: Entity<InputState>,
|
||||
|
||||
/// Input for decryption password when available
|
||||
pass_input: Entity<InputState>,
|
||||
|
||||
/// Error message
|
||||
error: Entity<Option<SharedString>>,
|
||||
|
||||
/// Timeout countdown
|
||||
countdown: Entity<Option<u64>>,
|
||||
|
||||
/// Whether password is required
|
||||
require_password: bool,
|
||||
|
||||
/// Whether logging in is in progress
|
||||
logging_in: bool,
|
||||
|
||||
/// Event subscriptions
|
||||
_subscriptions: SmallVec<[Subscription; 1]>,
|
||||
}
|
||||
|
||||
impl Login {
|
||||
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let key_input = cx.new(|cx| InputState::new(window, cx));
|
||||
let pass_input = cx.new(|cx| InputState::new(window, cx).masked(true));
|
||||
|
||||
let error = cx.new(|_| None);
|
||||
let countdown = cx.new(|_| None);
|
||||
|
||||
let mut subscriptions = smallvec![];
|
||||
|
||||
subscriptions.push(
|
||||
// Subscribe to key input events and process login when the user presses enter
|
||||
cx.subscribe_in(&key_input, window, |this, input, event, window, cx| {
|
||||
match event {
|
||||
InputEvent::PressEnter { .. } => {
|
||||
this.login(window, cx);
|
||||
}
|
||||
InputEvent::Change => {
|
||||
if input.read(cx).value().starts_with("ncryptsec1") {
|
||||
this.require_password = true;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
Self {
|
||||
focus_handle: cx.focus_handle(),
|
||||
key_input,
|
||||
pass_input,
|
||||
error,
|
||||
countdown,
|
||||
logging_in: false,
|
||||
require_password: false,
|
||||
_subscriptions: subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
fn login(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if self.logging_in {
|
||||
return;
|
||||
};
|
||||
|
||||
// Prevent duplicate login requests
|
||||
self.set_logging_in(true, cx);
|
||||
|
||||
let value = self.key_input.read(cx).value();
|
||||
let password = self.pass_input.read(cx).value();
|
||||
|
||||
if value.starts_with("bunker://") {
|
||||
self.login_with_bunker(&value, window, cx);
|
||||
} else if value.starts_with("ncryptsec1") {
|
||||
self.login_with_password(&value, &password, cx);
|
||||
} else if value.starts_with("nsec1") {
|
||||
if let Ok(secret) = SecretKey::parse(&value) {
|
||||
let keys = Keys::new(secret);
|
||||
self.login_with_keys(keys, cx);
|
||||
} else {
|
||||
self.set_error("Invalid", cx);
|
||||
}
|
||||
} else {
|
||||
self.set_error("Invalid", cx);
|
||||
}
|
||||
}
|
||||
|
||||
fn login_with_bunker(&mut self, content: &str, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let Ok(uri) = NostrConnectUri::parse(content) else {
|
||||
self.set_error("Bunker URI is not valid", cx);
|
||||
return;
|
||||
};
|
||||
|
||||
let app_keys = Keys::generate();
|
||||
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
|
||||
let mut signer = NostrConnect::new(uri, app_keys.clone(), timeout, None).unwrap();
|
||||
|
||||
// Handle auth url with the default browser
|
||||
signer.auth_url_handler(LumeAuthUrlHandler);
|
||||
|
||||
// Start countdown
|
||||
cx.spawn_in(window, async move |this, cx| {
|
||||
for i in (0..=NOSTR_CONNECT_TIMEOUT).rev() {
|
||||
if i == 0 {
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_countdown(None, cx);
|
||||
})
|
||||
.ok();
|
||||
} else {
|
||||
this.update(cx, |this, cx| {
|
||||
this.set_countdown(Some(i), cx);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
cx.background_executor().timer(Duration::from_secs(1)).await;
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
// Handle connection
|
||||
cx.spawn_in(window, async move |this, cx| {
|
||||
let result: Result<PublicKey, Error> = cx
|
||||
.background_executor()
|
||||
.await_on_background(async move {
|
||||
let client = client();
|
||||
let public_key = signer.get_public_key().await?;
|
||||
|
||||
// Update the signer
|
||||
client.set_signer(signer).await;
|
||||
|
||||
// Return the URI for storing the connection
|
||||
Ok(public_key)
|
||||
})
|
||||
.await;
|
||||
|
||||
this.update_in(cx, |_this, window, cx| {
|
||||
match result {
|
||||
Ok(public_key) => {
|
||||
let account = Account::global(cx);
|
||||
account.update(cx, |this, cx| {
|
||||
this.set_public_key(public_key, cx);
|
||||
})
|
||||
}
|
||||
Err(e) => {
|
||||
window.push_notification(Notification::error(e.to_string()), cx);
|
||||
}
|
||||
};
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn login_with_password(&mut self, content: &str, pwd: &str, cx: &mut Context<Self>) {
|
||||
if pwd.is_empty() {
|
||||
self.set_error("Password is required", cx);
|
||||
return;
|
||||
}
|
||||
|
||||
let Ok(enc) = EncryptedSecretKey::from_bech32(content) else {
|
||||
self.set_error("Secret Key is invalid", cx);
|
||||
return;
|
||||
};
|
||||
|
||||
let password = pwd.to_owned();
|
||||
|
||||
// Decrypt in the background to ensure it doesn't block the UI
|
||||
let task = cx.background_spawn(async move {
|
||||
if let Ok(content) = enc.decrypt(&password) {
|
||||
Ok(Keys::new(content))
|
||||
} else {
|
||||
Err(anyhow!("Invalid password"))
|
||||
}
|
||||
});
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
let result = task.await;
|
||||
|
||||
this.update(cx, |this, cx| {
|
||||
match result {
|
||||
Ok(keys) => {
|
||||
this.login_with_keys(keys, cx);
|
||||
}
|
||||
Err(e) => {
|
||||
this.set_error(e.to_string(), cx);
|
||||
}
|
||||
};
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn login_with_keys(&mut self, keys: Keys, cx: &mut Context<Self>) {
|
||||
let public_key = keys.public_key();
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
cx.background_executor()
|
||||
.await_on_background(async move {
|
||||
let client = client();
|
||||
client.set_signer(keys).await;
|
||||
})
|
||||
.await;
|
||||
|
||||
this.update(cx, |_this, cx| {
|
||||
let account = Account::global(cx);
|
||||
account.update(cx, |this, cx| {
|
||||
this.set_public_key(public_key, cx);
|
||||
});
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn set_error<S>(&mut self, message: S, cx: &mut Context<Self>)
|
||||
where
|
||||
S: Into<SharedString>,
|
||||
{
|
||||
// Reset the log in state
|
||||
self.set_logging_in(false, cx);
|
||||
|
||||
// Reset the countdown
|
||||
self.set_countdown(None, cx);
|
||||
|
||||
// Update error message
|
||||
self.error.update(cx, |this, cx| {
|
||||
*this = Some(message.into());
|
||||
cx.notify();
|
||||
});
|
||||
|
||||
// Clear the error message after 3 secs
|
||||
cx.spawn(async move |this, cx| {
|
||||
cx.background_executor().timer(Duration::from_secs(3)).await;
|
||||
|
||||
this.update(cx, |this, cx| {
|
||||
this.error.update(cx, |this, cx| {
|
||||
*this = None;
|
||||
cx.notify();
|
||||
});
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn set_logging_in(&mut self, status: bool, cx: &mut Context<Self>) {
|
||||
self.logging_in = status;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn set_countdown(&mut self, i: Option<u64>, cx: &mut Context<Self>) {
|
||||
self.countdown.update(cx, |this, cx| {
|
||||
*this = i;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for Login {
|
||||
fn panel_name(&self) -> &'static str {
|
||||
"Login"
|
||||
}
|
||||
|
||||
fn title(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
SharedString::from("Login")
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for Login {}
|
||||
|
||||
impl Focusable for Login {
|
||||
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Login {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.relative()
|
||||
.size_full()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(
|
||||
v_flex()
|
||||
.w_96()
|
||||
.gap_10()
|
||||
.child(
|
||||
div()
|
||||
.text_center()
|
||||
.text_lg()
|
||||
.font_semibold()
|
||||
.line_height(relative(1.3))
|
||||
.child(SharedString::from("Continue with Private Key or Bunker")),
|
||||
)
|
||||
.child(
|
||||
v_flex()
|
||||
.gap_3()
|
||||
.text_sm()
|
||||
.child(
|
||||
v_flex()
|
||||
.gap_1()
|
||||
.text_sm()
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.child("nsec or bunker://")
|
||||
.child(Input::new(&self.key_input)),
|
||||
)
|
||||
.when(self.require_password, |this| {
|
||||
this.child(
|
||||
v_flex()
|
||||
.gap_1()
|
||||
.text_sm()
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.child("Password:")
|
||||
.child(Input::new(&self.pass_input)),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
Button::new("login")
|
||||
.label("Continue")
|
||||
.primary()
|
||||
.loading(self.logging_in)
|
||||
.disabled(self.logging_in)
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.login(window, cx);
|
||||
})),
|
||||
)
|
||||
.when_some(self.countdown.read(cx).as_ref(), |this, i| {
|
||||
let msg = format!(
|
||||
"Approve connection request from your signer in {} seconds",
|
||||
i
|
||||
);
|
||||
this.child(
|
||||
div()
|
||||
.text_xs()
|
||||
.text_center()
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.child(SharedString::from(msg)),
|
||||
)
|
||||
})
|
||||
.when_some(self.error.read(cx).as_ref(), |this, error| {
|
||||
this.child(
|
||||
div()
|
||||
.text_xs()
|
||||
.text_center()
|
||||
.text_color(cx.theme().danger_foreground)
|
||||
.child(error.clone()),
|
||||
)
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod feed;
|
||||
pub mod login;
|
||||
pub mod new_account;
|
||||
pub mod onboarding;
|
||||
pub mod startup;
|
||||
|
||||
45
crates/lume/src/panels/new_account.rs
Normal file
45
crates/lume/src/panels/new_account.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use gpui::{
|
||||
div, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement,
|
||||
ParentElement, Render, SharedString, Window,
|
||||
};
|
||||
use gpui_component::dock::{Panel, PanelEvent};
|
||||
|
||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<NewAccount> {
|
||||
cx.new(|cx| NewAccount::new(window, cx))
|
||||
}
|
||||
|
||||
pub struct NewAccount {
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
impl NewAccount {
|
||||
fn new(_window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
Self {
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for NewAccount {
|
||||
fn panel_name(&self) -> &'static str {
|
||||
"New Account"
|
||||
}
|
||||
|
||||
fn title(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
SharedString::from("New Account")
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for NewAccount {}
|
||||
|
||||
impl Focusable for NewAccount {
|
||||
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for NewAccount {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div().child("New Account")
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,180 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use account::Account;
|
||||
use anyhow::Error;
|
||||
use common::{CLIENT_NAME, NOSTR_CONNECT_RELAY, NOSTR_CONNECT_TIMEOUT};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
div, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement,
|
||||
ParentElement, Render, SharedString, Window,
|
||||
div, img, px, relative, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
||||
Image, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
|
||||
StatefulInteractiveElement, Styled, Task, Window,
|
||||
};
|
||||
use gpui_component::button::{Button, ButtonVariants};
|
||||
use gpui_component::divider::Divider;
|
||||
use gpui_component::dock::{Panel, PanelEvent};
|
||||
use gpui_component::notification::Notification;
|
||||
use gpui_component::{h_flex, v_flex, ActiveTheme, Icon, IconName, Sizable, StyledExt, WindowExt};
|
||||
use nostr_connect::prelude::*;
|
||||
use qrcode::render::svg;
|
||||
use qrcode::QrCode;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use state::client;
|
||||
|
||||
use crate::workspace::OpenPanel;
|
||||
|
||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Onboarding> {
|
||||
cx.new(|cx| Onboarding::new(window, cx))
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NostrConnectApp {
|
||||
Nsec(String),
|
||||
Amber(String),
|
||||
Aegis(String),
|
||||
}
|
||||
|
||||
impl NostrConnectApp {
|
||||
pub fn all() -> Vec<Self> {
|
||||
vec![
|
||||
NostrConnectApp::Nsec("https://nsec.app".to_string()),
|
||||
NostrConnectApp::Amber("https://github.com/greenart7c3/Amber".to_string()),
|
||||
NostrConnectApp::Aegis("https://github.com/ZharlieW/Aegis".to_string()),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn url(&self) -> &str {
|
||||
match self {
|
||||
Self::Nsec(url) | Self::Amber(url) | Self::Aegis(url) => url,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> String {
|
||||
match self {
|
||||
NostrConnectApp::Nsec(_) => "nsec.app (Desktop)".into(),
|
||||
NostrConnectApp::Amber(_) => "Amber (Android)".into(),
|
||||
NostrConnectApp::Aegis(_) => "Aegis (iOS)".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Onboarding
|
||||
pub struct Onboarding {
|
||||
focus_handle: FocusHandle,
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// App keys for communicating with the remote signer
|
||||
app_keys: Keys,
|
||||
|
||||
/// QR code for logging in with Nostr Connect
|
||||
qr_code: Option<Arc<Image>>,
|
||||
|
||||
/// Background tasks
|
||||
_tasks: SmallVec<[Task<()>; 1]>,
|
||||
}
|
||||
|
||||
impl Onboarding {
|
||||
fn new(_window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let app_keys = Keys::generate();
|
||||
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
|
||||
let relay = RelayUrl::parse(NOSTR_CONNECT_RELAY).unwrap();
|
||||
let uri = NostrConnectUri::client(app_keys.public_key(), vec![relay], CLIENT_NAME);
|
||||
let qr_code = Self::generate_qr(uri.to_string().as_ref());
|
||||
|
||||
// NIP46: https://github.com/nostr-protocol/nips/blob/master/46.md
|
||||
//
|
||||
// Direct connection initiated by the client
|
||||
let signer = NostrConnect::new(uri, app_keys.clone(), timeout, None).unwrap();
|
||||
|
||||
let mut tasks = smallvec![];
|
||||
|
||||
tasks.push(
|
||||
// Wait for nostr connect
|
||||
cx.spawn_in(window, async move |this, cx| {
|
||||
let result: Result<PublicKey, Error> = cx
|
||||
.background_executor()
|
||||
.await_on_background(async move {
|
||||
let client = client();
|
||||
let public_key = signer.get_public_key().await?;
|
||||
|
||||
// Update the signer
|
||||
client.set_signer(signer).await;
|
||||
|
||||
// Return the URI for storing the connection
|
||||
Ok(public_key)
|
||||
})
|
||||
.await;
|
||||
|
||||
this.update_in(cx, |_this, window, cx| {
|
||||
match result {
|
||||
Ok(public_key) => {
|
||||
let account = Account::global(cx);
|
||||
account.update(cx, |this, cx| {
|
||||
this.set_public_key(public_key, cx);
|
||||
})
|
||||
}
|
||||
Err(e) => {
|
||||
window.push_notification(Notification::error(e.to_string()), cx);
|
||||
}
|
||||
};
|
||||
})
|
||||
.ok();
|
||||
}),
|
||||
);
|
||||
|
||||
Self {
|
||||
focus_handle: cx.focus_handle(),
|
||||
qr_code,
|
||||
app_keys,
|
||||
_tasks: tasks,
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_qr(value: &str) -> Option<Arc<Image>> {
|
||||
let code = QrCode::new(value).unwrap();
|
||||
let svg = code
|
||||
.render()
|
||||
.min_dimensions(256, 256)
|
||||
.dark_color(svg::Color("#000000"))
|
||||
.light_color(svg::Color("#FFFFFF"))
|
||||
.build();
|
||||
|
||||
Some(Arc::new(Image::from_bytes(
|
||||
gpui::ImageFormat::Svg,
|
||||
svg.into_bytes(),
|
||||
)))
|
||||
}
|
||||
|
||||
fn render_app<T>(&self, ix: usize, label: T, url: &str, cx: &Context<Self>) -> impl IntoElement
|
||||
where
|
||||
T: Into<SharedString>,
|
||||
{
|
||||
div()
|
||||
.id(ix)
|
||||
.flex_1()
|
||||
.rounded_md()
|
||||
.py_0p5()
|
||||
.px_2()
|
||||
.bg(cx.theme().list)
|
||||
.child(label.into())
|
||||
.on_click({
|
||||
let url = url.to_owned();
|
||||
move |_e, _window, cx| {
|
||||
cx.open_url(&url);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn render_apps(&self, cx: &Context<Self>) -> impl IntoIterator<Item = impl IntoElement> {
|
||||
let all_apps = NostrConnectApp::all();
|
||||
let mut items = Vec::with_capacity(all_apps.len());
|
||||
|
||||
for (ix, item) in all_apps.into_iter().enumerate() {
|
||||
items.push(self.render_app(ix, item.as_str(), item.url(), cx));
|
||||
}
|
||||
|
||||
items
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for Onboarding {
|
||||
@@ -32,14 +189,118 @@ impl Panel for Onboarding {
|
||||
|
||||
impl EventEmitter<PanelEvent> for Onboarding {}
|
||||
|
||||
impl EventEmitter<OpenPanel> for Onboarding {}
|
||||
|
||||
impl Focusable for Onboarding {
|
||||
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
||||
fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Onboarding {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div().child("Onboarding")
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
h_flex()
|
||||
.size_full()
|
||||
.child(
|
||||
v_flex()
|
||||
.flex_1()
|
||||
.h_full()
|
||||
.gap_10()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(
|
||||
v_flex()
|
||||
.items_center()
|
||||
.text_center()
|
||||
.child(
|
||||
div()
|
||||
.text_xl()
|
||||
.font_semibold()
|
||||
.line_height(relative(1.3))
|
||||
.child(SharedString::from("Welcome to Lume")),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.child(SharedString::from("An unambitious Nostr client")),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
v_flex()
|
||||
.w_80()
|
||||
.gap_3()
|
||||
.child(
|
||||
Button::new("continue")
|
||||
.icon(Icon::new(IconName::ArrowRight))
|
||||
.label("Start Browsing")
|
||||
.primary()
|
||||
.on_click(cx.listener(move |_this, _ev, _window, cx| {
|
||||
cx.emit(OpenPanel::Signup);
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
Divider::horizontal()
|
||||
.label("Already have an account? Continue with")
|
||||
.text_xs(),
|
||||
)
|
||||
.child(
|
||||
Button::new("key")
|
||||
.label("Secret Key or Bunker")
|
||||
.large()
|
||||
.link()
|
||||
.small()
|
||||
.on_click(cx.listener(move |_this, _ev, _window, cx| {
|
||||
cx.emit(OpenPanel::Login);
|
||||
})),
|
||||
),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
v_flex()
|
||||
.flex_1()
|
||||
.size_full()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.gap_5()
|
||||
.bg(cx.theme().muted)
|
||||
.when_some(self.qr_code.as_ref(), |this, qr| {
|
||||
this.child(
|
||||
img(qr.clone())
|
||||
.size(px(256.))
|
||||
.rounded_xl()
|
||||
.shadow_lg()
|
||||
.border_1()
|
||||
.border_color(cx.theme().primary_active),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
v_flex()
|
||||
.justify_center()
|
||||
.items_center()
|
||||
.text_center()
|
||||
.child(
|
||||
div()
|
||||
.font_semibold()
|
||||
.line_height(relative(1.3))
|
||||
.child(SharedString::from("Continue with Nostr Connect")),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_sm()
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.child(SharedString::from(
|
||||
"Use Nostr Connect apps to scan the code",
|
||||
)),
|
||||
)
|
||||
.child(
|
||||
h_flex()
|
||||
.mt_2()
|
||||
.gap_1()
|
||||
.text_xs()
|
||||
.justify_center()
|
||||
.children(self.render_apps(cx)),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use gpui_component::{h_flex, v_flex, ActiveTheme, StyledExt};
|
||||
use nostr_sdk::prelude::*;
|
||||
use person::PersonRegistry;
|
||||
|
||||
use crate::workspace::WorkspaceEvent;
|
||||
use crate::workspace::OpenPanel;
|
||||
|
||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Sidebar> {
|
||||
cx.new(|cx| Sidebar::new(window, cx))
|
||||
@@ -35,7 +35,7 @@ impl Panel for Sidebar {
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for Sidebar {}
|
||||
impl EventEmitter<WorkspaceEvent> for Sidebar {}
|
||||
impl EventEmitter<OpenPanel> for Sidebar {}
|
||||
|
||||
impl Focusable for Sidebar {
|
||||
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
||||
@@ -80,7 +80,7 @@ impl Render for Sidebar {
|
||||
.child(div().text_sm().child(SharedString::from(relay)))
|
||||
.on_click(cx.listener(move |_this, _ev, _window, cx| {
|
||||
if let Ok(url) = RelayUrl::parse(relay) {
|
||||
cx.emit(WorkspaceEvent::OpenRelay(url));
|
||||
cx.emit(OpenPanel::Relay(url));
|
||||
}
|
||||
})),
|
||||
)
|
||||
@@ -117,9 +117,7 @@ impl Render for Sidebar {
|
||||
.hover(|this| this.bg(cx.theme().list_hover))
|
||||
.child(div().text_sm().child(name.clone()))
|
||||
.on_click(cx.listener(move |_this, _ev, _window, cx| {
|
||||
cx.emit(WorkspaceEvent::OpenPublicKey(
|
||||
profile.public_key(),
|
||||
));
|
||||
cx.emit(OpenPanel::PublicKey(profile.public_key()));
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -12,14 +12,16 @@ use nostr_sdk::prelude::*;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
||||
use crate::panels::feed::Feed;
|
||||
use crate::panels::{onboarding, startup};
|
||||
use crate::panels::{login, new_account, onboarding, startup};
|
||||
use crate::sidebar;
|
||||
use crate::title_bar::AppTitleBar;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum WorkspaceEvent {
|
||||
OpenPublicKey(PublicKey),
|
||||
OpenRelay(RelayUrl),
|
||||
pub enum OpenPanel {
|
||||
PublicKey(PublicKey),
|
||||
Relay(RelayUrl),
|
||||
Signup,
|
||||
Login,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -36,18 +38,19 @@ pub struct Workspace {
|
||||
|
||||
impl Workspace {
|
||||
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let account = Account::global(cx);
|
||||
let onboarding = Arc::new(onboarding::init(window, cx));
|
||||
|
||||
// App's title bar
|
||||
let title_bar = cx.new(|cx| AppTitleBar::new(CLIENT_NAME, window, cx));
|
||||
|
||||
// Dock area for the workspace.
|
||||
let dock = cx.new(|cx| {
|
||||
let startup = Arc::new(onboarding::init(window, cx));
|
||||
let mut this = DockArea::new("dock", None, window, cx).panel_style(PanelStyle::TabBar);
|
||||
this.set_center(DockItem::panel(startup), window, cx);
|
||||
this.set_center(DockItem::panel(onboarding.clone()), window, cx);
|
||||
this
|
||||
});
|
||||
|
||||
let account = Account::global(cx);
|
||||
let mut subscriptions = smallvec![];
|
||||
|
||||
// Observe account entity changes
|
||||
@@ -59,6 +62,31 @@ impl Workspace {
|
||||
}),
|
||||
);
|
||||
|
||||
// Observe onboarding panel events
|
||||
subscriptions.push(cx.subscribe_in(
|
||||
&onboarding,
|
||||
window,
|
||||
|this, _sidebar, event: &OpenPanel, window, cx| {
|
||||
match event {
|
||||
OpenPanel::Login => {
|
||||
let view = login::init(window, cx);
|
||||
|
||||
this.dock.update(cx, |this, cx| {
|
||||
this.set_center(DockItem::panel(Arc::new(view)), window, cx);
|
||||
});
|
||||
}
|
||||
OpenPanel::Signup => {
|
||||
let view = new_account::init(window, cx);
|
||||
|
||||
this.dock.update(cx, |this, cx| {
|
||||
this.set_center(DockItem::panel(Arc::new(view)), window, cx);
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
},
|
||||
));
|
||||
|
||||
// Automatically sync theme with system appearance
|
||||
subscriptions.push(window.observe_window_appearance(|window, cx| {
|
||||
Theme::sync_system_appearance(Some(window), cx);
|
||||
@@ -80,20 +108,23 @@ impl Workspace {
|
||||
self._subscriptions.push(cx.subscribe_in(
|
||||
&sidebar,
|
||||
window,
|
||||
|this, _sidebar, event: &WorkspaceEvent, window, cx| {
|
||||
|this, _sidebar, event: &OpenPanel, window, cx| {
|
||||
match event {
|
||||
WorkspaceEvent::OpenPublicKey(public_key) => {
|
||||
OpenPanel::PublicKey(public_key) => {
|
||||
let view = cx.new(|cx| Feed::new(Some(*public_key), None, window, cx));
|
||||
|
||||
this.dock.update(cx, |this, cx| {
|
||||
this.add_panel(Arc::new(view), DockPlacement::Center, None, window, cx);
|
||||
});
|
||||
}
|
||||
WorkspaceEvent::OpenRelay(relay) => {
|
||||
OpenPanel::Relay(relay) => {
|
||||
let view = cx.new(|cx| Feed::new(None, Some(relay.to_owned()), window, cx));
|
||||
|
||||
this.dock.update(cx, |this, cx| {
|
||||
this.add_panel(Arc::new(view), DockPlacement::Center, None, window, cx);
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
},
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user