Skip to content

Commit 95c5f93

Browse files
adpa-msAdib Parkar
andauthored
Add Mica (Win11) and Acrylic (Win10) backdrops (#348)
Co-authored-by: Adib Parkar <[email protected]>
1 parent ddf19ec commit 95c5f93

File tree

3 files changed

+119
-140
lines changed

3 files changed

+119
-140
lines changed

Samples/Islands/DrawingIsland/DrawingIslandComponents/Components.idl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ namespace DrawingIslandComponents
66
[default_interface]
77
runtimeclass DrawingIsland : Windows.Foundation.IClosable
88
{
9+
// Construction
910
DrawingIsland(
1011
Microsoft.UI.Composition.Compositor compositor);
1112

12-
// TODO: Enable Mica on Win 11
13-
#if FALSE
14-
Boolean UseSystemBackdrop { get; set; };
15-
#endif
13+
// Properties
14+
Boolean EnableBackgroundTransparency;
1615

1716
Microsoft.UI.Content.ContentIsland Island { get; };
1817

18+
// Methods
1919
void LeftClickAndRelease(
2020
Windows.Foundation.Numerics.Vector2 currentPoint);
2121

2222
void RightClickAndRelease(
2323
Windows.Foundation.Numerics.Vector2 currentPoint);
2424

25-
void SetBackroundOpacity(Single backgroundOpacity);
25+
void SetIslandOpacity(Single islandOpacity);
2626

2727
void SetColorIndex(UInt32 colorIndex);
2828
}

Samples/Islands/DrawingIsland/DrawingIslandComponents/DrawingIsland.cpp

Lines changed: 95 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ namespace winrt::DrawingIslandComponents::implementation
2222
// - When the ContentIsland is destroyed, ContentIsland.AppData will call IClosable.Close
2323
// on this instance to break cycles and clean up expensive resources.
2424

25-
m_background.Visual = m_output.Compositor.CreateSpriteVisual();
26-
m_background.Visual.RelativeSizeAdjustment(float2(1.0f, 1.0f));
25+
m_output.RootVisual = m_output.Compositor.CreateContainerVisual();
26+
m_output.RootVisual.RelativeSizeAdjustment({ 1.0f, 1.0f });
2727

28-
m_island = winrt::ContentIsland::Create(m_background.Visual);
28+
m_island = winrt::ContentIsland::Create(m_output.RootVisual);
2929
m_island.AppData(get_strong().as<winrt::IInspectable>());
3030

3131
Output_Initialize();
@@ -61,6 +61,8 @@ namespace winrt::DrawingIslandComponents::implementation
6161
void
6262
DrawingIsland::Close()
6363
{
64+
m_output.RootVisual = nullptr;
65+
6466
m_input.KeyboardSource = nullptr;
6567
m_input.PretranslateSource = nullptr;
6668
m_input.PointerSource = nullptr;
@@ -76,30 +78,18 @@ namespace winrt::DrawingIslandComponents::implementation
7678
m_items.Items.clear();
7779
m_items.SelectedItem = nullptr;
7880

79-
// TODO: Enable Mica on Win 11
80-
#if FALSE
81-
// Destroy SystemBackdropController objects.
82-
if (m_backdropController != nullptr)
81+
if (nullptr != m_background.BackdropController)
8382
{
84-
if (m_backdropConfiguration != nullptr)
85-
{
86-
// m_backdropConfiguration is only initialized for the DrawingIsland that owns the
87-
// SystemBackdropController.
88-
89-
m_backdropController.Close();
90-
m_backdropController = nullptr;
91-
m_backdropConfiguration = nullptr;
92-
}
93-
else
94-
{
95-
// We're closing a DrawingIsland in a popup, not the owner DrawingIsland of the
96-
// SystemBackdropController. Therefore, remove the current target from the
97-
// Controller.
83+
// Clean up our SystemBackdropController.
84+
m_background.BackdropController.RemoveAllSystemBackdropTargets();
85+
m_background.BackdropController.Close();
86+
m_background.BackdropController = nullptr;
87+
}
9888

99-
m_backdropController.RemoveSystemBackdropTarget(m_backdropTarget);
100-
}
89+
if (nullptr != m_background.BackdropConfiguration)
90+
{
91+
m_background.BackdropConfiguration = nullptr;
10192
}
102-
#endif
10393

10494
// Make sure automation is destroyed.
10595
for (auto& automationPeer : m_uia.AutomationPeers)
@@ -132,6 +122,17 @@ namespace winrt::DrawingIslandComponents::implementation
132122
}
133123

134124

125+
void DrawingIsland::EnableBackgroundTransparency(
126+
bool value)
127+
{
128+
if (value != m_background.BackdropEnabled)
129+
{
130+
m_background.BackdropEnabled = value;
131+
Output_UpdateSystemBackdrop();
132+
}
133+
}
134+
135+
135136
IFACEMETHODIMP
136137
DrawingIsland::OnDirectMessage(
137138
IInputPreTranslateKeyboardSourceInterop* /*source*/,
@@ -435,11 +436,19 @@ namespace winrt::DrawingIslandComponents::implementation
435436
switch (activationListener.State())
436437
{
437438
case winrt::InputActivationState::Activated:
438-
m_background.Visual.Opacity(1.0f);
439+
m_output.RootVisual.Opacity(1.0f);
440+
if (nullptr != m_background.BackdropConfiguration)
441+
{
442+
m_background.BackdropConfiguration.IsInputActive(true);
443+
}
439444
break;
440445

441446
default:
442-
m_background.Visual.Opacity(m_background.Opacity);
447+
m_output.RootVisual.Opacity(m_output.Opacity);
448+
if (nullptr != m_background.BackdropConfiguration)
449+
{
450+
m_background.BackdropConfiguration.IsInputActive(false);
451+
}
443452
break;
444453
}
445454
});
@@ -753,13 +762,13 @@ namespace winrt::DrawingIslandComponents::implementation
753762
// also mirrored. The text will need another RelativeOffsetAdjustment and Scale to
754763
// return to the normal space.
755764

756-
m_background.Visual.RelativeOffsetAdjustment(winrt::float3(1, 0, 0));
757-
m_background.Visual.Scale(winrt::float3(-1, 1, 1));
765+
m_output.RootVisual.RelativeOffsetAdjustment(winrt::float3(1, 0, 0));
766+
m_output.RootVisual.Scale(winrt::float3(-1, 1, 1));
758767
}
759768
else
760769
{
761-
m_background.Visual.RelativeOffsetAdjustment(winrt::float3(0, 0, 0));
762-
m_background.Visual.Scale(winrt::float3(1, 1, 1));
770+
m_output.RootVisual.RelativeOffsetAdjustment(winrt::float3(0, 0, 0));
771+
m_output.RootVisual.Scale(winrt::float3(1, 1, 1));
763772
}
764773
m_prevState.LayoutDirection = m_island.LayoutDirection();
765774
}
@@ -768,6 +777,25 @@ namespace winrt::DrawingIslandComponents::implementation
768777
void
769778
DrawingIsland::Output_Initialize()
770779
{
780+
// Create the background visual.
781+
m_background.Visual = m_output.Compositor.CreateSpriteVisual();
782+
m_background.Visual.RelativeSizeAdjustment({ 1.0f, 1.0f });
783+
784+
m_background.BrushDefault = m_output.Compositor.CreateColorBrush(
785+
winrt::Color{ 0xFF, 0x20, 0x20, 0x20 });
786+
787+
m_background.BrushA = m_output.Compositor.CreateColorBrush(
788+
winrt::Color{ 0xFF, 0x99, 0x20, 0x20 });
789+
790+
m_background.BrushB = m_output.Compositor.CreateColorBrush(
791+
winrt::Color{ 0xFF, 0x20, 0x99, 0x20 });
792+
793+
m_background.BrushC = m_output.Compositor.CreateColorBrush(
794+
winrt::Color{ 0xFF, 0x20, 0x20, 0x99 });
795+
796+
m_background.Visual.Brush(m_background.BrushDefault);
797+
m_output.RootVisual.Children().InsertAtTop(m_background.Visual);
798+
771799
for (int i = 0; i < _countof(m_output.ColorBrushes); i++)
772800
{
773801
m_output.ColorBrushes[i] = m_output.Compositor.CreateColorBrush(s_colors[i]);
@@ -780,13 +808,13 @@ namespace winrt::DrawingIslandComponents::implementation
780808

781809
winrt::ContainerVisual drawingVisualsRoot = m_output.Compositor.CreateContainerVisual();
782810
m_items.Visuals = drawingVisualsRoot.Children();
783-
m_background.Visual.Children().InsertAtTop(drawingVisualsRoot);
811+
m_output.RootVisual.Children().InsertAtTop(drawingVisualsRoot);
784812

785813
m_items.CurrentColorVisual = m_output.Compositor.CreateSpriteVisual();
786814
m_items.CurrentColorVisual.Offset({0.0f, 0.0f, 0.0f});
787-
m_background.Visual.Children().InsertAtTop(m_items.CurrentColorVisual);
815+
m_output.RootVisual.Children().InsertAtTop(m_items.CurrentColorVisual);
788816

789-
SystemBackdrop_EvaluateUsage();
817+
Output_UpdateSystemBackdrop();
790818

791819
Output_UpdateCurrentColorVisual();
792820
}
@@ -844,99 +872,53 @@ namespace winrt::DrawingIslandComponents::implementation
844872
}
845873

846874

847-
// TODO: Enable Mica on Win 11
848-
#if FALSE
849875
void
850-
DrawingIsland::SystemBackdrop_Initialize()
876+
DrawingIsland::Output_UpdateSystemBackdrop()
851877
{
852-
// Don't initilize system backdrop if we haven't been configured for it.
853-
if (!m_useSystemBackdrop) return;
854-
855-
if (m_backdropController == nullptr)
878+
if (m_background.BackdropEnabled && nullptr == m_background.BackdropController)
856879
{
857-
m_backdropConfiguration = winrt::SystemBackdropConfiguration();
858-
m_backdropController = winrt::DesktopAcrylicController();
859-
m_backdropController.SetSystemBackdropConfiguration(m_backdropConfiguration);
880+
// Reduce the opacity of the background color visual so that we can see
881+
// the Mica/DesktopAcrylic behind it.
882+
m_background.Visual.Opacity(0.1f);
860883

861-
auto activationListener = winrt::InputActivationListener::GetForIsland(m_island);
862-
(void)activationListener.InputActivationChanged(
863-
[this, activationListener](
864-
winrt::InputActivationListener const&,
865-
winrt::InputActivationListenerActivationChangedEventArgs const&)
884+
// Create a new SystemBackdropConfiguration if we do not already have one.
885+
if (nullptr == m_background.BackdropConfiguration)
866886
{
867-
switch (activationListener.State())
868-
{
869-
case winrt::InputActivationState::Activated:
870-
m_backdropConfiguration.IsInputActive(true);
871-
break;
872-
873-
default:
874-
m_backdropConfiguration.IsInputActive(false);
875-
break;
876-
}
877-
});
878-
}
879-
880-
881-
// If we are the main content, we don't want to add custom clips or offsets to our
882-
// backdrop, so we can pass the ContentIsland as the target to the BackdropController.
883-
// This will by default fill the entire ContentIsland backdrop surface.
884-
885-
m_backdropTarget = m_island;
886-
887-
m_backdropController.AddSystemBackdropTarget(m_backdropTarget);
888-
}
889-
#endif
887+
m_background.BackdropConfiguration = winrt::SystemBackdropConfiguration();
888+
}
890889

890+
// Decide between Mica and DesktopAcrylic.
891+
if (winrt::MicaController::IsSupported())
892+
{
893+
m_background.BackdropController = winrt::MicaController();
894+
895+
}
896+
else
897+
{
898+
m_background.BackdropController = winrt::DesktopAcrylicController();
899+
}
891900

892-
void
893-
DrawingIsland::SystemBackdrop_EvaluateUsage()
894-
{
895-
BYTE backgroundBrushOpacity = 0xFF;
901+
// Link the SystemBackdropConfiguration to our SystemBackdropController.
902+
m_background.BackdropController.SetSystemBackdropConfiguration(
903+
m_background.BackdropConfiguration);
896904

897-
// TODO: Enable Mica on Win 11
898-
#if FALSE
899-
// If we use system backdrops, it will be behind our background visual. Make sure we can
900-
// see through the background visual to reveal the system backdrop.
901-
// reveal the system backdrop underneath.
902-
if (m_useSystemBackdrop)
903-
{
904-
backgroundBrushOpacity = 0x30;
905+
// Add our island as the target.
906+
m_background.BackdropController.AddSystemBackdropTarget(m_island);
905907
}
906-
#endif
907-
908-
// Create the background parent Visual that the individual square will be added into.
909-
m_background.BrushDefault = m_output.Compositor.CreateColorBrush(
910-
winrt::Color{ backgroundBrushOpacity, 0x20, 0x20, 0x20 });
911-
912-
m_background.BrushA = m_output.Compositor.CreateColorBrush(
913-
winrt::Color{ backgroundBrushOpacity, 0x99, 0x20, 0x20 });
914-
915-
m_background.BrushB = m_output.Compositor.CreateColorBrush(
916-
winrt::Color{ backgroundBrushOpacity, 0x20, 0x99, 0x20 });
917-
918-
m_background.BrushC = m_output.Compositor.CreateColorBrush(
919-
winrt::Color{ backgroundBrushOpacity, 0x20, 0x20, 0x99 });
920-
921-
m_background.Visual.Brush(m_background.BrushDefault);
922-
}
923-
924-
925-
// TODO: Enable Mica on Win 11
926-
#if FALSE
927-
void
928-
DrawingIsland::UseSystemBackdrop(
929-
boolean value)
930-
{
931-
if (m_useSystemBackdrop != value)
908+
else
932909
{
933-
m_useSystemBackdrop = value;
910+
// Reset the background opacity to 1.0
911+
m_background.Visual.Opacity(1.0f);
934912

935-
SystemBackdrop_EvaluateUsage();
913+
if (nullptr != m_background.BackdropController)
914+
{
915+
// Clean up our SystemBackdropController.
916+
m_background.BackdropController.RemoveAllSystemBackdropTargets();
917+
m_background.BackdropController.Close();
918+
m_background.BackdropController = nullptr;
919+
}
936920
}
937921
}
938-
#endif
939-
940922

941923
void
942924
DrawingIsland::Environment_Initialize()

0 commit comments

Comments
 (0)