Skip to content

Commit

Permalink
additional mouse events and focus events provided as Flows
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Aug 26, 2024
1 parent 34016d3 commit 6943ccb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion demo/mvp-presenter/src/test/kotlin/BrowserPresenterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.test.advanceUntilIdle
import kotlin.test.Test

@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
@OptIn(ExperimentalCoroutinesApi::class)
class BrowserPresenterTest {

@Test
Expand Down
52 changes: 52 additions & 0 deletions demo/simple-kotlin-dsl/src/main/kotlin/FocusDemo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of xemantic-kotlin-swing-dsl - Kotlin goodies for Java Swing.
*
* Copyright (C) 2024 Kazimierz Pogoda
*
* xemantic-kotlin-swing-dsl is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* xemantic-kotlin-swing-dsl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with xemantic-kotlin-swing-dsl. If not,
* see <https://www.gnu.org/licenses/>.
*/

package com.xemantic.kotlin.swing.demo

import com.xemantic.kotlin.swing.*
import java.awt.Dimension

fun main() = MainWindow("Component Focus") {
val helpBox = Label {
preferredSize = Dimension(200, 0)
}
BorderPanel {
center { helpBox }
west {
BoxPanel(BoxLayoutAxis.Y) {
+Button("action") {
focusGains.listen {
helpBox.text = "Action help"
}
}
+CheckBox("choice") {
focusGains.listen {
helpBox.text = "Choice help"
}
}
+Label("label") {
focusGains.listen {
helpBox.text = "Label help"
}
}
}
}
}
}
32 changes: 32 additions & 0 deletions xemantic-kotlin-swing-dsl-core/src/main/kotlin/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ val Component.mouseMoves: Flow<MouseEvent> get() =
val Component.mouseClicks: Flow<MouseEvent> get() =
mouseEvents.filter { it.id == MouseEvent.MOUSE_CLICKED }

val Component.mouseDrags: Flow<MouseEvent> get() =
mouseEvents.filter { it.id == MouseEvent.MOUSE_DRAGGED }

val Component.mousePresses: Flow<MouseEvent> get() =
mouseEvents.filter { it.id == MouseEvent.MOUSE_PRESSED }

val Component.mouseReleases: Flow<MouseEvent> get() =
mouseEvents.filter { it.id == MouseEvent.MOUSE_RELEASED }

val Component.mouseEnters: Flow<MouseEvent> get() =
mouseEvents.filter { it.id == MouseEvent.MOUSE_ENTERED }

val Component.mouseExits: Flow<MouseEvent> get() =
mouseEvents.filter { it.id == MouseEvent.MOUSE_EXITED }

val Component.focusEvents: Flow<FocusEvent> get() = callbackFlow {
val listener = object : FocusListener {
override fun focusGained(e: FocusEvent) { trySend(e) }
override fun focusLost(e: FocusEvent) { trySend(e) }
}
addFocusListener(listener)
awaitClose {
removeFocusListener(listener)
}
}

val Component.focusGains: Flow<FocusEvent> get() =
focusEvents.filter { it.id == FocusEvent.FOCUS_GAINED }

val Component.focusLosses: Flow<FocusEvent> get() =
focusEvents.filter { it.id == FocusEvent.FOCUS_LOST }

val JTextField.actionEvents: Flow<ActionEvent> get() = callbackFlow {
val listener = ActionListener { e -> trySend(e) }
addActionListener(listener)
Expand Down

0 comments on commit 6943ccb

Please sign in to comment.