Skip to content

Commit

Permalink
Support for "Open All" buttons (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: mohammad <[email protected]>
  • Loading branch information
AlahmadiQ8 and mohammad authored Mar 13, 2023
1 parent 0b21d45 commit 26dbf29
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
73 changes: 52 additions & 21 deletions src/main/kotlin/com/nbk/weyay/weyaydesktopclient/MainController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import javafx.event.EventHandler
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader
import javafx.fxml.Initializable
import javafx.scene.Node
import javafx.scene.control.*
import javafx.scene.layout.*
import javafx.scene.paint.Color
Expand Down Expand Up @@ -70,32 +71,35 @@ class MainController : CoroutineScope, Initializable {
saveButton.isDisable = newTab?.userData == null
saveAsButton.isDisable = newTab?.userData == null
}
NodeFactory(recentFilesContainer.children, RecentFiles.recentItems) { path ->
val pathParsed = Path.of(path)
BorderPane().apply {
left = Hyperlink(pathParsed.fileName.toString()).apply {
setOnAction {
openFile(Path.of(path).toFile())
}

}
right = Hyperlink().apply {
graphic = FontIcon("fas-times").apply { iconColor = Color.gray(0.0, 0.25) }
setOnAction {
RecentFiles.removeRecentFile(path)
NodeFactory(
recentFilesContainer.children,
RecentFiles.recentItems,
::recentFilesHomeScreenBuilder,
listOf(
BorderPane().apply {
left = Hyperlink("Open All").apply {
setOnAction {
RecentFiles.recentItems.forEach { openFile(Path.of(it).toFile()) }
}
}
}
}
}
)
)

NodeFactory(recentFilesMenu.items, RecentFiles.recentItems) { path ->
val pathParsed = Path.of(path)
MenuItem(pathParsed.fileName.toString()).apply {
setOnAction {
openFile(Path.of(path).toFile())
NodeFactory(
recentFilesMenu.items,
RecentFiles.recentItems,
::recentFilesMenuItemBuilder,
listOf(
SeparatorMenuItem(),
MenuItem("Open All").apply {
setOnAction {
RecentFiles.recentItems.forEach { openFile(Path.of(it).toFile()) }
}
}
}
}
)
)

recentFilesMenu.disableProperty().bind(Bindings.isEmpty(RecentFiles.recentItems))
recentFilesLabel.textProperty().bind(
Expand Down Expand Up @@ -280,4 +284,31 @@ class MainController : CoroutineScope, Initializable {
}
return tableData
}

fun recentFilesHomeScreenBuilder(path: String): Node {
val pathParsed = Path.of(path)
return BorderPane().apply {
left = Hyperlink(pathParsed.fileName.toString()).apply {
setOnAction {
openFile(Path.of(path).toFile())
}

}
right = Hyperlink().apply {
graphic = FontIcon("fas-times").apply { iconColor = Color.gray(0.0, 0.25) }
setOnAction {
RecentFiles.removeRecentFile(path)
}
}
}
}

fun recentFilesMenuItemBuilder(path: String): MenuItem {
val pathParsed = Path.of(path)
return MenuItem(pathParsed.fileName.toString()).apply {
setOnAction {
openFile(Path.of(path).toFile())
}
}
}
}
11 changes: 9 additions & 2 deletions src/main/kotlin/com/nbk/weyay/weyaydesktopclient/NodeFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import javafx.collections.ObservableList
class NodeFactory<T, R>(
private val nodes: ObservableList<R>,
private val data: ObservableList<T>,
private val buildNodeFor: (model: T) -> R
private val buildNodeFor: (model: T) -> R,
private val additionalNodes: Collection<R> = emptyList()
) {
init {
data.addListener(ListChangeListener { ch ->
Expand All @@ -26,13 +27,19 @@ class NodeFactory<T, R>(
nodes[index] = node
}
} else if (ch.wasRemoved()) {
nodes.remove(ch.from, ch.from + ch.removedSize)
if (nodes.size > 0) nodes.remove(ch.from, ch.from + ch.removedSize)
if (nodes.size == 1) nodes.clear()
} else if (ch.wasAdded()) {
val initialSize = nodes.size
nodes.addAll(ch.from, buildNodesFor(ch.addedSubList))
if (initialSize == 0) {
nodes.addAll(additionalNodes)
}
}
}
})
rebuildAllNodes()

}

private fun buildNodesFor(dataIn: List<T>): List<R> {
Expand Down

0 comments on commit 26dbf29

Please sign in to comment.