Visitor from the 20th century here, as far as GUI programming is concerned… I’m trying to recreate an old GUI app of mine. The original code is Scala 2.11 with Swing using a naive Presentation Model approach, and it’s way beyond redemption.
Now I’ve been looking into scalafx and I’m somewhat confused. It looks like the framework wants me to use properties and bindings, OTOH the API is very javaesque, for obvious reasons. Right now I feel really tempted to throw some unlawful cats code plus some extension methods on top…
given objectBindingFunctor: Functor[ObjectBinding] with
override def map[A, B](fa: ObjectBinding[A])(f: A => B): ObjectBinding[B] =
Bindings.createObjectBinding[B](() => f(fa.value), fa)
// Applicative accordingly, Monad maybe later
extension[A, J] (prop: Property[A, J])
def binding[B](f: A => B): ObjectBinding[B] =
Bindings.createObjectBinding[B](() => f(prop.value), prop)
extension (bnd: ObjectBinding[Boolean])
def asBooleanBinding: BooleanBinding =
Bindings.createBooleanBinding(() => bnd.value, bnd)
…so I could do stuff like this:
val hostBinding =
hostText.text.binding { h =>
Option.when(h.nonEmpty)(h) >>= Host.parseOption
}
val userBinding =
userText.text.binding(u => Option.when(u.nonEmpty)(u))
val prefsBinding =
(hostBinding, userBinding).mapN { (hostOpt, userOpt) =>
(hostOpt, userOpt).mapN(UserPrefs(_))
}
acceptButton.disable <== prefsBinding.map(_.isEmpty).asBooleanBinding
// return prefsBinding.value via Dialog#resultConverter
Questions:
- Am I missing some alternative framework/library for Scala 3 cross-platform apps (desktop only)?
- Am I holding it wrong, i.e. am I missing some API for nicely chaining properties, either in scalafx itself or from 3rd party libs on top?
- Am I going to shoot my own foot continuing the approach outlined above? How? Any better suggestions?
- What’s a good way to get this property/binding forest properly organized, especially for testing? Naively I’d start by encapsulating the property/binding “logic” in some presentation model style wrapper that can be tested standalone. But then again this may be a 20th century thought.
Any pointers and constructive criticism appreciated - thanks in advance!