Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163module UI.Alfred.State exposing (..)
import Alfred exposing (Alfred)import Browser.Dom as Domimport Keyboardimport Processimport Return exposing (return)import Return.Ext as Returnimport Taskimport UI.Types as UI exposing (Manager)
-- 📣
assign : Alfred UI.Msg -> Managerassign instance model = let pressedKeys = List.filter (\k -> case k of Keyboard.Meta -> True
Keyboard.Control -> True
_ -> False ) model.pressedKeys in 250 |> Process.sleep |> Task.andThen (\_ -> Dom.focus "diffuse__alfred") |> Task.andThen (\_ -> Dom.setViewportOf "alfred__results" 0 0) |> Task.attempt (\_ -> UI.Bypass) -- The "K" key seems to stick when using CMD + K, -- aka. Meta key + K, to show the command palette. -- https://github.com/ohanhi/keyboard/issues/14 |> return { model | alfred = Just instance, pressedKeys = pressedKeys }
gotInput : String -> ManagergotInput searchTerm model = model.alfred |> Maybe.map (determineResults searchTerm) |> (\a -> Return.singleton { model | alfred = a })
runAction : Int -> ManagerrunAction index model = case model.alfred of Just instance -> { result = Alfred.getAt index instance , searchTerm = instance.searchTerm } |> instance.action |> List.map Return.task |> Cmd.batch |> return { model | alfred = Nothing }
Nothing -> Return.singleton { model | alfred = Nothing }
runSelectedAction : ManagerrunSelectedAction model = case model.alfred of Just instance -> runAction instance.focus model
Nothing -> Return.singleton model
scrollToFocus : ManagerscrollToFocus model = let task = Task.map3 (\innerE outerE outerV -> outerV.viewport.y + innerE.element.y - outerE.element.y - 9 ) (Dom.getElement "alfred__results__focus") (Dom.getElement "alfred__results") (Dom.getViewportOf "alfred__results") in task |> Task.andThen (\a -> Dom.setViewportOf "alfred__results" 0 a) |> Task.attempt (\_ -> UI.Bypass) |> return model
selectNext : ManagerselectNext model = case model.alfred of Just instance -> let total = Alfred.length instance in instance |> (\i -> { i | focus = min (i.focus + 1) (total - 1) }) |> (\i -> { model | alfred = Just i }) |> scrollToFocus
Nothing -> Return.singleton model
selectPrevious : ManagerselectPrevious model = case model.alfred of Just instance -> instance |> (\i -> { i | focus = max (i.focus - 1) 0 }) |> (\i -> { model | alfred = Just i }) |> scrollToFocus
Nothing -> Return.singleton model
-- ⚗️
determineResults : String -> Alfred UI.Msg -> Alfred UI.MsgdetermineResults searchTerm alfred = let lowerSearchTerm = searchTerm |> String.toLower |> String.trim in if String.length lowerSearchTerm > 0 then { alfred | focus = 0 , searchTerm = Just searchTerm , results = List.map (\group -> group.items |> List.filter (.title >> String.toLower >> String.contains lowerSearchTerm) |> (\items -> { group | items = items } ) ) alfred.index }
else { alfred | focus = 0 , searchTerm = Nothing , results = alfred.index }