/
ExplorerItem.cpp
186 lines (160 loc) · 4.88 KB
/
ExplorerItem.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "pch.h"
#include "ExplorerItem.h"
#if __has_include("ExplorerItem.g.cpp")
#include "ExplorerItem.g.cpp"
#endif
using namespace winrt;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::Windows::ApplicationModel::Resources;
namespace
{
const wchar_t fileImagePath[] = L"ms-appx:///Assets/file.png";
const wchar_t folderImagePath[] = L"ms-appx:///Assets/folder.png";
std::wstring PowerRenameItemRenameStatusToString(PowerRenameItemRenameStatus status)
{
switch (status)
{
case PowerRenameItemRenameStatus::Init:
{
return L"Normal";
}
case PowerRenameItemRenameStatus::ShouldRename:
{
return L"Highlight";
}
case PowerRenameItemRenameStatus::ItemNameInvalidChar:
{
return L"Error";
}
case PowerRenameItemRenameStatus::ItemNameTooLong:
{
return L"Error";
}
default:
return L"Normal";
}
}
}
namespace winrt::PowerRenameUI::implementation
{
ExplorerItem::ExplorerItem(int32_t id, hstring const& original, hstring const& renamed, int32_t type, uint32_t depth, bool checked) :
m_id{ id }, m_idStr{ std::to_wstring(id) }, m_original{ original }, m_renamed{ renamed }, m_type{ type }, m_depth{ depth }, m_checked{ checked }
{
m_imagePath = (m_type == static_cast<UINT>(ExplorerItemType::Folder)) ? folderImagePath : fileImagePath;
}
int32_t ExplorerItem::Id()
{
return m_id;
}
hstring ExplorerItem::IdStr()
{
return m_idStr;
}
hstring ExplorerItem::Original()
{
return m_original;
}
void ExplorerItem::Original(hstring const& value)
{
if (m_original != value)
{
m_original = value;
m_propertyChanged(*this, Microsoft::UI::Xaml::Data::PropertyChangedEventArgs{ L"Original" });
}
}
hstring ExplorerItem::Renamed()
{
return m_renamed;
}
void ExplorerItem::Renamed(hstring const& value)
{
if (m_renamed != value)
{
m_renamed = value;
m_propertyChanged(*this, Microsoft::UI::Xaml::Data::PropertyChangedEventArgs{ L"Renamed" });
}
}
double ExplorerItem::Indentation()
{
return static_cast<double>(m_depth) * 12;
}
hstring ExplorerItem::ImagePath()
{
return m_imagePath;
}
int32_t ExplorerItem::Type()
{
return m_type;
}
void ExplorerItem::Type(int32_t value)
{
if (m_type != value)
{
m_type = value;
m_propertyChanged(*this, Microsoft::UI::Xaml::Data::PropertyChangedEventArgs{ L"Type" });
}
}
bool ExplorerItem::Checked()
{
return m_checked;
}
void ExplorerItem::Checked(bool value)
{
if (m_checked != value)
{
m_checked = value;
m_propertyChanged(*this, Microsoft::UI::Xaml::Data::PropertyChangedEventArgs{ L"Checked" });
if (m_checked && !m_renamed.empty())
{
VisualStateManager::GoToState(*this, PowerRenameItemRenameStatusToString(m_state), false);
}
else
{
VisualStateManager::GoToState(*this, L"Normal", false);
}
}
}
int32_t ExplorerItem::State()
{
return static_cast<int32_t>(m_state);
}
void ExplorerItem::State(int32_t value)
{
m_state = static_cast<PowerRenameItemRenameStatus>(value);
ErrorMessageTxt().Text(StateToErrorMessage());
if (m_renamed == L"")
{
VisualStateManager::GoToState(*this, L"Normal", false);
}
else
{
VisualStateManager::GoToState(*this, PowerRenameItemRenameStatusToString(m_state), false);
}
}
winrt::event_token ExplorerItem::PropertyChanged(winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return m_propertyChanged.add(handler);
}
void ExplorerItem::PropertyChanged(winrt::event_token const& token) noexcept
{
m_propertyChanged.remove(token);
}
std::wstring ExplorerItem::StateToErrorMessage()
{
auto factory = winrt::get_activation_factory<ResourceManager, IResourceManagerFactory>();
ResourceManager manager = factory.CreateInstance(L"resources.pri");
switch (m_state)
{
case PowerRenameItemRenameStatus::ItemNameInvalidChar:
{
return std::wstring{ manager.MainResourceMap().GetValue(L"Resources/ErrorMessage_InvalidChar").ValueAsString() };
}
case PowerRenameItemRenameStatus::ItemNameTooLong:
{
return std::wstring{ manager.MainResourceMap().GetValue(L"Resources/ErrorMessage_FileNameTooLong").ValueAsString() };
}
default:
return {};
}
}
}