A previous post 
<https://groups.google.com/d/msg/leo-editor/X2whzxeFfME/6sPNcGlbBgAJ> shows 
how Leo's wrapper classes can *use* flx.PyComponents without *being* 
components themselves.  So Leo's wrappers are *plain python classes *that 
inherit from Leo's base classes.  It's a huge collapse in complexity.

In this post, I'll show how Leo's wrappers can access *all* the actions on 
*both* sides of the Py/JS divide. It's an amazing pattern that every flexx 
programmer should know.

Here is the present LeoBrowserGui wrapper class. To repeat, this class is 
*not* a flexx component:

class LeoBrowserGui(leoGui.NullGui):

    def __init__ (self, c):
        super().__init__(guiName='BrowserGui')
        self.c = c
        self.last_frame = LeoBrowserFrame(c)
        self.root = Root()
        
    def echo(self):
        self.root.echo('From LeoBrowser Gui')
        
    def tree_echo(self):
        self.root.main_window.tree.echo('From LeoBrowser Gui')

Using the Root class, the *plain *echo and tree_echo methods can 
successfully call the echo *actions* in the LeoApp class (a 
flx.PyComponent) and the LeoFlexxTree class (a flx.Widget) !!

Wow. Think about that for a moment or three.

And here is the Root class:

class Root(flx.PyComponent):
    '''
    This class allows *plain* python classes
    to access *component* classes.
    '''
        
    def __getattr__ (self, attr):
        return getattr(self.root, attr)

*Example*

Executing "echo" from the minibuffer executes this code in app.do_command:

print('app.do_command: %s' % command)
self.gui.echo()
self.gui.tree_echo()

The resulting output is:

app.do_command: echo
===== echo ===== From LeoBrowser Gui
  ## Output from the Py side.
JS: ===== tree echo ===== From LeoBrowser Gui
  ## Output from the JS side.

*Discussion*

__getattr__ is a standard technique. By itself, it's no big deal.

But the effect is spectacular.  The Root class allows *plain *python 
methods to execute flexx actions on either side of the Py/JS divide. 

The "echo" and "tree_echo" *methods *become *pseudo-actions*. That's why 
the Root class is in The Book.

All of the wrapper classes will instantiate their own instances of Root.  
This will allow them to call flx.actions as needed. In fact, the wrappers 
will need to call only these actions:

- Redraw the screen.
- Update the body text.
- Write something to the log.
- Update the status line.

The LeoWapp project is nearing completion.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to