Skip to content

Commit 85e5b67

Browse files
committed
fix: replace deprecated get_allocation with GTK4 alternatives
Fixes #8 Replace Gtk.Widget.get_allocation() with compute_bounds() for positions and get_width()/get_height() for dimensions.
1 parent 9be4b2e commit 85e5b67

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

src/sugar4/activity/activity.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -906,9 +906,10 @@ def get_preview(self):
906906
return None
907907

908908
try:
909-
# GTK4 approach to getting preview
910-
allocation = self.canvas.get_allocation()
911-
if allocation.width <= 0 or allocation.height <= 0:
909+
# Get canvas dimensions for preview
910+
canvas_width = self.canvas.get_width()
911+
canvas_height = self.canvas.get_height()
912+
if canvas_width <= 0 or canvas_height <= 0:
912913
return None
913914

914915
# For GTK4, we need to use a different approach since snapshot()
@@ -918,12 +919,10 @@ def get_preview(self):
918919
drawing_widget = self._find_drawable_widget(self.canvas)
919920

920921
if drawing_widget and hasattr(drawing_widget, "snapshot"):
921-
# Use the drawable widget for preview
922-
widget_allocation = drawing_widget.get_allocation()
923-
canvas_width, canvas_height = (
924-
widget_allocation.width,
925-
widget_allocation.height,
926-
)
922+
# Get drawable widget dimensions for preview surface
923+
widget_width = drawing_widget.get_width()
924+
widget_height = drawing_widget.get_height()
925+
canvas_width, canvas_height = widget_width, widget_height
927926

928927
# Create Cairo surface for the widget
929928
screenshot_surface = cairo.ImageSurface(
@@ -937,9 +936,7 @@ def get_preview(self):
937936
# Try to render the widget
938937
try:
939938
snapshot = Gtk.Snapshot()
940-
drawing_widget.snapshot(
941-
snapshot, widget_allocation.width, widget_allocation.height
942-
)
939+
drawing_widget.snapshot(snapshot, widget_width, widget_height)
943940

944941
# Convert snapshot to cairo surface
945942
# For now, we'll create a basic preview

src/sugar4/graphics/palettewindow.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,6 @@ def draw_rectangle(self, cr, palette):
13111311
if not self.parent:
13121312
return
13131313

1314-
allocation = self.parent.get_allocation()
13151314
context = self.parent.get_style_context()
13161315
context.add_class("toolitem")
13171316
context.add_class("palette-down")

src/sugar4/graphics/toolbarbox.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,17 @@ def do_snapshot(self, snapshot):
438438
"""Render palette using snapshot drawing."""
439439
Gtk.Widget.do_snapshot(self, snapshot)
440440

441-
button_alloc = self._toolbar_button.get_allocation()
441+
success, bounds = self._toolbar_button.compute_bounds(self)
442+
if success:
443+
button_x = bounds.get_x()
444+
button_width = bounds.get_width()
445+
else:
446+
button_x = 0
447+
button_width = self._toolbar_button.get_width()
448+
442449
my_width = self.get_width()
443450

444-
if my_width > 0:
451+
if my_width > 0 and button_width > 0:
445452
color = Gdk.RGBA()
446453
color.red = 0.7
447454
color.green = 0.7
@@ -451,15 +458,14 @@ def do_snapshot(self, snapshot):
451458
line_width = style.FOCUS_LINE_WIDTH * 2
452459

453460
rect1 = Graphene.Rect()
454-
rect1.init(0, 0, button_alloc.x + style.FOCUS_LINE_WIDTH, line_width)
461+
rect1.init(0, 0, button_x + style.FOCUS_LINE_WIDTH, line_width)
455462
snapshot.append_color(color, rect1)
456463

457464
rect2 = Graphene.Rect()
458465
rect2.init(
459-
button_alloc.x + button_alloc.width - style.FOCUS_LINE_WIDTH,
466+
button_x + button_width - style.FOCUS_LINE_WIDTH,
460467
0,
461-
my_width
462-
- (button_alloc.x + button_alloc.width - style.FOCUS_LINE_WIDTH),
468+
my_width - (button_x + button_width - style.FOCUS_LINE_WIDTH),
463469
line_width,
464470
)
465471
snapshot.append_color(color, rect2)

src/sugar4/graphics/tray.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,18 @@ def scroll_to_item(self, item):
9797
logging.warning("Item not found in tray children")
9898
return
9999

100-
# Get the item's allocation
101-
allocation = item.get_allocation()
100+
success, bounds = item.compute_bounds(self.traybar)
101+
if not success:
102+
return
103+
102104
if self.orientation == Gtk.Orientation.HORIZONTAL:
103105
adj = self.get_hadjustment()
104-
start = allocation.x
105-
stop = allocation.x + allocation.width
106+
start = bounds.get_x()
107+
stop = bounds.get_x() + bounds.get_width()
106108
else:
107109
adj = self.get_vadjustment()
108-
start = allocation.y
109-
stop = allocation.y + allocation.height
110+
start = bounds.get_y()
111+
stop = bounds.get_y() + bounds.get_height()
110112

111113
# Scroll if needed
112114
if start < adj.get_value():
@@ -116,26 +118,28 @@ def scroll_to_item(self, item):
116118

117119
def _scroll_next(self):
118120
"""Scroll to next page."""
119-
allocation = self.get_allocation()
120121
if self.orientation == Gtk.Orientation.HORIZONTAL:
121122
adj = self.get_hadjustment()
122-
new_value = adj.get_value() + allocation.width
123-
adj.set_value(min(new_value, adj.get_upper() - allocation.width))
123+
width = self.get_width()
124+
new_value = adj.get_value() + width
125+
adj.set_value(min(new_value, adj.get_upper() - width))
124126
else:
125127
adj = self.get_vadjustment()
126-
new_value = adj.get_value() + allocation.height
127-
adj.set_value(min(new_value, adj.get_upper() - allocation.height))
128+
height = self.get_height()
129+
new_value = adj.get_value() + height
130+
adj.set_value(min(new_value, adj.get_upper() - height))
128131

129132
def _scroll_previous(self):
130133
"""Scroll to previous page."""
131-
allocation = self.get_allocation()
132134
if self.orientation == Gtk.Orientation.HORIZONTAL:
133135
adj = self.get_hadjustment()
134-
new_value = adj.get_value() - allocation.width
136+
width = self.get_width()
137+
new_value = adj.get_value() - width
135138
adj.set_value(max(adj.get_lower(), new_value))
136139
else:
137140
adj = self.get_vadjustment()
138-
new_value = adj.get_value() - allocation.height
141+
height = self.get_height()
142+
new_value = adj.get_value() - height
139143
adj.set_value(max(adj.get_lower(), new_value))
140144

141145
def do_get_preferred_width(self):
@@ -164,16 +168,17 @@ def _size_changed_cb(self, widget, pspec):
164168
self._update_scrollable_state()
165169

166170
def _update_scrollable_state(self):
167-
allocation = self.get_allocation()
168-
if allocation.width <= 1 and allocation.height <= 1:
171+
width = self.get_width()
172+
height = self.get_height()
173+
if width <= 1 and height <= 1:
169174
return
170175

171176
traybar_min, traybar_nat = self.traybar.get_preferred_size()
172177

173178
if self.orientation == Gtk.Orientation.HORIZONTAL:
174-
scrollable = traybar_nat.width > allocation.width
179+
scrollable = traybar_nat.width > width
175180
else:
176-
scrollable = traybar_nat.height > allocation.height
181+
scrollable = traybar_nat.height > height
177182

178183
if scrollable != self._scrollable:
179184
self._scrollable = scrollable

0 commit comments

Comments
 (0)