11#include " frontend/ftxui/ui/screens/Main.hpp"
2- #include " frontend/ftxui/components/scroll/Scrollable.hpp"
32#include " frontend/ftxui/state/library/Impl.hpp"
43
54using namespace ftxui ;
@@ -12,20 +11,43 @@ MainScreen::MainScreen(state::library::LibraryState& state) : m_state(state)
1211 artist_menu = Menu (&m_state.artists , &m_state.selected_artist ) | vscroll_indicator;
1312
1413 album_content =
15- Renderer ([&]() mutable -> Element { return vbox (m_state.returnAlbumElements ()); });
14+ Renderer ([&]() -> Element { return vbox (m_state.returnAlbumElements ()) | vscroll_indicator ; });
1615
17- album_scroller =
18- Scroller (album_content, &m_state. selected_album_index , Color::Green, Color::GrayDark) ;
16+ album_scroll = 0 . 0f ;
17+ album_scroll_target = 0 . 0f ;
1918
20- container = Container::Horizontal ({artist_menu, album_scroller});
19+ album_view = Renderer (album_content,
20+ [&]() -> Element
21+ {
22+ const auto & elems = m_state.returnAlbumElements ();
23+
24+ if (!elems.empty ())
25+ {
26+ album_scroll_target = float (m_state.selected_album_index ) /
27+ float (std::max<int >(1 , elems.size () - 1 ));
28+ }
29+
30+ constexpr float smoothing = 0 .15f ; // 0.1 = slow, 0.25 = fast
31+ album_scroll += (album_scroll_target - album_scroll) * smoothing;
32+
33+ album_scroll = std::clamp (album_scroll, 0 .0f , 1 .0f );
34+
35+ return album_content->Render () |
36+ focusPositionRelative (0 .0f , album_scroll) | frame | flex;
37+ });
38+
39+ container = Container::Horizontal ({
40+ artist_menu,
41+ album_view,
42+ });
2143}
2244
2345void MainScreen::syncFocus ()
2446{
2547 if (m_state.focusOnArtists ())
2648 artist_menu->TakeFocus ();
2749 else
28- album_scroller ->TakeFocus ();
50+ album_view ->TakeFocus ();
2951}
3052
3153auto MainScreen::component () -> Component { return container; }
@@ -51,7 +73,7 @@ auto MainScreen::render() -> Element
5173 auto artist_inner = window (text (" Artists " ) | bold, artist_menu->Render () | frame | flex) |
5274 size (WIDTH , EQUAL , half_width);
5375
54- auto album_inner = window (text (" Songs " ) | bold, album_scroller ->Render () | frame | flex) |
76+ auto album_inner = window (text (" Songs " ) | bold, album_view ->Render () | frame | flex) |
5577 size (WIDTH , EQUAL , term.dimx - half_width);
5678
5779 Color artist_border_color = m_state.focusOnArtists () ? Color::Green : Color::GrayDark;
0 commit comments