diff src/name/blackcap/clipman/Main.kt @ 17:9dd58db4d15a

Only convert RTF to HTML if needed. Much more efficient.
author David Barts <n5jrn@me.com>
date Tue, 21 Jan 2020 16:24:18 -0800
parents 732f92dc3bc6
children 5e0d1fe61da9
line wrap: on
line diff
--- a/src/name/blackcap/clipman/Main.kt	Tue Jan 21 13:07:35 2020 -0800
+++ b/src/name/blackcap/clipman/Main.kt	Tue Jan 21 16:24:18 2020 -0800
@@ -16,6 +16,7 @@
 import java.util.concurrent.Semaphore
 import java.util.logging.Level
 import java.util.logging.Logger
+import java.util.zip.CRC32
 import javax.swing.*
 import javax.swing.border.*
 import javax.swing.text.JTextComponent
@@ -78,59 +79,55 @@
             EmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER))
 
     override fun run() {
-        var oldContents = ""
-        var newContents = ""
+        var oldContents: PasteboardItem? = null
         while (true) {
             if (enabled) {
                 var contents = PasteboardItem.read()
-                if (contents != null) {
-                    newContents = when (contents) {
-                        is PasteboardItem.Plain -> contents.plain
-                        is PasteboardItem.HTML -> contents.html
+                if ((contents != null) && (contents != oldContents)) {
+                    val stdWidth = queue.parent.size.width - 2 * (PANEL_BORDER+OUTER_BORDER+INNER_BORDER+MARGIN_BORDER)
+                    val widget = JPanel().apply {
+                        layout = BoxLayout(this, BoxLayout.Y_AXIS)
+                        background = queue.parent.background
+                        border = outerBorder
+                    }
+                    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)
                     }
-                    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
-                            border = outerBorder
+                    if (html == null) {
+                        widget.run {
+                            add(stdLabel("Plain text"))
+                            add(ClipText().apply {
+                                contentType = "text/plain"
+                                text = plain
+                                font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
+                                border = stdBorder
+                                autoSize(stdWidth)
+                                setEditable(false)
+                                alignmentX = JTextPane.LEFT_ALIGNMENT
+                            })
                         }
-                        when (contents) {
-                            is PasteboardItem.Plain -> widget.run {
-                                add(stdLabel("Plain text"))
-                                add(ClipText().apply {
-                                    contentType = "text/plain"
-                                    text = contents.plain
-                                    font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
-                                    border = stdBorder
-                                    autoSize(stdWidth!!)
-                                    setEditable(false)
-                                    alignmentX = JTextPane.LEFT_ALIGNMENT
-                                })
+                    } else {
+                        widget.run {
+                            add(stdLabel("Styled text"))
+                            val (dhtml, style) = preproc(html)
+                            val hek = HTMLEditorKit().apply {
+                                style.addStyleSheet(styleSheet)
+                                styleSheet = style
                             }
-                            is PasteboardItem.HTML -> widget.run {
-                                add(stdLabel("Styled text"))
-                                val (html, style) = preproc(contents.html)
-                                val hek = HTMLEditorKit().apply {
-                                    style.addStyleSheet(styleSheet)
-                                    styleSheet = style
-                                }
-                                add(ClipText().apply {
-                                    editorKit = hek
-                                    text = html
-                                    border = stdBorder
-                                    autoSize(stdWidth!!)
-                                    setEditable(false)
-                                    alignmentX = JTextPane.LEFT_ALIGNMENT
-                                })
-                            }
+                            add(ClipText().apply {
+                                editorKit = hek
+                                text = dhtml
+                                border = stdBorder
+                                autoSize(stdWidth)
+                                setEditable(false)
+                                alignmentX = JTextPane.LEFT_ALIGNMENT
+                            })
                         }
-                        queue.add(QueueItem(widget, contents))
-                        oldContents = newContents
                     }
+                    queue.add(QueueItem(widget, contents))
+                    oldContents = contents
                 }
             }
             if (Thread.interrupted()) {