|
4 | 4 |
|
5 | 5 | #include "AdaptiveToggleInputRenderer.h" |
6 | 6 |
|
7 | | -using namespace Microsoft::WRL; |
8 | | -using namespace Microsoft::WRL::Wrappers; |
9 | | -using namespace ABI::AdaptiveCards::Rendering::Uwp; |
10 | | -using namespace ABI::AdaptiveCards::ObjectModel::Uwp; |
11 | | -using namespace ABI::Windows::Foundation; |
12 | | -using namespace ABI::Windows::Foundation::Collections; |
13 | | -using namespace ABI::Windows::UI::Xaml; |
14 | | -using namespace ABI::Windows::UI::Xaml::Controls; |
15 | | -using namespace ABI::Windows::UI::Xaml::Controls::Primitives; |
| 7 | +namespace Xaml |
| 8 | +{ |
| 9 | + using namespace winrt::Windows::UI::Xaml; |
| 10 | +} |
16 | 11 |
|
17 | | -namespace AdaptiveCards::Rendering::Uwp |
| 12 | +namespace XamlHelpers |
18 | 13 | { |
19 | | - HRESULT AdaptiveToggleInputRenderer::RuntimeClassInitialize() noexcept |
20 | | - try |
21 | | - { |
22 | | - return S_OK; |
23 | | - } |
24 | | - CATCH_RETURN; |
| 14 | + using namespace AdaptiveCards::Rendering::Uwp::XamlHelpers; |
| 15 | +} |
| 16 | + |
| 17 | +using namespace winrt::AdaptiveCards::ObjectModel::Uwp; |
| 18 | +using namespace winrt::AdaptiveCards::Rendering::Uwp; |
25 | 19 |
|
26 | | - HRESULT AdaptiveToggleInputRenderer::Render(_In_ IAdaptiveCardElement* adaptiveCardElement, |
27 | | - _In_ IAdaptiveRenderContext* renderContext, |
28 | | - _In_ IAdaptiveRenderArgs* renderArgs, |
29 | | - _COM_Outptr_ IUIElement** toggleInputControl) noexcept |
30 | | - try |
| 20 | +namespace winrt::AdaptiveCards::Rendering::Uwp::implementation |
| 21 | +{ |
| 22 | + Xaml::UIElement AdaptiveToggleInputRenderer::Render(IAdaptiveCardElement cardElement, |
| 23 | + AdaptiveRenderContext renderContext, |
| 24 | + AdaptiveRenderArgs renderArgs) |
31 | 25 | { |
32 | | - ComPtr<IAdaptiveHostConfig> hostConfig; |
33 | | - RETURN_IF_FAILED(renderContext->get_HostConfig(&hostConfig)); |
34 | | - if (!XamlHelpers::SupportsInteractivity(hostConfig.Get())) |
| 26 | + if (!renderContext.HostConfig().SupportsInteractivity()) |
35 | 27 | { |
36 | | - renderContext->AddWarning( |
37 | | - ABI::AdaptiveCards::ObjectModel::Uwp::WarningStatusCode::InteractivityNotSupported, |
38 | | - HStringReference(L"Toggle Input was stripped from card because interactivity is not supported").Get()); |
39 | | - return S_OK; |
40 | | - } |
| 28 | + renderContext.AddWarning(winrt::AdaptiveCards::ObjectModel::Uwp::WarningStatusCode::InteractivityNotSupported, |
| 29 | + L"Toggle Input was stripped from card because interactivity is not supported"); |
41 | 30 |
|
42 | | - ComPtr<IAdaptiveCardElement> cardElement(adaptiveCardElement); |
43 | | - ComPtr<IAdaptiveToggleInput> adaptiveToggleInput; |
44 | | - RETURN_IF_FAILED(cardElement.As(&adaptiveToggleInput)); |
45 | | - |
46 | | - ComPtr<ICheckBox> checkBox = |
47 | | - XamlHelpers::CreateABIClass<ICheckBox>(HStringReference(RuntimeClass_Windows_UI_Xaml_Controls_CheckBox)); |
48 | | - |
49 | | - HString title; |
50 | | - RETURN_IF_FAILED(adaptiveToggleInput->get_Title(title.GetAddressOf())); |
51 | | - |
52 | | - boolean wrap; |
53 | | - adaptiveToggleInput->get_Wrap(&wrap); |
54 | | - |
55 | | - XamlHelpers::SetContent(checkBox.Get(), title.Get(), wrap); |
56 | | - |
57 | | - HString value; |
58 | | - RETURN_IF_FAILED(adaptiveToggleInput->get_Value(value.GetAddressOf())); |
59 | | - |
60 | | - HString valueOn; |
61 | | - RETURN_IF_FAILED(adaptiveToggleInput->get_ValueOn(valueOn.GetAddressOf())); |
62 | | - |
63 | | - INT32 compareValueOn; |
64 | | - RETURN_IF_FAILED(WindowsCompareStringOrdinal(value.Get(), valueOn.Get(), &compareValueOn)); |
| 31 | + return nullptr; |
| 32 | + } |
65 | 33 |
|
66 | | - XamlHelpers::SetToggleValue(checkBox.Get(), (compareValueOn == 0)); |
| 34 | + AdaptiveToggleInput toggleInput = cardElement.as<AdaptiveToggleInput>(); |
67 | 35 |
|
68 | | - ComPtr<IUIElement> checkboxAsUIElement; |
69 | | - RETURN_IF_FAILED(checkBox.As(&checkboxAsUIElement)); |
70 | | - RETURN_IF_FAILED(XamlHelpers::AddHandledTappedEvent(checkboxAsUIElement.Get())); |
| 36 | + Xaml::Controls::CheckBox checkBox{}; |
| 37 | + XamlHelpers::SetContent(checkBox, toggleInput.Title(), toggleInput.Wrap()); |
71 | 38 |
|
72 | | - ComPtr<IFrameworkElement> frameworkElement; |
73 | | - RETURN_IF_FAILED(checkBox.As(&frameworkElement)); |
74 | | - RETURN_IF_FAILED(frameworkElement->put_VerticalAlignment(ABI::Windows::UI::Xaml::VerticalAlignment_Top)); |
75 | | - RETURN_IF_FAILED( |
76 | | - XamlHelpers::SetStyleFromResourceDictionary(renderContext, L"Adaptive.Input.Toggle", frameworkElement.Get())); |
| 39 | + checkBox.IsChecked(toggleInput.ValueOn() == toggleInput.Value()); |
| 40 | + checkBox.VerticalAlignment(Xaml::VerticalAlignment::Top); |
77 | 41 |
|
78 | | - ComPtr<IAdaptiveInputElement> adapitveToggleInputAsAdaptiveInput; |
79 | | - RETURN_IF_FAILED(adaptiveToggleInput.As(&adapitveToggleInputAsAdaptiveInput)); |
| 42 | + // Add Tap handler that sets the event as handled so that it doesn't propagate to the parent containers. |
| 43 | + checkBox.Tapped([](IInspectable const& /* sender */, Xaml::Input::TappedRoutedEventArgs const& args) |
| 44 | + { return args.Handled(true); }); |
80 | 45 |
|
81 | | - ComPtr<IUIElement> inputLayout; |
82 | | - ComPtr<IUIElement> validationError; |
83 | | - RETURN_IF_FAILED(XamlHelpers::HandleInputLayoutAndValidation( |
84 | | - adapitveToggleInputAsAdaptiveInput.Get(), checkboxAsUIElement.Get(), false, renderContext, &inputLayout, nullptr)); |
| 46 | + XamlHelpers::SetStyleFromResourceDictionary(renderContext, L"Adaptive.Input.Toggle", checkBox); |
| 47 | + Xaml::UIElement inputLayout = XamlHelpers::HandleInputLayoutAndValidation(toggleInput, checkBox, false, renderContext); |
85 | 48 |
|
86 | | - ComPtr<ToggleInputValue> input; |
87 | | - RETURN_IF_FAILED(MakeAndInitialize<ToggleInputValue>(&input, adaptiveToggleInput.Get(), checkBox.Get(), nullptr)); |
88 | | - RETURN_IF_FAILED(renderContext->AddInputValue(input.Get(), renderArgs)); |
| 49 | + winrt::com_ptr<::AdaptiveCards::Rendering::Uwp::ToggleInputValue> inputValue; |
| 50 | + THROW_IF_FAILED(Microsoft::WRL::MakeAndInitialize<::AdaptiveCards::Rendering::Uwp::ToggleInputValue>( |
| 51 | + inputValue.put(), |
| 52 | + toggleInput.as<ABI::AdaptiveCards::ObjectModel::Uwp::IAdaptiveToggleInput>().get(), |
| 53 | + checkBox.as<ABI::Windows::UI::Xaml::Controls::ICheckBox>().get(), |
| 54 | + nullptr)); |
89 | 55 |
|
90 | | - RETURN_IF_FAILED(inputLayout.CopyTo(toggleInputControl)); |
| 56 | + renderContext.AddInputValue(inputValue.as<IAdaptiveInputValue>(), renderArgs); |
91 | 57 |
|
92 | | - return S_OK; |
| 58 | + return inputLayout; |
93 | 59 | } |
94 | | - CATCH_RETURN; |
95 | 60 | } |
0 commit comments