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.

328 lines
9.4KB

  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. //==============================================================================
  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 && parentContentsList != nullptr)
  67. {
  68. auto l = new DirectoryContentsList (parentContentsList->getFilter(), thread);
  69. l->setDirectory (file,
  70. parentContentsList->isFindingDirectories(),
  71. parentContentsList->isFindingFiles());
  72. setSubContentsList (l, true);
  73. }
  74. changeListenerCallback (nullptr);
  75. }
  76. }
  77. }
  78. void removeSubContentsList()
  79. {
  80. if (subContentsList != nullptr)
  81. {
  82. subContentsList->removeChangeListener (this);
  83. subContentsList.reset();
  84. }
  85. }
  86. void setSubContentsList (DirectoryContentsList* newList, const bool canDeleteList)
  87. {
  88. removeSubContentsList();
  89. subContentsList = OptionalScopedPointer<DirectoryContentsList> (newList, canDeleteList);
  90. newList->addChangeListener (this);
  91. }
  92. bool selectFile (const File& target)
  93. {
  94. if (file == target)
  95. {
  96. setSelected (true, true);
  97. return true;
  98. }
  99. if (target.isAChildOf (file))
  100. {
  101. setOpen (true);
  102. for (int maxRetries = 500; --maxRetries > 0;)
  103. {
  104. for (int i = 0; i < getNumSubItems(); ++i)
  105. if (auto* f = dynamic_cast<FileListTreeItem*> (getSubItem (i)))
  106. if (f->selectFile (target))
  107. return true;
  108. // if we've just opened and the contents are still loading, wait for it..
  109. if (subContentsList != nullptr && subContentsList->isStillLoading())
  110. {
  111. Thread::sleep (10);
  112. rebuildItemsFromContentList();
  113. }
  114. else
  115. {
  116. break;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. void changeListenerCallback (ChangeBroadcaster*) override
  123. {
  124. rebuildItemsFromContentList();
  125. }
  126. void rebuildItemsFromContentList()
  127. {
  128. clearSubItems();
  129. if (isOpen() && subContentsList != nullptr)
  130. {
  131. for (int i = 0; i < subContentsList->getNumFiles(); ++i)
  132. addSubItem (new FileListTreeItem (owner, subContentsList, i,
  133. subContentsList->getFile(i), thread));
  134. }
  135. }
  136. void paintItem (Graphics& g, int width, int height) override
  137. {
  138. ScopedLock lock (iconUpdate);
  139. if (file != File())
  140. {
  141. updateIcon (true);
  142. if (icon.isNull())
  143. thread.addTimeSliceClient (this);
  144. }
  145. owner.getLookAndFeel().drawFileBrowserRow (g, width, height,
  146. file, file.getFileName(),
  147. &icon, fileSize, modTime,
  148. isDirectory, isSelected(),
  149. indexInContentsList, owner);
  150. }
  151. String getAccessibilityName() override
  152. {
  153. return file.getFileName();
  154. }
  155. void itemClicked (const MouseEvent& e) override
  156. {
  157. owner.sendMouseClickMessage (file, e);
  158. }
  159. void itemDoubleClicked (const MouseEvent& e) override
  160. {
  161. TreeViewItem::itemDoubleClicked (e);
  162. owner.sendDoubleClickMessage (file);
  163. }
  164. void itemSelectionChanged (bool) override
  165. {
  166. owner.sendSelectionChangeMessage();
  167. }
  168. int useTimeSlice() override
  169. {
  170. updateIcon (false);
  171. return -1;
  172. }
  173. void handleAsyncUpdate() override
  174. {
  175. owner.repaint();
  176. }
  177. const File file;
  178. private:
  179. FileTreeComponent& owner;
  180. DirectoryContentsList* parentContentsList;
  181. int indexInContentsList;
  182. OptionalScopedPointer<DirectoryContentsList> subContentsList;
  183. bool isDirectory;
  184. TimeSliceThread& thread;
  185. CriticalSection iconUpdate;
  186. Image icon;
  187. String fileSize, modTime;
  188. void updateIcon (const bool onlyUpdateIfCached)
  189. {
  190. if (icon.isNull())
  191. {
  192. auto hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
  193. auto im = ImageCache::getFromHashCode (hashCode);
  194. if (im.isNull() && ! onlyUpdateIfCached)
  195. {
  196. im = juce_createIconForFile (file);
  197. if (im.isValid())
  198. ImageCache::addImageToCache (im, hashCode);
  199. }
  200. if (im.isValid())
  201. {
  202. {
  203. ScopedLock lock (iconUpdate);
  204. icon = im;
  205. }
  206. triggerAsyncUpdate();
  207. }
  208. }
  209. }
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileListTreeItem)
  211. };
  212. //==============================================================================
  213. FileTreeComponent::FileTreeComponent (DirectoryContentsList& listToShow)
  214. : DirectoryContentsDisplayComponent (listToShow),
  215. itemHeight (22)
  216. {
  217. setRootItemVisible (false);
  218. refresh();
  219. }
  220. FileTreeComponent::~FileTreeComponent()
  221. {
  222. deleteRootItem();
  223. }
  224. void FileTreeComponent::refresh()
  225. {
  226. deleteRootItem();
  227. auto root = new FileListTreeItem (*this, nullptr, 0, directoryContentsList.getDirectory(),
  228. directoryContentsList.getTimeSliceThread());
  229. root->setSubContentsList (&directoryContentsList, false);
  230. setRootItem (root);
  231. }
  232. //==============================================================================
  233. File FileTreeComponent::getSelectedFile (const int index) const
  234. {
  235. if (auto* item = dynamic_cast<const FileListTreeItem*> (getSelectedItem (index)))
  236. return item->file;
  237. return {};
  238. }
  239. void FileTreeComponent::deselectAllFiles()
  240. {
  241. clearSelectedItems();
  242. }
  243. void FileTreeComponent::scrollToTop()
  244. {
  245. getViewport()->getVerticalScrollBar().setCurrentRangeStart (0);
  246. }
  247. void FileTreeComponent::setDragAndDropDescription (const String& description)
  248. {
  249. dragAndDropDescription = description;
  250. }
  251. void FileTreeComponent::setSelectedFile (const File& target)
  252. {
  253. if (auto* t = dynamic_cast<FileListTreeItem*> (getRootItem()))
  254. if (! t->selectFile (target))
  255. clearSelectedItems();
  256. }
  257. void FileTreeComponent::setItemHeight (int newHeight)
  258. {
  259. if (itemHeight != newHeight)
  260. {
  261. itemHeight = newHeight;
  262. if (auto* root = getRootItem())
  263. root->treeHasChanged();
  264. }
  265. }
  266. } // namespace juce