kotlinx-coroutines-swt

Library to easily use kotlinx.coroutines in SWT applications

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

de.brudaswen.kotlinx.coroutines
ArtifactId

ArtifactId

kotlinx-coroutines-swt
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

module
Description

Description

kotlinx-coroutines-swt
Library to easily use kotlinx.coroutines in SWT applications
Project URL

Project URL

https://github.com/brudaswen/kotlinx-coroutines-swt/
Source Code Management

Source Code Management

https://github.com/brudaswen/kotlinx-coroutines-swt/

Download kotlinx-coroutines-swt

Dependencies

compile (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.3.61
org.jetbrains.kotlinx : kotlinx-coroutines-core jar 1.3.3

Project Modules

There are no modules declared in this project.

Kotlin Coroutines SWT

Maven Central Snapshot CI Status Codecov License

Library to easily use kotlinx.coroutines in SWT applications

Provides Dispatchers.SWT, Dispatchers.swt(Display), Dispatchers.swt(Widget) and Dispatchers.Main implementation for SWT UI applications.

NOTE: The coroutine dispatcher Dispatchers.SWT (or Dispatchers.Main) dispatches events to the SWT default display. Therefore, the SWT default display should be created before calling Dispatchers.SWT. Otherwise a new default display is created (making the thread that invokes Dispatchers.SWT its user-interface thread).

Gradle Dependencies

// Kotlin Coroutines SWT
implementation("de.brudaswen.kotlinx.coroutines:kotlinx-coroutines-swt:1.0.0")

// Platform specific SWT dependency has to be added manually
implementation("org.eclipse.platform:org.eclipse.swt.gtk.linux.x86_64:3.113.0")
implementation("org.eclipse.platform:org.eclipse.swt.cocoa.macosx.x86_64:3.113.0")
implementation("org.eclipse.platform:org.eclipse.swt.win32.win32.x86_64:3.113.0")

// Kotlin Coroutines is added automatically, but can be added to force a specific version
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3")

Usage

fun main() {
    // Create UI in some thread
    val display = Display.getDefault()
    val shell = Shell(display)
    val label = Label(shell, SWT.NULL)
    // ...

    updateUiInNewThread(display, label)

    while (!display.isDisposed) {
        if (!display.readAndDispatch()) {
            display.sleep()
        }
    }
}

fun updateUiInNewThread(display: Display, label: Label) = thread {
    // Dispatch to default display (via [Dispatchers.Main])
    GlobalScope.launch(Dispatchers.Main) {
        label.text = "launch(Dispatchers.Main)"
    }

    // Dispatch to default display (via [Dispatchers.SWT])
    GlobalScope.launch(Dispatchers.SWT) {
        label.text = "launch(Dispatchers.SWT)"
    }

    // Dispatch to given display
    GlobalScope.launch(Dispatchers.swt(display)) {
        label.text = "launch(Dispatchers.swt(display))"
    }

    // Dispatch to display of widget
    GlobalScope.launch(Dispatchers.swt(label)) {
        label.text = "launch(Dispatchers.swt(label))"
    }

    // Dispatch via Display extension
    GlobalScope.launch(display) {
        label.orNull()?.text = "launch(display)"
    }

    // Dispatch via Widget extension
    GlobalScope.launch(label) {
        label.orNull()?.text = "launch(label)"
    }
}

Requirements

Dependency Versions
Kotlin Coroutines 1.1.0 ⁠– 1.3.3
SWT 3.105.3 ⁠– 3.113.0

License

Copyright 2020 Sven Obser

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
1.0.0
0.1.0