Skip to content

Remove WinRT composition (inheritance) from PaletteItem #19132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 16, 2025
27 changes: 20 additions & 7 deletions src/cascadia/LocalTests_TerminalApp/FilteredCommandTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT license.

#include "pch.h"
#include "../TerminalApp/CommandLinePaletteItem.h"
#include "../TerminalApp/CommandPalette.h"
#include "../TerminalApp/BasePaletteItem.h"
#include "CppWinrtTailored.h"

using namespace Microsoft::Console;
Expand All @@ -15,6 +15,19 @@ using namespace winrt::Microsoft::Terminal::Control;

namespace TerminalAppLocalTests
{
struct StringPaletteItem : winrt::implements<StringPaletteItem, winrt::TerminalApp::IPaletteItem, winrt::Windows::UI::Xaml::Data::INotifyPropertyChanged>, winrt::TerminalApp::implementation::BasePaletteItem<StringPaletteItem, winrt::TerminalApp::PaletteItemType::CommandLine>
{
StringPaletteItem(std::wstring_view value) :
_value{ value } {}

winrt::hstring Name() { return _value; }
winrt::hstring KeyChordText() { return {}; }
winrt::hstring Icon() { return {}; }

private:
winrt::hstring _value;
};

class FilteredCommandTests
{
BEGIN_TEST_CLASS(FilteredCommandTests)
Expand All @@ -40,7 +53,7 @@ namespace TerminalAppLocalTests
auto result = RunOnUIThread([]() {
const WEX::TestExecution::DisableVerifyExceptions disableExceptionsScope;

const auto paletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(L"AAAAAABBBBBBCCC") };
const auto paletteItem{ winrt::make<StringPaletteItem>(L"AAAAAABBBBBBCCC") };
const auto filteredCommand = winrt::make_self<winrt::TerminalApp::implementation::FilteredCommand>(paletteItem);

{
Expand Down Expand Up @@ -112,7 +125,7 @@ namespace TerminalAppLocalTests
void FilteredCommandTests::VerifyWeight()
{
auto result = RunOnUIThread([]() {
const auto paletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(L"AAAAAABBBBBBCCC") };
const auto paletteItem{ winrt::make<StringPaletteItem>(L"AAAAAABBBBBBCCC") };
const auto filteredCommand = winrt::make_self<winrt::TerminalApp::implementation::FilteredCommand>(paletteItem);

const auto weigh = [&](const wchar_t* str) {
Expand Down Expand Up @@ -152,8 +165,8 @@ namespace TerminalAppLocalTests
void FilteredCommandTests::VerifyCompare()
{
auto result = RunOnUIThread([]() {
const auto paletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(L"AAAAAABBBBBBCCC") };
const auto paletteItem2{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(L"BBBBBCCC") };
const auto paletteItem{ winrt::make<StringPaletteItem>(L"AAAAAABBBBBBCCC") };
const auto paletteItem2{ winrt::make<StringPaletteItem>(L"BBBBBCCC") };
{
Log::Comment(L"Testing comparison of commands with no filter");
const auto filteredCommand = winrt::make_self<winrt::TerminalApp::implementation::FilteredCommand>(paletteItem);
Expand Down Expand Up @@ -192,8 +205,8 @@ namespace TerminalAppLocalTests
void FilteredCommandTests::VerifyCompareIgnoreCase()
{
auto result = RunOnUIThread([]() {
const auto paletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(L"a") };
const auto paletteItem2{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(L"B") };
const auto paletteItem{ winrt::make<StringPaletteItem>(L"a") };
const auto paletteItem2{ winrt::make<StringPaletteItem>(L"B") };
{
const auto filteredCommand = winrt::make_self<winrt::TerminalApp::implementation::FilteredCommand>(paletteItem);
const auto filteredCommand2 = winrt::make_self<winrt::TerminalApp::implementation::FilteredCommand>(paletteItem2);
Expand Down
28 changes: 0 additions & 28 deletions src/cascadia/TerminalApp/ActionPaletteItem.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions src/cascadia/TerminalApp/ActionPaletteItem.h

This file was deleted.

14 changes: 0 additions & 14 deletions src/cascadia/TerminalApp/ActionPaletteItem.idl

This file was deleted.

44 changes: 44 additions & 0 deletions src/cascadia/TerminalApp/BasePaletteItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#pragma once

namespace winrt::TerminalApp::implementation
{
template<typename T, winrt::TerminalApp::PaletteItemType Ty>
struct BasePaletteItem
{
public:
winrt::TerminalApp::PaletteItemType Type() { return Ty; }

Windows::UI::Xaml::Controls::IconElement ResolvedIcon()
{
const auto icon{ static_cast<T*>(this)->Icon() };
if (!_resolvedIcon && !icon.empty())
{
const auto resolvedIcon{ Microsoft::Terminal::UI::IconPathConverter::IconWUX(icon) };
resolvedIcon.Width(16);
resolvedIcon.Height(16);
_resolvedIcon = resolvedIcon;
}
return _resolvedIcon;
}

til::property_changed_event PropertyChanged;

protected:
void BaseRaisePropertyChanged(wil::zwstring_view property)
{
PropertyChanged.raise(*static_cast<T*>(this), winrt::Windows::UI::Xaml::Data::PropertyChangedEventArgs{ property });
}

void InvalidateResolvedIcon()
{
_resolvedIcon = nullptr;
BaseRaisePropertyChanged(L"ResolvedIcon");
}

private:
Windows::UI::Xaml::Controls::IconElement _resolvedIcon{ nullptr };
};
}
26 changes: 0 additions & 26 deletions src/cascadia/TerminalApp/CommandLinePaletteItem.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/cascadia/TerminalApp/CommandLinePaletteItem.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/cascadia/TerminalApp/CommandLinePaletteItem.idl

This file was deleted.

63 changes: 38 additions & 25 deletions src/cascadia/TerminalApp/CommandPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// Licensed under the MIT license.

#include "pch.h"
#include "ActionPaletteItem.h"
#include "TabPaletteItem.h"
#include "CommandLinePaletteItem.h"
#include "CommandPalette.h"
#include "CommandPaletteItems.h"
#include <LibraryResources.h>

#include "CommandPalette.g.cpp"
Expand Down Expand Up @@ -233,9 +231,11 @@ namespace winrt::TerminalApp::implementation
}
else if (_currentMode == CommandPaletteMode::ActionMode && filteredCommand != nullptr)
{
if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
const auto item{ filteredCommand.Item() };
if (item.Type() == PaletteItemType::Action)
{
PreviewAction.raise(*this, actionPaletteItem.Command());
const auto actionPaletteItem{ winrt::get_self<ActionPaletteItem>(item) };
PreviewAction.raise(*this, actionPaletteItem->Command());
}
}
else if (_currentMode == CommandPaletteMode::CommandlineMode)
Expand Down Expand Up @@ -555,10 +555,11 @@ namespace winrt::TerminalApp::implementation
const auto enteredItem = listViewItem.Content();
if (const auto filteredCommand{ enteredItem.try_as<winrt::TerminalApp::FilteredCommand>() })
{
if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
const auto item{ filteredCommand.Item() };
if (item.Type() == PaletteItemType::Action)
{
// immediately preview the hovered command
PreviewAction.raise(*this, actionPaletteItem.Command());
const auto actionPaletteItem{ winrt::get_self<ActionPaletteItem>(item) };
PreviewAction.raise(*this, actionPaletteItem->Command());
}
}
}
Expand Down Expand Up @@ -589,9 +590,11 @@ namespace winrt::TerminalApp::implementation
{
if (_currentMode == CommandPaletteMode::ActionMode && filteredCommand)
{
if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
const auto item{ filteredCommand.Item() };
if (item.Type() == PaletteItemType::Action)
{
PreviewAction.raise(*this, actionPaletteItem.Command());
const auto actionPaletteItem{ winrt::get_self<ActionPaletteItem>(item) };
PreviewAction.raise(*this, actionPaletteItem->Command());
}
}
}
Expand All @@ -617,7 +620,7 @@ namespace winrt::TerminalApp::implementation
const auto selectedCommand = selectedList.GetAt(0);
if (const auto filteredCmd = selectedCommand.try_as<TerminalApp::FilteredCommand>())
{
if (const auto paletteItem = filteredCmd.Item().try_as<TerminalApp::PaletteItem>())
if (const auto paletteItem = filteredCmd.Item())
{
automationPeer.RaiseNotificationEvent(
Automation::Peers::AutomationNotificationKind::ItemAdded,
Expand Down Expand Up @@ -652,10 +655,13 @@ namespace winrt::TerminalApp::implementation
if (_nestedActionStack.Size() > 0)
{
const auto newPreviousAction{ _nestedActionStack.GetAt(_nestedActionStack.Size() - 1) };
const auto actionPaletteItem{ newPreviousAction.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() };

ParentCommandName(actionPaletteItem.Command().Name());
_updateCurrentNestedCommands(actionPaletteItem.Command());
const auto item{ newPreviousAction.Item() };
if (item.Type() == PaletteItemType::Action)
{
const auto actionPaletteItem{ winrt::get_self<ActionPaletteItem>(item) };
ParentCommandName(actionPaletteItem->Command().Name());
_updateCurrentNestedCommands(actionPaletteItem->Command());
}
}
else
{
Expand Down Expand Up @@ -757,16 +763,19 @@ namespace winrt::TerminalApp::implementation
}
else if (filteredCommand)
{
if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
auto item{ filteredCommand.Item() };
if (item.Type() == PaletteItemType::Action)
{
if (actionPaletteItem.Command().HasNestedCommands())
const auto actionPaletteItem{ winrt::get_self<ActionPaletteItem>(item) };
auto command{ actionPaletteItem->Command() };
if (command.HasNestedCommands())
{
// If this Command had subcommands, then don't dispatch the
// action. Instead, display a new list of commands for the user
// to pick from.
_nestedActionStack.Append(filteredCommand);
ParentCommandName(actionPaletteItem.Command().Name());
_updateCurrentNestedCommands(actionPaletteItem.Command());
ParentCommandName(command.Name());
_updateCurrentNestedCommands(command);

_updateUIForStackChange();
}
Expand All @@ -785,9 +794,9 @@ namespace winrt::TerminalApp::implementation
// But make an exception for the Toggle Command Palette action: we don't want the dispatch
// make the command palette - that was just closed - visible again.
// All other actions can just be dispatched.
if (actionPaletteItem.Command().ActionAndArgs().Action() != ShortcutAction::ToggleCommandPalette)
if (command.ActionAndArgs().Action() != ShortcutAction::ToggleCommandPalette)
{
DispatchCommandRequested.raise(*this, actionPaletteItem.Command());
DispatchCommandRequested.raise(*this, command);
}

TraceLoggingWrite(
Expand Down Expand Up @@ -837,9 +846,11 @@ namespace winrt::TerminalApp::implementation
{
if (filteredCommand)
{
if (const auto tabPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::TabPaletteItem>() })
const auto item{ filteredCommand.Item() };
if (item.Type() == PaletteItemType::Tab)
{
if (const auto tab{ tabPaletteItem.Tab() })
const auto tabPaletteItem{ winrt::get_self<TabPaletteItem>(item) };
if (const auto tab{ tabPaletteItem->Tab() })
{
SwitchToTabRequested.raise(*this, tab);
}
Expand Down Expand Up @@ -867,9 +878,11 @@ namespace winrt::TerminalApp::implementation
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));

if (const auto commandLinePaletteItem{ filteredCommand.value().Item().try_as<winrt::TerminalApp::CommandLinePaletteItem>() })
const auto item{ filteredCommand->Item() };
if (item.Type() == PaletteItemType::CommandLine)
{
CommandLineExecutionRequested.raise(*this, commandLinePaletteItem.CommandLine());
const auto commandLinePaletteItem{ winrt::get_self<CommandLinePaletteItem>(item) };
CommandLineExecutionRequested.raise(*this, commandLinePaletteItem->CommandLine());
_close();
}
}
Expand Down
Loading
Loading