Skip to content

Commit

Permalink
[SUTK] Bring tab associated with currently viewed NotebookPage into v…
Browse files Browse the repository at this point in the history
…iew of TabBar's ScrollContainer

 - Affected test: NOTEBOOK
 - Changes can be noticed by adding many new tabs and the newly created tab should come into view (i.e. shouldn't be hiding on the either side).
  • Loading branch information
ravi688 committed Jan 7, 2025
1 parent 3bd6941 commit 22c8156
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sutk/include/sutk/NotebookView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ namespace SUTK
Tab* getSelectedTab() noexcept { return m_selectedTab; }
};

typedef MaskedScrollableContainer<TabBar> ScrollableTabBar;
class SUTK_API ScrollableTabBar : public MaskedScrollableContainer<TabBar>
{
using MaskedScrollableContainer::MaskedScrollableContainer;
public:
// Brings (instantaneously) TabView associated with the Tab under the area of ScrollContainer of this ScrollableTabBar.
// If the Tab is hiding on the right side of the ScrollContainer, then the Tab is brought into view on the Right side
// If the Tab is hiding on the left side of the ScrollContainer, then the Tab is brought into view on the Left side
// No action is taken if the Tab is already into view.
void scrollToTab(Tab* tab) noexcept;
};

class TabRemoveAnimation;
class TabInsertAnimation;
Expand Down Expand Up @@ -231,7 +240,7 @@ namespace SUTK
~NotebookView() noexcept;
void checkForTabSwap() noexcept;

TabBar* getTabBar() noexcept { return m_tabBar; }
ScrollableTabBar* getTabBar() noexcept { return m_tabBar; }

OnPageSelectEvent& getOnPageSelectEvent() noexcept { return m_onPageSelectEvent; }

Expand Down
15 changes: 15 additions & 0 deletions sutk/source/NotebookView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,19 @@ namespace SUTK
}
}

void ScrollableTabBar::scrollToTab(Tab* tab) noexcept
{
ScrollContainer* scrollCont = getScrollContainer();
// Get rectangle of TabView in the local space of ScrollContainer
auto rect = tab->getTabView()->getRectRelativeTo(scrollCont);
// If the TabView is hiding on the right side of the ScrollContainer then scroll to left
if(f32 distance = rect.getRight() - scrollCont->getSize().width; distance > 0)
scrollCont->addScrollDelta(Vec2Df::left() * distance);
// else If the TabView is hiding on the left side of the ScrollContainer then scroll to right
else if(f32 distance = rect.getLeft() - scrollCont->getPosition().x; distance < 0)
scrollCont->addScrollDelta(Vec2Df::left() * distance);
}

Tab* NotebookView::createTab(const std::string_view labelStr, NotebookPage* page, Tab* afterTab) noexcept
{
// Create TabView
Expand Down Expand Up @@ -656,6 +669,8 @@ namespace SUTK
void NotebookView::viewPage(NotebookPage* page) noexcept
{
m_tabBar->selectTab(page->getTab());
// The tab associated with this page must be in the view
m_tabBar->scrollToTab(page->getTab());
}

static NotebookPage* GetAdjacentPage(NotebookPage* page) noexcept
Expand Down

0 comments on commit 22c8156

Please sign in to comment.