changeset 65:ca0fab758ff9

Hopefully fix the race conditions that sometimes make it crash.
author David Barts <n5jrn@me.com>
date Sun, 12 Jan 2025 11:32:17 -0800
parents a30deee457e3
children a8b04aa874e3
files build.xml src/name/blackcap/clipman/Main.kt
diffstat 2 files changed, 41 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/build.xml	Sun Jan 12 10:23:04 2025 -0800
+++ b/build.xml	Sun Jan 12 11:32:17 2025 -0800
@@ -32,8 +32,8 @@
   <property name="lc.app.name"   value="clipman"/>
   <property name="app.entry"     value="name.blackcap.clipman.MainKt"/>
   <property name="app.domain"    value="name.blackcap.${lc.app.name}"/>
-  <property name="app.copyright" value="Copyright © 2020–2022, David W. Barts."/>
-  <property name="app.version"   value="1.10"/>
+  <property name="app.copyright" value="Copyright © 2020–2025, David W. Barts."/>
+  <property name="app.version"   value="1.11"/>
   <property name="jar.name"      value="${basedir}/${lc.app.name}.jar"/>
   <property name="work.jar"      value="${basedir}/work.jar"/>
   <property name="lib.home"      value="${basedir}/lib"/>
--- a/src/name/blackcap/clipman/Main.kt	Sun Jan 12 10:23:04 2025 -0800
+++ b/src/name/blackcap/clipman/Main.kt	Sun Jan 12 11:32:17 2025 -0800
@@ -60,34 +60,40 @@
         var oldContents: PasteboardItem? = null
         while (true) {
             if (enabled) {
-                var contents = PasteboardItem.read()
+                var kontents: PasteboardItem? = null
+                inSynSwingThread {
+                    kontents = PasteboardItem.read()
+                }
+                val contents = kontents
                 if ((contents != null) && (contents != oldContents)) {
                     val (plain, html) = when(contents) {
                         is PasteboardItem.Plain -> Pair(contents.plain, null)
                         is PasteboardItem.HTML -> Pair(null, contents.html)
                         is PasteboardItem.RTF -> Pair(contents.plain, contents.html)
                     }
-                    val piv = if (html == null) {
-                        PasteboardItemView("Plain text", ClipText(contents).apply {
-                            contentType = "text/plain"
-                            text = plain
-                            font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
-                            resize()
-                        })
-                    } else {
-                        val (dhtml, style) = preproc(html)
-                        val hek = MyEditorKit().apply {
-                            style.addStyleSheet(defaultStyleSheet)
-                            styleSheet = style
+                    inSynSwingThread {
+                        val piv = if (html == null) {
+                            PasteboardItemView("Plain text", ClipText(contents).apply {
+                                contentType = "text/plain"
+                                text = plain
+                                font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
+                                resize()
+                            })
+                        } else {
+                            val (dhtml, style) = preproc(html)
+                            val hek = MyEditorKit().apply {
+                                style.addStyleSheet(defaultStyleSheet)
+                                styleSheet = style
+                            }
+                            PasteboardItemView("Styled text", ClipText(contents).apply {
+                                editorKit = hek
+                                text = dhtml
+                                resize()
+                            })
                         }
-                        PasteboardItemView("Styled text", ClipText(contents).apply {
-                            editorKit = hek
-                            text = dhtml
-                            resize()
-                        })
+                        piv.searchable.addMouseListener(this)
+                        Application.queue.add(QueueItem(contents, piv))
                     }
-                    piv.searchable.addMouseListener(this)
-                    Application.queue.add(QueueItem(contents, piv))
                     oldContents = contents
                 }
             }
@@ -140,22 +146,22 @@
         }
     }
 
- 	override fun mousePressed(e: MouseEvent) {
- 	    maybeShowPopup(e)
- 	}
+    override fun mousePressed(e: MouseEvent) {
+        maybeShowPopup(e)
+    }
 
- 	override fun mouseReleased(e: MouseEvent) {
- 	    maybeShowPopup(e)
- 	}
+    override fun mouseReleased(e: MouseEvent) {
+        maybeShowPopup(e)
+    }
 
- 	private fun maybeShowPopup(e: MouseEvent) {
- 	    if (e.isPopupTrigger()) {
- 	        Application.popupMenu.show(e.component, e.x, e.y)
- 	    }
- 	}
+    private fun maybeShowPopup(e: MouseEvent) {
+        if (e.isPopupTrigger()) {
+            Application.popupMenu.show(e.component, e.x, e.y)
+        }
+    }
 
- 	override fun mouseEntered(e: MouseEvent) { }
- 	override fun mouseExited(e: MouseEvent) { }
+    override fun mouseEntered(e: MouseEvent) { }
+    override fun mouseExited(e: MouseEvent) { }
 }
 
 object Application {