It looks like you are trying to implement a media explorer with ImGui, featuring a search box with debounce functionality. Let's integrate the necessary components and complete the code to achieve that.
Here's a more complete example ensuring all parts seamlessly work together:
-- Required Reaper and ImGui libraries
require 'reaper_api'
require 'ImGui'
-- Store the reference to the currently playing preview
local current_preview = nil
-- Variables for search functionality
local search_query = ""
local last_search_query = ""
local search_cache = {}
local debounce_timer = 0
-- Constants and other settings
local BASE_DIRECTORY = "E:\\Sample Packs\\COLLECTION"
local DEBOUNCE_DELAY = 0.3 -- in seconds
-- Function to check if a directory contains any matching files
function directory_contains_matching_files(path, query)
if search_cache[path] ~= nil then
return search_cache[path]
end
local idx = 0
local contains_matches = false
while true do
local file = reaper.EnumerateFiles(path, idx)
if not file then break end
if string.match(file:lower(), query:lower()) then
contains_matches = true
break
end
idx = idx + 1
end
search_cache[path] = contains_matches
return contains_matches
end
-- Function to handle media playback
function handle_playback(item_path, volume)
if current_preview ~= nil then
reaper.Main_OnCommand(current_preview, 0) -- Stop previous preview
end
-- Start new preview
current_preview = reaper.Main_OnCommand(reaper.NamedCommandLookup('_S&M_PREVIEW'), 0)
reaper.CSurf_SetPlayState(1) -- Play
reaper.SetMediaTrackInfo_Value(current_preview, "D_VOL", volume)
end
-- Recursive function to create directory tree and list files
function create_directory_tree(path, level)
local folder_idx = 0
while true do
local folder = reaper.EnumerateSubdirectories(path, folder_idx)
if not folder then break end
local full_folder_path = path .. "\\" .. folder
if directory_contains_matching_files(full_folder_path, search_query) then
if ImGui.TreeNode(folder) then
create_directory_tree(full_folder_path, level + 1)
ImGui.TreePop()
end
end
folder_idx = folder_idx + 1
end
local file_idx = 0
while true do
local file = reaper.EnumerateFiles(path, file_idx)
if not file then break end
if string.match(file:lower(), search_query:lower()) then
if ImGui.Selectable(file) then
handle_playback(path .. "\\" .. file, 1.0) -- Default volume is 1.0
end
end
file_idx = file_idx + 1
end
end
-- Function to render the media explorer
function render_media_explorer(ctx)
local changed, text = reaper.ImGui_InputText(ctx, "Search", search_query, 256)
if changed then
search_query = text
debounce_timer = reaper.time_precise() + DEBOUNCE_DELAY
end
if reaper.time_precise() > debounce_timer then
if search_query ~= last_search_query then
search_cache = {} -- Clear cache when the search query changes
last_search_query = search_query
end
-- Call the function to create the directory tree and search for files
create_directory_tree(BASE_DIRECTORY, 0)
end
end
-- Main loop to render the entire interface
function main()
reaper.ImGui_PushFont(ctx, base_font)
if reaper.ImGui_Begin(ctx, "Media Explorer", true) then
render_media_explorer(ctx)
reaper.ImGui_End(ctx)
end
reaper.ImGui_PopFont(ctx)
-- Repeat the main loop
reaper.defer(main)
end
-- Initialize ImGui
function init()
ctx = reaper.ImGui_CreateContext("Media Explorer")
base_font = reaper.ImGui_CreateFont('sans-serif', 16)
reaper.ImGui_AttachFont(ctx, base_font)
reaper.defer(main)
end
init()
Explanation:
-
Initialization:
- Created a context (
ctx) for ImGui.
- Initialized fonts.
-
Search Functionality:
- Added a debounce mechanism for the search input to prevent frequent search operations.
search_query is updated with the debounce delay.
- When the search query is changed, the cache is cleared to ensure accurate results.
-
Directory Tree and File Listing:
create_directory_tree recursively lists directories and files, checking each file against the search query.
- If the directory contains matching files, it shows and traverses it.
-
Media Playback:
- Implemented
handle_playback to play selected media items.
-
Main Loop:
- Rendered the main interface, handling the search bar and directory tree.
- Used
reaper.defer(main) to keep the interface responsive.
With this structure, you can build upon and customize the media explorer to suit your specific needs, integrating more functionalities for enhanced user experience.