{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Miso.Native.X.Element.Input.Method
(
focus
, blur
, setValue
, setSelectionRange
, getValue
, InputValue (..)
) where
import Miso hiding (focus, blur, setValue, setSelectionRange)
import Miso.Native.FFI (invokeExec)
data InputValue
= InputValue
{ InputValue -> MisoString
value :: MisoString
, InputValue -> Int
selectionStart :: Int
, InputValue -> Int
selectionEnd :: Int
, InputValue -> Bool
isComposing :: Bool
} deriving (Int -> InputValue -> ShowS
[InputValue] -> ShowS
InputValue -> String
(Int -> InputValue -> ShowS)
-> (InputValue -> String)
-> ([InputValue] -> ShowS)
-> Show InputValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputValue -> ShowS
showsPrec :: Int -> InputValue -> ShowS
$cshow :: InputValue -> String
show :: InputValue -> String
$cshowList :: [InputValue] -> ShowS
showList :: [InputValue] -> ShowS
Show, InputValue -> InputValue -> Bool
(InputValue -> InputValue -> Bool)
-> (InputValue -> InputValue -> Bool) -> Eq InputValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputValue -> InputValue -> Bool
== :: InputValue -> InputValue -> Bool
$c/= :: InputValue -> InputValue -> Bool
/= :: InputValue -> InputValue -> Bool
Eq)
instance FromJSVal InputValue where
fromJSVal :: JSVal -> IO (Maybe InputValue)
fromJSVal JSVal
o = do
let readProp :: MisoString -> IO b
readProp MisoString
name = JSVal -> IO b
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked (JSVal -> IO b) -> IO JSVal -> IO b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal
o JSVal -> MisoString -> IO JSVal
forall o. ToObject o => o -> MisoString -> IO JSVal
! (MisoString
name :: MisoString)
value <- MisoString -> IO MisoString
forall {b}. FromJSVal b => MisoString -> IO b
readProp MisoString
"value"
selectionStart <- readProp "selectionStart"
selectionEnd <- readProp "selectionEnd"
isComposing <- readProp "isComposing"
pure $ Just InputValue {..}
newtype SetValue = SetValue MisoString
instance ToJSVal SetValue where
toJSVal :: SetValue -> IO JSVal
toJSVal (SetValue MisoString
v) = do
o <- IO Object
create
set "value" v o
toJSVal o
data SetSelectionRange = SetSelectionRange Int Int
instance ToJSVal SetSelectionRange where
toJSVal :: SetSelectionRange -> IO JSVal
toJSVal (SetSelectionRange Int
s Int
e) = do
o <- IO Object
create
set "selectionStart" s o
set "selectionEnd" e o
toJSVal o
focus
:: MisoString
-> action
-> (MisoString -> action)
-> Effect context props model action
focus :: forall action context props model.
MisoString
-> action
-> (MisoString -> action)
-> Effect context props model action
focus MisoString
selector action
action = MisoString
-> MisoString
-> ()
-> (() -> action)
-> (MisoString -> action)
-> Effect context props model action
forall params argument action context props model.
(ToJSVal params, FromJSVal argument) =>
MisoString
-> MisoString
-> params
-> (argument -> action)
-> (MisoString -> action)
-> Effect context props model action
invokeExec MisoString
"focus" MisoString
selector () (\() -> action
action)
blur
:: MisoString
-> action
-> (MisoString -> action)
-> Effect context props model action
blur :: forall action context props model.
MisoString
-> action
-> (MisoString -> action)
-> Effect context props model action
blur MisoString
selector action
action = MisoString
-> MisoString
-> ()
-> (() -> action)
-> (MisoString -> action)
-> Effect context props model action
forall params argument action context props model.
(ToJSVal params, FromJSVal argument) =>
MisoString
-> MisoString
-> params
-> (argument -> action)
-> (MisoString -> action)
-> Effect context props model action
invokeExec MisoString
"blur" MisoString
selector () (\() -> action
action)
setValue
:: MisoString
-> MisoString
-> action
-> (MisoString -> action)
-> Effect context props model action
setValue :: forall action context props model.
MisoString
-> MisoString
-> action
-> (MisoString -> action)
-> Effect context props model action
setValue MisoString
selector MisoString
v action
action =
MisoString
-> MisoString
-> SetValue
-> (() -> action)
-> (MisoString -> action)
-> Effect context props model action
forall params argument action context props model.
(ToJSVal params, FromJSVal argument) =>
MisoString
-> MisoString
-> params
-> (argument -> action)
-> (MisoString -> action)
-> Effect context props model action
invokeExec MisoString
"setValue" MisoString
selector (MisoString -> SetValue
SetValue MisoString
v) (\() -> action
action)
setSelectionRange
:: MisoString
-> Int
-> Int
-> action
-> (MisoString -> action)
-> Effect context props model action
setSelectionRange :: forall action context props model.
MisoString
-> Int
-> Int
-> action
-> (MisoString -> action)
-> Effect context props model action
setSelectionRange MisoString
selector Int
s Int
e action
action =
MisoString
-> MisoString
-> SetSelectionRange
-> (() -> action)
-> (MisoString -> action)
-> Effect context props model action
forall params argument action context props model.
(ToJSVal params, FromJSVal argument) =>
MisoString
-> MisoString
-> params
-> (argument -> action)
-> (MisoString -> action)
-> Effect context props model action
invokeExec MisoString
"setSelectionRange" MisoString
selector (Int -> Int -> SetSelectionRange
SetSelectionRange Int
s Int
e) (\() -> action
action)
getValue
:: MisoString
-> (InputValue -> action)
-> (MisoString -> action)
-> Effect context props model action
getValue :: forall action context props model.
MisoString
-> (InputValue -> action)
-> (MisoString -> action)
-> Effect context props model action
getValue MisoString
selector = MisoString
-> MisoString
-> ()
-> (InputValue -> action)
-> (MisoString -> action)
-> Effect context props model action
forall params argument action context props model.
(ToJSVal params, FromJSVal argument) =>
MisoString
-> MisoString
-> params
-> (argument -> action)
-> (MisoString -> action)
-> Effect context props model action
invokeExec MisoString
"getValue" MisoString
selector ()