Skip to content

Instantly share code, notes, and snippets.

@Phuket2
Created July 29, 2016 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phuket2/06d09ee0f51214e91caa9fed5757418b to your computer and use it in GitHub Desktop.
Save Phuket2/06d09ee0f51214e91caa9fed5757418b to your computer and use it in GitHub Desktop.
themes_icon_view.py
'''
Pythonista Forum - @Phuket2
'''
import ui, editor, bz2, base64
#_pyui_file_name = 'booking.pyui'
__pyui_str__ ='''
QlpoOTFBWSZTWaO7pcYAGNlf4NVVUGd/9T/n3Y6/79/+IAAAQMAAYAv/A+B25OipHWmzGq
VFrRVbpiUohIELwkkp6I0jTxJ6QaABoDR6jeoE00AAABJQTBNNNNRU/UAmAAAAAAAABw0e
SaNGEaGBGCaNMgMRowQaAMDjJk0YhpoYCaGJo0yYgZGE0aaYQZMIkiInpNU9tFE80oDT1H
qaNNBoeoG1HoniTJ5IBEoECYo00mRTyTagGgZqaZAaHqGmg00GYvKkBQH41HgP9f74qFYW
gllIClVKIdJfyX1QwA4QkICGSwZGEHCDAQaoOQBVDJTKASSFwC6wQYhAiQAGBADBItxKAJ
UTAlw8/V5VcJggSCySCwgpIMIZ57KoMiiioaKCA+sQVLsASCkIxYqEIhCRSeuAEIotLAkU
gMRNKoiH6dUwwBE9XV2/u3FvqpVMkVK0ZfHsPKSQk54BHwYXr8/qx1+wzVaBh/QBRCioJR
VUGyAlswpKIBCKhgA0hcSCEJ2rKIEQgAtxOtRqEqCkW0UXQANiUqmbx7ee8zl1OPARlkF/
PftoIw4W4UQUg1ggMJRQOQXo5zZYg3dDr/PKmf010Q2Qshwu6Gq8UjJA80zQ9yCsk9Gck5
IGJJo5mzRtqUhWK9NMRaJdGWqtD7df4LVE9iOSyz6KlEz471WvEOB+eNJUNcABbyV1kFJy
cqnxoPyCWfkMlMgCpmoYWq4LEkmWLoXQveSSSSTPAmBMIYQpACCXM4GlJUktdCoEiSqCRK
IsCqQ02cRpHLOiFsS9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pAZIEQIRCMCKE1
oqasehTQJ0IOlDoE/LL1oqaRNCuPb6BDq67TrQq1jrMALjL9Ze0heTCF7WMaMIWKr76++g
YhWGLQAiGLRaoVEL/VMghshDUEEq1wABNPQTSLuSKcKEhR3dLjAA==
'''
from collections import deque
class ViewWalker(object):
# JonB
'''simple iterator for ui.View objects, capable of depth or breadth first traversal'''
def __init__(self,v,breadthfirst=False):
self._dq=deque([v])
self._breadth=breadthfirst
def __iter__(self):
'''required for iterator objects'''
return self
def __next__(self):
'''required for iterator objects. raise stopiteration once the queue is empty. '''
if not self._dq:
raise StopIteration
#pop next view...
if self._breadth:
v=self._dq.popleft()# oldest entry (FIFO)
else:
v=self._dq.pop() # newest entry (stack)
#then push its subviews
if hasattr(v,'subviews'):
self._dq.extend(v.subviews)
return v
def sub_view_objects(self, breadthfirst = False):
self._breadth=breadthfirst
return [s for s in self]
def sub_view_names(self, breadthfirst = False):
self._breadth=breadthfirst
return [s.name for s in self]
def pyui_decode(str):
s = bz2.decompress(base64.b64decode(str))
return s.decode('utf-8')
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
class ThemeViewer(ui.View):
def __init__(self, pyui_str , theme, *args, **kwargs):
#ui.load_view(_pyui_file_name,
#bindings={'MyClass': WrapInstance(self), 'self': self})
ui.load_view_str(pyui_str, bindings={'MyClass': WrapInstance(self), 'self': self})
super().__init__(*args, **kwargs)
self.tc = None
self.set_tint_color_cheat(theme)
self.theme = theme
self.border_width=.5
self.corner_radius = 6
self.border_color='darkgray'
self.update_view()
def update_view(self):
self['lb_theme'].border_color = 'darkgray'
self['lb_theme'].text = self.theme
# attempt to set all subviews border color to the correct
# tint color.only shown on obects that have a border_width set
for sv in ViewWalker(self).sub_view_objects():
sv.border_color = self.tc
def reset_theme(self, sender):
#print('in, reset_theme')
self.theme = sender.title
self.set_tint_color_cheat(self.theme)
editor.apply_ui_theme(self, theme_name = sender.title)
self.update_view()
def set_tint_color_cheat(self, theme):
# maybe this is a screw up....
#return
btn = ui.Button()
editor.apply_ui_theme(btn, theme_name = theme)
self.tc = btn.tint_color
def take_screenshot(self):
# take a screenshot of the ui.View.bounds
with ui.ImageContext(self.width, self.height) as ctx:
self.draw_snapshot()
return ctx.get_image()
if __name__ == '__main__':
# initial theme
theme = 'Cool Glow'
# decode the pyui str
pyui_str = pyui_decode(__pyui_str__)
tv = ThemeViewer(theme = theme , pyui_str = pyui_str)
editor.present_themed(tv, theme_name=theme, style='sheet', animated=False)
# take a screen shot of the view, could be used to create thumbnail
# images for example...
img = tv.take_screenshot()
# show the image in the console
img.show()
# write the snapshot to a png file
bytes = img.to_png()
with open(theme + '.png', 'wb') as file:
file.write(bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment