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

Add support for treectrl #24

Open
datajango opened this issue Feb 20, 2023 · 5 comments
Open

Add support for treectrl #24

datajango opened this issue Feb 20, 2023 · 5 comments

Comments

@datajango
Copy link

Add support for https://docs.wxpython.org/wx.TreeCtrl.html

@datajango
Copy link
Author

What is the plan for implementing complex control specific API's?

Below is my first attempt at a treectrl.

@mount.register(wx.TreeCtrl)
def treectrl(element, parent):
    instance = wx.TreeCtrl(parent, style=wx.LC_REPORT)
    instance.self_managed = True
    return update(element, instance)

@update.register(wx.TreeCtrl)
def treectrl(element, instance: wx.TreeCtrl):
    props = {**element['props']}
    if 'style' in props:
        del props['style']
    set_basic_props(instance, props)
    if 'root' in props:
        value = props.get('root')
        root = instance.AddRoot(value)
   return instance
I also need to add more support for methods to add items:
   self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.on_tree_item_right_click)
        self.root = self.AddRoot('Root ')
        self.SetItemHasChildren(self.root, True)

        self.node1 = self.AppendItem(self.root, 'Node 1')
        self.node2 = self.AppendItem(self.root, 'Node 2')
        self.SetItemHasChildren(self.node2, True)
        self.node3 = self.AppendItem(self.node2, 'Node 3')

@datajango
Copy link
Author

  if 'root' in props:
        value = props.get('root')
        root = instance.AddRoot(value)

Perhaps I should add a 'tree' rather than a 'root'? This tree format could be nested or flattened as I have done beklow:

{ 
  'root': {
      'label': 'Root',
      'data': {
           'id': '123456'
      },
      'children': [
          'node1', 'node2'
     ]
  },
  'node1': {
       'label': 'Node 1',
      'data': {
           'id': '123457'
       }
   },
  'node2': {
       'label': 'Node 2',
      'data': {
           'id': '123458'
       }
   }
}


@jamesdbrock
Copy link
Collaborator

jamesdbrock commented Feb 21, 2023

Perhaps I should add a 'tree' rather than a 'root'? This tree format could be nested or flattened as I have done below:

Yes, exactly. re-wx is a Model-View-Update framework, so we should never allow the view to update itself, like this:

   self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.on_tree_item_right_click)
       self.root = self.AddRoot('Root ')
       self.SetItemHasChildren(self.root, True)

The view should update the model only.

The model is the tree props. The tree props should specify the entire tree. On every render, the component should delete the entire tree from the root and then re-create the tree from the tree props.

And if I were you, I would not flatten the tree props. I would express it something like this.

{
      'text': 'Root',
      'children': [{
          'text': 'Node 1',
          'on_tree_item_right_click': self.handle_tree_item_right_click,
       },
       {
          'text': 'Node 2',
          'on_tree_item_right_click': self.handle_tree_item_right_click,
       }]
}

The ids should be chosen invisibly by the component.

@jamesdbrock
Copy link
Collaborator

TreeCtrl is going to be a very difficult component to wrap. Because really what we want to do is diffing and reconciliation on the tree. That's how we would avoid blindly deleting the entire tree and recreating it with every render. But we can’t use the re-wx patch diffing and reconciliation function on the tree, since the tree items are not wx.Windows with children.

@jamesdbrock
Copy link
Collaborator

The ComboBox control deletes and re-creates all of its elements on every render.

re-wx/rewx/widgets.py

Lines 273 to 275 in 70116d8

# we blanket delete/recreate the items for now, which
# seems to be Good Enough. Child diffing could be benchmarked
# to see if it's worth the effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants