Unverified Commit 26b421ed authored by Ivan Matkov's avatar Ivan Matkov Committed by GitHub

Remove `URIManager`, `ClipboardManager` and `CursorManager` (#1166)

They are out of the scope of the skiko library.

[SKIKO-1020](https://youtrack.jetbrains.com/issue/SKIKO-1020) Deprecate
and remove URIManager
[SKIKO-1021](https://youtrack.jetbrains.com/issue/SKIKO-1021) Deprecate
and remove ClipboardManager
[SKIKO-1022](https://youtrack.jetbrains.com/issue/SKIKO-1022) Deprecate
and remove CursorManager
parent 0fa95b75
package org.jetbrains.skiko.sample package org.jetbrains.skiko.sample
import org.jetbrains.skiko.CursorManager
import org.jetbrains.skiko.PredefinedCursors
import org.jetbrains.skiko.SkiaLayer import org.jetbrains.skiko.SkiaLayer
import java.awt.event.MouseEvent import java.awt.event.MouseEvent
import java.awt.event.MouseMotionListener import java.awt.event.MouseMotionListener
...@@ -9,8 +7,6 @@ import java.awt.event.MouseWheelEvent ...@@ -9,8 +7,6 @@ import java.awt.event.MouseWheelEvent
import java.awt.event.MouseWheelListener import java.awt.event.MouseWheelListener
class AwtClocks(private val layer: SkiaLayer) : Clocks(layer::renderApi), MouseMotionListener, MouseWheelListener { class AwtClocks(private val layer: SkiaLayer) : Clocks(layer::renderApi), MouseMotionListener, MouseWheelListener {
private val cursorManager = CursorManager()
init { init {
layer.addMouseMotionListener(this) layer.addMouseMotionListener(this)
} }
...@@ -24,9 +20,9 @@ class AwtClocks(private val layer: SkiaLayer) : Clocks(layer::renderApi), MouseM ...@@ -24,9 +20,9 @@ class AwtClocks(private val layer: SkiaLayer) : Clocks(layer::renderApi), MouseM
override fun mouseMoved(event: MouseEvent) { override fun mouseMoved(event: MouseEvent) {
if (event.x > 200) { if (event.x > 200) {
cursorManager.setCursor(layer.component, PredefinedCursors.HAND) layer.component.cursor = Cursor.HAND_CURSOR
} else { } else {
cursorManager.setCursor(layer.component, PredefinedCursors.DEFAULT) layer.component.cursor = Cursor.DEFAULT_CURSOR
} }
xpos = event.x.toDouble() xpos = event.x.toDouble()
ypos = event.y.toDouble() ypos = event.y.toDouble()
......
package org.jetbrains.skiko package org.jetbrains.skiko
import android.content.* import android.content.*
import android.content.ClipboardManager
import android.content.res.Configuration import android.content.res.Configuration
import android.net.Uri
import android.view.PointerIcon.*
import android.view.View import android.view.View
import org.jetbrains.skiko.redrawer.Redrawer import org.jetbrains.skiko.redrawer.Redrawer
...@@ -51,46 +48,3 @@ actual val currentSystemTheme: SystemTheme ...@@ -51,46 +48,3 @@ actual val currentSystemTheme: SystemTheme
else -> SystemTheme.UNKNOWN else -> SystemTheme.UNKNOWN
} }
} }
internal actual fun URIHandler_openUri(uri: String) {
defaultContext!!.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(uri)))
}
internal actual fun ClipboardManager_setText(text: String) {
val clipboard = defaultContext!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("", text)
clipboard.setPrimaryClip(clip)
}
internal actual fun ClipboardManager_getText(): String? {
val context = defaultContext!!
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = clipboard.primaryClip ?: return null
return clip.getItemAt(0)?.text?.toString()
}
internal actual fun ClipboardManager_hasText(): Boolean = !ClipboardManager_getText().isNullOrEmpty()
actual typealias Cursor = android.view.PointerIcon
// TODO: not sure if correct.
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {
if (component is View) {
component.pointerIcon = cursor
}
}
internal actual fun CursorManager_getCursor(component: Any): Cursor? =
if (component is View) {
component.pointerIcon
} else {
null
}
internal actual fun getCursorById(id: PredefinedCursorsId): Cursor =
when (id) {
PredefinedCursorsId.DEFAULT -> getSystemIcon(defaultContext!!, TYPE_DEFAULT)
PredefinedCursorsId.CROSSHAIR -> getSystemIcon(defaultContext!!, TYPE_CROSSHAIR)
PredefinedCursorsId.HAND -> getSystemIcon(defaultContext!!, TYPE_HAND)
PredefinedCursorsId.TEXT -> getSystemIcon(defaultContext!!, TYPE_TEXT)
}
\ No newline at end of file
package org.jetbrains.skiko package org.jetbrains.skiko
import org.jetbrains.skiko.redrawer.* import org.jetbrains.skiko.redrawer.*
import java.awt.Component
import java.awt.Desktop
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.UnsupportedFlavorException
import java.io.IOException
import java.net.URI
import java.net.URL
import javax.swing.UIManager import javax.swing.UIManager
actual fun setSystemLookAndFeel() = UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) actual fun setSystemLookAndFeel() = UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
...@@ -36,69 +27,3 @@ internal actual fun makeDefaultRenderFactory(): RenderFactory = ...@@ -36,69 +27,3 @@ internal actual fun makeDefaultRenderFactory(): RenderFactory =
else -> throw UnsupportedOperationException("AWT doesn't support $hostOs") else -> throw UnsupportedOperationException("AWT doesn't support $hostOs")
} }
} }
internal actual fun URIHandler_openUri(uri: String) {
val desktop = Desktop.getDesktop()
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(URI(uri))
} else when (hostOs) {
OS.Linux -> {
URI(uri) // Validate URI for exception behavior consistent with the Desktop.browse() case (throwing URISyntaxException)
Runtime.getRuntime().exec(arrayOf("xdg-open", URI(uri).toString()))
}
OS.Windows, OS.MacOS -> {
throw UnsupportedOperationException("AWT doesn't support the BROWSE action on $hostOs")
}
else -> throw UnsupportedOperationException("AWT doesn't support $hostOs")
}
}
private val systemClipboard by lazy {
try {
Toolkit.getDefaultToolkit().getSystemClipboard()
} catch (e: java.awt.HeadlessException) {
null
}
}
internal actual fun ClipboardManager_setText(text: String) {
systemClipboard?.setContents(StringSelection(text), null)
}
internal actual fun ClipboardManager_getText(): String? {
return try {
systemClipboard?.getData(DataFlavor.stringFlavor) as String?
} catch (_: UnsupportedFlavorException) {
null
} catch (_: IllegalStateException) {
null
} catch (_: IOException) {
null
}
}
internal actual fun ClipboardManager_hasText(): Boolean = !ClipboardManager_getText().isNullOrEmpty()
actual typealias Cursor = java.awt.Cursor
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {
if (component is Component) {
component.cursor = cursor
}
}
internal actual fun CursorManager_getCursor(component: Any): Cursor? {
return if (component is Component) {
component.cursor
} else {
null
}
}
internal actual fun getCursorById(id: PredefinedCursorsId): Cursor =
when (id) {
PredefinedCursorsId.DEFAULT -> Cursor(Cursor.DEFAULT_CURSOR)
PredefinedCursorsId.CROSSHAIR -> Cursor(Cursor.CROSSHAIR_CURSOR)
PredefinedCursorsId.HAND -> Cursor(Cursor.HAND_CURSOR)
PredefinedCursorsId.TEXT -> Cursor(Cursor.TEXT_CURSOR)
}
package org.jetbrains.skiko
/**
* Identifiers of predefined cursors.
*/
enum class PredefinedCursorsId {
DEFAULT,
CROSSHAIR,
TEXT,
HAND
}
/**
* Predefined platform cursors.
*/
object PredefinedCursors {
val DEFAULT: Cursor = getCursorById(PredefinedCursorsId.DEFAULT)
val CROSSHAIR: Cursor = getCursorById(PredefinedCursorsId.CROSSHAIR)
val TEXT: Cursor = getCursorById(PredefinedCursorsId.TEXT)
val HAND: Cursor = getCursorById(PredefinedCursorsId.HAND)
}
/**
* Pointer device cursor abstraction.
*/
expect class Cursor
internal expect fun getCursorById(id: PredefinedCursorsId): Cursor
package org.jetbrains.skiko
/**
* Manager for opening external links. Is open so that platform could provide an override if default doesn't fit.
*/
open class URIManager {
/**
* Asynchronous request to open a URI in system browser.
* [uri] a universal resource identifier to open, exact set of supported APIs is platform dependent
*/
open fun openUri(uri: String) = URIHandler_openUri(uri)
}
internal expect fun URIHandler_openUri(uri: String)
/**
* Manager for controlling system clipboard. Is open so that platform could provide an override if default doesn't fit.
*/
open class ClipboardManager {
/**
* Set current system clipboard content as text.
*/
open fun setText(text: String) = ClipboardManager_setText(text)
/**
* Get current system clipboard content as text.
*/
open fun getText(): String? = ClipboardManager_getText()
/**
* Returns true, if clipboard contains text
*/
open fun hasText(): Boolean = ClipboardManager_hasText()
}
internal expect fun ClipboardManager_setText(text: String)
internal expect fun ClipboardManager_getText(): String?
internal expect fun ClipboardManager_hasText(): Boolean
/**
* Manager to control cursor per native component.
*/
open class CursorManager {
/**
* Set cursor for the given component.
*/
open fun setCursor(component: Any?, cursor: Cursor) {
if (component != null) CursorManager_setCursor(component, cursor)
}
/**
* Get cursor for the given component.
*/
open fun getCursor(component: Any?): Cursor? = if (component != null) {
CursorManager_getCursor(component)
} else { null }
}
/**
* Sets cursor for the platform component.
*/
internal expect fun CursorManager_setCursor(component: Any, cursor: Cursor)
/**
* Gets current cursor for the platform component, or null if not defined or known.
*/
internal expect fun CursorManager_getCursor(component: Any): Cursor?
...@@ -2,15 +2,6 @@ package org.jetbrains.skiko ...@@ -2,15 +2,6 @@ package org.jetbrains.skiko
import platform.UIKit.* import platform.UIKit.*
internal actual fun ClipboardManager_setText(text: String) {
UIPasteboard.generalPasteboard.string = text
}
internal actual fun ClipboardManager_getText(): String? {
return UIPasteboard.generalPasteboard.string
}
internal actual fun ClipboardManager_hasText(): Boolean = UIPasteboard.generalPasteboard.hasStrings()
internal actual fun UIView.skikoInitializeUIView() { internal actual fun UIView.skikoInitializeUIView() {
multipleTouchEnabled = true multipleTouchEnabled = true
userInteractionEnabled = true userInteractionEnabled = true
......
package org.jetbrains.skiko package org.jetbrains.skiko
import org.w3c.dom.HTMLElement
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {
(component as? HTMLElement)?.style?.cursor = cursor
}
internal actual fun CursorManager_getCursor(component: Any): Cursor? {
return (component as? HTMLElement)?.style?.cursor
}
internal actual fun getNavigatorInfo(): String = internal actual fun getNavigatorInfo(): String =
js("navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform") as String js("navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform") as String
......
package org.jetbrains.skiko
actual fun URIHandler_openUri(uri: String) {
TODO("Implement URIHandler_openUri() on Linux")
}
internal actual fun ClipboardManager_setText(text: String) {
TODO("Implement ClipboardManager_setText() on Linux")
}
internal actual fun ClipboardManager_getText(): String? {
TODO("Implement ClipboardManager_getText() on Linux")
}
internal actual fun ClipboardManager_hasText(): Boolean = !ClipboardManager_getText().isNullOrEmpty()
actual typealias Cursor = Any
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {
TODO("Implement CursorManager_setCursor on Linux")
}
internal actual fun CursorManager_getCursor(component: Any): Cursor? {
TODO("Implement CursorManager_getCursor on Linux")
}
internal actual fun getCursorById(id: PredefinedCursorsId): Cursor =
when (id) {
PredefinedCursorsId.DEFAULT -> Any()
PredefinedCursorsId.CROSSHAIR -> Any()
PredefinedCursorsId.HAND -> Any()
PredefinedCursorsId.TEXT -> Any()
}
\ No newline at end of file
package org.jetbrains.skiko
import platform.AppKit.*
import platform.Foundation.NSURL
internal actual fun URIHandler_openUri(uri: String) {
NSWorkspace.sharedWorkspace.openURL(NSURL.URLWithString(uri)!!)
}
internal actual fun ClipboardManager_setText(text: String) {
NSPasteboard.generalPasteboard.clearContents()
NSPasteboard.generalPasteboard.setString(string = text, forType = NSPasteboardTypeString)
}
internal actual fun ClipboardManager_getText(): String? {
return NSPasteboard.generalPasteboard.stringForType(dataType = NSPasteboardTypeString)
}
internal actual fun ClipboardManager_hasText(): Boolean = !ClipboardManager_getText().isNullOrEmpty()
actual typealias Cursor = NSCursor
// TODO: not sure if it is correct.
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {
if (component is NSView) {
component.resetCursorRects()
component.addCursorRect(component.bounds(), cursor)
}
}
internal actual fun CursorManager_getCursor(component: Any): Cursor? {
return if (component is NSView) {
// TODO: likely incorrect.
NSCursor.currentCursor
} else {
null
}
}
internal actual fun getCursorById(id: PredefinedCursorsId): Cursor =
when (id) {
PredefinedCursorsId.DEFAULT -> NSCursor.arrowCursor
PredefinedCursorsId.CROSSHAIR -> NSCursor.crosshairCursor
PredefinedCursorsId.HAND -> NSCursor.pointingHandCursor
PredefinedCursorsId.TEXT -> NSCursor.IBeamCursor
}
\ No newline at end of file
...@@ -2,16 +2,6 @@ package org.jetbrains.skiko ...@@ -2,16 +2,6 @@ package org.jetbrains.skiko
import platform.UIKit.* import platform.UIKit.*
// tvOS doesn't have support for clipboard
internal actual fun ClipboardManager_setText(text: String) {
}
internal actual fun ClipboardManager_getText(): String? {
return null
}
internal actual fun ClipboardManager_hasText(): Boolean = false
internal actual fun UIView.skikoInitializeUIView() { internal actual fun UIView.skikoInitializeUIView() {
userInteractionEnabled = true userInteractionEnabled = true
} }
package org.jetbrains.skiko
import platform.Foundation.NSURL.Companion.URLWithString
import platform.UIKit.UIApplication
internal actual fun URIHandler_openUri(uri: String) {
UIApplication.sharedApplication.openURL(
url = URLWithString(uri)!!,
options = emptyMap<Any?, Any>(),
completionHandler = null
)
}
// TODO: not sure if correct.
actual typealias Cursor = Any
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {}
internal actual fun CursorManager_getCursor(component: Any): Cursor? = null
internal actual fun getCursorById(id: PredefinedCursorsId): Cursor =
when (id) {
PredefinedCursorsId.DEFAULT -> Any()
PredefinedCursorsId.CROSSHAIR -> Any()
PredefinedCursorsId.HAND -> Any()
PredefinedCursorsId.TEXT -> Any()
}
\ No newline at end of file
package org.jetbrains.skiko package org.jetbrains.skiko
import org.w3c.dom.HTMLElement
import org.w3c.dom.events.UIEvent import org.w3c.dom.events.UIEvent
internal actual fun CursorManager_setCursor(component: Any, cursor: Cursor) {
(component as? HTMLElement)?.style?.cursor = cursor
}
internal actual fun CursorManager_getCursor(component: Any): Cursor? {
return (component as? HTMLElement)?.style?.cursor
}
internal actual fun getNavigatorInfo(): String = internal actual fun getNavigatorInfo(): String =
js("navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform") js("navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform")
...@@ -7,32 +7,6 @@ internal actual inline fun <R> maybeSynchronized(lock: Any, block: () -> R): R = ...@@ -7,32 +7,6 @@ internal actual inline fun <R> maybeSynchronized(lock: Any, block: () -> R): R =
actual fun currentNanoTime(): Long = (window.performance.now() * 1_000_000).toLong() actual fun currentNanoTime(): Long = (window.performance.now() * 1_000_000).toLong()
internal actual fun URIHandler_openUri(uri: String) {
window.open(uri, target = "_blank")
}
internal actual fun ClipboardManager_setText(text: String) {
window.navigator.clipboard.writeText(text)
}
internal actual fun ClipboardManager_getText(): String? {
// TODO("implement ClipboardManager_getText")
// kotlinx.browser.window.navigator.clipboard.readText()
return null
}
internal actual fun ClipboardManager_hasText(): Boolean = !ClipboardManager_getText().isNullOrEmpty()
actual typealias Cursor = String
internal actual fun getCursorById(id: PredefinedCursorsId): Cursor =
when (id) {
PredefinedCursorsId.DEFAULT -> "default"
PredefinedCursorsId.CROSSHAIR -> "crosshair"
PredefinedCursorsId.HAND -> "pointer"
PredefinedCursorsId.TEXT -> "text"
}
internal actual fun loadOpenGLLibrary() { internal actual fun loadOpenGLLibrary() {
// Nothing to do here // Nothing to do here
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment