Skip to content

Commit

Permalink
Refactor BindProp type class
Browse files Browse the repository at this point in the history
  • Loading branch information
postsolar committed Feb 24, 2024
1 parent 679ce41 commit 1624390
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/AGS/Binding.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const unsafeBindPropImpl =
export const unsafeBindProp =
prop => object =>
object.bind(prop)

Expand Down
19 changes: 12 additions & 7 deletions src/AGS/Binding.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module AGS.Binding
( Binding
, class BindProp
, bindProp
, unsafeBindProp
, ValueOrBinding
, overValue
, overBinding
Expand All @@ -14,6 +13,7 @@ import Prelude
import Control.Apply (lift2)
import Data.Either (either)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Prim.Row as R
import Type.Proxy (Proxy(..))
import Untagged.TypeCheck (class HasRuntimeType)
import Untagged.Union (OneOf, asOneOf, toEither1)
Expand Down Expand Up @@ -56,14 +56,19 @@ foreign import applyBinding ∷ ∀ a b. Binding (a → b) → Binding a → Bin
foreign import pureBinding a. a Binding a
foreign import bindBinding a b. Binding a (a Binding b) Binding b

class BindProp k. Type k Type Constraint
class BindProp o p t | o p t where
bindProp o Binding t
class BindPropType Row Type Constraint
class BindProp object props | object props

unsafeBindProp @p @o @t. IsSymbol p BindProp o p t o Binding t
unsafeBindProp = unsafeBindPropImpl (reflectSymbol (Proxy @p))
bindProp
@prop @obj ty props rt
. R.Cons prop ty rt props
IsSymbol prop
BindProp obj props
obj
Binding ty
bindProp = unsafeBindProp (reflectSymbol (Proxy @prop))

foreign import unsafeBindPropImpl o t. String o Binding t
foreign import unsafeBindProp o t. String o Binding t

-- * ValueOrBinding

Expand Down
96 changes: 32 additions & 64 deletions src/AGS/Service/Mpris.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module AGS.Service.Mpris
, matchPlayer
, BusName
, Player
, PlayerProps
, PlayerRecord
, PlayerRecordR
, MprisMetadata
Expand All @@ -23,7 +24,7 @@ module AGS.Service.Mpris

import Prelude

import AGS.Binding (class BindProp, Binding, unsafeBindProp)
import AGS.Binding (class BindProp, Binding)
import AGS.Service (class BindServiceProp, class ServiceConnect, Service)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
Expand Down Expand Up @@ -152,70 +153,37 @@ fromPlayer = unsafeCopyGObjectProps @PlayerRecordR
fromForeignMetadata MprisMetadataF UndefinedOr MprisMetadata
fromForeignMetadata = mapRecordKind uorToMaybe

-- the dbus name that starts with org.mpris.MediaPlayer2
instance BindProp Player "bus-name" String where
bindProp o = unsafeBindProp @"bus-name" o

-- stripped from busName like spotify or firefox
instance BindProp Player "name" String where
bindProp o = unsafeBindProp @"name" o

-- name of the player like Spotify or Mozilla Firefox
instance BindProp Player "identity" String where
bindProp o = unsafeBindProp @"identity" o

-- .desktop entry without the extension
instance BindProp Player "entry" String where
bindProp o = unsafeBindProp @"entry" o

instance BindProp Player "trackid" String where
bindProp o = unsafeBindProp @"trackid" o

instance BindProp Player "track-artists" (Array String) where
bindProp o = unsafeBindProp @"track-artists" o

instance BindProp Player "track-album" String where
bindProp o = unsafeBindProp @"track-album" o

instance BindProp Player "track-title" String where
bindProp o = unsafeBindProp @"track-title" o

instance BindProp Player "track-cover-url" String where
bindProp o = unsafeBindProp @"track-cover-url" o

-- path to the cached cover art
instance BindProp Player "cover-path" String where
bindProp o = unsafeBindProp @"cover-path" o

-- "Playing" | "Paused" | "Stopped"
instance BindProp Player "play-back-status" String where
bindProp o = unsafeBindProp @"play-back-status" o

instance BindProp Player "can-go-next" Boolean where
bindProp o = unsafeBindProp @"can-go-next" o

instance BindProp Player "can-go-prev" Boolean where
bindProp o = unsafeBindProp @"can-go-prev" o

instance BindProp Player "can-play" Boolean where
bindProp o = unsafeBindProp @"can-play" o

-- null if shuffle is unsupported by the player
instance BindProp Player "shuffle-status" (Nullable Boolean) where
bindProp o = unsafeBindProp @"shuffle-status" o

-- "None" | "Track" | "Playlist" | null if shuffle is unsupported by the player
instance BindProp Player "loop-status" (Nullable String) where
bindProp o = unsafeBindProp @"loop-status" o

instance BindProp Player "volume" Number where
bindProp o = unsafeBindProp @"volume" o

instance BindProp Player "length" Number where
bindProp o = unsafeBindProp @"length" o
type PlayerProps =
-- the dbus name that starts with org.mpris.MediaPlayer2
( "bus-name" ∷ String
-- stripped from busName like spotify or firefox
, nameString
-- name of the player like Spotify or Mozilla Firefox
, identityString
-- .desktop entry without the extension
, entryString
, trackidString
, "track-artists" ∷ Array String
, "track-album" ∷ String
, "track-title" ∷ String
, "track-cover-url" ∷ String
-- path to the cached cover art
, "cover-path" ∷ String
-- "Playing" | "Paused" | "Stopped"
, "play-back-status" ∷ String
, "can-go-next" ∷ Boolean
, "can-go-prev" ∷ Boolean
, "can-play" ∷ Boolean
-- null if shuffle is unsupported by the player
, "shuffle-status" ∷ Nullable Boolean
-- "None" | "Track" | "Playlist" | null if shuffle is unsupported by the player
, "loop-status" ∷ Nullable String
, volumeNumber
, lengthNumber
, positionNumber
)

instance BindProp Player "position" Number where
bindProp o = unsafeBindProp @"position" o
instance BindProp Player PlayerProps

-- * Signals

Expand Down
4 changes: 0 additions & 4 deletions src/AGS/Variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ export const set =
f => v => () =>
v.setValue(f(v.value))

export const bindValue =
v =>
v.bind("value")

export const store =
v =>
Variable(v)
Expand Down
12 changes: 10 additions & 2 deletions src/AGS/Variable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module AGS.Variable

import Prelude

import AGS.Binding (Binding)
import AGS.Binding (class BindProp, Binding, bindProp)
import Data.Time.Duration (Milliseconds)
import Effect (Effect)
import Effect.Uncurried (EffectFn1)
Expand All @@ -32,8 +32,16 @@ foreign import get ∷ ∀ a. Variable a → Effect a
-- | Update the value of a variable.
foreign import set a. (a a) Variable a Effect Unit

type VariablePropsType Row Type
type VariableProps a =
( valuea
)

instance BindProp (Variable a) (VariableProps a)

-- | Bind the value of a variable.
foreign import bindValue a. Variable a Binding a
bindValue a. Variable a Binding a
bindValue = bindProp @"value"

-- | Create a variable.
foreign import store a. a Variable a
Expand Down

0 comments on commit 1624390

Please sign in to comment.