Generate time remaining text for fraction complete progress bars

As described in the previous commit "Clear progress bar text when
starting the bar (#230)" progress bar data is either reporting bytes
copied or fraction complete.  The bytes copied case gets in progress
text like this:
    544.00 MiB of 1.00 GiB copied (00:00:11 remaining)

But the fraction complete gets no text.

Now also generate time remaining text for progress bars only reporting
fraction complete.  As with the bytes copied text only add the time
remaining estimate after 5 seconds have passed.  Looks like:
    (00:01:59 remaining)

This is most useful for NTFS partition copy and resize operations which
can take a while depending on the amount of data involved.
This commit is contained in:
Mike Fleetwood 2023-06-12 22:36:12 +01:00 committed by Curtis Gedak
parent 1718c5d2fb
commit 3cbedad693
3 changed files with 27 additions and 8 deletions

View File

@ -70,7 +70,7 @@ public:
std::vector<OperationDetail*> & get_childs() ;
const std::vector<OperationDetail*> & get_childs() const ;
OperationDetail & get_last_child() ;
void run_progressbar( double progress, double target, ProgressBar_Text text_mode = PROGRESSBAR_TEXT_NONE );
void run_progressbar(double progress, double target, ProgressBar_Text text_mode = PROGRESSBAR_TEXT_TIME_REMAINING);
void stop_progressbar();
sigc::signal< void, const OperationDetail & > signal_update ;

View File

@ -26,7 +26,7 @@ namespace GParted
enum ProgressBar_Text
{
PROGRESSBAR_TEXT_NONE,
PROGRESSBAR_TEXT_TIME_REMAINING,
PROGRESSBAR_TEXT_COPY_BYTES
};
@ -36,7 +36,7 @@ public:
ProgressBar();
~ProgressBar();
void start( double target, ProgressBar_Text text_mode = PROGRESSBAR_TEXT_NONE );
void start(double target, ProgressBar_Text text_mode = PROGRESSBAR_TEXT_TIME_REMAINING);
void update( double progress );
void stop();
bool running() const;
@ -53,8 +53,8 @@ private:
double m_target; // Progress bar target should be > 0.0
double m_progress; // Should be 0.0 <= m_progress <= m_target
double m_fraction; // Always between 0.0 and 1.0 for passing to Gtk::ProgressBar.set_fraction()
ProgressBar_Text m_text_mode; // Whether to and style of text generation
Glib::ustring m_text; // Optional text for passing to Gtk::ProgressBar.set_text()
ProgressBar_Text m_text_mode; // Style of text generation
Glib::ustring m_text; // Text for passing to Gtk::ProgressBar.set_text()
Glib::Timer m_timer; // Measures elapsed time to the microsecond for accurate estimation
};

View File

@ -23,7 +23,7 @@ namespace GParted
{
ProgressBar::ProgressBar() : m_running( false ), m_target( 1.0 ), m_progress( 0.0 ), m_fraction( 0.0 ),
m_text_mode( PROGRESSBAR_TEXT_NONE )
m_text_mode(PROGRESSBAR_TEXT_TIME_REMAINING)
{
}
@ -84,14 +84,14 @@ void ProgressBar::do_update()
else if ( m_fraction > 1.0 )
m_fraction = 1.0;
double elapsed = m_timer.elapsed();
if ( m_text_mode == PROGRESSBAR_TEXT_COPY_BYTES )
{
double elapsed = m_timer.elapsed();
if ( m_running && elapsed >= 5.0 )
{
/* Only show "(00:01:59 remaining)" when a progress bar has been
* running for at least 5 seconds to allow the data copying rate
* to settle down a little before estimating the remaining time.
* to settle down a little before estimating the time remaining.
*/
std::time_t remaining = Utils::round( (m_target - m_progress) /
(m_progress / elapsed) );
@ -109,6 +109,25 @@ void ProgressBar::do_update()
Utils::format_size( m_target, 1 ) );
}
}
else /* (m_text_mode == PROGRESSBAR_TEXT_TIME_REMAINING) */
{
if (m_running && elapsed >= 5.0)
{
/* Only show "(00:01:59 remaining)" when a progress bar has been
* running for at least 5 seconds to allow the progress rate to
* settle down a little before estimating the time remaining.
*/
std::time_t remaining = Utils::round((m_target - m_progress) /
(m_progress / elapsed) );
m_text = Glib::ustring::compose( /* TO TRANSLATORS: looks like (00:01:59 remaining) */
_("(%1 remaining)"),
Utils::format_time(remaining));
}
else
{
m_text.clear();
}
}
}
}//GParted