feat: tip frame (sign/verify/encode/decode), wallet balance + receive address, F6 wallet screen with QR, dispatch Kind::Tip
This commit is contained in:
parent
7b147d8ebe
commit
10d81964a5
10 changed files with 511 additions and 24 deletions
240
src/tui.rs
240
src/tui.rs
|
|
@ -16,6 +16,7 @@ use crate::outbox::DEFAULT_MAX_ATTEMPTS;
|
|||
use crate::qr::{self, QrPayload};
|
||||
use crate::sanitize;
|
||||
use crate::store::{Friend, FriendProfile, Message, OutboxEntry, OutboxStatus};
|
||||
use crate::wallet;
|
||||
|
||||
const C_ACCENT: Color = Color::Rgb(232, 165, 75);
|
||||
const C_TEXT: Color = Color::Rgb(196, 214, 176);
|
||||
|
|
@ -1111,6 +1112,12 @@ enum Screen {
|
|||
path: String,
|
||||
entry: PassphraseEntry,
|
||||
},
|
||||
Wallet {
|
||||
receive_addr: String,
|
||||
balance: String,
|
||||
transfers: Vec<wallet::TransferRow>,
|
||||
qr_modal: Option<QrModal>,
|
||||
},
|
||||
Quit {
|
||||
prompt: QuitPrompt,
|
||||
},
|
||||
|
|
@ -1217,6 +1224,7 @@ impl App {
|
|||
};
|
||||
}
|
||||
KeyCode::F(5) => self.who_open = true,
|
||||
KeyCode::F(6) => self.open_wallet()?,
|
||||
KeyCode::Esc => self.composer.clear(),
|
||||
KeyCode::Backspace => {
|
||||
if self.focus == Pane::Composer {
|
||||
|
|
@ -1383,6 +1391,11 @@ impl App {
|
|||
}
|
||||
_ => {}
|
||||
},
|
||||
Screen::Wallet { .. } => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Char('r') | KeyCode::Enter => self.wallet_toggle_qr(),
|
||||
_ => {}
|
||||
},
|
||||
Screen::Quit { prompt } => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Enter => match prompt.on_char('\n') {
|
||||
|
|
@ -1586,6 +1599,76 @@ impl App {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn open_wallet(&mut self) -> Result<(), String> {
|
||||
let addr = match self.rt.block_on(self.node.wallet_receive_address()) {
|
||||
Ok(a) => a,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let balance = self.rt.block_on(self.node.wallet_balance());
|
||||
let transfers = match self.rt.block_on(self.node.wallet_recent_transfers(8)) {
|
||||
Ok(t) => t,
|
||||
Err(_) => Vec::new(),
|
||||
};
|
||||
self.screen = Screen::Wallet {
|
||||
receive_addr: addr,
|
||||
balance,
|
||||
transfers,
|
||||
qr_modal: None,
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn wallet_toggle_qr(&mut self) {
|
||||
let Screen::Wallet {
|
||||
ref mut qr_modal,
|
||||
ref receive_addr,
|
||||
..
|
||||
} = self.screen
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if let Some(modal) = qr_modal {
|
||||
*qr_modal = if modal.closed {
|
||||
qr_modal.take()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
return;
|
||||
}
|
||||
let uri = if receive_addr.starts_with('4') || receive_addr.starts_with('8') {
|
||||
format!("monero:{receive_addr}")
|
||||
} else {
|
||||
receive_addr.clone()
|
||||
};
|
||||
let m = match qrcode::QrCode::new(uri.as_bytes()) {
|
||||
Ok(m) => m,
|
||||
Err(_) => {
|
||||
qr_modal.replace(QrModal {
|
||||
closed: false,
|
||||
matrix_rows: Vec::new(),
|
||||
uri: receive_addr.clone(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
let cols = m.to_colors();
|
||||
let w = m.width();
|
||||
let rows: Vec<String> = (0..w)
|
||||
.map(|y| {
|
||||
(0..w)
|
||||
.map(|x| {
|
||||
cols[y * w + x].select('█', ' ')
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
qr_modal.replace(QrModal {
|
||||
closed: false,
|
||||
matrix_rows: rows,
|
||||
uri: receive_addr.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
fn submit_paste(&mut self, raw: &str) -> Result<(), String> {
|
||||
match qr::decode(raw.trim()) {
|
||||
Err(e) => {
|
||||
|
|
@ -1710,7 +1793,7 @@ impl App {
|
|||
let node = Arc::clone(&self.node);
|
||||
let (tx, rx) = mpsc::channel();
|
||||
self.status_note = Some("sending".into());
|
||||
std::mem::drop(self.rt.spawn(async move {
|
||||
let _j = self.rt.spawn(async move {
|
||||
// `send()` reports `Sent` when the circuit took it and `Queued` when
|
||||
// the peer's onion is unreachable. The footer then reads the real
|
||||
// row count from the outbox, so this note is only the first word.
|
||||
|
|
@ -1720,7 +1803,7 @@ impl App {
|
|||
Err(e) => format!("send failed: {}", short_send_reason(&e, FAIL_REASON_BUDGET)),
|
||||
};
|
||||
let _ = tx.send(note);
|
||||
}));
|
||||
});
|
||||
self.job_rx = Some(rx);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1755,6 +1838,9 @@ impl App {
|
|||
draw_passphrase(f, *kind, entry.first().is_some(), entry.buf())
|
||||
}
|
||||
Screen::Quit { prompt } => draw_quit(f, prompt.typed()),
|
||||
Screen::Wallet { receive_addr, balance, transfers, qr_modal } => {
|
||||
draw_wallet(f, receive_addr, balance, transfers, qr_modal.as_ref())
|
||||
}
|
||||
}
|
||||
if self.help_open {
|
||||
draw_text_overlay(f, "? help", help_overlay_text());
|
||||
|
|
@ -2056,24 +2142,25 @@ pub fn main_footer_hints() -> &'static str {
|
|||
}
|
||||
|
||||
fn footer_hints(screen: &Screen) -> &'static str {
|
||||
match screen {
|
||||
Screen::Main => main_footer_hints(),
|
||||
Screen::Share { .. } => "Esc back ? help",
|
||||
Screen::Paste { .. } => "Enter submit Esc back ? help",
|
||||
Screen::Approve { .. } => "y accept n reject ? help",
|
||||
Screen::Rotate { .. } => "type ROTATE Esc cancel ? help",
|
||||
Screen::Wipe { kind, .. } => match kind {
|
||||
WipeKind::Messages => "type WIPE Esc cancel ? help",
|
||||
WipeKind::All => "type WIPEALL Esc cancel ? help",
|
||||
},
|
||||
Screen::Profile { .. } => "Enter save Tab field Esc cancel ? help",
|
||||
Screen::ConfirmKeys { kind, .. } => match kind {
|
||||
BackupKind::Backup => "type BACKUP Esc cancel ? help",
|
||||
BackupKind::Restore => "type RESTORE Esc cancel ? help",
|
||||
},
|
||||
Screen::Passphrase { .. } => "Enter submit Esc cancel ? help",
|
||||
Screen::Quit { .. } => "CLEAR or QUIT then Enter Esc cancel ? help",
|
||||
}
|
||||
match screen {
|
||||
Screen::Main => main_footer_hints(),
|
||||
Screen::Share { .. } => "Esc back ? help",
|
||||
Screen::Paste { .. } => "Enter submit Esc back ? help",
|
||||
Screen::Approve { .. } => "y accept n reject ? help",
|
||||
Screen::Rotate { .. } => "type ROTATE Esc cancel ? help",
|
||||
Screen::Wipe { kind, .. } => match kind {
|
||||
WipeKind::Messages => "type WIPE Esc cancel ? help",
|
||||
WipeKind::All => "type WIPEALL Esc cancel ? help",
|
||||
},
|
||||
Screen::Profile { .. } => "Enter save Tab field Esc cancel ? help",
|
||||
Screen::ConfirmKeys { kind, .. } => match kind {
|
||||
BackupKind::Backup => "type BACKUP Esc cancel ? help",
|
||||
BackupKind::Restore => "type RESTORE Esc cancel ? help",
|
||||
},
|
||||
Screen::Passphrase { .. } => "Enter submit Esc cancel ? help",
|
||||
Screen::Wallet { .. } => "r toggle qr Esc back ? help",
|
||||
Screen::Quit { .. } => "CLEAR or QUIT then Enter Esc cancel ? help",
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_chrome_borders(block: Block<'_>) -> Block<'_> {
|
||||
|
|
@ -2308,6 +2395,119 @@ fn to_hex(bytes: &[u8]) -> String {
|
|||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct QrModal {
|
||||
closed: bool,
|
||||
matrix_rows: Vec<String>,
|
||||
#[allow(dead_code)]
|
||||
uri: String,
|
||||
}
|
||||
|
||||
fn draw_wallet(
|
||||
f: &mut Frame,
|
||||
receive_addr: &str,
|
||||
balance: &str,
|
||||
transfers: &[wallet::TransferRow],
|
||||
modal: Option<&QrModal>,
|
||||
) {
|
||||
let area = f.area();
|
||||
if let Some(m) = modal {
|
||||
// Clear behind the popup so the body does not bleed through.
|
||||
f.render_widget(Clear, area);
|
||||
let inner_w = area.width.saturating_sub(2).max(1);
|
||||
let inner_h = area.height.saturating_sub(2).max(1);
|
||||
let rows = m.matrix_rows.len().max(1);
|
||||
let content_h = rows.min(inner_h as usize);
|
||||
let popup_h = content_h as u16 + 2;
|
||||
let popup_w = inner_w.max(6);
|
||||
let x = area.x + (area.width.saturating_sub(popup_w)) / 2;
|
||||
let y = area.y + (area.height.saturating_sub(popup_h)) / 2;
|
||||
let popup = Rect {
|
||||
x,
|
||||
y,
|
||||
width: popup_w,
|
||||
height: popup_h,
|
||||
};
|
||||
f.render_widget(Clear, popup);
|
||||
let mut body = String::from("receive address\nEsc or R closes\n\n");
|
||||
if m.matrix_rows.is_empty() {
|
||||
body.push_str("(qr error)");
|
||||
} else {
|
||||
for row in m.matrix_rows.iter().take(content_h) {
|
||||
body.push_str(row);
|
||||
body.push('\n');
|
||||
}
|
||||
}
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.style(style_text())
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(
|
||||
apply_chrome_borders(Block::default())
|
||||
.border_style(style_accent())
|
||||
.title("qr — monero:receive"),
|
||||
),
|
||||
popup,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let cols = Layout::vertical([
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(4),
|
||||
Constraint::Length(2),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new("wallet")
|
||||
.style(style_accent())
|
||||
.block(pane_block_for("wallet", true)),
|
||||
cols[0],
|
||||
);
|
||||
|
||||
let addr_line = if receive_addr.is_empty() {
|
||||
"no address".into()
|
||||
} else {
|
||||
receive_addr.to_string()
|
||||
};
|
||||
let body = format!(
|
||||
"address
|
||||
{addr_line}
|
||||
|
||||
balance
|
||||
{balance}
|
||||
|
||||
recent transfers
|
||||
"
|
||||
);
|
||||
let mut lines: Vec<String> = body
|
||||
.lines()
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
if transfers.is_empty() {
|
||||
lines.push(" (none)".into());
|
||||
} else {
|
||||
for t in transfers.iter().take(5) {
|
||||
let xmr = crate::pay::atomic_to_xmr_str(&t.amount);
|
||||
lines.push(format!(" {} {} XMR", t.txid.chars().take(16).collect::<String>(), xmr));
|
||||
}
|
||||
}
|
||||
let help = "r toggle qr Esc back";
|
||||
lines.push("".into());
|
||||
f.render_widget(
|
||||
Paragraph::new(lines.join("\n"))
|
||||
.style(style_text())
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(apply_chrome_borders(Block::default()).border_style(style_border())),
|
||||
cols[2],
|
||||
);
|
||||
|
||||
f.render_widget(Paragraph::new(help).style(style_dim()), cols[3]);
|
||||
}
|
||||
|
||||
fn io_err(e: io::Error) -> String {
|
||||
e.to_string()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue