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.

121 lines
4.9 KiB

9 years ago
9 years ago
9 years ago
  1. # Sarah’s ScalaFX Utilities #
  2. This project contains helper functions, utilities and convenience functions
  3. for working with JavaFX and ScalaFX in Scala.
  4. ScalaFX does a tremendous job at making JavaFX more usable from Scala, but
  5. it doesn't go as far as it could in facilitating functional and reactive
  6. programming. This project is an attempt to add additional facilities that
  7. further bridge the beautiful paradigms of Scala with the powerful UI
  8. functionality offered JavaFX.
  9. In particular, here are some key features:
  10. * Monadic and applicative interfaces on top of `Observable` make it easy to
  11. build up computations.
  12. * Converters that allow you to use a `Future` or Akka `Stream` as an
  13. `Observable`.
  14. This code is offered as is with no guarantees. You are free to use it if you
  15. find it useful, but this is not part of any production project and it may have
  16. serious bugs. These APIs may also change at any time, and I make no guarantees
  17. that the project will be maintained at all. I welcome any bug reports and I
  18. will be happy to merge high-quality pull requests if you find a bug.
  19. ## Installation ##
  20. To use ScalaFX, add the following to your SBT build:
  21. libraryDependencies += "org.gerweck.scala" %% "scalafx-utils" % "0.14.3"
  22. This currently supports both Scala 2.11 and 2.12. Scala 2.11 will remain a
  23. first-class citizen until Scala 2.13 is released unless it would require
  24. substantial forking.
  25. ## Usage ##
  26. The primary use of this library is to provide a number of implicit conversions
  27. and instances, which are all brought into scope with this import:
  28. import org.gerweck.scalafx.util._
  29. If you use [Cats](https://typelevel.org/cats/), this makes ScalaFX observables
  30. instances of `Functor`, `Applicative` and `Monad`. It also provides some
  31. simple extension methods along these lines.
  32. ### Functional Transformations ###
  33. Note that the output of a functional transformation is always a
  34. `ReadOnlyObjectProperty[A]`, even if there exists a more specific result type
  35. like `ReadOnlyIntegerProperty` that would work. (The types used by ScalaFX are
  36. fairly complicated, and no real harm is done by using an `ObjectProperty` in
  37. all cases.)
  38. #### Map ####
  39. To facilitate functional programming, the standard `map` function allows you
  40. to transform an observable value using a pure function.
  41. Note that, for performance reasons, these functionally defined observables do
  42. not trigger an update if an input or output value is changed to one that is
  43. identical as defined by `equals`.
  44. import scalafx.beans.value._
  45. import scalafx.scene.control._
  46. import org.gerweck.scalafx.util._
  47. val textBox = new TextField { /* ... */ }
  48. val boxText: ObservableValue[String, String] = textBox.text
  49. /* Construct a new observable derived from the underlying one using `map` */
  50. val characterCount: ReadOnlyObjectProperty[String] = textBox.text map (_.size)
  51. #### Multiple Function Inputs ####
  52. If your function depends on several observable values, you can use the
  53. applicative behavior provided by the library. The Cats applicative
  54. functionality is all available, but there is a more convenient mechanism for
  55. the most common use case where you want to operate on a tuple.
  56. import scalafx.beans.property._
  57. import org.gerweck.scalafx.util._
  58. val startedDownloads = IntegerProperty(0)
  59. val finishedDownloads = IntegerProperty(0)
  60. val runningDownloads: ReadOnlyObjectProperty[Int] =
  61. (startedDownloads, finishedDownloads).observe map {
  62. case (st, fi) => st - fi
  63. }
  64. This `observe` extension method is available on tuples of any arity and
  65. efficiently processes updates from any of its dependent values.
  66. #### Monadically Chained Observables ####
  67. In addition to the behavior of an applicative functor, this library also
  68. provides the ability to act like a monadic functor by providing `flatMap` and
  69. `flatten`. *Where possible use the applicative syntax defined above rather
  70. than a chain of `flatMap` applications: the applicative format performs much
  71. better.*
  72. Here is an example of a model where you might have a dialog box or window.
  73. In this window, you could have a list selection where you choose from one of
  74. many transformation types. Once you've selected a transformation type, it will
  75. display a configuration panel that you can use to control the details of that
  76. transformation.
  77. import scalafx.beans.property._
  78. import org.gerweck.scalafx.util._
  79. /** An object that has a config dialog that produces a function */
  80. trait ConfigurableIntFunction {
  81. val typeName: String
  82. val configPanel: scalafx.scene.layout.Pane
  83. val currentFunction: ReadOnlyObjectProperty[Int => Int]
  84. }
  85. val selectedFunctionType: ObjectProperty[ConfigurableIntFunction] = ???
  86. val selectedFunction = selectedFunctionType flatMap (_.currentFunction)
  87. val inputInt = IntegerProperty(0)
  88. val outputInt =
  89. (selectedFunction, inputInt).observe map {
  90. case (sf, ii) => sf(ii)
  91. }