From 562e5db573dc9a75a0979726fe82aa458d6cb3a8 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 14 May 2024 22:41:32 -0500 Subject: [PATCH] unlock works --- src/components/button.rs | 9 ++++++- src/components/easing.rs | 3 +++ src/components/input.rs | 4 ++- src/components/spinner.rs | 6 +++-- src/core.rs | 5 ++++ src/main.rs | 3 ++- src/routes/mints.rs | 2 +- src/routes/receive.rs | 2 +- src/routes/send.rs | 4 +-- src/routes/unlock.rs | 52 ++++++++++++++++++++++----------------- 10 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/components/button.rs b/src/components/button.rs index 98b96aa..53b1dac 100644 --- a/src/components/button.rs +++ b/src/components/button.rs @@ -26,8 +26,15 @@ pub fn h_button(text_str: &str, icon: SvgIcon, loading: bool) -> Button<'_, Mess Button::new(center(content)) .style(|theme, status| { + let gray = lighten(theme.palette().background, 0.5); + + let border_color = match status { + Status::Disabled => gray, + _ => Color::WHITE, + }; + let border = Border { - color: Color::WHITE, + color: border_color, width: 2., radius: (8.).into(), }; diff --git a/src/components/easing.rs b/src/components/easing.rs index 5966f8a..5d1ccb3 100644 --- a/src/components/easing.rs +++ b/src/components/easing.rs @@ -1,3 +1,5 @@ +// Stolen from https://github.com/iced-rs/iced/tree/master/examples/loading_spinners + use iced::Point; use lyon_algorithms::measure::PathMeasurements; @@ -111,6 +113,7 @@ impl Builder { fn point(p: impl Into) -> lyon_algorithms::geom::Point { let p: Point = p.into(); + #[allow(clippy::manual_clamp)] lyon_algorithms::geom::point(p.x.min(1.0).max(0.0), p.y.min(1.0).max(0.0)) } } diff --git a/src/components/input.rs b/src/components/input.rs index 130f0ab..05bc903 100644 --- a/src/components/input.rs +++ b/src/components/input.rs @@ -23,11 +23,13 @@ pub fn h_input<'a>( placeholder: &'static str, value: &'a str, on_input: impl Fn(String) -> Message + 'a, - on_submit: Message, + on_submit: Option, secure: bool, id: Option<&'static str>, suffix: Option<&'static str>, ) -> Element<'a, Message, Theme> { + let on_submit = on_submit.unwrap_or(Message::Noop); + let input = TextInput::new(placeholder, value) .style(|theme: &Theme, status| { let gray = lighten(theme.palette().background, 0.5); diff --git a/src/components/spinner.rs b/src/components/spinner.rs index f303fc2..195737c 100644 --- a/src/components/spinner.rs +++ b/src/components/spinner.rs @@ -1,3 +1,5 @@ +// Stolen from https://github.com/iced-rs/iced/tree/master/examples/loading_spinners + //! Show a circular progress indicator. use iced::advanced::layout; use iced::advanced::renderer; @@ -26,9 +28,9 @@ const BASE_ROTATION_SPEED: u32 = u32::MAX / 80; pub fn the_spinner() -> Element<'static, Message, Theme> { let easing = &STANDARD; let spinner = Circular::::new() - .easing(&easing) + .easing(easing) .cycle_duration(Duration::from_secs_f32(2.)); - return spinner.into(); + spinner.into() } #[allow(missing_debug_implementations)] diff --git a/src/core.rs b/src/core.rs index 868ed16..8ff582e 100644 --- a/src/core.rs +++ b/src/core.rs @@ -13,6 +13,7 @@ use std::str::FromStr; use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::time::{Duration, SystemTime}; +use tokio::time::sleep; use iced::{ futures::{channel::mpsc::Sender, SinkExt}, @@ -219,10 +220,14 @@ pub fn run_core() -> Subscription { match msg { Some(UICoreMsg::Unlock(password)) => { + log::info!("Sending unlock message"); tx.send(Message::CoreMessage(CoreUIMsg::Unlocking)) .await .expect("should send"); + // TODO: for some reason the unlocking message gets delivered at the end if I don't sleep here + sleep(Duration::from_secs(1)).await; + // attempting to unlock let db = setup_db( path.join("harbor.sqlite") diff --git a/src/main.rs b/src/main.rs index c8f735b..95a6ae3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,7 +91,7 @@ enum ReceiveStatus { WaitingToReceive, } -#[derive(Default, Debug, Clone)] +#[derive(Default, Debug, Clone, PartialEq)] enum UnlockStatus { #[default] Locked, @@ -400,6 +400,7 @@ impl HarborWallet { Command::none() } CoreUIMsg::Unlocking => { + info!("Got unlocking message"); self.unlock_status = UnlockStatus::Unlocking; Command::none() } diff --git a/src/routes/mints.rs b/src/routes/mints.rs index 3b2b519..5ebfe92 100644 --- a/src/routes/mints.rs +++ b/src/routes/mints.rs @@ -13,7 +13,7 @@ pub fn mints(harbor: &HarborWallet) -> Element { "", &harbor.mint_invite_code_str, Message::MintInviteCodeInputChanged, - Message::Noop, + None, false, None, None, diff --git a/src/routes/receive.rs b/src/routes/receive.rs index a48a7b7..b440c97 100644 --- a/src/routes/receive.rs +++ b/src/routes/receive.rs @@ -47,7 +47,7 @@ pub fn receive(harbor: &HarborWallet) -> Element { "420", &harbor.receive_amount_str, Message::ReceiveAmountChanged, - Message::Noop, + None, false, None, Some("sats"), diff --git a/src/routes/send.rs b/src/routes/send.rs index 2a2ccb9..1ce40ce 100644 --- a/src/routes/send.rs +++ b/src/routes/send.rs @@ -23,7 +23,7 @@ pub fn send(harbor: &HarborWallet) -> Element { "420", &harbor.send_amount_input_str, Message::SendAmountInputChanged, - Message::Noop, + None, false, None, Some("sats"), @@ -34,7 +34,7 @@ pub fn send(harbor: &HarborWallet) -> Element { "abc123...", &harbor.send_dest_input_str, Message::SendDestInputChanged, - Message::Noop, + None, false, None, None, diff --git a/src/routes/unlock.rs b/src/routes/unlock.rs index 7c2f829..020c458 100644 --- a/src/routes/unlock.rs +++ b/src/routes/unlock.rs @@ -1,23 +1,36 @@ -use crate::components::{h_button, h_input, SvgIcon}; +use crate::{ + components::{h_button, h_input, SvgIcon}, + UnlockStatus, +}; use iced::{ widget::{center, column, container, text, Svg}, - Color, Theme, + Color, }; use iced::{Alignment, Element, Length}; use crate::{HarborWallet, Message}; -pub fn unlock(harbor: &HarborWallet) -> Element { - let unlock_button = h_button("Unlock", SvgIcon::DownLeft, false) - .on_press(Message::Unlock(harbor.password_input_str.clone())) - .width(Length::Fill); +pub fn unlock(harbor: &HarborWallet) -> Element { + let action = if harbor.unlock_status == UnlockStatus::Unlocking { + None + } else { + Some(Message::Unlock(harbor.password_input_str.clone())) + }; + + let unlock_button = h_button( + "Unlock", + SvgIcon::DownLeft, + harbor.unlock_status == UnlockStatus::Unlocking, + ) + .on_press_maybe(action.clone()) + .width(Length::Fill); let password_input = h_input( "Password", "", &harbor.password_input_str, Message::PasswordInputChanged, - Message::Unlock(harbor.password_input_str.clone()), + action, true, Some("password_unlock_input"), None, @@ -27,21 +40,16 @@ pub fn unlock(harbor: &HarborWallet) -> Element { .width(167) .height(61); - if let Some(e) = &harbor.unlock_failure_reason { - let error_reason = text(format!("Error: {:?}", e)) + let page_column = column![harbor_logo, password_input, unlock_button,] + .spacing(32) + .align_items(Alignment::Center) + .width(Length::Fixed(256.)); + + let page_column = page_column.push_maybe(harbor.unlock_failure_reason.as_ref().map(|r| { + text(format!("Error: {:?}", r)) .size(24) - .color(Color::from_rgb8(250, 0, 80)); + .color(Color::from_rgb8(250, 0, 80)) + })); - let page_columns = column![harbor_logo, password_input, unlock_button, error_reason] - .spacing(32) - .align_items(Alignment::Center) - .width(Length::Fixed(256.)); - container(center(page_columns)).into() - } else { - let page_columns = column![harbor_logo, password_input, unlock_button] - .spacing(32) - .align_items(Alignment::Center) - .width(Length::Fixed(256.)); - container(center(page_columns)).into() - } + container(center(page_column)).into() }