Skip to content

Commit ab7c7f5

Browse files
authored
Merge pull request #196 from Susko3/add-SDL_GetTrayEntries-helper
Implement helper for `SDL_GetTrayEntries()`
2 parents 3681588 + f988cb2 commit ab7c7f5

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

SDL3-CS.Tests/TestTray.cs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public void TestBasic()
2525
Assert.That(exit != null, SDL_GetError);
2626
SetCallback(exit, () => running = false);
2727

28+
var entries = SDL_GetTrayEntries(RootMenu);
29+
Assert.That(entries, Is.Not.Null, SDL_GetError);
30+
Assert.That(entries!.Count, Is.EqualTo(3));
31+
32+
for (int i = 0; i < entries.Count; i++)
33+
Console.WriteLine($"{i}. {SDL_GetTrayEntryLabel(entries[i]) ?? "<null>"}");
34+
2835
running = true;
2936

3037
while (running)

SDL3-CS/SDL3/SDL_tray.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ public enum SDL_TrayEntryFlags : UInt32
1818

1919
public static partial class SDL3
2020
{
21-
// The code below is currently incorrect because of https://github.com/libsdl-org/SDL/issues/11787.
22-
// [MustDisposeResource]
23-
// public static unsafe SDLOpaquePointerArray<SDL_TrayEntry>? SDL_GetTrayEntries(SDL_TrayMenu* menu)
24-
// {
25-
// int count;
26-
// var array = SDL_GetTrayEntries(menu, &count);
27-
// return SDLArray.CreateOpaque(array, count);
28-
// }
21+
public static unsafe SDLConstOpaquePointerArray<SDL_TrayEntry>? SDL_GetTrayEntries(SDL_TrayMenu* menu)
22+
{
23+
int count;
24+
var array = SDL_GetTrayEntries(menu, &count);
25+
return SDLArray.CreateConstOpaque(array, count);
26+
}
2927
}
3028
}

SDL3-CS/SDLArray.cs

+9
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,14 @@ internal static unsafe class SDLArray
7373

7474
return new SDLOpaquePointerArray<T>(array, count);
7575
}
76+
77+
internal static SDLConstOpaquePointerArray<T>? CreateConstOpaque<T>(T** array, int count)
78+
where T : unmanaged
79+
{
80+
if (array == null)
81+
return null;
82+
83+
return new SDLConstOpaquePointerArray<T>(array, count);
84+
}
7685
}
7786
}

SDL3-CS/SDLConstOpaquePointerArray.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Diagnostics;
6+
7+
namespace SDL
8+
{
9+
public sealed unsafe class SDLConstOpaquePointerArray<T>
10+
where T : unmanaged
11+
{
12+
private readonly T** array;
13+
public readonly int Count;
14+
15+
internal SDLConstOpaquePointerArray(T** array, int count)
16+
{
17+
this.array = array;
18+
Count = count;
19+
}
20+
21+
public T* this[int index]
22+
{
23+
get
24+
{
25+
ArgumentOutOfRangeException.ThrowIfNegative(index);
26+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
27+
Debug.Assert(array[index] != null);
28+
return array[index];
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)