Skip to content

Commit

Permalink
fix balance in gui, add font support, bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed May 28, 2024
1 parent 59bb916 commit ba16ebd
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 15 deletions.
29 changes: 24 additions & 5 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 @@ -13,7 +13,7 @@ authors = [
"Nikita Chashchinskii <[email protected]>"
]
edition = "2021"
version = "0.8.8"
version = "0.8.9"

[workspace.dependencies.bip300301]
git = "https://github.com/Ash-L2L/bip300301.git"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Thunder

## Install

Check out the repo with `git clone`, and then

```
git submodule update --init
cargo build
```
1 change: 1 addition & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dirs = "5.0.1"
eframe = "0.27.1"
futures = "0.3.30"
human-size = "0.4.3"
include_path = "0.1.1"
jsonrpsee = { version = "0.20.0", features = ["server"] }
rustreexo = { workspace = true }
parking_lot = "0.12.1"
Expand Down
39 changes: 39 additions & 0 deletions app/gui/fonts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Load fonts for egui
use std::sync::LazyLock;

use eframe::egui::{FontData, FontDefinitions, FontFamily};

static FIRA_MONO_NERD_REGULAR: &[u8] = include_path::include_path_bytes!(
"../../res/nerd-fonts/patched-fonts/FiraMono/Regular/FiraMonoNerdFont-Regular.otf"
);

static NOTO_SANS_MONO_NERD_REGULAR: &[u8] = include_path::include_path_bytes!(
"../../res/nerd-fonts/patched-fonts/Noto/Sans-Mono/NotoSansMNerdFont-Regular.ttf"
);

pub static FONT_DEFINITIONS: LazyLock<FontDefinitions> = LazyLock::new(|| {
let mut fonts = FontDefinitions::default();
// Install fonts
fonts.font_data.insert(
"Fira Mono Nerd Regular".to_owned(),
FontData::from_static(FIRA_MONO_NERD_REGULAR),
);
fonts.font_data.insert(
"Noto Sans Mono Nerd Regular".to_owned(),
FontData::from_static(NOTO_SANS_MONO_NERD_REGULAR),
);
// Set Fira Mono Nerd Regular as first monospace font
fonts
.families
.get_mut(&FontFamily::Monospace)
.unwrap()
.insert(0, "Fira Mono Nerd Regular".to_owned());
// Set Noto Sans Mono Nerd Regular as second monospace font
fonts
.families
.get_mut(&FontFamily::Monospace)
.unwrap()
.insert(1, "Noto Sans Mono Nerd Regular".to_owned());
fonts
});
27 changes: 19 additions & 8 deletions app/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::{net::SocketAddr, task::Poll};

use eframe::egui;
use eframe::egui::{self, RichText};
use strum::{EnumIter, IntoEnumIterator};
use thunder::{util::Watchable, wallet::Wallet};
use util::{show_btc_amount_from_sats, BITCOIN_LOGO_FA, BITCOIN_ORANGE};

use crate::{app::App, line_buffer::LineBuffer, util::PromiseStream};

mod block_explorer;
mod coins;
mod console_logs;
mod fonts;
mod mempool_explorer;
mod miner;
mod parent_chain;
Expand All @@ -19,6 +21,7 @@ mod withdrawals;
use block_explorer::BlockExplorer;
use coins::Coins;
use console_logs::ConsoleLogs;
use fonts::FONT_DEFINITIONS;
use mempool_explorer::MemPoolExplorer;
use miner::Miner;
use parent_chain::ParentChain;
Expand Down Expand Up @@ -62,7 +65,7 @@ struct BottomPanel {
wallet_updated: PromiseStream<<Wallet as Watchable<()>>::WatchStream>,
/// None if uninitialized
/// Some(None) if failed to initialize
balance: Option<Option<u64>>,
balance_sats: Option<Option<u64>>,
}

impl BottomPanel {
Expand All @@ -71,13 +74,13 @@ impl BottomPanel {
let wallet_updated = PromiseStream::from(wallet.watch());
Self {
wallet_updated,
balance: None,
balance_sats: None,
}
}

/// Updates values
fn update(&mut self, app: &App) {
self.balance = match app.wallet.get_balance() {
self.balance_sats = match app.wallet.get_balance() {
Ok(balance) => Some(Some(balance)),
Err(err) => {
let err = anyhow::Error::from(err);
Expand All @@ -88,11 +91,18 @@ impl BottomPanel {
}

fn show_balance(&self, ui: &mut egui::Ui) {
match self.balance {
Some(Some(balance)) => {
match self.balance_sats {
Some(Some(balance_sats)) => {
ui.monospace(
RichText::new(BITCOIN_LOGO_FA.to_string())
.color(BITCOIN_ORANGE),
);
ui.monospace_selectable_singleline(
false,
format!("Balance: {balance}"),
format!(
"Balance: {}",
show_btc_amount_from_sats(balance_sats)
),
);
}
Some(None) => {
Expand Down Expand Up @@ -151,14 +161,15 @@ impl BottomPanel {
impl EguiApp {
pub fn new(
app: App,
_cc: &eframe::CreationContext<'_>,
cc: &eframe::CreationContext<'_>,
logs_capture: LineBuffer,
rpc_addr: SocketAddr,
) -> Self {
// Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
// Restore app state using cc.storage (requires the "persistence" feature).
// Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
// for e.g. egui::PaintCallback.
cc.egui_ctx.set_fonts(FONT_DEFINITIONS.clone());
let rt_guard = app.runtime.enter();
let bottom_panel = BottomPanel::new(&app.wallet);
drop(rt_guard);
Expand Down
27 changes: 26 additions & 1 deletion app/gui/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
use std::borrow::Borrow;

use eframe::egui::{self, InnerResponse, Response, Ui};
use bip300301::bitcoin;
use eframe::egui::{self, Color32, InnerResponse, Response, Ui};

/// Bitcoin Orange Color
pub const BITCOIN_ORANGE: Color32 = Color32::from_rgb(0xf7, 0x93, 0x1a);

/// Unicode BTC symbol (U+20BF)
pub const BTC_UNICODE: char = '\u{20bf}';

/// Font-Awesome Bitcoin Logo symbol (U+F10F)
/// Note that this symbol is wider than other glyphs, often taking up as much
/// space as 3 chars.
pub const BITCOIN_LOGO_FA: char = '\u{f10f}';

/// Show a [`bitcoin::Amount`]
pub fn show_btc_amount(amount: bitcoin::Amount) -> String {
format!(
"{BTC_UNICODE}{}",
amount.to_string_in(bitcoin::Denomination::Bitcoin)
)
}

/// Show a Bitcoin amount from sats
pub fn show_btc_amount_from_sats(sats: u64) -> String {
show_btc_amount(bitcoin::Amount::from_sat(sats))
}

// extension for InnerResponse<Response> and InnerResponse<Option<Response>>
pub trait InnerResponseExt {
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit ba16ebd

Please sign in to comment.