Skip to content

Commit

Permalink
transaction_collapse
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Jun 8, 2022
1 parent 6a3cc4b commit 1a5c487
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 12 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bitcoin = { version = "0.27", features = ["base64", "use-serde"] }
revaultd = { version = "0.4.0", default-features = false}
backtrace = "0.3"

iced = { version = "0.4", default-features= false, features = ["tokio", "wgpu", "svg", "qr_code"] }
iced = { version = "0.4", default-features= false, features = ["tokio", "wgpu", "svg", "qr_code", "pure"] }
iced_native = "0.5"
revault_ui = { path = "./ui" }
revault_hwi = { path = "./hwi" }
Expand Down
46 changes: 42 additions & 4 deletions src/app/view/vault.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chrono::NaiveDateTime;
use iced::{tooltip, Alignment, Column, Container, Element, Length, Row, Tooltip};
use iced::{pure::Pure, tooltip, Alignment, Column, Container, Element, Length, Row, Tooltip};

use bitcoin::{util::bip32::Fingerprint, Amount};
use revault_ui::{
color,
component::{badge, button, card, separation, text::Text, TooltipStyle},
component::{badge, button, card, collapse::collapse, separation, text::Text, TooltipStyle},
icon,
};

Expand All @@ -14,17 +14,24 @@ use crate::daemon::model::{
outpoint, transaction_from_hex, Vault, VaultStatus, VaultTransactions, WalletTransaction,
};

#[derive(Debug)]
pub struct VaultModal {
copy_button: iced::button::State,
modal: layout::Modal,
state: iced::pure::State,
}

impl std::fmt::Debug for VaultModal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VaultModal").finish()
}
}

impl VaultModal {
pub fn new() -> Self {
VaultModal {
copy_button: iced::button::State::default(),
modal: layout::Modal::default(),
state: iced::pure::State::default(),
}
}

Expand All @@ -51,7 +58,12 @@ impl VaultModal {
if let Some(tx) = &txs.unvault {
col = col.push(transaction(ctx, "Unvault transaction", tx));
}
col = col.push(transaction(ctx, "Deposit transaction", &txs.deposit));
col = col.push(transaction_collapse(
ctx,
"Deposit transaction",
&txs.deposit,
&mut self.state,
));

self.modal.view(
ctx,
Expand Down Expand Up @@ -120,6 +132,32 @@ fn vault<'a>(
))
}

fn transaction_collapse<'a, T: Clone + 'a>(
ctx: &Context,
title: &str,
transaction: &WalletTransaction,
state: &'a mut iced::pure::State,
) -> Container<'a, T> {
let tx = transaction_from_hex(&transaction.hex);
Container::new(Pure::new(
state,
collapse::<_, T, _, _, _>(
move || {
iced::pure::row()
.push(iced::pure::text("hello"))
.push(iced::pure::text("hello-again"))
.width(Length::Fill)
.into()
},
move || {
iced::pure::column()
.push(iced::pure::text(format!("{}", tx.txid())))
.into()
},
),
))
}

fn transaction<'a, T: 'a>(
ctx: &Context,
title: &str,
Expand Down
6 changes: 4 additions & 2 deletions ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ edition = "2018"
resolver = "2"

[dependencies]
iced = { version = "0.4", default-features= false, features = ["wgpu", "svg"] }
iced_lazy = "0.1"
iced = { version = "0.4", default-features= false, features = ["wgpu", "svg", "pure"] }
iced_lazy = { version = "0.1", features = ["pure"]}
iced_native = "0.5"
iced_pure = "0.2.2"
90 changes: 85 additions & 5 deletions ui/src/component/collapse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,87 @@
use iced::button::State;
use iced_lazy::Component;
use iced::{
alignment,
pure::{button, column, row, text},
Alignment, Length,
};
use iced_lazy::pure::{self, Component};
use iced_native::text;
use iced_pure::Element;
use std::marker::PhantomData;

pub struct Collapse<'a> {
button: &'a mut State,
collapsed: bool,
pub fn collapse<
'a,
Message: 'a,
T: Into<Message> + Clone + 'a,
Renderer: text::Renderer + 'static,
H: Fn() -> Element<'a, T, Renderer> + 'a,
C: Fn() -> Element<'a, T, Renderer> + 'a,
>(
header: H,
content: C,
) -> impl Into<Element<'a, Message, Renderer>> {
Collapse {
header,
content,
phantom: PhantomData,
}
}

struct Collapse<'a, H, C> {
header: H,
content: C,
phantom: PhantomData<&'a H>,
}

#[derive(Debug, Clone, Copy)]
enum Event<T> {
Internal(T),
Collapse(bool),
}

impl<'a, Message, Renderer, T, H, C> Component<Message, Renderer> for Collapse<'a, H, C>
where
T: Into<Message> + Clone + 'a,
H: Fn() -> Element<'a, T, Renderer>,
C: Fn() -> Element<'a, T, Renderer>,
Renderer: text::Renderer + 'static,
{
type State = bool;
type Event = Event<T>;

fn update(&mut self, state: &mut Self::State, event: Event<T>) -> Option<Message> {
match event {
Event::Internal(e) => Some(e.into()),
Event::Collapse(s) => {
*state = s;
None
}
}
}

fn view(&self, state: &Self::State) -> Element<Self::Event, Renderer> {
if *state {
column()
.push(button((self.header)().map(Event::Internal)).on_press(Event::Collapse(false)))
.push((self.content)().map(Event::Internal))
.into()
} else {
column()
.push(button((self.header)().map(Event::Internal)).on_press(Event::Collapse(true)))
.into()
}
}
}

impl<'a, Message, Renderer, T, H: 'a, C: 'a> From<Collapse<'a, H, C>>
for Element<'a, Message, Renderer>
where
Message: 'a,
Renderer: 'static + text::Renderer,
T: Into<Message> + Clone + 'a,
H: Fn() -> Element<'a, T, Renderer>,
C: Fn() -> Element<'a, T, Renderer>,
{
fn from(c: Collapse<'a, H, C>) -> Self {
pure::component(c).into()
}
}

0 comments on commit 1a5c487

Please sign in to comment.