aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/components/Menu.h
blob: 8c54e29cce18309cce7541ff186972ad6447e895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once
#include "../controls/Button.h"
#include "../controls/Control.h"
#include "../controls/FlexLayout.h"
#include "../controls/TextBlock.h"
#include "../controls/Window.h"
#include "../helper/ClickDetector.h"
#include "Component.h"

#include <functional>
#include <vector>

namespace cru::ui::components {
class CRU_UI_API MenuItem : public Component {
 public:
  MenuItem();
  explicit MenuItem(std::string text);

 public:
  controls::Control* GetRootControl() override { return &container_; }

  void SetText(std::string text);
  IEvent<const helper::ClickEventArgs&>* ClickEvent();

 private:
  controls::Button container_;
  controls::TextBlock text_;
};

class CRU_UI_API Menu : public Component {
 private:
  constexpr static auto kLogTag = "cru::ui::components::Menu";

 public:
  Menu();
  ~Menu();

 public:
  controls::Control* GetRootControl() override { return &container_; }

  Index GetItemCount() { return static_cast<Index>(items_.size()); }

  void AddItem(Component* component) { AddItemAt(component, GetItemCount()); }
  void AddItemAt(Component* component, Index index);
  Component* RemoveItemAt(Index index);
  void ClearItems();

  void AddTextItem(std::string text, std::function<void()> on_click) {
    AddTextItemAt(std::move(text), GetItemCount(), std::move(on_click));
  }
  void AddTextItemAt(std::string text, Index index,
                     std::function<void()> on_click);

  void SetOnItemClick(std::function<void(Index)> on_item_click) {
    on_item_click_ = std::move(on_item_click);
  }

 private:
  controls::FlexLayout container_;
  std::vector<Component*> items_;

  std::function<void(Index)> on_item_click_;
};

class CRU_UI_API PopupMenu : public Component {
 public:
  explicit PopupMenu(controls::Control* attached_control = nullptr);
  ~PopupMenu();

 public:
  controls::Control* GetRootControl() override;

  controls::Window* GetPopup() { return popup_.get(); }
  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_;

  std::unique_ptr<controls::Window> popup_;
  Menu menu_;
};
}  // namespace cru::ui::components