Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: add iced_lazy #340

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 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
47 changes: 43 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,33 @@ 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)
.spacing(20)
.into()
},
move || {
iced::pure::column()
.push(iced::pure::text(format!("{}", tx.txid())))
.into()
},
),
))
}

fn transaction<'a, T: 'a>(
ctx: &Context,
title: &str,
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct Sandbox<S: State> {
state: S,
}

impl<S: State + Send + 'static> Sandbox<S> {
impl<S: State + 'static> Sandbox<S> {
pub fn new(state: S) -> Self {
return Self { state };
}
Expand Down
5 changes: 4 additions & 1 deletion ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ edition = "2018"
resolver = "2"

[dependencies]
iced = { version = "0.4", default-features= false, features = ["wgpu", "svg"] }
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"
87 changes: 87 additions & 0 deletions ui/src/component/collapse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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 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()
}
}
1 change: 1 addition & 0 deletions ui/src/component/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod badge;
pub mod button;
pub mod collapse;
pub mod form;
pub mod image;
pub mod notification;
Expand Down