Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added API to insert and remove children #814

Merged
merged 31 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8a724fc
added API to remove and insert children
Feb 15, 2020
7591949
added cocoa API to insert and remove children
Feb 15, 2020
eff99cb
remove constraints when container is set to None
Feb 15, 2020
cf48c4b
set self.constraints.container to None...
Feb 15, 2020
e72dcc7
fixed refresh on remove
Feb 15, 2020
2ac28bd
moved refresh code out of container setter
Feb 15, 2020
5ffe71a
added flake8 exceptions
Feb 16, 2020
a0b2585
explicitly set container to None
Feb 16, 2020
4ed5a06
remove unnecessary `refresh` reimplementation
Feb 16, 2020
ee1cd23
clean up widget refresh
Feb 16, 2020
8574903
remove unnecessary check for children
Feb 16, 2020
ab6deab
added test app for box
Feb 16, 2020
8fe8417
style fix
Feb 16, 2020
047a791
more style fixes
Feb 16, 2020
778a9a1
fixed setting app without an implementation
Feb 16, 2020
050c051
renamed box example to layout_test
Apr 23, 2020
f128997
Merge branch 'master' into remove-widgets
Apr 23, 2020
329171f
refresh the entire window content when adding / removing widgets
Apr 23, 2020
effc8a4
refresh_sublayouts before we refresh ourself
Apr 23, 2020
f3bea4b
fixed typo in layout_test example
Apr 25, 2020
adc6c4c
simplify add children code
Apr 25, 2020
a6f4035
cleanup and add comments
Apr 25, 2020
9067c61
remove unnecessary check for self.interface.window
Apr 25, 2020
2a397a8
add reparent button and disable unneeded buttons
Apr 25, 2020
c91f70f
updated doc strings
Apr 25, 2020
e1a30ca
added comments to clarify test cases
Apr 26, 2020
3306030
O(1) check if we are already the parent
Apr 26, 2020
991fb2c
bumped travertino requirement to 0.1.3
Apr 26, 2020
f41f4b0
Merge branch 'master' into remove-widgets
freakboy3742 May 25, 2020
42d0052
Cosmetic rename of test app.
freakboy3742 May 25, 2020
2ca293f
Fixed test case output change from travertino bump.
freakboy3742 May 25, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/layout_test/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Layout Test
===========

Test app that demonstrates adding and removing widgets from box.

Quickstart
~~~~~~~~~~

To run this example:

$ pip install toga
$ python -m tayout_test
9 changes: 9 additions & 0 deletions examples/layout_test/layout_test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Examples of valid version strings
# __version__ = '1.2.3.dev1' # Development release 1
# __version__ = '1.2.3a1' # Alpha Release 1
# __version__ = '1.2.3b1' # Beta Release 1
# __version__ = '1.2.3rc1' # RC Release 1
# __version__ = '1.2.3' # Final Release
# __version__ = '1.2.3.post1' # Post Release 1

__version__ = '0.0.1'
4 changes: 4 additions & 0 deletions examples/layout_test/layout_test/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from layout_test.app import main

if __name__ == '__main__':
main().main_loop()
82 changes: 82 additions & 0 deletions examples/layout_test/layout_test/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import toga
from toga.style import Pack
from toga.constants import ROW, COLUMN, CENTER, BLUE


class ExampleBoxApp(toga.App):

def startup(self):

self.button_add = toga.Button(
label='Add image',
style=Pack(padding=10, width=120),
on_press=self.add_image,
)

self.button_remove = toga.Button(
label='Remove image',
style=Pack(padding=10, width=120),
on_press=self.remove_image,
)

self.button_insert = toga.Button(
label='Insert image',
style=Pack(padding=10, width=120),
on_press=self.insert_image,
)

self.button_add_to_scroll = toga.Button(
label='Add new label',
style=Pack(padding=10, width=120),
on_press=self.add_label,
)

self.scroll_box = toga.Box(style=Pack(direction=COLUMN, padding=10, flex=1, color=BLUE))
self.scroll_view = toga.ScrollContainer(content=self.scroll_box, style=Pack(width=120))

icon = toga.Icon('')
self.image_view = toga.ImageView(icon, style=Pack(padding=10, width=120, height=120))

self.button_box = toga.Box(
children=[
self.button_add,
self.button_remove,
self.button_insert,
self.button_add_to_scroll,
],
style=Pack(direction=COLUMN),
)

self.box = toga.Box(
children=[self.button_box, self.scroll_view],
style=Pack(direction=ROW, padding=10, alignment=CENTER, flex=1)
)

self.main_window = toga.MainWindow()
self.main_window.content = self.box
self.main_window.show()

def add_image(self, sender):
self.button_box.add(self.image_view)

def insert_image(self, sender):
self.button_box.insert(1, self.image_view)

def remove_image(self, sender):
self.button_box.remove(self.image_view)

def add_label(self, sender):
new_label = toga.Label(
'Label {}'.format(len(self.scroll_box.children)),
style=Pack(padding=2, width=70)
)
self.scroll_box.add(new_label)


def main():
return ExampleBoxApp('Layout Test', 'org.beeware.widgets.layout_test')


if __name__ == '__main__':
app = main()
app.main_loop()
1 change: 1 addition & 0 deletions examples/layout_test/layout_test/resources/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Put any icons or images in this directory.
44 changes: 44 additions & 0 deletions examples/layout_test/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[build-system]
requires = ["briefcase"]

[tool.briefcase]
project_name = "Layout Test"
bundle = "org.beeware"
version = "0.0.1"
url = "https://beeware.org"
license = "BSD license"
author = 'Tiberius Yak'
author_email = "tiberius@beeware.org"

[tool.briefcase.app.tayout_test]
formal_name = "Layout Test"
description = "A testing app"
sources = ['tayout_test']
requires = []


[tool.briefcase.app.tayout_test.macOS]
requires = [
'toga-cocoa',
]

[tool.briefcase.app.tayout_test.linux]
requires = [
'toga-gtk',
]

[tool.briefcase.app.tayout_test.windows]
requires = [
'toga-winforms',
]

# Mobile deployments
[tool.briefcase.app.tayout_test.iOS]
requires = [
'toga-iOS',
]

[tool.briefcase.app.tayout_test.android]
requires = [
'toga-android',
]
67 changes: 37 additions & 30 deletions src/cocoa/toga_cocoa/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,46 @@ def container(self):

@container.setter
def container(self, value):
self._container = value
# print("Add constraints for", self.widget, 'in', self.container, self.widget.interface.layout)
self.left_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(
self.widget.native, NSLayoutAttributeLeft,
NSLayoutRelationEqual,
self.container.native, NSLayoutAttributeLeft,
1.0, 10 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.left_constraint)
if value is None and self.container:
self.container.native.removeConstraint(self.width_constraint)
self.container.native.removeConstraint(self.height_constraint)
self.container.native.removeConstraint(self.left_constraint)
self.container.native.removeConstraint(self.top_constraint)
self._container = value
else:
self._container = value
# print("Add constraints for", self.widget, 'in', self.container, self.widget.interface.layout)
self.left_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501
self.widget.native, NSLayoutAttributeLeft,
NSLayoutRelationEqual,
self.container.native, NSLayoutAttributeLeft,
1.0, 10 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.left_constraint)

self.top_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(
self.widget.native, NSLayoutAttributeTop,
NSLayoutRelationEqual,
self.container.native, NSLayoutAttributeTop,
1.0, 5 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.top_constraint)
self.top_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501
self.widget.native, NSLayoutAttributeTop,
NSLayoutRelationEqual,
self.container.native, NSLayoutAttributeTop,
1.0, 5 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.top_constraint)

self.width_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(
self.widget.native, NSLayoutAttributeRight,
NSLayoutRelationEqual,
self.widget.native, NSLayoutAttributeLeft,
1.0, 50 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.width_constraint)
self.width_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501
self.widget.native, NSLayoutAttributeRight,
NSLayoutRelationEqual,
self.widget.native, NSLayoutAttributeLeft,
1.0, 50 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.width_constraint)

self.height_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(
self.widget.native, NSLayoutAttributeBottom,
NSLayoutRelationEqual,
self.widget.native, NSLayoutAttributeTop,
1.0, 30 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.height_constraint)
self.height_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501
self.widget.native, NSLayoutAttributeBottom,
NSLayoutRelationEqual,
self.widget.native, NSLayoutAttributeTop,
1.0, 30 # Use a dummy, non-zero value for now
)
self.container.native.addConstraint(self.height_constraint)

def update(self, x, y, width, height):
# print("UPDATE", self.widget, 'in', self.container, 'to', x, y, width, height)
Expand Down
30 changes: 23 additions & 7 deletions src/cocoa/toga_cocoa/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def __init__(self, interface):
self.native = None
self.create()
self.interface.style.reapply()
self.set_enabled(self.interface.enabled)

def create(self):
raise NotImplementedError()

def set_app(self, app):
pass
Expand All @@ -24,17 +28,20 @@ def container(self):

@container.setter
def container(self, container):
self._container = container
self._container.native.addSubview(self.native)
self.constraints.container = container

if container is None:
self.constraints.container = None
self._container = None
self.native.removeFromSuperview()
else:
self._container = container
self._container.native.addSubview(self.native)
self.constraints.container = container

for child in self.interface.children:
child._impl.container = container

self.rehint()
if self.interface.window and self.interface.window._impl.native.isVisible:
# refresh window content to reflect added subview
self.interface.window.content.refresh()

def set_enabled(self, value):
self.native.enabled = self.interface.enabled
Expand Down Expand Up @@ -66,7 +73,16 @@ def set_background_color(self, color):
def add_child(self, child):
# if we don't have a window, we won't have a container
if self.interface.window:
child.container = self.container or self.interface.window.content._impl
# if we don't have a container, we *are* the container
child.container = self.container or self

def insert_child(self, index, child):
self.add_child(child)

def remove_child(self, child):
# if we don't have a window, we won't have a container
if self.interface.window:
child.container = None

def add_constraints(self):
self.native.translatesAutoresizingMaskIntoConstraints = False
Expand Down
66 changes: 55 additions & 11 deletions src/core/toga/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,59 @@ def add(self, *children):
ValueError: If this node is a leaf, and cannot have children.
"""
for child in children:
super().add(child)
if child not in self.children:
super().add(child)

child.app = self.app
child.window = self.window

if self._impl:
self._impl.add_child(child._impl)

if self.window:
self.window.content.refresh()

def insert(self, index, child):
"""Insert a node as a child of this one.
Args:
index: Position of child node.
child: A node to add as a child to this node.

Raises:
ValueError: If this node is a leaf, and cannot have children.
"""
if child not in self.children:
super().insert(index, child)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running the demo app, this line raises errors about super() has no method insert.

Copy link
Member Author

@samschott samschott Apr 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR requires beeware/travertino#10 to work. That PR adds insert and remove methods to Node, tests those methods, and fixes an issue where a widget's origin is not recalculated when it is added as a child.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/me facepalms. My apologies - I completely forgot about that dependency.


child.app = self.app
child.window = self.window

if self._impl:
self._impl.add_child(child._impl)
self._impl.insert_child(index, child._impl)

if self.window:
self.window.content.refresh()

def remove(self, *children):
"""Remove a node as a child of this one.
Args:
child: A node to add as a child to this node.

Raises:
ValueError: If this node is a leaf, and cannot have children.
"""
for child in children:
if child in self.children:
super().remove(child)

child.app = None
child.window = None

if self._impl:
self._impl.remove_child(child._impl)

if self.window:
self.window.content.refresh()

@property
def app(self):
Expand All @@ -86,15 +132,14 @@ def app(self):

@app.setter
def app(self, app):
if self._app is not None:
if not (self._app is None or app is None):
if self._app != app:
raise ValueError("Widget %s is already associated with an App" % self)
elif app is not None:
elif self._impl:
self._app = app
self._impl.set_app(app)
if self._children is not None:
for child in self._children:
child.app = app
for child in self.children:
child.app = app

@property
def window(self):
Expand Down Expand Up @@ -135,10 +180,9 @@ def refresh(self):
if self._root:
self._root.refresh()
else:
super().refresh(self._impl.viewport)
self.refresh_sublayouts()
super().refresh(self._impl.viewport)

def refresh_sublayouts(self):
if self._children is not None:
for child in self._children:
child.refresh_sublayouts()
for child in self.children:
child.refresh_sublayouts()
Loading