Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional mouse events and focus events provided as Flows #6

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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
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
@@ -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)