From 9250f4d919bc800fe66a50a0874183458e6ebbbe Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 10 Sep 2018 23:58:34 +0800 Subject: ... --- CruUI/CruUI.vcxproj | 2 +- CruUI/CruUI.vcxproj.filters | 6 +++--- CruUI/application.h | 1 - CruUI/cru_event.cpp | 5 ----- CruUI/debug_base.h | 45 ++++++++++++++++++++++++++++++++++++++++ CruUI/graph/graph.cpp | 32 +--------------------------- CruUI/graph/graph.h | 35 ++++++++++++++++++++++++++----- CruUI/system_headers.h | 1 + CruUI/ui/control.cpp | 4 +--- CruUI/ui/controls/text_block.cpp | 23 +++++++------------- CruUI/ui/window.cpp | 1 - 11 files changed, 90 insertions(+), 65 deletions(-) delete mode 100644 CruUI/cru_event.cpp create mode 100644 CruUI/debug_base.h (limited to 'CruUI') 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 @@ + @@ -171,7 +172,6 @@ - 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 @@ Header Files + + Header Files + @@ -83,9 +86,6 @@ Source Files - - Source Files - Source Files 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 #include #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 +#include +#include + +#include "base.h" + +namespace cru::debug +{ +#ifdef CRU_DEBUG + inline void DebugTime(Function&& 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(after - before); + OutputDebugStringW(fmt::format(L"{}: {}ms.\n", hint_message, duration.count()).c_str()); + } + + template + TReturn DebugTime(Function&& 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(after - before); + OutputDebugStringW(fmt::format(L"{}: {}ms.\n", hint_message, duration.count()).c_str()); + return std::move(result); + } +#else + inline void DebugTime(Function&& action, const wchar_t* const hint_message) + { + action(); + } + + template + TReturn DebugTime(Function&& 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(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(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(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 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 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(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(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 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 #include 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(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(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 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_); } } -- cgit v1.2.3