Skip to content

Commit

Permalink
Merge pull request #1658 from freakboy3742/cocoa-window-refs
Browse files Browse the repository at this point in the history
Corrections to widget and window handling
  • Loading branch information
mhsmith authored Nov 7, 2022
2 parents 6f7b9eb + 08f0b6c commit 2e1b56b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions changes/1659.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An error caused by retrieving `app.current_window` on macOS has been resolved.
1 change: 1 addition & 0 deletions changes/1660.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If a window has existing content when it is added to an app, the widget registry no longer raises an error.
9 changes: 8 additions & 1 deletion src/cocoa/src/toga_cocoa/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def onToolbarButtonPress_(self, obj) -> None:
item.action(obj)


class TogaWindow(NSWindow):
interface = objc_property(object, weak=True)
impl = objc_property(object, weak=True)


class Window:
def __init__(self, interface, title, position, size):
self.interface = interface
Expand All @@ -156,12 +161,14 @@ def __init__(self, interface, title, position, size):

# Create the window with a default frame;
# we'll update size and position later.
self.native = NSWindow.alloc().initWithContentRect(
self.native = TogaWindow.alloc().initWithContentRect(
NSMakeRect(0, 0, 0, 0),
styleMask=mask,
backing=NSBackingStoreBuffered,
defer=False,
)
self.native.interface = self.interface
self.native.impl = self

self.set_title(title)
self.set_size(size)
Expand Down
1 change: 0 additions & 1 deletion src/core/src/toga/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def app(self, app):

self._app = app
self._impl.set_app(app._impl)
app.widgets.update(self.widgets)

if self.content:
self.content.app = app
Expand Down
25 changes: 19 additions & 6 deletions src/core/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,32 @@ def test_set_app_after_content(self):

def test_set_app_adds_window_widgets_to_app(self):

id1, id2, id3 = "id1", "id2", "id3"
id0, id1, id2, id3 = "id0", "id1", "id2", "id3"
widget1, widget2, widget3 = (
toga.Widget(id=id1),
toga.Widget(id=id2),
toga.Widget(id=id3),
toga.Label(id=id1, text="label 1"),
toga.Label(id=id2, text="label 1"),
toga.Label(id=id3, text="label 1"),
)
self.window.widgets.update({widget1, widget2, widget3})
content = toga.Box(id=id0, children=[widget1, widget2, widget3])

self.window.content = content

# The window has widgets in it's repository
self.assertEqual(len(self.window.widgets), 4)
self.assertEqual(self.window.widgets[id0], content)
self.assertEqual(self.window.widgets[id1], widget1)
self.assertEqual(self.window.widgets[id2], widget2)
self.assertEqual(self.window.widgets[id3], widget3)

# The app doesn't know about the widgets
self.assertEqual(len(self.app.widgets), 0)

# Assign the window to the app
self.window.app = self.app

self.assertEqual(len(self.app.widgets), 3)
# The window's content widgets are now known to the app.
self.assertEqual(len(self.app.widgets), 4)
self.assertEqual(self.app.widgets[id0], content)
self.assertEqual(self.app.widgets[id1], widget1)
self.assertEqual(self.app.widgets[id2], widget2)
self.assertEqual(self.app.widgets[id3], widget3)
Expand Down
18 changes: 18 additions & 0 deletions src/dummy/src/toga_dummy/window.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
from .utils import LoggedObject, not_required, not_required_on


@not_required
class Viewport:
def __init__(self, window):
self.baseline_dpi = 96
self.dpi = 96
self.window = window

@property
def width(self):
return self.window.get_size()[0]

@property
def height(self):
return self.window.get_size()[1]


class Window(LoggedObject):
def __init__(self, interface, title, position, size):
super().__init__()
Expand All @@ -18,6 +34,8 @@ def clear_content(self):

def set_content(self, widget):
self._action("set content", widget=widget)
self._set_value("content", widget)
widget.viewport = Viewport(self)

def get_title(self):
return self._get_value("title")
Expand Down

0 comments on commit 2e1b56b

Please sign in to comment.