diff options
-rw-r--r-- | CruUI/exception.cpp | 8 | ||||
-rw-r--r-- | CruUI/main.cpp | 35 | ||||
-rw-r--r-- | CruUI/ui/control.h | 11 | ||||
-rw-r--r-- | CruUI/ui/controls/button.h | 9 | ||||
-rw-r--r-- | CruUI/ui/controls/linear_layout.cpp | 4 | ||||
-rw-r--r-- | CruUI/ui/controls/linear_layout.h | 7 |
6 files changed, 38 insertions, 36 deletions
diff --git a/CruUI/exception.cpp b/CruUI/exception.cpp index 38075247..a1d59ceb 100644 --- a/CruUI/exception.cpp +++ b/CruUI/exception.cpp @@ -7,9 +7,9 @@ namespace cru inline std::string HResultMakeMessage(HRESULT h_result, std::optional<std::string> message) { if (message.has_value()) - return fmt::format("An HResultError is thrown. HRESULT: {:#08x}.\n", h_result); - else return fmt::format("An HResultError is thrown. HRESULT: {:#08x}.\nAdditional message: {}\n", h_result, message.value()); + else + return fmt::format("An HResultError is thrown. HRESULT: {:#08x}.\n", h_result); } HResultError::HResultError(HRESULT h_result, std::optional<std::string_view> additional_message) @@ -21,9 +21,9 @@ namespace cru inline std::string Win32MakeMessage(DWORD error_code, std::optional<std::string> message) { if (message.has_value()) - return fmt::format("Last error code: {:#04x}.\n", error_code); - else return fmt::format("Last error code: {:#04x}.\nAdditional message: {}\n", error_code, message.value()); + else + return fmt::format("Last error code: {:#04x}.\n", error_code); } Win32Error::Win32Error(DWORD error_code, std::optional<std::string_view> additional_message) diff --git a/CruUI/main.cpp b/CruUI/main.cpp index f2c5747d..0fd747a1 100644 --- a/CruUI/main.cpp +++ b/CruUI/main.cpp @@ -10,10 +10,10 @@ using cru::String; using cru::Application; using cru::ui::Window; -using cru::ui::MeasureMode; using cru::ui::Alignment; using cru::ui::LayoutLength; using cru::ui::Thickness; +using cru::ui::CreateWithLayout; using cru::ui::controls::LinearLayout; using cru::ui::controls::TextBlock; using cru::ui::controls::ToggleButton; @@ -76,9 +76,7 @@ int APIENTRY wWinMain( //test 2 - const auto layout = LinearLayout::Create(); - - layout->GetLayoutParams()->width = LayoutLength::Exactly(500); + const auto layout = CreateWithLayout<LinearLayout>(LayoutLength::Exactly(500), LayoutLength::Content()); layout->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args) { @@ -87,10 +85,7 @@ int APIENTRY wWinMain( }); { - const auto inner_layout = LinearLayout::Create(LinearLayout::Orientation::Horizontal); - inner_layout->GetLayoutParams()->width.alignment = Alignment::End; - - layout->AddChild(inner_layout); + const auto inner_layout = CreateWithLayout<LinearLayout>(LayoutLength::Content(Alignment::End), LayoutLength::Content(), LinearLayout::Orientation::Horizontal); inner_layout->AddChild(TextBlock::Create(L"Toggle debug border")); @@ -101,24 +96,17 @@ int APIENTRY wWinMain( }); inner_layout->AddChild(toggle_button); + layout->AddChild(inner_layout); } { const auto button = Button::Create(); - button->AddChild(MarginContainer::Create(Thickness(20, 5), {TextBlock::Create(L"button")})); + button->AddChild(MarginContainer::Create(Thickness(20, 5), { TextBlock::Create(L"button") })); layout->AddChild(button); } - auto&& create_text_block = [](const String& text, const LayoutLength& width = LayoutLength::Content(), const LayoutLength& height = LayoutLength::Content()) { - const auto text_block = TextBlock::Create(text); - text_block->GetLayoutParams()->width = width; - text_block->GetLayoutParams()->height = height; - return text_block; - }; - - { - const auto text_block = create_text_block(L"Hello World!!!", LayoutLength::Exactly(200), LayoutLength::Exactly(80)); + const auto text_block = CreateWithLayout<TextBlock>(LayoutLength::Exactly(200), LayoutLength::Exactly(80), L"Hello World!!!"); text_block->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args) { @@ -129,18 +117,13 @@ int APIENTRY wWinMain( } { - const auto text_block = create_text_block(L"This is a very very very very very long sentence!!!", LayoutLength::Stretch(), LayoutLength::Stretch()); + const auto text_block = CreateWithLayout<TextBlock>(LayoutLength::Stretch(), LayoutLength::Stretch(), L"This is a very very very very very long sentence!!!"); text_block->SetSelectable(true); layout->AddChild(text_block); } - { - const auto text_block = TextBlock::Create(L"This is a little short sentence!!!"); - text_block->GetLayoutParams()->width.alignment = Alignment::Start; - layout->AddChild(text_block); - } - - layout->AddChild(create_text_block(L"By crupest!!!", LayoutLength::Stretch(), LayoutLength::Stretch())); + layout->AddChild(CreateWithLayout<TextBlock>(LayoutLength::Content(Alignment::Start), LayoutLength::Content(), L"This is a little short sentence!!!")); + layout->AddChild(CreateWithLayout<TextBlock>(LayoutLength::Content(Alignment::End), LayoutLength::Stretch(), L"By crupest!!!")); window.AddChild(layout); diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index 2f23fd21..3810f5ac 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -4,6 +4,7 @@ #include <unordered_map> #include <any> #include <typeinfo> +#include <utility> #include <fmt/format.h> #include "base.h" @@ -348,5 +349,15 @@ namespace cru // Return the ancestor if one control is the ancestor of the other one, otherwise nullptr. Control* IsAncestorOrDescendant(Control* left, Control* right); + + template <typename TControl, typename... Args> + TControl* CreateWithLayout(const LayoutLength& width, const LayoutLength& height, Args&&... args) + { + static_assert(std::is_base_of_v<Control, TControl>, "TControl is not a control class."); + TControl* control = TControl::Create(std::forward<Args>(args)...); + control->GetLayoutParams()->width = width; + control->GetLayoutParams()->height = height; + return control; + } } } diff --git a/CruUI/ui/controls/button.h b/CruUI/ui/controls/button.h index f9ad7726..bd3f6eb3 100644 --- a/CruUI/ui/controls/button.h +++ b/CruUI/ui/controls/button.h @@ -1,5 +1,7 @@ #pragma once +#include <initializer_list> + #include "ui/control.h" namespace cru::ui::controls @@ -7,9 +9,12 @@ namespace cru::ui::controls class Button : public Control { public: - static Button* Create() + static Button* Create(const std::initializer_list<Control*>& children = std::initializer_list<Control*>()) { - return new Button(); + const auto button = new Button(); + for (const auto control : children) + button->AddChild(control); + return button; } protected: diff --git a/CruUI/ui/controls/linear_layout.cpp b/CruUI/ui/controls/linear_layout.cpp index 116802ba..22bf26e9 100644 --- a/CruUI/ui/controls/linear_layout.cpp +++ b/CruUI/ui/controls/linear_layout.cpp @@ -89,7 +89,7 @@ namespace cru::ui::controls const auto available_width = rest_available_size_for_children.width / stretch_control_list.size(); for (const auto control : stretch_control_list) { - control->Measure(Size(available_width, AtLeast0(rest_available_size_for_children.height))); + control->Measure(Size(AtLeast0(available_width), rest_available_size_for_children.height)); const auto size = control->GetDesiredSize(); rest_available_size_for_children.width -= size.width; secondary_side_child_max_length = std::max(size.height, secondary_side_child_max_length); @@ -100,7 +100,7 @@ namespace cru::ui::controls const auto available_height = rest_available_size_for_children.height / stretch_control_list.size(); for (const auto control : stretch_control_list) { - control->Measure(Size(AtLeast0(rest_available_size_for_children.width), available_height)); + control->Measure(Size(rest_available_size_for_children.width, AtLeast0(available_height))); const auto size = control->GetDesiredSize(); rest_available_size_for_children.height -= size.height; secondary_side_child_max_length = std::max(size.width, secondary_side_child_max_length); diff --git a/CruUI/ui/controls/linear_layout.h b/CruUI/ui/controls/linear_layout.h index ead56081..369824d4 100644 --- a/CruUI/ui/controls/linear_layout.h +++ b/CruUI/ui/controls/linear_layout.h @@ -13,9 +13,12 @@ namespace cru::ui::controls Vertical }; - static LinearLayout* Create(const Orientation orientation = Orientation::Vertical) + static LinearLayout* Create(const Orientation orientation = Orientation::Vertical, const std::initializer_list<Control*>& children = std::initializer_list<Control*>()) { - return new LinearLayout(orientation); + const auto linear_layout = new LinearLayout(orientation); + for (const auto control : children) + linear_layout->AddChild(control); + return linear_layout; } protected: |