omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    [Idea/Share] starter for Custom ui.View, orientation friendly, ui.Present condense

    Pythonista
    orientation ui.view.present idea share
    3
    6
    4695
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phuket2
      Phuket2 last edited by

      I think I got the below code right. A lot of variations to test. But even if it's not 100% still illustrates one strategy to start writing a custom ui.View that orientation friendly. Hmm, well I think it is. I have only tried on my iPad Pro. But is suppose to be. But as far as I can see you need to go through these steps. I know some of the code could be simplified, but if there is a simpler way to do the same, love to hear about it. Anyway, it's like the testlab, just trying.

      '''
      	Pythonista Forum - @Phuket2
      	
      	idea for a starter .py that helps with getting ready to build a 
      	custom view that is orientation friendly, and trying to combine
      	the ui.View.present method
      '''
      import ui, editor
      
      def calc_view_frame(pres, f):
      	style = pres.get('style', '')
      	m_bar = pres.get('hide_title_bar', False)
      	m_bar_adj = 0
      	
      	if not m_bar:
      		m_bar_adj = 64
      	
      	# sidebar depreciated...will also give a error, mapped to full_screen
      	style = style if style != 'sidebar' else 'full_screen'
      	
      	if style is 'sheet':
      		return f
      	elif style is 'panel':
      		w, h = ui.get_screen_size()
      		return (0, 0, w, h - 96)
      	elif style is 'popover':
      		return f
      	else:
      		w, h = ui.get_screen_size()
      		return (0, 0, w, h - m_bar_adj)
      	
      def present_me(v, modal=False, theme=None, *args, **kwargs):
      	from time import sleep
      	'''
      		present_me - combine ui.View.present operations
      	'''
      		
      	if not theme:
      		v.present(*args, **kwargs)
      	else:
      		# editor is also passing the title_bar_color and title_color
      		# kwarg, have to remove it from kwargs, otherwise
      		# we get an error
      		if kwargs.get('title_bar_color', None):
      			kwargs.pop('title_bar_color')
      			
      		if kwargs.get('title_color', None):
      			kwargs.pop('title_color')
      			
      		editor.present_themed(v, theme_name=theme, *args, **kwargs)
      		
      		# if animated is False, we enter a very small delay.  if this
      		# is not done, can get errorous screen sizes reported
      		animated = kwargs.get('animated', None)
      		if not animated:
      			sleep(.001)
      			
      	if modal:
      		v.wait_modal()
      				
      class MyClass(ui.View):
      	def __init__(self, *args, **kwargs):
      		super().__init__(*args, **kwargs)
      		print('init - bounds', self.bounds)	
      		
      	def layout(self):
      		print('layout - bounds', self.bounds)	
      					
      if __name__ == '__main__':
      	_use_theme = False
      	w, h = 600, 800
      	f = (0, 0, w, h)
      	
      	pres = \
      	{
      		'style': 'sheet',
      		'hide_title_bar': False,
      		'animated': True,
      		'title_bar_color': 'blue',
      		'title_color': 'orange',
      		'popover_location': (0,0),
      		'orientations': None,
      	}
      	theme = 'Oceanic'
      	#theme = ''
      	modal = False
      	
      	f = calc_view_frame(pres,f)
      	
      	mc = MyClass(frame=f, bg_color='white', name = 'present_me')
      	present_me(mc, modal = True, theme = theme, **pres)
      	
      	print('finished')
      
      
      1 Reply Last reply Reply Quote 0
      • Phuket2
        Phuket2 last edited by

        I should mention that one of the goals I was going for was to get the bounds to be the same in the init call of the class as well as the layout method. Makes it so much easier to size objects as well as apply flex attrs. Well for me it does anyway.

        1 Reply Last reply Reply Quote 0
        • TutorialDoctor
          TutorialDoctor last edited by

          Indeed, the scientific method does apply to "computer science." Always wanted to be a scientist myself and never thought I would end up programming. Tests are perfectly okay.

          Phuket2 1 Reply Last reply Reply Quote 0
          • Phuket2
            Phuket2 @TutorialDoctor last edited by

            @TutorialDoctor , yes agreed. I think sharing the tests are ok. People have a in built desire to improve on things. I have seen many good things come from simple discussions here. Just by people wanting to improve on what's presented. What's nice here, it's done without ego. I feel embarrassed for myself sometimes when I post something stupid. But the replies are always constructive.

            What I have put here is very straight fwd. And really only important if you want the flexibility to mix presentation styles as well as dealing with hidden_title_bar etc. I think in the near future omz will expand the ui.View.present method to handle themes.

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              themes are handled in th editor module:
              editor.present_themed(ui_view, theme_name=None, **kwargs)

              Phuket2 1 Reply Last reply Reply Quote 0
              • Phuket2
                Phuket2 @JonB last edited by

                @JonB , yeah I know themes are handled in the editor module. At least for now. Themes are very complex, I know you know. So who knows we're they will end up once @omz thinks about it more. There are so many side effects with themes. Or let's say decisions to be made about implementation/API. maybe it will just stay in the editor module.even On my best day, I am glad it's not me deciding... It's very tricky

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors