Skip to content

Commit

Permalink
fix for ResizingFrame weights
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzoic committed Apr 15, 2024
1 parent e212414 commit ffef260
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions countess/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,52 +150,37 @@ def __init__(self, tk_parent, *a, orientation: Orientation = Orientation.AUTOMAT
self._children : list[self.ChildInfo] = []
super().__init__(tk_parent, *a, **k)
self._default_cursor = self.cget("cursor") or 'arrow'
self.configure(cursor = _RESIZE_CURSOR_H if self._is_horizontal else _RESIZE_CURSOR_V)
self.bind("<Configure>", self.on_configure)
self.bind("<Button-1>", self.on_click)
self.bind("<B1-Motion>", self.on_drag)
self._dragging : tuple(int, tk.Widget, int, tk.Widget, int, int) = None

def resize_cursor(self):
if platform.system() == 'Darwin':
return 'resizeleftright'
elif platform.system() == 'Windows':
return 'size_we'
else:
return 'sb_h_double_arrow'

def _resize(self):
# normalize weights so that sum(weights) == 1

# XXX may end up with a couple of pixels left over due to rounding errors
total_pixels = (self._width if self._is_horizontal else self._height) - (len(self._children)-1) * self.BORDER
total_weight = sum(c.weight for c in self._children)
for n, c in enumerate(self._children):
c.weight = c.weight / total_weight
print(f"{n} {c.weight}")

# XXX may end up with a couple of pixels left over due to rounding

if self._is_horizontal:
self.configure(cursor = _RESIZE_CURSOR_H)
avail_width = self._width - (len(self._children)-1) * self.BORDER
x = 0
for c in self._children:
width = int(c.weight * avail_width)
c.widget.place(x=x,w=width,y=0,h=self._height)
x += width + self.BORDER
else:
self.configure(cursor = _RESIZE_CURSOR_V)
avail_height = self._height - (len(self._children)-1) * self.BORDER
y = 0
for c in self._children:
height = int(c.weight * avail_height)
c.widget.place(x=0,w=self._width,y=y,h=height)
y += height + self.BORDER
pos = 0
for c in self._children:
child_size = (c.weight * total_pixels) // total_weight
if self._is_horizontal:
c.widget.place(x=pos,w=child_size,y=0,h=self._height)
else:
c.widget.place(x=0,w=self._width,y=pos,h=child_size)
pos += child_size + self.BORDER

def on_configure(self, event):
self._width = event.width
self._height = event.height
if self._orientation == self.Orientation.AUTOMATIC:
self._is_horizontal = self._width > self._height
self.configure(cursor = _RESIZE_CURSOR_H if self._is_horizontal else _RESIZE_CURSOR_V)
self._resize()

# XXX this whole DragInfo thing seems overcomplicated, revisit it later.

def on_click(self, event):
prev = None
for c in self._children:
Expand Down Expand Up @@ -250,7 +235,7 @@ def add_child(self, widget: tk.Widget, index: Optional[int] = None, weight: floa
index = len(self._children)
if not widget.cget('cursor'):
widget.configure(cursor=self._default_cursor)
self._children.insert(index, self.ChildInfo(widget, weight / (len(self._children) or 1)))
self._children.insert(index, self.ChildInfo(widget, weight))
self._resize()

def add_frame(self, *a, index: Optional[int] = None, weight: float = 1.0, **k):
Expand Down

0 comments on commit ffef260

Please sign in to comment.