-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Async FontDialog methods and sample
- Loading branch information
1 parent
1ea670b
commit ae84b20
Showing
5 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Gtk; | ||
|
||
public partial class FontDialog | ||
{ | ||
//TODO: Async methods should be generated automatically (https://github.com/gircore/gir.core/issues/893) | ||
public Task<Pango.FontFace?> ChooseFaceAsync(Window parent, Pango.FontFace? fontFace) | ||
{ | ||
var tcs = new TaskCompletionSource<Pango.FontFace?>(); | ||
|
||
var callbackHandler = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) => | ||
{ | ||
if (sourceObject is null) | ||
{ | ||
tcs.SetException(new Exception("Missing source object")); | ||
return; | ||
} | ||
|
||
var chooseFontFaceResult = Internal.FontDialog.ChooseFaceFinish(sourceObject.Handle, res.Handle, out var error); | ||
|
||
if (!error.IsInvalid) | ||
tcs.SetException(new GLib.GException(error)); | ||
else | ||
{ | ||
var result = GObject.Internal.ObjectWrapper.WrapNullableHandle<Pango.FontFace>(chooseFontFaceResult, false); | ||
tcs.SetResult(result); | ||
} | ||
}); | ||
|
||
Internal.FontDialog.ChooseFace( | ||
Handle, | ||
parent.Handle, | ||
fontFace?.Handle ?? IntPtr.Zero, | ||
IntPtr.Zero, | ||
callbackHandler.NativeCallback, | ||
IntPtr.Zero | ||
); | ||
|
||
return tcs.Task; | ||
} | ||
|
||
//TODO: Async methods should be generated automatically (https://github.com/gircore/gir.core/issues/893) | ||
public Task<Pango.FontFamily?> ChooseFamilyAsync(Window parent, Pango.FontFamily? fontFamily) | ||
{ | ||
var tcs = new TaskCompletionSource<Pango.FontFamily?>(); | ||
|
||
var callbackHandler = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) => | ||
{ | ||
if (sourceObject is null) | ||
{ | ||
tcs.SetException(new Exception("Missing source object")); | ||
return; | ||
} | ||
|
||
var chooseFontFamilyResult = Internal.FontDialog.ChooseFamilyFinish(sourceObject.Handle, res.Handle, out var error); | ||
|
||
if (!error.IsInvalid) | ||
tcs.SetException(new GLib.GException(error)); | ||
else | ||
{ | ||
var result = GObject.Internal.ObjectWrapper.WrapNullableHandle<Pango.FontFamily>(chooseFontFamilyResult, false); | ||
tcs.SetResult(result); | ||
} | ||
}); | ||
|
||
Internal.FontDialog.ChooseFamily( | ||
Handle, | ||
parent.Handle, | ||
fontFamily?.Handle ?? IntPtr.Zero, | ||
IntPtr.Zero, | ||
callbackHandler.NativeCallback, | ||
IntPtr.Zero | ||
); | ||
|
||
return tcs.Task; | ||
} | ||
|
||
//TODO: Async methods should be generated automatically (https://github.com/gircore/gir.core/issues/893) | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Libs\Gtk-4.0\Gtk-4.0.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
|
||
var application = Gtk.Application.New("org.gir.core", Gio.ApplicationFlags.FlagsNone); | ||
application.OnActivate += (sender, args) => | ||
{ | ||
var labelSelectedFont = Gtk.Label.New(""); | ||
labelSelectedFont.SetMarginTop(12); | ||
labelSelectedFont.SetMarginBottom(12); | ||
labelSelectedFont.SetMarginStart(12); | ||
labelSelectedFont.SetMarginEnd(12); | ||
|
||
var buttonSelectFont = Gtk.Button.New(); | ||
buttonSelectFont.Label = "Select Font"; | ||
buttonSelectFont.SetMarginTop(12); | ||
buttonSelectFont.SetMarginBottom(12); | ||
buttonSelectFont.SetMarginStart(12); | ||
buttonSelectFont.SetMarginEnd(12); | ||
|
||
var gtkBox = Gtk.Box.New(Gtk.Orientation.Vertical, 0); | ||
gtkBox.Append(labelSelectedFont); | ||
gtkBox.Append(buttonSelectFont); | ||
|
||
var window = Gtk.ApplicationWindow.New((Gtk.Application) sender); | ||
window.Title = "Gtk4 Window"; | ||
window.SetDefaultSize(300, 300); | ||
window.Child = gtkBox; | ||
|
||
buttonSelectFont.OnClicked += async (_, _) => | ||
{ | ||
try | ||
{ | ||
var fontDialog = Gtk.FontDialog.New(); | ||
var selectedFont = await fontDialog.ChooseFontAsync(window, null); | ||
labelSelectedFont.SetLabel(selectedFont.ToString()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
//Prints "Dismissed by user" if dialog is cancelled | ||
Console.WriteLine(ex.Message); | ||
} | ||
}; | ||
|
||
window.Show(); | ||
}; | ||
return application.RunWithSynchronizationContext(null); |