Skip to content

Commit

Permalink
Housekeeping (#94)
Browse files Browse the repository at this point in the history
Cleaning up the top-level state file.

Also fixes:
- Context-menu which initiated drag-n-drop
- Invisible track selection

Adds:
- Info about UI
  • Loading branch information
icidasset authored May 2, 2018
1 parent 381687a commit 5271151
Show file tree
Hide file tree
Showing 24 changed files with 552 additions and 278 deletions.
81 changes: 81 additions & 0 deletions src/App/Alfred/State.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module Alfred.State exposing (..)

import Alfred.System
import Alfred.Types exposing (..)
import Response
import Keyboard.Extra as Keyboard
import Types as TopLevel


-- 💧


initialModel : Model TopLevel.Msg
initialModel =
{ instance = Nothing
}



-- 🔥


update : Msg TopLevel.Msg -> Model TopLevel.Msg -> ( Model TopLevel.Msg, Cmd TopLevel.Msg )
update msg model =
case msg of
Assign instance ->
(!) { model | instance = Just instance } []

CalculateResults searchTerm ->
model.instance
|> Maybe.map (Alfred.System.calculateResults searchTerm)
|> (\a -> { model | instance = a })
|> (\m -> ( m, Cmd.none ))

Hide ->
(!) { model | instance = Nothing } []

RunAction index ->
model.instance
|> Maybe.map (Alfred.System.runAction index)
|> Maybe.map (Tuple.mapFirst <| \a -> { model | instance = Just a })
|> Maybe.withDefault ( model, Cmd.none )

------------------------------------
-- Keyboard (Down)
------------------------------------
KeydownMsg Keyboard.ArrowDown ->
case model.instance of
Just context ->
context
|> (\c -> { c | focus = min (List.length c.results - 1) (c.focus + 1) })
|> (\c -> { model | instance = Just c })
|> Response.withCmd Cmd.none

Nothing ->
(,) model Cmd.none

KeydownMsg Keyboard.ArrowUp ->
case model.instance of
Just context ->
context
|> (\c -> { c | focus = max 0 (c.focus - 1) })
|> (\c -> { model | instance = Just c })
|> Response.withCmd Cmd.none

Nothing ->
(,) model Cmd.none

KeydownMsg _ ->
(,) model Cmd.none



-- 🌱


subscriptions : Model msg -> Sub (Msg msg)
subscriptions _ =
Sub.batch
[ Keyboard.downs KeydownMsg
]
9 changes: 7 additions & 2 deletions src/App/Alfred/System.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module Alfred.System exposing (..)

import List.Extra as List
import Types exposing (Alfred)
import Alfred.Types
import Types as TopLevel


type alias Alfred =
Alfred.Types.Alfred TopLevel.Msg


calculateResults : String -> Alfred -> Alfred
Expand All @@ -28,7 +33,7 @@ calculateResults searchTerm alfred =
}


runAction : Int -> Alfred -> ( Alfred, Cmd Types.Msg )
runAction : Int -> Alfred -> ( Alfred, Cmd TopLevel.Msg )
runAction index alfred =
( alfred
, alfred.results
Expand Down
38 changes: 38 additions & 0 deletions src/App/Alfred/Types.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Alfred.Types exposing (..)

import Keyboard.Extra as Keyboard


-- Messages


type Msg msg
= Assign (Alfred msg)
| CalculateResults String
| Hide
| RunAction Int
-- Keyboard
| KeydownMsg Keyboard.Key



-- Model


type alias Model msg =
{ instance : Maybe (Alfred msg)
}



-- Alfred


type alias Alfred msg =
{ action : Maybe String -> Maybe String -> Cmd msg
, focus : Int
, index : List String
, message : String
, results : List String
, searchTerm : Maybe String
}
12 changes: 8 additions & 4 deletions src/App/Alfred/View.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Alfred.View exposing (entry)

import Alfred.Types as Types exposing (Msg(..))
import Json.Decode as Json
import Types as TopLevel

Expand All @@ -25,11 +26,14 @@ import Styles exposing (Styles(Alfred, Zed))
-- 🍯


entry : TopLevel.Alfred -> Node
entry : Types.Alfred TopLevel.Msg -> Node
entry context =
column
(Alfred Container)
[ onEnterKey (TopLevel.RunAlfredAction context.focus)
[ context.focus
|> RunAction
|> TopLevel.AlfredMsg
|> onEnterKey

--
, center
Expand Down Expand Up @@ -66,7 +70,7 @@ entry context =
[ paddingXY (scaled 1) (scaled 0)
, paddingBottom (scaled -1)
]
{ onChange = TopLevel.CalculateAlfredResults
{ onChange = CalculateResults >> TopLevel.AlfredMsg
, value = ""
, label = placeholder { text = "Type to search", label = hiddenLabel "Search" }
, options = [ focusOnLoad ]
Expand Down Expand Up @@ -110,7 +114,7 @@ resultView : Int -> Int -> String -> Node
resultView focus idx result =
el
(Alfred ResultItem)
[ idx |> TopLevel.RunAlfredAction |> onClick
[ idx |> RunAction |> TopLevel.AlfredMsg |> onClick
, paddingXY (scaled -2) (scaled -5)
, vary Active (focus == idx)
]
Expand Down
44 changes: 44 additions & 0 deletions src/App/ContextMenu/State.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module ContextMenu.State exposing (..)

import ContextMenu.Types exposing (..)
import Playlists.ContextMenu
import Sources.ContextMenu
import Types as TopLevel


-- 💧


initialModel : Model TopLevel.Msg
initialModel =
{ instance = Nothing
}



-- 🔥


update : Msg -> Model TopLevel.Msg -> ( Model TopLevel.Msg, Cmd TopLevel.Msg )
update msg model =
case msg of
Hide ->
(!) { model | instance = Nothing } []

ShowPlaylistMenu playlistName mousePos ->
let
instance =
mousePos
|> Playlists.ContextMenu.listMenu playlistName
|> Just
in
(!) { model | instance = instance } []

ShowSourceMenu sourceId mousePos ->
let
instance =
mousePos
|> Sources.ContextMenu.listMenu sourceId
|> Just
in
(!) { model | instance = instance } []
34 changes: 34 additions & 0 deletions src/App/ContextMenu/Types.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module ContextMenu.Types exposing (..)

import Mouse
import Svg exposing (Svg)


-- Messages


type Msg
= Hide
| ShowPlaylistMenu String Mouse.Position
| ShowSourceMenu String Mouse.Position



-- Model


type alias Model msg =
{ instance : Maybe (ContextMenu msg)
}



-- Context Menu


type ContextMenu msg
= ContextMenu (ContextMenuItems msg) Mouse.Position


type alias ContextMenuItems msg =
List ( Svg msg, String, msg )
20 changes: 9 additions & 11 deletions src/App/ContextMenu/View.elm
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
module ContextMenu.View exposing (entry)

import ContextMenu.Styles exposing (Styles(..))
import ContextMenu.Types exposing (..)
import Element exposing (..)
import Element.Attributes exposing (..)
import Element.Events exposing (onClick, onWithOptions)
import Element.Types exposing (..)
import Json.Decode
import Mouse exposing (Position)
import Styles exposing (Styles(..))
import Svg exposing (Svg)
import Types exposing (ContextMenu, Msg(..))
import Types as TopLevel exposing (Msg(ContextMenuMsg))
import Variables exposing (scaled)
import Variations exposing (Variations)


-- 🍯


entry : Maybe Types.ContextMenu -> Element Styles.Styles Variations Msg
entry : Maybe (ContextMenu TopLevel.Msg) -> Node
entry m =
case m of
Just (Types.ContextMenu items mousePos) ->
Just (ContextMenu.Types.ContextMenu items mousePos) ->
column
(Styles.ContextMenu Container)
(attributes mousePos)
Expand All @@ -33,12 +35,12 @@ entry m =
-- 🐝


attributes : Position -> List (Element.Attribute Variations Msg)
attributes : Position -> List Attr
attributes mousePos =
[ minWidth (px 170)

-- Events
, onWithOptions "click" eventOptions (Json.Decode.succeed NoOp)
, onWithOptions "click" eventOptions (Json.Decode.succeed <| ContextMenuMsg <| Hide)

-- Position
, inlineStyle
Expand All @@ -57,7 +59,7 @@ eventOptions =
}


itemView : ( Svg Msg, String, Msg ) -> Element Styles.Styles Variations Msg
itemView : ( Svg TopLevel.Msg, String, TopLevel.Msg ) -> Node
itemView ( icon, label, msg ) =
row
(Styles.ContextMenu Item)
Expand All @@ -67,11 +69,7 @@ itemView ( icon, label, msg ) =
, verticalCenter

-- Events
, HideContextMenu
|> List.singleton
|> List.append [ msg ]
|> DoAll
|> onClick
, onClick msg
]
[ el WithoutLineHeight [] (html icon)
, text label
Expand Down
Loading

0 comments on commit 5271151

Please sign in to comment.