Skip to content

Commit

Permalink
Try to do the DataTemplate thing, and fail.
Browse files Browse the repository at this point in the history
Surprise surprise! we ran into an old friend:
* #9288
* #9487
* microsoft/microsoft-ui-xaml#2121

so uh, this is ded.
  • Loading branch information
zadjii-msft committed Jun 10, 2024
1 parent 8d8590d commit def4190
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 2 deletions.
108 changes: 107 additions & 1 deletion src/cascadia/TerminalApp/SnippetsPaneContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "SnippetsPaneContent.h"
#include "SnippetsPaneContent.g.cpp"
#include "FilteredTask.g.cpp"
#include "SnippetsItemTemplateSelector.g.cpp"

using namespace winrt::Windows::Foundation;
using namespace winrt::Microsoft::Terminal::Settings;
Expand All @@ -23,7 +24,8 @@ namespace winrt::TerminalApp::implementation
{
InitializeComponent();

// auto res = Windows::UI::Xaml::Application::Current().Resources();
// _itemTemplateSelector = Resources().Lookup(winrt::box_value(L"SnippetsItemTemplateSelector")).try_as<SnippetsItemTemplateSelector>();

auto bg = Resources().Lookup(winrt::box_value(L"PageBackground"));
Background(bg.try_as<WUX::Media::Brush>());
}
Expand Down Expand Up @@ -129,4 +131,108 @@ namespace winrt::TerminalApp::implementation
}
}

// // Method Description:
// // - This event is triggered when filteredActionView is looking for item container (ListViewItem)
// // to use to present the filtered actions.
// // GH#9288: unfortunately the default lookup seems to choose items with wrong data templates,
// // e.g., using DataTemplate rendering actions for tab palette items.
// // We handle this event by manually selecting an item from the cache.
// // If no item is found we allocate a new one.
// // Arguments:
// // - args: the ChoosingItemContainerEventArgs allowing to get container candidate suggested by the
// // system and replace it with another candidate if required.
// // Return Value:
// // - <none>
// void SnippetsPaneContent::_choosingItemContainer(
// const Windows::UI::Xaml::Controls::ListViewBase& /*sender*/,
// const Windows::UI::Xaml::Controls::ChoosingItemContainerEventArgs& args)
// {
// const auto dataTemplate = _itemTemplateSelector.SelectTemplate(args.Item());
// const auto itemContainer = args.ItemContainer();
// if (itemContainer && itemContainer.ContentTemplate() == dataTemplate)
// {
// // If the suggested candidate is OK simply remove it from the cache
// // (so we won't allocate it until it is released) and return
// _listViewItemsCache[dataTemplate].erase(itemContainer);
// }
// else
// {
// // We need another candidate, let's look it up inside the cache
// auto& containersByTemplate = _listViewItemsCache[dataTemplate];
// if (!containersByTemplate.empty())
// {
// // There cache contains available items for required DataTemplate
// // Let's return one of them (and remove it from the cache)
// auto firstItem = containersByTemplate.begin();
// args.ItemContainer(*firstItem);
// containersByTemplate.erase(firstItem);
// }
// else
// {
// ElementFactoryGetArgs factoryArgs{};
// const auto listViewItem = _listItemTemplate.GetElement(factoryArgs).try_as<Controls::ListViewItem>();
// listViewItem.ContentTemplate(dataTemplate);

// if (dataTemplate == _itemTemplateSelector.NestedItemTemplate())
// {
// const auto helpText = winrt::box_value(RS_(L"CommandPalette_MoreOptions/[using:Windows.UI.Xaml.Automation]AutomationProperties/HelpText"));
// listViewItem.SetValue(Automation::AutomationProperties::HelpTextProperty(), helpText);
// }

// args.ItemContainer(listViewItem);
// }
// }
// args.IsContainerPrepared(true);
// }

// // Method Description:
// // - This event is triggered when the data item associate with filteredActionView list item is changing.
// // We check if the item is being recycled. In this case we return it to the cache
// // Arguments:
// // - args: the ContainerContentChangingEventArgs describing the container change
// // Return Value:
// // - <none>
// void SnippetsPaneContent::_containerContentChanging(
// const Windows::UI::Xaml::Controls::ListViewBase& /*sender*/,
// const Windows::UI::Xaml::Controls::ContainerContentChangingEventArgs& args)
// {
// const auto itemContainer = args.ItemContainer();
// if (args.InRecycleQueue() && itemContainer && itemContainer.ContentTemplate())
// {
// _listViewItemsCache[itemContainer.ContentTemplate()].insert(itemContainer);
// itemContainer.DataContext(nullptr);
// }
// else
// {
// itemContainer.DataContext(args.Item());
// }
// }

#pragma region(SnippetsItemTemplateSelector)

WUX::DataTemplate SnippetsItemTemplateSelector::SelectTemplateCore(const winrt::IInspectable& item, const winrt::WUX::DependencyObject& /*container*/)
{
return SelectTemplateCore(item);
}

// Method Description:
// - This method is called once command palette decides how to render a filtered command.
// Currently we support two ways to render command, that depend on its palette item type:
// - For TabPalette item we render an icon, a title, and some tab-related indicators like progress bar (as defined by TabItemTemplate)
// - All other items are currently rendered with icon, title and optional key-chord (as defined by GeneralItemTemplate)
// Arguments:
// - item - an instance of filtered command to render
// Return Value:
// - data template to use for rendering
WUX::DataTemplate SnippetsItemTemplateSelector::SelectTemplateCore(const winrt::IInspectable& item)
{
if (const auto filteredTask{ item.try_as<winrt::TerminalApp::FilteredTask>() })
{
return filteredTask.HasChildren() ? NestedItemTemplate() : GeneralItemTemplate();
}

return GeneralItemTemplate();
}
#pragma endregion

}
18 changes: 18 additions & 0 deletions src/cascadia/TerminalApp/SnippetsPaneContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once
#include "SnippetsPaneContent.g.h"
#include "FilteredTask.g.h"
#include "SnippetsItemTemplateSelector.g.h"
#include "FilteredCommand.h"
#include "ActionPaletteItem.h"
#include <LibraryResources.h>
Expand Down Expand Up @@ -58,6 +59,11 @@ namespace winrt::TerminalApp::implementation
void _filterTextChanged(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& args);

void _updateFilteredCommands();

// void _choosingItemContainer(const Windows::UI::Xaml::Controls::ListViewBase& sender, const Windows::UI::Xaml::Controls::ChoosingItemContainerEventArgs& args);
// void _containerContentChanging(const Windows::UI::Xaml::Controls::ListViewBase& sender, const Windows::UI::Xaml::Controls::ContainerContentChangingEventArgs& args);
// winrt::TerminalApp::SnippetsItemTemplateSelector _itemTemplateSelector{ nullptr };
// std::unordered_map<Windows::UI::Xaml::DataTemplate, std::unordered_set<Windows::UI::Xaml::Controls::Primitives::SelectorItem, winrt_object_hash>, winrt_object_hash> _listViewItemsCache;
};

struct FilteredTask : FilteredTaskT<FilteredTask, TerminalApp::implementation::FilteredCommand>
Expand Down Expand Up @@ -135,9 +141,21 @@ namespace winrt::TerminalApp::implementation
winrt::Microsoft::Terminal::Settings::Model::Command _command{ nullptr };
winrt::Windows::Foundation::Collections::IObservableVector<TerminalApp::FilteredTask> _children{ nullptr };
};

struct SnippetsItemTemplateSelector : SnippetsItemTemplateSelectorT<SnippetsItemTemplateSelector>
{
SnippetsItemTemplateSelector() = default;

Windows::UI::Xaml::DataTemplate SelectTemplateCore(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::UI::Xaml::DependencyObject&);
Windows::UI::Xaml::DataTemplate SelectTemplateCore(const winrt::Windows::Foundation::IInspectable&);

til::property<winrt::Windows::UI::Xaml::DataTemplate> NestedItemTemplate;
til::property<winrt::Windows::UI::Xaml::DataTemplate> GeneralItemTemplate;
};
}

namespace winrt::TerminalApp::factory_implementation
{
BASIC_FACTORY(SnippetsPaneContent);
BASIC_FACTORY(SnippetsItemTemplateSelector);
}
37 changes: 36 additions & 1 deletion src/cascadia/TerminalApp/SnippetsPaneContent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,38 @@
</mux:TreeViewItem>
</DataTemplate>

<DataTemplate x:Key="NestedItemTemplate"
x:DataType="local:FilteredTask">
<mux:TreeViewItem x:Name="rootItem"
ItemsSource="{x:Bind Children}"
Visibility="{x:Bind Visibility, Mode=OneWay}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ContentPresenter Grid.Column="0">
<IconSourceElement Width="16"
Height="16"
IconSource="{x:Bind mtu:IconPathConverter.IconSourceWUX(Item.Icon), Mode=OneTime}"
Visibility="Collapsed" />
</ContentPresenter>

<local:HighlightedTextControl Grid.Column="1"
HorizontalAlignment="Left"
Text="{x:Bind HighlightedName, Mode=OneWay}" />
</Grid>
</mux:TreeViewItem>
</DataTemplate>

<local:SnippetsItemTemplateSelector x:Key="SnippetsItemTemplateSelector"
GeneralItemTemplate="{StaticResource TaskItemTemplate}"
NestedItemTemplate="{StaticResource NestedItemTemplate}" />

<ResourceDictionary.ThemeDictionaries>
<!-- same as in MainPage, this is SolidBackgroundFillColorTertiary -->
<ResourceDictionary x:Key="Dark">
Expand Down Expand Up @@ -245,8 +277,11 @@
AllowFocusOnInteraction="True"
CanDragItems="False"
CanReorderItems="False"
ItemTemplate="{StaticResource TaskItemTemplate}"
ItemContainerStyleSelector="{StaticResource SnippetsItemTemplateSelector}"
Visibility="{x:Bind HasSnippets, Mode=OneWay}" />

<!-- ChoosingItemContainer="_choosingItemContainer" -->
<!-- ContainerContentChanging="_containerContentChanging" -->
</Grid>

</UserControl>
7 changes: 7 additions & 0 deletions src/cascadia/TerminalApp/TerminalPaneContent.idl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ namespace TerminalApp
Boolean HasSnippets { get; };
event Windows.Foundation.TypedEventHandler<Object, Microsoft.Terminal.Settings.Model.Command> DispatchCommandRequested;
}

[default_interface] runtimeclass SnippetsItemTemplateSelector : Windows.UI.Xaml.Controls.DataTemplateSelector
{
SnippetsItemTemplateSelector();
Windows.UI.Xaml.DataTemplate GeneralItemTemplate;
Windows.UI.Xaml.DataTemplate NestedItemTemplate;
}
}

0 comments on commit def4190

Please sign in to comment.