Skip to content

Commit

Permalink
Merge pull request #19 from marad/bug_fixes
Browse files Browse the repository at this point in the history
* fixed unfocusing of non-managed window
* it's no longer possible to move window to currently active view so window is not disappearing into oblivion
* fixed race condition on event processing - async events produced by the system are now processed in order in separate thread which should fix some weird behavior
  • Loading branch information
marad authored Nov 21, 2023
2 parents 765a48b + 34bd1c7 commit 03f3de8
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repositories {
}

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("net.java.dev.jna:jna-platform:5.13.0")
implementation("org.greenrobot:eventbus-java:3.3.1")
implementation("com.melloware:jintellitype:1.4.1")
Expand Down
20 changes: 18 additions & 2 deletions src/main/kotlin/gh/marad/tiler/app/internal/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import gh.marad.tiler.actions.ActionsFacade
import gh.marad.tiler.actions.ReloadConfig
import gh.marad.tiler.app.AppFacade
import gh.marad.tiler.common.BroadcastingEventHandler
import gh.marad.tiler.common.TilerCommand
import gh.marad.tiler.config.ConfigFacade
import gh.marad.tiler.config.Hotkey
import gh.marad.tiler.os.OsFacade
import gh.marad.tiler.tiler.TilerFacade
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import java.awt.MenuItem
import java.awt.SystemTray
Expand All @@ -24,11 +29,22 @@ class App(val config: ConfigFacade, val os: OsFacade, val tiler: TilerFacade, va
actions.registerActionListener(ActionHandler(this, os, tiler))
val executor = TilerCommandsExecutorAndWatcher(os, config.getFilteringRules())
executor.execute(tiler.initializeWithOpenWindows())
val commandChannel = Channel<List<TilerCommand>>(100)
val evenHandler = BroadcastingEventHandler(
TilerWindowEventHandler(tiler, config.getFilteringRules(), os, executor),
TilerWindowEventHandler(tiler, config.getFilteringRules(), os, commandChannel),
RestoreWindowsOnExitEventHandler(os)
)
os.startEventHandling(evenHandler)

runBlocking {
launch {
for (commands in commandChannel) {
executor.execute(commands)
}
}
os.startEventHandling(evenHandler)
commandChannel.close()
coroutineContext.cancelChildren()
}
}

override fun reloadConfig() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
package gh.marad.tiler.app.internal

import gh.marad.tiler.common.TilerCommand
import gh.marad.tiler.common.filteringrules.FilteringRules
import gh.marad.tiler.os.OsFacade
import gh.marad.tiler.common.Window
import gh.marad.tiler.os.WindowEventHandler
import gh.marad.tiler.tiler.TilerFacade
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.runBlocking

class TilerWindowEventHandler(
private val tiler: TilerFacade,
private val filteringRules: FilteringRules,
private val os: OsFacade,
private val executor: TilerCommandsExecutorAndWatcher
private val commandChannel: Channel<List<TilerCommand>>
): WindowEventHandler {
override fun windowActivated(window: Window) {
override fun windowActivated(window: Window) = runBlocking {
if (filteringRules.shouldManage(window)) {
executor.execute(tiler.addWindow(window))
commandChannel.send(tiler.addWindow(window))
}
}

override fun windowAppeared(window: Window) {
override fun windowAppeared(window: Window) = runBlocking {
if (filteringRules.shouldManage(window)) {
executor.execute(tiler.addWindow(window))
commandChannel.send(tiler.addWindow(window))
}
}

override fun windowDisappeared(window: Window) {
executor.execute(tiler.removeWindow(window))
override fun windowDisappeared(window: Window) = runBlocking {
commandChannel.send(tiler.removeWindow(window))
}

override fun windowMinimized(window: Window) {
executor.execute(tiler.removeWindow(window))
override fun windowMinimized(window: Window) = runBlocking {
commandChannel.send(tiler.removeWindow(window))
}

override fun windowRestored(window: Window) {
override fun windowRestored(window: Window) = runBlocking {
if (filteringRules.shouldManage(window)) {
executor.execute(tiler.addWindow(window))
commandChannel.send(tiler.addWindow(window))
}
}

override fun windowMovedOrResized(window: Window) {
if (!filteringRules.shouldManage(window)) return
val foundWindow = os.windowsUnderCursor().lastOrNull { it.isVisible }
if (foundWindow != null && foundWindow.id != window.id) {
executor.execute(tiler.swapWindows(window, foundWindow))
} else {
executor.execute(tiler.retile())
override fun windowMovedOrResized(window: Window) = runBlocking {
if (filteringRules.shouldManage(window)) {
val foundWindow = os.windowsUnderCursor().lastOrNull { it.isVisible }
if (foundWindow != null && foundWindow.id != window.id) {
commandChannel.send(tiler.swapWindows(window, foundWindow))
} else {
commandChannel.send(tiler.retile())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/gh/marad/tiler/common/TilerCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ data class SetWindowPosition(val windowId: WindowId, val position: WindowPositio
/**
* Minimizes given window
*/
data class MinimizeWindow(val windowId: WindowId) : TilerCommand
data class HideWindow(val windowId: WindowId) : TilerCommand
data class ShowWindow(val windowId: WindowId) : TilerCommand
data class ActivateWindow(val windowId: WindowId) : TilerCommand
2 changes: 1 addition & 1 deletion src/main/kotlin/gh/marad/tiler/os/internal/WindowsOs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class WindowsOs : OsFacade {
User32.INSTANCE.SetWindowPlacement(hwnd, placement)
}

is MinimizeWindow -> {
is HideWindow -> {
val hwnd = (command.windowId as WID).handle
User32.INSTANCE.ShowWindow(hwnd, User32.SW_HIDE)
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/gh/marad/tiler/tiler/internal/Tiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ class Tiler(
override fun removeWindow(window: Window): List<TilerCommand> {
viewManager.currentView().removeWindow(window.id)
val windowToActivate = viewManager.currentView().windowToActivate()
return if (windowToActivate != null) {
return if (!viewManager.currentView().hasWindow(os.activeWindow().id) && windowToActivate != null) {
retile() + ActivateWindow(windowToActivate)
} else {
retile()
}
}

override fun moveWindow(window: TilerWindow, viewId: Int): List<TilerCommand> {
if (!enabled) return emptyList()
if (!enabled || viewId == viewManager.currentViewId) return emptyList()
viewManager.moveWindow(window.id, viewId)
val windowToActivate = viewManager.currentView().windowToActivate()
return if (windowToActivate != null) {
(listOf(MinimizeWindow(window.id), ActivateWindow(windowToActivate)) + retile())
return if (!viewManager.currentView().hasWindow(os.activeWindow().id) && windowToActivate != null) {
(listOf(HideWindow(window.id), ActivateWindow(windowToActivate)) + retile())
} else {
(listOf(MinimizeWindow(window.id)) + retile())
(listOf(HideWindow(window.id)) + retile())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ViewManager(private val defaultLayout: () -> Layout) {
private var _activeViewId: Int = 0
private val _views = mutableMapOf(0 to View(layout = defaultLayout()))

val currentViewId: Int
get() = _activeViewId

@Suppress("MemberVisibilityCanBePrivate")
fun getView(viewId: Int): View = _views.getOrPut(viewId) { View(layout = defaultLayout()) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ViewSwitcher(private val viewManager: ViewManager, private val filteringRu
.map { ShowWindow(it.id) }
val minimizeCommands = view.filterWindowsNotInView(desktopState.getManagableWindows(filteringRules))
.filterNot { it.isMinimized || !it.isVisible }
.map { MinimizeWindow(it.id) }
.map { HideWindow(it.id) }
val windowToActivate = view.windowToActivate()

return (minimizeCommands + showCommands)
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/gr/marad/tiler/GraphicalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.kotest.property.Arb
import io.kotest.property.arbitrary.arbitrary
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.next
import kotlinx.coroutines.channels.Channel
import org.jetbrains.skija.*
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL
Expand Down Expand Up @@ -84,7 +85,7 @@ val os = object : OsFacade {
}
}
}
is MinimizeWindow -> {
is HideWindow -> {
desktopWindows.replaceAll {
if (it.window.id == cmd.windowId) {
it.copy(minimized = true)
Expand All @@ -105,8 +106,7 @@ val os = object : OsFacade {
}
val config = ConfigFacade.createConfig(os)
val tiler = TilerFacade.createTiler(config, os)
val executor = TilerCommandsExecutorAndWatcher(os, filteringRules)
val eventHandler = TilerWindowEventHandler(tiler, filteringRules, os, executor)
val eventHandler = TilerWindowEventHandler(tiler, filteringRules, os, Channel())

val xGen2 = Arb.int(0, width -10)
val yGen2 = Arb.int(0, height -10)
Expand Down

0 comments on commit 03f3de8

Please sign in to comment.