Skip to content

Commit 1a5c487

Browse files
committed
transaction_collapse
1 parent 6a3cc4b commit 1a5c487

File tree

5 files changed

+148
-12
lines changed

5 files changed

+148
-12
lines changed

Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bitcoin = { version = "0.27", features = ["base64", "use-serde"] }
2424
revaultd = { version = "0.4.0", default-features = false}
2525
backtrace = "0.3"
2626

27-
iced = { version = "0.4", default-features= false, features = ["tokio", "wgpu", "svg", "qr_code"] }
27+
iced = { version = "0.4", default-features= false, features = ["tokio", "wgpu", "svg", "qr_code", "pure"] }
2828
iced_native = "0.5"
2929
revault_ui = { path = "./ui" }
3030
revault_hwi = { path = "./hwi" }

src/app/view/vault.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use chrono::NaiveDateTime;
2-
use iced::{tooltip, Alignment, Column, Container, Element, Length, Row, Tooltip};
2+
use iced::{pure::Pure, tooltip, Alignment, Column, Container, Element, Length, Row, Tooltip};
33

44
use bitcoin::{util::bip32::Fingerprint, Amount};
55
use revault_ui::{
66
color,
7-
component::{badge, button, card, separation, text::Text, TooltipStyle},
7+
component::{badge, button, card, collapse::collapse, separation, text::Text, TooltipStyle},
88
icon,
99
};
1010

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

17-
#[derive(Debug)]
1817
pub struct VaultModal {
1918
copy_button: iced::button::State,
2019
modal: layout::Modal,
20+
state: iced::pure::State,
21+
}
22+
23+
impl std::fmt::Debug for VaultModal {
24+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25+
f.debug_struct("VaultModal").finish()
26+
}
2127
}
2228

2329
impl VaultModal {
2430
pub fn new() -> Self {
2531
VaultModal {
2632
copy_button: iced::button::State::default(),
2733
modal: layout::Modal::default(),
34+
state: iced::pure::State::default(),
2835
}
2936
}
3037

@@ -51,7 +58,12 @@ impl VaultModal {
5158
if let Some(tx) = &txs.unvault {
5259
col = col.push(transaction(ctx, "Unvault transaction", tx));
5360
}
54-
col = col.push(transaction(ctx, "Deposit transaction", &txs.deposit));
61+
col = col.push(transaction_collapse(
62+
ctx,
63+
"Deposit transaction",
64+
&txs.deposit,
65+
&mut self.state,
66+
));
5567

5668
self.modal.view(
5769
ctx,
@@ -120,6 +132,32 @@ fn vault<'a>(
120132
))
121133
}
122134

135+
fn transaction_collapse<'a, T: Clone + 'a>(
136+
ctx: &Context,
137+
title: &str,
138+
transaction: &WalletTransaction,
139+
state: &'a mut iced::pure::State,
140+
) -> Container<'a, T> {
141+
let tx = transaction_from_hex(&transaction.hex);
142+
Container::new(Pure::new(
143+
state,
144+
collapse::<_, T, _, _, _>(
145+
move || {
146+
iced::pure::row()
147+
.push(iced::pure::text("hello"))
148+
.push(iced::pure::text("hello-again"))
149+
.width(Length::Fill)
150+
.into()
151+
},
152+
move || {
153+
iced::pure::column()
154+
.push(iced::pure::text(format!("{}", tx.txid())))
155+
.into()
156+
},
157+
),
158+
))
159+
}
160+
123161
fn transaction<'a, T: 'a>(
124162
ctx: &Context,
125163
title: &str,

ui/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ edition = "2018"
99
resolver = "2"
1010

1111
[dependencies]
12-
iced = { version = "0.4", default-features= false, features = ["wgpu", "svg"] }
13-
iced_lazy = "0.1"
12+
iced = { version = "0.4", default-features= false, features = ["wgpu", "svg", "pure"] }
13+
iced_lazy = { version = "0.1", features = ["pure"]}
14+
iced_native = "0.5"
15+
iced_pure = "0.2.2"

ui/src/component/collapse.rs

+85-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,87 @@
1-
use iced::button::State;
2-
use iced_lazy::Component;
1+
use iced::{
2+
alignment,
3+
pure::{button, column, row, text},
4+
Alignment, Length,
5+
};
6+
use iced_lazy::pure::{self, Component};
7+
use iced_native::text;
8+
use iced_pure::Element;
9+
use std::marker::PhantomData;
310

4-
pub struct Collapse<'a> {
5-
button: &'a mut State,
6-
collapsed: bool,
11+
pub fn collapse<
12+
'a,
13+
Message: 'a,
14+
T: Into<Message> + Clone + 'a,
15+
Renderer: text::Renderer + 'static,
16+
H: Fn() -> Element<'a, T, Renderer> + 'a,
17+
C: Fn() -> Element<'a, T, Renderer> + 'a,
18+
>(
19+
header: H,
20+
content: C,
21+
) -> impl Into<Element<'a, Message, Renderer>> {
22+
Collapse {
23+
header,
24+
content,
25+
phantom: PhantomData,
26+
}
27+
}
28+
29+
struct Collapse<'a, H, C> {
30+
header: H,
31+
content: C,
32+
phantom: PhantomData<&'a H>,
33+
}
34+
35+
#[derive(Debug, Clone, Copy)]
36+
enum Event<T> {
37+
Internal(T),
38+
Collapse(bool),
39+
}
40+
41+
impl<'a, Message, Renderer, T, H, C> Component<Message, Renderer> for Collapse<'a, H, C>
42+
where
43+
T: Into<Message> + Clone + 'a,
44+
H: Fn() -> Element<'a, T, Renderer>,
45+
C: Fn() -> Element<'a, T, Renderer>,
46+
Renderer: text::Renderer + 'static,
47+
{
48+
type State = bool;
49+
type Event = Event<T>;
50+
51+
fn update(&mut self, state: &mut Self::State, event: Event<T>) -> Option<Message> {
52+
match event {
53+
Event::Internal(e) => Some(e.into()),
54+
Event::Collapse(s) => {
55+
*state = s;
56+
None
57+
}
58+
}
59+
}
60+
61+
fn view(&self, state: &Self::State) -> Element<Self::Event, Renderer> {
62+
if *state {
63+
column()
64+
.push(button((self.header)().map(Event::Internal)).on_press(Event::Collapse(false)))
65+
.push((self.content)().map(Event::Internal))
66+
.into()
67+
} else {
68+
column()
69+
.push(button((self.header)().map(Event::Internal)).on_press(Event::Collapse(true)))
70+
.into()
71+
}
72+
}
73+
}
74+
75+
impl<'a, Message, Renderer, T, H: 'a, C: 'a> From<Collapse<'a, H, C>>
76+
for Element<'a, Message, Renderer>
77+
where
78+
Message: 'a,
79+
Renderer: 'static + text::Renderer,
80+
T: Into<Message> + Clone + 'a,
81+
H: Fn() -> Element<'a, T, Renderer>,
82+
C: Fn() -> Element<'a, T, Renderer>,
83+
{
84+
fn from(c: Collapse<'a, H, C>) -> Self {
85+
pure::component(c).into()
86+
}
787
}

0 commit comments

Comments
 (0)