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
23// Forward decl
24struct TrayMenu;
25
26// Forward decl
27struct TrayBase;
28
29// Forward decl
30struct TrayRef;
31
32// Forward decl
33struct Tray;
34
35// Forward decl
36struct TrayEntryBase;
37
38// Forward decl
39struct TrayEntryRef;
40
41// Forward decl
42struct TrayEntry;
43
51
63using TrayEntryFlags = Uint32;
64
66 SDL_TRAYENTRY_BUTTON;
67
69 SDL_TRAYENTRY_CHECKBOX;
70
72 SDL_TRAYENTRY_SUBMENU;
73
75 SDL_TRAYENTRY_DISABLED;
76
80constexpr TrayEntryFlags TRAYENTRY_CHECKED = SDL_TRAYENTRY_CHECKED;
81
92struct TrayBase : Resource<SDL_Tray*>
93{
95
119 : Resource(CheckError(SDL_CreateTray(icon.get(), tooltip)))
120 {
121 }
122
135 void SetIcon(SurfaceBase& icon) { SDL_SetTrayIcon(get(), icon.get()); }
136
149 void SetTooltip(StringParam tooltip) { SDL_SetTrayTooltip(get(), tooltip); }
150
173
195 TrayMenu GetMenu() const;
196};
197
207{
208 using TrayBase::TrayBase;
209
213 constexpr TrayRef(const TrayRef& other)
214 : TrayBase(other.get())
215 {
216 }
217
221 constexpr TrayRef(TrayRef&& other)
222 : TrayBase(other.release())
223 {
224 }
225
229 constexpr ~TrayRef() = default;
230
235 {
236 release(other.release());
237 return *this;
238 }
239
252 void reset(SDL_Tray* newResource = {})
253 {
254 SDL_DestroyTray(release(newResource));
255 }
256};
257
266struct Tray : TrayRef
267{
268 using TrayRef::TrayRef;
269
273 constexpr explicit Tray(SDL_Tray* resource = {})
274 : TrayRef(resource)
275 {
276 }
277
278 constexpr Tray(const Tray& other) = delete;
279
283 constexpr Tray(Tray&& other) = default;
284
288 ~Tray() { reset(); }
289
294 {
295 reset(other.release());
296 return *this;
297 }
298};
299
311using TrayCallback = SDL_TrayCallback;
312
322using TrayCB = std::function<void(TrayEntryRef)>;
323
330{
331 SDL_TrayMenu* m_trayMenu;
332
333public:
339 constexpr TrayMenu(SDL_TrayMenu* trayMenu = {})
340 : m_trayMenu(trayMenu)
341 {
342 }
343
347 constexpr bool operator==(const TrayMenu& other) const = default;
348
352 constexpr bool operator==(SDL_TrayMenu* trayMenu) const
353 {
354 return operator==(TrayMenu(trayMenu));
355 }
356
362 constexpr operator SDL_TrayMenu*() const { return m_trayMenu; }
363
369 constexpr explicit operator bool() const { return m_trayMenu != nullptr; }
370
386 std::span<TrayEntry> GetEntries();
387
415 StringParam label,
416 TrayEntryFlags flags);
417
443
462
480 TrayRef GetParentTray() const;
481};
482
493struct TrayEntryBase : Resource<SDL_TrayEntry*>
494{
495 using Resource::Resource;
496
518 TrayMenu CreateSubmenu() { return SDL_CreateTraySubmenu(get()); }
519
541 TrayMenu GetSubmenu() { return SDL_GetTraySubmenu(get()); }
542
562 void SetLabel(StringParam label) { SDL_SetTrayEntryLabel(get(), label); }
563
580 const char* GetLabel() const { return SDL_GetTrayEntryLabel(get()); }
581
598 void SetChecked(bool checked) { SDL_SetTrayEntryChecked(get(), checked); }
599
616 bool GetChecked() const { return SDL_GetTrayEntryChecked(get()); }
617
632 void SetEnabled(bool enabled) { SDL_SetTrayEntryEnabled(get(), enabled); }
633
648 bool GetEnabled() const { return SDL_GetTrayEntryEnabled(get()); }
649
663 void SetCallback(TrayCB callback);
664
680 void SetCallback(TrayCallback callback, void* userdata)
681 {
682 SDL_SetTrayEntryCallback(get(), callback, userdata);
683 }
684
694 void Click() { SDL_ClickTrayEntry(get()); }
695
708 TrayMenu GetParent() { return SDL_GetTrayEntryParent(get()); }
709};
710
720{
721 using TrayEntryBase::TrayEntryBase;
722
726 constexpr TrayEntryRef(const TrayEntryRef& other)
727 : TrayEntryBase(other.get())
728 {
729 }
730
734 constexpr TrayEntryRef(TrayEntryRef&& other)
735 : TrayEntryBase(other.release())
736 {
737 }
738
742 constexpr ~TrayEntryRef() = default;
743
748 {
749 release(other.release());
750 return *this;
751 }
752
764 void reset(SDL_TrayEntry* newResource = {})
765 {
766 SDL_RemoveTrayEntry(release(newResource));
767 }
768
780 void Remove() { reset(); }
781};
782
792{
794
798 constexpr explicit TrayEntry(SDL_TrayEntry* resource = {})
799 : TrayEntryRef(resource)
800 {
801 }
802
803 constexpr TrayEntry(const TrayEntry& other) = delete;
804
808 constexpr TrayEntry(TrayEntry&& other) = default;
809
814
819 {
820 reset(other.release());
821 return *this;
822 }
823};
824
825inline TrayMenu TrayBase::CreateMenu() { return SDL_CreateTrayMenu(get()); }
826
827inline TrayMenu TrayBase::GetMenu() const { return SDL_GetTrayMenu(get()); }
828
829inline std::span<TrayEntry> TrayMenu::GetEntries()
830{
831 int count;
832 auto entries = SDL_GetTrayEntries(m_trayMenu, &count);
833 return std::span<TrayEntry>{reinterpret_cast<TrayEntry*>(entries),
834 size_t(count)};
835}
836
838 StringParam label,
839 TrayEntryFlags flags)
840{
841 return SDL_InsertTrayEntryAt(m_trayMenu, pos, label, flags);
842}
843
845{
846 return SDL_GetTrayMenuParentEntry(m_trayMenu);
847}
848
850{
851 return SDL_GetTrayMenuParentTray(m_trayMenu);
852}
853
864inline void UpdateTrays() { SDL_UpdateTrays(); }
865
866#pragma region impl
868
870 TrayEntryFlags flags)
871{
872 return InsertEntry(-1, std::move(label), flags);
873}
874
876{
879 [](void* userdata, SDL_TrayEntry* entry) {
880 Wrapper::Call(userdata, TrayEntryRef{entry});
881 },
882 Wrapper::Wrap(get(), std::move(callback)));
883}
884
885#pragma endregion impl
886
887} // namespace SDL
888
889#endif /* SDL3PP_TRAY_H_ */
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Tray * release(SDL_Tray * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_Tray * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
An opaque handle representing a menu/submenu on a system tray object.
Definition SDL3pp_tray.h:330
constexpr bool operator==(const TrayMenu &other) const =default
Default comparison operator.
constexpr TrayMenu(SDL_TrayMenu *trayMenu={})
Wraps TrayMenu.
Definition SDL3pp_tray.h:339
DetachedTrayEntry AppendEntry(StringParam label, TrayEntryFlags flags)
Appends a tray entry.
Definition SDL3pp_tray.h:869
constexpr bool operator==(SDL_TrayMenu *trayMenu) const
Compares with the underlying type.
Definition SDL3pp_tray.h:352
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
DetachedTrayEntry InsertEntry(int pos, StringParam label, TrayEntryFlags flags)
Insert a tray entry at a given position.
Definition SDL3pp_tray.h:837
void UpdateTrays()
Update the trays.
Definition SDL3pp_tray.h:864
TrayMenu CreateMenu()
Create a menu for a system tray.
Definition SDL3pp_tray.h:825
constexpr TrayEntryFlags TRAYENTRY_BUTTON
Make the entry a simple button. Required.
Definition SDL3pp_tray.h:65
TrayMenu GetMenu() const
Gets a previously created tray menu.
Definition SDL3pp_tray.h:827
constexpr TrayEntryFlags TRAYENTRY_DISABLED
Make the entry disabled. Optional.
Definition SDL3pp_tray.h:74
TrayEntryRef GetParentEntry() const
Gets the entry for which the menu is a submenu, if the current menu is a submenu.
Definition SDL3pp_tray.h:844
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:849
std::span< TrayEntry > GetEntries()
Returns a list of entries in the menu, in order.
Definition SDL3pp_tray.h:829
SDL_TrayCallback TrayCallback
A callback that is invoked when a tray entry is selected.
Definition SDL3pp_tray.h:311
constexpr TrayEntryFlags TRAYENTRY_CHECKED
Make the entry checked.
Definition SDL3pp_tray.h:80
Uint32 TrayEntryFlags
Flags that control the creation of system tray entries.
Definition SDL3pp_tray.h:63
constexpr TrayEntryFlags TRAYENTRY_SUBMENU
Prepare the entry to have a submenu. Required.
Definition SDL3pp_tray.h:71
constexpr TrayEntryFlags TRAYENTRY_CHECKBOX
Make the entry a checkbox. Required.
Definition SDL3pp_tray.h:68
std::function< void(TrayEntryRef)> TrayCB
A callback that is invoked when a tray entry is selected.
Definition SDL3pp_tray.h:322
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
A detached reference to resource that might be transformed into an owned handle.
Definition SDL3pp_resource.h:156
Store callbacks by key.
Definition SDL3pp_callbackWrapper.h:222
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:138
An opaque handle representing a toplevel system tray object.
Definition SDL3pp_tray.h:93
TrayBase(SurfaceBase &icon, StringParam tooltip)
Create an icon to be placed in the operating system's tray, or equivalent.
Definition SDL3pp_tray.h:118
void SetTooltip(StringParam tooltip)
Updates the system tray icon's tooltip.
Definition SDL3pp_tray.h:149
void SetIcon(SurfaceBase &icon)
Updates the system tray icon's icon.
Definition SDL3pp_tray.h:135
An opaque handle representing an entry on a system tray object.
Definition SDL3pp_tray.h:494
void SetEnabled(bool enabled)
Sets whether or not an entry is enabled.
Definition SDL3pp_tray.h:632
TrayMenu GetSubmenu()
Gets a previously created tray entry submenu.
Definition SDL3pp_tray.h:541
void SetLabel(StringParam label)
Sets the label of an entry.
Definition SDL3pp_tray.h:562
bool GetEnabled() const
Gets whether or not an entry is enabled.
Definition SDL3pp_tray.h:648
TrayMenu CreateSubmenu()
Create a submenu for a system tray entry.
Definition SDL3pp_tray.h:518
const char * GetLabel() const
Gets the label of an entry.
Definition SDL3pp_tray.h:580
bool GetChecked() const
Gets whether or not an entry is checked.
Definition SDL3pp_tray.h:616
void SetCallback(TrayCB callback)
Sets a callback to be invoked when the entry is selected.
Definition SDL3pp_tray.h:875
void SetCallback(TrayCallback callback, void *userdata)
Sets a callback to be invoked when the entry is selected.
Definition SDL3pp_tray.h:680
TrayMenu GetParent()
Gets the menu containing a certain tray entry.
Definition SDL3pp_tray.h:708
void Click()
Simulate a click on a tray entry.
Definition SDL3pp_tray.h:694
void SetChecked(bool checked)
Sets whether or not an entry is checked.
Definition SDL3pp_tray.h:598
Handle to a non owned trayEntry.
Definition SDL3pp_tray.h:720
constexpr TrayEntryRef(TrayEntryRef &&other)
Move constructor.
Definition SDL3pp_tray.h:734
void reset(SDL_TrayEntry *newResource={})
Removes a tray entry.
Definition SDL3pp_tray.h:764
TrayEntryRef & operator=(TrayEntryRef other)
Assignment operator.
Definition SDL3pp_tray.h:747
constexpr TrayEntryRef(const TrayEntryRef &other)
Copy constructor.
Definition SDL3pp_tray.h:726
constexpr ~TrayEntryRef()=default
Default constructor.
void Remove()
Removes a tray entry.
Definition SDL3pp_tray.h:780
Handle to an owned trayEntry.
Definition SDL3pp_tray.h:792
constexpr TrayEntry(SDL_TrayEntry *resource={})
Constructs from the underlying resource.
Definition SDL3pp_tray.h:798
TrayEntry & operator=(TrayEntry other)
Assignment operator.
Definition SDL3pp_tray.h:818
constexpr TrayEntry(TrayEntry &&other)=default
Move constructor.
~TrayEntry()
Frees up resource when object goes out of scope.
Definition SDL3pp_tray.h:813
Handle to a non owned tray.
Definition SDL3pp_tray.h:207
TrayRef & operator=(TrayRef other)
Assignment operator.
Definition SDL3pp_tray.h:234
void reset(SDL_Tray *newResource={})
Destroys a tray object.
Definition SDL3pp_tray.h:252
constexpr ~TrayRef()=default
Default constructor.
constexpr TrayRef(const TrayRef &other)
Copy constructor.
Definition SDL3pp_tray.h:213
constexpr TrayRef(TrayRef &&other)
Move constructor.
Definition SDL3pp_tray.h:221
Handle to an owned tray.
Definition SDL3pp_tray.h:267
constexpr Tray(Tray &&other)=default
Move constructor.
~Tray()
Frees up resource when object goes out of scope.
Definition SDL3pp_tray.h:288
constexpr Tray(SDL_Tray *resource={})
Constructs from the underlying resource.
Definition SDL3pp_tray.h:273
Tray & operator=(Tray other)
Assignment operator.
Definition SDL3pp_tray.h:293