aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-10 23:58:34 +0800
committercrupest <crupest@outlook.com>2018-09-10 23:58:34 +0800
commit9250f4d919bc800fe66a50a0874183458e6ebbbe (patch)
tree1d25285801a6d92cfa8bde6dd0d13433e05d4c04
parentaa8ba64f4f580552ba14325dd3e04f38a3c9a1de (diff)
downloadcru-9250f4d919bc800fe66a50a0874183458e6ebbbe.tar.gz
cru-9250f4d919bc800fe66a50a0874183458e6ebbbe.tar.bz2
cru-9250f4d919bc800fe66a50a0874183458e6ebbbe.zip
...
-rw-r--r--CruUI/CruUI.vcxproj2
-rw-r--r--CruUI/CruUI.vcxproj.filters6
-rw-r--r--CruUI/application.h1
-rw-r--r--CruUI/cru_event.cpp5
-rw-r--r--CruUI/debug_base.h45
-rw-r--r--CruUI/graph/graph.cpp32
-rw-r--r--CruUI/graph/graph.h35
-rw-r--r--CruUI/system_headers.h1
-rw-r--r--CruUI/ui/control.cpp4
-rw-r--r--CruUI/ui/controls/text_block.cpp23
-rw-r--r--CruUI/ui/window.cpp1
11 files changed, 90 insertions, 65 deletions
diff --git a/CruUI/CruUI.vcxproj b/CruUI/CruUI.vcxproj
index 62b23165..7bf7eb98 100644
--- a/CruUI/CruUI.vcxproj
+++ b/CruUI/CruUI.vcxproj
@@ -156,6 +156,7 @@
<ClInclude Include="application.h" />
<ClInclude Include="base.h" />
<ClInclude Include="cru_event.h" />
+ <ClInclude Include="debug_base.h" />
<ClInclude Include="exception.h" />
<ClInclude Include="graph\graph.h" />
<ClInclude Include="system_headers.h" />
@@ -171,7 +172,6 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="application.cpp" />
- <ClCompile Include="cru_event.cpp" />
<ClCompile Include="exception.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="graph\graph.cpp" />
diff --git a/CruUI/CruUI.vcxproj.filters b/CruUI/CruUI.vcxproj.filters
index a282cfda..ec7a6de9 100644
--- a/CruUI/CruUI.vcxproj.filters
+++ b/CruUI/CruUI.vcxproj.filters
@@ -60,6 +60,9 @@
<ClInclude Include="ui\controls\linear_layout.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="debug_base.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="application.cpp">
@@ -83,9 +86,6 @@
<ClCompile Include="timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="cru_event.cpp">
- <Filter>Source Files</Filter>
- </ClCompile>
<ClCompile Include="ui\ui_base.cpp">
<Filter>Source Files</Filter>
</ClCompile>
diff --git a/CruUI/application.h b/CruUI/application.h
index daea7e06..be5d21ba 100644
--- a/CruUI/application.h
+++ b/CruUI/application.h
@@ -1,7 +1,6 @@
#pragma once
#include "system_headers.h"
-#include <functional>
#include <memory>
#include "base.h"
diff --git a/CruUI/cru_event.cpp b/CruUI/cru_event.cpp
deleted file mode 100644
index 3977b3b2..00000000
--- a/CruUI/cru_event.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-#include "cru_event.h"
-
-namespace cru {
-
-}
diff --git a/CruUI/debug_base.h b/CruUI/debug_base.h
new file mode 100644
index 00000000..f78decbd
--- /dev/null
+++ b/CruUI/debug_base.h
@@ -0,0 +1,45 @@
+#pragma once
+
+
+#include "system_headers.h"
+#include <type_traits>
+#include <chrono>
+#include <fmt/format.h>
+
+#include "base.h"
+
+namespace cru::debug
+{
+#ifdef CRU_DEBUG
+ inline void DebugTime(Function<void()>&& action, const wchar_t* const hint_message)
+ {
+ const auto before = std::chrono::steady_clock::now();
+ action();
+ const auto after = std::chrono::steady_clock::now();
+ const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
+ OutputDebugStringW(fmt::format(L"{}: {}ms.\n", hint_message, duration.count()).c_str());
+ }
+
+ template<typename TReturn>
+ TReturn DebugTime(Function<TReturn()>&& action, const wchar_t* const hint_message)
+ {
+ const auto before = std::chrono::steady_clock::now();
+ auto&& result = action();
+ const auto after = std::chrono::steady_clock::now();
+ const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
+ OutputDebugStringW(fmt::format(L"{}: {}ms.\n", hint_message, duration.count()).c_str());
+ return std::move(result);
+ }
+#else
+ inline void DebugTime(Function<void()>&& action, const wchar_t* const hint_message)
+ {
+ action();
+ }
+
+ template<typename TReturn>
+ TReturn DebugTime(Function<TReturn()>&& action, const wchar_t* const hint_message)
+ {
+ return action();
+ }
+#endif
+}
diff --git a/CruUI/graph/graph.cpp b/CruUI/graph/graph.cpp
index 4f65a86c..00d5137d 100644
--- a/CruUI/graph/graph.cpp
+++ b/CruUI/graph/graph.cpp
@@ -200,7 +200,7 @@ namespace cru {
return std::make_shared<WindowRenderTarget>(this, hwnd);
}
- Dpi GraphManager::GetDpi()
+ Dpi GraphManager::GetDpi() const
{
Dpi dpi;
d2d1_factory_->GetDesktopDpi(&dpi.x, &dpi.y);
@@ -213,35 +213,5 @@ namespace cru {
d2d1_factory_->ReloadSystemMetrics()
);
}
-
- inline int DipToPixelInternal(float dip, float dpi)
- {
- return static_cast<int>(dip * dpi / 96.0f);
- }
-
- int DipToPixelX(float dipX)
- {
- return DipToPixelInternal(dipX, Application::GetInstance()->GetGraphManager()->GetDpi().x);
- }
-
- int DipToPixelY(float dipY)
- {
- return DipToPixelInternal(dipY, Application::GetInstance()->GetGraphManager()->GetDpi().y);
- }
-
- inline float DipToPixelInternal(int pixel, float dpi)
- {
- return static_cast<float>(pixel) * 96.0f / dpi;
- }
-
- float PixelToDipX(int pixelX)
- {
- return DipToPixelInternal(pixelX, Application::GetInstance()->GetGraphManager()->GetDpi().x);
- }
-
- float PixelToDipY(int pixelY)
- {
- return DipToPixelInternal(pixelY, Application::GetInstance()->GetGraphManager()->GetDpi().y);
- }
}
}
diff --git a/CruUI/graph/graph.h b/CruUI/graph/graph.h
index bb1de716..4f0898d6 100644
--- a/CruUI/graph/graph.h
+++ b/CruUI/graph/graph.h
@@ -107,7 +107,7 @@ namespace cru
std::shared_ptr<WindowRenderTarget> CreateWindowRenderTarget(HWND hwnd);
//Get the desktop dpi.
- Dpi GetDpi();
+ Dpi GetDpi() const;
//Reload system metrics including desktop dpi.
void ReloadSystemMetrics();
@@ -129,10 +129,35 @@ namespace cru
Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_;
};
- int DipToPixelX(float dip_x);
- int DipToPixelY(float dip_y);
- float PixelToDipX(int pixel_x);
- float PixelToDipY(int pixel_y);
+ inline int DipToPixelInternal(const float dip, const float dpi)
+ {
+ return static_cast<int>(dip * dpi / 96.0f);
+ }
+
+ inline int DipToPixelX(const float dip_x)
+ {
+ return DipToPixelInternal(dip_x, Application::GetInstance()->GetGraphManager()->GetDpi().x);
+ }
+
+ inline int DipToPixelY(const float dip_y)
+ {
+ return DipToPixelInternal(dip_y, Application::GetInstance()->GetGraphManager()->GetDpi().y);
+ }
+
+ inline float DipToPixelInternal(const int pixel, const float dpi)
+ {
+ return static_cast<float>(pixel) * 96.0f / dpi;
+ }
+
+ inline float PixelToDipX(const int pixel_x)
+ {
+ return DipToPixelInternal(pixel_x, Application::GetInstance()->GetGraphManager()->GetDpi().x);
+ }
+
+ inline float PixelToDipY(const int pixel_y)
+ {
+ return DipToPixelInternal(pixel_y, Application::GetInstance()->GetGraphManager()->GetDpi().y);
+ }
Microsoft::WRL::ComPtr<ID2D1DeviceContext> WindowRenderTarget::GetD2DDeviceContext() const
{
diff --git a/CruUI/system_headers.h b/CruUI/system_headers.h
index bd33b5f6..99c091e1 100644
--- a/CruUI/system_headers.h
+++ b/CruUI/system_headers.h
@@ -4,6 +4,7 @@
//include system headers
#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <windowsx.h>
diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp
index 507beee8..73981891 100644
--- a/CruUI/ui/control.cpp
+++ b/CruUI/ui/control.cpp
@@ -6,6 +6,7 @@
#include "window.h"
#include "timer.h"
+#include "debug_base.h"
namespace cru {
namespace ui {
@@ -257,12 +258,9 @@ namespace cru {
void Control::Layout(const Rect& rect)
{
- auto before = std::chrono::steady_clock::now();
SetPositionRelative(rect.GetLeftTop());
SetSize(rect.GetSize());
OnLayout(rect);
- auto after = std::chrono::steady_clock::now();
- OutputDebugStringW((L"Layout time duration:" + std::to_wstring(std::chrono::duration_cast<std::chrono::milliseconds>(after - before).count()) + L"\n").c_str());
}
Size Control::GetDesiredSize() const
diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp
index d15bd681..1201fac6 100644
--- a/CruUI/ui/controls/text_block.cpp
+++ b/CruUI/ui/controls/text_block.cpp
@@ -5,6 +5,7 @@
#include "ui/window.h"
#include "graph/graph.h"
#include "exception.h"
+#include "debug_base.h"
namespace cru
{
@@ -52,12 +53,10 @@ namespace cru
void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args)
{
- auto before = std::chrono::steady_clock::now();
- RecreateTextLayout();
+ text_layout_->SetMaxWidth(args.GetNewSize().width);
+ text_layout_->SetMaxHeight(args.GetNewSize().height);
Repaint();
- auto after = std::chrono::steady_clock::now();
- OutputDebugStringW((L"TextBlock OnSizeChangedCore time duration:" + std::to_wstring(std::chrono::duration_cast<std::chrono::milliseconds>(after - before).count()) + L"\n").c_str());
-
+ Control::OnSizeChangedCore(args);
}
void TextBlock::OnDraw(ID2D1DeviceContext* device_context)
@@ -73,7 +72,6 @@ namespace cru
const auto layout_params = GetLayoutParams();
-
if (layout_params->width.mode == MeasureMode::Stretch && layout_params->height.mode == MeasureMode::Stretch)
return available_size;
@@ -100,17 +98,12 @@ namespace cru
measure_size.width = get_measure_length(layout_params->width, available_size.width);
measure_size.height = get_measure_length(layout_params->height, available_size.height);
-
- Microsoft::WRL::ComPtr<IDWriteTextLayout> measure_text_layout;
-
- const auto dwrite_factory = GetDWriteFactory();
-
- ThrowIfFailed(dwrite_factory->CreateTextLayout(text_.c_str(), text_.size(),
- text_format_.Get(), measure_size.width, measure_size.height, &measure_text_layout)
- );
+ ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width));
+ ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height));
DWRITE_TEXT_METRICS metrics{};
- measure_text_layout->GetMetrics(&metrics);
+
+ ThrowIfFailed(text_layout_->GetMetrics(&metrics));
const Size measure_result(metrics.width, metrics.height);
diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp
index 392bca87..3a9393ff 100644
--- a/CruUI/ui/window.cpp
+++ b/CruUI/ui/window.cpp
@@ -165,7 +165,6 @@ namespace cru
void Window::Repaint() {
if (IsWindowValid()) {
InvalidateRect(hwnd_, nullptr, false);
- UpdateWindow(hwnd_);
}
}