From f2edc4bbb4426d1f0fa85a44f343e4b8c44607a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 30 Mar 2026 21:58:37 +0200 Subject: Keep message list scrolled to bottom --- src/Main.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Main.hs b/src/Main.hs index bad0ec1..55f677d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -248,9 +248,16 @@ setup = do SelectedConversation conv | maybe False (msgPeer cur `sameIdentity`) (conversationPeer conv) -> do + scrollTop <- js_get_scrollTop messagesList + scrollHeight <- js_get_scrollHeight messagesList + clientHeight <- js_get_clientHeight messagesList + ul <- js_get_firstChild messagesList appendMessages gs ul $ map Left $ reverse $ dmThreadToListSince prev cur + when (scrollTop + clientHeight >= scrollHeight) $ do + js_set_scrollTop messagesList =<< js_get_scrollHeight messagesList + _ -> return () Nothing -> return () @@ -539,6 +546,7 @@ selectConversation gs@GlobalState {..} conv = do js_setAttribute body (toJSString "data-selected") (toJSString "conversation") Nothing -> return () + mapM_ (\mlist -> js_set_scrollTop mlist =<< js_get_scrollHeight mlist) =<< JS.getElementById "msg_list" mapM_ js_focus =<< JS.getElementById "msg_text" return $ SelectedConversation conv @@ -737,6 +745,18 @@ foreign import javascript unsafe "$1.value = $2" foreign import javascript unsafe "$1.focus()" js_focus :: JSVal -> IO () +foreign import javascript unsafe "$1.scrollTop" + js_get_scrollTop :: JSVal -> IO Int + +foreign import javascript unsafe "$1.scrollTop = $2" + js_set_scrollTop :: JSVal -> Int -> IO () + +foreign import javascript unsafe "$1.scrollHeight" + js_get_scrollHeight :: JSVal -> IO Int + +foreign import javascript unsafe "$1.clientHeight" + js_get_clientHeight :: JSVal -> IO Int + foreign import javascript unsafe "window" js_window :: JSVal -- cgit v1.2.3