Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created October 20, 2017 07:48
Show Gist options
  • Select an option

  • Save typemytype/0ec4f71a11597d5936c8941bff15b6d4 to your computer and use it in GitHub Desktop.

Select an option

Save typemytype/0ec4f71a11597d5936c8941bff15b6d4 to your computer and use it in GitHub Desktop.
import vanilla
import AppKit
class _CheckBoxManualBuild(vanilla.vanillaCheckBox._CheckBoxManualBuild):
# both the container view and the check box will be adjusted.
# this is necessary to create the appropriate buffer
# and to handle the alignment.
allFrameAdjustments = {
"mini": (0, -4, 0, 8),
"small": (0, -2, 0, 4),
"regular": (0, -2, 0, 4),
}
def __init__(self, posSize, title, callback=None, value=False, sizeStyle="regular"):
# This control is created by making a NSView that will contain
# a NSButton set to check box mode and another NSButton set
# to borderless mode to show the text. These buttons have their
# callback set to this class, which then calls the callback
# assigned to this class.
self._setupView("NSView", posSize)
self._callback = callback
buttonSizes = {
"mini": (10, 10),
"small": (18, 18),
"regular": (22, 22)
}
left, top, width, height = posSize
self.frameAdjustments = self.allFrameAdjustments[sizeStyle]
buttonWidth, buttonHeight = buttonSizes[sizeStyle]
buttonLeft, buttonTop = self.frameAdjustments[:2]
buttonLeft= abs(buttonLeft)
buttonTop = abs(buttonTop)
# adjust the position of the text button in relation to the check box
textBoxPosSize = {
# left, top, height
"mini": (12, 5, 12),
"small": (14, 6, 14),
"regular": (16, 3, 17)
}
textBoxLeft, textBoxTop, textBoxHeight = textBoxPosSize[sizeStyle]
textBoxWidth = 0
self._checkBox = vanilla.vanillaCheckBox._CheckBoxManualBuildButton((0, 0, buttonWidth, buttonHeight), "", callback=self._buttonHit, sizeStyle=sizeStyle)
self._checkBox.set(value)
self._textButton = vanilla.vanillaCheckBox._CheckBoxManualBuildTextButton((textBoxLeft, textBoxTop, textBoxWidth, textBoxHeight), title=title, callback=self._buttonHit, sizeStyle=sizeStyle)
class CheckBox(_CheckBoxManualBuild):
"""
A standard check box.::
from vanilla import *
class CheckBoxDemo(object):
def __init__(self):
self.w = Window((100, 40))
self.w.checkBox = CheckBox((10, 10, -10, 20), "A CheckBox",
callback=self.checkBoxCallback, value=True)
self.w.open()
def checkBoxCallback(self, sender):
print "check box state change!", sender.get()
CheckBoxDemo()
**posSize** Tuple of form (left, top, width, height) representing the position and size of
the check box. The size of the check box should match the appropriate value for the given *sizeStyle*.
+-------------------------+
| **Standard Dimensions** |
+---------+---+-----------+
| Regular | H | 22 |
+---------+---+-----------+
| Small | H | 18 |
+---------+---+-----------+
| Mini | H | 10 |
+---------+---+-----------+
**title** The text to be displayed next to the check box. Pass *None* is no title is desired.
**callback** The method to be called when the user changes the state of the check box.
**value** A boolean representing the state of the check box.
**sizeStyle** A string representing the desired size style of the check box. The options are:
+-----------+
| "regular" |
+-----------+
| "small" |
+-----------+
| "mini" |
+-----------+
"""
def __init__(self, posSize, title, callback=None, value=False, sizeStyle="regular"):
super(CheckBox, self).__init__(posSize=posSize, title=title, callback=callback, value=value, sizeStyle=sizeStyle)
w = vanilla.Window((600, 300))
h = 10
for title in ("Hello", "World"):
for sizeStyle in ("regular", "small", "mini"):
t = "%s %s" % (title, sizeStyle)
vanillaTitle = t + " vanilla"
ch = vanilla.CheckBox((10, h, 200, 22), vanillaTitle, sizeStyle=sizeStyle)
setattr(w, vanillaTitle, ch)
adjustedTitle = t + " adjusted"
ch = CheckBox((10+150, h, 200, 22), adjustedTitle, sizeStyle=sizeStyle)
setattr(w, adjustedTitle, ch)
nsButtonTitle = t + " NSSwitchButton"
ch = vanilla.vanillaCheckBox._CheckBoxStandardBuild((10+150+180, h, 200, 22), nsButtonTitle, sizeStyle=sizeStyle)
setattr(w, nsButtonTitle, ch)
h += 30
w.open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment