Skip to content

Commit

Permalink
Reapply (#15568) + Updates (#16129)
Browse files Browse the repository at this point in the history
* Reapply "Bump DBus stack (#15568)" (#15656)

This reverts commit e787bb4.

* Bump dbus libs versions again

* Fix PathHandler usages

* Add suppression for FreeDesktop lib

---------

Co-authored-by: Max Katz <maxkatz6@outlook.com>
  • Loading branch information
jmacato and maxkatz6 authored Jun 26, 2024
1 parent 0b5adbd commit 9c955d3
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 175 deletions.
10 changes: 10 additions & 0 deletions api/Avalonia.FreeDesktop.nupkg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Tmds.DBus.SourceGenerator.PropertyChanges`1</Target>
<Left>baseline/netstandard2.0/Avalonia.FreeDesktop.dll</Left>
<Right>target/netstandard2.0/Avalonia.FreeDesktop.dll</Right>
</Suppression>
</Suppressions>
4 changes: 2 additions & 2 deletions src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Tmds.DBus.Protocol" Version="0.16.0" />
<PackageReference Include="Tmds.DBus.SourceGenerator" Version="0.0.15" PrivateAssets="all" />
<PackageReference Include="Tmds.DBus.Protocol" Version="0.19.0" />
<PackageReference Include="Tmds.DBus.SourceGenerator" Version="0.0.17" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.FreeDesktop/DBusIme/Fcitx/FcitxICWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public ValueTask<IDisposable> WatchCommitStringAsync(Action<Exception?, string>

public ValueTask<IDisposable> WatchForwardKeyAsync(Action<Exception?, (uint keyval, uint state, int type)> handler) =>
_old?.WatchForwardKeyAsync(handler)
?? _modern?.WatchForwardKeyAsync((e, ev) => handler.Invoke(e, (ev.keyval, ev.state, ev.type ? 1 : 0)))
?? _modern?.WatchForwardKeyAsync((e, ev) => handler.Invoke(e, (ev.Keyval, ev.State, ev.Type ? 1 : 0)))
?? new ValueTask<IDisposable>(Disposable.Empty);

public ValueTask<IDisposable> WatchUpdateFormattedPreeditAsync(
Action<Exception?, ((string?, int)[]? str, int cursorpos)> handler) =>
_old?.WatchUpdateFormattedPreeditAsync(handler!)
?? _modern?.WatchUpdateFormattedPreeditAsync(handler!)
?? new ValueTask<IDisposable>(Disposable.Empty);

public Task SetCapacityAsync(uint flags) =>
_old?.SetCapacityAsync(flags) ?? _modern?.SetCapabilityAsync(flags) ?? Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override async Task<bool> Connect(string name)
var resp = await method.CreateICv3Async(GetAppName(),
Process.GetCurrentProcess().Id);

var proxy = new OrgFcitxFcitxInputContext(Connection, name, $"/inputcontext_{resp.icid}");
var proxy = new OrgFcitxFcitxInputContext(Connection, name, $"/inputcontext_{resp.Icid}");
_context = new FcitxICWrapper(proxy);
}
else
Expand Down
21 changes: 10 additions & 11 deletions src/Avalonia.FreeDesktop/DBusIme/IBus/IBusX11TextInputMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ private void OnShowPreedit(Exception? obj)
Client.SetPreeditText(_preeditText, _preeditText == null ? null : _preeditCursor);
}

private void OnUpdatePreedit(Exception? arg1, (DBusVariantItem text, uint cursor_pos, bool visible) preeditComponents)
private void OnUpdatePreedit(Exception? arg1, (VariantValue Text, uint CursorPos, bool Visible) preeditComponents)
{
if (preeditComponents.text is { Value: DBusStructItem { Count: >= 3 } structItem } &&
structItem[2] is DBusStringItem stringItem)
if (preeditComponents.Text is { Type: VariantValueType.Struct, Count: >= 3 } structItem && structItem.GetItem(2) is { Type: VariantValueType.String} stringItem)
{
_preeditText = stringItem.Value;
_preeditText = stringItem.GetString();
_preeditCursor = _preeditText != null
? Utf16Utils.CharacterOffsetToStringOffset(_preeditText,
(int)Math.Min(preeditComponents.cursor_pos, int.MaxValue), false)
(int)Math.Min(preeditComponents.CursorPos, int.MaxValue), false)
: 0;

_preeditShown = true;
}
else
Expand Down Expand Up @@ -102,13 +101,13 @@ private void OnForwardKey(Exception? e, (uint keyval, uint keycode, uint state)
});
}

private void OnCommitText(Exception? e, DBusVariantItem variantItem)
private void OnCommitText(Exception? e, VariantValue variantItem)
{
if (_insideReset > 0)
if (_insideReset > 0)
{
// For some reason iBus can trigger a CommitText while being reset.
// Thankfully the signal is sent _during_ Reset call processing,
// so it arrives on-the-wire before Reset call result, so we can
// so it arrives on-the-wire before Reset call result, so we can
// check if we have any pending Reset calls and ignore the signal here
return;
}
Expand All @@ -118,8 +117,8 @@ private void OnCommitText(Exception? e, DBusVariantItem variantItem)
return;
}

if (variantItem.Value is DBusStructItem { Count: >= 3 } structItem && structItem[2] is DBusStringItem stringItem)
FireCommit(stringItem.Value);
if (variantItem.Count >= 3 && variantItem.GetItem(2) is { Type: VariantValueType.String } stringItem)
FireCommit(stringItem.GetString());
}

protected override Task DisconnectAsync() => _service?.DestroyAsync() ?? Task.CompletedTask;
Expand Down
103 changes: 50 additions & 53 deletions src/Avalonia.FreeDesktop/DBusMenuExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public DBusMenuExporterImpl(Connection connection, IntPtr xid)
InitBackingProperties();
Connection = connection;
_xid = (uint)xid.ToInt32();
Path = GenerateDBusMenuObjPath;

PathHandler = new PathHandler(GenerateDBusMenuObjPath);
PathHandler.Add(this);

SetNativeMenu(new NativeMenu());
_ = InitializeAsync();
}
Expand All @@ -53,24 +56,22 @@ public DBusMenuExporterImpl(Connection connection, string path)
InitBackingProperties();
Connection = connection;
_appMenu = false;
Path = path;

PathHandler = new PathHandler(path);
PathHandler.Add(this);

SetNativeMenu(new NativeMenu());
_ = InitializeAsync();
}

private void InitBackingProperties()
{
BackingProperties.Version = 4;
BackingProperties.Status = string.Empty;
BackingProperties.TextDirection = string.Empty;
BackingProperties.IconThemePath = Array.Empty<string>();
Version = 4;
}

protected override Connection Connection { get; }
public override Connection Connection { get; }

public override string Path { get; }

protected override ValueTask<(uint revision, (int, Dictionary<string, DBusVariantItem>, DBusVariantItem[]) layout)> OnGetLayoutAsync(int parentId, int recursionDepth, string[] propertyNames)
protected override ValueTask<(uint Revision, (int, Dictionary<string, Variant>, Variant[]) Layout)> OnGetLayoutAsync(int parentId, int recursionDepth, string[] propertyNames)
{
var menu = GetMenu(parentId);
var layout = GetLayout(menu.item, menu.menu, recursionDepth, propertyNames);
Expand All @@ -80,22 +81,22 @@ private void InitBackingProperties()
OnIsNativeMenuExportedChanged?.Invoke(this, EventArgs.Empty);
}

return new ValueTask<(uint, (int, Dictionary<string, DBusVariantItem>, DBusVariantItem[]))>((_revision, layout));
return new ValueTask<(uint, (int, Dictionary<string, Variant>, Variant[]))>((_revision, layout));
}

protected override ValueTask<(int, Dictionary<string, DBusVariantItem>)[]> OnGetGroupPropertiesAsync(int[] ids, string[] propertyNames)
protected override ValueTask<(int, Dictionary<string, Variant>)[]> OnGetGroupPropertiesAsync(int[] ids, string[] propertyNames)
=> new(ids.Select(id => (id, GetProperties(GetMenu(id), propertyNames))).ToArray());

protected override ValueTask<DBusVariantItem> OnGetPropertyAsync(int id, string name) =>
new(GetProperty(GetMenu(id), name) ?? new DBusVariantItem("i", new DBusInt32Item(0)));
protected override ValueTask<Variant> OnGetPropertyAsync(int id, string name) =>
new(GetProperty(GetMenu(id), name) ?? new Variant(0));

protected override ValueTask OnEventAsync(int id, string eventId, DBusVariantItem data, uint timestamp)
protected override ValueTask OnEventAsync(int id, string eventId, VariantValue data, uint timestamp)
{
HandleEvent(id, eventId);
return new ValueTask();
}

protected override ValueTask<int[]> OnEventGroupAsync((int, string, DBusVariantItem, uint)[] events)
protected override ValueTask<int[]> OnEventGroupAsync((int, string, VariantValue, uint)[] events)
{
foreach (var e in events)
HandleEvent(e.Item1, e.Item2);
Expand All @@ -104,20 +105,20 @@ protected override ValueTask<int[]> OnEventGroupAsync((int, string, DBusVariantI

protected override ValueTask<bool> OnAboutToShowAsync(int id) => new(false);

protected override ValueTask<(int[] updatesNeeded, int[] idErrors)> OnAboutToShowGroupAsync(int[] ids) =>
protected override ValueTask<(int[] UpdatesNeeded, int[] IdErrors)> OnAboutToShowGroupAsync(int[] ids) =>
new((Array.Empty<int>(), Array.Empty<int>()));

private async Task InitializeAsync()
{
Connection.AddMethodHandler(this);
Connection.AddMethodHandler(this.PathHandler!);
if (!_appMenu)
return;

_registrar = new ComCanonicalAppMenuRegistrar(Connection, "com.canonical.AppMenu.Registrar", "/com/canonical/AppMenu/Registrar");
try
{
if (!_disposed)
await _registrar.RegisterWindowAsync(_xid, Path);
await _registrar.RegisterWindowAsync(_xid, this.PathHandler!.Path);
}
catch
{
Expand Down Expand Up @@ -220,66 +221,63 @@ private int GetId(NativeMenuItemBase item)
"type", "label", "enabled", "visible", "shortcut", "toggle-type", "children-display", "toggle-state", "icon-data"
};

private static DBusVariantItem? GetProperty((NativeMenuItemBase? item, NativeMenu? menu) i, string name)
private static Variant? GetProperty((NativeMenuItemBase? item, NativeMenu? menu) i, string name)
{
var (it, menu) = i;

if (it is NativeMenuItemSeparator)
{
if (name == "type")
return new DBusVariantItem("s", new DBusStringItem("separator"));
return new Variant("separator");
}
else if (it is NativeMenuItem item)
{
if (name == "type")
return null;
if (name == "label")
return new DBusVariantItem("s", new DBusStringItem(item.Header ?? "<null>"));
return new Variant(item.Header ?? "<null>");
if (name == "enabled")
{
if (item.Menu is not null && item.Menu.Items.Count == 0)
return new DBusVariantItem("b", new DBusBoolItem(false));
return new Variant(false);
if (!item.IsEnabled)
return new DBusVariantItem("b", new DBusBoolItem(false));
return new Variant(false);
return null;
}

if (name == "visible") {
if (!item.IsVisible)
return new DBusVariantItem("b", new DBusBoolItem(false));
return new DBusVariantItem("b", new DBusBoolItem(true));
}
if (name == "visible")
return new Variant(item.IsVisible);

if (name == "shortcut")
{
if (item.Gesture is null)
return null;
if (item.Gesture.KeyModifiers == 0)
return null;
var lst = new List<DBusItem>();
var lst = new Array<Variant>();
var mod = item.Gesture;
if (mod.KeyModifiers.HasAllFlags(KeyModifiers.Control))
lst.Add(new DBusStringItem("Control"));
lst.Add(new Variant("Control"));
if (mod.KeyModifiers.HasAllFlags(KeyModifiers.Alt))
lst.Add(new DBusStringItem("Alt"));
lst.Add(new Variant("Alt"));
if (mod.KeyModifiers.HasAllFlags(KeyModifiers.Shift))
lst.Add(new DBusStringItem("Shift"));
lst.Add(new Variant("Shift"));
if (mod.KeyModifiers.HasAllFlags(KeyModifiers.Meta))
lst.Add(new DBusStringItem("Super"));
lst.Add(new DBusStringItem(item.Gesture.Key.ToString()));
return new DBusVariantItem("aas", new DBusArrayItem(DBusType.Array, new[] { new DBusArrayItem(DBusType.String, lst) }));
lst.Add(new Variant("Super"));
lst.Add(new Variant(item.Gesture.Key.ToString()));
return Variant.FromArray(new Array<Array<Variant>>(new[] { lst }));
}

if (name == "toggle-type")
{
if (item.ToggleType == NativeMenuItemToggleType.CheckBox)
return new DBusVariantItem("s", new DBusStringItem("checkmark"));
return new Variant("checkmark");
if (item.ToggleType == NativeMenuItemToggleType.Radio)
return new DBusVariantItem("s", new DBusStringItem("radio"));
return new Variant("radio");
}

if (name == "toggle-state" && item.ToggleType != NativeMenuItemToggleType.None)
return new DBusVariantItem("i", new DBusInt32Item(item.IsChecked ? 1 : 0));
return new Variant(item.IsChecked ? 1 : 0);

if (name == "icon-data")
{
Expand All @@ -292,50 +290,49 @@ private int GetId(NativeMenuItemBase item)
var icon = loader.LoadIcon(item.Icon.PlatformImpl.Item);
using var ms = new MemoryStream();
icon.Save(ms);
return new DBusVariantItem("ay", new DBusByteArrayItem(ms.ToArray()));
return Variant.FromArray(new Array<byte>(ms.ToArray()));
}
}
}

if (name == "children-display")
return menu is not null ? new DBusVariantItem("s", new DBusStringItem("submenu")) : null;
{
if (menu is not null)
return new Variant("submenu");
return null;
}
}

return null;
}

private static Dictionary<string, DBusVariantItem> GetProperties((NativeMenuItemBase? item, NativeMenu? menu) i, string[] names)
private static Dictionary<string, Variant> GetProperties((NativeMenuItemBase? item, NativeMenu? menu) i, string[] names)
{
if (names.Length == 0)
names = s_allProperties;
var properties = new Dictionary<string, DBusVariantItem>();
var properties = new Dictionary<string, Variant>();
foreach (var n in names)
{
var v = GetProperty(i, n);
if (v is not null)
properties.Add(n, v);
if (v.HasValue)
properties.Add(n, v.Value);
}

return properties;
}

private (int, Dictionary<string, DBusVariantItem>, DBusVariantItem[]) GetLayout(NativeMenuItemBase? item, NativeMenu? menu, int depth, string[] propertyNames)
private (int, Dictionary<string, Variant>, Variant[]) GetLayout(NativeMenuItemBase? item, NativeMenu? menu, int depth, string[] propertyNames)
{
var id = item is null ? 0 : GetId(item);
var props = GetProperties((item, menu), propertyNames);
var children = depth == 0 || menu is null ? Array.Empty<DBusVariantItem>() : new DBusVariantItem[menu.Items.Count];
var children = depth == 0 || menu is null ? Array.Empty<Variant>() : new Variant[menu.Items.Count];
if (menu is not null)
{
for (var c = 0; c < children.Length; c++)
{
var ch = menu.Items[c];
var layout = GetLayout(ch, (ch as NativeMenuItem)?.Menu, depth == -1 ? -1 : depth - 1, propertyNames);
children[c] = new DBusVariantItem("(ia{sv}av)", new DBusStructItem(new DBusItem[]
{
new DBusInt32Item(layout.Item1),
new DBusArrayItem(DBusType.DictEntry, layout.Item2.Select(static x => new DBusDictEntryItem(new DBusStringItem(x.Key), x.Value)).ToArray()),
new DBusArrayItem(DBusType.Variant, layout.Item3)
}));
children[c] = Variant.FromStruct(Struct.Create(layout.Item1, new Dict<string, Variant>(layout.Item2), new Array<Variant>(layout.Item3)));
}
}

Expand Down
Loading

0 comments on commit 9c955d3

Please sign in to comment.