From ef306205c4a30a3375682e0f8a3b6273ecc9683b Mon Sep 17 00:00:00 2001 From: Anton Yemelyanov Date: Wed, 20 Dec 2023 19:13:49 +0200 Subject: [PATCH] donations panel --- core/src/modules/donations.rs | 105 ++++++++++++++++++++++++++++++++++ core/src/modules/mod.rs | 3 +- core/src/modules/overview.rs | 8 +-- core/src/utils/format.rs | 9 ++- 4 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 core/src/modules/donations.rs diff --git a/core/src/modules/donations.rs b/core/src/modules/donations.rs new file mode 100644 index 0000000..2806ac9 --- /dev/null +++ b/core/src/modules/donations.rs @@ -0,0 +1,105 @@ +use std::{borrow::Cow, collections::hash_map::Entry}; + +use crate::imports::*; +pub struct Donations { + qr : HashMap, +} + +impl Donations { + + pub const ADDRESS: &'static str = "kaspatest:qqdr2mv4vkes6kvhgy8elsxhvzwde42629vnpcxe4f802346rnfkklrhz0x7x"; + + pub fn new(_runtime: Runtime) -> Self { + Self { + qr : Default::default(), + } + } + + fn qr(&mut self) -> (String,load::Bytes) { + + let (uri,qr) = match self.qr.entry(theme_color().name.clone()) { + Entry::Occupied(entry) => entry.into_mut(), + Entry::Vacant(entry) => { + let uri = format!("bytes://{}-{}.svg", Self::ADDRESS, theme_color().name); + let qr = render_qrcode(&Self::ADDRESS, 128, 128); + entry.insert((uri, qr.as_bytes().to_vec().into())) + }, + }; + + (uri.clone(),qr.clone()) + } + +} + +impl ModuleT for Donations { + + fn style(&self) -> ModuleStyle { + ModuleStyle::Mobile + } + + fn render( + &mut self, + core: &mut Core, + _ctx: &egui::Context, + _frame: &mut eframe::Frame, + ui: &mut egui::Ui, + ) { + let (uri, qr) = self.qr(); + let back = Rc::new(RefCell::new(false)); + + Panel::new(self) + .with_caption("Supporting Kaspa NG") + .with_back_enabled(core.has_stack(), |_|{ + *back.borrow_mut() = true; + }) + .with_close_enabled(false, |_|{ + }) + // .with_header(|_ctx,ui| { + + // }) + .with_body(|_ctx,ui| { + use egui_phosphor::light::CLIPBOARD_TEXT; + + ui.add_space(8.); + ui.label("This project relies on the support of the community in all areas including development, testing and funding."); + ui.label(" "); + ui.label("If you are able to contribute by donating, we would greatly appreciate your support."); + ui.label(" "); + ui.label("You can send donations to the following address:"); + ui.label(" "); + + ui.add( + Image::new(ImageSource::Bytes { uri : Cow::Owned(uri), bytes: qr }) + .fit_to_original_size(1.0) + .texture_options(TextureOptions::NEAREST) + ); + + ui.label(" "); + + if ui + .add(Label::new(format!("{} {CLIPBOARD_TEXT}", format_address_string(Self::ADDRESS, Some(12)))).sense(Sense::click())) + .on_hover_ui_at_pointer(|ui|{ + ui.vertical(|ui|{ + ui.label("Click to copy the donation address to clipboard".to_string()); + }); + }) + .clicked() { + ui.output_mut(|o| o.copied_text = Self::ADDRESS.to_owned()); + runtime().notify(UserNotification::info(format!("{CLIPBOARD_TEXT} {}", i18n("Copied to clipboard"))).short()) + } + + }) + .with_footer(|_this,ui| { + if ui.large_button("Close").clicked() { + *back.borrow_mut() = true; + } + }) + .render(ui); + + if *back.borrow_mut() { + core.back(); + } + + } + +} diff --git a/core/src/modules/mod.rs b/core/src/modules/mod.rs index e959312..a92f69e 100644 --- a/core/src/modules/mod.rs +++ b/core/src/modules/mod.rs @@ -9,6 +9,7 @@ kaspa_ng_macros::register_modules!( account_manager, block_dag, changelog, + donations, export, import, metrics, @@ -19,8 +20,8 @@ kaspa_ng_macros::register_modules!( settings, testing, wallet_create, - wallet_secret, wallet_open, + wallet_secret, welcome, ] ); diff --git a/core/src/modules/overview.rs b/core/src/modules/overview.rs index bea8eb4..22ebf6b 100644 --- a/core/src/modules/overview.rs +++ b/core/src/modules/overview.rs @@ -90,7 +90,7 @@ impl Overview { let top = 32.; let logo_rect = Rect::from_min_size(Pos2::new(left, top), logo_size); - if screen_rect.width() > 768.0 { + if screen_rect.width() > 768.0 && !core.device().single_pane() { Image::new(ImageSource::Bytes { uri : Cow::Borrowed("bytes://logo.svg"), bytes : Bytes::Static(crate::app::KASPA_NG_LOGO_SVG)}) .maintain_aspect_ratio(true) .max_size(logo_size) @@ -328,10 +328,8 @@ impl Overview { .default_open(true) .show(ui, |ui| { ui.label("Please support Kaspa NG development"); - // if ui.link("kaspatest:qqdr2mv4vkes6kvhgy8elsxhvzwde42629vnpcxe4f802346rnfkklrhz0x7x").clicked() { - let donation_address = "kaspatest:qqdr2mv4vkes6kvhgy8elsxhvzwde42629vnpcxe4f802346rnfkklrhz0x7x"; - if ui.link(format_address(&Address::try_from(donation_address).unwrap(), Some(12))).clicked() { - println!("link clicked..."); + if ui.link(format_address_string(modules::Donations::ADDRESS, Some(12))).clicked() { + core.select::(); } }); }); diff --git a/core/src/utils/format.rs b/core/src/utils/format.rs index 1a59eee..2f0b6e8 100644 --- a/core/src/utils/format.rs +++ b/core/src/utils/format.rs @@ -21,9 +21,8 @@ pub fn format_duration(millis: u64) -> String { } } -pub fn format_address(address: &Address, range: Option) -> String { - let address = address.to_string(); - +pub fn format_address_string(address: impl Into, range: Option) -> String { + let address = address.into(); let parts = address.split(':').collect::>(); let prefix = parts[0]; let payload = parts[1]; @@ -38,6 +37,10 @@ pub fn format_address(address: &Address, range: Option) -> String { format!("{prefix}:{left}....{right}") } +pub fn format_address(address: &Address, range: Option) -> String { + format_address_string(address, range) +} + /// SOMPI (u64) to KASPA (string) with suffix layout job generator pub fn s2kws_layout_job( enable: bool,