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.

287 lines
8.2KB

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