Skip to main content

cosmic_applet_avestruz/views/
zap.rs

1// SPDX-License-Identifier: MIT
2
3//! The zap view for Avestruz: how much to zap a note, and then the invoice
4//! to pay, as a QR code for a wallet to scan.
5
6use cosmic::Element;
7use cosmic::iced::widget::image::FilterMethod;
8use cosmic::iced::widget::text::Wrapping;
9use cosmic::iced::{Alignment, Background, Border, Color, Length};
10use cosmic::widget::{self, button, container, text, text_input};
11
12use crate::fl;
13use crate::helpers::compact_count;
14use crate::messages::Message;
15use crate::nostr::models::display_name;
16use crate::nostr::zap::{Invoice, Zapper};
17use crate::state::{AppModel, ZapStage, ZapState};
18use crate::views::components::chip;
19
20/// The amounts offered, in sats: Vega's.
21const ZAP_PRESETS: [u64; 5] = [21, 100, 500, 1_000, 5_000];
22
23/// How wide the QR code is shown, the white around it included.
24const QR_SIZE: f32 = 300.0;
25const QR_RADIUS: f32 = 8.0;
26
27/// A line of small print, wrapped.
28fn small_print<'a>(message: String) -> Element<'a, Message> {
29    text(message).size(12).wrapping(Wrapping::WordOrGlyph).width(Length::Fill).into()
30}
31
32/// The invoice as a QR code, on white whatever the theme, or word that
33/// there is none to show.
34fn invoice_code<'a>(qr: Option<&cosmic::widget::image::Handle>) -> Element<'a, Message> {
35    let Some(qr) = qr else {
36        return small_print(fl!("zap-no-code"));
37    };
38    // Scaled without smoothing: the edges of the modules stay edges.
39    let picture = cosmic::widget::image(qr.clone())
40        .width(Length::Fixed(QR_SIZE))
41        .height(Length::Fixed(QR_SIZE))
42        .filter_method(FilterMethod::Nearest);
43    let code = container(picture).class(cosmic::theme::Container::custom(|_theme| container::Style {
44        background: Some(Background::Color(Color::WHITE)),
45        border: Border { radius: QR_RADIUS.into(), ..Default::default() },
46        ..Default::default()
47    }));
48    container(code).width(Length::Fill).align_x(Alignment::Center).into()
49}
50
51impl AppModel {
52    /// The line above the zap view: the way back, and what this is.
53    fn view_zap_header(&self) -> Element<'_, Message> {
54        let back =
55            button::icon(widget::icon::from_name("go-previous-symbolic")).tooltip(fl!("zap-back")).on_press(Message::CloseZap);
56        widget::Row::new()
57            .push(back)
58            .push(text(fl!("zap-title")).size(16).font(cosmic::font::bold()))
59            .spacing(8)
60            .align_y(Alignment::Center)
61            .padding([4, 12])
62            .into()
63    }
64
65    /// Choosing how much to zap, and what to say with it.
66    fn view_zap_choice<'a>(&'a self, zap: &'a ZapState, name: String) -> Element<'a, Message> {
67        let requesting = matches!(zap.stage, ZapStage::Requesting);
68        let chosen = zap.sats();
69
70        let presets = ZAP_PRESETS.into_iter().fold(widget::Row::new().spacing(6), |row, sats| {
71            row.push(chip(compact_count(sats), chosen == Some(sats), Message::ZapAmountChanged(sats.to_string())))
72        });
73        let amount = widget::Row::new()
74            .push(text_input(fl!("zap-amount-placeholder"), &zap.amount).on_input(Message::ZapAmountChanged).width(Length::Fill))
75            .push(text(fl!("zap-sats")).size(13))
76            .spacing(8)
77            .align_y(Alignment::Center);
78        let comment = text_input(fl!("zap-comment-placeholder"), &zap.comment)
79            .on_input(Message::ZapCommentChanged)
80            .on_submit(|_| Message::RequestZapInvoice)
81            .width(Length::Fill);
82
83        let mut body = widget::Column::new()
84            .push(text(fl!("zap-to", name = name)).size(14).font(cosmic::font::bold()).wrapping(Wrapping::WordOrGlyph))
85            .push(small_print(zap.address.clone()))
86            .push(presets)
87            .push(amount)
88            .push(comment)
89            .spacing(8);
90        if self.zapper() == Zapper::Anonymous {
91            body = body.push(small_print(fl!("zap-anonymous")));
92        }
93        if let Some(error) = &zap.error {
94            body = body.push(text(error.as_str()).size(12).class(cosmic::theme::Text::Accent).wrapping(Wrapping::WordOrGlyph));
95        }
96        let label = if requesting { fl!("zap-requesting") } else { fl!("zap-get-invoice") };
97        let ask = button::suggested(label)
98            .on_press_maybe((!requesting && chosen.is_some()).then_some(Message::RequestZapInvoice))
99            .width(Length::Fill);
100        body.push(ask).into()
101    }
102
103    /// The invoice to pay: its QR code, what paying it does, and copying it.
104    fn view_zap_invoice<'a>(
105        &'a self,
106        invoice: &'a Invoice,
107        qr: Option<&'a cosmic::widget::image::Handle>,
108        copied: bool,
109        waiting: bool,
110        name: String,
111    ) -> Element<'a, Message> {
112        let what = if !invoice.is_zap {
113            fl!("zap-plain-payment", name = name.clone())
114        } else if waiting {
115            fl!("zap-waiting")
116        } else {
117            fl!("zap-not-seen")
118        };
119        let copy = button::standard(if copied { fl!("zap-copied") } else { fl!("zap-copy") }).on_press(Message::CopyZapInvoice);
120        let buttons = widget::Row::new()
121            .push(copy)
122            .push(widget::space::horizontal())
123            .push(button::suggested(fl!("zap-done")).on_press(Message::CloseZap))
124            .align_y(Alignment::Center);
125
126        widget::Column::new()
127            .push(
128                text(fl!("zap-invoice-for", sats = invoice.sats, name = name))
129                    .size(14)
130                    .font(cosmic::font::bold())
131                    .wrapping(Wrapping::WordOrGlyph),
132            )
133            .push(invoice_code(qr))
134            .push(small_print(fl!("zap-scan")))
135            .push(small_print(what))
136            .push(buttons)
137            .spacing(8)
138            .into()
139    }
140
141    /// The zap went through.
142    fn view_zap_paid(&self, sats: u64, name: String) -> Element<'_, Message> {
143        widget::Column::new()
144            .push(
145                text(fl!("zap-paid", sats = sats, name = name))
146                    .size(16)
147                    .font(cosmic::font::bold())
148                    .wrapping(Wrapping::WordOrGlyph),
149            )
150            .push(button::suggested(fl!("zap-done")).on_press(Message::CloseZap))
151            .spacing(12)
152            .align_x(Alignment::Center)
153            .width(Length::Fill)
154            .into()
155    }
156
157    /// Render the zap view. With no note being zapped there is nothing to
158    /// show but what is under it.
159    pub fn view_zap(&self) -> Element<'_, Message> {
160        let Some(zap) = &self.zap else {
161            return self.view_feed();
162        };
163        let name = display_name(&self.profiles, &zap.recipient).into_owned();
164        let body = match &zap.stage {
165            ZapStage::Choosing | ZapStage::Requesting => self.view_zap_choice(zap, name),
166            ZapStage::Invoice { invoice, qr, copied, waiting } => {
167                self.view_zap_invoice(invoice, qr.as_ref(), *copied, waiting.is_some(), name)
168            }
169            ZapStage::Paid { sats } => self.view_zap_paid(*sats, name),
170        };
171
172        widget::Column::new()
173            .push(self.view_zap_header())
174            .push(widget::divider::horizontal::default())
175            .push(container(body).padding([8, 12]).width(Length::Fill))
176            .into()
177    }
178}