From 736e3764e1e3b96abb8939c910690521b9d9151c Mon Sep 17 00:00:00 2001 From: Andrzej Martyna Date: Mon, 29 Dec 2025 23:51:50 +0100 Subject: [PATCH] Resolved: several issues reported in PR --- .../Tabs/TabsCache.cs | 6 +- .../Tabs/TabsTracker.cs | 72 +++++++++---------- .../Tabs/TabsWalker.cs | 5 +- .../Views/SettingsControl.xaml.cs | 2 +- 4 files changed, 38 insertions(+), 47 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsCache.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsCache.cs index 331ba2bae..3bd8de31c 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsCache.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsCache.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Windows.Automation; -using System.Windows.Forms; -using System.Windows.Input; -using System.Xml.Linq; using static Flow.Launcher.Plugin.BrowserBookmark.Main; namespace Flow.Launcher.Plugin.BrowserBookmark.Tabs; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsTracker.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsTracker.cs index 5d849c2e5..bc5088885 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsTracker.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsTracker.cs @@ -83,16 +83,17 @@ public class TabsTracker : IDisposable public void Dispose() { - if (_focusHandler != null) - { - Automation.RemoveAutomationFocusChangedEventHandler(_focusHandler); - _focusHandler = null; - } var windowsToUnsubscribe = _browserWindowsTracked.ToList(); foreach (var wnd in windowsToUnsubscribe) { UnsubscribeStructureChangedForWindow(wnd); } + if (_focusHandler != null) + { + Automation.RemoveAutomationFocusChangedEventHandler(_focusHandler); + _focusHandler = null; + _initialized = false; + } } public void ExpectUrl(string url) @@ -145,44 +146,37 @@ public class TabsTracker : IDisposable return; int pid = element.Current.ProcessId; - try - { - using var process = Process.GetProcessById(pid); - var chromium = chromiumProcessNames.Contains(process.ProcessName); - var firefox = firefoxProcessNames.Contains(process.ProcessName); - if (!chromium && !firefox) - return; // not a browser + using var process = Process.GetProcessById(pid); - Context.API.LogDebug(ClassName, $"The active browser is {process.ProcessName}"); + var chromium = chromiumProcessNames.Contains(process.ProcessName); + var firefox = firefoxProcessNames.Contains(process.ProcessName); + if (!chromium && !firefox) + return; // not a browser - var rootElement = AutomationElement.FromHandle(process.MainWindowHandle); - if (rootElement == null) - return; + Context.API.LogDebug(ClassName, $"The active browser is {process.ProcessName}"); - Context.API.LogDebug(ClassName, $"The root element is {rootElement.Current.Name}"); - - var currentTab = _walker.GetCurrentTabFromWindow(rootElement, process, CancellationToken.None); - if (currentTab != null) - { - lock (_sync) - { - Context.API.LogDebug(ClassName, $"Registering {urlToBind} as tab: {currentTab.Title}"); - UrlToBrowserTab[urlToBind] = currentTab; - _expectedUrl = null; - - // required to take the tab into account by Flow Launcher main UI search window - Context.API.ReQuery(); - } - - Automation.AddStructureChangedEventHandler(rootElement, TreeScope.Subtree, OnStructureChanged); - Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, rootElement, TreeScope.Subtree, OnWindowClosed); - _browserWindowsTracked.Add(rootElement); - } - } - catch (ArgumentException) - { - // No such process / not running + var rootElement = AutomationElement.FromHandle(process.MainWindowHandle); + if (rootElement == null) return; + + Context.API.LogDebug(ClassName, $"The root element is {rootElement.Current.Name}"); + + var currentTab = _walker.GetCurrentTabFromWindow(rootElement, process, CancellationToken.None); + if (currentTab != null) + { + lock (_sync) + { + Context.API.LogDebug(ClassName, $"Registering {urlToBind} as tab: {currentTab.Title}"); + UrlToBrowserTab[urlToBind] = currentTab; + _expectedUrl = null; + + // required to take the tab into account by Flow Launcher main UI search window + Context.API.ReQuery(); + } + + Automation.AddStructureChangedEventHandler(rootElement, TreeScope.Subtree, OnStructureChanged); + Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, rootElement, TreeScope.Subtree, OnWindowClosed); + _browserWindowsTracked.Add(rootElement); } } catch (Exception ex) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsWalker.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsWalker.cs index e264891f2..17885485e 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsWalker.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Tabs/TabsWalker.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; +using System.Threading.Tasks; using System.Windows.Automation; using BrowserTabs; using static Flow.Launcher.Plugin.BrowserBookmark.Main; @@ -67,7 +68,7 @@ internal class TabsWalker if (tabs.Count == 0) { Context.API.LogDebug(ClassName, "No valid tabs found"); - Thread.Sleep(_tabRetryInterval); + Task.Delay(_tabRetryInterval, cancellationToken); continue; } @@ -99,7 +100,7 @@ internal class TabsWalker } Context.API.LogDebug(ClassName, "No new tab found"); - Thread.Sleep(_tabRetryInterval); + Task.Delay(_tabRetryInterval, cancellationToken); } Context.API.LogDebug(ClassName, "Timeout waiting for new tab"); diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/SettingsControl.xaml.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/SettingsControl.xaml.cs index 5a10cca37..0fca79409 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/SettingsControl.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/SettingsControl.xaml.cs @@ -54,7 +54,7 @@ public partial class SettingsControl set { Settings.ReuseTabs = value; - _ = Task.Run(() => Main.ReloadAllBookmarks()); + // reloading of bookmarks is not needed while this settings changes } }