Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions core/shared/src/main/scala/gooey/component/button.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.component

import cats.data.Chain
import gooey.Algebra
import gooey.WritableVar

final case class Button(
label: Option[String],
onClick: () => Unit,
observers: Chain[WritableVar[Unit]]
) extends Component[Button.Algebra, Unit] {
def withLabel(label: String): Button =
this.copy(label = Some(label))

def withoutLabel: Button =
this.copy(label = "Click")

def withOnClick(onClick: () => Unit): Button =
this.copy(onClick = onClick)

def withObserver(writable: WritableVar[String]): Button =
this.copy(observers = writable +: observers)

private[gooey] def build(algebra: Button.Algebra)(
env: algebra.Env
): algebra.UI[Unit] =
algebra.button(label, onClick, observers)(env)
}

object Button {
trait Algebra extends gooey.Algebra {
def button(
label: Option[String],
onClick: () => Unit,
observers: Chain[WritableVar[Unit]]
)(env: Env): UI[Unit]
}

val empty: Button = Button("", () => {}, Chain.empty)
}
3 changes: 3 additions & 0 deletions docs/src/css/creative-scala.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ p {
a {
@apply hover:underline hover:decoration-solid;
}
button {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:ring-2 focus:ring-blue-300 transition-colors duration-150 ease-in-out;
}
.content a {
@apply underline decoration-dotted decoration-1 hover:decoration-solid;
}
Expand Down
32 changes: 31 additions & 1 deletion swing/src/main/scala/gooey/swing/Algebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ import gooey.component.Checkbox
import gooey.component.Map
import gooey.component.Pure
import gooey.component.Textbox
import gooey.component.Button
import gooey.component.style.*
import net.bulbyvr.swing.io.all.{_, given}
import net.bulbyvr.swing.io.wrapper.*

type Algebra =
gooey.Algebra & And.Algebra & Checkbox.Algebra & Map.Algebra & Pure.Algebra &
Textbox.Algebra
Textbox.Algebra & Button.Algebra

given Algebra: gooey.Algebra
with And.Algebra
with Checkbox.Algebra
with Map.Algebra
with Pure.Algebra
with Textbox.Algebra
with Button.Algebra
with {

type Env = Environment
Expand Down Expand Up @@ -116,6 +118,34 @@ given Algebra: gooey.Algebra
}
}

def button(
label: Option[String],
onClick: () => Unit,
observers: Chain[WritableVar[Unit]]
)(env: Env): UI[Unit] = {
SignallingRef[IO].of(()).toResource.flatMap { clickSignal =>
val signals = addObservers(observers, clickSignal)
val component = makeComponent(
makeLabel(label),
buttonWithClick(onClick, clickSignal)
)
signals *> component.map(c => (c, ()))
}
}

private def buttonWithClick(
onClick: () => Unit,
clickSignal: SignallingRef[IO, Unit]
): Resource[IO, Component[IO]] =
Resource.pure[IO, JButton](
new JButton().tap { btn =>
btn.addActionListener { _ =>
onClick()
clickSignal.set(())
}
}
).map(Component(_))

// Utilities -------------------------------------------------------

def makeComponent[A](
Expand Down