Skip to content

Commit

Permalink
[SUTK] Added setLabel() within in the Tab class; getting reference to…
Browse files Browse the repository at this point in the history
… TabView and then calling its setLabel() is disallowed
  • Loading branch information
ravi688 committed Nov 23, 2024
1 parent 0e01e12 commit 4124dc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions sutk/include/sutk/NotebookView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ namespace SUTK
};

class NotebookView;
class TabBar;

class SUTK_API Tab : public com::LinkedListNodeBase<Tab>
{
Expand All @@ -101,15 +102,19 @@ namespace SUTK
friend class TabShiftAnimation;
friend class TabBar;
private:
TabBar* m_tabBar;
TabView* m_tabView;
NotebookPage* m_page;
u32 m_index;
Vec2Df m_pos;
public:
// Setters
void setPage(NotebookPage* page) noexcept;
// NOTE: this function must be used to change the lable of the tab. Do not use getTabView()->setLabel()
void setLabel(const std::string_view str) noexcept;
// Getters
Vec2Df getPos() const noexcept { return m_pos; }
TabBar* getTabBar() noexcept { return m_tabBar; }
TabView* getTabView() noexcept { return m_tabView; }
NotebookPage* getPage() noexcept { return m_page; }
u32 getIndex() const noexcept { return m_index; }
Expand Down
27 changes: 19 additions & 8 deletions sutk/source/NotebookView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace SUTK

void NotebookPage::setLabel(const std::string_view str) noexcept
{
m_tab->getTabView()->setLabel(str);
m_tab->setLabel(str);
}

const std::string& NotebookPage::getLabel() const noexcept
Expand Down Expand Up @@ -490,6 +490,20 @@ namespace SUTK
if(page)
page->m_tab = this;
}
void Tab::setLabel(const std::string_view str) noexcept
{
com_assert(COM_DESCRIPTION(!m_tabBar->isLockedLayout()), "Undefined behaviour, You can't change label of a tab while some tab animations are going on (the layout is locked)");
// NOTE: Calling setLabel() resizes the tabView to fit the label, which results in layout recalculation
// Since, we call setLayoutAttributes() further, it would trigger another layout recalculation.
// Therefore, we are better off locking the layout first and then we are finished, unlock it at the end.
m_tabBar->lockLayout();
m_tabView->setLabel(str);
LayoutAttributes attr = m_tabView->getLayoutAttributes();
attr.minSize.width = TAB_VIEW_MIN_WIDTH;
attr.prefSize.width = std::max(TAB_VIEW_MIN_WIDTH, m_tabView->getSize().width);
m_tabView->setLayoutAttributes(attr);
m_tabBar->unlockLayout(true);
}

TabBar::TabBar(UIDriver& driver, Container* parent) noexcept : HBoxContainer(driver, parent), m_root(com::null_pointer<Tab>())
{
Expand All @@ -510,6 +524,7 @@ namespace SUTK
{
lockLayout();
Tab* tab = new Tab();
tab->m_tabBar = this;
TabView* tabView = getUIDriver().createContainer<TabView>(com::null_pointer<Container>());
tab->m_tabView = tabView;
if(after)
Expand All @@ -528,13 +543,9 @@ namespace SUTK
if(!tab->getPrev())
m_root = tab;
addAt(tabView, tab->m_index);
tabView->setLabel(labelStr);
LayoutAttributes attr = tabView->getLayoutAttributes();
attr.minSize.width = TAB_VIEW_MIN_WIDTH;
attr.prefSize.width = std::max(TAB_VIEW_MIN_WIDTH, tabView->getSize().width);
tabView->setLayoutAttributes(attr);
// Recalculate the layout to set the position and sizes of the tab views correctly
unlockLayout(true);
// Unlock the layout but do not recalculate, because we are further calling setLabel() which does recalculates at the end
unlockLayout();
tab->setLabel(labelStr);
return tab;
}
void TabBar::destroyTab(Tab* tab) noexcept
Expand Down

0 comments on commit 4124dc3

Please sign in to comment.