From 6943ccb9a186b2e5109081e4a370ba1988f929d2 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Mon, 26 Aug 2024 12:04:25 +0200 Subject: [PATCH] additional mouse events and focus events provided as Flows --- .../src/test/kotlin/BrowserPresenterTest.kt | 2 +- .../src/main/kotlin/FocusDemo.kt | 52 +++++++++++++++++++ .../src/main/kotlin/Events.kt | 32 ++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 demo/simple-kotlin-dsl/src/main/kotlin/FocusDemo.kt diff --git a/demo/mvp-presenter/src/test/kotlin/BrowserPresenterTest.kt b/demo/mvp-presenter/src/test/kotlin/BrowserPresenterTest.kt index 5ba2fe0..69ffb43 100644 --- a/demo/mvp-presenter/src/test/kotlin/BrowserPresenterTest.kt +++ b/demo/mvp-presenter/src/test/kotlin/BrowserPresenterTest.kt @@ -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 diff --git a/demo/simple-kotlin-dsl/src/main/kotlin/FocusDemo.kt b/demo/simple-kotlin-dsl/src/main/kotlin/FocusDemo.kt new file mode 100644 index 0000000..9055f9e --- /dev/null +++ b/demo/simple-kotlin-dsl/src/main/kotlin/FocusDemo.kt @@ -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 . + */ + +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" + } + } + } + } + } +} diff --git a/xemantic-kotlin-swing-dsl-core/src/main/kotlin/Events.kt b/xemantic-kotlin-swing-dsl-core/src/main/kotlin/Events.kt index b247f7d..b8410db 100644 --- a/xemantic-kotlin-swing-dsl-core/src/main/kotlin/Events.kt +++ b/xemantic-kotlin-swing-dsl-core/src/main/kotlin/Events.kt @@ -68,6 +68,38 @@ val Component.mouseMoves: Flow get() = val Component.mouseClicks: Flow get() = mouseEvents.filter { it.id == MouseEvent.MOUSE_CLICKED } +val Component.mouseDrags: Flow get() = + mouseEvents.filter { it.id == MouseEvent.MOUSE_DRAGGED } + +val Component.mousePresses: Flow get() = + mouseEvents.filter { it.id == MouseEvent.MOUSE_PRESSED } + +val Component.mouseReleases: Flow get() = + mouseEvents.filter { it.id == MouseEvent.MOUSE_RELEASED } + +val Component.mouseEnters: Flow get() = + mouseEvents.filter { it.id == MouseEvent.MOUSE_ENTERED } + +val Component.mouseExits: Flow get() = + mouseEvents.filter { it.id == MouseEvent.MOUSE_EXITED } + +val Component.focusEvents: Flow 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 get() = + focusEvents.filter { it.id == FocusEvent.FOCUS_GAINED } + +val Component.focusLosses: Flow get() = + focusEvents.filter { it.id == FocusEvent.FOCUS_LOST } + val JTextField.actionEvents: Flow get() = callbackFlow { val listener = ActionListener { e -> trySend(e) } addActionListener(listener)