Skip to content

Commit

Permalink
Added Carousel To WinUI3/UWP (#8812)
Browse files Browse the repository at this point in the history
* added shared model changes for Carousel

* fixed formatting with clang-format

* added missing files for linking

* added carousel

* core functionality done

* adding rtl

* implemented the rest of the features

* chagned solid to transparency

* added uwp support

* clean up

* fixed space
  • Loading branch information
jwoo-msft authored Feb 2, 2024
1 parent 54fa541 commit e7bfca5
Show file tree
Hide file tree
Showing 33 changed files with 797 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ namespace AdaptiveCardsSharedModelUnitTest
Assert::AreEqual<std::string>(carousel->GetElementTypeString(), CardElementTypeToString(CardElementType::Carousel));
Assert::AreEqual<bool>(carousel->GetAutoLoop().value(), false);
Assert::AreEqual<int>(carousel->GetInitialPage().value(), static_cast<size_t>(1));
Assert::AreEqual<std::string>(carousel->GetHeightInPixels(), "100px");
Assert::AreEqual<int>(carousel->GetHeightInPixels(), 100);
Assert::AreEqual<int>(carousel->GetTimer().value(), 5000);

auto carouselPage = std::dynamic_pointer_cast<CarouselPage>(carousel->GetPages()[0]);
Expand Down Expand Up @@ -300,7 +300,7 @@ namespace AdaptiveCardsSharedModelUnitTest
Assert::AreEqual<std::string>(carousel->GetElementTypeString(), CardElementTypeToString(CardElementType::Carousel));
Assert::AreEqual<bool>(carousel->GetAutoLoop().value(), false);
Assert::AreEqual<int>(carousel->GetInitialPage().value(), static_cast<size_t>(1));
Assert::AreEqual<std::string>(carousel->GetHeightInPixels(), "100px");
Assert::AreEqual<int>(carousel->GetHeightInPixels(), 100);
Assert::AreEqual<int>(carousel->GetTimer().value(), 5000);

auto carouselPage = std::dynamic_pointer_cast<CarouselPage>(carousel->GetPages()[0]);
Expand Down
47 changes: 38 additions & 9 deletions source/shared/cpp/ObjectModel/Carousel.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "Carousel.h"

using namespace AdaptiveCards;

namespace AdaptiveCards
{
Carousel::Carousel() : CollectionCoreElement(CardElementType::Carousel)
Carousel::Carousel() : StyledCollectionElement(CardElementType::Carousel)
{
PopulateKnownPropertiesSet();
}
Expand All @@ -26,10 +28,12 @@ Json::Value Carousel::SerializeToJsonValue() const
{
Json::Value root = CollectionCoreElement::SerializeToJsonValue();

if (!m_heightInPixels.empty())
{
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::HeightInPixels)] = m_heightInPixels;
}
if (m_heightInPixels)
{
std::ostringstream stringStream;
stringStream << m_heightInPixels;
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::HeightInPixels)] = stringStream.str() + "px";
}

if (m_initialPage.has_value())
{
Expand Down Expand Up @@ -62,6 +66,11 @@ Json::Value Carousel::SerializeToJsonValue() const
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Timer)] = m_timer.value_or(0);
}

if (m_rtl.has_value())
{
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Rtl)] = m_rtl.value_or("");
}

return root;
}

Expand All @@ -80,12 +89,12 @@ void Carousel::SetPages(const std::vector<std::shared_ptr<AdaptiveCards::Carouse
m_pages = value;
}

std::string Carousel::GetHeightInPixels() const
unsigned int Carousel::GetHeightInPixels() const
{
return m_heightInPixels;
}

void Carousel::SetHeightInPixels(const std::string& value)
void Carousel::SetHeightInPixels(const unsigned int value)
{
m_heightInPixels = value;
}
Expand Down Expand Up @@ -130,6 +139,16 @@ void Carousel::setAutoLoop(const std::optional<bool>& value)
m_autoLoop = value;
}

std::optional<bool> Carousel::GetRtl() const
{
return m_rtl;
}

void Carousel::SetRtl(const std::optional<bool>& value)
{
m_rtl = value;
}

void Carousel::DeserializeChildren(ParseContext& context, const Json::Value& value)
{
if (auto deserializedPages =
Expand All @@ -146,9 +165,15 @@ std::shared_ptr<BaseCardElement> CarouselParser::Deserialize(ParseContext& conte

context.AddProhibitedElementType(m_prohibitedTypesList);

std::shared_ptr<Carousel> carousel = CollectionCoreElement::Deserialize<Carousel>(context, value);
std::shared_ptr<Carousel> carousel = StyledCollectionElement::Deserialize<Carousel>(context, value);

const auto& optionalPixelHeight =
ParseSizeForPixelSize(ParseUtil::GetString(value, AdaptiveCardSchemaKey::HeightInPixels), &context.warnings);

carousel->SetHeightInPixels(ParseUtil::GetString(value, AdaptiveCardSchemaKey::HeightInPixels));
if (optionalPixelHeight.has_value())
{
carousel->SetHeightInPixels(optionalPixelHeight.value_or(0));
}

carousel->SetInitialPage(ParseUtil::GetOptionalUnsignedInt(value, AdaptiveCardSchemaKey::InitialPage));

Expand All @@ -159,6 +184,10 @@ std::shared_ptr<BaseCardElement> CarouselParser::Deserialize(ParseContext& conte
carousel->SetOrientation(ParseUtil::GetOptionalEnumValue<CarouselOrientation>(
value, AdaptiveCardSchemaKey::Orientation, CarouselOrientationFromString));

carousel->SetRtl(ParseUtil::GetOptionalBool(value, AdaptiveCardSchemaKey::Rtl));

context.RemoveProhibitedElementType(m_prohibitedTypesList);

return carousel;
}

Expand Down
16 changes: 11 additions & 5 deletions source/shared/cpp/ObjectModel/Carousel.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once

#include "pch.h"
#include "CollectionCoreElement.h"
#include "StyledCollectionElement.h"
#include "CarouselPage.h"

namespace AdaptiveCards
{
class Carousel : public CollectionCoreElement
class Carousel : public StyledCollectionElement
{
public:
Carousel();
Expand All @@ -23,8 +25,8 @@ class Carousel : public CollectionCoreElement
const std::vector<std::shared_ptr<AdaptiveCards::CarouselPage>>& GetPages() const;
void SetPages(const std::vector<std::shared_ptr<AdaptiveCards::CarouselPage>>& value);

std::string GetHeightInPixels() const;
void SetHeightInPixels(const std::string& value);
unsigned int GetHeightInPixels() const;
void SetHeightInPixels(const unsigned int value);

std::optional<unsigned long> GetTimer() const;
void SetTimer(const std::optional<unsigned long>& value);
Expand All @@ -38,15 +40,19 @@ class Carousel : public CollectionCoreElement
std::optional<bool> GetAutoLoop() const;
void setAutoLoop(const std::optional<bool>& value);

std::optional<bool> GetRtl() const;
void SetRtl(const std::optional<bool>& value);

private:
void PopulateKnownPropertiesSet();

std::vector<std::shared_ptr<AdaptiveCards::CarouselPage>> m_pages;
std::string m_heightInPixels;
unsigned int m_heightInPixels{0};
std::optional<unsigned int> m_timer;
std::optional<unsigned int> m_initialPage;
std::optional<CarouselOrientation> m_orientation;
std::optional<bool> m_autoLoop;
std::optional<bool> m_rtl;
};

class CarouselParser : public BaseCardElementParser
Expand Down
2 changes: 2 additions & 0 deletions source/shared/cpp/ObjectModel/CarouselPage.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "CarouselPage.h"

Expand Down
2 changes: 2 additions & 0 deletions source/shared/cpp/ObjectModel/CarouselPage.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once

#include "Container.h"
Expand Down
11 changes: 11 additions & 0 deletions source/shared/cpp/ObjectModel/ParseContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ void ParseContext::AddProhibitedElementType(const std::vector<std::string>& list
m_prohibitedElementTypes.insert(list.begin(), list.end());
}

void ParseContext::RemoveProhibitedElementType(const std::vector<std::string>& list)
{
for (const auto& type : list)
{
if (const auto itr = m_prohibitedElementTypes.find(type); itr != m_prohibitedElementTypes.end())
{
m_prohibitedElementTypes.erase(itr);
}
}
}

void ParseContext::ShouldParse(const std::string& typeString)
{
if (m_prohibitedElementTypes.find(typeString) != m_prohibitedElementTypes.end())
Expand Down
1 change: 1 addition & 0 deletions source/shared/cpp/ObjectModel/ParseContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ParseContext
void PopBleedDirection();

void AddProhibitedElementType(const std::vector<std::string>& list);
void RemoveProhibitedElementType(const std::vector<std::string>& list);
void ShouldParse(const std::string& type);

private:
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@

namespace AdaptiveCards
{
constexpr const char* const c_sharedModelVersion = "1.5";
constexpr const char* const c_sharedModelVersion = "1.6";
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace AdaptiveCards.ObjectModel.Uwp
AdaptiveCard,
TextBlock,
Image,
Carousel,
CarouselPage,
Container,
Column,
ColumnSet,
Expand Down Expand Up @@ -258,6 +260,12 @@ namespace AdaptiveCards.ObjectModel.Uwp
MenuItem
};

enum CarouselOrientation
{
Horizontal = 0,
Vertical,
};

declare { interface Windows.Foundation.Collections.IVector<AdaptiveRemoteResourceInformation>; }

interface IAdaptiveCardElement
Expand Down Expand Up @@ -593,6 +601,25 @@ namespace AdaptiveCards.ObjectModel.Uwp
Windows.Foundation.Collections.IVector<AdaptiveColumn> Columns { get; };
};

[default_interface] runtimeclass AdaptiveCarouselPage : IAdaptiveContainer, IAdaptiveContainerBase, IAdaptiveCardElement
{
AdaptiveCarouselPage();
};

runtimeclass AdaptiveCarousel : IAdaptiveContainerBase, IAdaptiveCardElement
{
AdaptiveCarousel();
Windows.Foundation.Collections.IVector<AdaptiveCarouselPage> Pages { get; };
CarouselOrientation Orientation;
Windows.Foundation.IReference<UInt64> Timer;
Windows.Foundation.IReference<UInt32> InitialPage;
Windows.Foundation.IReference<Boolean> AutoLoop;
Windows.Foundation.IReference<VerticalContentAlignment> VerticalContentAlignment;
Windows.Foundation.IReference<Boolean> Rtl;
AdaptiveBackgroundImage BackgroundImage;
UInt32 HeightInPixels;
};

runtimeclass AdaptiveFact
{
AdaptiveFact();
Expand Down
10 changes: 10 additions & 0 deletions source/uwp/Renderer/idl/AdaptiveCards.Rendering.Uwp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ namespace AdaptiveCards
AdaptiveImageSetRenderer();
}

[default_interface] runtimeclass AdaptiveCarouselRenderer : IAdaptiveElementRenderer
{
AdaptiveCarouselRenderer();
}

[default_interface] runtimeclass AdaptiveCarouselPageRenderer : IAdaptiveElementRenderer
{
AdaptiveCarouselPageRenderer();
}

[default_interface] runtimeclass AdaptiveContainerRenderer : IAdaptiveElementRenderer
{
AdaptiveContainerRenderer();
Expand Down
4 changes: 4 additions & 0 deletions source/uwp/SharedObjectModel/SharedObjectModel.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
<ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarousel.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarouselPage.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)lib\pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClInclude Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarousel.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarouselPage.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)lib\pch.h" />
<ClCompile Include="$(MSBuildThisFileDirectory)lib\AdaptiveActionElement.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)lib\AdaptiveActionParserRegistration.cpp" />
Expand Down
12 changes: 12 additions & 0 deletions source/uwp/SharedObjectModel/SharedObjectModel.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@
<ClCompile Include="$(MSBuildThisFileDirectory)lib\pch.cpp">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarousel.cpp">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarouselPage.cpp">
<Filter>lib</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)lib\AdaptiveActionElement.h">
Expand Down Expand Up @@ -473,5 +479,11 @@
<ClInclude Include="$(MSBuildThisFileDirectory)lib\pch.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarousel.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)lib\AdaptiveCarouselPage.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion source/uwp/SharedObjectModel/lib/AdaptiveCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace winrt::AdaptiveCards::ObjectModel::Xaml_OM::implementation
try
{
::AdaptiveCards::ParseContext context(sharedModelElementParserRegistration, sharedModelActionParserRegistration);
const std::string c_rendererVersion = "1.5";
const std::string c_rendererVersion = "1.6";
auto sharedParseResult = ::AdaptiveCards::AdaptiveCard::DeserializeFromString(jsonString, c_rendererVersion, context);
adaptiveParseResult->AdaptiveCard =
*winrt::make_self<implementation::AdaptiveCard>(sharedParseResult->GetAdaptiveCard());
Expand Down
Loading

0 comments on commit e7bfca5

Please sign in to comment.