comparison src/name/blackcap/clipman/Main.kt @ 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 8c6d6ad92aa1
children
comparison
equal deleted inserted replaced
64:a30deee457e3 65:ca0fab758ff9
58 58
59 override fun run() { 59 override fun run() {
60 var oldContents: PasteboardItem? = null 60 var oldContents: PasteboardItem? = null
61 while (true) { 61 while (true) {
62 if (enabled) { 62 if (enabled) {
63 var contents = PasteboardItem.read() 63 var kontents: PasteboardItem? = null
64 inSynSwingThread {
65 kontents = PasteboardItem.read()
66 }
67 val contents = kontents
64 if ((contents != null) && (contents != oldContents)) { 68 if ((contents != null) && (contents != oldContents)) {
65 val (plain, html) = when(contents) { 69 val (plain, html) = when(contents) {
66 is PasteboardItem.Plain -> Pair(contents.plain, null) 70 is PasteboardItem.Plain -> Pair(contents.plain, null)
67 is PasteboardItem.HTML -> Pair(null, contents.html) 71 is PasteboardItem.HTML -> Pair(null, contents.html)
68 is PasteboardItem.RTF -> Pair(contents.plain, contents.html) 72 is PasteboardItem.RTF -> Pair(contents.plain, contents.html)
69 } 73 }
70 val piv = if (html == null) { 74 inSynSwingThread {
71 PasteboardItemView("Plain text", ClipText(contents).apply { 75 val piv = if (html == null) {
72 contentType = "text/plain" 76 PasteboardItemView("Plain text", ClipText(contents).apply {
73 text = plain 77 contentType = "text/plain"
74 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE) 78 text = plain
75 resize() 79 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
76 }) 80 resize()
77 } else { 81 })
78 val (dhtml, style) = preproc(html) 82 } else {
79 val hek = MyEditorKit().apply { 83 val (dhtml, style) = preproc(html)
80 style.addStyleSheet(defaultStyleSheet) 84 val hek = MyEditorKit().apply {
81 styleSheet = style 85 style.addStyleSheet(defaultStyleSheet)
86 styleSheet = style
87 }
88 PasteboardItemView("Styled text", ClipText(contents).apply {
89 editorKit = hek
90 text = dhtml
91 resize()
92 })
82 } 93 }
83 PasteboardItemView("Styled text", ClipText(contents).apply { 94 piv.searchable.addMouseListener(this)
84 editorKit = hek 95 Application.queue.add(QueueItem(contents, piv))
85 text = dhtml
86 resize()
87 })
88 } 96 }
89 piv.searchable.addMouseListener(this)
90 Application.queue.add(QueueItem(contents, piv))
91 oldContents = contents 97 oldContents = contents
92 } 98 }
93 } 99 }
94 if (Thread.interrupted()) { 100 if (Thread.interrupted()) {
95 return 101 return
138 Application.styledRequired.disable() 144 Application.styledRequired.disable()
139 } 145 }
140 } 146 }
141 } 147 }
142 148
143 override fun mousePressed(e: MouseEvent) { 149 override fun mousePressed(e: MouseEvent) {
144 maybeShowPopup(e) 150 maybeShowPopup(e)
145 } 151 }
146 152
147 override fun mouseReleased(e: MouseEvent) { 153 override fun mouseReleased(e: MouseEvent) {
148 maybeShowPopup(e) 154 maybeShowPopup(e)
149 } 155 }
150 156
151 private fun maybeShowPopup(e: MouseEvent) { 157 private fun maybeShowPopup(e: MouseEvent) {
152 if (e.isPopupTrigger()) { 158 if (e.isPopupTrigger()) {
153 Application.popupMenu.show(e.component, e.x, e.y) 159 Application.popupMenu.show(e.component, e.x, e.y)
154 } 160 }
155 } 161 }
156 162
157 override fun mouseEntered(e: MouseEvent) { } 163 override fun mouseEntered(e: MouseEvent) { }
158 override fun mouseExited(e: MouseEvent) { } 164 override fun mouseExited(e: MouseEvent) { }
159 } 165 }
160 166
161 object Application { 167 object Application {
162 /* name we call ourselves */ 168 /* name we call ourselves */
163 val MYNAME = "ClipMan" 169 val MYNAME = "ClipMan"