Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions RimeSharp/Rime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,22 @@ public RimeCandidate[] GetCandidates(RimeSessionId sessionId, int start = 0, int
{
var iterator = new RimeCandidateListIterator();
if (!_api.CandidateListFromIndex(sessionId, ref iterator, start)) return [];

var candidates = new List<RimeCandidate>();
var i = 0;
while (i < count && _api.CandidateListNext(ref iterator))
try
{
var i = 0;
while (i < count && _api.CandidateListNext(ref iterator))
{
candidates.Add(iterator.Candidate);
++i;
}
}
finally
{
candidates.Add(iterator.Candidate);
++i;
_api.CandidateListEnd(ref iterator);
}
_api.CandidateListEnd(ref iterator);

return [.. candidates];
}

Expand Down
23 changes: 15 additions & 8 deletions RimeSharp/RimeCustomSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ public class RimeSwitcherSettings() : RimeCustomSettings(s_api.SwitcherSettingsI
private RimeSchemaListItem[] GetSchemaList(SchemaListAccess access)
{
if (!access(handle, out var list)) return [];
var size = Marshal.SizeOf<RimeSchemaListItem>();
var items = new RimeSchemaListItem[(int)list.Size];
for (var i = 0; i < (int)list.Size; ++i)

try
{
var size = Marshal.SizeOf<RimeSchemaListItem>();
var items = new RimeSchemaListItem[(int)list.Size];
for (var i = 0; i < (int)list.Size; ++i)
{
var ptr = IntPtr.Add(list.List, i * size);
items[i] = Marshal.PtrToStructure<RimeSchemaListItem>(ptr);
}
return items;
}
finally
{
var ptr = IntPtr.Add(list.List, i * size);
items[i] = Marshal.PtrToStructure<RimeSchemaListItem>(ptr);
s_api.SchemaListDestroy(ref list);
}
s_api.SchemaListDestroy(ref list);
return items;
}

public RimeSchemaListItem[] GetAvailableSchemaList()
Expand All @@ -47,4 +54,4 @@ public RimeSchemaListItem[] GetSelectedSchemaList()

public bool SelectSchemas(string[] schemaIdList)
=> s_api.SelectSchemas(handle, schemaIdList, schemaIdList.Length);
}
}
2 changes: 1 addition & 1 deletion RimeSharp/RimeLevers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal bool GetAvailableSchemaList(IntPtr ptr, out RimeSchemaList list)
=> _levers.GetAvailableSchemaList(ptr, out list);

internal bool GetSelectedSchemaList(IntPtr ptr, out RimeSchemaList list)
=> _levers.GetAvailableSchemaList(ptr, out list);
=> _levers.GetSelectedSchemaList(ptr, out list);

internal void SchemaListDestroy(ref RimeSchemaList list)
=> _levers.SchemaListDestroy(ref list);
Expand Down
24 changes: 22 additions & 2 deletions RimeSharp/RimeStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,29 @@ public struct RimeCandidateListIterator
{
public UIntPtr Ptr;
public int Index;
// librime owns these pointers and replaces them during iteration.
private NativeRimeCandidate _candidate;

[MarshalAs(UnmanagedType.Struct)]
public RimeCandidate Candidate;
public readonly RimeCandidate Candidate => _candidate.ToManaged();
}

[StructLayout(LayoutKind.Sequential)]
internal struct NativeRimeCandidate
{
internal IntPtr Text;
internal IntPtr Comment;
private readonly IntPtr _reserved;

internal readonly RimeCandidate ToManaged()
{
return new RimeCandidate
{
Text = UTF8Marshal.PtrToStringUTF8(Text),
Comment = Comment == IntPtr.Zero
? null
: UTF8Marshal.PtrToStringUTF8(Comment),
};
}
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
Loading