TODO 优化控件布局语法

This commit is contained in:
Nanako 2025-04-21 10:39:32 +08:00
parent c322a4b6dd
commit 900db6dd70
4 changed files with 25 additions and 13 deletions

View File

@ -65,9 +65,6 @@ int main(int argc, char* argv[]) {
text_block->set_text(config_info_str);
// text_block->set_text(U"Hello, World! 你好,世界!\n换行测试1111测试测试测试测试,测试测试😀🐵🙏 😃🐵🙏");
const auto& text_block2 = std::make_shared<mtext_block>();
text_block2->set_text(U"Hello, World!");
const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
window->set_content(
mnew(mv_box)
@ -78,7 +75,8 @@ int main(int argc, char* argv[]) {
[
mslot(mbutton)
.margin({ 10 })
.visibility(visibility_t::visible)(text_block)
.visibility(visibility_t::visible)
+text_block
],
mslot(mv_box)
@ -88,7 +86,9 @@ int main(int argc, char* argv[]) {
mslot(mbutton)
.margin({ 10 })
.visibility(visibility_t::visible)
(text_block2)
+mnew(mtext_block)
->set_text(U"Hello, World!")
->set_font_size(24)
]
]
);

View File

@ -6,31 +6,42 @@ class font_face_interface;
class mtext_block : public mleaf_widget {
public:
void on_paint(mirage_paint_context& in_context) override;
void set_text(const std::u32string& in_text) {
auto set_text(const std::u32string& in_text) {
text_ = in_text;
update_layout();
using this_type = std::remove_reference_t<decltype(*this)>;
return std::static_pointer_cast<this_type>(shared_from_this());
}
void set_font(const std::shared_ptr<font_face_interface>& in_font) {
auto set_font(const std::shared_ptr<font_face_interface>& in_font) {
font_ = in_font;
update_layout();
using this_type = std::remove_reference_t<decltype(*this)>;
return std::static_pointer_cast<this_type>(shared_from_this());
}
void set_font_size(float in_font_size) {
auto set_font_size(float in_font_size) {
font_size_ = in_font_size;
update_layout();
using this_type = std::remove_reference_t<decltype(*this)>;
return std::static_pointer_cast<this_type>(shared_from_this());
}
void set_line_spacing(float in_line_spacing) {
auto set_line_spacing(float in_line_spacing) {
line_spacing_ = in_line_spacing;
update_layout();
using this_type = std::remove_reference_t<decltype(*this)>;
return std::static_pointer_cast<this_type>(shared_from_this());
}
void set_max_width(float in_max_width) {
auto set_max_width(float in_max_width) {
max_width_ = in_max_width;
update_layout();
using this_type = std::remove_reference_t<decltype(*this)>;
return std::static_pointer_cast<this_type>(shared_from_this());
}
const auto& get_text() const { return text_; }

View File

@ -8,9 +8,7 @@ class mwindow;
/**
* @class mwidget
* @brief UI组件包装器
*
* widget entity的封装
* @brief UI组件的基类
*/
class mwidget : public std::enable_shared_from_this<mwidget> {
public:

View File

@ -30,6 +30,9 @@ struct mwidget_decl {
auto operator[](Args&&... in_args) {
return std::static_pointer_cast<WidgetType>((*widget_)[std::forward<Args>(in_args)...]);
}
auto operator->() {
return widget_.get();
}
operator std::shared_ptr<mwidget>() const { return widget_; }
std::shared_ptr<WidgetType> widget_;