aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-12-01 23:20:01 +0800
committercrupest <crupest@outlook.com>2020-12-01 23:20:01 +0800
commit0fb7a0e0b2b9e04ca414b1e47c69cc854c79831b (patch)
tree46d7f93e96bca49b0ec114c5595567b9bbcec6e3 /src
parent4fcf336d15fe246259ee18ccc99808d80e69c455 (diff)
downloadcru-0fb7a0e0b2b9e04ca414b1e47c69cc854c79831b.tar.gz
cru-0fb7a0e0b2b9e04ca414b1e47c69cc854c79831b.tar.bz2
cru-0fb7a0e0b2b9e04ca414b1e47c69cc854c79831b.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/ui/CMakeLists.txt4
-rw-r--r--src/ui/helper/Styler.cpp27
-rw-r--r--src/ui/style/Styler.cpp0
-rw-r--r--src/ui/style/Trigger.cpp49
4 files changed, 51 insertions, 29 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
index 974a3959..5cb50ce3 100644
--- a/src/ui/CMakeLists.txt
+++ b/src/ui/CMakeLists.txt
@@ -24,7 +24,6 @@ add_library(cru_ui STATIC
helper/BorderStyle.cpp
helper/ClickDetector.cpp
helper/ShortcutHub.cpp
- helper/Styler.cpp
host/LayoutPaintCycler.cpp
host/WindowHost.cpp
render/BorderRenderObject.cpp
@@ -35,6 +34,7 @@ add_library(cru_ui STATIC
render/ScrollRenderObject.cpp
render/StackLayoutRenderObject.cpp
render/TextRenderObject.cpp
+ style/Trigger.cpp
)
target_sources(cru_ui PUBLIC
${CRU_UI_INCLUDE_DIR}/Base.hpp
@@ -58,7 +58,6 @@ target_sources(cru_ui PUBLIC
${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
@@ -72,5 +71,6 @@ target_sources(cru_ui PUBLIC
${CRU_UI_INCLUDE_DIR}/render/ScrollRenderObject.hpp
${CRU_UI_INCLUDE_DIR}/render/StackLayoutRenderObject.hpp
${CRU_UI_INCLUDE_DIR}/render/TextRenderObject.hpp
+ ${CRU_UI_INCLUDE_DIR}/style/Trigger.hpp
)
target_link_libraries(cru_ui PUBLIC cru_platform_gui)
diff --git a/src/ui/helper/Styler.cpp b/src/ui/helper/Styler.cpp
deleted file mode 100644
index 6500a3f7..00000000
--- a/src/ui/helper/Styler.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#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
diff --git a/src/ui/style/Styler.cpp b/src/ui/style/Styler.cpp
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/src/ui/style/Styler.cpp
diff --git a/src/ui/style/Trigger.cpp b/src/ui/style/Trigger.cpp
new file mode 100644
index 00000000..b7292ce3
--- /dev/null
+++ b/src/ui/style/Trigger.cpp
@@ -0,0 +1,49 @@
+#include "cru/ui/style/Trigger.hpp"
+
+#include "cru/ui/controls/Control.hpp"
+#include "cru/ui/helper/ClickDetector.hpp"
+
+namespace cru::ui::style {
+void Trigger::Raise(bool value) {
+ if (value == current_) return;
+ current_ = value;
+ change_event_.Raise(value);
+}
+
+CompoundTrigger::CompoundTrigger(std::vector<Trigger*> triggers)
+ : triggers_(std::move(triggers)) {
+ for (auto trigger : triggers_) {
+ guard_ += trigger->ChangeEvent()->AddHandler(
+ [this](bool) { Raise(this->CalculateState(triggers_)); });
+ }
+}
+
+bool AndTrigger::CalculateState(const std::vector<Trigger*>& triggers) const {
+ for (auto trigger : triggers) {
+ if (!trigger->GetState()) return false;
+ }
+ return true;
+}
+
+bool OrTrigger::CalculateState(const std::vector<Trigger*>& triggers) const {
+ for (auto trigger : triggers) {
+ if (trigger->GetState()) return true;
+ }
+ return false;
+}
+
+FocusTrigger::FocusTrigger(controls::Control* control, bool has_focus)
+ : has_focus_(has_focus) {
+ guard_ += control->GainFocusEvent()->Direct()->AddHandler(
+ [this](auto) { Raise(has_focus_); });
+ guard_ += control->LoseFocusEvent()->Direct()->AddHandler(
+ [this](auto) { Raise(!has_focus_); });
+}
+
+ClickStateTrigger::ClickStateTrigger(controls::IClickableControl* control,
+ helper::ClickState click_state)
+ : click_state_(click_state) {
+ guard_ += control->ClickStateChangeEvent()->AddHandler(
+ [this](helper::ClickState cs) { Raise(cs == click_state_); });
+}
+} // namespace cru::ui::style