Skip to content

Commit

Permalink
Allow winui to detect if it's in CBS package (#3520)
Browse files Browse the repository at this point in the history
* build cbs

* Revert "build cbs"

This reverts commit 24b6812.

* cbs:

* Update dev/dll/SharedHelpers.cpp

Co-authored-by: Jevan Saks <jevansa@microsoft.com>

* Update dev/dll/XamlControlsResources.cpp

Co-authored-by: Jevan Saks <jevansa@microsoft.com>

* remove check for old platform and add assert

* make pipeline build cbs resources.pri and manifest

* Update dev/dll/XamlControlsResources.cpp

* Update dev/dll/XamlControlsResources.cpp

* fail fast

* Update dev/dll/XamlControlsResources.cpp

* fix package name when generate CBSManifest

Co-authored-by: Jevan Saks <jevansa@microsoft.com>
  • Loading branch information
licanhua and jevansaks authored Nov 16, 2020
1 parent 0d4c295 commit c9b5e60
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
7 changes: 7 additions & 0 deletions build/AzurePipelinesTemplates/MUX-CreateNugetPackage-Job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ jobs:
sourceFolder: '$(Build.SourcesDirectory)\Artifacts\drop'
Contents: '**\Microsoft.UI.Xaml*.appx'

# Copy the CBS packages into the same location in the artifact drop to be republished at the end of the job.
- task: CopyFiles@2
inputs:
targetFolder: '${{ parameters.nupkgdir }}'
sourceFolder: '$(Build.SourcesDirectory)\Artifacts\drop'
Contents: '**\CBS*'

- powershell: |
$prereleaseTag = "${{ parameters.prereleaseVersionTag }}"
if ("${{ parameters.useReleaseTag}}" -eq [bool]::TrueString) { $prereleaseTag = "" }
Expand Down
9 changes: 9 additions & 0 deletions build/FrameworkPackage/MakeFrameworkPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ $manifestContents = $manifestContents.Replace('$(ActivatableTypes)', "$Activatab
$manifestContents = $manifestContents.Replace('$(Version)', "$Version")
Set-Content -Value $manifestContents $fullOutputPath\PackageContents\AppxManifest.xml

$manifestContents = $manifestContents.Replace("$PackageName", "Microsoft.UI.Xaml.CBS")
$manifestContents = $manifestContents.Replace('FrameworkPackageDetector', "CBSPackageDetector")
Set-Content -Value $manifestContents $fullOutputPath\CBSAppxManifest.xml

# Call GetFullPath to clean up the path -- makepri is very picky about double slashes in the path.
$priConfigPath = [IO.Path]::GetFullPath("$fullOutputPath\priconfig.xml")
$priOutputPath = [IO.Path]::GetFullPath("$fullOutputPath\resources.pri")
$priCBSOutputPath = [IO.Path]::GetFullPath("$fullOutputPath\CBSresources.pri")
$noiseAssetPath = [IO.Path]::GetFullPath("$fullOutputPath\Assets\NoiseAsset_256x256_PNG.png")
$resourceContents = [IO.Path]::GetFullPath("$fullOutputPath\Resources")
$pfxPath = [IO.Path]::GetFullPath("..\MSTest.pfx")
Expand All @@ -259,6 +263,11 @@ Write-Host $makepriNew
cmd /c $makepriNew
if ($LastExitCode -ne 0) { Exit 1 }

$makepriNew = "`"" + (Join-Path $WindowsSdkBinDir "makepri.exe") + "`" new /pr $fullOutputPath /cf $priConfigPath /of $priCBSOutputPath /in Microsoft.UI.Xaml.CBS /o"
Write-Host $makepriNew
cmd /c $makepriNew
if ($LastExitCode -ne 0) { Exit 1 }

$outputAppxFileFullPath = Join-Path $fullOutputPath "$PackageName.appx"
$outputAppxFileFullPath = [IO.Path]::GetFullPath($outputAppxFileFullPath)

Expand Down
9 changes: 9 additions & 0 deletions dev/ResourceHelper/ResourceAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ winrt::ResourceMap ResourceAccessor::GetResourceMap()
winrt::hstring packageName{ MUXCONTROLS_PACKAGE_NAME };
return winrt::ResourceManager::Current().AllResourceMaps().Lookup(packageName);
}
else if (SharedHelpers::IsInCBSPackage())
{
winrt::hstring packageName{ MUXCONTROLS_CBS_PACKAGE_NAME };
return winrt::ResourceManager::Current().AllResourceMaps().Lookup(packageName);
}
else
{
return winrt::ResourceManager::Current().MainResourceMap();
Expand All @@ -39,6 +44,10 @@ winrt::LoadedImageSurface ResourceAccessor::GetImageSurface(const wstring_view &
{
return winrt::Uri{ std::wstring(L"ms-resource://" MUXCONTROLS_PACKAGE_NAME "/Files/Microsoft.UI.Xaml/Assets/") + std::wstring(assetName.data()) + std::wstring(L".png") };
}
else if (SharedHelpers::IsInCBSPackage())
{
return winrt::Uri{ std::wstring(L"ms-resource://" MUXCONTROLS_CBS_PACKAGE_NAME "/Files/Microsoft.UI.Xaml/Assets/") + std::wstring(assetName.data()) + std::wstring(L".png") };
}
else
{
return winrt::Uri{ std::wstring(L"ms-resource:///Files/Microsoft.UI.Xaml/Assets/") + std::wstring(assetName.data()) + std::wstring(L".png") };
Expand Down
40 changes: 24 additions & 16 deletions dev/dll/SharedHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,30 +257,38 @@ bool SharedHelpers::IsAPIContractV3Available()

void* __stdcall winrt_get_activation_factory(std::wstring_view const& name);

bool SharedHelpers::IsInFrameworkPackage()
bool IsInPackage(std::wstring_view detectorName)
{
static bool isInFrameworkPackage = []() {
// Special type that we manually list here which is not part of the Nuget dll distribution package.
// This is our breadcrumb that we leave to be able to detect at runtime that we're using the framework package.
// It's listed only in the Framework packages' AppxManifest.xml as an activatable type but only so
// that RoGetActivationFactory will change behavior and call our DllGetActivationFactory. It doesn't
// mater what comes back for the activationfactory. If it succeeds it means we're running against
// the framework package.
// Special type that we manually list here which is not part of the Nuget dll distribution package.
// This is our breadcrumb that we leave to be able to detect at runtime that we're using the framework package.
// It's listed only in the Framework packages' AppxManifest.xml as an activatable type but only so
// that RoGetActivationFactory will change behavior and call our DllGetActivationFactory. It doesn't
// matter what comes back for the activationfactory. If it succeeds it means we're running against
// the framework package.

winrt::hstring typeName{ L"Microsoft.UI.Private.Controls.FrameworkPackageDetector"sv};
winrt::IActivationFactory activationFactory;
winrt::hstring typeName{ detectorName };
winrt::IActivationFactory activationFactory;

if (SUCCEEDED(RoGetActivationFactory(static_cast<HSTRING>(winrt::get_abi(typeName)), winrt::guid_of<IActivationFactory>(), winrt::put_abi(activationFactory))))
{
return true;
}
if (SUCCEEDED(RoGetActivationFactory(static_cast<HSTRING>(winrt::get_abi(typeName)), winrt::guid_of<IActivationFactory>(), winrt::put_abi(activationFactory))))
{
return true;
}

return false;
}();
return false;
}

bool SharedHelpers::IsInFrameworkPackage()
{
static bool isInFrameworkPackage = IsInPackage(L"Microsoft.UI.Private.Controls.FrameworkPackageDetector"sv);
return isInFrameworkPackage;
}

bool SharedHelpers::IsInCBSPackage()
{
static bool isInCBSPackage = IsInPackage(L"Microsoft.UI.Private.Controls.CBSPackageDetector"sv);
return isInCBSPackage;
}

// Platform scale helpers
winrt::Rect SharedHelpers::ConvertDipsToPhysical(winrt::UIElement const& xamlRootReference, const winrt::Rect& dipsRect)
{
Expand Down
19 changes: 19 additions & 0 deletions dev/dll/XamlControlsResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void XamlControlsResources::UpdateSource()
const bool is19H1OrHigher = SharedHelpers::Is19H1OrHigher();

const bool isInFrameworkPackage = SharedHelpers::IsInFrameworkPackage();
const bool isInCBSPackage = SharedHelpers::IsInCBSPackage();

hstring compactPrefix = useCompactResources ? L"compact_" : L"";
hstring packagePrefix = L"ms-appx:///" MUXCONTROLSROOT_NAMESPACE_STR "/Themes/";
Expand All @@ -49,6 +50,10 @@ void XamlControlsResources::UpdateSource()
{
packagePrefix = L"ms-appx://" MUXCONTROLS_PACKAGE_NAME "/" MUXCONTROLSROOT_NAMESPACE_STR "/Themes/";
}
else if (isInCBSPackage)
{
packagePrefix = L"ms-appx://" MUXCONTROLS_CBS_PACKAGE_NAME "/" MUXCONTROLSROOT_NAMESPACE_STR "/Themes/";
}

hstring releasePrefix;

Expand Down Expand Up @@ -100,6 +105,8 @@ void SetDefaultStyleKeyWorker(winrt::IControlProtected const& controlProtected,
const bool is19H1OrHigher = SharedHelpers::Is19H1OrHigher();

const bool isInFrameworkPackage = SharedHelpers::IsInFrameworkPackage();
const bool isInCBSPackage = SharedHelpers::IsInCBSPackage();

if (isInFrameworkPackage)
{
if (is19H1OrHigher)
Expand All @@ -123,6 +130,18 @@ void SetDefaultStyleKeyWorker(winrt::IControlProtected const& controlProtected,
return L"ms-appx://" MUXCONTROLS_PACKAGE_NAME "/" MUXCONTROLSROOT_NAMESPACE_STR "/Themes/rs2_generic.xaml";
}
}
else if (isInCBSPackage)
{
if (is19H1OrHigher)
{
return L"ms-appx://" MUXCONTROLS_CBS_PACKAGE_NAME "/" MUXCONTROLSROOT_NAMESPACE_STR "/Themes/19h1_generic.xaml";
}
else
{
MUX_FAIL_FAST_MSG("CBS package doesn't apply to old platforms");
return L"ms-appx://" MUXCONTROLS_CBS_PACKAGE_NAME "/" MUXCONTROLSROOT_NAMESPACE_STR "/Themes/rs2_generic.xaml";
}
}
else
{
if (is19H1OrHigher)
Expand Down
3 changes: 2 additions & 1 deletion dev/dll/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ HRESULT WINAPI DllGetActivationFactory(_In_ HSTRING activatableClassId, _Out_ ::
uint32_t length{};
wchar_t const* const buffer = WindowsGetStringRawBuffer(activatableClassId, &length);
std::wstring_view const name{ buffer, length };
if (name == L"Microsoft.UI.Private.Controls.FrameworkPackageDetector"sv)
if (name == L"Microsoft.UI.Private.Controls.FrameworkPackageDetector"sv ||
name == L"Microsoft.UI.Private.Controls.CBSPackageDetector"sv)
{
winrt::hstring resources{L"Microsoft.UI.Xaml.Controls.XamlControlsResources"sv};
// It doesn't matter *what* we return so return a type that everyone uses.
Expand Down
2 changes: 2 additions & 0 deletions dev/inc/BuildMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
#else
#define MUXCONTROLS_PACKAGE_NAME L"Microsoft.UI.Xaml.2.5"
#endif

#define MUXCONTROLS_CBS_PACKAGE_NAME L"Microsoft.UI.Xaml.CBS"
1 change: 1 addition & 0 deletions dev/inc/SharedHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class SharedHelpers
static bool IsAPIContractV3Available(); // RS1

static bool IsInFrameworkPackage();
static bool IsInCBSPackage();

// Platform scale helpers
static winrt::Rect ConvertDipsToPhysical(winrt::UIElement const& xamlRootReference, const winrt::Rect& dipsRect);
Expand Down

0 comments on commit c9b5e60

Please sign in to comment.