diff --git a/GUIs.ClipboardWatcher/ProfileFilter.vb b/GUIs.ClipboardWatcher/ProfileFilter.vb index b05cda61..f1fb821a 100644 --- a/GUIs.ClipboardWatcher/ProfileFilter.vb +++ b/GUIs.ClipboardWatcher/ProfileFilter.vb @@ -62,6 +62,11 @@ Public Class ProfileFilter Return _Profiles End Function + Public Function LogRemainingProfiles(Profiles As List(Of ProfileData), StepDescription As String) As List(Of ProfileData) + _Logger.Debug("Profiles remaining after Step {0}: {1}", StepDescription, Profiles.Count) + Return Profiles + End Function + Public Function FilterProfilesByClipboardRegex(Profiles As List(Of ProfileData), ClipboardContents As String) As List(Of ProfileData) Dim oFilteredProfiles As New List(Of ProfileData) @@ -99,37 +104,37 @@ Public Class ProfileFilter Return oFilteredProfiles End Function - Public Function FilterProfilesByProcess(Profiles As List(Of ProfileData), CurrentProcessName As String) As List(Of ProfileData) + Public Function FilterProfilesByProcess(Profiles As List(Of ProfileData), ProcessName As String) As List(Of ProfileData) Dim oFilteredProfiles As New List(Of ProfileData) Try - For Each oProfile In Profiles + For Each oProfile As ProfileData In Profiles Dim oGuid = oProfile.Guid Dim oProcesses As New List(Of ProcessData) - For Each oProcessDef As ProcessData In oProfile.Processes - If oProcessDef.PROFILE_ID <> oGuid Then + For Each oProcess As ProcessData In oProfile.Processes + If oProcess.PROFILE_ID <> oGuid Then Continue For End If - Dim oIsMatch = oProcessDef.ProcessName.ToLower = CurrentProcessName.ToLower + Dim oIsMatch = oProcess.ProcessName.ToLower = ProcessName.ToLower Dim oParent = _ProfileMatch.FindNodeByTag(_TreeView.Nodes, oProfile.Name & "-REGEX") If oParent IsNot Nothing Then - Dim oNode = _ProfileMatch.NewProcessNode(oProfile, oProcessDef, oIsMatch) + Dim oNode = _ProfileMatch.NewProcessNode(oProfile, oProcess, oIsMatch) oParent.Nodes.Add(oNode) End If _Logger.Debug($"FilterProfilesByProcess: Checking Profile: {oProfile.Name}") If oIsMatch Then - _Logger.Debug($"Processname Matched: {oProcessDef.ProcessName}") + _Logger.Debug($"Processname Matched: {oProcess.ProcessName}") oFilteredProfiles.Add(oProfile) _Logger.Info("Profile {0} matched!", oProfile.Name) - oProfile.MatchedProcessID = oProcessDef.Guid - oProcessDef.IsMatched = True - oProcesses.Add(oProcessDef) + oProfile.MatchedProcessID = oProcess.Guid + oProcess.IsMatched = True + oProcesses.Add(oProcess) oProfile.IsMatched = True - oProfile.MatchedProcessID = oProcessDef.Guid + oProfile.MatchedProcessID = oProcess.Guid End If Next @@ -150,17 +155,29 @@ Public Class ProfileFilter For Each oProfile As ProfileData In Profiles _Logger.Debug("Checking WindowDefinition for profile: {0}...", oProfile.Name) - Dim oWindows As New List(Of WindowData) + + Dim oFilteredWindows As New List(Of WindowData) + + ' Create list of all controls that match the current process and matched window + For Each oWindow As WindowData In oProfile.Windows + If oWindow.WindowProcessID = oProfile.MatchedProcessID Then + oFilteredWindows.Add(oWindow) + End If + Next ' If Profile has no windows at all, it automatically matches - If oProfile.Windows.Count = 0 Then + If oFilteredWindows.Count = 0 Then + _Logger.Debug("Profile has no Windows assigned, automatic MATCH") + oProfile.IsMatched = True oProfiles.Add(oProfile) Continue For End If - For Each oWindowDef As WindowData In oProfile.Windows + Dim oWindows As New List(Of WindowData) + + For Each oWindowDef As WindowData In oFilteredWindows If oWindowDef.WindowProcessID <> oProfile.MatchedProcessID Then Continue For Try If oWindowDef.Regex = String.Empty Then @@ -208,30 +225,40 @@ Public Class ProfileFilter Public Function FilterProfilesByFocusedControl(Profiles As List(Of ProfileData), ClipboardContents As String, WindowHandle As IntPtr) As List(Of ProfileData) Dim oFilteredProfiles As New List(Of ProfileData) Dim oWindow As New Window(_LogConfig) + Dim oFocusedWindow As Window.WindowInfo + + Try + oFocusedWindow = oWindow.GetWindowInfo() + If oFocusedWindow Is Nothing Then + Throw New ApplicationException("Window Information is Empty") + End If + Catch ex As Exception + _Logger.Error(ex) + Return Profiles + End Try + + For Each oProfile As ProfileData In Profiles + Dim oFilteredControls As New List(Of ControlData) + + ' Create list of all controls that match the current process and matched window + For Each oControl As ControlData In oProfile.Controls + If oProfile.MatchedWindowID = oControl.WindowId And oFocusedWindow.ProcessName = oControl.ProcessName Then + oFilteredControls.Add(oControl) + End If + Next - For Each oProfile In Profiles ' If Profile has no controls at all, it automatically matches - If oProfile.Controls.Count = 0 Then + If oFilteredControls.Count = 0 Then + _Logger.Debug("Profile has no Controls assigned, automatic MATCH") oFilteredProfiles.Add(oProfile) Continue For End If - Dim oControls As New List(Of ControlData) + Dim oMatchingControls As New List(Of ControlData) - For Each oControl In oProfile.Controls + For Each oControl In oFilteredControls Dim oFound As Boolean = False - ' If current control does not belong to the current process, skip it - Try - Dim oFocusedWindow As Window.WindowInfo = oWindow.GetWindowInfo() - - If oFocusedWindow.ProcessName <> oControl.ProcessName Then - Continue For - End If - Catch ex As Exception - _Logger.Error(ex) - End Try - ' If control name is empty, use coordinates If oControl.ControlName Is Nothing OrElse oControl.ControlName = String.Empty Then Dim oControlBounds As Dictionary(Of String, Window.RectangleInfo) @@ -279,7 +306,8 @@ Public Class ProfileFilter End If If oFound Then - oControls.Add(oControl) + _Logger.Debug("Control {0} has MATCH", oControl.ControlName) + oMatchingControls.Add(oControl) End If Dim oParent = _ProfileMatch.FindNodeByTag(_TreeView.Nodes, oControl.WindowId & "-WINDOW") @@ -290,8 +318,9 @@ Public Class ProfileFilter Next ' If Profile has controls that matched, the profile matches - If oControls.Count > 0 Then - oProfile.Controls = oControls + If oMatchingControls.Count > 0 Then + _Logger.Debug("Profile has {0} MATCHING Controls", oMatchingControls.Count) + oProfile.Controls = oMatchingControls oFilteredProfiles.Add(oProfile) _Logger.Info("Profile {0} matched!", oProfile.Name) diff --git a/GUIs.ClipboardWatcher/ProfileMatch.vb b/GUIs.ClipboardWatcher/ProfileMatch.vb index 8f36bf4f..b1886e5b 100644 --- a/GUIs.ClipboardWatcher/ProfileMatch.vb +++ b/GUIs.ClipboardWatcher/ProfileMatch.vb @@ -83,7 +83,7 @@ Public Class ProfileMatch _Logger.Debug("New Control Node for Profile {0} and Control {1}", Profile.Name, Control.Description) Dim oMatchText = IIf(IsMatch, "MATCH", "NO MATCH") - Dim oText = $"{GetMatchText(IsMatch)} on Control {Control.Description}: {IsMatch.ToString}" + Dim oText = $"{GetMatchText(IsMatch)} on Control {Control.Description}" Dim oNode As New TreeNode() With { .Text = oText,