-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,198 additions
and
1,155 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
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,62 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace NickvisionMoney.GNOME.Helpers; | ||
|
||
/// <summary> | ||
/// Helper methods for GDK | ||
/// </summary> | ||
public static partial class GdkHelpers | ||
{ | ||
[LibraryImport("libadwaita-1.so.0", StringMarshalling = StringMarshalling.Utf8)] // Using "gdk" doesn't work here for some reason | ||
[return: MarshalAs(UnmanagedType.I1)] | ||
private static partial bool gdk_rgba_parse(out RGBA rgba, string spec); | ||
[LibraryImport("libadwaita-1.so.0", StringMarshalling = StringMarshalling.Utf8)] // Using "gdk" doesn't work here for some reason | ||
private static partial string gdk_rgba_to_string(ref RGBA rgba); | ||
|
||
/// <summary> | ||
/// Helper RGBA struct. Used instead of Gdk.RGBA | ||
/// </summary> | ||
[StructLayout(LayoutKind.Sequential)] | ||
public struct RGBA | ||
{ | ||
/// <summary> | ||
/// Red channel (0.0-1.0) | ||
/// </summary> | ||
public float Red; | ||
/// <summary> | ||
/// Green channel (0.0-1.0) | ||
/// </summary> | ||
public float Green; | ||
/// <summary> | ||
/// Blue channel (0.0-1.0) | ||
/// </summary> | ||
public float Blue; | ||
/// <summary> | ||
/// Alpha channel (0.0-1.0) | ||
/// </summary> | ||
public float Alpha; | ||
|
||
/// <summary> | ||
/// Helper method to parse color string to GdkExt.RGBA struct | ||
/// </summary> | ||
/// <param name="colorRGBA">Struct to write to</param> | ||
/// <param name="spec">Color string</param> | ||
/// <returns>Whether or not the string was parsed successfully</returns> | ||
public static bool Parse(out RGBA? colorRGBA, string spec) | ||
{ | ||
if (gdk_rgba_parse(out var val, spec)) | ||
{ | ||
colorRGBA = val; | ||
return true; | ||
} | ||
colorRGBA = null; | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a string representation of the RGBA | ||
/// </summary> | ||
/// <returns>The string representation of the RGBA</returns> | ||
public override string ToString() => gdk_rgba_to_string(ref this); | ||
} | ||
} |
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,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
|
||
namespace NickvisionMoney.GNOME.Helpers; | ||
|
||
/// <summary> | ||
/// Helper methods for GTK | ||
/// </summary> | ||
public unsafe static partial class GtkHelpers | ||
{ | ||
[LibraryImport("libadwaita-1.so.0", StringMarshalling = StringMarshalling.Utf8)] | ||
private static partial void gtk_color_dialog_button_set_rgba(nint button, ref GdkHelpers.RGBA rgba); | ||
|
||
/// <summary> | ||
/// Helper extension method for Gtk.ColorButton to get color as GdkHelpers.RGBA | ||
/// </summary> | ||
/// <param name="button">Color button</param> | ||
/// <returns>Color as GdkHelpers.RGBA</returns> | ||
public static GdkHelpers.RGBA GetExtRgba(this Gtk.ColorDialogButton button) => Marshal.PtrToStructure<GdkHelpers.RGBA>(button.GetRgba().Handle.DangerousGetHandle()); | ||
|
||
/// <summary> | ||
/// Helper extension method for Gtk.ColorButton to set color as GdkHelpers.RGBA | ||
/// </summary> | ||
/// <param name="button">Color button</param> | ||
/// <param name="color">Color as GdkHelpers.RGBA</param> | ||
public static void SetExtRgba(this Gtk.ColorDialogButton button, GdkHelpers.RGBA color) => gtk_color_dialog_button_set_rgba(button.Handle, ref color); | ||
|
||
/// <summary> | ||
/// Extension method for Gtk.ColorDialog to choose a color | ||
/// </summary> | ||
/// <param name="dialog">Color dialog</param> | ||
/// <param name="parent">Parent window</param> | ||
/// <exception cref="Exception">Thrown if failed to choose a color</exception> | ||
/// <returns>Color struct if successful, or null</returns> | ||
public static Task<GdkHelpers.RGBA?> ChooseRgbaAsync(this Gtk.ColorDialog dialog, Gtk.Window parent) | ||
{ | ||
var tcs = new TaskCompletionSource<GdkHelpers.RGBA?>(); | ||
|
||
var callback = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) => | ||
{ | ||
if (sourceObject is null) | ||
{ | ||
tcs.SetException(new Exception("Missing source object")); | ||
} | ||
else | ||
{ | ||
var color = Gtk.Internal.ColorDialog.ChooseRgbaFinish(sourceObject.Handle, res.Handle, out var error); | ||
if (!error.IsInvalid) | ||
{ | ||
tcs.SetException(new Exception(error.ToString() ?? "")); | ||
} | ||
else if (color.DangerousGetHandle() == IntPtr.Zero) | ||
{ | ||
tcs.SetResult(null); | ||
} | ||
else | ||
{ | ||
tcs.SetResult(Marshal.PtrToStructure<GdkHelpers.RGBA>(color.DangerousGetHandle())); | ||
} | ||
} | ||
}); | ||
|
||
Gtk.Internal.ColorDialog.ChooseRgba( | ||
self: dialog.Handle, | ||
parent: parent.Handle, | ||
initialColor: new Gdk.Internal.RGBAOwnedHandle(IntPtr.Zero), | ||
cancellable: IntPtr.Zero, | ||
callback: callback.NativeCallback, | ||
userData: 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
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
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
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
Oops, something went wrong.