Skip to content

Commit

Permalink
Add better support for file associations (#153)
Browse files Browse the repository at this point in the history
Resolves #153
  • Loading branch information
wyskoj committed Dec 22, 2022
1 parent 3e859c1 commit 0424fa9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 37 deletions.
98 changes: 63 additions & 35 deletions src/main/kotlin/org/wysko/midis2jam2/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import com.formdev.flatlaf.FlatDarkLaf
import com.install4j.api.launcher.SplashScreen
import com.install4j.api.launcher.StartupNotification
import org.wysko.midis2jam2.gui.Launcher
import org.wysko.midis2jam2.gui.LauncherController
import org.wysko.midis2jam2.gui.UpdateChecker.checkForUpdates
Expand Down Expand Up @@ -103,46 +104,73 @@ fun main(args: Array<String>) {
}
}

application {
Window(
onCloseRequest = ::exitApplication,
title = "midis2jam2 launcher",
state = rememberWindowState(
placement = WindowPlacement.Maximized,
position = WindowPosition(Alignment.Center)
),
icon = BitmapPainter(useResource("ico/icon32.png", ::loadImageBitmap))
) {
launcherController = Launcher()
this.window.contentPane.dropTarget = object : DropTarget() {
@Synchronized
override fun drop(dtde: DropTargetDropEvent) {
dtde.let {
it.acceptDrop(DnDConstants.ACTION_REFERENCE)
(it.transferable.getTransferData(DataFlavor.javaFileListFlavor) as List<*>).firstOrNull()
?.let { file -> launcherController?.setSelectedFile?.invoke(file as File) }
if (args.isNotEmpty()) {
Execution.start(
properties = Properties().apply {
setProperty("midi_file", args.first())
setProperty("midi_device", launcherState.getProperty("midi_device"))
},
onStart = {
launcherController?.setFreeze?.invoke(true)
MIDISearchFrame.lock()
},
onReady = {},
onFinish = {
launcherController?.setFreeze?.invoke(false)
MIDISearchFrame.unlock()
}
)
SplashScreen.hide()
} else {
application {
Window(
onCloseRequest = ::exitApplication,
title = "midis2jam2 launcher",
state = rememberWindowState(
placement = WindowPlacement.Maximized,
position = WindowPosition(Alignment.Center)
),
icon = BitmapPainter(useResource("ico/icon32.png", ::loadImageBitmap))
) {
launcherController = Launcher()
this.window.contentPane.dropTarget = object : DropTarget() {
@Synchronized
override fun drop(dtde: DropTargetDropEvent) {
dtde.let {
it.acceptDrop(DnDConstants.ACTION_REFERENCE)
(it.transferable.getTransferData(DataFlavor.javaFileListFlavor) as List<*>).firstOrNull()
?.let { file -> launcherController?.setSelectedFile?.invoke(file as File) }
}
}
}
}
}
if (args.isNotEmpty()) {
Execution.start(
properties = Properties().apply {
setProperty("midi_file", args.first())
setProperty("midi_device", launcherState.getProperty("midi_device"))
},
onStart = {
launcherController?.setFreeze?.invoke(true)
MIDISearchFrame.lock()
},
onReady = {},
onFinish = {
launcherController?.setFreeze?.invoke(false)
MIDISearchFrame.unlock()
checkForUpdates() // I'm checking for updates, whether you like it or not.
/* Register subsequent invocations */
StartupNotification.registerStartupListener {
if (launcherController?.getFreeze?.invoke() != true) {
Execution.start(
properties = Properties().apply {
setProperty(
"midi_file",
it.run {
if (it.startsWith('"') && it.endsWith('"')) drop(1).dropLast(1) else this
}
)
setProperty("midi_device", launcherState.getProperty("midi_device"))
},
onStart = {
launcherController?.setFreeze?.invoke(true)
MIDISearchFrame.lock()
},
onReady = {},
onFinish = {
launcherController?.setFreeze?.invoke(false)
MIDISearchFrame.unlock()
}
)
}
)
}
}
checkForUpdates() // I'm checking for updates, whether you like it or not.
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/org/wysko/midis2jam2/gui/Launcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,16 @@ fun Launcher(): LauncherController {
{ file: File ->
selectedMIDIFile = file
midiFileTextField?.invoke(file)
}
},
{ freeze }
)
}

/** Provides a way for the launcher component to allow external modifications. */
data class LauncherController(
internal val setFreeze: (setFreeze: Boolean) -> Unit,
internal val setSelectedFile: (file: File) -> Unit
internal val setSelectedFile: (file: File) -> Unit,
internal val getFreeze: () -> Boolean
)

/**
Expand Down

0 comments on commit 0424fa9

Please sign in to comment.