From cf17b6875cd0809598c694b69b7b2e83baae5aff Mon Sep 17 00:00:00 2001 From: Noel Welsh Date: Thu, 20 Apr 2023 09:36:36 +0100 Subject: [PATCH] Working Swing example --- build.sbt | 2 +- .../scala/gooey/examples/BasicSwing.scala | 55 +++++++++++++++++++ .../src/main/scala/gooey/swing/Algebra.scala | 12 ++-- 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 examples/jvm/src/main/scala/gooey/examples/BasicSwing.scala diff --git a/build.sbt b/build.sbt index fc4a86f..e6e4b93 100644 --- a/build.sbt +++ b/build.sbt @@ -174,7 +174,7 @@ lazy val examples = crossProject(JSPlatform, JVMPlatform) ) .jvmConfigure( _.settings(mimaPreviousArtifacts := Set.empty) - .dependsOn(core.jvm) + .dependsOn(core.jvm, swing) ) .jsConfigure( _.settings(mimaPreviousArtifacts := Set.empty) diff --git a/examples/jvm/src/main/scala/gooey/examples/BasicSwing.scala b/examples/jvm/src/main/scala/gooey/examples/BasicSwing.scala new file mode 100644 index 0000000..f7a9b27 --- /dev/null +++ b/examples/jvm/src/main/scala/gooey/examples/BasicSwing.scala @@ -0,0 +1,55 @@ +/* + * Copyright 2023 Creative Scala + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gooey.examples + +import fs2.concurrent.Signal +import gooey.component.* +import gooey.component.style.* +import gooey.swing.{_, given} +import gooey.syntax.all.* +import net.bulbyvr.swing.io.IOSwingApp +import net.bulbyvr.swing.io.all.{_, given} + +object BasicSwing extends IOSwingApp { + def render = + Checkbox.empty + .withLabel("Is this awesome?") + .and( + Textbox.empty + .withLabel( + "Describe, in your own words, the amount of awesomeness" + ) + .withStyle(TextboxStyle.SingleLine) + ) + .create + .flatMap { case (elt, signal) => + window( + title := "Gooey", + box( + elt, + label( + text <-- signal.map((a, _) => + s"Awesomeness ${if a then "is over 9000" else "needs improving"}" + ) + ), + label( + text <-- signal.map((_, r) => s"Reasons given are $r") + ) + ) + ) + } +} diff --git a/swing/src/main/scala/gooey/swing/Algebra.scala b/swing/src/main/scala/gooey/swing/Algebra.scala index 62f4b6f..33484b3 100644 --- a/swing/src/main/scala/gooey/swing/Algebra.scala +++ b/swing/src/main/scala/gooey/swing/Algebra.scala @@ -31,10 +31,6 @@ import gooey.component.style.* import net.bulbyvr.swing.io.all.{_, given} import net.bulbyvr.swing.io.wrapper.* -import javax.swing.* -import javax.swing.event.DocumentEvent -import javax.swing.event.DocumentListener - given Algebra: gooey.Algebra with And.Algebra with Checkbox.Algebra @@ -53,7 +49,7 @@ given Algebra: gooey.Algebra } def makeLabel(theLabel: Option[String]): Resource[IO, Component[IO]] = - theLabel.fold(Label[IO]("")) { l => Label[IO](l) } + theLabel.fold(label(text := "")) { l => label(text := l) } def and[A, B](f: UI[A], s: UI[B]): UI[(A, B)] = { for { @@ -61,7 +57,7 @@ given Algebra: gooey.Algebra snd <- s (c1, s1) = fst (c2, s2) = snd - c <- flow(c1, c2) + c <- box(c1, c2) } yield (c, (s1, s2).tupled) } @@ -71,8 +67,8 @@ given Algebra: gooey.Algebra makeComponent( makeLabel(label), net.bulbyvr.swing.io.all.checkbox.withSelf { self => - onValueChange --> { - _.foreach(_ => self.enabled.get.flatMap(output.set)) + onBtnClick --> { + _.evalMap(_ => self.selected.get).foreach(output.set) } } )