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

Column renderers #1478

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
1 change: 1 addition & 0 deletions docs/reference/api/widgets/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Reference
:members:
:undoc-members:
:inherited-members:
:exclude-members: Column
1 change: 1 addition & 0 deletions docs/reference/api/widgets/tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Reference
:members:
:undoc-members:
:inherited-members:
:exclude-members: Column
2 changes: 1 addition & 1 deletion examples/tree_source/tree_source/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def can_have_children(self):
# Property that returns the first column value as (icon, label)
@property
def name(self):
return self._icon, self.path.name
Copy link
Member

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.

Copy link
Member Author

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.

return self.path.name

# Property that returns modified date as str
@property
Expand Down
2 changes: 2 additions & 0 deletions src/android/toga_android/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .widgets.slider import Slider
from .widgets.switch import Switch
from .widgets.table import Table
from .widgets.tablecolumn import Column
from .widgets.textinput import TextInput
from .widgets.timepicker import TimePicker
from .widgets.webview import WebView
Expand All @@ -38,6 +39,7 @@ def not_implemented(feature):
"Button",
"Canvas",
"Command",
"Column",
"DatePicker",
"Font",
"Icon",
Expand Down
54 changes: 31 additions & 23 deletions src/android/toga_android/widgets/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ def onClick(self, view):


class Table(Widget):
table_layout = None
color_selected = None
color_unselected = None
selection = {}
_deleted_column = None
_font_impl = None

def create(self):

self.selection = {}
self._deleted_column = None

# get the selection color from the current theme
current_theme = MainActivity.singletonThis.getApplication().getTheme()
attrs = [R__attr.colorBackground, R__attr.colorControlHighlight]
Expand All @@ -68,6 +67,7 @@ def create(self):
parent_layout_params.gravity = Gravity.TOP
parent.setLayoutParams(parent_layout_params)
vscroll_view = ScrollView(self._native_activity)

# add vertical scroll view
vscroll_view_layout_params = LinearLayout__LayoutParams(
LinearLayout__LayoutParams.MATCH_PARENT,
Expand All @@ -79,6 +79,7 @@ def create(self):
TableLayout__Layoutparams.MATCH_PARENT,
TableLayout__Layoutparams.WRAP_CONTENT
)

# add horizontal scroll view
hscroll_view = HorizontalScrollView(self._native_activity)
hscroll_view_layout_params = LinearLayout__LayoutParams(
Expand All @@ -91,14 +92,15 @@ def create(self):
# add table layout to scrollbox
self.table_layout.setLayoutParams(table_layout_params)
hscroll_view.addView(self.table_layout)

# add scroll box to parent layout
parent.addView(vscroll_view, vscroll_view_layout_params)
self.native = parent
if self.interface.data is not None:
self.change_source(self.interface.data)

def change_source(self, source):
self.selection = {}
self.selection.clear()
self.table_layout.removeAllViews()
if source is not None:
self.table_layout.addView(self.create_table_header())
Expand All @@ -111,7 +113,7 @@ def clear_selection(self):
for i in range(self.table_layout.getChildCount()):
row = self.table_layout.getChildAt(i)
row.setBackgroundColor(self.color_unselected)
self.selection = {}
self.selection.clear()

def create_table_header(self):
table_row = TableRow(MainActivity.singletonThis)
Expand All @@ -120,16 +122,20 @@ def create_table_header(self):
TableRow__Layoutparams.WRAP_CONTENT
)
table_row.setLayoutParams(table_row_params)
for col_index in range(len(self.interface._accessors)):
if self.interface._accessors[col_index] == self._deleted_column:

for column in self.interface.columns:
if column is self._deleted_column:
continue

text_view = TextView(MainActivity.singletonThis)
text_view.setText(self.interface.headings[col_index])
text_view.setText(column.title)

if self._font_impl:
text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP, self._font_impl.get_size())
text_view.setTypeface(self._font_impl.get_typeface(), Typeface.BOLD)
else:
text_view.setTypeface(text_view.getTypeface(), Typeface.BOLD)

text_view_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT,
TableRow__Layoutparams.WRAP_CONTENT
Expand All @@ -138,9 +144,13 @@ def create_table_header(self):
text_view_params.gravity = Gravity.START
text_view.setLayoutParams(text_view_params)
table_row.addView(text_view)

return table_row

def create_table_row(self, row_index):

row = self.interface.data[row_index]

table_row = TableRow(MainActivity.singletonThis)
table_row_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT,
Expand All @@ -150,14 +160,18 @@ def create_table_row(self, row_index):
table_row.setClickable(True)
table_row.setOnClickListener(TogaOnClickListener(impl=self))
table_row.setId(row_index)
for col_index in range(len(self.interface._accessors)):
if self.interface._accessors[col_index] == self._deleted_column:

for column in self.interface.columns:
if column is self._deleted_column:
continue

text_view = TextView(MainActivity.singletonThis)
text_view.setText(self.get_data_value(row_index, col_index))
text_view.setText(column.get_data_for_node(row, "text"))

if self._font_impl:
text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP, self._font_impl.get_size())
text_view.setTypeface(self._font_impl.get_typeface(), self._font_impl.get_style())

text_view_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT,
TableRow__Layoutparams.WRAP_CONTENT
Expand All @@ -166,14 +180,8 @@ def create_table_row(self, row_index):
text_view_params.gravity = Gravity.START
text_view.setLayoutParams(text_view_params)
table_row.addView(text_view)
return table_row

def get_data_value(self, row_index, col_index):
if self.interface.data is None or self.interface._accessors is None:
return None
row_object = self.interface.data[row_index]
value = getattr(row_object, self.interface._accessors[col_index])
return value
return table_row

def get_selection(self):
selection = []
Expand Down Expand Up @@ -210,11 +218,11 @@ def set_on_select(self, handler):
def set_on_double_click(self, handler):
self.interface.factory.not_implemented('Table.set_on_double_click()')

def add_column(self, heading, accessor):
def add_column(self, column):
self.change_source(self.interface.data)

def remove_column(self, accessor):
self._deleted_column = accessor
def remove_column(self, column):
self._deleted_column = column
self.change_source(self.interface.data)
self._deleted_column = None

Expand Down
12 changes: 12 additions & 0 deletions src/android/toga_android/widgets/tablecolumn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .base import Widget


class Column(Widget):
def create(self):
Copy link
Member Author

Choose a reason for hiding this comment

The 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
2 changes: 2 additions & 0 deletions src/cocoa/toga_cocoa/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .widgets.splitcontainer import SplitContainer
from .widgets.switch import Switch
from .widgets.table import Table
from .widgets.tablecolumn import Column
from .widgets.textinput import TextInput
from .widgets.tree import Tree
from .widgets.webview import WebView
Expand Down Expand Up @@ -57,6 +58,7 @@ def not_implemented(feature):
'Box',
'Button',
'Canvas',
'Column',
'DetailedList',
'Divider',
'ImageView',
Expand Down
7 changes: 7 additions & 0 deletions src/cocoa/toga_cocoa/libs/appkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,13 @@ class NSLineBreakMode(Enum):
# NSTableCellView.h
NSTableCellView = ObjCClass('NSTableCellView')

######################################################################
# NSTableColumn.h

NSTableColumnNoResizing = 0
NSTableColumnAutoresizingMask = 1 << 0
NSTableColumnUserResizingMask = 1 << 1

######################################################################
# NSTableView.h
NSTableColumn = ObjCClass('NSTableColumn')
Expand Down
91 changes: 64 additions & 27 deletions src/cocoa/toga_cocoa/widgets/internal/cells.py
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,
Expand All @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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
Expand All @@ -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:
Expand Down
Loading