diff --git a/example/src/main.cpp b/example/src/main.cpp index f552426..42d7942 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -78,12 +78,12 @@ int main(int argc, char* argv[]) { (text_block) ); - // const auto button2 = mnew(mbutton) - // mslot(mbutton) - // .visibility(visibility_t::visible) - // [ - // text_block2 - // ]; + const auto button2 = std::make_shared(); + button2->push_slot( + mslot(mbutton) + .visibility(visibility_t::visible) + (text_block2) + ); const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!"); window->set_content( diff --git a/src/mirage_widget/src/widget/widget_new.h b/src/mirage_widget/src/widget/widget_new.h index 3f5b36b..3ff2259 100644 --- a/src/mirage_widget/src/widget/widget_new.h +++ b/src/mirage_widget/src/widget/widget_new.h @@ -6,6 +6,11 @@ #include "widget_new.h" #include "compound_widget/mbutton.h" +/** + * @struct mwidget_decl + * @tparam WidgetType 要创建的组件类型 + * @brief 组件布局声明结构体, 用于创建和管理组件实例 + */ template struct mwidget_decl { mwidget_decl() { @@ -30,5 +35,41 @@ struct mwidget_decl { std::shared_ptr widget_; }; +/** + * @brief 创建一个新的组件插槽 + * @param type 组件类型 + * @code +// 创建一个组件插槽 +mslot(mv_box) +// 使用方式如下 +auto button = std::make_shared(); +button->push_slot( + mslot(mbutton) + .margin({10}) + .visibility(visibility_t::visible) + (text_block) +); + * @endcode + */ #define mslot(type) type::slot_type() + +/** + * @param type 组件类型 + * @code +// 创建一个新的组件实例 +mnew(mv_box) +[ + // 第一个子组件 + mslot(mv_box) + .h_alignment(horizontal_alignment_t::center) + .v_alignment(vertical_alignment_t::center) + (child_widget), + + // 第二个子组件 + mslot(mv_box) + (child_widget2) +]; + * @endcode + * @note 对于简单的创建控件也可以不使用mnew方式,而是使用std::make_shared(),然后使用布局容器的成员函数来管理布局,具体可以看mcompound_widget和mpanel_widget + */ #define mnew(type, ...) mwidget_decl(__VA_ARGS__)