Przeglądaj źródła

feat: double click on a tab open a tab after it

Jocelyn Boullier 5 lat temu
rodzic
commit
360161e7ad
3 zmienionych plików z 26 dodań i 18 usunięć
  1. 20 13
      src/Background.purs
  2. 1 1
      src/Model.purs
  3. 5 4
      src/Sidebar/Components/Tabs.purs

+ 20 - 13
src/Background.purs

@@ -17,7 +17,8 @@ import Control.Alt (map, (<#>), (<$>), (<|>))
 import Control.Alternative (empty, pure, (*>))
 import Control.Bind ((=<<), (>>=))
 import Control.Category (identity, (>>>))
-import Data.Array (catMaybes, deleteAt, filter, foldl, fromFoldable, insertAt, mapWithIndex, (!!))
+import Data.Array as A
+import Data.CommutativeRing ((+))
 import Data.Eq ((/=), (==))
 import Data.Foldable (for_)
 import Data.Function (const, flip, (#))
@@ -101,7 +102,7 @@ onTabCreated stateRef (Tab tab) = do
     updateWindow win =
       -- this will delete the window if there is an issue with the position..
       -- not the best solution but we can't really recover from it anyway.
-      (insertAt t.index t.id win.positions)
+      (A.insertAt t.index t.id win.positions)
         <#> \newPos ->
             win
               { positions = newPos
@@ -120,7 +121,8 @@ onTabUpdated stateRef tid cinfo tab' = do
   updateTab (Tab t) =
     -- update by replacing the tab only if it already exists
     (over (_tabFromWindow (Tab t)) (map $ const (Tab t)))
-    -- or update the currently detached tab
+      -- or update the currently detached tab
+      
       >>> ( \s -> case s.detached of
             Just (Tab t')
               | t.id == t'.id -> s { detached = Just (Tab t') }
@@ -158,18 +160,17 @@ onTabMoved ref tid minfo = do
 
   moveElement :: forall a. Int -> Int -> Array a -> Maybe (Array a)
   moveElement from to arr = do
-    tab <- arr !! from
-    deleteAt from arr >>= insertAt to tab
+    tab <- arr A.!! from
+    A.deleteAt from arr >>= A.insertAt to tab
 
   -- | update the index of the tab given the positions
   updateTabsIndex :: Array TabId -> M.Map TabId Tab -> M.Map TabId Tab
   updateTabsIndex positions tabs =
     let
       modifyFuncs :: Array (M.Map TabId Tab -> M.Map TabId Tab)
-      modifyFuncs = mapWithIndex (\idx tid' -> set (at tid' <<< _Just <<< _Newtype <<< _index) idx) positions
+      modifyFuncs = A.mapWithIndex (\idx tid' -> set (at tid' <<< _Just <<< _Newtype <<< _index) idx) positions
     in
-      foldl (#) tabs modifyFuncs
-
+      A.foldl (#) tabs modifyFuncs
 
 onTabActived :: (Ref.Ref GlobalState) -> OnActivated.ActiveInfo -> Effect Unit
 onTabActived stateRef (OnActivated.ActiveInfo aInfo) = do
@@ -201,7 +202,7 @@ onTabActived stateRef (OnActivated.ActiveInfo aInfo) = do
 stateDeleteTab :: WindowId -> TabId -> GlobalState -> GlobalState
 stateDeleteTab wid tid =
   ( (set (_windowIdToTabIdToTab wid tid) Nothing)
-      >>> over (_windowIdToWindow wid <<< _positions) (filter ((/=) tid))
+      >>> over (_windowIdToWindow wid <<< _positions) (A.filter ((/=) tid))
   )
 
 deleteTab :: (Ref.Ref GlobalState) -> WindowId -> TabId -> Effect Unit
@@ -275,10 +276,10 @@ onNewWindowId port stateRef listenerRef winId = do
     ( \w -> do
         Runtime.postMessageJson port
           $ BgInitialTabList
-          $ fromFoldable
+          $ A.fromFoldable
           $ w.positions
           <#> (flip M.lookup w.tabs)
-          # catMaybes
+          # A.catMaybes
     )
     (M.lookup winId latestState.windows)
   --  add the new onMessage listener
@@ -299,11 +300,17 @@ initWindowState port ref winId =
 -- TODO don't pass the full ref, but only a set of function to manipulate/access 
 -- the data required
 manageSidebar :: (Ref.Ref GlobalState) -> WindowId -> Runtime.Port -> SidebarEvent -> Effect Unit
-manageSidebar _ winId port = case _ of 
+manageSidebar ref winId port = case _ of
   SbDeleteTab tabId -> launchAff_ $ removeOne tabId
   SbActivateTab tabId -> launchAff_ $ activateTab tabId
   SbMoveTab tabId newIndex -> moveTab tabId { index: newIndex }
-  SbCreateTab -> createTab { windowId: winId }
+  SbCreateTab tid' -> case tid' of
+    Nothing -> createTab { windowId: winId }
+    Just tid ->
+      Ref.read ref <#> view (_positions >>> _windowIdToWindow winId)
+        >>= \positions -> case A.elemIndex tid positions of
+            Nothing -> createTab { windowId: winId }
+            Just idx -> createTab { windowId: winId, index: idx + 1 }
   _ -> pure unit
 
 onDisconnect :: forall a. (Ref.Ref GlobalState) -> WindowId -> Listener a -> Effect Unit

+ 1 - 1
src/Model.purs

@@ -174,7 +174,7 @@ instance showBackgroundEvent :: Show BackgroundEvent where
 data SidebarEvent
   = SbDeleteTab TabId
   | SbActivateTab TabId
-  | SbCreateTab
+  | SbCreateTab (Maybe TabId)
   | SbMoveTab TabId Int
   | SbDetacheTab
   | SbCreatedGroup

+ 5 - 4
src/Sidebar/Components/Tabs.purs

@@ -42,7 +42,7 @@ data Query a
 data Action
   = UserClosedTab TabId Event
   | UserActivatedTab TabId Event
-  | UserOpenedTab Event
+  | UserOpenedTab (Maybe TabId) Event
   -- drags
   | TabDragStart DE.DragEvent Tab Int
   | TabDragOver DE.DragEvent Int
@@ -95,7 +95,7 @@ render state =
   in
     HH.div
       [ HP.id_ "tabs"
-      , HE.onDoubleClick (\ev -> Just (UserOpenedTab $ ME.toEvent ev))
+      , HE.onDoubleClick (\ev -> Just (UserOpenedTab Nothing (ME.toEvent ev)))
       ]
       (A.mapWithIndex renderTab tabs)
   where
@@ -112,6 +112,7 @@ render state =
       , HE.onMouseLeave \evt -> Just $ TabMouseLeave evt index
       -- click event
       , HE.onClick (\ev -> Just (UserActivatedTab t.id (ME.toEvent ev)))
+      , HE.onDoubleClick (\ev -> Just (UserOpenedTab (Just t.id) (ME.toEvent ev)))
       -- TODO: on double click on a tab, open a tab right below
       -- clases
       , HP.classes $ H.ClassName
@@ -171,13 +172,13 @@ handleAction = case _ of
           Event.stopPropagation ev
           log "sb: activated a tab"
     H.raise $ SbActivateTab tid
-  UserOpenedTab ev -> do
+  UserOpenedTab tid ev -> do
     H.liftEffect
       $ do
           Event.preventDefault ev
           Event.stopPropagation ev
           log "sb: created a tab"
-    H.raise SbCreateTab
+    H.raise $ SbCreateTab tid
   -- Drag actions
   TabDragStart dragEvent tab index -> do
     let