Skip to content

Commit

Permalink
🖌️ Refactor composition helpers (#17)
Browse files Browse the repository at this point in the history
Clean up some composition helpers to make it easier for future changes
to incorporate animations and customization functionality.
  • Loading branch information
haydenmc authored Jul 22, 2024
1 parent 5f8cfaf commit 3d65feb
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 246 deletions.
10 changes: 4 additions & 6 deletions src/FlashCom/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace FlashCom
{
SPDLOG_INFO("App::OnKeyDown - Navigating to {}", childNode->GetName());
m_dataModel->CurrentNode = childNode;
m_ui.Update();
m_ui.Update(View::UpdateReasonKind::Navigating);
}
else
{
Expand All @@ -175,9 +175,7 @@ namespace FlashCom
}

void App::OnKeyUp(uint8_t /*vkeyCode*/)
{

}
{ }

void App::Show()
{
Expand All @@ -192,7 +190,7 @@ namespace FlashCom
m_isShowing = false;
m_ui.Hide();
m_dataModel->CurrentNode = m_dataModel->RootNode.get();
m_ui.Update();
m_ui.Update(View::UpdateReasonKind::Hiding);
}

void App::OnSettingsCommand()
Expand All @@ -207,7 +205,7 @@ namespace FlashCom
{
SPDLOG_INFO("App::OnReloadCommand");
LoadDataModel();
m_ui.Update();
m_ui.Update(View::UpdateReasonKind::Reloading);
}

void App::OnExitCommand()
Expand Down
7 changes: 2 additions & 5 deletions src/FlashCom/FlashCom.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@
<ClCompile Include="Models\DataModel.cpp" />
<ClCompile Include="Models\TreeNode.cpp" />
<ClCompile Include="Settings\SettingsManager.cpp" />
<ClCompile Include="View\CompositionHost.cpp" />
<ClCompile Include="View\CompositionManager.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="View\TextVisual.cpp" />
<ClCompile Include="View\TrayIcon.cpp" />
<ClCompile Include="View\Ui.cpp" />
</ItemGroup>
Expand All @@ -132,10 +131,8 @@
<ClInclude Include="Models\DataModel.h" />
<ClInclude Include="Models\TreeNode.h" />
<ClInclude Include="Settings\SettingsManager.h" />
<ClInclude Include="View\CompositionHost.h" />
<ClInclude Include="View\CompositionManager.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="View\ICompositionVisual.h" />
<ClInclude Include="View\TextVisual.h" />
<ClInclude Include="View\TrayIcon.h" />
<ClInclude Include="View\Ui.h" />
</ItemGroup>
Expand Down
13 changes: 2 additions & 11 deletions src/FlashCom/FlashCom.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="View\CompositionHost.cpp">
<ClCompile Include="View\CompositionManager.cpp">
<Filter>Source Files\View</Filter>
</ClCompile>
<ClCompile Include="Input\LowLevelKeyboardHookHelper.cpp">
<Filter>Source Files\Input</Filter>
</ClCompile>
<ClCompile Include="View\TextVisual.cpp">
<Filter>Source Files\View</Filter>
</ClCompile>
<ClCompile Include="Settings\SettingsManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -83,18 +80,12 @@
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="View\CompositionHost.h">
<ClInclude Include="View\CompositionManager.h">
<Filter>Header Files\View</Filter>
</ClInclude>
<ClInclude Include="Input\LowLevelKeyboardHookHelper.h">
<Filter>Header Files\Input</Filter>
</ClInclude>
<ClInclude Include="View\ICompositionVisual.h">
<Filter>Header Files\View</Filter>
</ClInclude>
<ClInclude Include="View\TextVisual.h">
<Filter>Header Files\View</Filter>
</ClInclude>
<ClInclude Include="Settings\SettingsManager.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#include <pch.h>

#include "CompositionHost.h"
#include "TextVisual.h"

#include "CompositionManager.h"
#include <DispatcherQueue.h>


namespace
{
inline winrt::WS::DispatcherQueueController CreateDispatcherQueue()
Expand All @@ -28,9 +24,7 @@ namespace
}

inline winrt::WUICD::DesktopWindowTarget CreateDesktopWindowTarget(
HWND window,
winrt::WUIC::Compositor compositor
)
HWND window, winrt::WUIC::Compositor compositor)
{
namespace abi = ABI::Windows::UI::Composition::Desktop;

Expand All @@ -48,7 +42,7 @@ namespace
namespace FlashCom::View
{
#pragma region Public
CompositionHost::CompositionHost(HostWindow const& hostWindow) :
CompositionManager::CompositionManager(HostWindow const& hostWindow) :
m_hostWindow{ hostWindow },
m_dispatcherQueueController{ CreateDispatcherQueue() },
m_compositor{ winrt::WUIC::Compositor{} },
Expand All @@ -63,43 +57,51 @@ namespace FlashCom::View
}
{ }

winrt::WUIC::ContainerVisual CompositionHost::CreateRootVisual()
winrt::Windows::UI::Composition::Compositor CompositionManager::GetCompositor()
{
auto root{ m_compositor.CreateContainerVisual() };
root.RelativeSizeAdjustment({ 1.0f, 1.0f });
root.Offset({ 0, 0, 0 });

// Create background
// TODO: Allow this to be customized.
winrt::Windows::UI::Color bgColor{ 255, 128, 128, 128 };
winrt::MGCE::ColorSourceEffect bgColorEffect{};
bgColorEffect.Color(bgColor);
winrt::MGCE::BlendEffect blendEffect{};
blendEffect.Name(L"Blend");
blendEffect.Mode(winrt::MGCE::BlendEffectMode::LinearBurn);
blendEffect.Background(winrt::WUIC::CompositionEffectSourceParameter{ L"source" });
blendEffect.Foreground(bgColorEffect);
winrt::WUIC::CompositionEffectFactory blendEffectFactory{
m_compositor.CreateEffectFactory(blendEffect)
};
winrt::WUIC::CompositionEffectBrush blendBrush{
blendEffectFactory.CreateBrush()
};
auto backdropBrush{ m_compositor.CreateBackdropBrush() };
blendBrush.SetSourceParameter(L"source", backdropBrush);
auto backgroundVisual{ m_compositor.CreateSpriteVisual() };
backgroundVisual.Brush(blendBrush);
backgroundVisual.RelativeSizeAdjustment({ 1, 1 });
backgroundVisual.AnchorPoint({ 0.5, 0.5 });
backgroundVisual.RelativeOffsetAdjustment({ 0.5, 0.5, 0 });
root.Children().InsertAtBottom(backgroundVisual);

return root;
return m_compositor;
}

void CompositionHost::PresentRootVisual(winrt::WUIC::ContainerVisual rootVisual)
void CompositionManager::PresentRootVisual(winrt::WUIC::ContainerVisual rootVisual)
{
m_target.Root(rootVisual);
}

BrushWithBounds CompositionManager::CreateTextBrush(
winrt::MGCT::CanvasTextFormat textFormat, std::string_view content)
{
// Compute bounds of text
winrt::MGCT::CanvasTextLayout textLayout{
m_canvasDevice,
winrt::to_hstring(content),
textFormat,
0,
0
};
textLayout.WordWrapping(winrt::MGCT::CanvasWordWrapping::NoWrap);
auto textBounds{ textLayout.LayoutBounds() };

// Draw text
auto drawingSurface{
m_graphicsDevice.CreateDrawingSurface(
{ textBounds.Width, textBounds.Height },
winrt::WGDX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
winrt::WGDX::DirectXAlphaMode::Premultiplied
)
};
auto drawingSession{
winrt::MGCUC::CanvasComposition::CreateDrawingSession(drawingSurface)
};
drawingSession.Clear(winrt::WUI::Colors::Transparent());
drawingSession.DrawTextLayout(
textLayout,
{ 0, 0 },
winrt::WUI::Color{ 255, 192, 192, 192 }
);
return {
.Bounds = drawingSurface.Size(),
.Brush = m_compositor.CreateSurfaceBrush(drawingSurface)
};
}
#pragma endregion Public
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#pragma once
#include "HostWindow.h"
#include "ICompositionVisual.h"

#include <vector>

namespace FlashCom::View
{
class CompositionHost
struct BrushWithBounds
{
public:
CompositionHost(HostWindow const & hostWindow);
winrt::WF::Size Bounds;
winrt::WUIC::CompositionSurfaceBrush Brush;
};

winrt::Windows::UI::Composition::ContainerVisual CreateRootVisual();
class CompositionManager
{
public:
CompositionManager(HostWindow const & hostWindow);
winrt::Windows::UI::Composition::Compositor GetCompositor();
void PresentRootVisual(
winrt::Windows::UI::Composition::ContainerVisual rootVisual);

template<class C, class... Args>
std::unique_ptr<C> CreateVisual(Args... args)
{
return std::make_unique<C>(m_compositor, m_canvasDevice, m_graphicsDevice, args...);
}
BrushWithBounds CreateTextBrush(winrt::MGCT::CanvasTextFormat textFormat,
std::string_view content);

private:
HostWindow const & m_hostWindow;
Expand All @@ -31,7 +31,5 @@ namespace FlashCom::View
const winrt::Microsoft::Graphics::Canvas::CanvasDevice m_canvasDevice{ nullptr };
const winrt::Windows::UI::Composition::CompositionGraphicsDevice
m_graphicsDevice{ nullptr };

std::vector<std::unique_ptr<ICompositionVisual>> m_visuals;
};
}
12 changes: 0 additions & 12 deletions src/FlashCom/View/ICompositionVisual.h

This file was deleted.

107 changes: 0 additions & 107 deletions src/FlashCom/View/TextVisual.cpp

This file was deleted.

Loading

0 comments on commit 3d65feb

Please sign in to comment.