-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Async FontDialog methods and sample #993
Add Async FontDialog methods and sample #993
Conversation
Thanks for your contribution. At a first glance the code looks good 👍 Some minor remarks:
If you change your code please |
Thanks for the quick review. |
6f1e512
to
d3eefcd
Compare
Can you add your sample to the solution file inside the |
d3eefcd
to
b351bcc
Compare
Done |
I took a detailed look into your PR and found an issue with Therefore I think the code must look like: public Task<Pango.FontDescription?> ChooseFontAsync(Window parent, Pango.FontDescription? fontDescription)
{
var tcs = new TaskCompletionSource<Pango.FontDescription?>();
var callbackHandler = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) =>
{
if (sourceObject is null)
{
tcs.SetException(new Exception("Missing source object"));
return;
}
var fontDescriptionOwnedHandle = Internal.FontDialog.ChooseFontFinish(sourceObject.Handle, res.Handle, out var error);
if (!error.IsInvalid)
tcs.SetException(new GLib.GException(error));
else
{
if (fontDescriptionOwnedHandle.IsInvalid)
tcs.SetResult(null);
else
{
var result = new Pango.FontDescription(fontDescriptionOwnedHandle);
tcs.SetResult(result);
}
}
});
var initialValue = (Pango.Internal.FontDescriptionHandle?) fontDescription?.Handle ?? Pango.Internal.FontDescriptionUnownedHandle.NullHandle;
Internal.FontDialog.ChooseFont(
Handle,
parent.Handle,
initialValue,
IntPtr.Zero,
callbackHandler.NativeCallback,
IntPtr.Zero
);
return tcs.Task;
} I'm not sure if your sample can be considered complete. If a user presses cancel, it means that a "recoverable error" occurred. Which in C# is an exception. I already asked the gnome devs regarding this behaviour. They argument that "if the user is expected to select a font, not doing so is an error". So if you press cancel in the font chooser the sample app crashes. I'm not sure if or how you want to attribute to this behaviour or just add some comment? One possible way to fix this would be: buttonSelectFont.OnClicked += async (_, _) =>
{
try
{
var fontDialog = Gtk.FontDialog.New();
var selectedFont = await fontDialog.ChooseFontAsync(window, null);
labelSelectedFont.SetLabel(selectedFont?.ToString() ?? string.Empty);
}
catch (Exception ex)
{
//Prints "Dismissed by user" if dialog is cancelled
Console.WriteLine(ex.Message);
}
}; Do you have another proposal how to attribute to the possible exception? If those points are fixed, it is good to go 👍 |
b351bcc
to
ca438cf
Compare
Thanks for the detailed review, I have updated as suggested. |
I think you missed the nullable annotations for the return type and the task completion source in There should be a warning in your ide in the line |
ca438cf
to
ae84b20
Compare
Sorry to bother you even more. In the sample the nullable handling is missing, too: |
ae84b20
to
948208f
Compare
No bother at all, done, Thanks |
Added manual binding for FontDialog Async methods.
Added a sample application to use
ChooseFontAsync
function.