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 29 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 layout_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()
119 changes: 119 additions & 0 deletions examples/layout_test/layout_test/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import toga
from toga.style import Pack
from toga.constants import ROW, COLUMN, CENTER


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,
enabled=False,
)

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

self.button_reparent = toga.Button(
label='Reparent image',
style=Pack(padding=10, width=120),
on_press=self.reparent_image,
enabled=False,
)

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(children=[], style=Pack(direction=COLUMN, padding=10, flex=1))
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=60, height=60))

# this tests addind children during init, before we have an implementaiton
self.button_box = toga.Box(
children=[
self.button_add,
self.button_insert,
self.button_reparent,
self.button_remove,
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)
)

# add a couple of labels to get us started
# this tests adding children when we already have an impl but no window or app
for i in range(3):
self.add_label()

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

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

self.button_reparent.enabled = True
self.button_remove.enabled = True
self.button_add.enabled = False
self.button_insert.enabled = False

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

self.button_reparent.enabled = True
self.button_remove.enabled = True
self.button_add.enabled = False
self.button_insert.enabled = False

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

self.button_reparent.enabled = False
self.button_remove.enabled = False
self.button_add.enabled = True
self.button_insert.enabled = True

def reparent_image(self, sender):
if self.image_view.parent is self.button_box:
self.scroll_box.insert(0, self.image_view)
elif self.image_view.parent is self.scroll_box:
self.button_box.add(self.image_view)

def add_label(self, sender=None):
# this tests adding children when we already have an impl, window and app
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
29 changes: 20 additions & 9 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 @@ -64,9 +71,13 @@ def set_background_color(self, color):
# INTERFACE

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
child.container = self.container or self

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

def remove_child(self, child):
child.container = None

def add_constraints(self):
self.native.translatesAutoresizingMaskIntoConstraints = False
Expand Down
2 changes: 1 addition & 1 deletion src/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
include_package_data=True,
install_requires=[
'travertino>=0.1.0',
'travertino>=0.1.3',
'importlib_metadata;python_version<"3.8"',
],
tests_require=[
Expand Down
Loading