Skip to content

Commit e2c17ea

Browse files
committed
Search bar: recent images and macros now included as search terms
For keyboard power users, this makes it extremely fast to load a recent image or macro - type Ctrl+F, some characters from the filename, and Enter!
1 parent 8a8160e commit e2c17ea

File tree

7 files changed

+169
-68
lines changed

7 files changed

+169
-68
lines changed

Classes/pdMRUManager.cls

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@ Attribute VB_Exposed = False
2020
'Last update: Trimmed fileName in MRU_AddNewFile, and deleted old XML file if Max limit changed.
2121
'
2222
'This class is responsible for the creation and maintenance of MRU or "Most Recently Used" lists.
23-
' It contains functionality for saving and loading MRU lists to files, and updating a single
24-
' menu array. The functionality can be further specialized if required.
23+
' It contains functionality for saving and loading MRU lists to files, and updating a single
24+
' menu array. The functionality can be further specialized if required.
2525
'
26-
'Many thanks to Raj Chaudhuri for rewriting this class as a generic manager, so PD can now support recent file lists
27-
' in multiple places throughout the program.
26+
'Many thanks to Raj Chaudhuri for rewriting this class as a generic manager, so PD can now support
27+
'crecent file lists in multiple places throughout the program.
2828
'
2929
'Unless otherwise noted, all source code in this file is shared under a simplified BSD license.
3030
' Full license details are available in the LICENSE.md file, or at https://photodemon.org/license/
3131
'
3232
'***************************************************************************
33-
Option Explicit
34-
3533

36-
'These functions are used to shrink long path entries to a max number of characters. PhotoDemon exposes a global preference
37-
' for Recent File menus, and this API is used to shrink full-length paths as necessary to fit them within a menu.
34+
Option Explicit
3835

39-
Private Const maxMRULength As Long = 64
36+
'Long filenames (and/or paths, if the user selects that option in Tools > Preference) are automatically
37+
' shrunk using PathCompact; this constant controls the maximum length, in characters.
38+
Private Const MAX_MRU_LENGTH_IN_CHARS As Long = 64
4039

4140
'MRUlist will contain string entries of all the most recently used files
4241
Private m_FileList() As String
@@ -47,8 +46,8 @@ Private m_NumEntries As Long
4746
'PD's standard XML engine is used to write the recent file lists out to file
4847
Private m_XMLEngine As pdXML
4948

50-
'The file where this class persistently stores its data. In PD, the path of this file is controlled by the global
51-
' preferences object (UserPrefs)
49+
'The file where this class persistently stores its data. In PD, the path of this file is controlled by
50+
' the preferences manager (UserPrefs)
5251
Private m_XMLFilename As String
5352

5453
'This class relies on a child class that implements that the IMRUList interface.
@@ -69,35 +68,31 @@ Private Function GetMRUCaption(ByRef newFile As String) As String
6968
If (UserPrefs.GetPref_Long("Core", "MRU Caption Length", 0) = 0) Then
7069
GetMRUCaption = Files.FileGetName(newFile)
7170
Else
72-
GetMRUCaption = Files.PathCompact(newFile, maxMRULength)
71+
GetMRUCaption = Files.PathCompact(newFile, MAX_MRU_LENGTH_IN_CHARS)
7372
End If
7473

7574
End Function
7675

7776
'Return the actual file path at a given index
7877
Public Function GetSpecificMRU(ByVal mIndex As Long) As String
79-
80-
If (mIndex <= m_NumEntries) And (mIndex >= 0) Then
78+
If (mIndex >= 0) And (mIndex <= m_NumEntries) Then
8179
GetSpecificMRU = m_FileList(mIndex)
8280
Else
8381
GetSpecificMRU = vbNullString
8482
End If
85-
8683
End Function
8784

8885
'Return a menu-friendly caption of a given index
89-
Public Function GetSpecificMRUCaption(ByVal Index As Long) As String
90-
91-
If (Index <= m_NumEntries) And (Index >= 0) Then
92-
GetSpecificMRUCaption = GetMRUCaption(m_FileList(Index))
86+
Public Function GetSpecificMRUCaption(ByVal mIndex As Long) As String
87+
If (mIndex >= 0) And (mIndex <= m_NumEntries) Then
88+
GetSpecificMRUCaption = GetMRUCaption(m_FileList(mIndex))
9389
Else
9490
GetSpecificMRUCaption = vbNullString
9591
End If
96-
9792
End Function
9893

99-
'Return the path to an MRU thumbnail file. (PD uses PNGs only, but any valid image format should technically work.)
100-
Public Function GetMRUThumbnailPath(ByVal Index As Long) As String
94+
'Return the path to an MRU thumbnail file (if one exists; PD does not currently support this for macros)
95+
Public Function GetMRUThumbnailPath(ByVal mIndex As Long) As String
10196

10297
GetMRUThumbnailPath = vbNullString
10398

@@ -106,7 +101,7 @@ Public Function GetMRUThumbnailPath(ByVal Index As Long) As String
106101

107102
'Ignore thumbnail requests if the current list is empty
108103
If (m_NumEntries > 0) Then
109-
If (Index >= 0) And (Index <= m_NumEntries) Then GetMRUThumbnailPath = m_MRUList.GetThumbnailPath(Me, Index)
104+
If (mIndex >= 0) And (mIndex <= m_NumEntries) Then GetMRUThumbnailPath = m_MRUList.GetThumbnailPath(Me, mIndex)
110105
End If
111106

112107
End If
@@ -130,12 +125,12 @@ Public Sub MRU_LoadFromFile()
130125
End If
131126

132127
'Allow the child to run any required initialization steps
133-
Dim Cancel As Boolean
134-
Cancel = False
135-
m_MRUList.BeforeListLoad Me, Cancel
128+
Dim childCanceled As Boolean
129+
childCanceled = False
130+
m_MRUList.BeforeListLoad Me, childCanceled
136131

137132
'If something goes wrong, the child is allowed to abort the load process. Do not proceed if cancellation was requested.
138-
If Cancel Then Exit Sub
133+
If childCanceled Then Exit Sub
139134

140135
'We are now ready to load the actual MRU data from file.
141136

@@ -186,16 +181,13 @@ End Sub
186181
'Save the current MRU list to file (currently done at program close)
187182
Public Sub MRU_SaveToFile()
188183

189-
Dim Cancel As Boolean
190-
Cancel = False
184+
Dim saveCancel As Boolean
185+
saveCancel = False
191186

192187
'Allow the child to do any necessary prep work. They can cancel this operation; if they do, the file will not be saved.
193-
m_MRUList.BeforeListSave Me, Cancel
194-
If Cancel Then Exit Sub
188+
m_MRUList.BeforeListSave Me, saveCancel
189+
If saveCancel Then Exit Sub
195190

196-
'Since the switch to pdFSO for file operations, we should no longer experience write errors, but better safe than sorry
197-
On Error GoTo MRUSaveFailure
198-
199191
'Reset whatever XML data we may have stored at present - we will be rewriting the full MRU file from scratch.
200192
ResetXMLData
201193

@@ -222,9 +214,6 @@ Public Sub MRU_SaveToFile()
222214
'Allow the child to perform any post-save cleanup
223215
m_MRUList.AfterListSave Me
224216

225-
226-
MRUSaveFailure:
227-
228217
End Sub
229218

230219
'Add another file to the MRU list
@@ -234,10 +223,10 @@ Public Sub MRU_AddNewFile(ByVal newFile As String, Optional ByRef srcImage As pd
234223
newFile = Strings.TrimNull(newFile)
235224

236225
'Allow the child to perform any necessary UI prep work. The child also has the option to cancel this operation.
237-
Dim Cancel As Boolean
238-
Cancel = False
239-
m_MRUList.BeforeFileAdded Me, newFile, Cancel
240-
If Cancel Then Exit Sub
226+
Dim childCancel As Boolean
227+
childCancel = False
228+
m_MRUList.BeforeFileAdded Me, newFile, childCancel
229+
If childCancel Then Exit Sub
241230

242231
'Locators are used to determine if this file already exists in the recent files list.
243232
' If it does, we will simply shuffle its position instead of adding it as a new entry.
@@ -376,4 +365,3 @@ Public Sub InitList(specificList As IMRUList)
376365
'If an XML file exists, it will be loaded separately, by the MRU_LoadFromFile() function
377366

378367
End Sub
379-

Classes/pdRecentFiles.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Friend Function GetNumOfItems() As Long
490490
End Function
491491

492492
Private Sub InternalError(ByVal errText As String)
493-
PDDebug.LogAction "WARNING! pdRecentFiles has a problem: " & errText
493+
PDDebug.LogAction "WARNING! pdRecentFiles problem: " & errText
494494
End Sub
495495

496496
Private Sub Class_Initialize()

Forms/Layerpanel_Search.frm

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,21 @@ Option Explicit
6565
Private WithEvents lastUsedSettings As pdLastUsedSettings
6666
Attribute lastUsedSettings.VB_VarHelpID = -1
6767

68-
'Some search terms are managed by the menu manager. Others are managed by the tool manager.
68+
'Some search terms are managed by the menu manager; other, the tool manager or a miscellaneous catcha-ll.
6969
' We want to condense these into a single list of search terms, and when a search result is returned,
7070
' we'll match it up against its relevant source stack.
7171
Private m_AllSearchTerms As pdStringStack
7272
Private m_MenuSearchTerms As pdStringStack
73+
74+
'Menus can auto-map between searchable text and underlying query ID. Tools and miscellaneous targets cannot,
75+
' so we need to store *two* stacks per category - one for human-readable search terms, and another for
76+
' tool action IDs.
7377
Private m_ToolSearchTerms As pdStringStack
7478
Private m_ToolActions As pdStringStack
7579

80+
Private m_MiscSearchTerms As pdStringStack
81+
Private m_MiscActions As pdStringStack
82+
7683
Private Sub Form_Load()
7784

7885
'Load any last-used settings for this form
@@ -132,8 +139,13 @@ Private Sub Form_Resize()
132139
ReflowInterface
133140
End Sub
134141

142+
'If the search box does not have focus, give it focus. If it already has focus, select all text.
135143
Public Sub SetFocusToSearchBox()
136-
srchMain.SetFocusToSearchBox
144+
If srchMain.HasFocus() Then
145+
srchMain.SelectAll
146+
Else
147+
srchMain.SetFocusToSearchBox
148+
End If
137149
End Sub
138150

139151
Private Sub lastUsedSettings_ReadCustomPresetData()
@@ -144,13 +156,30 @@ Private Sub srchMain_Click(bestSearchHit As String)
144156

145157
srchMain.Text = bestSearchHit
146158

147-
If (m_MenuSearchTerms.ContainsString(bestSearchHit) >= 0) Then
159+
'Because the search term can come from one of three stacks (menus, tools, misc), we need to identify
160+
' which stack the current search term originated from to best know where to trigger its associated action.
161+
162+
'Check the menu stack first
163+
Dim idxQuery As Long
164+
idxQuery = m_MenuSearchTerms.ContainsString(bestSearchHit, True)
165+
166+
If (idxQuery >= 0) Then
148167
Actions.LaunchAction_BySearch bestSearchHit
149168

150-
'If the search result is *not* a menu, the menu module can't auto-map it to a corresponding
151-
' action string. Return the action string instead.
152169
Else
153-
Actions.LaunchAction_ByName m_ToolActions.GetString(m_ToolSearchTerms.ContainsString(bestSearchHit, True)), pdas_Search
170+
171+
'Check the tool stack next
172+
idxQuery = m_ToolSearchTerms.ContainsString(bestSearchHit, True)
173+
If (idxQuery >= 0) Then
174+
Actions.LaunchAction_ByName m_ToolActions.GetString(idxQuery), pdas_Search
175+
Else
176+
177+
'Finally, check the miscellaneous stack
178+
idxQuery = m_MiscSearchTerms.ContainsString(bestSearchHit, True)
179+
Actions.LaunchAction_ByName m_MiscActions.GetString(idxQuery), pdas_Search
180+
181+
End If
182+
154183
End If
155184

156185
'Before exiting, update the search list as available items may have changed.
@@ -169,12 +198,20 @@ End Sub
169198

170199
Private Sub UpdateSearchTerms()
171200

201+
'Start with menu-based search terms
172202
Set m_AllSearchTerms = New pdStringStack
173203
Menus.GetSearchableMenuList m_MenuSearchTerms
174204
m_AllSearchTerms.CloneStack m_MenuSearchTerms
175205

206+
'Add tool search terms
176207
toolbar_Toolbox.GetListOfToolNamesAndActions m_ToolSearchTerms, m_ToolActions
177208
m_AllSearchTerms.AppendStack m_ToolSearchTerms
209+
210+
'Add misc search terms
211+
Actions.GetMiscellaneousSearchActions m_MiscSearchTerms, m_MiscActions
212+
m_AllSearchTerms.AppendStack m_MiscSearchTerms
213+
214+
'Forward the full list to the search box
178215
srchMain.SetSearchList m_AllSearchTerms
179216

180217
End Sub

Forms/MainWindow.frm

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,14 +3532,7 @@ Private Sub MnuRecentFiles_Click(Index As Integer)
35323532
End Sub
35333533

35343534
Private Sub MnuRecentMacros_Click(Index As Integer)
3535-
3536-
'Load the MRU Macro path that correlates to this index. (If one is not found, a null string is returned)
3537-
Dim tmpString As String
3538-
tmpString = g_RecentMacros.GetSpecificMRU(Index)
3539-
3540-
'Check - just in case - to make sure the path isn't empty
3541-
If (LenB(tmpString) <> 0) Then Macros.PlayMacroFromFile tmpString
3542-
3535+
Actions.LaunchAction_ByName COMMAND_TOOLS_MACRO_RECENT & Trim$(Str$(Index))
35433536
End Sub
35443537

35453538
Private Sub MnuRender_Click(Index As Integer)

0 commit comments

Comments
 (0)