progress bar
This commit is contained in:
@@ -5,8 +5,9 @@ use client_network::{
|
||||
};
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use egui::{
|
||||
Align, CentralPanel, CollapsingHeader, Color32, Context, CornerRadius, Frame, Layout, Response,
|
||||
ScrollArea, SidePanel, Stroke, TopBottomPanel, Ui, ViewportCommand,
|
||||
Align, CentralPanel, CollapsingHeader, Color32, Context, CornerRadius, Frame, Layout,
|
||||
ProgressBar, Response, ScrollArea, SidePanel, Stroke, TopBottomPanel, Ui, Vec2,
|
||||
ViewportCommand,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use std::{collections::HashMap, fmt::format, io::Seek};
|
||||
@@ -52,6 +53,13 @@ pub struct P2PClientApp {
|
||||
|
||||
current_downloading_file_map: MerkleTree,
|
||||
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,
|
||||
show_network_window: bool,
|
||||
show_choose_server_window: bool,
|
||||
@@ -86,6 +94,8 @@ impl P2PClientApp {
|
||||
active_server: "".to_string(),
|
||||
shared_tree: generate_base_tree(),
|
||||
current_downloading_file_map: current_downloading_file_map,
|
||||
current_total_chunks: None,
|
||||
current_received_chunks: 0,
|
||||
root_downloading_file: "".to_string(),
|
||||
remaining_chunks: HashSet::new(),
|
||||
show_network_window: false,
|
||||
@@ -104,6 +114,14 @@ impl P2PClientApp {
|
||||
pub fn clear_success(&mut self) {
|
||||
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 ---
|
||||
@@ -293,6 +311,7 @@ impl eframe::App for P2PClientApp {
|
||||
true,
|
||||
));
|
||||
self.remaining_chunks.insert(entry);
|
||||
self.set_current_total_chunks(Some(self.remaining_chunks.len()));
|
||||
}
|
||||
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() {
|
||||
/*let file = OpenOptions::new()
|
||||
.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é");
|
||||
}
|
||||
}
|
||||
@@ -516,26 +544,35 @@ impl eframe::App for P2PClientApp {
|
||||
TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
match self.server_status {
|
||||
ServerStatus::Loading => {
|
||||
ui.spinner();
|
||||
ServerStatus::Loading => ui.spinner(),
|
||||
ServerStatus::Connected => ui.label("Registered but no server peer chosen..."),
|
||||
ServerStatus::NotConnected => ui.label("No connection.."),
|
||||
ServerStatus::ConnectedHandshake => ui.label("📡"),
|
||||
};
|
||||
|
||||
ui.add_space(8.0); // small gap
|
||||
|
||||
// desired progress bar width
|
||||
let bar_width = 220.0f32;
|
||||
// push it to the right by adding space equal to remaining width minus bar width
|
||||
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),
|
||||
);
|
||||
}
|
||||
ServerStatus::Connected => {
|
||||
ui.label("Registered but no server peer chosen...");
|
||||
}
|
||||
ServerStatus::NotConnected => {
|
||||
ui.label("No connection..");
|
||||
}
|
||||
ServerStatus::ConnectedHandshake => {
|
||||
let str = format!("📡");
|
||||
ui.label(str);
|
||||
}
|
||||
}
|
||||
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();
|
||||
|
||||
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| {
|
||||
// Start drawing the tree from the root hash
|
||||
self.draw_file_tree(ui, tree);
|
||||
@@ -858,7 +895,7 @@ impl eframe::App for P2PClientApp {
|
||||
// --- Helper for Drawing the Recursive File Tree ---
|
||||
|
||||
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.loaded_fs
|
||||
@@ -875,7 +912,7 @@ impl P2PClientApp {
|
||||
}
|
||||
|
||||
fn draw_file_node(
|
||||
&self,
|
||||
&mut self,
|
||||
ui: &mut Ui,
|
||||
to_draw: NodeHash,
|
||||
tree: &MerkleTree,
|
||||
|
||||
Reference in New Issue
Block a user