changeset 9:8fcff14defa2

Stomp out race conditions and set width adaptively.
author David Barts <n5jrn@me.com>
date Sun, 19 Jan 2020 15:43:01 -0800
parents 7715ff59f053
children e7e067f5b649
files src/name/blackcap/clipman/Main.kt
diffstat 1 files changed, 29 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/clipman/Main.kt	Sun Jan 19 14:00:17 2020 -0800
+++ b/src/name/blackcap/clipman/Main.kt	Sun Jan 19 15:43:01 2020 -0800
@@ -15,6 +15,7 @@
 import java.util.Date
 import java.util.logging.Level
 import java.util.logging.Logger
+import java.util.concurrent.Semaphore
 import javax.swing.*
 import javax.swing.border.*
 import javax.swing.text.JTextComponent
@@ -28,7 +29,12 @@
 /* default sizes */
 val CPWIDTH = 640
 val CPHEIGHT = 480
-val TPWIDTH = CPWIDTH - 60
+
+/* border widths */
+val PANEL_BORDER = 9
+val OUTER_BORDER = 9  /* must be 6 or more */
+val INNER_BORDER = 1
+val MARGIN_BORDER = 3
 
 /* kills the updating thread (and does a system exit) when needed */
 class KillIt(val thr: Thread) : WindowListener {
@@ -51,9 +57,12 @@
 /* the updating thread */
 class UpdateIt(val queue: PasteboardQueue, val interval: Int): Thread() {
     @Volatile var enabled = true
-    private val outerBorder = MatteBorder(3, 9, 9, 9, queue.parent.background)
+    private val outerBorder =
+        MatteBorder(OUTER_BORDER-6, OUTER_BORDER, OUTER_BORDER, OUTER_BORDER,
+            queue.parent.background)
     private val stdBorder =
-        CompoundBorder(LineBorder(Color.GRAY, 1), EmptyBorder(3, 3, 3, 3))
+        CompoundBorder(LineBorder(Color.GRAY, INNER_BORDER),
+            EmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER))
 
     override fun run() {
         var oldContents = ""
@@ -67,6 +76,10 @@
                         is PasteboardItem.HTML -> contents.plain
                     }
                     if (oldContents != newContents) {
+                        var stdWidth: Int? = null
+                        inSynSwingThread {
+                            stdWidth = queue.parent.size.width - 2 * (PANEL_BORDER+OUTER_BORDER+INNER_BORDER+MARGIN_BORDER)
+                        }
                         var widget = JPanel().apply {
                             layout = BoxLayout(this, BoxLayout.Y_AXIS)
                             background = queue.parent.background
@@ -80,7 +93,7 @@
                                     text = contents.plain
                                     font = Font(Font.MONOSPACED, Font.PLAIN, 14)
                                     border = stdBorder
-                                    autoSize(TPWIDTH)
+                                    autoSize(stdWidth!!)
                                     setEditable(false)
                                     alignmentX = JTextPane.LEFT_ALIGNMENT
                                 })
@@ -92,7 +105,7 @@
                                     toolTipText = "Styled text"
                                     text = scrub(contents.html)
                                     border = stdBorder
-                                    autoSize(TPWIDTH)
+                                    autoSize(stdWidth!!)
                                     setEditable(false)
                                     alignmentX = JTextPane.LEFT_ALIGNMENT
                                 })
@@ -136,10 +149,10 @@
     val frame = JFrame(MYNAME)
     val con = JPanel().apply {
         layout = BoxLayout(this, BoxLayout.Y_AXIS)
-        border = EmptyBorder(9, 9, 9, 9)
+        border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
         background = frame.background
     }
-    inSwingThread {
+    inSynSwingThread {
         frame.apply {
             contentPane.add(
                 JScrollPane(con).apply {
@@ -167,6 +180,15 @@
     SwingUtilities.invokeLater(Runnable(block))
 }
 
+fun inSynSwingThread(block: () -> Unit) {
+    val ready = Semaphore(0)
+    inSwingThread {
+        block()
+        ready.release()
+    }
+    ready.acquire()
+}
+
 fun JTextComponent.autoSize(width: Int): Unit {
     val SLOP = 10
     val dim = Dimension(width, width)