Trying to write a palette plugin that allows for activating instances from the sidebar.
I got it mostly working but somehow I can’t figure out how to adjust the actual palette window frame height to dynamically change according to the number of instances.
Any hints on how to make this work?
For example: The Light instance should be part of the list. (Also, I see ‘Multiline Label’ text in the .xib file…should I just set the title blank like this: title=""
?)
Here is the code:
from __future__ import division, print_function, unicode_literals
from GlyphsApp import *
from GlyphsApp.plugins import *
from AppKit import NSButton, NSMakeRect
class ActivateInstancesPalettePlugin (PalettePlugin):
dialog = objc.IBOutlet()
textField = objc.IBOutlet()
def settings(self):
self.name = Glyphs.localize({
'en': 'Activate Instances',
'de': 'Aktiviere Instanzen',
'fr': 'Activer les instances',
'es': 'Activar instancias',
'pt': 'Ativar instâncias',
})
# Load .nib dialog (without .extension)
self.loadNib('IBdialog', __file__)
def start(self):
# Adding a callback for the 'GSUpdateInterface' event
Glyphs.addCallback(self.update, UPDATEINTERFACE)
def __del__(self):
Glyphs.removeCallback(self.update)
@objc.IBAction
def checkBoxAction_(self, sender):
# Retrieve instance by sender's title
instance = [i for i in Glyphs.font.instances if i.name == sender.title()][0]
instance.active = sender.state()
@objc.python_method
def update(self, sender=None):
font = Glyphs.font
if font is not None:
instances = sorted(font.instances, key=lambda instance: font.instances.index(instance))
num_instances = len(instances)
checkbox_height = 22
padding = 5
# Calculate the required height for the checkboxes
checkboxes_height = checkbox_height * num_instances
# Calculate the overall height, including padding and space for the dialog title and buttons
overall_height = checkboxes_height + padding * 2 + 10 # Adjust the value based on your needs
width = 160
# Adjust the size of the dialog box
self.dialog.setFrame_(NSMakeRect(self.dialog.frame().origin.x, self.dialog.frame().origin.y, width, overall_height))
# remove old checkboxes
for checkbox in self.dialog.subviews():
if isinstance(checkbox, NSButton) and checkbox.title() in [instance.name for instance in instances]:
checkbox.removeFromSuperview()
# Add new checkboxes
for i, instance in enumerate(instances):
yPos = overall_height - padding - checkbox_height - i * (checkbox_height + padding) # Calculate y-coordinate from top to bottom
checkbox = NSButton.alloc().initWithFrame_(NSMakeRect(12, yPos, width - 12, checkbox_height))
checkbox.setButtonType_(3) # Set to checkbox
checkbox.setTitle_(instance.name)
checkbox.setState_(instance.active)
checkbox.setTarget_(self)
checkbox.setAction_(objc.selector(self.checkBoxAction_, signature=b"v@:@"))
self.dialog.addSubview_(checkbox)
def __file__(self):
"""Please leave this method unchanged"""
return __file__