aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-05-23 23:50:00 +0800
committercrupest <crupest@outlook.com>2020-05-23 23:50:00 +0800
commitf3a8fd608a9776ef0a5f547da918a32cf6074060 (patch)
tree85b320479296ae12339ee1e28bab66ab001cb44b /src
parent75ff8a6a05afd02aaadf7e3049b0a0e305241182 (diff)
downloadcru-f3a8fd608a9776ef0a5f547da918a32cf6074060.tar.gz
cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.tar.bz2
cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/common/CMakeLists.txt6
-rw-r--r--src/common/logger.cpp6
-rw-r--r--src/ui/CMakeLists.txt2
-rw-r--r--src/ui/controls/text_box.cpp5
-rw-r--r--src/ui/controls/text_control_service.hpp34
-rw-r--r--src/ui/render/scroll_render_object.cpp1
-rw-r--r--src/win/exception.cpp28
-rw-r--r--src/win/graph/direct/painter.cpp17
-rw-r--r--src/win/native/window.cpp1
9 files changed, 63 insertions, 37 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 6e621bf1..43d1dedb 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -6,7 +6,6 @@ target_sources(cru_base PUBLIC
${CRU_BASE_INCLUDE_DIR}/base.hpp
${CRU_BASE_INCLUDE_DIR}/bitmask.hpp
${CRU_BASE_INCLUDE_DIR}/event.hpp
- ${CRU_BASE_INCLUDE_DIR}/format.hpp
${CRU_BASE_INCLUDE_DIR}/logger.hpp
${CRU_BASE_INCLUDE_DIR}/pre_config.hpp
${CRU_BASE_INCLUDE_DIR}/self_resolvable.hpp
@@ -14,5 +13,6 @@ target_sources(cru_base PUBLIC
target_include_directories(cru_base PUBLIC ${CRU_INCLUDE_DIR})
target_compile_definitions(cru_base PUBLIC $<$<CONFIG:Debug>:CRU_DEBUG>)
-find_path(GSL_INCLUDE_DIR NAMES gsl PATH_SUFFIXES ms-gsl)
-target_include_directories(cru_base PUBLIC ${GSL_INCLUDE_DIR})
+find_package(Microsoft.GSL CONFIG REQUIRED)
+find_package(fmt CONFIG REQUIRED)
+target_link_libraries(cru_base PUBLIC Microsoft.GSL::GSL fmt::fmt)
diff --git a/src/common/logger.cpp b/src/common/logger.cpp
index 6c1422f9..97599b0a 100644
--- a/src/common/logger.cpp
+++ b/src/common/logger.cpp
@@ -1,7 +1,5 @@
#include "cru/common/logger.hpp"
-#include "cru/common/format.hpp"
-
#include <array>
#include <cstdlib>
#include <ctime>
@@ -60,8 +58,8 @@ void Logger::Log(LogLevel level, const std::string_view &s) {
std::array<char, 50> buffer;
std::strftime(buffer.data(), 50, "%c", std::localtime(&now));
- source->Write(level, util::Format("[{}] {}: {}\n", buffer.data(),
- LogLevelToString(level), s));
+ source->Write(level, fmt::format("[{}] {}: {}\n", buffer.data(),
+ LogLevelToString(level), s));
}
}
} // namespace cru::log
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
index a8fd46a5..777e4fbc 100644
--- a/src/ui/CMakeLists.txt
+++ b/src/ui/CMakeLists.txt
@@ -25,6 +25,7 @@ add_library(cru_ui STATIC
render/flex_layout_render_object.cpp
render/layout_utility.cpp
render/render_object.cpp
+ render/scroll_render_object.cpp
render/stack_layout_render_object.cpp
render/text_render_object.cpp
render/window_render_object.cpp
@@ -54,6 +55,7 @@ target_sources(cru_ui PUBLIC
${CRU_UI_INCLUDE_DIR}/render/layout_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/layout_utility.hpp
${CRU_UI_INCLUDE_DIR}/render/render_object.hpp
+ ${CRU_UI_INCLUDE_DIR}/render/scroll_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/stack_layout_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/text_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/window_render_object.hpp
diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp
index 7b63eea1..8b7dc692 100644
--- a/src/ui/controls/text_box.cpp
+++ b/src/ui/controls/text_box.cpp
@@ -29,6 +29,7 @@ TextBox::TextBox() : border_render_object_(new BorderRenderObject()) {
service_ = std::make_unique<TextControlService<TextBox>>(this);
service_->SetEnabled(true);
+ service_->SetCaretVisible(true);
GainFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) {
this->service_->SetEnabled(true);
@@ -43,6 +44,10 @@ TextBox::TextBox() : border_render_object_(new BorderRenderObject()) {
TextBox::~TextBox() {}
+render::RenderObject* TextBox::GetRenderObject() const {
+ return border_render_object_.get();
+}
+
render::TextRenderObject* TextBox::GetTextRenderObject() {
return text_render_object_.get();
}
diff --git a/src/ui/controls/text_control_service.hpp b/src/ui/controls/text_control_service.hpp
index 57a6efa7..626423bf 100644
--- a/src/ui/controls/text_control_service.hpp
+++ b/src/ui/controls/text_control_service.hpp
@@ -10,8 +10,7 @@
#include "cru/ui/ui_event.hpp"
namespace cru::ui::controls {
-constexpr float caret_width = 2;
-constexpr long long caret_blink_duration = 500;
+constexpr int k_default_caret_blink_duration = 500;
// TControl should inherits `Control` and has following methods:
// ```
@@ -31,14 +30,11 @@ class TextControlService : public Object {
bool IsEnabled() { return enable_; }
void SetEnabled(bool enable);
- int GetCaretPosition() { return caret_position_; }
- void SetCaretPosition(int position) { caret_position_ = position; }
-
bool IsCaretVisible() { return caret_visible_; }
void SetCaretVisible(bool visible);
- // please set correct offset before calling this
- void DrawCaret(platform::graph::IPainter* painter);
+ int GetCaretBlinkDuration() { return caret_blink_duration_; }
+ void SetCaretBlinkDuration(int milliseconds);
private:
void AbortSelection();
@@ -60,8 +56,8 @@ class TextControlService : public Object {
bool enable_ = false;
bool caret_visible_ = false;
- int caret_position_ = 0;
long long caret_timer_id_ = -1;
+ int caret_blink_duration_ = k_default_caret_blink_duration;
// nullopt means not selecting
std::optional<MouseButton> select_down_button_;
@@ -105,12 +101,22 @@ void TextControlService<TControl>::SetCaretVisible(bool visible) {
if (this->enable_) {
if (visible) {
- this->SetupCaretTimer();
+ this->SetupCaret();
} else {
- this->TearDownCaretTimer();
+ this->TearDownCaret();
}
}
-} // namespace cru::ui::controls
+}
+
+template <typename TControl>
+void TextControlService<TControl>::SetCaretBlinkDuration(int milliseconds) {
+ if (this->caret_blink_duration_ == milliseconds) return;
+
+ if (this->enable_ && this->caret_visible_) {
+ this->TearDownCaret();
+ this->SetupCaret();
+ }
+}
template <typename TControl>
void TextControlService<TControl>::AbortSelection() {
@@ -130,7 +136,7 @@ void TextControlService<TControl>::SetupCaret() {
this->control_->GetTextRenderObject()->SetDrawCaret(true);
this->caret_timer_id_ = application->SetInterval(
- std::chrono::milliseconds(caret_blink_duration),
+ std::chrono::milliseconds(this->caret_blink_duration_),
[this] { this->control_->GetTextRenderObject()->ToggleDrawCaret(); });
}
@@ -169,7 +175,6 @@ void TextControlService<TControl>::MouseMoveHandler(
const auto result = text_render_object->TextHitTest(
text_render_object->FromRootToContent(args.GetPoint()));
const auto position = result.position + (result.trailing ? 1 : 0);
- this->caret_position_ = position;
log::Debug(
"TextControlService: Text selection changed on mouse move, range: {}, "
"{}.",
@@ -178,6 +183,7 @@ void TextControlService<TControl>::MouseMoveHandler(
TextRange::FromTwoSides(
static_cast<unsigned>(position),
static_cast<unsigned>(this->select_start_position_)));
+ text_render_object->SetCaretPosition(position);
}
}
@@ -194,6 +200,8 @@ void TextControlService<TControl>::MouseDownHandler(
const auto result = text_render_object->TextHitTest(
text_render_object->FromRootToContent(args.GetPoint()));
const auto position = result.position + (result.trailing ? 1 : 0);
+ text_render_object->SetSelectionRange(std::nullopt);
+ text_render_object->SetCaretPosition(position);
this->select_start_position_ = position;
log::Debug("TextControlService: Begin to select text, start position: {}.",
position);
diff --git a/src/ui/render/scroll_render_object.cpp b/src/ui/render/scroll_render_object.cpp
new file mode 100644
index 00000000..f77ee636
--- /dev/null
+++ b/src/ui/render/scroll_render_object.cpp
@@ -0,0 +1 @@
+#include "cru/ui/render/scroll_render_object.hpp"
diff --git a/src/win/exception.cpp b/src/win/exception.cpp
index e0f114b5..fde3ec6f 100644
--- a/src/win/exception.cpp
+++ b/src/win/exception.cpp
@@ -1,36 +1,32 @@
#include "cru/win/exception.hpp"
-#include "cru/common/format.hpp"
+#include <fmt/format.h>
+#include <optional>
namespace cru::platform::win {
-using util::Format;
inline std::string HResultMakeMessage(HRESULT h_result,
- const std::string_view* message) {
- char buffer[20];
- sprintf_s(buffer, "%#08x", h_result);
-
- if (message)
- return Format("HRESULT: {}.\nMessage: {}\n", buffer, *message);
+ std::optional<std::string_view> message) {
+ if (message.has_value())
+ return fmt::format(FMT_STRING("HRESULT: {:#08x}. Message: {}"), h_result,
+ *message);
else
- return Format("HRESULT: {}.\n", buffer);
+ return fmt::format(FMT_STRING("HRESULT: {:#08x}."), h_result);
}
HResultError::HResultError(HRESULT h_result)
- : PlatformException(HResultMakeMessage(h_result, nullptr)),
+ : PlatformException(HResultMakeMessage(h_result, std::nullopt)),
h_result_(h_result) {}
HResultError::HResultError(HRESULT h_result,
const std::string_view& additional_message)
- : PlatformException(HResultMakeMessage(h_result, &additional_message)),
+ : PlatformException(HResultMakeMessage(h_result, additional_message)),
h_result_(h_result) {}
inline std::string Win32MakeMessage(DWORD error_code,
- const std::string_view& message) {
- char buffer[20];
- sprintf_s(buffer, "%#04x", error_code);
-
- return Format("Last error code: {}.\nMessage: {}\n", buffer, message);
+ std::string_view message) {
+ return fmt::format("Last error code: {:#04x}.\nMessage: {}\n", error_code,
+ message);
}
Win32Error::Win32Error(const std::string_view& message)
diff --git a/src/win/graph/direct/painter.cpp b/src/win/graph/direct/painter.cpp
index c5150ad4..df0075e0 100644
--- a/src/win/graph/direct/painter.cpp
+++ b/src/win/graph/direct/painter.cpp
@@ -71,6 +71,23 @@ void D2DPainter::DrawText(const Point& offset, ITextLayout* text_layout,
b->GetD2DBrushInterface());
}
+void D2DPainter::PushLayer(const Rect& bounds) {
+ CheckValidation();
+
+ Microsoft::WRL::ComPtr<ID2D1Layer> layer;
+ ThrowIfFailed(render_target_->CreateLayer(&layer));
+
+ render_target_->PushLayer(D2D1::LayerParameters(Convert(bounds)),
+ layer.Get());
+
+ layers_.push_back(std::move(layer));
+}
+
+void D2DPainter::PopLayer() {
+ render_target_->PopLayer();
+ layers_.pop_back();
+}
+
void D2DPainter::EndDraw() {
if (is_drawing_) {
is_drawing_ = false;
diff --git a/src/win/native/window.cpp b/src/win/native/window.cpp
index bda8e764..bed9a264 100644
--- a/src/win/native/window.cpp
+++ b/src/win/native/window.cpp
@@ -1,6 +1,5 @@
#include "cru/win/native/window.hpp"
-#include "cru/common/format.hpp"
#include "cru/common/logger.hpp"
#include "cru/platform/check.hpp"
#include "cru/win/native/cursor.hpp"