Audio plugin host https://kx.studio/carla
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.

325 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 this technical preview, this file is not subject to commercial licensing.
  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. //==============================================================================
  16. class FileListTreeItem : public TreeViewItem,
  17. private TimeSliceClient,
  18. private AsyncUpdater,
  19. private ChangeListener
  20. {
  21. public:
  22. FileListTreeItem (FileTreeComponent& treeComp,
  23. DirectoryContentsList* parentContents,
  24. int indexInContents,
  25. const File& f,
  26. TimeSliceThread& t)
  27. : file (f),
  28. owner (treeComp),
  29. parentContentsList (parentContents),
  30. indexInContentsList (indexInContents),
  31. subContentsList (nullptr, false),
  32. thread (t)
  33. {
  34. DirectoryContentsList::FileInfo fileInfo;
  35. if (parentContents != nullptr
  36. && parentContents->getFileInfo (indexInContents, fileInfo))
  37. {
  38. fileSize = File::descriptionOfSizeInBytes (fileInfo.fileSize);
  39. modTime = fileInfo.modificationTime.formatted ("%d %b '%y %H:%M");
  40. isDirectory = fileInfo.isDirectory;
  41. }
  42. else
  43. {
  44. isDirectory = true;
  45. }
  46. }
  47. ~FileListTreeItem() override
  48. {
  49. thread.removeTimeSliceClient (this);
  50. clearSubItems();
  51. removeSubContentsList();
  52. }
  53. //==============================================================================
  54. bool mightContainSubItems() override { return isDirectory; }
  55. String getUniqueName() const override { return file.getFullPathName(); }
  56. int getItemHeight() const override { return owner.getItemHeight(); }
  57. var getDragSourceDescription() override { return owner.getDragAndDropDescription(); }
  58. void itemOpennessChanged (bool isNowOpen) override
  59. {
  60. if (isNowOpen)
  61. {
  62. clearSubItems();
  63. isDirectory = file.isDirectory();
  64. if (isDirectory)
  65. {
  66. if (subContentsList == nullptr)
  67. {
  68. jassert (parentContentsList != nullptr);
  69. auto l = new DirectoryContentsList (parentContentsList->getFilter(), thread);
  70. l->setDirectory (file,
  71. parentContentsList->isFindingDirectories(),
  72. parentContentsList->isFindingFiles());
  73. setSubContentsList (l, true);
  74. }
  75. changeListenerCallback (nullptr);
  76. }
  77. }
  78. }
  79. void removeSubContentsList()
  80. {
  81. if (subContentsList != nullptr)
  82. {
  83. subContentsList->removeChangeListener (this);
  84. subContentsList.reset();
  85. }
  86. }
  87. void setSubContentsList (DirectoryContentsList* newList, const bool canDeleteList)
  88. {
  89. removeSubContentsList();
  90. subContentsList = OptionalScopedPointer<DirectoryContentsList> (newList, canDeleteList);
  91. newList->addChangeListener (this);
  92. }
  93. bool selectFile (const File& target)
  94. {
  95. if (file == target)
  96. {
  97. setSelected (true, true);
  98. return true;
  99. }
  100. if (target.isAChildOf (file))
  101. {
  102. setOpen (true);
  103. for (int maxRetries = 500; --maxRetries > 0;)
  104. {
  105. for (int i = 0; i < getNumSubItems(); ++i)
  106. if (auto* f = dynamic_cast<FileListTreeItem*> (getSubItem (i)))
  107. if (f->selectFile (target))
  108. return true;
  109. // if we've just opened and the contents are still loading, wait for it..
  110. if (subContentsList != nullptr && subContentsList->isStillLoading())
  111. {
  112. Thread::sleep (10);
  113. rebuildItemsFromContentList();
  114. }
  115. else
  116. {
  117. break;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. void changeListenerCallback (ChangeBroadcaster*) override
  124. {
  125. rebuildItemsFromContentList();
  126. }
  127. void rebuildItemsFromContentList()
  128. {
  129. clearSubItems();
  130. if (isOpen() && subContentsList != nullptr)
  131. {
  132. for (int i = 0; i < subContentsList->getNumFiles(); ++i)
  133. addSubItem (new FileListTreeItem (owner, subContentsList, i,
  134. subContentsList->getFile(i), thread));
  135. }
  136. }
  137. void paintItem (Graphics& g, int width, int height) override
  138. {
  139. ScopedLock lock (iconUpdate);
  140. if (file != File())
  141. {
  142. updateIcon (true);
  143. if (icon.isNull())
  144. thread.addTimeSliceClient (this);
  145. }
  146. owner.getLookAndFeel().drawFileBrowserRow (g, width, height,
  147. file, file.getFileName(),
  148. &icon, fileSize, modTime,
  149. isDirectory, isSelected(),
  150. indexInContentsList, owner);
  151. }
  152. void itemClicked (const MouseEvent& e) override
  153. {
  154. owner.sendMouseClickMessage (file, e);
  155. }
  156. void itemDoubleClicked (const MouseEvent& e) override
  157. {
  158. TreeViewItem::itemDoubleClicked (e);
  159. owner.sendDoubleClickMessage (file);
  160. }
  161. void itemSelectionChanged (bool) override
  162. {
  163. owner.sendSelectionChangeMessage();
  164. }
  165. int useTimeSlice() override
  166. {
  167. updateIcon (false);
  168. return -1;
  169. }
  170. void handleAsyncUpdate() override
  171. {
  172. owner.repaint();
  173. }
  174. const File file;
  175. private:
  176. FileTreeComponent& owner;
  177. DirectoryContentsList* parentContentsList;
  178. int indexInContentsList;
  179. OptionalScopedPointer<DirectoryContentsList> subContentsList;
  180. bool isDirectory;
  181. TimeSliceThread& thread;
  182. CriticalSection iconUpdate;
  183. Image icon;
  184. String fileSize, modTime;
  185. void updateIcon (const bool onlyUpdateIfCached)
  186. {
  187. if (icon.isNull())
  188. {
  189. auto hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
  190. auto im = ImageCache::getFromHashCode (hashCode);
  191. if (im.isNull() && ! onlyUpdateIfCached)
  192. {
  193. im = juce_createIconForFile (file);
  194. if (im.isValid())
  195. ImageCache::addImageToCache (im, hashCode);
  196. }
  197. if (im.isValid())
  198. {
  199. {
  200. ScopedLock lock (iconUpdate);
  201. icon = im;
  202. }
  203. triggerAsyncUpdate();
  204. }
  205. }
  206. }
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileListTreeItem)
  208. };
  209. //==============================================================================
  210. FileTreeComponent::FileTreeComponent (DirectoryContentsList& listToShow)
  211. : DirectoryContentsDisplayComponent (listToShow),
  212. itemHeight (22)
  213. {
  214. setRootItemVisible (false);
  215. refresh();
  216. }
  217. FileTreeComponent::~FileTreeComponent()
  218. {
  219. deleteRootItem();
  220. }
  221. void FileTreeComponent::refresh()
  222. {
  223. deleteRootItem();
  224. auto root = new FileListTreeItem (*this, nullptr, 0, directoryContentsList.getDirectory(),
  225. directoryContentsList.getTimeSliceThread());
  226. root->setSubContentsList (&directoryContentsList, false);
  227. setRootItem (root);
  228. }
  229. //==============================================================================
  230. File FileTreeComponent::getSelectedFile (const int index) const
  231. {
  232. if (auto* item = dynamic_cast<const FileListTreeItem*> (getSelectedItem (index)))
  233. return item->file;
  234. return {};
  235. }
  236. void FileTreeComponent::deselectAllFiles()
  237. {
  238. clearSelectedItems();
  239. }
  240. void FileTreeComponent::scrollToTop()
  241. {
  242. getViewport()->getVerticalScrollBar().setCurrentRangeStart (0);
  243. }
  244. void FileTreeComponent::setDragAndDropDescription (const String& description)
  245. {
  246. dragAndDropDescription = description;
  247. }
  248. void FileTreeComponent::setSelectedFile (const File& target)
  249. {
  250. if (auto* t = dynamic_cast<FileListTreeItem*> (getRootItem()))
  251. if (! t->selectFile (target))
  252. clearSelectedItems();
  253. }
  254. void FileTreeComponent::setItemHeight (int newHeight)
  255. {
  256. if (itemHeight != newHeight)
  257. {
  258. itemHeight = newHeight;
  259. if (auto* root = getRootItem())
  260. root->treeHasChanged();
  261. }
  262. }
  263. } // namespace juce