From bf8a53b51376f0d06a63064b65674c67394cf9f8 Mon Sep 17 00:00:00 2001 From: Shaojun Li Date: Fri, 26 Jul 2024 11:07:42 +0800 Subject: [PATCH 1/2] Fix IME not working in some scenarios. * If other services trigger `OnNameChange` before `GetNameOwnerAsync`, then we will incorrectly connect to other services, and will be stuck at `Connect`. We should ignore irrelevant services. * `WatchNameOwnerChangedAsync` should be called only once. --- .../DBusIme/DBusTextInputMethodBase.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs b/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs index 158321acf5c..d72933d4279 100644 --- a/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs +++ b/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using Avalonia.Input.Raw; @@ -59,12 +60,18 @@ public DBusTextInputMethodBase(Connection connection, params string[] knownNames private async Task WatchAsync() { + var dbus = new OrgFreedesktopDBus(Connection, "org.freedesktop.DBus", "/org/freedesktop/DBus"); + try + { + _disposables.Add(await dbus.WatchNameOwnerChangedAsync(OnNameChange)); + } + catch (DBusException) + { + } foreach (var name in _knownNames) { - var dbus = new OrgFreedesktopDBus(Connection, "org.freedesktop.DBus", "/org/freedesktop/DBus"); try { - _disposables.Add(await dbus.WatchNameOwnerChangedAsync(OnNameChange)); var nameOwner = await dbus.GetNameOwnerAsync(name); OnNameChange(null, (name, null, nameOwner)); } @@ -87,6 +94,11 @@ private async void OnNameChange(Exception? e, (string ServiceName, string? OldOw return; } + if (!_knownNames.Contains(args.ServiceName)) + { + return; + } + if (args.NewOwner is not null && _currentName is null) { _onlineNamesQueue.Enqueue(args.ServiceName); From 0582034b7fa77a93b47b488111b32cd633d87cf3 Mon Sep 17 00:00:00 2001 From: Shaojun Li Date: Fri, 26 Jul 2024 14:17:22 +0800 Subject: [PATCH 2/2] Add log. --- src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs b/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs index d72933d4279..764bc9df157 100644 --- a/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs +++ b/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs @@ -65,8 +65,9 @@ private async Task WatchAsync() { _disposables.Add(await dbus.WatchNameOwnerChangedAsync(OnNameChange)); } - catch (DBusException) + catch (DBusException e) { + Logger.TryGet(LogEventLevel.Error, LogArea.FreeDesktopPlatform)?.Log(this, $"WatchNameOwnerChangedAsync failed: {e}"); } foreach (var name in _knownNames) { @@ -75,8 +76,9 @@ private async Task WatchAsync() var nameOwner = await dbus.GetNameOwnerAsync(name); OnNameChange(null, (name, null, nameOwner)); } - catch (DBusException) + catch (DBusException e) { + Logger.TryGet(LogEventLevel.Error, LogArea.FreeDesktopPlatform)?.Log(this, $"GetNameOwnerAsync failed: {e}"); } } }