module Engine.Window.Drop
  ( Callback
  , callback

  , mkCallback
  ) where

import RIO

import Graphics.UI.GLFW qualified as GLFW
import RIO.App (appEnv)
import UnliftIO.Resource (ReleaseKey)
import UnliftIO.Resource qualified as Resource

import Engine.Events.Sink (MonadSink)
import Engine.Types (GlobalHandles(..))

type Callback m = [FilePath] -> m ()

callback :: MonadSink rs m => Callback m -> m ReleaseKey
callback :: forall rs (m :: * -> *).
MonadSink rs m =>
Callback m -> m ReleaseKey
callback Callback m
handler = do
  Window
window <- (App GlobalHandles rs -> Window) -> m Window
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((App GlobalHandles rs -> Window) -> m Window)
-> (App GlobalHandles rs -> Window) -> m Window
forall a b. (a -> b) -> a -> b
$ GlobalHandles -> Window
ghWindow (GlobalHandles -> Window)
-> (App GlobalHandles rs -> GlobalHandles)
-> App GlobalHandles rs
-> Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. App GlobalHandles rs -> GlobalHandles
forall env st. App env st -> env
appEnv
  (UnliftIO m -> IO ()) -> m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(UnliftIO m -> IO a) -> m a
withUnliftIO \UnliftIO m
ul ->
    Window -> Maybe DropCallback -> IO ()
GLFW.setDropCallback Window
window (Maybe DropCallback -> IO ())
-> (DropCallback -> Maybe DropCallback) -> DropCallback -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DropCallback -> Maybe DropCallback
forall a. a -> Maybe a
Just (DropCallback -> IO ()) -> DropCallback -> IO ()
forall a b. (a -> b) -> a -> b
$ UnliftIO m -> Callback m -> DropCallback
forall (m :: * -> *). UnliftIO m -> Callback m -> DropCallback
mkCallback UnliftIO m
ul Callback m
handler
  IO () -> m ReleaseKey
forall (m :: * -> *). MonadResource m => IO () -> m ReleaseKey
Resource.register (IO () -> m ReleaseKey) -> IO () -> m ReleaseKey
forall a b. (a -> b) -> a -> b
$
    Window -> Maybe DropCallback -> IO ()
GLFW.setDropCallback Window
window Maybe DropCallback
forall a. Maybe a
Nothing

mkCallback :: UnliftIO m -> Callback m -> GLFW.DropCallback
mkCallback :: forall (m :: * -> *). UnliftIO m -> Callback m -> DropCallback
mkCallback (UnliftIO forall a. m a -> IO a
ul) Callback m
action =
  \Window
_window [String]
files ->
    m () -> IO ()
forall a. m a -> IO a
ul (m () -> IO ()) -> m () -> IO ()
forall a b. (a -> b) -> a -> b
$ Callback m
action [String]
files