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
21// Forward decl
22struct Tray;
23
25using TrayRaw = SDL_Tray*;
26
27// Forward decl
28struct TrayRef;
29
31using TrayMenuRaw = SDL_TrayMenu*;
32
33// Forward decl
34struct TrayMenu;
35
36// Forward decl
37struct TrayEntry;
38
40using TrayEntryRaw = SDL_TrayEntry*;
41
42// Forward decl
43struct TrayEntryScoped;
44
47
60
62 SDL_TRAYENTRY_BUTTON;
63
65 SDL_TRAYENTRY_CHECKBOX;
66
68 SDL_TRAYENTRY_SUBMENU;
69
71 SDL_TRAYENTRY_DISABLED;
72
74constexpr TrayEntryFlags TRAYENTRY_CHECKED = SDL_TRAYENTRY_CHECKED;
75
87using TrayCallback = void(SDLCALL*)(void* userdata, TrayEntryRaw entry);
88
101
109class Tray
110{
111 TrayRaw m_resource = nullptr;
112
113public:
115 constexpr Tray(std::nullptr_t = nullptr) noexcept
116 : m_resource(nullptr)
117 {
118 }
119
127 constexpr explicit Tray(TrayRaw resource) noexcept
128 : m_resource(resource)
129 {
130 }
131
133 constexpr Tray(const Tray& other) noexcept = delete;
134
136 constexpr Tray(Tray&& other) noexcept
137 : Tray(other.release())
138 {
139 }
140
141 constexpr Tray(const TrayRef& other) = delete;
142
143 constexpr Tray(TrayRef&& other) = delete;
144
169
171 ~Tray() { SDL_DestroyTray(m_resource); }
172
174 constexpr Tray& operator=(Tray&& other) noexcept
175 {
176 std::swap(m_resource, other.m_resource);
177 return *this;
178 }
179
181 Tray& operator=(const Tray& other) = delete;
182
184 constexpr TrayRaw get() const noexcept { return m_resource; }
185
187 constexpr TrayRaw release() noexcept
188 {
189 auto r = m_resource;
190 m_resource = nullptr;
191 return r;
192 }
193
195 constexpr auto operator<=>(const Tray& other) const noexcept = default;
196
198 constexpr explicit operator bool() const noexcept { return !!m_resource; }
199
212 void Destroy();
213
226 void SetIcon(SurfaceRef icon);
227
240 void SetTooltip(StringParam tooltip);
241
264
286 TrayMenu GetMenu() const;
287};
288
294struct TrayRef : Tray
295{
296 using Tray::Tray;
297
305 constexpr TrayRef(TrayRaw resource) noexcept
306 : Tray(resource)
307 {
308 }
309
317 constexpr TrayRef(const Tray& resource) noexcept
318 : Tray(resource.get())
319 {
320 }
321
329 constexpr TrayRef(Tray&& resource) noexcept
330 : Tray(std::move(resource).release())
331 {
332 }
333
335 constexpr TrayRef(const TrayRef& other) noexcept
336 : Tray(other.get())
337 {
338 }
339
341 constexpr TrayRef(TrayRef&& other) noexcept
342 : Tray(other.get())
343 {
344 }
345
348
350 TrayRef& operator=(const TrayRef& other) noexcept
351 {
352 release();
353 Tray::operator=(Tray(other.get()));
354 return *this;
355 }
356
358 constexpr operator TrayRaw() const noexcept { return get(); }
359};
360
367{
368 TrayMenuRaw m_trayMenu;
369
370public:
376 constexpr TrayMenu(TrayMenuRaw trayMenu = {}) noexcept
377 : m_trayMenu(trayMenu)
378 {
379 }
380
386 constexpr operator TrayMenuRaw() const noexcept { return m_trayMenu; }
387
403 std::span<TrayEntry> GetEntries();
404
430 TrayEntry InsertEntry(int pos, StringParam label, TrayEntryFlags flags);
431
457
476
494 TrayRef GetParentTray() const;
495};
496
505{
506 TrayEntryRaw m_resource = nullptr;
507
508public:
510 constexpr TrayEntry(std::nullptr_t = nullptr) noexcept
511 : m_resource(nullptr)
512 {
513 }
514
520 constexpr TrayEntry(TrayEntryRaw resource) noexcept
521 : m_resource(resource)
522 {
523 }
524
526 constexpr operator TrayEntryRaw() const noexcept { return m_resource; }
527
529 constexpr TrayEntryRaw get() const noexcept { return m_resource; }
530
532 constexpr TrayEntryRaw release() noexcept
533 {
534 auto r = m_resource;
535 m_resource = nullptr;
536 return r;
537 }
538
540 constexpr auto operator<=>(const TrayEntry& other) const noexcept = default;
541
543 constexpr explicit operator bool() const noexcept { return !!m_resource; }
544
556 void Remove();
557
580
603
623 void SetLabel(StringParam label);
624
641 const char* GetLabel() const;
642
659 void SetChecked(bool checked);
660
677 bool GetChecked() const;
678
693 void SetEnabled(bool enabled);
694
709 bool GetEnabled() const;
710
724 void SetCallback(TrayCB callback);
725
741 void SetCallback(TrayCallback callback, void* userdata);
742
751 void Click();
752
766};
767
770{
772
773 constexpr TrayEntryScoped(const TrayEntryScoped& other) = delete;
774
776 constexpr TrayEntryScoped(TrayEntryScoped&& other) noexcept
777 : TrayEntry(other.release())
778 {
779 }
780
782 constexpr TrayEntryScoped(TrayEntry&& other) noexcept
783 : TrayEntry(std::move(other).release())
784 {
785 }
786
789};
790
815{
816 return Tray(icon, std::move(tooltip));
817}
818
819inline Tray::Tray(SurfaceRef icon, StringParam tooltip)
820 : m_resource(SDL_CreateTray(icon, tooltip))
821{
822}
823
837inline void SetTrayIcon(TrayRef tray, SurfaceRef icon)
838{
839 SDL_SetTrayIcon(tray, icon);
840}
841
842inline void Tray::SetIcon(SurfaceRef icon)
843{
844 SDL::SetTrayIcon(m_resource, icon);
845}
846
860inline void SetTrayTooltip(TrayRef tray, StringParam tooltip)
861{
862 SDL_SetTrayTooltip(tray, tooltip);
863}
864
865inline void Tray::SetTooltip(StringParam tooltip)
866{
867 SDL::SetTrayTooltip(m_resource, std::move(tooltip));
868}
869
893{
894 return SDL_CreateTrayMenu(tray);
895}
896
897inline TrayMenu Tray::CreateMenu() { return SDL::CreateTrayMenu(m_resource); }
898
922{
923 return SDL_CreateTraySubmenu(entry);
924}
925
927{
928 return SDL::CreateTraySubmenu(m_resource);
929}
930
953inline TrayMenu GetTrayMenu(TrayRef tray) { return SDL_GetTrayMenu(tray); }
954
955inline TrayMenu Tray::GetMenu() const { return SDL::GetTrayMenu(m_resource); }
956
980{
981 return SDL_GetTraySubmenu(entry);
982}
983
985{
986 return SDL::GetTraySubmenu(m_resource);
987}
988
1005inline std::span<TrayEntry> GetTrayEntries(TrayMenu menu)
1006{
1007 int count;
1008 auto entries = SDL_GetTrayEntries(menu, &count);
1009 return std::span<TrayEntry>{reinterpret_cast<TrayEntry*>(entries),
1010 size_t(count)};
1011}
1012
1013inline std::span<TrayEntry> TrayMenu::GetEntries()
1014{
1015 return SDL::GetTrayEntries(m_trayMenu);
1016}
1017
1031inline void RemoveTrayEntry(TrayEntryRaw entry) { SDL_RemoveTrayEntry(entry); }
1032
1034
1062 int pos,
1063 StringParam label,
1064 TrayEntryFlags flags)
1065{
1066 return SDL_InsertTrayEntryAt(menu, pos, label, flags);
1067}
1068
1070 StringParam label,
1071 TrayEntryFlags flags)
1072{
1073 return SDL::InsertTrayEntryAt(m_trayMenu, pos, std::move(label), flags);
1074}
1075
1097{
1098 SDL_SetTrayEntryLabel(entry, label);
1099}
1100
1102{
1103 SDL::SetTrayEntryLabel(m_resource, std::move(label));
1104}
1105
1123inline const char* GetTrayEntryLabel(TrayEntryRef entry)
1124{
1125 return SDL_GetTrayEntryLabel(entry);
1126}
1127
1128inline const char* TrayEntry::GetLabel() const
1129{
1130 return SDL::GetTrayEntryLabel(m_resource);
1131}
1132
1150inline void SetTrayEntryChecked(TrayEntryRef entry, bool checked)
1151{
1152 SDL_SetTrayEntryChecked(entry, checked);
1153}
1154
1155inline void TrayEntry::SetChecked(bool checked)
1156{
1157 SDL::SetTrayEntryChecked(m_resource, checked);
1158}
1159
1178{
1179 return SDL_GetTrayEntryChecked(entry);
1180}
1181
1182inline bool TrayEntry::GetChecked() const
1183{
1184 return SDL::GetTrayEntryChecked(m_resource);
1185}
1186
1202inline void SetTrayEntryEnabled(TrayEntryRef entry, bool enabled)
1203{
1204 SDL_SetTrayEntryEnabled(entry, enabled);
1205}
1206
1207inline void TrayEntry::SetEnabled(bool enabled)
1208{
1209 SDL::SetTrayEntryEnabled(m_resource, enabled);
1210}
1211
1228{
1229 return SDL_GetTrayEntryEnabled(entry);
1230}
1231
1232inline bool TrayEntry::GetEnabled() const
1233{
1234 return SDL::GetTrayEntryEnabled(m_resource);
1235}
1236
1254 TrayCallback callback,
1255 void* userdata)
1256{
1257 SDL_SetTrayEntryCallback(entry, callback, userdata);
1258}
1259
1260inline void TrayEntry::SetCallback(TrayCallback callback, void* userdata)
1261{
1262 SDL::SetTrayEntryCallback(m_resource, callback, userdata);
1263}
1264
1275inline void ClickTrayEntry(TrayEntryRef entry) { SDL_ClickTrayEntry(entry); }
1276
1277inline void TrayEntry::Click() { SDL::ClickTrayEntry(m_resource); }
1278
1293inline void DestroyTray(TrayRaw tray) { SDL_DestroyTray(tray); }
1294
1295inline void Tray::Destroy() { DestroyTray(release()); }
1296
1311{
1312 return SDL_GetTrayEntryParent(entry);
1313}
1314
1316{
1317 return SDL::GetTrayEntryParent(m_resource);
1318}
1319
1339{
1340 return SDL_GetTrayMenuParentEntry(menu);
1341}
1342
1344{
1345 return SDL::GetTrayMenuParentEntry(m_trayMenu);
1346}
1347
1367{
1368 return SDL_GetTrayMenuParentTray(menu);
1369}
1370
1372{
1373 return SDL::GetTrayMenuParentTray(m_trayMenu);
1374}
1375
1386inline void UpdateTrays() { SDL_UpdateTrays(); }
1387
1389
1391{
1392 return InsertEntry(-1, std::move(label), flags);
1393}
1394
1395inline void TrayEntry::SetCallback(TrayCB callback)
1396{
1397 SetCallback(callback.wrapper, callback.data);
1398}
1399
1400} // namespace SDL
1401
1402#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:505
constexpr TrayEntry(TrayEntryRaw resource) noexcept
Constructs from raw TrayEntry.
Definition: SDL3pp_tray.h:520
void SetCallback(TrayCB callback)
Sets a callback to be invoked when the entry is selected.
Definition: SDL3pp_tray.h:1395
constexpr TrayEntryRaw get() const noexcept
Retrieves underlying TrayEntryRaw.
Definition: SDL3pp_tray.h:529
constexpr TrayEntry(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_tray.h:510
constexpr TrayEntryRaw release() noexcept
Retrieves underlying TrayEntryRaw and clear this.
Definition: SDL3pp_tray.h:532
constexpr auto operator<=>(const TrayEntry &other) const noexcept=default
Comparison.
An opaque handle representing a menu/submenu on a system tray object.
Definition: SDL3pp_tray.h:367
TrayEntry AppendEntry(StringParam label, TrayEntryFlags flags)
Appends a tray entry.
Definition: SDL3pp_tray.h:1390
constexpr TrayMenu(TrayMenuRaw trayMenu={}) noexcept
Wraps TrayMenu.
Definition: SDL3pp_tray.h:376
An opaque handle representing a toplevel system tray object.
Definition: SDL3pp_tray.h:110
constexpr Tray(const Tray &other) noexcept=delete
Copy constructor.
constexpr TrayRaw get() const noexcept
Retrieves underlying TrayRaw.
Definition: SDL3pp_tray.h:184
constexpr Tray(TrayRaw resource) noexcept
Constructs from raw Tray.
Definition: SDL3pp_tray.h:127
~Tray()
Destructor.
Definition: SDL3pp_tray.h:171
Tray & operator=(const Tray &other)=delete
Assignment operator.
constexpr Tray(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_tray.h:115
constexpr Tray & operator=(Tray &&other) noexcept
Assignment operator.
Definition: SDL3pp_tray.h:174
constexpr auto operator<=>(const Tray &other) const noexcept=default
Comparison.
constexpr Tray(Tray &&other) noexcept
Move constructor.
Definition: SDL3pp_tray.h:136
constexpr TrayRaw release() noexcept
Retrieves underlying TrayRaw and clear this.
Definition: SDL3pp_tray.h:187
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:281
void(SDLCALL *)(void *userdata, TrayEntryRaw entry) TrayCallback
A callback that is invoked when a tray entry is selected.
Definition: SDL3pp_tray.h:87
TrayMenu CreateSubmenu()
Create a submenu for a system tray entry.
Definition: SDL3pp_tray.h:926
void SetTrayIcon(TrayRef tray, SurfaceRef icon)
Updates the system tray icon's icon.
Definition: SDL3pp_tray.h:837
TrayEntry InsertEntry(int pos, StringParam label, TrayEntryFlags flags)
Insert a tray entry at a given position.
Definition: SDL3pp_tray.h:1069
SDL_TrayMenu * TrayMenuRaw
Alias to raw representation for TrayMenu.
Definition: SDL3pp_tray.h:31
void SetTooltip(StringParam tooltip)
Updates the system tray icon's tooltip.
Definition: SDL3pp_tray.h:865
void UpdateTrays()
Update the trays.
Definition: SDL3pp_tray.h:1386
TrayEntry InsertTrayEntryAt(TrayMenu menu, int pos, StringParam label, TrayEntryFlags flags)
Insert a tray entry at a given position.
Definition: SDL3pp_tray.h:1061
void SetTrayEntryCallback(TrayEntryRef entry, TrayCallback callback, void *userdata)
Sets a callback to be invoked when the entry is selected.
Definition: SDL3pp_tray.h:1253
TrayMenu GetMenu() const
Gets a previously created tray menu.
Definition: SDL3pp_tray.h:955
void DestroyTray(TrayRaw tray)
Destroys a tray object.
Definition: SDL3pp_tray.h:1293
void SetTrayEntryLabel(TrayEntryRef entry, StringParam label)
Sets the label of an entry.
Definition: SDL3pp_tray.h:1096
constexpr TrayEntryFlags TRAYENTRY_BUTTON
Make the entry a simple button. Required.
Definition: SDL3pp_tray.h:61
TrayMenu CreateTraySubmenu(TrayEntryRef entry)
Create a submenu for a system tray entry.
Definition: SDL3pp_tray.h:921
void SetChecked(bool checked)
Sets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1155
constexpr TrayEntryFlags TRAYENTRY_DISABLED
Make the entry disabled. Optional.
Definition: SDL3pp_tray.h:70
TrayMenu CreateTrayMenu(TrayRef tray)
Create a menu for a system tray.
Definition: SDL3pp_tray.h:892
TrayMenu CreateMenu()
Create a menu for a system tray.
Definition: SDL3pp_tray.h:897
TrayEntryRef GetParentEntry() const
Gets the entry for which the menu is a submenu, if the current menu is a submenu.
Definition: SDL3pp_tray.h:1343
void SetTrayEntryChecked(TrayEntryRef entry, bool checked)
Sets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1150
TrayMenu GetParent()
Gets the menu containing a certain tray entry.
Definition: SDL3pp_tray.h:1315
TrayEntryRef GetTrayMenuParentEntry(TrayMenuRaw menu)
Gets the entry for which the menu is a submenu, if the current menu is a submenu.
Definition: SDL3pp_tray.h:1338
bool GetTrayEntryChecked(TrayEntryRef entry)
Gets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1177
TrayMenu GetTrayMenu(TrayRef tray)
Gets a previously created tray menu.
Definition: SDL3pp_tray.h:953
TrayRef 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:1371
void Click()
Simulate a click on a tray entry.
Definition: SDL3pp_tray.h:1277
std::span< TrayEntry > GetTrayEntries(TrayMenu menu)
Returns a list of entries in the menu, in order.
Definition: SDL3pp_tray.h:1005
void SetTrayEntryEnabled(TrayEntryRef entry, bool enabled)
Sets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1202
const char * GetTrayEntryLabel(TrayEntryRef entry)
Gets the label of an entry.
Definition: SDL3pp_tray.h:1123
TrayMenu GetTraySubmenu(TrayEntryRef entry)
Gets a previously created tray entry submenu.
Definition: SDL3pp_tray.h:979
void Destroy()
Destroys a tray object.
Definition: SDL3pp_tray.h:1295
Tray CreateTray(SurfaceRef icon, StringParam tooltip)
Create an icon to be placed in the operating system's tray, or equivalent.
Definition: SDL3pp_tray.h:814
void SetEnabled(bool enabled)
Sets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1207
void RemoveTrayEntry(TrayEntryRaw entry)
Removes a tray entry.
Definition: SDL3pp_tray.h:1031
TrayMenu GetTrayEntryParent(TrayEntryRef entry)
Gets the menu containing a certain tray entry.
Definition: SDL3pp_tray.h:1310
TrayMenu GetSubmenu()
Gets a previously created tray entry submenu.
Definition: SDL3pp_tray.h:984
void SetTrayTooltip(TrayRef tray, StringParam tooltip)
Updates the system tray icon's tooltip.
Definition: SDL3pp_tray.h:860
void ClickTrayEntry(TrayEntryRef entry)
Simulate a click on a tray entry.
Definition: SDL3pp_tray.h:1275
const char * GetLabel() const
Gets the label of an entry.
Definition: SDL3pp_tray.h:1128
void SetIcon(SurfaceRef icon)
Updates the system tray icon's icon.
Definition: SDL3pp_tray.h:842
bool GetChecked() const
Gets whether or not an entry is checked.
Definition: SDL3pp_tray.h:1182
bool GetTrayEntryEnabled(TrayEntryRef entry)
Gets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1227
SDL_TrayEntry * TrayEntryRaw
Alias to raw representation for TrayEntry.
Definition: SDL3pp_tray.h:40
std::span< TrayEntry > GetEntries()
Returns a list of entries in the menu, in order.
Definition: SDL3pp_tray.h:1013
TrayRef 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:1366
bool GetEnabled() const
Gets whether or not an entry is enabled.
Definition: SDL3pp_tray.h:1232
constexpr TrayEntryFlags TRAYENTRY_CHECKED
Make the entry checked. This is valid only for checkboxes. Optional.
Definition: SDL3pp_tray.h:74
void Remove()
Removes a tray entry.
Definition: SDL3pp_tray.h:1033
Uint32 TrayEntryFlags
Flags that control the creation of system tray entries.
Definition: SDL3pp_tray.h:59
constexpr TrayEntryFlags TRAYENTRY_SUBMENU
Prepare the entry to have a submenu. Required.
Definition: SDL3pp_tray.h:67
constexpr TrayEntryFlags TRAYENTRY_CHECKBOX
Make the entry a checkbox. Required.
Definition: SDL3pp_tray.h:64
void SetLabel(StringParam label)
Sets the label of an entry.
Definition: SDL3pp_tray.h:1101
SDL_Tray * TrayRaw
Alias to raw representation for Tray.
Definition: SDL3pp_tray.h:25
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:169
Reference for Surface.
Definition: SDL3pp_surface.h:1975
RAII owning version TrayEntry.
Definition: SDL3pp_tray.h:770
constexpr TrayEntryScoped(TrayEntryScoped &&other) noexcept
Move constructor.
Definition: SDL3pp_tray.h:776
~TrayEntryScoped()
Destructor.
Definition: SDL3pp_tray.h:788
constexpr TrayEntryScoped(TrayEntry &&other) noexcept
Move constructor.
Definition: SDL3pp_tray.h:782
Reference for Tray.
Definition: SDL3pp_tray.h:295
constexpr TrayRef(TrayRaw resource) noexcept
Constructs from raw Tray.
Definition: SDL3pp_tray.h:305
~TrayRef()
Destructor.
Definition: SDL3pp_tray.h:347
constexpr TrayRef(const TrayRef &other) noexcept
Copy constructor.
Definition: SDL3pp_tray.h:335
constexpr TrayRef(const Tray &resource) noexcept
Constructs from Tray.
Definition: SDL3pp_tray.h:317
constexpr TrayRef(Tray &&resource) noexcept
Constructs from Tray.
Definition: SDL3pp_tray.h:329
TrayRef & operator=(const TrayRef &other) noexcept
Assignment operator.
Definition: SDL3pp_tray.h:350
constexpr TrayRef(TrayRef &&other) noexcept
Move constructor.
Definition: SDL3pp_tray.h:341