Skip to content

Commit

Permalink
[PowerRename] Tweak UI and fix performance issues (#14365)
Browse files Browse the repository at this point in the history
* Init

* Update MainWindow.xaml

* Add identation

* Remove template selector

* Vertical UI

* Update PowerRenameUILib.vcxproj

* Revert "Vertical UI"

This reverts commit d0b3d26.

* Revert "Update PowerRenameUILib.vcxproj"

This reverts commit ba18503.

* Tweaks to margins

* Updated tweaks

* Update MainWindow.xaml

* Wire counters

* Improve perf: Constant O(1) find-item-by-id time instead of O(n)

Co-authored-by: Laute <Niels.Laute@philips.com>
  • Loading branch information
stefansjfw and Laute committed Nov 17, 2021
1 parent c934127 commit 5a4822f
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 349 deletions.
31 changes: 7 additions & 24 deletions src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ bool AppWindow::OnCreate(HWND, LPCREATESTRUCT) noexcept
try
{
PopulateExplorerItems();
UpdateCounts();
SetHandlers();
ReadSettings();
}
Expand Down Expand Up @@ -246,11 +247,6 @@ void AppWindow::PopulateExplorerItems()
m_prManager->GetVisibleItemCount(&count);
Logger::debug(L"Number of visible items: {}", count);

UINT currDepth = 0;
std::stack<UINT> parents{};
UINT prevId = 0;
parents.push(0);

for (UINT i = 0; i < count; ++i)
{
CComPtr<IPowerRenameItem> renameItem;
Expand All @@ -274,23 +270,8 @@ void AppWindow::PopulateExplorerItems()
bool isSubFolderContent = false;
winrt::check_hresult(renameItem->GetIsFolder(&isFolder));

if (depth > currDepth)
{
parents.push(prevId);
currDepth = depth;
}
else
{
while (currDepth > depth)
{
parents.pop();
currDepth--;
}
currDepth = depth;
}
m_mainUserControl.AddExplorerItem(
id, originalName, newName == nullptr ? hstring{} : hstring{ newName }, isFolder ? 0 : 1, parents.top(), selected);
prevId = id;
id, originalName, newName == nullptr ? hstring{} : hstring{ newName }, isFolder ? 0 : 1, depth, selected);
}
}
}
Expand Down Expand Up @@ -638,6 +619,7 @@ void AppWindow::SwitchView()

m_prManager->SwitchFilter(0);
PopulateExplorerItems();
UpdateCounts();
}

void AppWindow::Rename(bool closeWindow)
Expand Down Expand Up @@ -851,11 +833,12 @@ void AppWindow::UpdateCounts()
m_selectedCount = selectedCount;
m_renamingCount = renamingCount;

// Update counts UI elements if/when added

// Update Rename button state
m_mainUserControl.UIUpdatesItem().ButtonRenameEnabled(renamingCount > 0);
}

m_mainUserControl.UIUpdatesItem().OriginalCount(std::to_wstring(m_mainUserControl.ExplorerItems().Size()));
m_mainUserControl.UIUpdatesItem().RenamedCount(std::to_wstring(m_renamingCount));
}

HRESULT AppWindow::OnItemAdded(_In_ IPowerRenameItem* renameItem)
Expand All @@ -878,7 +861,6 @@ HRESULT AppWindow::OnUpdate(_In_ IPowerRenameItem* renameItem)
}
}

UpdateCounts();
return S_OK;
}

Expand Down Expand Up @@ -935,6 +917,7 @@ HRESULT AppWindow::OnRegExCompleted(_In_ DWORD threadId)
}
}

UpdateCounts();
return S_OK;
}

Expand Down
37 changes: 17 additions & 20 deletions src/modules/powerrename/PowerRenameUILib/ExplorerItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#include "ExplorerItem.h"
#include "ExplorerItem.g.cpp"

namespace {
const wchar_t fileImagePath[] = L"ms-appx:///Assets/file.png";
const wchar_t folderImagePath[] = L"ms-appx:///Assets/folder.png";
}

namespace winrt::PowerRenameUILib::implementation
{
ExplorerItem::ExplorerItem(int32_t id, hstring const& original, hstring const& renamed, int32_t type, bool checked) :
m_id{ id }, m_idStr{ std::to_wstring(id) }, m_original{ original }, m_renamed{ renamed }, m_type{ type }, m_checked{ checked }
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 }
{
if (m_type == static_cast<UINT>(ExplorerItemType::Folder))
{
m_children = winrt::single_threaded_observable_vector<PowerRenameUILib::ExplorerItem>();
}
m_imagePath = (m_type == static_cast<UINT>(ExplorerItemType::Folder)) ? folderImagePath : fileImagePath;
}

int32_t ExplorerItem::Id()
Expand Down Expand Up @@ -51,6 +53,15 @@ namespace winrt::PowerRenameUILib::implementation
}
}

double ExplorerItem::Indentation() {
return static_cast<double>(m_depth) * 12;
}

hstring ExplorerItem::ImagePath()
{
return m_imagePath;
}

int32_t ExplorerItem::Type()
{
return m_type;
Expand Down Expand Up @@ -79,20 +90,6 @@ namespace winrt::PowerRenameUILib::implementation
}
}

winrt::Windows::Foundation::Collections::IObservableVector<winrt::PowerRenameUILib::ExplorerItem> ExplorerItem::Children()
{
return m_children;
}

void ExplorerItem::Children(Windows::Foundation::Collections::IObservableVector<PowerRenameUILib::ExplorerItem> const& value)
{
if (m_children != value)
{
m_children = value;
m_propertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"Children" });
}
}

winrt::event_token ExplorerItem::PropertyChanged(winrt::Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
{
return m_propertyChanged.add(handler);
Expand Down
8 changes: 6 additions & 2 deletions src/modules/powerrename/PowerRenameUILib/ExplorerItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace winrt::PowerRenameUILib::implementation
File = 1
};

ExplorerItem() = delete;
ExplorerItem() = default;

ExplorerItem(int32_t id, hstring const& original, hstring const& renamed, int32_t type, bool checked);
ExplorerItem(int32_t id, hstring const& original, hstring const& renamed, int32_t type, uint32_t depth, bool checked);
int32_t Id();
hstring IdStr();
hstring Original();
void Original(hstring const& value);
hstring Renamed();
void Renamed(hstring const& value);
double Indentation();
hstring ImagePath();
int32_t Type();
void Type(int32_t value);
bool Checked();
Expand All @@ -34,6 +36,8 @@ namespace winrt::PowerRenameUILib::implementation
hstring m_idStr;
winrt::hstring m_original;
winrt::hstring m_renamed;
uint32_t m_depth;
hstring m_imagePath;
winrt::Windows::Foundation::Collections::IObservableVector<PowerRenameUILib::ExplorerItem> m_children;
int32_t m_type;
bool m_checked;
Expand Down
6 changes: 4 additions & 2 deletions src/modules/powerrename/PowerRenameUILib/ExplorerItem.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ namespace PowerRenameUILib
{
runtimeclass ExplorerItem : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
ExplorerItem(Int32 id, String original, String renamed, Int32 type, Boolean checked);
ExplorerItem();
ExplorerItem(Int32 id, String original, String renamed, Int32 type, UInt32 depth, Boolean checked);
Int32 Id { get; };
String IdStr { get; };
String Original;
String Renamed;
Double Indentation { get; };
String ImagePath { get; };
Int32 Type;
Boolean Checked;
Windows.Foundation.Collections.IObservableVector<ExplorerItem> Children;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

60 changes: 11 additions & 49 deletions src/modules/powerrename/PowerRenameUILib/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,11 @@ namespace winrt::PowerRenameUILib::implementation
return m_uiUpdatesItem;
}

void MainWindow::AddExplorerItem(int32_t id, hstring const& original, hstring const& renamed, int32_t type, int32_t parentId, bool checked)
void MainWindow::AddExplorerItem(int32_t id, hstring const& original, hstring const& renamed, int32_t type, uint32_t depth, bool checked)
{
auto newItem = winrt::make<PowerRenameUILib::implementation::ExplorerItem>(id, original, renamed, type, checked);
if (parentId == 0)
{
m_explorerItems.Append(newItem);
}
else
{
auto parent = FindById(parentId);
parent.Children().Append(newItem);
}
auto newItem = winrt::make<PowerRenameUILib::implementation::ExplorerItem>(id, original, renamed, type, depth, checked);
m_explorerItems.Append(newItem);
m_explorerItemsMap[id] = newItem;
}

void MainWindow::UpdateExplorerItem(int32_t id, hstring const& newName)
Expand Down Expand Up @@ -208,51 +201,20 @@ namespace winrt::PowerRenameUILib::implementation

PowerRenameUILib::ExplorerItem MainWindow::FindById(int32_t id)
{
auto fakeRoot = winrt::make<PowerRenameUILib::implementation::ExplorerItem>(0, L"Fake", L"", 0, false);
fakeRoot.Children(m_explorerItems);
return FindById(fakeRoot, id);
return m_explorerItemsMap.contains(id) ? m_explorerItemsMap[id] : NULL;
}

PowerRenameUILib::ExplorerItem MainWindow::FindById(PowerRenameUILib::ExplorerItem& root, int32_t id)
void MainWindow::ToggleAll(bool checked)
{
if (root.Id() == id)
return root;

if (root.Type() == static_cast<UINT>(ExplorerItem::ExplorerItemType::Folder))
{
for (auto c : root.Children())
{
auto result = FindById(c, id);
if (result != NULL)
return result;
}
}

return NULL;
}

void MainWindow::ToggleAll(PowerRenameUILib::ExplorerItem node, bool checked)
{
if (node == NULL)
return;

node.Checked(checked);

if (node.Type() == static_cast<UINT>(ExplorerItem::ExplorerItemType::Folder))
{
for (auto c : node.Children())
{
ToggleAll(c, checked);
}
}
std::for_each(m_explorerItems.begin(), m_explorerItems.end(), [checked](auto item) { item.Checked(checked); });
}

void MainWindow::Checked_ids(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const&)
{
auto checkbox = sender.as<Windows::UI::Xaml::Controls::CheckBox>();
auto id = std::stoi(std::wstring{ checkbox.Name() });
auto item = FindById(id);
if (checkbox.IsChecked().GetBoolean() != item.Checked())
if (item != NULL && checkbox.IsChecked().GetBoolean() != item.Checked())
{
m_uiUpdatesItem.Checked(checkbox.IsChecked().GetBoolean());
m_uiUpdatesItem.ChangedExplorerItemId(id);
Expand All @@ -263,9 +225,7 @@ namespace winrt::PowerRenameUILib::implementation
{
if (checkBox_selectAll().IsChecked().GetBoolean() != m_allSelected)
{
auto fakeRoot = winrt::make<PowerRenameUILib::implementation::ExplorerItem>(0, L"Fake", L"", 0, false);
fakeRoot.Children(m_explorerItems);
ToggleAll(fakeRoot, checkBox_selectAll().IsChecked().GetBoolean());
ToggleAll(checkBox_selectAll().IsChecked().GetBoolean());
m_uiUpdatesItem.ToggleAll();
m_allSelected = !m_allSelected;
}
Expand All @@ -278,6 +238,7 @@ namespace winrt::PowerRenameUILib::implementation
if (!m_uiUpdatesItem.ShowAll())
{
m_explorerItems.Clear();
m_explorerItemsMap.clear();
m_uiUpdatesItem.ShowAll(true);
}
}
Expand All @@ -289,6 +250,7 @@ namespace winrt::PowerRenameUILib::implementation
if (m_uiUpdatesItem.ShowAll())
{
m_explorerItems.Clear();
m_explorerItemsMap.clear();
m_uiUpdatesItem.ShowAll(false);
}
}
Expand Down

0 comments on commit 5a4822f

Please sign in to comment.