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 TrayRef;
28
29// Forward decl
30struct Tray;
31
41
51
52// Forward decl
53struct TrayEntryRef;
54
55// Forward decl
56struct TrayEntry;
57
67
77
85
97using TrayEntryFlags = Uint32;
98
100 SDL_TRAYENTRY_BUTTON;
101
103 SDL_TRAYENTRY_CHECKBOX;
104
106 SDL_TRAYENTRY_SUBMENU;
107
109 SDL_TRAYENTRY_DISABLED;
110
114constexpr TrayEntryFlags TRAYENTRY_CHECKED = SDL_TRAYENTRY_CHECKED;
115
125struct TrayRef : Resource<SDL_Tray*>
126{
127 using Resource::Resource;
128
141 void SetIcon(SurfaceRef icon) { SDL_SetTrayIcon(get(), icon.get()); }
142
155 void SetTooltip(StringParam tooltip) { SDL_SetTrayTooltip(get(), tooltip); }
156
179
201 TrayMenu GetMenu() const;
202
217 static void reset(SDL_Tray* resource) { SDL_DestroyTray(resource); }
218};
219
227struct Tray : ResourceUnique<TrayRef>
228{
230
254 static Tray Create(SurfaceRef icon, StringParam tooltip)
255 {
256 return Tray(SDL_CreateTray(icon, tooltip));
257 }
258
271 void Destroy() { reset(); }
276
277};
278
279
281{
282 return TrayShared(std::move(*this));
283}
284
294struct TrayUnsafe : ResourceUnsafe<TrayRef>
295{
297
301 constexpr explicit TrayUnsafe(Tray&& other)
302 : TrayUnsafe(other.release())
303 {
304 }
305};
306
318using TrayCallback = SDL_TrayCallback;
319
330using TrayCB = std::function<void(TrayEntryRef)>;
331
338{
339 SDL_TrayMenu* m_trayMenu;
340
341public:
347 constexpr TrayMenu(SDL_TrayMenu* trayMenu = {})
348 : m_trayMenu(trayMenu)
349 {
350 }
351
355 constexpr bool operator==(const TrayMenu& other) const = default;
356
360 constexpr bool operator==(SDL_TrayMenu* trayMenu) const
361 {
362 return operator==(TrayMenu(trayMenu));
363 }
364
370 constexpr operator SDL_TrayMenu*() const { return m_trayMenu; }
371
377 constexpr explicit operator bool() const { return m_trayMenu != nullptr; }
378
394 std::span<TrayEntry> GetEntries();
395
423 StringParam label,
424 TrayEntryFlags flags);
425
451
470
488 TrayRef GetParentTray() const;
489};
490
500struct TrayEntryRef : Resource<SDL_TrayEntry*>
501{
502 using Resource::Resource;
503
525 TrayMenu CreateSubmenu() { return SDL_CreateTraySubmenu(get()); }
526
548 TrayMenu GetSubmenu() { return SDL_GetTraySubmenu(get()); }
549
569 void SetLabel(StringParam label) { SDL_SetTrayEntryLabel(get(), label); }
570
587 const char* GetLabel() const { return SDL_GetTrayEntryLabel(get()); }
588
605 void SetChecked(bool checked) { SDL_SetTrayEntryChecked(get(), checked); }
606
623 bool GetChecked() const { return SDL_GetTrayEntryChecked(get()); }
624
639 void SetEnabled(bool enabled) { SDL_SetTrayEntryEnabled(get(), enabled); }
640
655 bool GetEnabled() const { return SDL_GetTrayEntryEnabled(get()); }
656
670 void SetCallback(TrayCB callback);
671
687 void SetCallback(TrayCallback callback, void* userdata)
688 {
689 SDL_SetTrayEntryCallback(get(), callback, userdata);
690 }
691
701 void Click() { SDL_ClickTrayEntry(get()); }
702
715 TrayMenu GetParent() { return SDL_GetTrayEntryParent(get()); }
716
730 static void reset(SDL_TrayEntry* resource) { SDL_RemoveTrayEntry(resource); }
731};
732
740struct TrayEntry : ResourceUnique<TrayEntryRef>
741{
743
755 void Remove() { reset(); }
760
761};
762
763
765{
766 return TrayEntryShared(std::move(*this));
767}
768
778struct TrayEntryUnsafe : ResourceUnsafe<TrayEntryRef>
779{
781
785 constexpr explicit TrayEntryUnsafe(TrayEntry&& other)
786 : TrayEntryUnsafe(other.release())
787 {
788 }
789};
790
791inline TrayMenu TrayRef::CreateMenu() { return SDL_CreateTrayMenu(get()); }
792
793inline TrayMenu TrayRef::GetMenu() const { return SDL_GetTrayMenu(get()); }
794
795inline std::span<TrayEntry> TrayMenu::GetEntries()
796{
797 int count;
798 auto entries = SDL_GetTrayEntries(m_trayMenu, &count);
799 return std::span<TrayEntry>{reinterpret_cast<TrayEntry*>(entries),
800 size_t(count)};
801}
802
804 StringParam label,
805 TrayEntryFlags flags)
806{
807 return DetachedTrayEntry(
808 SDL_InsertTrayEntryAt(m_trayMenu, pos, label, flags));
809}
810
812{
813 return SDL_GetTrayMenuParentEntry(m_trayMenu);
814}
815
817{
818 return SDL_GetTrayMenuParentTray(m_trayMenu);
819}
820
831inline void UpdateTrays() { SDL_UpdateTrays(); }
832
833#pragma region impl
835
837 TrayEntryFlags flags)
838{
839 return InsertEntry(-1, std::move(label), flags);
840}
841
843{
846 [](void* userdata, SDL_TrayEntry* entry) {
847 Wrapper::Call(userdata, TrayEntryRef{entry});
848 },
849 Wrapper::Wrap(get(), std::move(callback)));
850}
851
852#pragma endregion impl
853
854} // namespace SDL
855
856#endif /* SDL3PP_TRAY_H_ */
A detached reference to resource that might be transformed into an owned handle.
Definition SDL3pp_resource.h:373
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_Tray * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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:338
constexpr bool operator==(const TrayMenu &other) const =default
Default comparison operator.
constexpr TrayMenu(SDL_TrayMenu *trayMenu={})
Wraps TrayMenu.
Definition SDL3pp_tray.h:347
DetachedTrayEntry AppendEntry(StringParam label, TrayEntryFlags flags)
Appends a tray entry.
Definition SDL3pp_tray.h:836
constexpr bool operator==(SDL_TrayMenu *trayMenu) const
Compares with the underlying type.
Definition SDL3pp_tray.h:360
DetachedTrayEntry InsertEntry(int pos, StringParam label, TrayEntryFlags flags)
Insert a tray entry at a given position.
Definition SDL3pp_tray.h:803
TrayMenu CreateMenu()
Create a menu for a system tray.
Definition SDL3pp_tray.h:791
DetachedResource< TrayEntryRef, TrayEntry > DetachedTrayEntry
A trayEntry result that will be owned only if assigned to a TrayEntry.
Definition SDL3pp_tray.h:84
ResourceShared< TrayEntry > TrayEntryShared
Handle to a shared trayEntry.
Definition SDL3pp_tray.h:66
void UpdateTrays()
Update the trays.
Definition SDL3pp_tray.h:831
TrayEntryShared share()
Move this trayEntry into a TrayEntryShared.
Definition SDL3pp_tray.h:764
constexpr TrayEntryFlags TRAYENTRY_BUTTON
Make the entry a simple button. Required.
Definition SDL3pp_tray.h:99
TrayMenu GetMenu() const
Gets a previously created tray menu.
Definition SDL3pp_tray.h:793
constexpr TrayEntryFlags TRAYENTRY_DISABLED
Make the entry disabled. Optional.
Definition SDL3pp_tray.h:108
TrayEntryRef GetParentEntry() const
Gets the entry for which the menu is a submenu, if the current menu is a submenu.
Definition SDL3pp_tray.h:811
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:816
ResourceShared< Tray > TrayShared
Handle to a shared tray.
Definition SDL3pp_tray.h:40
TrayShared share()
Move this tray into a TrayShared.
Definition SDL3pp_tray.h:280
std::span< TrayEntry > GetEntries()
Returns a list of entries in the menu, in order.
Definition SDL3pp_tray.h:795
SDL_TrayCallback TrayCallback
A callback that is invoked when a tray entry is selected.
Definition SDL3pp_tray.h:318
constexpr TrayEntryFlags TRAYENTRY_CHECKED
Make the entry checked.
Definition SDL3pp_tray.h:114
Uint32 TrayEntryFlags
Flags that control the creation of system tray entries.
Definition SDL3pp_tray.h:97
constexpr TrayEntryFlags TRAYENTRY_SUBMENU
Prepare the entry to have a submenu. Required.
Definition SDL3pp_tray.h:105
constexpr TrayEntryFlags TRAYENTRY_CHECKBOX
Make the entry a checkbox. Required.
Definition SDL3pp_tray.h:102
std::function< void(TrayEntryRef)> TrayCB
A callback that is invoked when a tray entry is selected.
Definition SDL3pp_tray.h:330
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Store callbacks by key.
Definition SDL3pp_callbackWrapper.h:222
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:153
An opaque handle representing an entry on a system tray object.
Definition SDL3pp_tray.h:501
TrayMenu GetSubmenu()
Gets a previously created tray entry submenu.
Definition SDL3pp_tray.h:548
void SetChecked(bool checked)
Sets whether or not an entry is checked.
Definition SDL3pp_tray.h:605
void SetCallback(TrayCB callback)
Sets a callback to be invoked when the entry is selected.
Definition SDL3pp_tray.h:842
TrayMenu CreateSubmenu()
Create a submenu for a system tray entry.
Definition SDL3pp_tray.h:525
static void reset(SDL_TrayEntry *resource)
Removes a tray entry.
Definition SDL3pp_tray.h:730
void Click()
Simulate a click on a tray entry.
Definition SDL3pp_tray.h:701
void SetLabel(StringParam label)
Sets the label of an entry.
Definition SDL3pp_tray.h:569
void SetCallback(TrayCallback callback, void *userdata)
Sets a callback to be invoked when the entry is selected.
Definition SDL3pp_tray.h:687
TrayMenu GetParent()
Gets the menu containing a certain tray entry.
Definition SDL3pp_tray.h:715
const char * GetLabel() const
Gets the label of an entry.
Definition SDL3pp_tray.h:587
void SetEnabled(bool enabled)
Sets whether or not an entry is enabled.
Definition SDL3pp_tray.h:639
bool GetEnabled() const
Gets whether or not an entry is enabled.
Definition SDL3pp_tray.h:655
bool GetChecked() const
Gets whether or not an entry is checked.
Definition SDL3pp_tray.h:623
Unsafe Handle to trayEntry.
Definition SDL3pp_tray.h:779
constexpr TrayEntryUnsafe(TrayEntry &&other)
Constructs TrayEntryUnsafe from TrayEntry.
Definition SDL3pp_tray.h:785
Handle to an owned trayEntry.
Definition SDL3pp_tray.h:741
void Remove()
Removes a tray entry.
Definition SDL3pp_tray.h:755
An opaque handle representing a toplevel system tray object.
Definition SDL3pp_tray.h:126
void SetIcon(SurfaceRef icon)
Updates the system tray icon's icon.
Definition SDL3pp_tray.h:141
void SetTooltip(StringParam tooltip)
Updates the system tray icon's tooltip.
Definition SDL3pp_tray.h:155
static void reset(SDL_Tray *resource)
Destroys a tray object.
Definition SDL3pp_tray.h:217
Unsafe Handle to tray.
Definition SDL3pp_tray.h:295
constexpr TrayUnsafe(Tray &&other)
Constructs TrayUnsafe from Tray.
Definition SDL3pp_tray.h:301
Handle to an owned tray.
Definition SDL3pp_tray.h:228
void Destroy()
Destroys a tray object.
Definition SDL3pp_tray.h:271
static Tray Create(SurfaceRef icon, StringParam tooltip)
Create an icon to be placed in the operating system's tray, or equivalent.
Definition SDL3pp_tray.h:254