From 4c37a3dbee3cef210d27ee95a1ff155b16339eda Mon Sep 17 00:00:00 2001 From: Anton Yemelyanov Date: Sat, 13 Jan 2024 08:54:40 +0200 Subject: [PATCH] wip - alternate server option in status bar --- core/src/primitives/block.rs | 83 ++++++++++++++------------ core/src/runtime/services/kaspa/mod.rs | 9 +++ core/src/status.rs | 29 +++++++++ resources/i18n/i18n.json | 7 ++- 4 files changed, 88 insertions(+), 40 deletions(-) diff --git a/core/src/primitives/block.rs b/core/src/primitives/block.rs index d5f8b45..7fd9d0a 100644 --- a/core/src/primitives/block.rs +++ b/core/src/primitives/block.rs @@ -7,6 +7,7 @@ pub struct BlockDagGraphSettings { pub y_dist: f64, pub graph_length_daa: usize, pub center_vspc: bool, + pub balance_vspc: bool, pub show_vspc: bool, pub show_daa: bool, pub show_grid: bool, @@ -16,10 +17,10 @@ impl Default for BlockDagGraphSettings { fn default() -> Self { Self { y_scale: 10.0, - // y_dist: 70.0, y_dist: 7.0, graph_length_daa: 1024, center_vspc: false, + balance_vspc: true, show_vspc: true, show_daa: true, show_grid: true, @@ -106,49 +107,57 @@ impl DaaBucket { let y_distance = settings.y_dist; let len = self.blocks.len(); - #[allow(clippy::collapsible_else_if)] - if let Some(mut vspc_idx) = self.blocks.iter().position(|block| block.vspc) { - if settings.center_vspc && len > 2 { - let mid = len / 2; - if vspc_idx != mid { - self.blocks.swap(vspc_idx, mid); - vspc_idx = mid; - self.blocks.iter_mut().for_each(|block| { - block.settled = false; - }); + if settings.balance_vspc { + #[allow(clippy::collapsible_else_if)] + if let Some(mut vspc_idx) = self.blocks.iter().position(|block| block.vspc) { + if settings.center_vspc && len > 2 { + let mid = len / 2; + if vspc_idx != mid { + self.blocks.swap(vspc_idx, mid); + vspc_idx = mid; + self.blocks.iter_mut().for_each(|block| { + block.settled = false; + }); + } } - } - let vspc_y = if settings.center_vspc { - 0.0 - } else { - self.blocks - .get(vspc_idx) - .map(|block| block.dst_y) - .unwrap_or_default() - }; - - let mut y = vspc_y; - (0..vspc_idx).rev().for_each(|idx| { - let block = &mut self.blocks[idx]; - y -= y_distance; - block.dst_y = y; - }); - y = vspc_y; - ((vspc_idx + 1)..len).for_each(|idx| { - let block = &mut self.blocks[idx]; - y += y_distance; - block.dst_y = y; - }); - } else { - if len > 1 { - let mut y = -(len as f64 * y_distance / 2.0); - (0..len).for_each(|idx| { + let vspc_y = if settings.center_vspc { + 0.0 + } else { + self.blocks + .get(vspc_idx) + .map(|block| block.dst_y) + .unwrap_or_default() + }; + + let mut y = vspc_y; + (0..vspc_idx).rev().for_each(|idx| { + let block = &mut self.blocks[idx]; + y -= y_distance; + block.dst_y = y; + }); + y = vspc_y; + ((vspc_idx + 1)..len).for_each(|idx| { let block = &mut self.blocks[idx]; y += y_distance; block.dst_y = y; }); + } else { + if len > 1 { + let mut y = -(len as f64 * y_distance / 2.0); + (0..len).for_each(|idx| { + let block = &mut self.blocks[idx]; + y += y_distance; + block.dst_y = y; + }); + } } + } else { + (0..len).for_each(|idx| { + let block = &mut self.blocks[idx]; + block.dst_y = + hash_to_y_coord(&block.data.header.hash, settings.y_scale) * y_distance * 0.3; + }); } } diff --git a/core/src/runtime/services/kaspa/mod.rs b/core/src/runtime/services/kaspa/mod.rs index c93062b..434f06f 100644 --- a/core/src/runtime/services/kaspa/mod.rs +++ b/core/src/runtime/services/kaspa/mod.rs @@ -77,6 +77,7 @@ pub struct KaspaService { pub task_ctl: Channel<()>, pub network: Mutex, pub wallet: Arc, + pub services_start_instant: Mutex>, #[cfg(not(target_arch = "wasm32"))] pub kaspad: Mutex>>, #[cfg(not(target_arch = "wasm32"))] @@ -122,6 +123,7 @@ impl KaspaService { task_ctl: Channel::oneshot(), network: Mutex::new(settings.node.network), wallet: Arc::new(wallet), + services_start_instant: Mutex::new(None), #[cfg(not(target_arch = "wasm32"))] kaspad: Mutex::new(None), #[cfg(not(target_arch = "wasm32"))] @@ -261,6 +263,8 @@ impl KaspaService { } pub async fn stop_all_services(&self) -> Result<()> { + self.services_start_instant.lock().unwrap().take(); + if !self.wallet().has_rpc() { return Ok(()); } @@ -304,6 +308,11 @@ impl KaspaService { } pub async fn start_all_services(self: &Arc, rpc: Rpc, network: Network) -> Result<()> { + self.services_start_instant + .lock() + .unwrap() + .replace(Instant::now()); + let rpc_api = rpc.rpc_api().clone(); self.wallet() diff --git a/core/src/status.rs b/core/src/status.rs index af47355..a39f7cb 100644 --- a/core/src/status.rs +++ b/core/src/status.rs @@ -223,12 +223,41 @@ impl<'core> Status<'core> { } } NodeConnectionConfigKind::PublicServerRandom => { + if let Some(instant) = runtime() + .kaspa_service() + .services_start_instant + .lock() + .unwrap() + .as_ref() + { + let elapsed = instant.elapsed(); + if elapsed.as_millis() > 2_500 { + if ui + .add( + Label::new(RichText::new(i18n( + "Click to try an another server...", + ))) + .sense(Sense::click()), + ) + .clicked() + { + runtime() + .kaspa_service() + .update_services(&self.core.settings.node); + } + + ui.separator(); + } + } + if let Some(rpc_url) = runtime().kaspa_service().rpc_url() { ui.label(format!( "{} {} ...", i18n("Connecting to"), rpc_url )); + + ui.ctx().request_repaint_after(Duration::from_millis(250)); } } }, diff --git a/resources/i18n/i18n.json b/resources/i18n/i18n.json index 255a924..61fe3a3 100644 --- a/resources/i18n/i18n.json +++ b/resources/i18n/i18n.json @@ -19,8 +19,8 @@ "sv": "Swedish", "pa": "Panjabi", "nl": "Dutch", - "es": "Español", "uk": "Ukrainian", + "es": "Español", "af": "Afrikaans", "et": "Esti", "en": "English", @@ -65,10 +65,10 @@ "fa": {}, "lt": {}, "sv": {}, + "es": {}, "pa": {}, "nl": {}, "uk": {}, - "es": {}, "af": {}, "et": {}, "en": { @@ -123,9 +123,9 @@ "Settings": "Settings", "Check for Updates": "Check for Updates", "wRPC Connection Settings": "wRPC Connection Settings", + "Custom Public Node": "Custom Public Node", "p2p Rx/s": "p2p Rx/s", "Theme Color": "Theme Color", - "Custom Public Node": "Custom Public Node", "Remote Connection:": "Remote Connection:", "Difficulty": "Difficulty", "Uptime:": "Uptime:", @@ -303,6 +303,7 @@ "Your default wallet private key mnemonic is:": "Your default wallet private key mnemonic is:", "Select Wallet": "Select Wallet", "Theme Color:": "Theme Color:", + "Click to try an another server...": "Click to try an another server...", "Theme Style": "Theme Style", "Show password": "Show password", "Balance: N/A": "Balance: N/A",