The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. Image juce_createIconForFile (const File& file);
  16. //==============================================================================
  17. FileListComponent::FileListComponent (DirectoryContentsList& listToShow)
  18. : ListBox ({}, nullptr),
  19. DirectoryContentsDisplayComponent (listToShow),
  20. lastDirectory (listToShow.getDirectory())
  21. {
  22. setTitle ("Files");
  23. setModel (this);
  24. directoryContentsList.addChangeListener (this);
  25. }
  26. FileListComponent::~FileListComponent()
  27. {
  28. directoryContentsList.removeChangeListener (this);
  29. }
  30. int FileListComponent::getNumSelectedFiles() const
  31. {
  32. return getNumSelectedRows();
  33. }
  34. File FileListComponent::getSelectedFile (int index) const
  35. {
  36. return directoryContentsList.getFile (getSelectedRow (index));
  37. }
  38. void FileListComponent::deselectAllFiles()
  39. {
  40. deselectAllRows();
  41. }
  42. void FileListComponent::scrollToTop()
  43. {
  44. getVerticalScrollBar().setCurrentRangeStart (0);
  45. }
  46. void FileListComponent::setSelectedFile (const File& f)
  47. {
  48. for (int i = directoryContentsList.getNumFiles(); --i >= 0;)
  49. {
  50. if (directoryContentsList.getFile (i) == f)
  51. {
  52. fileWaitingToBeSelected = File();
  53. selectRow (i);
  54. return;
  55. }
  56. }
  57. deselectAllRows();
  58. fileWaitingToBeSelected = f;
  59. }
  60. //==============================================================================
  61. void FileListComponent::changeListenerCallback (ChangeBroadcaster*)
  62. {
  63. updateContent();
  64. if (lastDirectory != directoryContentsList.getDirectory())
  65. {
  66. fileWaitingToBeSelected = File();
  67. lastDirectory = directoryContentsList.getDirectory();
  68. deselectAllRows();
  69. }
  70. if (fileWaitingToBeSelected != File())
  71. setSelectedFile (fileWaitingToBeSelected);
  72. }
  73. //==============================================================================
  74. class FileListComponent::ItemComponent : public Component,
  75. private TimeSliceClient,
  76. private AsyncUpdater
  77. {
  78. public:
  79. ItemComponent (FileListComponent& fc, TimeSliceThread& t)
  80. : owner (fc), thread (t)
  81. {
  82. }
  83. ~ItemComponent() override
  84. {
  85. thread.removeTimeSliceClient (this);
  86. }
  87. //==============================================================================
  88. void paint (Graphics& g) override
  89. {
  90. getLookAndFeel().drawFileBrowserRow (g, getWidth(), getHeight(),
  91. file, file.getFileName(),
  92. &icon, fileSize, modTime,
  93. isDirectory, highlighted,
  94. index, owner);
  95. }
  96. void mouseDown (const MouseEvent& e) override
  97. {
  98. owner.selectRowsBasedOnModifierKeys (index, e.mods, true);
  99. owner.sendMouseClickMessage (file, e);
  100. }
  101. void mouseDoubleClick (const MouseEvent&) override
  102. {
  103. owner.sendDoubleClickMessage (file);
  104. }
  105. void update (const File& root, const DirectoryContentsList::FileInfo* fileInfo,
  106. int newIndex, bool nowHighlighted)
  107. {
  108. thread.removeTimeSliceClient (this);
  109. if (nowHighlighted != highlighted || newIndex != index)
  110. {
  111. index = newIndex;
  112. highlighted = nowHighlighted;
  113. repaint();
  114. }
  115. File newFile;
  116. String newFileSize, newModTime;
  117. if (fileInfo != nullptr)
  118. {
  119. newFile = root.getChildFile (fileInfo->filename);
  120. newFileSize = File::descriptionOfSizeInBytes (fileInfo->fileSize);
  121. newModTime = fileInfo->modificationTime.formatted ("%d %b '%y %H:%M");
  122. }
  123. if (newFile != file
  124. || fileSize != newFileSize
  125. || modTime != newModTime)
  126. {
  127. file = newFile;
  128. fileSize = newFileSize;
  129. modTime = newModTime;
  130. icon = Image();
  131. isDirectory = fileInfo != nullptr && fileInfo->isDirectory;
  132. repaint();
  133. }
  134. if (file != File() && icon.isNull() && ! isDirectory)
  135. {
  136. updateIcon (true);
  137. if (! icon.isValid())
  138. thread.addTimeSliceClient (this);
  139. }
  140. }
  141. int useTimeSlice() override
  142. {
  143. updateIcon (false);
  144. return -1;
  145. }
  146. void handleAsyncUpdate() override
  147. {
  148. repaint();
  149. }
  150. private:
  151. //==============================================================================
  152. FileListComponent& owner;
  153. TimeSliceThread& thread;
  154. File file;
  155. String fileSize, modTime;
  156. Image icon;
  157. int index = 0;
  158. bool highlighted = false, isDirectory = false;
  159. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  160. {
  161. return createIgnoredAccessibilityHandler (*this);
  162. }
  163. void updateIcon (const bool onlyUpdateIfCached)
  164. {
  165. if (icon.isNull())
  166. {
  167. auto hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
  168. auto im = ImageCache::getFromHashCode (hashCode);
  169. if (im.isNull() && ! onlyUpdateIfCached)
  170. {
  171. im = juce_createIconForFile (file);
  172. if (im.isValid())
  173. ImageCache::addImageToCache (im, hashCode);
  174. }
  175. if (im.isValid())
  176. {
  177. icon = im;
  178. triggerAsyncUpdate();
  179. }
  180. }
  181. }
  182. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
  183. };
  184. //==============================================================================
  185. int FileListComponent::getNumRows()
  186. {
  187. return directoryContentsList.getNumFiles();
  188. }
  189. String FileListComponent::getNameForRow (int rowNumber)
  190. {
  191. return directoryContentsList.getFile (rowNumber).getFileName();
  192. }
  193. void FileListComponent::paintListBoxItem (int, Graphics&, int, int, bool)
  194. {
  195. }
  196. Component* FileListComponent::refreshComponentForRow (int row, bool isSelected, Component* existingComponentToUpdate)
  197. {
  198. jassert (existingComponentToUpdate == nullptr || dynamic_cast<ItemComponent*> (existingComponentToUpdate) != nullptr);
  199. auto comp = static_cast<ItemComponent*> (existingComponentToUpdate);
  200. if (comp == nullptr)
  201. comp = new ItemComponent (*this, directoryContentsList.getTimeSliceThread());
  202. DirectoryContentsList::FileInfo fileInfo;
  203. comp->update (directoryContentsList.getDirectory(),
  204. directoryContentsList.getFileInfo (row, fileInfo) ? &fileInfo : nullptr,
  205. row, isSelected);
  206. return comp;
  207. }
  208. void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
  209. {
  210. sendSelectionChangeMessage();
  211. }
  212. void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
  213. {
  214. }
  215. void FileListComponent::returnKeyPressed (int currentSelectedRow)
  216. {
  217. sendDoubleClickMessage (directoryContentsList.getFile (currentSelectedRow));
  218. }
  219. } // namespace juce