changeset 24:dac8dfb4b549

Preliminary menu bar support.
author David Barts <n5jrn@me.com>
date Thu, 23 Jan 2020 19:25:17 -0800
parents 14049bc97a7c
children 3129d0e24086
files src/name/blackcap/clipman/Main.kt
diffstat 1 files changed, 62 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/clipman/Main.kt	Thu Jan 23 14:29:32 2020 -0800
+++ b/src/name/blackcap/clipman/Main.kt	Thu Jan 23 19:25:17 2020 -0800
@@ -10,6 +10,9 @@
 import java.awt.Font
 import java.awt.Toolkit;
 import java.awt.datatransfer.*
+import java.awt.event.ActionEvent
+import java.awt.event.ActionListener
+import java.awt.event.KeyEvent
 import java.awt.event.WindowEvent
 import java.awt.event.WindowListener
 import java.util.Date
@@ -19,8 +22,8 @@
 import javax.swing.*
 import javax.swing.border.*
 import javax.swing.text.JTextComponent
+import javax.swing.text.html.HTMLEditorKit
 import javax.swing.text.html.StyleSheet
-import javax.swing.text.html.HTMLEditorKit
 import kotlin.concurrent.thread
 import org.jsoup.Jsoup
 import org.jsoup.nodes.*
@@ -183,6 +186,9 @@
 
 fun main(args: Array<String>) {
     LOGGER.log(Level.INFO, "beginning execution")
+    if (OS.type == OS.MAC) {
+        System.setProperty("apple.laf.useScreenMenuBar", "true")
+    }
     var frame: JFrame? = null
     var con: JPanel? = null
     inSynSwingThread {
@@ -194,6 +200,7 @@
             background = frame!!.background
         }
         frame!!.apply {
+            jMenuBar = makeMenuBar()
             contentPane.add(
                 JScrollPane(con!!).apply {
                     verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
@@ -210,6 +217,55 @@
     inSwingThread { frame!!.addWindowListener(KillIt(updater)) }
 }
 
+class MenuItemListener: ActionListener {
+    override fun actionPerformed(e: ActionEvent) {
+        println(e.actionCommand + " selected")
+    }
+}
+
+fun makeMenuBar() = JMenuBar().apply {
+    val al: ActionListener = MenuItemListener()
+    if (OS.type != OS.MAC) {
+        add(JMenu("File").apply {
+            add(JMenuItem("Quit").apply {
+                actionCommand = "File.Quit"
+                addActionListener(al)
+                makeShortcut(KeyEvent.VK_Q)
+            })
+        })
+    }
+    add(JMenu("Edit").apply {
+        add(JMenuItem("Clone").apply {
+            actionCommand = "Edit.Clone"
+            addActionListener(al)
+            makeShortcut(KeyEvent.VK_C)
+        })
+        add(JMenuItem("Coerce…").apply {
+            actionCommand = "Edit.Coerce"
+            addActionListener(al)
+            makeShortcut(KeyEvent.VK_K)
+        })
+        add(JMenuItem("Delete").apply {
+            actionCommand = "Edit.Delete"
+            addActionListener(al)
+            setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0))
+        })
+        add(JMenuItem("Find…").apply {
+            actionCommand = "Edit.Find"
+            addActionListener(al)
+            makeShortcut(KeyEvent.VK_F)
+        })
+    })
+    if (OS.type != OS.MAC) {
+        add(JMenu("Help").apply {
+            add(JMenuItem("About ClipMan…").apply {
+                actionCommand = "Help.About"
+                addActionListener(al)
+            })
+        })
+    }
+}
+
 fun inSwingThread(block: () -> Unit) {
     SwingUtilities.invokeLater(Runnable(block))
 }
@@ -231,3 +287,8 @@
     val r = modelToView(document.length)
     preferredSize = Dimension(width, r.y + r.height + SLOP)
 }
+
+val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
+fun JMenuItem.makeShortcut(key: Int): Unit {
+    setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
+}