From 74bb9cd27242b9320f99ff4d2b50c3051576cc14 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Feb 2022 16:53:51 +0800 Subject: ... --- include/cru/ui/components/Component.h | 19 ++++++ include/cru/ui/components/Component.hpp | 19 ------ include/cru/ui/components/Menu.h | 107 ++++++++++++++++++++++++++++++++ include/cru/ui/components/Menu.hpp | 107 -------------------------------- 4 files changed, 126 insertions(+), 126 deletions(-) create mode 100644 include/cru/ui/components/Component.h delete mode 100644 include/cru/ui/components/Component.hpp create mode 100644 include/cru/ui/components/Menu.h delete mode 100644 include/cru/ui/components/Menu.hpp (limited to 'include/cru/ui/components') diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h new file mode 100644 index 00000000..0b871dc4 --- /dev/null +++ b/include/cru/ui/components/Component.h @@ -0,0 +1,19 @@ +#pragma once +#include "../Base.h" + +namespace cru::ui::components { +// In destructor, component should check all owned controls whether it is +// attached to window, if not, destroy them, otherwise it is host's duty to +// destroy them. +class CRU_UI_API Component : public Object { + public: + Component() = default; + + CRU_DELETE_COPY(Component) + CRU_DELETE_MOVE(Component) + + ~Component() = default; + + virtual controls::Control* GetRootControl() = 0; +}; +} // namespace cru::ui::components diff --git a/include/cru/ui/components/Component.hpp b/include/cru/ui/components/Component.hpp deleted file mode 100644 index 4cbc3791..00000000 --- a/include/cru/ui/components/Component.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "../Base.hpp" - -namespace cru::ui::components { -// In destructor, component should check all owned controls whether it is -// attached to window, if not, destroy them, otherwise it is host's duty to -// destroy them. -class CRU_UI_API Component : public Object { - public: - Component() = default; - - CRU_DELETE_COPY(Component) - CRU_DELETE_MOVE(Component) - - ~Component() = default; - - virtual controls::Control* GetRootControl() = 0; -}; -} // namespace cru::ui::components diff --git a/include/cru/ui/components/Menu.h b/include/cru/ui/components/Menu.h new file mode 100644 index 00000000..32936f84 --- /dev/null +++ b/include/cru/ui/components/Menu.h @@ -0,0 +1,107 @@ +#pragma once +#include "Component.h" +#include "cru/common/Base.h" +#include "cru/ui/controls/Button.h" +#include "cru/ui/controls/Control.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/controls/Popup.h" +#include "cru/ui/controls/TextBlock.h" + +#include +#include + +namespace cru::ui::components { +class CRU_UI_API MenuItem : public Component { + public: + MenuItem(); + explicit MenuItem(String text); + + CRU_DELETE_COPY(MenuItem) + CRU_DELETE_MOVE(MenuItem) + + ~MenuItem(); + + public: + controls::Control* GetRootControl() override { return container_; } + + void SetText(String text); + + void SetOnClick(std::function on_click) { + on_click_ = std::move(on_click); + } + + private: + controls::Button* container_; + controls::TextBlock* text_; + std::function on_click_; +}; + +class CRU_UI_API Menu : public Component { + public: + Menu(); + + CRU_DELETE_COPY(Menu) + CRU_DELETE_MOVE(Menu) + + ~Menu(); + + public: + controls::Control* GetRootControl() override { return container_; } + + gsl::index GetItemCount() const { + return static_cast(items_.size()); + } + + void AddItem(Component* component) { AddItem(component, GetItemCount()); } + void AddItem(Component* component, gsl::index index); + Component* RemoveItem(gsl::index index); + void ClearItems(); + + void AddTextItem(String text, std::function on_click) { + AddTextItem(std::move(text), GetItemCount(), std::move(on_click)); + } + void AddTextItem(String text, Index index, std::function on_click); + + void SetOnItemClick(std::function on_item_click) { + on_item_click_ = std::move(on_item_click); + } + + private: + controls::FlexLayout* container_; + std::vector items_; + + std::function on_item_click_; +}; + +class CRU_UI_API PopupMenu : public Component { + public: + explicit PopupMenu(controls::Control* attached_control = nullptr); + + CRU_DELETE_COPY(PopupMenu) + CRU_DELETE_MOVE(PopupMenu) + + ~PopupMenu(); + + public: + controls::Control* GetRootControl() override; + + controls::Popup* GetPopup() { return popup_; } + Menu* GetMenu() { return menu_; } + + // position relative to screen left top. + void SetPosition(const Point& position); + void Show(); + // position relative to screen left top. + void Show(const Point& position) { + SetPosition(position); + Show(); + } + void Close(); + + private: + controls::Control* attached_control_; + + controls::Popup* popup_; + Menu* menu_; +}; +} // namespace cru::ui::components diff --git a/include/cru/ui/components/Menu.hpp b/include/cru/ui/components/Menu.hpp deleted file mode 100644 index 9b60eb02..00000000 --- a/include/cru/ui/components/Menu.hpp +++ /dev/null @@ -1,107 +0,0 @@ -#pragma once -#include "Component.hpp" -#include "cru/common/Base.hpp" -#include "cru/ui/controls/Button.hpp" -#include "cru/ui/controls/Control.hpp" -#include "cru/ui/controls/FlexLayout.hpp" -#include "cru/ui/controls/Popup.hpp" -#include "cru/ui/controls/TextBlock.hpp" - -#include -#include - -namespace cru::ui::components { -class CRU_UI_API MenuItem : public Component { - public: - MenuItem(); - explicit MenuItem(String text); - - CRU_DELETE_COPY(MenuItem) - CRU_DELETE_MOVE(MenuItem) - - ~MenuItem(); - - public: - controls::Control* GetRootControl() override { return container_; } - - void SetText(String text); - - void SetOnClick(std::function on_click) { - on_click_ = std::move(on_click); - } - - private: - controls::Button* container_; - controls::TextBlock* text_; - std::function on_click_; -}; - -class CRU_UI_API Menu : public Component { - public: - Menu(); - - CRU_DELETE_COPY(Menu) - CRU_DELETE_MOVE(Menu) - - ~Menu(); - - public: - controls::Control* GetRootControl() override { return container_; } - - gsl::index GetItemCount() const { - return static_cast(items_.size()); - } - - void AddItem(Component* component) { AddItem(component, GetItemCount()); } - void AddItem(Component* component, gsl::index index); - Component* RemoveItem(gsl::index index); - void ClearItems(); - - void AddTextItem(String text, std::function on_click) { - AddTextItem(std::move(text), GetItemCount(), std::move(on_click)); - } - void AddTextItem(String text, Index index, std::function on_click); - - void SetOnItemClick(std::function on_item_click) { - on_item_click_ = std::move(on_item_click); - } - - private: - controls::FlexLayout* container_; - std::vector items_; - - std::function on_item_click_; -}; - -class CRU_UI_API PopupMenu : public Component { - public: - explicit PopupMenu(controls::Control* attached_control = nullptr); - - CRU_DELETE_COPY(PopupMenu) - CRU_DELETE_MOVE(PopupMenu) - - ~PopupMenu(); - - public: - controls::Control* GetRootControl() override; - - controls::Popup* GetPopup() { return popup_; } - Menu* GetMenu() { return menu_; } - - // position relative to screen left top. - void SetPosition(const Point& position); - void Show(); - // position relative to screen left top. - void Show(const Point& position) { - SetPosition(position); - Show(); - } - void Close(); - - private: - controls::Control* attached_control_; - - controls::Popup* popup_; - Menu* menu_; -}; -} // namespace cru::ui::components -- cgit v1.2.3