From 2b923c2bb585bee5a039151aff4c7a79c9a4756d Mon Sep 17 00:00:00 2001 From: daiqingshuang Date: Wed, 2 Apr 2025 17:08:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=93=BE=E5=BC=8F=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/src/main.cpp | 30 +++++++++++--------- src/mirage_widget/widget/mcompound_widget.h | 14 ++++++++++ src/mirage_widget/widget/widget_new.h | 31 +++++++++++++-------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/example/src/main.cpp b/example/src/main.cpp index fb03be6..1b28748 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -23,23 +23,27 @@ int main(int argc, char* argv[]) { const auto& text_block2 = std::make_shared(); text_block2->set_text(U"Hello, World!"); - const auto button = std::make_shared(); - button->set_content(text_block); + const auto button = mnew(mbutton) + mslot(mbutton) + [ + text_block + ]; - const auto button2 = std::make_shared(); - 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(); diff --git a/src/mirage_widget/widget/mcompound_widget.h b/src/mirage_widget/widget/mcompound_widget.h index 023a230..c2a841b 100644 --- a/src/mirage_widget/widget/mcompound_widget.h +++ b/src/mirage_widget/widget/mcompound_widget.h @@ -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_; + } //-------------- 尺寸计算 -------------- diff --git a/src/mirage_widget/widget/widget_new.h b/src/mirage_widget/widget/widget_new.h index 9b248c4..b8cd399 100644 --- a/src/mirage_widget/widget/widget_new.h +++ b/src/mirage_widget/widget/widget_new.h @@ -3,6 +3,8 @@ #include "mpanel_widget.h" #include "mwidget.h" +#include "widget_new.h" +#include "compound_widget/mbutton.h" // 前向声明 template @@ -18,6 +20,15 @@ concept has_set_content = requires(T& t, std::shared_ptr in_widget) { t.set_content(in_widget); }; +template +struct mwidget_slot_guard { + auto& operator+(const typename WidgetType::slot_type& in_slot) { + slots_.push_back(in_slot); + return *this; + } + std::vector slots_; +}; + template struct mwidget_decl { mwidget_decl() { @@ -29,22 +40,20 @@ struct mwidget_decl { widget_ = std::make_shared(std::forward(in_args)...); } - auto& operator[](const std::shared_ptr& in_widget) requires has_set_content { - 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 { - widget_->push_slot(in_slot); - return *this; - } - - auto& s() requires has_add_slot { - return widget_->add_slot(); + std::shared_ptr operator<<=(const mwidget_slot_guard& in_guard) { + for (const auto& slot: in_guard.slots_) { + widget_->push_slot(slot); + } + return widget_; } operator std::shared_ptr() const { return widget_; } std::shared_ptr widget_; }; -#define mnew(type, ...) mwidget_decl(__VA_ARGS__) #define mslot(type) +type::slot_type() +#define mnew(type, ...) mwidget_decl(__VA_ARGS__) <<= mwidget_slot_guard()