-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathuifilelistdialog.cpp
115 lines (100 loc) · 2.52 KB
/
uifilelistdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// uifilelistdialog.cpp
//
// Copyright (c) 2019-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
#include "uifilelistdialog.h"
#include "fileutil.h"
#include "strutil.h"
UiFileListDialog::UiFileListDialog(const UiDialogParams& p_Params, const std::string& p_CurrentDir)
: UiListDialog(p_Params, true /*p_ShadeHidden*/)
, m_CurrentDir(p_CurrentDir)
{
m_DirEntrys = FileUtil::ListPaths(m_CurrentDir);
UpdateList();
}
UiFileListDialog::~UiFileListDialog()
{
}
std::string UiFileListDialog::GetCurrentDir()
{
return m_CurrentDir;
}
std::string UiFileListDialog::GetSelectedPath()
{
return m_SelectedPath;
}
void UiFileListDialog::OnSelect()
{
if (m_CurrentDirEntrys.size() == 0) return;
if (m_Index < (int)m_CurrentDirEntrys.size())
{
const DirEntry& dirEntry = *std::next(m_CurrentDirEntrys.begin(), m_Index);
if (dirEntry.IsDir())
{
m_CurrentDir = FileUtil::AbsolutePath(m_CurrentDir + "/" + dirEntry.name);
m_DirEntrys = FileUtil::ListPaths(m_CurrentDir);
m_FilterStr.clear();
UpdateList();
UpdateFooter();
}
else
{
m_SelectedPath = FileUtil::AbsolutePath(m_CurrentDir + "/" + dirEntry.name);
m_Result = true;
m_Running = false;
}
}
}
void UiFileListDialog::OnBack()
{
m_CurrentDir = FileUtil::AbsolutePath(m_CurrentDir + "/..");
m_DirEntrys = FileUtil::ListPaths(m_CurrentDir);
m_FilterStr.clear();
UpdateList();
UpdateFooter();
}
bool UiFileListDialog::OnTimer()
{
return false;
}
void UiFileListDialog::UpdateList()
{
if (m_FilterStr.empty())
{
m_CurrentDirEntrys = m_DirEntrys;
}
else
{
m_CurrentDirEntrys.clear();
for (const auto& dirEntry : m_DirEntrys)
{
if (StrUtil::ToLower(dirEntry.name).find(StrUtil::ToLower(StrUtil::ToString(m_FilterStr))) != std::string::npos)
{
m_CurrentDirEntrys.insert(dirEntry);
}
}
}
int maxNameLen = m_W - 9;
m_Items.clear();
for (auto& dirEntry : m_CurrentDirEntrys)
{
std::wstring name = StrUtil::ToWString(dirEntry.name);
name = StrUtil::TrimPadWString(name.substr(0, maxNameLen), maxNameLen);
// add 9 chars for dir / filesize
if (dirEntry.IsDir())
{
name += L" (dir)";
}
else
{
// max 7 - ex: "1234 KB"
std::string size = FileUtil::GetSuffixedSize(dirEntry.size);
size = std::string(7 - size.size(), ' ') + size;
name += L" " + StrUtil::ToWString(size);
}
m_Items.push_back(name);
}
m_Index = 0;
}