aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/theme_builder/components/StyleRuleSetEditor.cpp36
-rw-r--r--src/theme_builder/components/StyleRuleSetEditor.h6
-rw-r--r--src/ui/helper/ClickDetector.cpp10
-rw-r--r--src/ui/host/RoutedEventDispatch.h32
4 files changed, 52 insertions, 32 deletions
diff --git a/src/theme_builder/components/StyleRuleSetEditor.cpp b/src/theme_builder/components/StyleRuleSetEditor.cpp
index dc09125f..8c64f9cf 100644
--- a/src/theme_builder/components/StyleRuleSetEditor.cpp
+++ b/src/theme_builder/components/StyleRuleSetEditor.cpp
@@ -1,5 +1,7 @@
#include "StyleRuleSetEditor.h"
+#include "cru/ui/ThemeManager.h"
#include "cru/ui/controls/FlexLayout.h"
+#include "cru/ui/render/FlexLayoutRenderObject.h"
#include "cru/ui/style/Condition.h"
#include "cru/ui/style/Styler.h"
@@ -8,20 +10,25 @@ using namespace cru::ui::controls;
StyleRuleSetEditor::StyleRuleSetEditor() {
scroll_view_.SetChild(&container_);
- rules_layout_.SetFlexDirection(ui::controls::FlexDirection::Vertical);
+ container_.SetFlexDirection(ui::render::FlexDirection::Vertical);
container_.AddChild(&rules_layout_);
container_.AddChild(&add_button_);
- add_button_.SetChild(&add_button_text_);
- add_button_text_.SetText(u"+");
+ rules_layout_.SetFlexDirection(ui::controls::FlexDirection::Vertical);
+
+ add_button_.GetStyleRuleSet()->SetParent(
+ ui::ThemeManager::GetInstance()->GetResourceStyleRuleSet(
+ u"cru.theme_builder.icon-button.style"));
+ add_button_.SetIconWithSvgPathDataStringResourceKey(u"icon.plus",
+ {0, 0, 16, 16});
+ add_button_.SetPreferredSize({24, 24});
+ add_button_.SetPadding(ui::Thickness(2));
+ add_button_.SetIconFillColor(ui::colors::green);
add_button_.ClickEvent()->AddSpyOnlyHandler([this] {
auto rule_set = ui::style::StyleRule(ui::style::NoCondition::Create(),
ui::style::CompoundStyler::Create({}));
style_rule_set_->AddStyleRule(rule_set);
- style_rule_editors_.push_back(std::make_unique<StyleRuleEditor>());
- style_rule_editors_.back()->SetValue(rule_set);
- rules_layout_.AddChild(style_rule_editors_.back()->GetRootControl());
});
}
@@ -29,19 +36,22 @@ StyleRuleSetEditor::~StyleRuleSetEditor() {}
void StyleRuleSetEditor::BindStyleRuleSet(
std::shared_ptr<ui::style::StyleRuleSet> rule_set) {
+ Expects(style_rule_set_ == nullptr && rule_set);
style_rule_set_ = std::move(rule_set);
+ UpdateView(style_rule_set_.get());
+ style_rule_set_->ChangeEvent()->AddSpyOnlyHandler(
+ [this] { UpdateView(style_rule_set_.get()); });
+}
- rules_layout_.ClearChildren();
+void StyleRuleSetEditor::UpdateView(ui::style::StyleRuleSet* style_rule_set) {
style_rule_editors_.clear();
- for (Index i = 0; i < style_rule_set_->GetSize(); ++i) {
- const auto& rule = style_rule_set_->GetStyleRule(i);
+ for (Index i = 0; i < style_rule_set->GetSize(); ++i) {
+ const auto& rule = style_rule_set->GetStyleRule(i);
auto style_rule_editor = std::make_unique<StyleRuleEditor>();
style_rule_editor->SetValue(rule, false);
- style_rule_editor->RemoveEvent()->AddSpyOnlyHandler([this, i] {
- style_rule_set_->RemoveStyleRule(i);
- style_rule_editors_.erase(style_rule_editors_.begin() + i);
- });
+ style_rule_editor->RemoveEvent()->AddSpyOnlyHandler(
+ [this, i] { style_rule_set_->RemoveStyleRule(i); });
style_rule_editor->ChangeEvent()->AddSpyOnlyHandler(
[this, i, editor = style_rule_editor.get()]() {
style_rule_set_->SetStyleRule(i, editor->GetValue());
diff --git a/src/theme_builder/components/StyleRuleSetEditor.h b/src/theme_builder/components/StyleRuleSetEditor.h
index 3ffad77f..1f2ae216 100644
--- a/src/theme_builder/components/StyleRuleSetEditor.h
+++ b/src/theme_builder/components/StyleRuleSetEditor.h
@@ -23,13 +23,15 @@ class StyleRuleSetEditor : public ui::components::Component {
void BindStyleRuleSet(std::shared_ptr<ui::style::StyleRuleSet> rule_set);
private:
+ void UpdateView(ui::style::StyleRuleSet* style_rule_set);
+
+ private:
std::shared_ptr<ui::style::StyleRuleSet> style_rule_set_;
ui::controls::ScrollView scroll_view_;
ui::controls::FlexLayout container_;
ui::controls::FlexLayout rules_layout_;
std::vector<std::unique_ptr<StyleRuleEditor>> style_rule_editors_;
- ui::controls::Button add_button_;
- ui::controls::TextBlock add_button_text_;
+ ui::controls::IconButton add_button_;
};
} // namespace cru::theme_builder::components
diff --git a/src/ui/helper/ClickDetector.cpp b/src/ui/helper/ClickDetector.cpp
index 8ebdfabc..f76f8af4 100644
--- a/src/ui/helper/ClickDetector.cpp
+++ b/src/ui/helper/ClickDetector.cpp
@@ -58,8 +58,7 @@ ClickDetector::ClickDetector(controls::Control* control) {
this->state_ == ClickState::Hover) {
if (!this->control_->CaptureMouse()) {
if constexpr (debug_flags::click_detector) {
- CRU_LOG_DEBUG(
- u"Failed to capture mouse when begin click.");
+ CRU_LOG_DEBUG(u"Failed to capture mouse when begin click.");
}
return;
}
@@ -77,10 +76,12 @@ ClickDetector::ClickDetector(controls::Control* control) {
button == button_) {
if (this->state_ == ClickState::Press) {
this->SetState(ClickState::Hover);
+ auto resolver = this->control_->CreateResolver();
this->event_.Raise(ClickEventArgs{this->control_,
this->down_point_,
args.GetPoint(), button});
- this->control_->ReleaseMouse();
+ auto c = resolver.Resolve();
+ if (c) c->ReleaseMouse();
} else if (this->state_ == ClickState::PressInactive) {
this->SetState(ClickState::None);
this->control_->ReleaseMouse();
@@ -136,8 +137,7 @@ void ClickDetector::SetState(ClickState state) {
UnreachableCode();
}
};
- CRU_LOG_DEBUG(u"Click state changed, new state: {}.",
- to_string(state));
+ CRU_LOG_DEBUG(u"Click state changed, new state: {}.", to_string(state));
}
state_ = state;
diff --git a/src/ui/host/RoutedEventDispatch.h b/src/ui/host/RoutedEventDispatch.h
index 3c2a98d8..2f437b31 100644
--- a/src/ui/host/RoutedEventDispatch.h
+++ b/src/ui/host/RoutedEventDispatch.h
@@ -1,4 +1,5 @@
#pragma once
+#include "cru/common/SelfResolvable.h"
#include "cru/common/log/Logger.h"
#include "cru/ui/DebugFlags.h"
#include "cru/ui/controls/Control.h"
@@ -39,11 +40,11 @@ void DispatchEvent(
return;
}
- std::vector<controls::Control*> receive_list;
+ std::vector<ObjectResolver<controls::Control>> receive_list;
auto parent = original_sender;
while (parent != last_receiver) {
- receive_list.push_back(parent);
+ receive_list.push_back(parent->CreateResolver());
auto p = parent->GetParent();
assert(!(p == nullptr && last_receiver != nullptr));
parent = p;
@@ -56,10 +57,10 @@ void DispatchEvent(
auto i = receive_list.crbegin();
const auto end = --receive_list.crend();
for (; i != end; ++i) {
- log += (*i)->GetControlType();
+ log += i->Resolve()->GetControlType();
log += u" -> ";
}
- log += (*i)->GetControlType();
+ log += i->Resolve()->GetControlType();
CRU_LOG_DEBUG(log);
}
@@ -70,8 +71,10 @@ void DispatchEvent(
// tunnel
for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) {
count++;
- EventArgs event_args(*i, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>(((*i)->*event_ptr)()->Tunnel())
+ auto control = i->Resolve();
+ if (!control) continue;
+ EventArgs event_args(control, original_sender, std::forward<Args>(args)...);
+ static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Tunnel())
->Raise(event_args);
if (event_args.IsHandled()) {
handled = true;
@@ -86,10 +89,13 @@ void DispatchEvent(
// bubble
if (!handled) {
- for (auto i : receive_list) {
+ for (const auto& resolver : receive_list) {
count--;
- EventArgs event_args(i, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>((i->*event_ptr)()->Bubble())
+ auto control = resolver.Resolve();
+ if (!control) continue;
+ EventArgs event_args(control, original_sender,
+ std::forward<Args>(args)...);
+ static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Bubble())
->Raise(event_args);
if (event_args.IsHandled()) {
if constexpr (debug_flags::routed_event)
@@ -103,9 +109,11 @@ void DispatchEvent(
}
// direct
- for (auto i : receive_list) {
- EventArgs event_args(i, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>((i->*event_ptr)()->Direct())
+ for (auto resolver : receive_list) {
+ auto control = resolver.Resolve();
+ if (!control) continue;
+ EventArgs event_args(control, original_sender, std::forward<Args>(args)...);
+ static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Direct())
->Raise(event_args);
}