Skip to content

Commit

Permalink
Breadcrumb bar tweaks
Browse files Browse the repository at this point in the history
* Fix comments on content provider
* Wire command icons to content provider
* Handle null content of FileSystemView results in the demo

For #8
  • Loading branch information
kirill-grouchnikov committed Jan 11, 2022
1 parent 0bca8d1 commit 9fff4cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
Expand Up @@ -24,30 +24,27 @@ import java.io.InputStream
* Content provider for a breadcrumb bar.
*/
interface BreadcrumbBarContentProvider<T> {
/** Returns the display text for the item, or for the root is null is passed. */
/** Returns the display text for the item, or for the root is `null` is passed. */
fun getDisplayText(item: T?): String

/** Returns the icon for the item, or for the root is null is passed. */
/** Returns the icon for the item, or for the root is `null` is passed. */
fun getIcon(item: T?): Painter? = null

/**
* Returns the choice elements that correspond to the specified path. If the
* path is empty, `null` should be returned. If path is
* `null`, the "root" elements should be returned
* item is `null`, the list of root elements should be returned.
*
* @param path Breadcrumb bar path.
* @return The choice elements that correspond to the specified path
* @param item Breadcrumb bar item.
* @return The choice elements that correspond to the specified item.
*/
suspend fun getPathChoices(item: T?): List<T>

/**
* Returns the leaf elements that correspond to the specified path. If the
* path is empty, `null` should be returned. If path is
* `null`, leaf content of the "root" elements should be returned. Most probably, if
* your root is more than one element, you should be returning null in here.
* Returns the leaf elements that correspond to the specified item. If the specified
* item has no leaf elements, empty list should be returned.
*
* @param path Breadcrumb bar path.
* @return The leaf elements that correspond to the specified path
* @param item Breadcrumb bar item.
* @return The leaf elements that correspond to the specified item.
*/
suspend fun getLeaves(item: T): List<T>

Expand All @@ -59,9 +56,7 @@ interface BreadcrumbBarContentProvider<T> {
* @return Input stream with the leaf content. May be `null` if
* this is not applicable.
*/
suspend fun getLeafContent(leaf: T): InputStream? {
return null
}
suspend fun getLeafContent(leaf: T): InputStream? = null
}

data class BreadcrumbBarPresentationModel(
Expand Down
Expand Up @@ -38,7 +38,6 @@ import org.pushingpixels.aurora.window.AuroraWindow
import org.pushingpixels.aurora.window.AuroraWindowScope
import org.pushingpixels.aurora.window.auroraApplication
import java.io.File
import java.io.InputStream
import javax.swing.JFileChooser
import javax.swing.filechooser.FileSystemView

Expand Down Expand Up @@ -79,10 +78,11 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror
}

override suspend fun getPathChoices(item: File?): List<File> {
// If our path is empty, get the file system roots. Otherwise, get all files under
// the last file in the path.
// If our item is null, get the file system roots. Otherwise, get all files under
// this file item.
val candidates =
if (item == null) fileSystemView.roots else item.listFiles()
(if (item == null) fileSystemView.roots else item.listFiles())
?: return emptyList()

// Now filter out hidden ones and non-directories, map the rest to
// what the content provider needs to return, and sort them by display name
Expand All @@ -92,18 +92,15 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror
}

override suspend fun getLeaves(item: File): List<File> {
// Get all files under the last file in the path, filter out hidden ones and
// Get all files under the file item, filter out hidden ones and
// directory ones, map the rest to what the content provider needs to
// return, and sort them by display name
return item.listFiles()
val candidates = item.listFiles() ?: return emptyList()
return candidates
.filterNot { it.isDirectory || fileSystemView.isHiddenFile(it) }
.map { it }
.sortedBy { getDisplayText(it).lowercase() }
}

override suspend fun getLeafContent(leaf: File): InputStream? {
return null
}
}

val contentModel = remember { mutableStateListOf<Command>() }
Expand All @@ -118,7 +115,7 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror

return Command(
text = this.getDisplayText(item),
icon = null,
icon = this.getIcon(item),
action = {
// This is called when the path item is clicked
while (contentModel.size > level) {
Expand All @@ -130,7 +127,7 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror
group = CommandGroup(title = null,
commands = pathChoices.map { pathChoice ->
Command(text = this.getDisplayText(pathChoice),
icon = null,
icon = this.getIcon(pathChoice),
action = {
// This is called when a dropdown item is clicked
while (contentModel.size > level) {
Expand Down

0 comments on commit 9fff4cc

Please sign in to comment.