Skip to content

Commit

Permalink
GNOME - Remove GirExt
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Nov 14, 2023
1 parent a6002ef commit a7286c6
Show file tree
Hide file tree
Showing 14 changed files with 1,198 additions and 1,155 deletions.
5 changes: 2 additions & 3 deletions NickvisionMoney.GNOME/Controls/GroupRow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Helpers;
using NickvisionMoney.Shared.Models;
Expand Down Expand Up @@ -106,9 +105,9 @@ public void UpdateRow(Group group, string defaultColor, CultureInfo cultureAmoun
_filterActive = filterActive;
_cultureAmount = cultureAmount;
//Color
if (!GdkExt.RGBA.Parse(out var color, _group.RGBA))
if (!GdkHelpers.RGBA.Parse(out var color, _group.RGBA))
{
GdkExt.RGBA.Parse(out color, _defaultColor);
GdkHelpers.RGBA.Parse(out color, _defaultColor);
}
//Row Settings
SetTitle(_group.Name);
Expand Down
5 changes: 2 additions & 3 deletions NickvisionMoney.GNOME/Controls/RecentAccountRow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Models;
using System;
Expand All @@ -12,7 +11,7 @@ namespace NickvisionMoney.GNOME.Controls;
public partial class RecentAccountRow : Adw.ActionRow
{
private readonly RecentAccount _recentAccount;
private readonly GdkExt.RGBA _color;
private readonly GdkHelpers.RGBA _color;

[Gtk.Connect] private readonly Gtk.Image _prefixColor;
[Gtk.Connect] private readonly Gtk.Button _prefixButton;
Expand Down Expand Up @@ -40,7 +39,7 @@ private RecentAccountRow(Gtk.Builder builder, RecentAccount account, string colo
_prefixButton.OnClicked += (sender, e) => Selected?.Invoke(this, _recentAccount);
_removeButton.SetVisible(canRemove);
_removeButton.OnClicked += (sender, e) => RemoveRequested?.Invoke(this, _recentAccount);
GdkExt.RGBA.Parse(out var color, colorString);
GdkHelpers.RGBA.Parse(out var color, colorString);
_color = color!.Value;
var luma = _color.Red * 0.2126 + _color.Green * 0.7152 + _color.Blue * 0.0722;
if (onStartScreen)
Expand Down
5 changes: 2 additions & 3 deletions NickvisionMoney.GNOME/Controls/TransactionId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Helpers;
using System.Globalization;

Expand Down Expand Up @@ -42,9 +41,9 @@ public TransactionId(uint id) : this(Builder.FromFile("transaction_id.ui"), id)
/// <param name="defaultColor">A default color</param>
public void UpdateColor(string colorString, string defaultColor, bool useNativeDigits)
{
if (!GdkExt.RGBA.Parse(out var color, colorString))
if (!GdkHelpers.RGBA.Parse(out var color, colorString))
{
GdkExt.RGBA.Parse(out color, defaultColor);
GdkHelpers.RGBA.Parse(out color, defaultColor);
}
var red = (int)(color!.Value.Red * 255);
var green = (int)(color.Value.Green * 255);
Expand Down
62 changes: 62 additions & 0 deletions NickvisionMoney.GNOME/Helpers/GdkHelpers.Cs
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);
}
}
76 changes: 76 additions & 0 deletions NickvisionMoney.GNOME/Helpers/GtkHelpers.Cs
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;
}
}
1 change: 0 additions & 1 deletion NickvisionMoney.GNOME/NickvisionMoney.GNOME.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<ItemGroup>
<PackageReference Include="GirCore.Adw-1" Version="0.5.0-preview.3" />
<PackageReference Include="Nickvision.Aura" Version="2023.11.2" />
<PackageReference Include="Nickvision.GirExt" Version="2023.7.3" />
<PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="7.3.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
</ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions NickvisionMoney.GNOME/Views/AccountView.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Controls;
using NickvisionMoney.GNOME.Controls;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
using NickvisionMoney.Shared.Events;
Expand Down
3 changes: 1 addition & 2 deletions NickvisionMoney.GNOME/Views/GroupDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
using System;
Expand Down Expand Up @@ -120,7 +119,7 @@ private GroupDialog(Gtk.Builder builder, GroupDialogController controller, Gtk.W
//Load Group
_nameRow.SetText(_controller.Group.Name);
_descriptionRow.SetText(_controller.Group.Description);
GdkExt.RGBA.Parse(out var color, _controller.Group.RGBA);
GdkHelpers.RGBA.Parse(out var color, _controller.Group.RGBA);
_colorButton.SetExtRgba(color!.Value);
Validate();
_constructing = false;
Expand Down
1 change: 0 additions & 1 deletion NickvisionMoney.GNOME/Views/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Controls;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
Expand Down
1 change: 0 additions & 1 deletion NickvisionMoney.GNOME/Views/NewAccountDialog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nickvision.Aura.Keyring;
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
using NickvisionMoney.Shared.Models;
Expand Down
15 changes: 7 additions & 8 deletions NickvisionMoney.GNOME/Views/PreferencesDialog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
using NickvisionMoney.Shared.Models;
using System;
Expand Down Expand Up @@ -74,17 +73,17 @@ private PreferencesDialog(Gtk.Builder builder, PreferencesViewController control
OnHide += Hide;
//Load Config
_themeRow.SetSelected((uint)_controller.Theme);
GdkExt.RGBA.Parse(out var transactionColor, _controller.TransactionDefaultColor);
GdkHelpers.RGBA.Parse(out var transactionColor, _controller.TransactionDefaultColor);
_transactionColorButton.SetExtRgba(transactionColor!.Value);
GdkExt.RGBA.Parse(out var transferColor, _controller.TransferDefaultColor);
GdkHelpers.RGBA.Parse(out var transferColor, _controller.TransferDefaultColor);
_transferColorButton.SetExtRgba(transferColor!.Value);
GdkExt.RGBA.Parse(out var groupColor, _controller.GroupDefaultColor);
GdkHelpers.RGBA.Parse(out var groupColor, _controller.GroupDefaultColor);
_groupColorButton.SetExtRgba(groupColor!.Value);
GdkExt.RGBA.Parse(out var accountCheckingColor, _controller.AccountCheckingColor);
GdkHelpers.RGBA.Parse(out var accountCheckingColor, _controller.AccountCheckingColor);
_accountCheckingColorButton.SetExtRgba(accountCheckingColor!.Value);
GdkExt.RGBA.Parse(out var accountSavingsColor, _controller.AccountSavingsColor);
GdkHelpers.RGBA.Parse(out var accountSavingsColor, _controller.AccountSavingsColor);
_accountSavingsColorButton.SetExtRgba(accountSavingsColor!.Value);
GdkExt.RGBA.Parse(out var accountBusinessColor, _controller.AccountBusinessColor);
GdkHelpers.RGBA.Parse(out var accountBusinessColor, _controller.AccountBusinessColor);
_accountBusinessColorButton.SetExtRgba(accountBusinessColor!.Value);
_nativeDigitsRow.SetActive(_controller.UseNativeDigits);
_insertSeparatorRow.SetSelected((uint)_controller.InsertSeparator);
Expand Down
5 changes: 2 additions & 3 deletions NickvisionMoney.GNOME/Views/TransactionDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Controls;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
Expand Down Expand Up @@ -212,7 +211,7 @@ private TransactionDialog(Gtk.Builder builder, TransactionDialogController contr
_colorDropDown.SetSelected((e.Item2.UseGroupColor && _groupRow.GetSelected() != 0) ? 0u : 1u);
_colorDropDown.SetVisible(_groupRow.GetSelected() != 0);
_colorButton.SetVisible(_colorDropDown.GetSelected() == 1);
GdkExt.RGBA.Parse(out var transactionColor, e.Item2.RGBA);
GdkHelpers.RGBA.Parse(out var transactionColor, e.Item2.RGBA);
_colorButton.SetExtRgba(transactionColor!.Value);
}
};
Expand Down Expand Up @@ -417,7 +416,7 @@ private TransactionDialog(Gtk.Builder builder, TransactionDialogController contr
_colorDropDown.SetSelected((_controller.Transaction.UseGroupColor && _groupRow.GetSelected() != 0) ? 0u : 1u);
_colorDropDown.SetVisible(_groupRow.GetSelected() != 0);
_colorButton.SetVisible(_colorDropDown.GetSelected() == 1);
GdkExt.RGBA.Parse(out var transactionColor, _controller.Transaction.RGBA);
GdkHelpers.RGBA.Parse(out var transactionColor, _controller.Transaction.RGBA);
_colorButton.SetExtRgba(transactionColor!.Value);
UpdateTagsList();
_receipt = _controller.Transaction.Receipt;
Expand Down
1 change: 0 additions & 1 deletion NickvisionMoney.GNOME/Views/TransferDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Nickvision.GirExt;
using NickvisionMoney.GNOME.Controls;
using NickvisionMoney.GNOME.Helpers;
using NickvisionMoney.Shared.Controllers;
Expand Down
Loading

0 comments on commit a7286c6

Please sign in to comment.