progress bar

This commit is contained in:
Tiago Batista Cardoso
2026-01-25 00:54:54 +01:00
parent 61edd8cd24
commit 2283ef5f33

View File

@@ -5,8 +5,9 @@ use client_network::{
}; };
use crossbeam_channel::{Receiver, Sender}; use crossbeam_channel::{Receiver, Sender};
use egui::{ use egui::{
Align, CentralPanel, CollapsingHeader, Color32, Context, CornerRadius, Frame, Layout, Response, Align, CentralPanel, CollapsingHeader, Color32, Context, CornerRadius, Frame, Layout,
ScrollArea, SidePanel, Stroke, TopBottomPanel, Ui, ViewportCommand, ProgressBar, Response, ScrollArea, SidePanel, Stroke, TopBottomPanel, Ui, Vec2,
ViewportCommand,
}; };
use std::collections::HashSet; use std::collections::HashSet;
use std::{collections::HashMap, fmt::format, io::Seek}; use std::{collections::HashMap, fmt::format, io::Seek};
@@ -52,6 +53,13 @@ pub struct P2PClientApp {
current_downloading_file_map: MerkleTree, current_downloading_file_map: MerkleTree,
remaining_chunks: HashSet<[u8; 32]>, remaining_chunks: HashSet<[u8; 32]>,
// total number of chunks expected for the current download (set when download starts)
current_total_chunks: Option<usize>,
// number of chunks received so far (count of removed remaining_chunks)
current_received_chunks: usize,
root_downloading_file: String, root_downloading_file: String,
show_network_window: bool, show_network_window: bool,
show_choose_server_window: bool, show_choose_server_window: bool,
@@ -86,6 +94,8 @@ impl P2PClientApp {
active_server: "".to_string(), active_server: "".to_string(),
shared_tree: generate_base_tree(), shared_tree: generate_base_tree(),
current_downloading_file_map: current_downloading_file_map, current_downloading_file_map: current_downloading_file_map,
current_total_chunks: None,
current_received_chunks: 0,
root_downloading_file: "".to_string(), root_downloading_file: "".to_string(),
remaining_chunks: HashSet::new(), remaining_chunks: HashSet::new(),
show_network_window: false, show_network_window: false,
@@ -104,6 +114,14 @@ impl P2PClientApp {
pub fn clear_success(&mut self) { pub fn clear_success(&mut self) {
self.success_message = None; self.success_message = None;
} }
fn set_current_total_chunks(&mut self, len: Option<usize>) {
self.current_total_chunks = len
}
fn set_current_received_chunks(&mut self, arg: usize) {
self.current_received_chunks = arg
}
} }
// --- eframe::App Trait Implementation --- // --- eframe::App Trait Implementation ---
@@ -293,6 +311,7 @@ impl eframe::App for P2PClientApp {
true, true,
)); ));
self.remaining_chunks.insert(entry); self.remaining_chunks.insert(entry);
self.set_current_total_chunks(Some(self.remaining_chunks.len()));
} }
self.remaining_chunks.remove(&hash); self.remaining_chunks.remove(&hash);
} }
@@ -301,6 +320,13 @@ impl eframe::App for P2PClientApp {
} }
_ => {} _ => {}
} }
if let Some(total) = self.current_total_chunks {
// recompute received (safer than incrementing)
let received = total.saturating_sub(self.remaining_chunks.len());
self.current_received_chunks = received;
}
if self.remaining_chunks.is_empty() { if self.remaining_chunks.is_empty() {
/*let file = OpenOptions::new() /*let file = OpenOptions::new()
.append(true) .append(true)
@@ -325,6 +351,8 @@ impl eframe::App for P2PClientApp {
} }
} }
}*/ }*/
self.current_total_chunks = None;
self.current_received_chunks = 0;
println!("bigfile téléchargé"); println!("bigfile téléchargé");
} }
} }
@@ -516,26 +544,35 @@ impl eframe::App for P2PClientApp {
TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| { TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
match self.server_status { match self.server_status {
ServerStatus::Loading => { ServerStatus::Loading => ui.spinner(),
ui.spinner(); ServerStatus::Connected => ui.label("Registered but no server peer chosen..."),
} ServerStatus::NotConnected => ui.label("No connection.."),
ServerStatus::Connected => { ServerStatus::ConnectedHandshake => ui.label("📡"),
ui.label("Registered but no server peer chosen..."); };
}
ServerStatus::NotConnected => { ui.add_space(8.0); // small gap
ui.label("No connection..");
} // desired progress bar width
ServerStatus::ConnectedHandshake => { let bar_width = 220.0f32;
let str = format!("📡"); // push it to the right by adding space equal to remaining width minus bar width
ui.label(str); let push = (ui.available_width() - bar_width).max(0.0);
} ui.add_space(push);
if let Some(total) = self.current_total_chunks {
let received = self.current_received_chunks;
let frac = if total == 0 {
1.0
} else {
received as f32 / total as f32
};
ui.add(
ProgressBar::new(frac)
.show_percentage()
.animate(true)
.desired_height(10.0),
);
} }
ui.add_space(ui.available_width() - 30.0);
// formater mm:ss
let secs = self.remaining.as_secs();
let minutes = secs / 60;
let seconds = secs % 60;
ui.label(format!("{:02}:{:02}", minutes, seconds));
}); });
}); });
@@ -798,7 +835,7 @@ impl eframe::App for P2PClientApp {
ui.separator(); ui.separator();
if let Some(active_peer) = &self.active_peer { if let Some(active_peer) = &self.active_peer {
if let Some(tree) = self.loaded_fs.get(active_peer) { if let Some(tree) = self.loaded_fs.clone().get(active_peer) {
ScrollArea::vertical().show(ui, |ui| { ScrollArea::vertical().show(ui, |ui| {
// Start drawing the tree from the root hash // Start drawing the tree from the root hash
self.draw_file_tree(ui, tree); self.draw_file_tree(ui, tree);
@@ -858,7 +895,7 @@ impl eframe::App for P2PClientApp {
// --- Helper for Drawing the Recursive File Tree --- // --- Helper for Drawing the Recursive File Tree ---
impl P2PClientApp { impl P2PClientApp {
fn draw_file_tree(&self, ui: &mut Ui, tree: &MerkleTree) { fn draw_file_tree(&mut self, ui: &mut Ui, tree: &MerkleTree) {
assert!(self.active_peer.is_some()); assert!(self.active_peer.is_some());
assert!( assert!(
self.loaded_fs self.loaded_fs
@@ -875,7 +912,7 @@ impl P2PClientApp {
} }
fn draw_file_node( fn draw_file_node(
&self, &mut self,
ui: &mut Ui, ui: &mut Ui,
to_draw: NodeHash, to_draw: NodeHash,
tree: &MerkleTree, tree: &MerkleTree,