SOLVED Color List



  • Hey! Me again:)

    I had a question regarding the color/text lists in RoboFont e.g. the preferences' appearance list. Would you be able to explain how this was done? I've been trying to figure out how you did it for a week+ but no luck. I got like 80% of the way there but is the color block just a drawn rectangle, but then I am confused how it has the attributes of a color well? Thanks in advance!

    C



  • @connor happy to help. thank you & everyone else for asking questions, I’m learning a lot too!



  • Gustavo, Thank you so much for sharing this! I was going about this is a much more convoluted way, so this is really helpful!

    Thank you guys for being so supportive on the forum:)

    C



  • hello @connor,

    color cells in lists are made with a custom RFColorCell object from the internal lib module. you can use stuff from the lib module in your scripts, but ⚠️ the API may change over time, without a nice deprecation warning like in mojo.

    (color cells are super useful. maybe they can be moved to vanilla or defconAppKit? @frederik @tal)

    here’s an example script showing a vanilla list with editable color cells:

    Screen Shot 2019-04-25 at 16.46.35.png

    from AppKit import NSColor
    from vanilla import Window, List
    from lib.cells.colorCell import RFColorCell, RFPopupColorPanel
    from lib.tools.misc import NSColorToRgba
    
    class ListWithColorCellsExample:
    
        def __init__(self, aList):
            columnDescriptions = [
                dict(title="Name", key="name", editable=True),
                dict(title="Color", key="color", cell=RFColorCell.alloc().initWithDoubleClickCallback_(self.colorDoubleClickCallback)),
            ]
            self.w = Window((300, 200), title='list with color cells')
            self.w.list = List((10, 10, -10, -10), [],
                        columnDescriptions=columnDescriptions,
                        allowsEmptySelection=False,
                        allowsMultipleSelection=False,
                        selectionCallback=self.selectionCallback)
            self.load(aList)
            self.w.open()
    
        def load(self, aList):
            items = []
            for name, color in aList:
                item = {
                    'name'  : name,
                    'color' : NSColor.colorWithCalibratedRed_green_blue_alpha_(*color),
                }
                items.append(item)
            self.w.list.set(items)
    
        def getSelectedItem(self):
            items = self.w.list.get()
            if len(items):
                i = self.w.list.getSelection()[0]
                return items[i]
    
        def selectionCallback(self, sender):
            item = self.getSelectedItem()
            print(item['name'], NSColorToRgba(item['color']))
    
        def colorDoubleClickCallback(self, sender):
            item = self.getSelectedItem()
            self.popupColorPanel = RFPopupColorPanel(self.setColorCallback, item['color'], alpha=True)
    
        def setColorCallback(self, sender):
            item = self.getSelectedItem()        
            item['color'] = sender.color()
    
    myList = [
        ('color 1', (0, 1, 0, 1)),
        ('color 2', (1, 0, 0, 1)),
        ('color 3', (0, 0, 1, 1)),
    ]
    
    ListWithColorCellsExample(myList)
    

    enjoy! 🎨


Log in to reply