aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/ui/helper/BorderStyle.hpp23
-rw-r--r--include/cru/ui/helper/Styler.hpp47
-rw-r--r--src/ui/CMakeLists.txt4
-rw-r--r--src/ui/helper/BorderStyle.cpp0
-rw-r--r--src/ui/helper/Styler.cpp27
5 files changed, 101 insertions, 0 deletions
diff --git a/include/cru/ui/helper/BorderStyle.hpp b/include/cru/ui/helper/BorderStyle.hpp
new file mode 100644
index 00000000..0ec0d9ee
--- /dev/null
+++ b/include/cru/ui/helper/BorderStyle.hpp
@@ -0,0 +1,23 @@
+#pragma once
+#include "cru/ui/Base.hpp"
+
+#include <optional>
+
+namespace cru::ui::helper {
+struct BorderStyleOfClickState {
+ BorderStyleOfClickState(std::optional<BorderStyle> focus = std::nullopt,
+ std::optional<BorderStyle> not_focus = std::nullopt)
+ : focus(std::move(focus)), not_focus(std::move(not_focus)) {}
+
+ std::optional<BorderStyle> focus;
+ std::optional<BorderStyle> not_focus;
+};
+
+struct BorderStyleList {
+ BorderStyle default_one;
+ std::optional<BorderStyleOfClickState> normal;
+ std::optional<BorderStyleOfClickState> hover;
+ std::optional<BorderStyleOfClickState> press;
+ std::optional<BorderStyleOfClickState> press_inactive;
+};
+} // namespace cru::ui::helper
diff --git a/include/cru/ui/helper/Styler.hpp b/include/cru/ui/helper/Styler.hpp
new file mode 100644
index 00000000..ed8bfbdc
--- /dev/null
+++ b/include/cru/ui/helper/Styler.hpp
@@ -0,0 +1,47 @@
+#pragma once
+#include "cru/common/Base.hpp"
+#include "cru/common/Event.hpp"
+#include "cru/ui/Base.hpp"
+#include "cru/ui/helper/ClickDetector.hpp"
+#include "gsl/pointers"
+
+#include <memory>
+
+namespace cru::ui::helper {
+struct ControlStyleState {
+ ClickState click_state;
+ bool focus;
+};
+
+class Styler : public Object {
+ public:
+ // You could provide your click detector. Otherwise a new one will be created.
+ explicit Styler(gsl::not_null<controls::Control*> control,
+ ClickDetector* click_detector = nullptr);
+
+ CRU_DELETE_COPY(Styler)
+ CRU_DELETE_MOVE(Styler)
+
+ ~Styler();
+
+ public:
+ gsl::not_null<controls::Control*> GetControl() const { return control_; }
+ gsl::not_null<ClickDetector*> GetClickDetector() const {
+ return click_detector_;
+ }
+
+ IEvent<ControlStyleState>* StateChangeEvent() { return &state_change_event_; }
+
+ private:
+ void RaiseStateChangeEvent();
+
+ private:
+ gsl::not_null<controls::Control*> control_;
+ std::unique_ptr<ClickDetector> managed_click_detector_;
+ gsl::not_null<ClickDetector*> click_detector_;
+
+ Event<ControlStyleState> state_change_event_;
+
+ EventRevokerListGuard event_guard_;
+};
+} // namespace cru::ui::helper
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
index d9edf49e..974a3959 100644
--- a/src/ui/CMakeLists.txt
+++ b/src/ui/CMakeLists.txt
@@ -21,8 +21,10 @@ add_library(cru_ui STATIC
controls/TextControlService.hpp
controls/Window.cpp
events/UiEvent.cpp
+ helper/BorderStyle.cpp
helper/ClickDetector.cpp
helper/ShortcutHub.cpp
+ helper/Styler.cpp
host/LayoutPaintCycler.cpp
host/WindowHost.cpp
render/BorderRenderObject.cpp
@@ -53,8 +55,10 @@ target_sources(cru_ui PUBLIC
${CRU_UI_INCLUDE_DIR}/controls/TextBlock.hpp
${CRU_UI_INCLUDE_DIR}/controls/Window.hpp
${CRU_UI_INCLUDE_DIR}/events/UiEvent.hpp
+ ${CRU_UI_INCLUDE_DIR}/helper/BorderStyle.hpp
${CRU_UI_INCLUDE_DIR}/helper/ClickDetector.hpp
${CRU_UI_INCLUDE_DIR}/helper/ShortcutHub.hpp
+ ${CRU_UI_INCLUDE_DIR}/helper/Styler.hpp
${CRU_UI_INCLUDE_DIR}/host/LayoutPaintCycler.hpp
${CRU_UI_INCLUDE_DIR}/host/WindowHost.hpp
${CRU_UI_INCLUDE_DIR}/render/Base.hpp
diff --git a/src/ui/helper/BorderStyle.cpp b/src/ui/helper/BorderStyle.cpp
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/src/ui/helper/BorderStyle.cpp
diff --git a/src/ui/helper/Styler.cpp b/src/ui/helper/Styler.cpp
new file mode 100644
index 00000000..6500a3f7
--- /dev/null
+++ b/src/ui/helper/Styler.cpp
@@ -0,0 +1,27 @@
+#include "cru/ui/helper/Styler.hpp"
+#include "cru/ui/helper/ClickDetector.hpp"
+#include "gsl/pointers"
+
+namespace cru::ui::helper {
+Styler::Styler(gsl::not_null<controls::Control*> control,
+ ClickDetector* click_detector)
+ : control_(control),
+ managed_click_detector_(click_detector ? nullptr
+ : new ClickDetector(control)),
+ click_detector_(click_detector ? click_detector
+ : managed_click_detector_.get()) {
+ event_guard_ += control_->GainFocusEvent()->Direct()->AddHandler(
+ [this](auto) { this->RaiseStateChangeEvent(); });
+ event_guard_ += control_->LoseFocusEvent()->Direct()->AddHandler(
+ [this](auto) { this->RaiseStateChangeEvent(); });
+ event_guard_ += click_detector_->StateChangeEvent()->AddHandler(
+ [this](auto) { this->RaiseStateChangeEvent(); });
+}
+
+Styler::~Styler() = default;
+
+void Styler::RaiseStateChangeEvent() {
+ this->state_change_event_.Raise(ControlStyleState{
+ this->click_detector_->GetState(), this->control_->HasFocus()});
+}
+} // namespace cru::ui::helper