Skip to content

Commit

Permalink
additional mouse events and focus events provided as Flows (#6)
Browse files Browse the repository at this point in the history
* additional mouse events and focus events provided as Flows
* build-branch github action
  • Loading branch information
morisil authored Aug 26, 2024
1 parent 34016d3 commit 4e2683b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .github/workflows/build-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build branch
on:
push:
branches-ignore:
- main
pull_request:
branches-ignore:
- main
jobs:
build_main:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/[email protected]
- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 22
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
- name: Build
run: ./gradlew build
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 4e2683b

Please sign in to comment.