Skip to content

Commit

Permalink
Tweaks to breadcrumb bar content provider
Browse files Browse the repository at this point in the history
Change from interface to abstract class so that it can track its current loading job and cancel it if necessary. For #8
  • Loading branch information
kirill-grouchnikov committed Jan 14, 2022
1 parent f471f81 commit deaec36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
Expand Up @@ -19,7 +19,8 @@ import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.graphics.painter.Painter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.pushingpixels.aurora.theming.BackgroundAppearanceStrategy
import org.pushingpixels.aurora.theming.IconFilterStrategy
Expand All @@ -28,12 +29,14 @@ import java.io.InputStream
/**
* Content provider for a breadcrumb bar.
*/
interface BreadcrumbBarContentProvider<T> {
abstract class BreadcrumbBarContentProvider<T> {
internal var currentJob: Job? = null

/** Returns the display text for the item, or for the root is `null` is passed. */
fun getDisplayText(item: T?): String
abstract fun getDisplayText(item: T?): String

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

/**
* Returns the choice elements that correspond to the specified path. If the
Expand All @@ -42,7 +45,7 @@ interface BreadcrumbBarContentProvider<T> {
* @param item Breadcrumb bar item.
* @return The choice elements that correspond to the specified item.
*/
suspend fun getPathChoices(item: T?): List<T>
abstract suspend fun getPathChoices(item: T?): List<T>

/**
* Returns the leaf elements that correspond to the specified item. If the specified
Expand All @@ -51,7 +54,7 @@ interface BreadcrumbBarContentProvider<T> {
* @param item Breadcrumb bar item.
* @return The leaf elements that correspond to the specified item.
*/
suspend fun getLeaves(item: T): List<T>
abstract suspend fun getLeaves(item: T): List<T>

/**
* Returns the input stream with the leaf content. Some implementations may
Expand All @@ -61,7 +64,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? = null
open suspend fun getLeafContent(leaf: T): InputStream? = null
}

suspend fun <T> BreadcrumbBarContentProvider<T>.getPathCommand(
Expand All @@ -78,11 +81,12 @@ suspend fun <T> BreadcrumbBarContentProvider<T>.getPathCommand(
text = this.getDisplayText(item),
icon = this.getIcon(item),
action = {
currentJob?.cancel()
// This is called when the path item is clicked
while (commands.size > level) {
commands.removeLast()
}
scope.launch {
scope.launch(Dispatchers.Default) {
onItemSelected.invoke(item!!)
}
},
Expand All @@ -96,7 +100,8 @@ suspend fun <T> BreadcrumbBarContentProvider<T>.getPathCommand(
while (commands.size > level) {
commands.removeLast()
}
scope.launch {
currentJob?.cancel()
currentJob = scope.launch(Dispatchers.Default) {
commands.add(
getPathCommand(
scope = scope,
Expand Down Expand Up @@ -124,7 +129,7 @@ fun <T> BreadcrumbBarContentModel(
val scope = rememberCoroutineScope()

LaunchedEffect(null) {
coroutineScope {
scope.launch(Dispatchers.Default) {
// Root content for the breadcrumb bar
commands.add(
contentProvider.getPathCommand(
Expand Down
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.rememberWindowState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.pushingpixels.aurora.component.AuroraBreadcrumbBar
import org.pushingpixels.aurora.component.model.*
Expand Down Expand Up @@ -98,8 +99,8 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror
val scope = rememberCoroutineScope()

val fileSystemView = FileSystemView.getFileSystemView()
val breadcrumbBarContentProvider: BreadcrumbBarContentProvider<File> =
object : BreadcrumbBarContentProvider<File> {
val breadcrumbBarContentProvider =
object: BreadcrumbBarContentProvider<File>() {
override fun getDisplayText(item: File?): String {
if (item == null) {
return ""
Expand Down Expand Up @@ -136,7 +137,7 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror

val commandPanelContentModel = remember { mutableStateOf<CommandPanelContentModel?>(null) }
val onBreadcrumbItemSelected: (File) -> Unit = {
scope.launch {
scope.launch(Dispatchers.Default) {
commandPanelContentModel.value = getCommandPanelContent(breadcrumbBarContentProvider, it)
}
}
Expand Down Expand Up @@ -203,7 +204,7 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror
filePath.add(0, currentFile)
currentFile = fileSystemView.getParentDirectory(currentFile)
}
// Convert to list of commands
// Convert to list of commands and set as content model
breadcrumbBarContentModel.clear()
for ((index, file) in filePath.withIndex()) {
breadcrumbBarContentModel.add(
Expand All @@ -216,8 +217,9 @@ fun AuroraWindowScope.BreadcrumbContent(auroraSkinDefinition: MutableState<Auror
)
)
}
commandPanelContentModel.value =
getCommandPanelContent(breadcrumbBarContentProvider, selected)
// And trigger the item selected callback to populate the
// our command panel
onBreadcrumbItemSelected.invoke(selected)
}
}
}),
Expand Down

0 comments on commit deaec36

Please sign in to comment.