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

Scala prefs preview #60

Merged
merged 2 commits into from
Apr 10, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/main/scala/com/melvic/chi/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.melvic.chi
// TODO: Store this on a properties file
object Config {
val MaxColumnWidth: Int = 80
val DialogFontSize: Int = 14
val DialogHeaderFontSize: Int = 15
val ComponentFontSize: Float = 14
val ComponentHeaderFontSize: Int = 15
val CustomSettingsPath = "user-prefs.json"
val DefaultSettingsPath = "preferences.json"
val DefaultFontPath = "/fonts/FiraCode-Regular.ttf"
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/com/melvic/chi/config/Preferences.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ object Preferences {
def loadDefaults: Preferences = new Preferences {
content = SettingsContent.loadDefaults
}

def fromContent(settingsContent: SettingsContent): Preferences =
new Preferences {
content = settingsContent
}
}
11 changes: 9 additions & 2 deletions src/main/scala/com/melvic/chi/eval/package.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.melvic.chi

import com.melvic.chi.ast.Definition
import com.melvic.chi.config.Preferences
import com.melvic.chi.out.Result

package object eval {
type Evaluate = String => String

def generateAndShow(code: String)(implicit prefs: Preferences): String =
Result.show(Generate.fromSignatureString(code))
def generateWith(code: String)(f: Result[Definition] => String)(implicit prefs: Preferences): String =
f(Generate.fromSignatureString(code))

def generateAndShowWithInfo(code: String)(implicit prefs: Preferences): String =
generateWith(code)(Result.showWithInfo)

def generateAndShowCode(code: String)(implicit prefs: Preferences): String =
generateWith(code)(Result.showCode)
}
2 changes: 1 addition & 1 deletion src/main/scala/com/melvic/chi/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ package object chi {
type Result[A] = Result.Result[A]
type Evaluate = eval.Evaluate

def generateAndShow(code: String)(implicit prefs: Preferences): String = eval.generateAndShow(code)
def generateAndShow(code: String)(implicit prefs: Preferences): String = eval.generateAndShowWithInfo(code)
}
10 changes: 9 additions & 1 deletion src/main/scala/com/melvic/chi/views/FontUtils.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.melvic.chi.views

import java.awt.Component
import com.melvic.chi.Config

import java.awt.{Component, Font}

object FontUtils {
def updateSize[C <: Component](component: C, size: Float): C =
Expand All @@ -17,4 +19,10 @@ object FontUtils {
component.setFont(newFont)
component
}

def withComponentFont[C <: Component](component: C): C =
updateSize(component, Config.ComponentFontSize)

def withComponentHeaderFont[C <: Component](component: C) =
updateStyle(updateSize(component, Config.ComponentHeaderFontSize), Font.BOLD)
}
1 change: 1 addition & 0 deletions src/main/scala/com/melvic/chi/views/MainWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.melvic.chi.views

import com.melvic.chi.Evaluate
import com.melvic.chi.config.Preferences
import com.melvic.chi.views.dialogs.PreferencesDialog
import com.melvic.chi.views.menus.MenuBar

import java.awt._
Expand Down
16 changes: 10 additions & 6 deletions src/main/scala/com/melvic/chi/views/ToolBarComponent.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.melvic.chi.views

import com.melvic.chi.Config
import com.melvic.chi.views.dialogs.PreferencesDialog

import java.awt.Dimension
import java.awt.event.ActionListener
import javax.swing.{ImageIcon, JButton, JToolBar}

class ToolBarComponent(editorComponent: EditorComponent, preferencesDialog: PreferencesDialog) extends JToolBar {
add(makeButton("run", "Run code", "Run", _ => editorComponent.run()))
add(makeButton("clear", "Clear all text", "Clear", _ => editorComponent.clear()))
class ToolBarComponent(editorComponent: EditorComponent, preferencesDialog: PreferencesDialog)
extends JToolBar {
add(makeButton("preferences", "Show Preferences", "Preferences", _ => preferencesDialog.display()))
addSeparator(new Dimension(20, 10))
add(makeButton("run", "Run code", "Run", _ => editorComponent.run()))
add(makeButton("clear", "Clear all texts", "Clear", _ => editorComponent.clear()))

private def makeButton(
imageName: String,
Expand All @@ -23,10 +27,10 @@ class ToolBarComponent(editorComponent: EditorComponent, preferencesDialog: Pref
val imageURL = getClass.getResource(Config.IconsDir + imageName + ".png")
if (imageURL != null)
button.setIcon(new ImageIcon(imageURL, altText))
else {
button.setText(altText)
else
System.err.println("Icon not found: " + imageName)
}

button.setText(altText)

button
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.melvic.chi.views
package com.melvic.chi.views.dialogs

import com.melvic.chi.Config
import com.melvic.chi.config.SettingsContent.ScalaSettings
import com.melvic.chi.config.{Preferences, SettingsContent}
import com.melvic.chi.views.FontUtils
import net.miginfocom.swing.MigLayout

import java.awt.{Font, Frame}
import java.awt.Frame
import java.awt.event.{ActionEvent, ActionListener, ItemEvent, ItemListener}
import javax.swing._
import scala.Function.const

class PreferencesDialog(frame: Frame)(implicit preferences: Preferences)
extends JDialog(frame, true) {
class PreferencesDialog(frame: Frame)(implicit preferences: Preferences) extends JDialog(frame, true) {
setTitle("Preferences")

val pointFreeBox = createCheckBox("Point-free style")
Expand Down Expand Up @@ -83,20 +84,40 @@ class PreferencesDialog(frame: Frame)(implicit preferences: Preferences)
addTitle("Scala Settings", scalaPane)
checkBoxes.foreach(scalaPane.add(_, "wrap"))

val previewComponent = PreviewComponent.fromPanel(scalaPane)
addCheckBoxListeners(previewComponent)
scalaPane
}

private def createCheckBox(text: String): JCheckBox =
FontUtils.updateSize(new JCheckBox(text), Config.DialogFontSize)
FontUtils.withComponentFont(new JCheckBox(text))

private def addTitle(title: String, panel: JPanel): Unit = {
panel.add(
FontUtils.updateStyle(
FontUtils.updateSize(new JLabel(title), Config.DialogHeaderFontSize),
Font.BOLD
),
FontUtils.withComponentHeaderFont(new JLabel(title)),
"gapleft 20, wrap"
)
panel.add(new JSeparator(), "growx, wrap, gapbottom 20")
}

private def addCheckBoxListeners(previewComponent: PreviewComponent): Unit = {
def rerunPreview(): Unit = {
val scalaSettings = preferences.content.scala
val newScalaSettings = scalaSettings.copy(
pointFree = pointFreeBox.isSelected,
simplifyMatch = simplifyMatchBox.isSelected,
usePredef = usePredefBox.isSelected
)
val newPreferences = Preferences.fromContent(SettingsContent(newScalaSettings))
println(newScalaSettings)
previewComponent.run(newPreferences)
}

pointFreeBox.addActionListener(_ => rerunPreview())
pointFreeBox.addItemListener(_ => rerunPreview())
simplifyMatchBox.addActionListener(_ => rerunPreview())
simplifyMatchBox.addItemListener(_ => rerunPreview())
usePredefBox.addActionListener(_ => rerunPreview())
usePredefBox.addItemListener(_ => rerunPreview())
}
}
47 changes: 47 additions & 0 deletions src/main/scala/com/melvic/chi/views/dialogs/PreviewComponent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.melvic.chi.views.dialogs

import com.melvic.chi.config.Preferences
import com.melvic.chi.eval.generateAndShowCode
import com.melvic.chi.views.{FontUtils, TextAreaComponent}
import org.fife.ui.rtextarea.RTextScrollPane

import java.awt.Font
import javax.swing.{JLabel, JPanel}

class PreviewComponent(implicit prefs: Preferences) extends RTextScrollPane(new TextAreaComponent) {
val textArea = getTextArea
textArea.setEditable(false)
textArea.setColumns(70)
textArea.setRows(20)
FontUtils.withComponentFont(textArea)

val inputs = List(
"def compose[A, B, C](f: B => C, g: A => B): A => C",
"def disjunctionElimination[A, B, C](f: A => C, g: B => C): Either[A, B] => C",
"def id[A]: A => A",
"def identity[A]: A => A"
)

run(prefs)

def run(preferences: Preferences): Unit = {
val output = inputs.map(generateAndShowCode(_)(preferences)).mkString("\n\n")
textArea.setText(output)
}
}

object PreviewComponent {
def fromPanel(panel: JPanel, extraLabelConstraints: String = "gaptop 20")(
implicit prefs: Preferences
): PreviewComponent = {
val label = new JLabel("Preview:")
val fullLabelConstraints = "wrap" +
(if (extraLabelConstraints.nonEmpty) s", $extraLabelConstraints" else "")
FontUtils.withComponentFont(FontUtils.updateStyle(label, Font.BOLD))

val previewComponent = new PreviewComponent()
panel.add(label, fullLabelConstraints)
panel.add(previewComponent, "wrap")
previewComponent
}
}
2 changes: 1 addition & 1 deletion src/main/scala/com/melvic/chi/views/menus/FileMenu.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.melvic.chi.views.menus

import com.melvic.chi.config.Preferences
import com.melvic.chi.views.PreferencesDialog
import com.melvic.chi.views.dialogs.PreferencesDialog

import java.awt.Frame
import java.awt.event.{ActionEvent, KeyEvent}
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/com/melvic/chi/views/menus/MenuBar.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.melvic.chi.views.menus

import com.melvic.chi.config.Preferences
import com.melvic.chi.views.{EditorComponent, PreferencesDialog}
import com.melvic.chi.views.EditorComponent
import com.melvic.chi.views.dialogs.PreferencesDialog

import java.awt.Frame
import javax.swing.JMenuBar
Expand Down