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

Add toolbar #57

Merged
merged 1 commit into from
Apr 9, 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
24 changes: 23 additions & 1 deletion LICENSE-3RD-PARTY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,26 @@ SOFTWARE.
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
OTHER DEALINGS IN THE FONT SOFTWARE.

The MIT License (MIT)

Copyright (c) 2013-2017 Cole Bemis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions src/main/scala/com/melvic/chi/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ object Config {
val CustomSettingsPath = "user-prefs.json"
val DefaultSettingsPath = "preferences.json"
val DefaultFontPath = "/fonts/FiraCode-Regular.ttf"
val IconsDir = "/icons/"
}
4 changes: 2 additions & 2 deletions src/main/scala/com/melvic/chi/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.melvic.chi
import com.github.weisj.darklaf.LafManager
import com.github.weisj.darklaf.theme.DarculaTheme
import com.melvic.chi.config.{Preferences, SettingsContent}
import com.melvic.chi.views.MainComponent
import com.melvic.chi.views.MainWindow

import javax.swing.SwingUtilities

Expand All @@ -22,7 +22,7 @@ object Main {
LafManager.install(new DarculaTheme)

override def run(): Unit = {
new MainComponent(generateAndShow).setVisible(true)
new MainWindow(generateAndShow).setVisible(true)
}
})
}
20 changes: 0 additions & 20 deletions src/main/scala/com/melvic/chi/views/MainComponent.scala

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/scala/com/melvic/chi/views/MainWindow.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.melvic.chi.views

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

import java.awt._
import javax.swing._

class MainWindow(evaluate: Evaluate)(implicit preferences: Preferences) extends JFrame {
val editorComponent = new EditorComponent(evaluate)
val preferencesDialog = new PreferencesDialog(this)

setTitle("Chi")
createContentPane(editorComponent)
setJMenuBar(new MenuBar(this, editorComponent, preferencesDialog))
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
pack()
setLocationRelativeTo(null)
setExtendedState(Frame.MAXIMIZED_BOTH)

def createContentPane(editorComponent: EditorComponent): Unit = {
val mainComponent = new JPanel
mainComponent.setLayout(new BorderLayout())
mainComponent.add(new ToolBarComponent(editorComponent, preferencesDialog), BorderLayout.PAGE_START)
mainComponent.add(editorComponent, BorderLayout.CENTER)
setContentPane(mainComponent)
}
}
33 changes: 33 additions & 0 deletions src/main/scala/com/melvic/chi/views/ToolBarComponent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.melvic.chi.views

import com.melvic.chi.Config

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()))
add(makeButton("preferences", "Show Preferences", "Preferences", _ => preferencesDialog.display()))

private def makeButton(
imageName: String,
tooltipText: String,
altText: String,
actionListener: ActionListener
): JButton = {
val button = new JButton()
button.setToolTipText(tooltipText)
button.addActionListener(actionListener)

val imageURL = getClass.getResource(Config.IconsDir + imageName + ".png")
if (imageURL != null)
button.setIcon(new ImageIcon(imageURL, altText))
else {
button.setText(altText)
System.err.println("Icon not found: " + imageName)
}

button
}
}
9 changes: 5 additions & 4 deletions src/main/scala/com/melvic/chi/views/menus/FileMenu.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import java.awt.Frame
import java.awt.event.{ActionEvent, KeyEvent}
import javax.swing.{JMenu, JMenuItem, KeyStroke}

class FileMenu(frame: Frame)(implicit preferences: Preferences) extends JMenu("File") {
class FileMenu(frame: Frame, prefsDialog: PreferencesDialog)(implicit preferences: Preferences)
extends JMenu("File") {
add(preferencesMenuItem)
add(exitMenuItem)

val prefsDialog = new PreferencesDialog(frame)

private def preferencesMenuItem = {
val prefsItem = new JMenuItem("Preferences")
prefsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK))
prefsItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK)
)
prefsItem.addActionListener(_ => prefsDialog.display())
prefsItem
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/scala/com/melvic/chi/views/menus/MenuBar.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.melvic.chi.views.menus

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

import java.awt.Frame
import java.awt.event.{ActionEvent, KeyEvent}
import javax.swing.{JMenu, JMenuBar, JMenuItem, KeyStroke}
import javax.swing.JMenuBar

class MenuBar(frame: Frame, editorView: EditorComponent)(implicit prefs: Preferences) extends JMenuBar {
add(new FileMenu(frame))
class MenuBar(frame: Frame, editorView: EditorComponent, prefsDialog: PreferencesDialog)(
implicit prefs: Preferences
) extends JMenuBar {
add(new FileMenu(frame, prefsDialog))
add(new EditorMenu(editorView))
}