Skip to content

Commit

Permalink
fix for programmatically selecting a tab before window is displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
Freaxed committed Jan 27, 2025
1 parent 61cca1f commit 79ea78d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/helpers/gtab/GTabView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ GTabView::AttachedToWindow()
fScrollRightTabButton->SetTarget(this);
fTabsContainer->SetTarget(this);
fTabMenuTabButton->SetTarget(this);
BGroupView::AttachedToWindow();
}


Expand All @@ -112,10 +113,10 @@ GTabView::MessageReceived(BMessage* message)
{
switch(message->what) {
case kLeftTabButton:
fTabsContainer->ShiftTabs(-1);
fTabsContainer->ShiftTabs(-1, "shift left");
break;
case kRightTabButton:
fTabsContainer->ShiftTabs(+1);
fTabsContainer->ShiftTabs(+1, "shift right");
break;
case kSelectedTabButton:
{
Expand Down
33 changes: 26 additions & 7 deletions src/helpers/gtab/TabsContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ TabsContainer::TabsContainer(GTabView* tabView,
fSelectedTab(nullptr),
fGTabView(tabView),
fTabShift(0),
fAffinity(affinity)
fAffinity(affinity),
fFirstLayout(true)
{
SetFlags(Flags()|B_FRAME_EVENTS);
GroupLayout()->AddView(0, new Filler(this));
Expand All @@ -47,7 +48,7 @@ TabsContainer::AddTab(GTab* tab, int32 index)
SelectTab(tab);
}

ShiftTabs(0);
ShiftTabs(0, "add tab");
}


Expand Down Expand Up @@ -103,7 +104,7 @@ TabsContainer::RemoveTab(GTab* tab)
if (fTabShift > 0 && fTabShift >= CountTabs()) {
shift -= 1;
}
ShiftTabs(shift);
ShiftTabs(shift, "remove tab");

return tab;
}
Expand Down Expand Up @@ -137,7 +138,7 @@ TabsContainer::SelectTab(GTab* tab, bool invoke)
}

if (fTabShift >= index) {
ShiftTabs(index - fTabShift);
ShiftTabs(index - fTabShift, "select tab_1");
} else {
// let's ensure at least the tab's "middle point"
// is visible.
Expand All @@ -152,7 +153,7 @@ TabsContainer::SelectTab(GTab* tab, bool invoke)
break;
}
}
ShiftTabs(shift);
ShiftTabs(shift, "select tab_2");
}
}
}
Expand All @@ -161,8 +162,11 @@ TabsContainer::SelectTab(GTab* tab, bool invoke)


void
TabsContainer::ShiftTabs(int32 delta)
TabsContainer::ShiftTabs(int32 delta, const char* src)
{
if (Bounds().IsValid() == false)
return;

int32 newShift = fTabShift + delta;
if (newShift < 0)
newShift = 0;
Expand All @@ -178,6 +182,7 @@ TabsContainer::ShiftTabs(int32 delta)
tab->Show();
}
}
//DEBUG printf(".. updating from %d to %d (%s)\n", fTabShift, newShift,src);
fTabShift = newShift;
_UpdateScrolls();
}
Expand Down Expand Up @@ -212,7 +217,7 @@ TabsContainer::FrameResized(float w, float h)
break;
}
if (tox != 0)
ShiftTabs(tox);
ShiftTabs(tox, "select tab_1");
}
// end
_UpdateScrolls();
Expand Down Expand Up @@ -253,3 +258,17 @@ TabsContainer::_UpdateScrolls()
fGTabView->UpdateScrollButtons(false, false);
}
}


void
TabsContainer::DoLayout()
{
BGroupView::DoLayout();
if (fFirstLayout == true && Bounds().IsValid()) {
GTab* selected = fSelectedTab;
fSelectedTab = nullptr;
SelectTab(selected);
fFirstLayout = false;
}

}
5 changes: 4 additions & 1 deletion src/helpers/gtab/TabsContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TabsContainer : public BGroupView, public BInvoker {
GTab* TabAt(int32 index) const;
int32 IndexOfTab(GTab* tab) const;

void ShiftTabs(int32 delta); // 0 to refresh the current state
void ShiftTabs(int32 delta, const char* src); // 0 to refresh the current state

void MouseDownOnTab(GTab* tab, BPoint where, const int32 buttons);

Expand All @@ -41,6 +41,8 @@ class TabsContainer : public BGroupView, public BInvoker {

tab_affinity GetAffinity() const { return fAffinity; }

void DoLayout();

private:
void _PrintToStream();
void _UpdateScrolls();
Expand All @@ -49,4 +51,5 @@ class TabsContainer : public BGroupView, public BInvoker {
GTabView* fGTabView;
int32 fTabShift;
tab_affinity fAffinity;
bool fFirstLayout;
};

0 comments on commit 79ea78d

Please sign in to comment.