Skip to content

Commit

Permalink
[STUDIO] Add tab reordering and better auto-selection mechanics to ta…
Browse files Browse the repository at this point in the history
…b layout (#51)
  • Loading branch information
ObaraEmmanuel committed Nov 7, 2024
1 parent 78adec5 commit dec8edb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion studio/feature/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,8 @@ def _update_stacking(self, indices, silently=False):
if not indices:
return

child_list = next(iter(indices)).layout._children
layout = next(iter(indices)).layout
child_list = layout._children
# reorder child list based on indices

indices = sorted(
Expand All @@ -981,6 +982,7 @@ def _update_stacking(self, indices, silently=False):
widget.lift(widget.layout.body)

prev_data = dict(sorted(prev_data.items(), key=lambda x: x[1]))
layout.layout_strategy.widgets_reordered()

if not silently and prev_data != data:
self.studio.new_action(Action(
Expand Down
16 changes: 14 additions & 2 deletions studio/lib/layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def _update_stacking(self):
else:
widget.lift(self.container.body)

def widgets_reordered(self):
pass

def widget_released(self, widget):
pass

Expand Down Expand Up @@ -980,7 +983,7 @@ class TabLayoutStrategy(BaseLayoutStrategy):
name = "TabLayout"
icon = "notebook"
manager = "tab"
stacking_support = False
stacking_support = True

def __init__(self, master):
super().__init__(master)
Expand Down Expand Up @@ -1014,12 +1017,15 @@ def get_restore(self, widget):
def _redraw(self, start_index=0):
affected = self.children[start_index:]
cache = {}
selected = self.container.select()
for child in affected:
cache[child] = self.info(child)
self.container.body.forget(child)
for child in affected:
for i, child in enumerate(affected):
self.container.body.add(child)
self.container.body.tab(child, **cache.get(child, {}))
if str(child) == selected:
self.container.select(i)

def resize_widget(self, widget, direction, delta):
pass
Expand All @@ -1030,6 +1036,9 @@ def add_widget(self, widget, bounds=None, **kwargs):
self.container.body.add(widget, text=widget.id)
self.container.body.tab(widget, **kwargs)
self._insert(widget)
length = len(self.container.tabs())
if length > 1:
self.container.select(length - 1)

def remove_widget(self, widget):
super().remove_widget(widget)
Expand Down Expand Up @@ -1057,6 +1066,9 @@ def _tab_switched(self, *_):
# Take its level one place above other tabs
self._current_tab.level = self.level + 2

def widgets_reordered(self):
self._redraw(0)

def copy_layout(self, widget, from_):
info = from_.layout.body.tab(from_)
self.add_widget(widget, (0, 0, 0, 0), **info)
Expand Down

0 comments on commit dec8edb

Please sign in to comment.