forked from edvin/tornadofx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspace.kt
480 lines (422 loc) · 17.4 KB
/
Workspace.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package tornadofx
import javafx.beans.property.ObjectProperty
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.value.ChangeListener
import javafx.collections.FXCollections
import javafx.geometry.Pos
import javafx.geometry.Side
import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.control.Button
import javafx.scene.control.TabPane
import javafx.scene.control.ToolBar
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyCodeCombination
import javafx.scene.input.KeyCombination
import javafx.scene.layout.BorderPane
import javafx.scene.layout.HBox
import javafx.scene.layout.StackPane
import tornadofx.Workspace.NavigationMode.Stack
import tornadofx.Workspace.NavigationMode.Tabs
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf
class HeadingContainer : HBox() {
init {
addClass("heading-container")
}
}
class WorkspaceArea : BorderPane() {
internal var dynamicComponentMode: Boolean = false
internal val dynamicComponents = FXCollections.observableArrayList<Node>()
var header: ToolBar by singleAssign()
init {
addClass("workspace")
}
}
open class Workspace(title: String = "Workspace", navigationMode: NavigationMode = Stack) : View(title) {
var refreshButton: Button by singleAssign()
var saveButton: Button by singleAssign()
var createButton: Button by singleAssign()
var deleteButton: Button by singleAssign()
var backButton: Button by singleAssign()
var forwardButton: Button by singleAssign()
enum class NavigationMode { Stack, Tabs }
val navigationModeProperty: ObjectProperty<NavigationMode> = SimpleObjectProperty(navigationMode)
var navigationMode by navigationModeProperty
val viewStack = FXCollections.observableArrayList<UIComponent>()
val maxViewStackDepthProperty = SimpleIntegerProperty(DefaultViewStackDepth)
var maxViewStackDepth by maxViewStackDepthProperty
val headingContainer = HeadingContainer()
val tabContainer = TabPane().addClass("editor-container")
val stackContainer = StackPane().addClass("editor-container")
val contentContainerProperty = SimpleObjectProperty<Parent>(stackContainer)
var contentContainer by contentContainerProperty
val showHeadingLabelProperty = SimpleBooleanProperty(true)
var showHeadingLabel by showHeadingLabelProperty
val dockedComponentProperty: ObjectProperty<UIComponent> = SimpleObjectProperty()
val dockedComponent: UIComponent? get() = dockedComponentProperty.value
private val viewPos = integerBinding(viewStack, dockedComponentProperty) { viewStack.indexOf(dockedComponent) }
val leftDrawer: Drawer
get() = (root.left as? Drawer) ?: Drawer(Side.LEFT, false, false).also {
root.left = it
it.toFront()
}
val rightDrawer: Drawer
get() = (root.right as? Drawer) ?: Drawer(Side.RIGHT, false, false).also {
root.right = it
it.toFront()
}
val bottomDrawer: Drawer
get() = (root.bottom as? Drawer) ?: Drawer(Side.BOTTOM, false, false).also {
root.bottom = it
it.toFront()
}
companion object {
val activeWorkspaces = FXCollections.observableArrayList<Workspace>()
val DefaultViewStackDepth = 10
fun closeAll() {
activeWorkspaces.forEach(Workspace::close)
}
var defaultSavable = true
var defaultDeletable = true
var defaultRefreshable = true
var defaultCloseable = true
var defaultComplete = true
var defaultCreatable = true
init {
importStylesheet("/tornadofx/workspace.css")
}
}
fun disableNavigation() {
viewStack.clear()
maxViewStackDepth = 0
}
private fun registerWorkspaceAccelerators() {
accelerators[KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN)] = {
if (!saveButton.isDisable) onSave()
}
accelerators[KeyCodeCombination(KeyCode.N, KeyCombination.SHORTCUT_DOWN)] = {
if (!createButton.isDisable) onCreate()
}
accelerators[KeyCodeCombination(KeyCode.R, KeyCombination.SHORTCUT_DOWN)] = {
if (!refreshButton.isDisable) onRefresh()
}
accelerators[KeyCombination.valueOf("F5")] = {
if (!refreshButton.isDisable) onRefresh()
}
}
override fun onDock() {
activeWorkspaces += this
}
override fun onUndock() {
activeWorkspaces -= this
}
override val root = WorkspaceArea().apply {
top {
vbox {
header = toolbar {
addClass("header")
// Force the container to retain pos center even when it's resized (hack to make ToolBar behave)
skinProperty().onChange {
(lookup(".container") as? HBox)?.apply {
alignment = Pos.CENTER_LEFT
alignmentProperty().onChange {
if (it != Pos.CENTER_LEFT) alignment = Pos.CENTER_LEFT
}
}
}
button {
addClass("icon-only")
backButton = this
graphic = label { addClass("icon", "back") }
action {
if (dockedComponent?.onNavigateBack() ?: true) {
navigateBack()
}
}
disableProperty().bind(booleanBinding(viewPos, viewStack) { value < 1 })
}
button {
addClass("icon-only")
forwardButton = this
graphic = label { addClass("icon", "forward") }
action {
if (dockedComponent?.onNavigateForward() ?: true) {
navigateForward()
}
}
disableProperty().bind(booleanBinding(viewPos, viewStack) { value == viewStack.size - 1 })
}
button {
addClass("icon-only")
refreshButton = this
isDisable = true
graphic = label {
addClass("icon", "refresh")
}
action {
onRefresh()
}
}
button {
addClass("icon-only")
saveButton = this
isDisable = true
graphic = label { addClass("icon", "save") }
action {
onSave()
}
}
button {
addClass("icon-only")
createButton = this
isDisable = true
graphic = label { addClass("icon", "create") }
action {
onCreate()
}
}
button {
addClass("icon-only")
deleteButton = this
isDisable = true
graphic = label { addClass("icon", "delete") }
action {
onDelete()
}
}
add(headingContainer)
spacer()
}
}
}
}
val header: ToolBar get() = root.header
fun navigateForward(): Boolean {
if (!forwardButton.isDisabled) {
dock(viewStack[viewPos.get() + 1], false)
return true
}
return false
}
fun navigateBack(): Boolean {
if (!backButton.isDisabled) {
dock(viewStack[viewPos.get() - 1], false)
return true
}
return false
}
init {
// @Suppress("LeakingThis")
// if (!scope.hasActiveWorkspace) scope.workspaceInstance = this
navigationModeProperty.addListener { _, ov, nv -> navigationModeChanged(ov, nv) }
tabContainer.tabs.onChange { change ->
while (change.next()) {
if (change.wasRemoved()) {
change.removed.forEach {
if (it == dockedComponent) {
titleProperty.unbind()
refreshButton.disableProperty().unbind()
saveButton.disableProperty().unbind()
createButton.disableProperty().unbind()
deleteButton.disableProperty().unbind()
}
}
}
if (change.wasAdded()) {
change.addedSubList.forEach {
it.content.properties["tornadofx.tab"] = it
}
}
}
}
tabContainer.selectionModel.selectedItemProperty().addListener { observableValue, ov, nv ->
val newCmp = nv?.content?.uiComponent<UIComponent>()
val oldCmp = ov?.content?.uiComponent<UIComponent>()
if (newCmp != null && newCmp != dockedComponent) {
setAsCurrentlyDocked(newCmp)
}
if (oldCmp != newCmp) oldCmp?.callOnUndock()
if (newCmp == null) {
headingContainer.children.clear()
clearDynamicComponents()
dockedComponentProperty.value = null
}
}
dockedComponentProperty.onChange { child ->
if (child != null) {
inDynamicComponentMode {
if (contentContainer == stackContainer) {
tabContainer.tabs.clear()
stackContainer.clear()
stackContainer += child
} else {
stackContainer.clear()
var tab = tabContainer.tabs.find { it.content == child.root }
if (tab == null) {
tabContainer += child
tab = tabContainer.tabs.last()
} else {
child.callOnDock()
}
tabContainer.selectionModel.select(tab)
if (!child.isDocked) child.callOnDock()
}
}
}
}
navigationModeChanged(null, navigationMode)
registerWorkspaceAccelerators()
}
private fun navigationModeChanged(oldMode: NavigationMode?, newMode: NavigationMode?) {
if (oldMode == null || oldMode != newMode) {
contentContainer = if (navigationMode == Stack) stackContainer else tabContainer
root.center = contentContainer
if (contentContainer == stackContainer && tabContainer.tabs.isNotEmpty()) {
tabContainer.tabs.clear()
}
dockedComponent?.also {
dockedComponentProperty.value = null
if (oldMode == Stack && newMode == Tabs) {
val listener = it.rootParentChangeListener
it.root.parentProperty().removeListener(listener)
it.callOnUndock()
dock(it, true)
it.root.parentProperty().addListener(listener)
} else {
dock(it, true)
}
}
}
if (newMode == Stack) {
if (backButton !in root.header.items) {
root.header.items.add(0, backButton)
root.header.items.add(1, forwardButton)
}
} else {
root.header.items -= backButton
root.header.items -= forwardButton
}
}
override fun onSave() {
dockedComponentProperty.value
?.takeIf { it.effectiveSavable.value }
?.onSave()
}
override fun onDelete() {
dockedComponentProperty.value
?.takeIf { it.effectiveDeletable.value }
?.onDelete()
}
override fun onCreate() {
dockedComponentProperty.value
?.takeIf { it.effectiveCreatable.value }
?.onCreate()
}
override fun onRefresh() {
dockedComponentProperty.value
?.takeIf { it.effectiveRefreshable.value }
?.onRefresh()
}
inline fun <reified T : UIComponent> dock(scope: Scope = this@Workspace.scope, params: Map<*, Any?>? = null) = dock(find<T>(scope, params))
inline fun <reified T : UIComponent> dock(scope: Scope = this@Workspace.scope, vararg params: Pair<*, Any?>) { dock<T>(scope, params.toMap()) }
fun dock(child: UIComponent, forward: Boolean = true) {
if (child == dockedComponent) return
// Remove everything after viewpos if moving forward
if (forward) while (viewPos.get() < viewStack.size -1) viewStack.removeAt(viewPos.get() + 1)
val addToStack = contentContainer == stackContainer && maxViewStackDepth > 0 && child !in viewStack
if (addToStack) viewStack += child
setAsCurrentlyDocked(child)
// Ensure max stack size
while (viewStack.size >= maxViewStackDepth && viewStack.isNotEmpty())
viewStack.removeAt(0)
}
private fun setAsCurrentlyDocked(child: UIComponent) {
titleProperty.bind(child.titleProperty)
rebindWorkspaceButtons(child)
headingContainer.children.clear()
headingContainer.label(child.headingProperty) {
graphicProperty().bind(child.iconProperty)
removeWhen(!showHeadingLabelProperty)
}
clearDynamicComponents()
dockedComponentProperty.value = child
if (currentWindow?.isShowing != true && currentWindow?.aboutToBeShown != true)
FX.log.warning("UIComponent $child docked in invisible workspace $workspace")
}
fun rebindWorkspaceButtons(child: UIComponent) {
refreshButton.disableProperty().cleanBind(!child.effectiveRefreshable)
saveButton.disableProperty().cleanBind(!child.effectiveSavable)
createButton.disableProperty().cleanBind(!child.effectiveCreatable)
deleteButton.disableProperty().cleanBind(!child.effectiveDeletable)
}
private fun clearDynamicComponents() {
root.dynamicComponents.forEach(Node::removeFromParent)
root.dynamicComponents.clear()
}
fun inDynamicComponentMode(function: () -> Unit) {
root.dynamicComponentMode = true
try {
function()
} finally {
root.dynamicComponentMode = false
}
}
/**
* Create a new scope and associate it with this Workspace and optionally add one
* or more ScopedInstance instances into the scope. The op block operates on the workspace and is passed the new scope. The following example
* creates a new scope, injects a Customer Model into it and docks the CustomerEditor
* into the Workspace:
*
* <pre>
* workspace.withNewScope(CustomerModel(customer)) { newScope ->
* dock<CustomerEditor>(newScope)
* }
* </pre>
*/
fun withNewScope(vararg setInScope: ScopedInstance, op: Workspace.(Scope) -> Unit) = op(this, Scope(this, *setInScope))
/**
* Create a new scope and associate it with this Workspace and dock the given UIComponent type into
* the scope, passing the given parameters on to the UIComponent and optionally injecting the given Injectables into the new scope.
*/
inline fun <reified T : UIComponent> dockInNewScope(params: Map<*, Any?>, vararg setInScope: ScopedInstance) {
withNewScope(*setInScope) { newScope ->
dock<T>(newScope, params)
}
}
/**
* Create a new scope and associate it with this Workspace and dock the given UIComponent type into
* the scope, optionally injecting the given Injectables into the new scope.
*/
inline fun <reified T : UIComponent> dockInNewScope(vararg setInScope: ScopedInstance) {
withNewScope(*setInScope) { newScope ->
dock<T>(newScope)
}
}
/**
* Create a new scope and associate it with this Workspace and dock the given UIComponent type into
* the scope and optionally injecting the given Injectables into the new scope.
*/
fun <T : UIComponent> dockInNewScope(uiComponent: T, vararg setInScope: ScopedInstance) {
withNewScope(*setInScope) {
dock(uiComponent)
}
}
/**
* Will automatically dock the given [UIComponent] if the [ListMenuItem] is selected.
*/
inline fun <reified T : UIComponent> ListMenuItem.dockOnSelect() {
whenSelected { dock<T>() }
}
}
open class WorkspaceApp(val initiallyDockedView: KClass<out UIComponent>, vararg stylesheet: KClass<out Stylesheet>) : App(Workspace::class, *stylesheet) {
init {
if (initiallyDockedView.isSubclassOf(Workspace::class))
log.warning("WorkspaceApp called with a Workspace as parameter! Change to App($initiallyDockedView::class) instead.")
}
override fun onBeforeShow(view: UIComponent) {
workspace.dock(find(initiallyDockedView))
}
}