-
-
Notifications
You must be signed in to change notification settings - Fork 668
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
Column renderers #1478
base: main
Are you sure you want to change the base?
Column renderers #1478
Changes from 11 commits
38ad5e9
e794460
13ccfc4
1b2f086
f90a44b
99ace69
9a0673c
3b52674
ce0115a
a79ae1f
45ebace
e231cf5
95e5f1f
67846e0
34c9bdf
708ae75
6690292
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,4 @@ Reference | |
:members: | ||
:undoc-members: | ||
:inherited-members: | ||
:exclude-members: Column |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ Reference | |
:members: | ||
:undoc-members: | ||
:inherited-members: | ||
:exclude-members: Column |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from .base import Widget | ||
|
||
|
||
class Column(Widget): | ||
def create(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this is still a no-op. I'll need to become more familiar with android APIs to do anything useful here |
||
pass | ||
|
||
def set_title(self, value): | ||
pass | ||
|
||
def set_editable(self, value): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
|
||
from rubicon.objc import objc_property, send_super | ||
from rubicon.objc.runtime import objc_id | ||
|
||
from toga_cocoa.libs import ( | ||
NSAffineTransform, | ||
NSBezierPath, | ||
|
@@ -24,31 +28,39 @@ | |
NSLayoutConstraint, | ||
NSLayoutRelationEqual, | ||
NSLineBreakMode, | ||
NSButton, | ||
NSSwitchButton, | ||
at, | ||
objc_method, | ||
) | ||
|
||
|
||
class TogaIconView(NSTableCellView): | ||
class TogaTableCellView(NSTableCellView): | ||
|
||
imageView = objc_property(objc_id) | ||
checkbox = objc_property(objc_id) | ||
textField = objc_property(objc_id) | ||
|
||
@objc_method | ||
def setup(self): | ||
image_view = NSImageView.alloc().init() | ||
text_field = NSTextField.alloc().init() | ||
def initWithLayout(self): | ||
send_super(__class__, self, "init") | ||
self.imageView = NSImageView.alloc().init() | ||
self.checkbox = NSButton.alloc().init() | ||
self.textField = NSTextField.alloc().init() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How significant is the overhead of defining the image and checkbox widgets for every cell, and adding them to the layout, even if they're not used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. In my experience, the overhead has been small since in practice only the number of TogaTableCellView instances that are actually visible on screen will be created (maybe a few more than that) and then reused when scrolling. We might gain a bit by lazily allocating images and checkboxes as needed while slightly complicating the logic. |
||
|
||
# this will retain image_view and text_field without needing to keep | ||
# a Python reference | ||
self.addSubview(image_view) | ||
self.addSubview(text_field) | ||
self.addSubview(self.imageView) | ||
self.addSubview(self.checkbox) | ||
self.addSubview(self.textField) | ||
|
||
self.imageView = image_view | ||
self.textField = text_field | ||
self.checkbox.setButtonType(NSSwitchButton) | ||
self.checkbox.setAllowsMixedState(True) | ||
|
||
self.textField.cell.lineBreakMode = NSLineBreakMode.byTruncatingTail | ||
self.textField.bordered = False | ||
self.textField.drawsBackground = False | ||
|
||
self.imageView.translatesAutoresizingMaskIntoConstraints = False | ||
self.checkbox.translatesAutoresizingMaskIntoConstraints = False | ||
self.textField.translatesAutoresizingMaskIntoConstraints = False | ||
|
||
# center icon vertically in cell | ||
|
@@ -72,19 +84,40 @@ def setup(self): | |
None, NSLayoutAttributeNotAnAttribute, | ||
1, 16 | ||
) | ||
# center checkbox vertically in cell | ||
self.cb_vertical_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501 | ||
self.checkbox, NSLayoutAttributeCenterY, | ||
NSLayoutRelationEqual, | ||
self, NSLayoutAttributeCenterY, | ||
1, 0 | ||
) | ||
# align left edge of checkbox with right edge of icon | ||
self.cb_left_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501 | ||
self.checkbox, NSLayoutAttributeLeft, | ||
NSLayoutRelationEqual, | ||
self.imageView, NSLayoutAttributeRight, | ||
1, 5 | ||
) | ||
# set fixed width of checkbox | ||
self.cb_width_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501 | ||
self.checkbox, NSLayoutAttributeWidth, | ||
NSLayoutRelationEqual, | ||
None, NSLayoutAttributeNotAnAttribute, | ||
1, 16 | ||
) | ||
# align text vertically in cell | ||
self.tv_vertical_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501 | ||
self.textField, NSLayoutAttributeCenterY, | ||
NSLayoutRelationEqual, | ||
self, NSLayoutAttributeCenterY, | ||
1, 0, | ||
) | ||
# align left edge of text with right edge of icon | ||
# align left edge of text with right edge of checkbox | ||
self.tv_left_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501 | ||
self.textField, NSLayoutAttributeLeft, | ||
NSLayoutRelationEqual, | ||
self.imageView, NSLayoutAttributeRight, | ||
1, 5 # 5 pixels padding between icon and text | ||
self.checkbox, NSLayoutAttributeRight, | ||
1, 5 # 5 pixels padding between checkbox and text | ||
) | ||
# align right edge of text with right edge of cell | ||
self.tv_right_constraint = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_( # NOQA:E501 | ||
|
@@ -97,36 +130,40 @@ def setup(self): | |
self.addConstraint(self.iv_vertical_constraint) | ||
self.addConstraint(self.iv_left_constraint) | ||
self.addConstraint(self.iv_width_constraint) | ||
self.addConstraint(self.cb_vertical_constraint) | ||
self.addConstraint(self.cb_left_constraint) | ||
self.addConstraint(self.cb_width_constraint) | ||
self.addConstraint(self.tv_vertical_constraint) | ||
self.addConstraint(self.tv_left_constraint) | ||
self.addConstraint(self.tv_right_constraint) | ||
|
||
return self | ||
|
||
@objc_method | ||
def setImage_(self, image): | ||
|
||
if not self.imageView: | ||
self.setup() | ||
|
||
if image: | ||
self.imageView.image = image | ||
# set icon width to 16 | ||
self.iv_width_constraint.constant = 16 | ||
# add padding between icon and text | ||
self.tv_left_constraint.constant = 5 | ||
self.cb_left_constraint.constant = 5 | ||
else: | ||
self.imageView.image = None | ||
# set icon width to 0 | ||
self.iv_width_constraint.constant = 0 | ||
# remove padding between icon and text | ||
self.tv_left_constraint.constant = 0 | ||
self.cb_left_constraint.constant = 0 | ||
|
||
@objc_method | ||
def setText_(self, text): | ||
def setCheckState_(self, value): | ||
if isinstance(value, int): | ||
self.cb_width_constraint.constant = 16 | ||
self.tv_left_constraint.constant = 5 | ||
self.checkbox.state = value | ||
|
||
if not self.imageView: | ||
self.setup() | ||
if value is None: | ||
self.cb_width_constraint.constant = 0 | ||
self.tv_left_constraint.constant = 0 | ||
|
||
self.textField.stringValue = text | ||
@objc_method | ||
def setText_(self, text): | ||
self.textField.stringValue = text or "" | ||
|
||
|
||
# A TogaDetailedCell contains: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment is now out of date; plus, the demo has now lost the icons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. It will regain the icons once the API for that is set.