From ad8e67c1775969435ae82bb460a74c3b9461193d Mon Sep 17 00:00:00 2001 From: Shaojun Li Date: Thu, 12 Sep 2024 13:04:54 +0800 Subject: [PATCH] Fix IME not working in some scenarios. (#16476) * 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. * Add log. --- .../DBusIme/DBusTextInputMethodBase.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs b/src/Avalonia.FreeDesktop/DBusIme/DBusTextInputMethodBase.cs index 158321acf5..764bc9df15 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,17 +60,25 @@ namespace Avalonia.FreeDesktop.DBusIme private async Task WatchAsync() { + var dbus = new OrgFreedesktopDBus(Connection, "org.freedesktop.DBus", "/org/freedesktop/DBus"); + try + { + _disposables.Add(await dbus.WatchNameOwnerChangedAsync(OnNameChange)); + } + catch (DBusException e) + { + Logger.TryGet(LogEventLevel.Error, LogArea.FreeDesktopPlatform)?.Log(this, $"WatchNameOwnerChangedAsync failed: {e}"); + } 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)); } - catch (DBusException) + catch (DBusException e) { + Logger.TryGet(LogEventLevel.Error, LogArea.FreeDesktopPlatform)?.Log(this, $"GetNameOwnerAsync failed: {e}"); } } } @@ -87,6 +96,11 @@ namespace Avalonia.FreeDesktop.DBusIme return; } + if (!_knownNames.Contains(args.ServiceName)) + { + return; + } + if (args.NewOwner is not null && _currentName is null) { _onlineNamesQueue.Enqueue(args.ServiceName);