修复链式调用

This commit is contained in:
daiqingshuang 2025-04-02 17:08:26 +08:00
parent da19e27f5b
commit 2b923c2bb5
3 changed files with 51 additions and 24 deletions

View File

@ -23,23 +23,27 @@ int main(int argc, char* argv[]) {
const auto& text_block2 = std::make_shared<mtext_block>();
text_block2->set_text(U"Hello, World!");
const auto button = std::make_shared<mbutton>();
button->set_content(text_block);
const auto button = mnew(mbutton)
mslot(mbutton)
[
text_block
];
const auto button2 = std::make_shared<mbutton>();
button2->set_content(text_block2);
const auto button2 = mnew(mbutton)
mslot(mbutton)
[
text_block2
];
const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
window->set_content(
mnew(mh_box)
mslot(mh_box)
[
button
]
mslot(mh_box)
[
button2
]
mnew(mborder)
mslot(mborder)
.h_alignment(horizontal_alignment_t::center)
.v_alignment(vertical_alignment_t::center)
[
button
]
);
mirage_app::get().run();

View File

@ -69,9 +69,23 @@ public:
auto& slot = get_child_slot();
slot.slot_owner = shared_this;
slot.set(in_widget);
in_widget->init();
in_widget->set_parent(shared_this);
return slot;
}
auto& push_slot(const SlotType& in_slot) {
const auto& child_widget = in_slot.get();
on_set_content(child_widget);
invalidate(invalidate_reason::layout);
auto shared_this = shared_from_this();
slot_ = in_slot;
slot_.slot_owner = shared_this;
child_widget->init();
child_widget->set_parent(shared_this);
return slot_;
}
//-------------- 尺寸计算 --------------

View File

@ -3,6 +3,8 @@
#include "mpanel_widget.h"
#include "mwidget.h"
#include "widget_new.h"
#include "compound_widget/mbutton.h"
// 前向声明
template<typename WidgetType>
@ -18,6 +20,15 @@ concept has_set_content = requires(T& t, std::shared_ptr<mwidget> in_widget) {
t.set_content(in_widget);
};
template<typename WidgetType>
struct mwidget_slot_guard {
auto& operator+(const typename WidgetType::slot_type& in_slot) {
slots_.push_back(in_slot);
return *this;
}
std::vector<typename WidgetType::slot_type> slots_;
};
template<typename WidgetType>
struct mwidget_decl {
mwidget_decl() {
@ -29,22 +40,20 @@ struct mwidget_decl {
widget_ = std::make_shared<WidgetType>(std::forward<Args>(in_args)...);
}
auto& operator[](const std::shared_ptr<mwidget>& in_widget) requires has_set_content<WidgetType> {
return widget_->set_content(in_widget);
}
// 禁止复制
mwidget_decl(const mwidget_decl&) = delete;
mwidget_decl& operator=(const mwidget_decl&) = delete;
auto& operator+(const typename WidgetType::slot_type& in_slot) requires has_add_slot<WidgetType> {
widget_->push_slot(in_slot);
return *this;
}
auto& s() requires has_add_slot<WidgetType> {
return widget_->add_slot();
std::shared_ptr<WidgetType> operator<<=(const mwidget_slot_guard<WidgetType>& in_guard) {
for (const auto& slot: in_guard.slots_) {
widget_->push_slot(slot);
}
return widget_;
}
operator std::shared_ptr<mwidget>() const { return widget_; }
std::shared_ptr<WidgetType> widget_;
};
#define mnew(type, ...) mwidget_decl<type>(__VA_ARGS__)
#define mslot(type) +type::slot_type()
#define mnew(type, ...) mwidget_decl<type>(__VA_ARGS__) <<= mwidget_slot_guard<type>()