Skip to content

Instantly share code, notes, and snippets.

@encela95dus
Created August 15, 2018 12:55
Show Gist options
  • Save encela95dus/1ea94076e60e4d58f8a92915ce3d3e0d to your computer and use it in GitHub Desktop.
Save encela95dus/1ea94076e60e4d58f8a92915ce3d3e0d to your computer and use it in GitHub Desktop.
simple_navigation_view.py
[
{
"class" : "View",
"attributes" : {
"name" : "Main View",
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {600, 400}}",
"selected" : false,
"nodes" : [
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "main view",
"font_name" : "<System>",
"name" : "label1",
"class" : "Label",
"alignment" : "left",
"frame" : "{{45, 104}, {150, 32}}",
"uuid" : "0B1C41A7-4126-448B-9280-0FC8579D0774"
},
"frame" : "{{224, 166}, {150, 55}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"uuid" : "1BB1CA87-874C-408E-8536-7E328159B638",
"name" : "button1",
"action" : "button_action",
"class" : "Button",
"frame" : "{{260, 184}, {80, 32}}",
"title" : "print_view_name"
},
"frame" : "{{218, 256}, {156, 48}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
[
{
"class" : "View",
"attributes" : {
"name" : "Subview1",
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {600, 400}}",
"selected" : false,
"nodes" : [
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "Subview 1",
"font_name" : "<System>",
"name" : "label1",
"class" : "Label",
"alignment" : "left",
"frame" : "{{45, 104}, {150, 32}}",
"uuid" : "5ACF2819-96A2-4C54-89E4-D7E8963A15E1"
},
"frame" : "{{222, 171}, {150, 32}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"uuid" : "96E13161-8A5B-4430-956B-A02665598A82",
"name" : "button1",
"action" : "button_action",
"class" : "Button",
"frame" : "{{260, 184}, {80, 32}}",
"title" : "Button"
},
"frame" : "{{222, 263}, {144, 52}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
[
{
"class" : "View",
"attributes" : {
"name" : "Subview2",
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {600, 400}}",
"selected" : false,
"nodes" : [
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "Subview 2",
"font_name" : "<System>",
"name" : "label1",
"class" : "Label",
"alignment" : "left",
"frame" : "{{45, 104}, {150, 32}}",
"uuid" : "590803AC-D7E1-4D8A-B5B7-E7F2CAFDD034"
},
"frame" : "{{224, 174}, {150, 58}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"title" : "Button",
"name" : "button1",
"action" : "button_action",
"class" : "Button",
"frame" : "{{260, 184}, {80, 32}}",
"uuid" : "D957F725-49FD-4AB3-AF2D-C9B3693A8E79"
},
"frame" : "{{224, 284}, {150, 74}}",
"selected" : true,
"nodes" : [
]
}
]
}
]
import ui
from functools import partial
def button_action(sender):
print(sender.superview.name)
class SimpleNavigationView(ui.View):
def add_close_buttonitem(self, v):
close = ui.ButtonItem()
close.image = ui.Image.named('ionicons-close-24')
close.action = self.bt_close
v.left_button_items = [close]
def add_right_buttonitem(self, v, action):
right = ui.ButtonItem()
right.image = ui.Image.named('ionicons-arrow-right-b-24')
right.action = action
v.right_button_items = [right]
def bt_close(self, sender):
self.nav_view.superview.close()
def bt_subview(self, i, sender):
subview = ui.load_view(self.subviews_pyui[i])
if i < (len(self.subviews_pyui)-1):
self.add_right_buttonitem(subview, self.bt_subviews[i+1])
self.nav_view.push_view(subview)
def make_bt_subview(self):
for i in range(len(self.subviews_pyui)-1, 0, -1):
self.bt_subviews[i] = partial(self.bt_subview, i)
def __init__(self, w=600, h=400, subviews_pyui=None):
super().__init__(frame=(0,0,w,h))
self.subviews_pyui = subviews_pyui
self.bt_subviews = [None]*len(self.subviews_pyui)
if len(self.subviews_pyui) > 1:
self.main_view = ui.load_view(self.subviews_pyui[0])
self.make_bt_subview()
self.add_close_buttonitem(self.main_view)
self.add_right_buttonitem(self.main_view, self.bt_subviews[1])
self.nav_view = ui.NavigationView(self.main_view)
self.nav_view.width = w
self.nav_view.height = h
self.add_subview(self.nav_view)
SimpleNavigationView(w=600, h=400,
subviews_pyui=('navtest_mainview', 'navtest_subview1', 'navtest_subview2')
).present('sheet', hide_title_bar=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment