Loading...
Searching...
No Matches
MenuItem.h
Go to the documentation of this file.
1//
2// MenuItem.h
3//
4
5#ifndef MALENA_MENUITEM_H
6#define MALENA_MENUITEM_H
7
8#pragma once
9
12#include <functional>
13#include <string>
14#include <vector>
15
16namespace ml
17{
48 struct MenuItem
49 {
50 std::string label;
51 std::string shortcut;
52 const sf::Texture* icon = nullptr;
53 bool checked = false;
54 bool enabled = true;
55 bool separator = false;
56 std::function<void()> action;
57 std::vector<MenuItem> submenu;
58
59 // ── Factory helpers ───────────────────────────────────────────────────
60
62 static MenuItem item(const std::string& label,
63 std::function<void()> action = {},
64 const std::string& shortcut = "",
65 const sf::Texture* icon = nullptr)
66 {
67 MenuItem m;
68 m.label = label;
69 m.action = std::move(action);
71 m.icon = icon;
72 return m;
73 }
74
76 static MenuItem chk(const std::string& label,
77 bool& ref,
78 std::function<void()> onChange = {})
79 {
80 MenuItem m;
81 m.label = label;
82 m.checked = ref;
83 m.action = [&ref, cb = std::move(onChange)]{
84 ref = !ref;
85 if (cb) cb();
86 };
87 return m;
88 }
89
91 static MenuItem sub(const std::string& label,
92 std::vector<MenuItem> items)
93 {
94 MenuItem m;
95 m.label = label;
96 m.submenu = std::move(items);
97 return m;
98 }
99
101 static MenuItem sep()
102 {
103 MenuItem m;
104 m.separator = true;
105 return m;
106 }
107
109 static MenuItem disabled(const std::string& label)
110 {
111 MenuItem m;
112 m.label = label;
113 m.enabled = false;
114 return m;
115 }
116
117 [[nodiscard]] bool hasSubmenu() const { return !submenu.empty(); }
118 };
119
120} // namespace ml
121
122#endif // MALENA_MENUITEM_H
Definition Component.h:22
A single entry in a MenuBar dropdown or submenu.
Definition MenuItem.h:49
bool checked
Definition MenuItem.h:53
static MenuItem chk(const std::string &label, bool &ref, std::function< void()> onChange={})
Checkable item that toggles a bool reference.
Definition MenuItem.h:76
static MenuItem sep()
Horizontal divider line.
Definition MenuItem.h:101
bool enabled
Definition MenuItem.h:54
static MenuItem item(const std::string &label, std::function< void()> action={}, const std::string &shortcut="", const sf::Texture *icon=nullptr)
Standard clickable item.
Definition MenuItem.h:62
const sf::Texture * icon
Definition MenuItem.h:52
std::function< void()> action
Definition MenuItem.h:56
std::string shortcut
Definition MenuItem.h:51
static MenuItem sub(const std::string &label, std::vector< MenuItem > items)
Item that opens a submenu.
Definition MenuItem.h:91
std::vector< MenuItem > submenu
Definition MenuItem.h:57
bool separator
Definition MenuItem.h:55
bool hasSubmenu() const
Definition MenuItem.h:117
std::string label
Definition MenuItem.h:50
static MenuItem disabled(const std::string &label)
Greyed-out non-interactive label.
Definition MenuItem.h:109