SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_tray.h
1#ifndef SDL3PP_TRAY_H_
2#define SDL3PP_TRAY_H_
3
4#include <SDL3/SDL_tray.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_surface.h"
7
8namespace SDL {
9
22// Forward decl
23struct Tray;
24
26using TrayRaw = SDL_Tray*;
27
28// Forward decl
29struct TrayRef;
30
33{
35
38 : value(value)
39 {
40 }
41
43 constexpr TrayParam(std::nullptr_t _ = nullptr)
44 : value(nullptr)
45 {
46 }
47
49 constexpr explicit operator bool() const { return !!value; }
50
52 constexpr auto operator<=>(const TrayParam& other) const = default;
53
55 constexpr operator TrayRaw() const { return value; }
56};
57
59struct TrayMenu;
60
62using TrayMenuRaw = SDL_TrayMenu*;
63
64// Forward decl
65struct TrayMenu;
66
67// Forward decl
68struct TrayEntry;
69
71using TrayEntryRaw = SDL_TrayEntry*;
72
73// Forward decl
74struct TrayEntryScoped;
75
78{
80
83 : value(value)
84 {
85 }
86
88 constexpr TrayEntryParam(std::nullptr_t _ = nullptr)
89 : value(nullptr)
90 {
91 }
92
94 constexpr explicit operator bool() const { return !!value; }
95
97 constexpr auto operator<=>(const TrayEntryParam& other) const = default;
98
100 constexpr operator TrayEntryRaw() const { return value; }
101};
102
115
117 SDL_TRAYENTRY_BUTTON;
118
120 SDL_TRAYENTRY_CHECKBOX;
121
123 SDL_TRAYENTRY_SUBMENU;
124
126 SDL_TRAYENTRY_DISABLED;
127
129constexpr TrayEntryFlags TRAYENTRY_CHECKED = SDL_TRAYENTRY_CHECKED;
130
142using TrayCallback = SDL_TrayCallback;
143
154using TrayCB = std::function<void(TrayEntryRaw)>;
155
163class Tray
164{
165 TrayRaw m_resource = nullptr;
166
167public:
169 constexpr Tray() = default;
170
178 constexpr explicit Tray(const TrayRaw resource)
179 : m_resource(resource)
180 {
181 }
182
184 constexpr Tray(const Tray& other) = delete;
185
187 constexpr Tray(Tray&& other)
188 : Tray(other.release())
189 {
190 }
191
192 constexpr Tray(const TrayRef& other) = delete;
193
194 constexpr Tray(TrayRef&& other) = delete;
195
220 : m_resource(SDL_CreateTray(icon, tooltip))
221 {
222 }
223
225 ~Tray() { SDL_DestroyTray(m_resource); }
226
229 {
230 std::swap(m_resource, other.m_resource);
231 return *this;
232 }
233
235 constexpr TrayRaw get() const { return m_resource; }
236
238 constexpr TrayRaw release()
239 {
240 auto r = m_resource;
241 m_resource = nullptr;
242 return r;
243 }
244
246 constexpr auto operator<=>(const Tray& other) const = default;
247
249 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
250
252 constexpr explicit operator bool() const { return !!m_resource; }
253
255 constexpr operator TrayParam() const { return {m_resource}; }
256
270 void Destroy();
271
284 void SetIcon(SurfaceParam icon);
285
298 void SetTooltip(StringParam tooltip);
299
322
344 TrayMenu GetMenu() const;
345};
346
348struct TrayRef : Tray
349{
358 : Tray(resource.value)
359 {
360 }
361
363 TrayRef(const TrayRef& other)
364 : Tray(other.get())
365 {
366 }
367
370};
371
378{
379 TrayMenuRaw m_trayMenu;
380
381public:
387 constexpr TrayMenu(TrayMenuRaw trayMenu = {})
388 : m_trayMenu(trayMenu)
389 {
390 }
391
397 constexpr operator TrayMenuRaw() const { return m_trayMenu; }
398
414 std::span<TrayEntry> GetEntries();
415
441 TrayEntry InsertEntry(int pos, StringParam label, TrayEntryFlags flags);
442
468
487
505 TrayParam GetParentTray() const;
506};
507
516{
517 TrayEntryRaw m_resource = nullptr;
518
519public:
521 constexpr TrayEntry() = default;
522
528 constexpr TrayEntry(const TrayEntryRaw resource)
529 : m_resource(resource)
530 {
531 }
532
534 constexpr TrayEntry(const TrayEntry& other) = default;
535
537 constexpr TrayEntry(TrayEntry&& other)
538 : TrayEntry(other.release())
539 {
540 }
541
544
547 {
548 std::swap(m_resource, other.m_resource);
549 return *this;
550 }
551
553 constexpr TrayEntryRaw get() const { return m_resource; }
554
557 {
558 auto r = m_resource;
559 m_resource = nullptr;
560 return r;
561 }
562
564 constexpr auto operator<=>(const TrayEntry& other) const = default;
565
567 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
568
570 constexpr explicit operator bool() const { return !!m_resource; }
571
573 constexpr operator TrayEntryParam() const { return {m_resource}; }
574
587 void Remove();
588
611
634
654 void SetLabel(StringParam label);
655
672 const char* GetLabel() const;
673
690 void SetChecked(bool checked);
691
708 bool GetChecked() const;
709
724 void SetEnabled(bool enabled);
725
740 bool GetEnabled() const;
741
755 void SetCallback(TrayCB callback);
756
772 void SetCallback(TrayCallback callback, void* userdata);
773
783 void Click();
784
798};
799
802{
804
805 constexpr TrayEntryScoped(const TrayEntry& other) = delete;
806
808 constexpr TrayEntryScoped(TrayEntry&& other)
809 : TrayEntry(other.release())
810 {
811 }
812
815};
816
841{
842 return Tray(icon, std::move(tooltip));
843}
844
858inline void SetTrayIcon(TrayParam tray, SurfaceParam icon)
859{
860 SDL_SetTrayIcon(tray, icon);
861}
862
864{
865 SDL::SetTrayIcon(m_resource, icon);
866}
867
881inline void SetTrayTooltip(TrayParam tray, StringParam tooltip)
882{
883 SDL_SetTrayTooltip(tray, tooltip);
884}
885
886inline void Tray::SetTooltip(StringParam tooltip)
887{
888 SDL::SetTrayTooltip(m_resource, std::move(tooltip));
889}
890
914{
915 return SDL_CreateTrayMenu(tray);
916}
917
918inline TrayMenu Tray::CreateMenu() { return SDL::CreateTrayMenu(m_resource); }
919
943{
944 return SDL_CreateTraySubmenu(entry);
945}
946
948{
949 return SDL::CreateTraySubmenu(m_resource);
950}
951
974inline TrayMenu GetTrayMenu(TrayParam tray) { return SDL_GetTrayMenu(tray); }
975
976inline TrayMenu Tray::GetMenu() const { return SDL::GetTrayMenu(m_resource); }
977
1001{
1002 return SDL_GetTraySubmenu(entry);
1003}
1004
1006{
1007 return SDL::GetTraySubmenu(m_resource);
1008}
1009
1026inline std::span<TrayEntry> GetTrayEntries(TrayMenu menu)
1027{
1028 int count;
1029 auto entries = SDL_GetTrayEntries(menu, &count);
1030 return std::span<TrayEntry>{reinterpret_cast<TrayEntry*>(entries),
1031 size_t(count)};
1032}
1033
1034inline std::span<TrayEntry> TrayMenu::GetEntries()
1035{
1036 return SDL::GetTrayEntries(m_trayMenu);
1037}
1038
1052inline void RemoveTrayEntry(TrayEntryRaw entry) { SDL_RemoveTrayEntry(entry); }
1053
1055
1083 int pos,
1084 StringParam label,
1085 TrayEntryFlags flags)
1086{
1087 return SDL_InsertTrayEntryAt(menu, pos, label, flags);
1088}
1089
1091 StringParam label,
1092 TrayEntryFlags flags)
1093{
1094 return SDL::InsertTrayEntryAt(m_trayMenu, pos, std::move(label), flags);
1095}
1096
1118{
1119 SDL_SetTrayEntryLabel(entry, label);
1120}
1121
1123{
1124 SDL::SetTrayEntryLabel(m_resource, std::move(label));
1125}
1126
1144inline const char* GetTrayEntryLabel(TrayEntryParam entry)
1145{
1146 return SDL_GetTrayEntryLabel(entry);
1147}
1148
1149inline const char* TrayEntry::GetLabel() const
1150{
1151 return SDL::GetTrayEntryLabel(m_resource);
1152}
1153
1171inline void SetTrayEntryChecked(TrayEntryParam entry, bool checked)
1172{
1173 SDL_SetTrayEntryChecked(entry, checked);
1174}
1175
1176inline void TrayEntry::SetChecked(bool checked)
1177{
1178 SDL::SetTrayEntryChecked(m_resource, checked);
1179}
1180
1199{
1200 return SDL_GetTrayEntryChecked(entry);
1201}
1202
1203inline bool TrayEntry::GetChecked() const
1204{
1205 return SDL::GetTrayEntryChecked(m_resource);
1206}
1207
1223inline void SetTrayEntryEnabled(TrayEntryParam entry, bool enabled)
1224{
1225 SDL_SetTrayEntryEnabled(entry, enabled);
1226}
1227
1228inline void TrayEntry::SetEnabled(bool enabled)
1229{
1230 SDL::SetTrayEntryEnabled(m_resource, enabled);
1231}
1232
1249{
1250 return SDL_GetTrayEntryEnabled(entry);
1251}
1252
1253inline bool TrayEntry::GetEnabled() const
1254{
1255 return SDL::GetTrayEntryEnabled(m_resource);
1256}
1257
1275 TrayCallback callback,
1276 void* userdata)
1277{
1278 SDL_SetTrayEntryCallback(entry, callback, userdata);
1279}
1280
1281inline void TrayEntry::SetCallback(TrayCallback callback, void* userdata)
1282{
1283 SDL::SetTrayEntryCallback(m_resource, callback, userdata);
1284}
1285
1296inline void ClickTrayEntry(TrayEntryParam entry) { SDL_ClickTrayEntry(entry); }
1297
1298inline void TrayEntry::Click() { SDL::ClickTrayEntry(m_resource); }
1299
1314inline void DestroyTray(TrayRaw tray) { SDL_DestroyTray(tray); }
1315
1316inline void Tray::Destroy() { DestroyTray(release()); }
1317
1332{
1333 return SDL_GetTrayEntryParent(entry);
1334}
1335
1337{
1338 return SDL::GetTrayEntryParent(m_resource);
1339}
1340
1360{
1361 return SDL_GetTrayMenuParentEntry(menu);
1362}
1363
1365{
1366 return SDL::GetTrayMenuParentEntry(m_trayMenu);
1367}
1368
1388{
1389 return SDL_GetTrayMenuParentTray(menu);
1390}
1391
1393{
1394 return SDL::GetTrayMenuParentTray(m_trayMenu);
1395}
1396
1407inline void UpdateTrays() { SDL_UpdateTrays(); }
1408
1410
1412{
1413 return InsertEntry(-1, std::move(label), flags);
1414}
1415
1416inline void TrayEntry::SetCallback(TrayCB callback)
1417{
1419 SetCallback([](void* userdata,
1420 SDL_TrayEntry* entry) { Wrapper::Call(userdata, entry); },
1421 Wrapper::Wrap(get(), std::move(callback)));
1422}
1423
1424} // namespace SDL
1425
1426#endif /* SDL3PP_TRAY_H_ */
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
An opaque handle representing an entry on a system tray object.
Definition: SDL3pp_tray.h:516
constexpr TrayEntryRaw get() const
Retrieves underlying TrayEntryRaw.
Definition: SDL3pp_tray.h:553
constexpr TrayEntry()=default
Default ctor.
TrayEntry & operator=(TrayEntry other)
Assignment operator.
Definition: SDL3pp_tray.h:546
constexpr TrayEntry(const TrayEntry &other)=default
Copy constructor.
void SetCallback(TrayCB callback)
Sets a callback to be invoked when the entry is selected.
Definition: SDL3pp_tray.h:1416
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_tray.h:567
constexpr auto operator<=>(const TrayEntry &other) const =default
Comparison.
constexpr TrayEntry(TrayEntry &&other)
Move constructor.
Definition: SDL3pp_tray.h:537
~TrayEntry()
Destructor.
Definition: SDL3pp_tray.h:543
constexpr TrayEntry(const TrayEntryRaw resource)
Constructs from TrayEntryParam.
Definition: SDL3pp_tray.h:528
constexpr TrayEntryRaw release()
Retrieves underlying TrayEntryRaw and clear this.
Definition: SDL3pp_tray.h:556
An opaque handle representing a menu/submenu on a system tray object.
Definition: SDL3pp_tray.h:378
constexpr TrayMenu(TrayMenuRaw trayMenu={})
Wraps TrayMenu.
Definition: SDL3pp_tray.h:387
TrayEntry AppendEntry(StringParam label, TrayEntryFlags flags)
Appends a tray entry.
Definition: SDL3pp_tray.h:1411
An opaque handle representing a toplevel system tray object.
Definition: SDL3pp_tray.h:164
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_tray.h:249
constexpr Tray()=default
Default ctor.
constexpr TrayRaw release()
Retrieves underlying TrayRaw and clear this.
Definition: SDL3pp_tray.h:238
constexpr Tray(const TrayRaw resource)
Constructs from TrayParam.
Definition: SDL3pp_tray.h:178
~Tray()
Destructor.
Definition: SDL3pp_tray.h:225
constexpr auto operator<=>(const Tray &other) const =default
Comparison.
constexpr Tray(Tray &&other)
Move constructor.
Definition: SDL3pp_tray.h:187
constexpr TrayRaw get() const
Retrieves underlying TrayRaw.
Definition: SDL3pp_tray.h:235
Tray(SurfaceParam icon, StringParam tooltip)
Create an icon to be placed in the operating system's tray, or equivalent.
Definition: SDL3pp_tray.h:219
constexpr Tray(const Tray &other)=delete
Copy constructor.
Tray & operator=(Tray other)
Assignment operator.
Definition: SDL3pp_tray.h:228
Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:328
TrayMenu CreateSubmenu()
Create a submenu for a system tray entry.
Definition: SDL3pp_tray.h:947
TrayEntry InsertEntry(int pos, StringParam label, TrayEntryFlags flags)
Insert a tray entry at a given position.
Definition: SDL3pp_tray.h:1090
SDL_TrayMenu * TrayMenuRaw
Alias to raw representation for TrayMenu.
Definition: SDL3pp_tray.h:62
void SetTooltip(StringParam tooltip)
Updates the system tray icon's tooltip.
Definition: SDL3pp_tray.h:886
void SetTrayEntryEnabled(TrayEntryParam entry, bool enabled)
Sets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1223
void UpdateTrays()
Update the trays.
Definition: SDL3pp_tray.h:1407
bool GetTrayEntryEnabled(TrayEntryParam entry)
Gets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1248
TrayEntry InsertTrayEntryAt(TrayMenu menu, int pos, StringParam label, TrayEntryFlags flags)
Insert a tray entry at a given position.
Definition: SDL3pp_tray.h:1082
TrayEntryParam GetParentEntry() const
Gets the entry for which the menu is a submenu, if the current menu is a submenu.
Definition: SDL3pp_tray.h:1364
TrayMenu GetMenu() const
Gets a previously created tray menu.
Definition: SDL3pp_tray.h:976
void DestroyTray(TrayRaw tray)
Destroys a tray object.
Definition: SDL3pp_tray.h:1314
constexpr TrayEntryFlags TRAYENTRY_BUTTON
Make the entry a simple button. Required.
Definition: SDL3pp_tray.h:116
void SetIcon(SurfaceParam icon)
Updates the system tray icon's icon.
Definition: SDL3pp_tray.h:863
void SetChecked(bool checked)
Sets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1176
constexpr TrayEntryFlags TRAYENTRY_DISABLED
Make the entry disabled. Optional.
Definition: SDL3pp_tray.h:125
TrayParam GetTrayMenuParentTray(TrayMenuRaw menu)
Gets the tray for which this menu is the first-level menu, if the current menu isn't a submenu.
Definition: SDL3pp_tray.h:1387
TrayMenu CreateMenu()
Create a menu for a system tray.
Definition: SDL3pp_tray.h:918
void SetTrayEntryCallback(TrayEntryParam entry, TrayCallback callback, void *userdata)
Sets a callback to be invoked when the entry is selected.
Definition: SDL3pp_tray.h:1274
TrayMenu GetParent()
Gets the menu containing a certain tray entry.
Definition: SDL3pp_tray.h:1336
void SetTrayEntryLabel(TrayEntryParam entry, StringParam label)
Sets the label of an entry.
Definition: SDL3pp_tray.h:1117
TrayMenu GetTrayEntryParent(TrayEntryParam entry)
Gets the menu containing a certain tray entry.
Definition: SDL3pp_tray.h:1331
std::function< void(TrayEntryRaw)> TrayCB
A callback that is invoked when a tray entry is selected.
Definition: SDL3pp_tray.h:154
void Click()
Simulate a click on a tray entry.
Definition: SDL3pp_tray.h:1298
std::span< TrayEntry > GetTrayEntries(TrayMenu menu)
Returns a list of entries in the menu, in order.
Definition: SDL3pp_tray.h:1026
void SetTrayIcon(TrayParam tray, SurfaceParam icon)
Updates the system tray icon's icon.
Definition: SDL3pp_tray.h:858
TrayMenu GetTraySubmenu(TrayEntryParam entry)
Gets a previously created tray entry submenu.
Definition: SDL3pp_tray.h:1000
void Destroy()
Destroys a tray object.
Definition: SDL3pp_tray.h:1316
void SetEnabled(bool enabled)
Sets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1228
TrayMenu CreateTrayMenu(TrayParam tray)
Create a menu for a system tray.
Definition: SDL3pp_tray.h:913
void RemoveTrayEntry(TrayEntryRaw entry)
Removes a tray entry.
Definition: SDL3pp_tray.h:1052
void ClickTrayEntry(TrayEntryParam entry)
Simulate a click on a tray entry.
Definition: SDL3pp_tray.h:1296
Tray CreateTray(SurfaceParam icon, StringParam tooltip)
Create an icon to be placed in the operating system's tray, or equivalent.
Definition: SDL3pp_tray.h:840
TrayEntryParam GetTrayMenuParentEntry(TrayMenuRaw menu)
Gets the entry for which the menu is a submenu, if the current menu is a submenu.
Definition: SDL3pp_tray.h:1359
bool GetTrayEntryChecked(TrayEntryParam entry)
Gets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1198
const char * GetTrayEntryLabel(TrayEntryParam entry)
Gets the label of an entry.
Definition: SDL3pp_tray.h:1144
TrayMenu GetSubmenu()
Gets a previously created tray entry submenu.
Definition: SDL3pp_tray.h:1005
const char * GetLabel() const
Gets the label of an entry.
Definition: SDL3pp_tray.h:1149
bool GetChecked() const
Gets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1203
SDL_TrayEntry * TrayEntryRaw
Alias to raw representation for TrayEntry.
Definition: SDL3pp_tray.h:71
std::span< TrayEntry > GetEntries()
Returns a list of entries in the menu, in order.
Definition: SDL3pp_tray.h:1034
SDL_TrayCallback TrayCallback
A callback that is invoked when a tray entry is selected.
Definition: SDL3pp_tray.h:142
bool GetEnabled() const
Gets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1253
void SetTrayTooltip(TrayParam tray, StringParam tooltip)
Updates the system tray icon's tooltip.
Definition: SDL3pp_tray.h:881
TrayParam GetParentTray() const
Gets the tray for which this menu is the first-level menu, if the current menu isn't a submenu.
Definition: SDL3pp_tray.h:1392
constexpr TrayEntryFlags TRAYENTRY_CHECKED
Make the entry checked. This is valid only for checkboxes. Optional.
Definition: SDL3pp_tray.h:129
void Remove()
Removes a tray entry.
Definition: SDL3pp_tray.h:1054
Uint32 TrayEntryFlags
Flags that control the creation of system tray entries.
Definition: SDL3pp_tray.h:114
TrayMenu GetTrayMenu(TrayParam tray)
Gets a previously created tray menu.
Definition: SDL3pp_tray.h:974
TrayMenu CreateTraySubmenu(TrayEntryParam entry)
Create a submenu for a system tray entry.
Definition: SDL3pp_tray.h:942
void SetTrayEntryChecked(TrayEntryParam entry, bool checked)
Sets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1171
constexpr TrayEntryFlags TRAYENTRY_SUBMENU
Prepare the entry to have a submenu. Required.
Definition: SDL3pp_tray.h:122
constexpr TrayEntryFlags TRAYENTRY_CHECKBOX
Make the entry a checkbox. Required.
Definition: SDL3pp_tray.h:119
void SetLabel(StringParam label)
Sets the label of an entry.
Definition: SDL3pp_tray.h:1122
SDL_Tray * TrayRaw
Alias to raw representation for Tray.
Definition: SDL3pp_tray.h:26
Main include header for the SDL3pp library.
Store callbacks by key.
Definition: SDL3pp_callbackWrapper.h:222
Safely wrap Surface for non owning parameters.
Definition: SDL3pp_surface.h:46
Safely wrap TrayEntry for non owning parameters.
Definition: SDL3pp_tray.h:78
constexpr TrayEntryParam(TrayEntryRaw value)
Constructs from TrayEntryRaw.
Definition: SDL3pp_tray.h:82
constexpr auto operator<=>(const TrayEntryParam &other) const =default
Comparison.
constexpr TrayEntryParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_tray.h:88
TrayEntryRaw value
parameter's TrayEntryRaw
Definition: SDL3pp_tray.h:79
RAII owning version TrayEntry.
Definition: SDL3pp_tray.h:802
constexpr TrayEntryScoped(TrayEntry &&other)
Move constructor.
Definition: SDL3pp_tray.h:808
~TrayEntryScoped()
Destructor.
Definition: SDL3pp_tray.h:814
Safely wrap Tray for non owning parameters.
Definition: SDL3pp_tray.h:33
TrayRaw value
parameter's TrayRaw
Definition: SDL3pp_tray.h:34
constexpr auto operator<=>(const TrayParam &other) const =default
Comparison.
constexpr TrayParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_tray.h:43
constexpr TrayParam(TrayRaw value)
Constructs from TrayRaw.
Definition: SDL3pp_tray.h:37
Semi-safe reference for Tray.
Definition: SDL3pp_tray.h:349
TrayRef(TrayParam resource)
Constructs from TrayParam.
Definition: SDL3pp_tray.h:357
~TrayRef()
Destructor.
Definition: SDL3pp_tray.h:369
TrayRef(const TrayRef &other)
Copy constructor.
Definition: SDL3pp_tray.h:363