Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
custom_simple_navigation_view.py
import ui
from functools import partial
class SimpleNavigationView(ui.View):
def push_view(self, v):
if self.current_view:
self.stack.append(self.current_view)
self.remove_subview(self.current_view)
self.add_left_buttonitem(self.pop_view)
if v.bt_subview:
self.add_right_buttonitem(v.bt_subview)
else:
self.right_button_items = []
self.current_view = v
self.add_subview(v)
def pop_view(self, sender):
self.remove_subview(self.current_view)
self.current_view = self.stack.pop()
self.add_subview(self.current_view)
self.add_right_buttonitem(self.current_view.bt_subview)
if self.stack:
self.add_left_buttonitem(self.pop_view)
else:
self.left_button_items = []
def add_close_buttonitem(self):
close = ui.ButtonItem()
close.image = ui.Image.named('ionicons-close-24')
close.action = self.bt_close
self.left_button_items = [close]
def add_left_buttonitem(self, action):
left = ui.ButtonItem()
left.image = ui.Image.named('ionicons-arrow-left-b-24')
left.action = action
self.left_button_items = [left]
def add_right_buttonitem(self, action):
right = ui.ButtonItem()
right.image = ui.Image.named('ionicons-arrow-right-b-24')
right.action = action
self.right_button_items = [right]
def bt_close(self, sender):
self.close()
def bt_subview(self, i, sender):
subview = ui.load_view(self.subviews_pyui[i])
if i < (len(self.subviews_pyui)-1):
subview.bt_subview = self.bt_subviews[i+1]
else:
subview.bt_subview = None
self.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)
self.stack = []
self.current_view = None
if len(self.subviews_pyui) > 1:
self.main_view = ui.load_view(self.subviews_pyui[0])
self.make_bt_subview()
self.main_view.bt_subview = self.bt_subviews[1]
self.push_view(self.main_view)
SimpleNavigationView(w=600, h=400,
subviews_pyui=('navtest_mainview', 'navtest_subview1', 'navtest_subview2')
).present('sheet') #, hide_title_bar=True)
[
{
"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" : "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" : true,
"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" : [
]
}
]
}
]
import ui
from functools import partial
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
You can’t perform that action at this time.