I'm not sure I would call that a mixin -- really, all that's going on  
is Python is allowing you to call any function, anywhere.  In a  
language like Scala that does have what I'd consider "real" mixins  
(traits over there), then on_key_down would become a function  
available in TableWidget's "namespace".  (Not trying to make trouble  
here, just a pedantic note on terminology -- I quite like Python.)

Anyway, it seems like all you need is to implement your  
ArrowKeySelection functionality in one lib, and call its functions  
from your UI widget's method implementations.  There is the slight  
detail of getting ArrowKeySelection's contribution to the widget's  
state rolled in, but if you're using, say, a clojure map for your  
widget's state (hopefully wrapped in a ref to keep those multithread  
gremlins away), then it's easy enough to add an 'init' function to  
ArrowKeySelection's namespace that will add the fields it requires to  
any given map.  Your widgets can then hook into that when they're  
first initializing their state map.

- Chas

On Feb 11, 2009, at 3:55 AM, Jesse Aldridge wrote:

>
> I'm trying to re-implement some python stuff in clojure.  Here's how
> it works:  I have  a class called ArrowKeySelection.  I use this class
> as a mixin with various gui classes.  The class adds some abstract
> logic for handling keys - the arrow keys move a selector around a
> matrix and the enter key calls an activation method with the selected
> object.  The implementation details are left to the extending class.
>
> My python class looks something like this:
>
> class ArrowKeySelection:
>    def __init__(self):
>        self.selected_row, self.selected_col = (0,0)
>
>    def on_key_down(self, code):
>        if keycodes.is_arrow_key(code):
>            ... move the selector around
>        if code == "return" or code == "space":
>            ...
>            self.activate(selected)
>
>    def get_selected(self):
>        ...
>
>    def select(self, desired_object):
>        ...
>
>    def num_cols(self):
>        ...
>    def num_rows(self):
>        ...
>
>    ...
>
> ----------
>
> Then when I use it, I make something like this:
>
> ----------
>
> class TableWidget(HtmlWidget, ArrowKeySelection):
>    def __init__(self, strings):
>        HtmlWidget.__init__(self, None)
>        self.strings = strings
>
>        ArrowKeySelection.__init__(self)
>
>        self.set_html()
>
>    def set_html(self):
>        ... construct an html table from the current state
>
>    def on_key_down(self, code):
>        ArrowKeySelection.on_key_down(self, code)
>        self.set_html()
>
>    def activate(self, object):
>        ...
>
> ------------
>
> So what would be the best way to re-implement this functionality in
> clojure?
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to