Skip to content

Commit

Permalink
Working Swing example
Browse files Browse the repository at this point in the history
  • Loading branch information
noelwelsh committed Apr 20, 2023
1 parent 16c9f0f commit cf17b68
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions examples/jvm/src/main/scala/gooey/examples/BasicSwing.scala
Original file line number Diff line number Diff line change
@@ -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")
)
)
)
}
}
12 changes: 4 additions & 8 deletions swing/src/main/scala/gooey/swing/Algebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,15 +49,15 @@ 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 {
fst <- f
snd <- s
(c1, s1) = fst
(c2, s2) = snd
c <- flow(c1, c2)
c <- box(c1, c2)
} yield (c, (s1, s2).tupled)
}

Expand All @@ -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)
}
}
)
Expand Down

0 comments on commit cf17b68

Please sign in to comment.