Skip to content

Commit

Permalink
Copy progress is a real dialog now. Yay.
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Jul 25, 2021
1 parent 6ef88ba commit 8f4f32c
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 108 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 25.07.2021 19:40:43 +0100 - build 5855

1. Copy progress is a real dialog now. Yay.

--------------------------------------------------------------------------------
drkns 23.07.2021 01:41:38 +0100 - build 5854

Expand Down
53 changes: 27 additions & 26 deletions far/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ class ShellCopy : noncopyable
void SetSecurity(const string& FileName, const os::security::descriptor& sd);
void ResetSecurity(const string& FileName);

// called by copy_selected_items
void CalcTotalSize() const;
std::pair<unsigned long long, unsigned long long> CalcTotalSize() const;

void ShellSetAttr(const string& Dest, os::fs::attributes Attr);
void SetDestDizPath(const string& DestPath);
Expand Down Expand Up @@ -206,7 +205,6 @@ class ShellCopy : noncopyable
bool FilesPresent{};
bool AskRO{};
bool m_UseFilter{};
bool m_TotalSizeCalculated{};
HANDLE m_FileHandleForStreamSizeFix{};
size_t m_NumberOfTargets{};
std::list<created_folders> m_CreatedFolders;
Expand Down Expand Up @@ -1297,17 +1295,30 @@ ShellCopy::ShellCopy(
}

if (!CP)
CP = std::make_unique<copy_progress>(Move, ShowTotalCopySize, ShowCopyTime);
{
unsigned long long
TotalFiles{},
TotalBytes{};

CP->reset_current();
if (SelCount != 1 || FolderPresent)
{
// Не сканируем каталоги при создании линков
if (ShowTotalCopySize && !(Flags & FCOPY_LINK))
std::tie(TotalFiles, TotalBytes) = CalcTotalSize();
}
else
{
TotalFiles = 1;
TotalBytes = SingleSelectedFileSize;
}

if (SelCount == 1 && !FolderPresent)
{
CP->set_total_files(1);
CP->set_total_bytes(SingleSelectedFileSize);
CP->set_current_total(SingleSelectedFileSize);
CP = std::make_unique<copy_progress>(Move, ShowTotalCopySize, ShowCopyTime);
CP->set_total_files(TotalFiles);
CP->set_total_bytes(TotalBytes);
}

CP->reset_current();

// Обнулим инфу про дизы
strDestDizPath.clear();
Flags&=~FCOPY_DIZREAD;
Expand Down Expand Up @@ -1367,19 +1378,6 @@ void ShellCopy::copy_selected_items(const string& Dest)
const auto move_rename = (0 != (Flags & FCOPY_MOVE));
bool SameDisk = false;

if (!m_TotalSizeCalculated)
{
// ! Не сканируем каталоги при создании линков
if (CP->IsTotalVisible() && !(Flags & FCOPY_LINK))
CalcTotalSize();

m_TotalSizeCalculated = true;
}
else
{
CP->reset_current();
}

// Основной цикл копирования одной порции.
//
for (const auto& i: SrcPanel->enum_selected())
Expand Down Expand Up @@ -3332,7 +3330,7 @@ DWORD ShellCopy::CopyProgressRoutine(unsigned long long TotalFileSize, unsigned
return Abort?PROGRESS_CANCEL:PROGRESS_CONTINUE;
}

void ShellCopy::CalcTotalSize() const
std::pair<unsigned long long, unsigned long long> ShellCopy::CalcTotalSize() const
{
unsigned long long Files{}, Bytes{};

Expand Down Expand Up @@ -3373,8 +3371,11 @@ void ShellCopy::CalcTotalSize() const
}

// INFO: Это для варианта, когда "ВСЕГО = общий размер * количество целей"
CP->set_total_files(Files * m_NumberOfTargets);
CP->set_total_bytes(Bytes * m_NumberOfTargets);
return
{
Files * m_NumberOfTargets,
Bytes * m_NumberOfTargets
};
}

/*
Expand Down
170 changes: 115 additions & 55 deletions far/copy_progress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "copy_progress.hpp"

// Internal:
#include "colormix.hpp"
#include "lang.hpp"
#include "config.hpp"
#include "keyboard.hpp"
Expand All @@ -44,8 +43,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "strmix.hpp"
#include "interf.hpp"
#include "message.hpp"
#include "scrbuf.hpp"
#include "global.hpp"
#include "dialog.hpp"

// Platform:

Expand All @@ -59,21 +57,105 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* Общее время ожидания пользователя */
extern std::chrono::steady_clock::duration WaitUserTime;

namespace
{
enum
{
DlgW = 76,
DlgH = 15,
};

enum progress_items
{
pr_doublebox,
pr_src_label,
pr_src_name,
pr_dst_label,
pr_dst_name,
pr_current_progress,
pr_separator1,
pr_total_files,
pr_total_bytes,
pr_total_progress,
pr_separator2,
pr_stats,

pr_count
};
}

copy_progress::copy_progress(bool Move, bool Total, bool Time):
m_CurrentBarSize(CanvasWidth()),
m_TotalBarSize(CanvasWidth()),
m_Move(Move),
m_Total(Total),
m_ShowTime(Time),
m_Color(colors::PaletteColorToFarColor(COL_DIALOGTEXT)),
m_TimeCheck(time_check::mode::immediate, GetRedrawTimeout()),
m_SpeedUpdateCheck(time_check::mode::immediate, 3s)
{
auto ProgressDlgItems = MakeDialogItems<progress_items::pr_count>(
{
{ DI_DOUBLEBOX, {{ 3, 1 }, { DlgW - 4, DlgH - 2 }}, DIF_NONE, msg(m_Move? lng::MMoveDlgTitle : lng::MCopyDlgTitle), },
{ DI_TEXT, {{ 5, 2 }, { DlgW - 6, 2 }}, DIF_NONE, msg(m_Move? lng::MCopyMoving :lng::MCopyCopying), },
{ DI_TEXT, {{ 5, 3 }, { DlgW - 6, 3 }}, DIF_NONE, {}, },
{ DI_TEXT, {{ 5, 4 }, { DlgW - 6, 4 }}, DIF_NONE, msg(lng::MCopyTo), },
{ DI_TEXT, {{ 5, 5 }, { DlgW - 6, 5 }}, DIF_NONE, {}, },
{ DI_TEXT, {{ 5, 6 }, { DlgW - 6, 6 }}, DIF_NONE, make_progressbar(CanvasWidth(), 0, true, false) },
{ DI_TEXT, {{-1, 7 }, { DlgW - 6, 7 }}, DIF_SEPARATOR, msg(lng::MCopyDlgTotal), },
{ DI_TEXT, {{ 5, 8 }, { DlgW - 6, 8 }}, DIF_NONE, {}, },
{ DI_TEXT, {{ 5, 9 }, { DlgW - 6, 9 }}, DIF_NONE, {}, },
{ DI_TEXT, {{ 5, 10}, { DlgW - 6, 10}}, DIF_NONE, make_progressbar(CanvasWidth(), 0, true, false), },
{ DI_TEXT, {{-1, 11}, { DlgW - 6, 11}}, DIF_SEPARATOR, {}, },
{ DI_TEXT, {{ 5, 12}, { DlgW - 6, 12}}, DIF_NONE, {}, },
});

if (!m_Total)
{
ProgressDlgItems[progress_items::pr_total_progress].Flags |= DIF_HIDDEN;

for (size_t i = progress_items::pr_total_progress + 1; i != progress_items::pr_count; ++i)
{
--ProgressDlgItems[i].Y1;
--ProgressDlgItems[i].Y2;
}

--ProgressDlgItems[progress_items::pr_doublebox].Y2;
}

if (!m_ShowTime)
{
ProgressDlgItems[progress_items::pr_separator2].Flags |= DIF_HIDDEN;
ProgressDlgItems[progress_items::pr_stats].Flags |= DIF_HIDDEN;
ProgressDlgItems[progress_items::pr_doublebox].Y2 -= 2;
}

m_Dialog = Dialog::create(ProgressDlgItems, [](Dialog* const Dlg, intptr_t const Msg, intptr_t const Param1, void* const Param2)
{
if (Msg == DN_RESIZECONSOLE)
{
COORD Position{ -1, -1 };
Dlg->SendMessage(DM_MOVEDIALOG, 1, &Position);
}

return Dlg->DefProc(Msg, Param1, Param2);
});

const int DialogHeight = ProgressDlgItems[progress_items::pr_doublebox].Y2 - ProgressDlgItems[progress_items::pr_doublebox].Y1 + 1 + 2;
m_Dialog->SetPosition({ -1, -1, DlgW, DialogHeight });
m_Dialog->SetCanLoseFocus(true);
m_Dialog->Process();

}

copy_progress::~copy_progress()
{
if (m_Dialog)
m_Dialog->CloseDialog();
}

size_t copy_progress::CanvasWidth()
{
return 52;
return DlgW - 10;
}

void copy_progress::skip()
Expand Down Expand Up @@ -150,27 +232,42 @@ void copy_progress::Flush()
if (!m_TimeCheck || CheckEsc())
return;

CreateBackground();
SCOPED_ACTION(Dialog::suppress_redraw)(m_Dialog.get());

m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_src_name, UNSAFE_CSTR(m_Src));
m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_dst_name, UNSAFE_CSTR(m_Dst));

const auto CurrentProgress = make_progressbar(m_CurrentBarSize, m_CurrentPercent, true, !m_Total);
m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_current_progress, UNSAFE_CSTR(CurrentProgress));

Text({ m_Rect.left + 5, m_Rect.top + 3 }, m_Color, m_Src);
Text({ m_Rect.left + 5, m_Rect.top + 5 }, m_Color, m_Dst);
Text({ m_Rect.left + 5, m_Rect.top + 8 }, m_Color, m_FilesCopied);
m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_total_files, UNSAFE_CSTR(m_FilesCopied));

const auto Result = FormatCounter(lng::MCopyBytesTotalInfo, lng::MCopyFilesTotalInfo, m_BytesTotal.Copied, m_BytesTotal.Total, m_Total, CanvasWidth() - 5);
Text({ m_Rect.left + 5, m_Rect.top + 9 }, m_Color, Result);
m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_total_bytes, UNSAFE_CSTR(Result));

if (!m_Time.empty())
if (m_Total)
{
const size_t Width = m_Rect.width() - 10;
const auto XPos = m_Rect.left + 5;
const auto YPos = m_Rect.top + (m_Total? 12 : 11);
const auto TotalProgress = make_progressbar(m_TotalBarSize, m_TotalPercent, true, true);
m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_total_progress, UNSAFE_CSTR(TotalProgress));
}

if (!m_Time.empty())
{
const auto Width = CanvasWidth();
const auto ConsumedSpace = m_Time.size() + 1 + m_TimeLeft.size() + 1 + m_Speed.size();
const auto FillerWidth = ConsumedSpace >= Width? 0 : (Width - ConsumedSpace) / 2;
const auto RemainingSpace = ConsumedSpace > Width? 0 : Width - ConsumedSpace;
const auto FirstFillerWidth = RemainingSpace / 2;
const auto SecondFillerWidth = RemainingSpace - FirstFillerWidth;

const auto Stat = cut_right(
concat(
fit_to_left(m_Time, m_Time.size() + 1 + FirstFillerWidth),
fit_to_left(m_TimeLeft, m_TimeLeft.size() + 1 + SecondFillerWidth),
m_Speed),
Width
);

Text({ XPos, YPos }, m_Color, m_Time);
Text({ static_cast<int>(XPos + m_Time.size() + 1 + FillerWidth), YPos }, m_Color, m_TimeLeft);
Text({ static_cast<int>(m_Rect.right + 1 - 5 - m_Speed.size()), YPos }, m_Color, m_Speed);
m_Dialog->SendMessage(DM_SETTEXTPTR, progress_items::pr_stats, UNSAFE_CSTR(Stat));
}

if (m_Total || (m_Files.Total == 1))
Expand All @@ -180,8 +277,6 @@ void copy_progress::Flush()
msg(m_Move? lng::MCopyMovingTitle : lng::MCopyCopyingTitle))
);
}

Global->ScrBuf->Flush();
}

void copy_progress::reset_current()
Expand Down Expand Up @@ -235,41 +330,6 @@ void copy_progress::add_total_bytes(unsigned long long const Value)
m_BytesTotal.Total += Value;
}

void copy_progress::CreateBackground()
{
const auto& Title = msg(m_Move? lng::MMoveDlgTitle : lng::MCopyDlgTitle);

std::vector<string> Items =
{
msg(m_Move? lng::MCopyMoving :lng::MCopyCopying),
{}, // source name
msg(lng::MCopyTo),
{}, // dest path
make_progressbar(m_CurrentBarSize, m_CurrentPercent, true, !m_Total),
L'\x1' + msg(lng::MCopyDlgTotal),
{}, // files [total] <processed>
{} // bytes [total] <processed>
};

// total progress bar
if (m_Total)
{
Items.emplace_back(make_progressbar(m_TotalBarSize, m_TotalPercent, true, true));
}

// time & speed
if (m_ShowTime)
{
Items.emplace_back(L"\x1"sv);
Items.emplace_back();
}

m_Rect = Message(MSG_LEFTALIGN | MSG_NOFLUSH,
Title,
std::move(Items),
{}).GetPosition();
}

void copy_progress::SetNames(const string& Src, const string& Dst)
{
if (m_ShowTime && !m_Files.Copied)
Expand Down
7 changes: 4 additions & 3 deletions far/copy_progress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "wakeful.hpp"
#include "datetime.hpp"
#include "plugin.hpp"
#include "windowsfwd.hpp"

// Platform:

Expand All @@ -53,6 +54,7 @@ class copy_progress: noncopyable
{
public:
copy_progress(bool Move, bool Total, bool Time);
~copy_progress();

bool IsCancelled() const { return m_IsCancelled; }
bool IsTotalVisible() const { return m_Total; }
Expand Down Expand Up @@ -80,15 +82,13 @@ class copy_progress: noncopyable
private:
bool CheckEsc();
void Flush();
void CreateBackground();
void SetCurrentProgress(unsigned long long CompletedSize, unsigned long long TotalSize);
void SetTotalProgress(unsigned long long CompletedSize, unsigned long long TotalSize);
void UpdateTime(unsigned long long SizeDone, unsigned long long SizeToGo);

std::chrono::steady_clock::time_point m_CopyStartTime;
taskbar::indeterminate m_TB;
wakeful m_Wakeful;
small_rectangle m_Rect{};

size_t m_CurrentBarSize;
int m_CurrentPercent{};
Expand All @@ -100,7 +100,6 @@ class copy_progress: noncopyable
bool m_Total;
bool m_ShowTime;
bool m_IsCancelled{};
FarColor m_Color;
time_check m_TimeCheck;
time_check m_SpeedUpdateCheck;
string m_Src, m_Dst;
Expand All @@ -125,6 +124,8 @@ class copy_progress: noncopyable
unsigned long long Total{};
}
m_BytesCurrent, m_BytesTotal;

dialog_ptr m_Dialog;
};

#endif // COPY_PROGRESS_HPP_3D1EAAD8_8353_459C_8826_33AAAE06D01F
Loading

0 comments on commit 8f4f32c

Please sign in to comment.