Skip to content

Commit

Permalink
Add DBus sample to read desktop appearance color scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel committed Aug 16, 2024
1 parent c1b996f commit 19ea31d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/GirCore.Libs.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"Samples\\Adw-1\\Window\\Window.csproj",
"Samples\\GdkPixbuf-2.0\\TestLoading\\TestLoading.csproj",
"Samples\\GdkPixbuf-2.0\\TestMemoryLeaks\\TestMemoryLeaks.csproj",
"Samples\\Gio-2.0\\DBusNotification\\DBusNotification.csproj",
"Samples\\Gio-2.0\\DBus\\DBus.csproj",
"Samples\\Gst-1.0\\VideoPlayback\\VideoPlayback.csproj",
"Samples\\Gtk-4.0\\AboutDialog\\AboutDialog.csproj",
"Samples\\Gtk-4.0\\Builder\\Builder.csproj",
Expand Down
2 changes: 1 addition & 1 deletion src/GirCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{1F2E
Samples\Directory.Build.props = Samples\Directory.Build.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBusNotification", "Samples\Gio-2.0\DBusNotification\DBusNotification.csproj", "{48A4AF23-C232-4498-9D25-B4620ECD9325}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBus", "Samples\Gio-2.0\DBus\DBus.csproj", "{48A4AF23-C232-4498-9D25-B4620ECD9325}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gst-1.0", "Libs\Gst-1.0\Gst-1.0.csproj", "{1567FD28-14D5-4161-AB66-B099E78F6FFB}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>DBus</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal static class Program
private static void Main(string[] args)
{
Gio.Module.Initialize();
Sample.DBus.ReadDesktopAppearanceColorScheme();
Sample.DBus.SendNotification();
}
}
42 changes: 42 additions & 0 deletions src/Samples/Gio-2.0/DBus/ReadDesktopAppereanceColorScheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Gio;
using GLib;

namespace Sample;

public static partial class DBus
{
public static void ReadDesktopAppearanceColorScheme()
{
Console.WriteLine("Press enter to read desktop appearance color scheme...");
Console.ReadLine();

//Portal spec: https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-org.freedesktop.portal.Settings

var bus = DBusConnection.Get(BusType.Session);
using var parameters = Variant.NewTuple(new[] {
Variant.NewString("org.freedesktop.appearance"),
Variant.NewString("color-scheme"),
});

using Variant ret = bus.CallSync(
busName: "org.freedesktop.portal.Desktop",
objectPath: "/org/freedesktop/portal/desktop",
interfaceName: "org.freedesktop.portal.Settings",
methodName: "Read",
parameters: parameters,
replyType: VariantType.New("(v)"),
flags: DBusCallFlags.None,
timeoutMsec: 9999,
cancellable: null
);
var mode = ret.GetChildValue(0).GetVariant().GetVariant().GetUint32() switch
{
0 => "No Preference",
1 => "DarkMode",
2 => "LightMode",
_ => "Unknown"
};
Console.WriteLine("Result: " + mode);
}
}

0 comments on commit 19ea31d

Please sign in to comment.