+
Skip to content

Add Async FontDialog methods and sample #993

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

Merged
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
1 change: 1 addition & 0 deletions src/GirCore.Libs.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"Samples/Gtk-4.0/AboutDialog/AboutDialog.csproj",
"Samples/Gtk-4.0/Builder/Builder.csproj",
"Samples/Gtk-4.0/DrawingArea/DrawingArea.csproj",
"Samples/Gtk-4.0/FontDialog/FontDialog.csproj",
"Samples/Gtk-4.0/Window/Window.csproj",
"Samples/WebKit-6.0/JavascriptCall/JavascriptCall.csproj",
"Samples/WebKit-6.0/JavascriptCallback/JavascriptCallback.csproj",
Expand Down
2 changes: 2 additions & 0 deletions src/GirCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gtk-4.0", "Libs/Gtk-4.0/Gtk
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Gtk-4.0", "Gtk-4.0", "{7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FontDialog", "Samples/Gtk-4.0/FontDialog/FontDialog.csproj", "{D215B861-1A51-436A-82EB-FCF7AF892748}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Window", "Samples/Gtk-4.0/Window/Window.csproj", "{AD618F85-F8F5-454B-9CA0-ABF936492065}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AboutDialog", "Samples/Gtk-4.0/AboutDialog/AboutDialog.csproj", "{4008E1B8-AC7D-4CC8-AB74-8576493C4FDD}"
Expand Down
121 changes: 121 additions & 0 deletions src/Libs/Gtk-4.0/Public/FontDialog.cs
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;
}
}
13 changes: 13 additions & 0 deletions src/Samples/Gtk-4.0/FontDialog/FontDialog.csproj
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>
45 changes: 45 additions & 0 deletions src/Samples/Gtk-4.0/FontDialog/Program.cs
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() ?? string.Empty);
}
catch (Exception ex)
{
//Prints "Dismissed by user" if dialog is cancelled
Console.WriteLine(ex.Message);
}
};

window.Show();
};
return application.RunWithSynchronizationContext(null);
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载