Skip to main content

cosmic_applet_avestruz/views/
login.rs

1// SPDX-License-Identifier: MIT
2
3//! Sign-in view for Avestruz.
4//!
5//! One field takes an `nsec`, an `npub` or a `bunker://` address. It hides
6//! what is typed, because what is typed may be a secret.
7
8use cosmic::Element;
9use cosmic::iced::widget::text::Wrapping;
10use cosmic::iced::{Alignment, Length};
11use cosmic::widget::text_input::secure_input;
12use cosmic::widget::{self, button, container, text};
13
14use crate::fl;
15use crate::messages::{Message, SecretInput};
16use crate::nostr::account::CredentialError;
17use crate::state::{AppModel, BunkerRefusal, LoginProgress};
18use crate::views::components::app_icon;
19
20/// Why a sign-in attempt was refused, in the user's language.
21fn credential_error_text(error: CredentialError) -> String {
22    match error {
23        CredentialError::Empty => fl!("sign-in-error-empty"),
24        CredentialError::NotAKey => fl!("sign-in-error-not-a-key"),
25        CredentialError::InvalidSecret => fl!("sign-in-error-invalid-secret"),
26        CredentialError::InvalidPublic => fl!("sign-in-error-invalid-public"),
27        CredentialError::InvalidBunker => fl!("sign-in-error-invalid-bunker"),
28    }
29}
30
31/// What is said when a remote signer turned the sign-in down: why, the key
32/// to authorize there, with a button that copies it, and, of an address
33/// with a secret, that the secret may have been used up.
34fn refusal_notice(refusal: &BunkerRefusal) -> Element<'_, Message> {
35    let small = |message: String| text(message).size(11).wrapping(Wrapping::WordOrGlyph);
36    let mut notice = widget::Column::new()
37        .push(
38            text(fl!("sign-in-refused", error = refusal.reason.as_str()))
39                .size(12)
40                .class(cosmic::theme::Text::Accent)
41                .wrapping(Wrapping::WordOrGlyph),
42        )
43        .spacing(6)
44        .width(Length::Fill);
45    if let Some(client) = refusal.client {
46        let copy = if refusal.copied { fl!("sign-in-client-key-copied") } else { fl!("sign-in-copy-client-key") };
47        notice = notice
48            .push(small(fl!("sign-in-client-key-help")))
49            // In full: it is what goes into the signer's configuration.
50            .push(text(client.to_hex()).size(11).font(cosmic::font::mono()).wrapping(Wrapping::Glyph))
51            .push(button::standard(copy).on_press(Message::CopyClientKey));
52    }
53    if refusal.with_secret {
54        notice = notice.push(small(fl!("sign-in-secret-help")));
55    }
56    notice.into()
57}
58
59impl AppModel {
60    /// Render the sign-in view: a field for a key or a bunker address.
61    pub fn view_login(&self) -> Element<'_, Message> {
62        let input = secure_input(
63            fl!("sign-in-placeholder"),
64            &self.login_input,
65            Some(Message::ToggleLoginInputVisibility),
66            self.login_input_hidden,
67        )
68        .on_input(|value| Message::LoginInputChanged(SecretInput(value)))
69        .on_submit(|_| Message::SubmitLogin)
70        .width(Length::Fill);
71
72        let sign_in_label = match self.login_progress {
73            LoginProgress::Idle => fl!("sign-in"),
74            LoginProgress::Storing => fl!("signing-in"),
75            LoginProgress::Connecting => fl!("sign-in-connecting"),
76        };
77        let sign_in = button::suggested(sign_in_label)
78            .on_press_maybe((self.login_progress == LoginProgress::Idle).then_some(Message::SubmitLogin))
79            .width(Length::Fill);
80
81        let mut content = widget::Column::new()
82            .push(widget::icon::icon(app_icon()).size(48))
83            .push(text(fl!("app-name")).size(24))
84            .push(text(fl!("sign-in-prompt")).size(14))
85            .push(widget::space::vertical().height(8))
86            .push(input);
87
88        if let Some(error) = self.login_error {
89            content = content.push(text(credential_error_text(error)).size(12).class(cosmic::theme::Text::Accent));
90        }
91
92        if let Some(refusal) = &self.login_refusal {
93            content = content.push(refusal_notice(refusal));
94        }
95
96        let mut content = content.push(text(fl!("sign-in-help")).size(11).wrapping(Wrapping::WordOrGlyph)).push(sign_in);
97        // A remote signer may wait on a person: say so, and offer a way out.
98        if self.login_progress == LoginProgress::Connecting {
99            content = content
100                .push(text(fl!("sign-in-waiting")).size(11).wrapping(Wrapping::WordOrGlyph))
101                .push(button::text(fl!("sign-in-cancel")).on_press(Message::CancelLogin));
102        }
103        let content = content.spacing(12).align_x(Alignment::Center).padding(20).width(Length::Fill);
104
105        container(content).width(Length::Fill).align_x(Alignment::Center).align_y(Alignment::Center).into()
106    }
107}