Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screen Rotation and Re-layout #1302

Closed
RobertFlatt opened this issue Jun 16, 2018 · 2 comments
Closed

Screen Rotation and Re-layout #1302

RobertFlatt opened this issue Jun 16, 2018 · 2 comments

Comments

@RobertFlatt
Copy link
Contributor

I am having a problem with screen rotate under Python for Android.

The example below does not re-layout on device rotation. The contents does rotate, but is not not resized for the new width and height. It does re-layout when I tap the screen after rotation.
It is compiled with "--orientation sensor". I am using p4a master branch dated 5/17/2018.

On device rotation, I'm looking for a way to trigger re-layout from Python. My code can implement the re-layout.

It appears that on_pause() and on_resume() are called when the app enters/returns from the background. Unlike Java on Android they are not called on rotation. Not my expectation, but I can see why it is that way.

It appears that on_rotate() is never called, either from the App class or layout class. What am I missing?

Once I have a trigger I assume I will have to get the new size using Java, because Python's Window.size does not appear to be updated till after re-layout.

Suggestions for handling screen rotation re-layout?

Thanks.

from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.app import App

Builder.load_string('''
<LifecycleScreen>:
    cols: 1
    Label:
        text: root.label0
    Label:
        text: root.label1
    Button:
        text: 'Reset'
        on_release: root.my_callback()
''')


class LifecycleScreen(GridLayout):
    label0 = StringProperty('Got Call:False,False,False.')
    label1 = StringProperty('Original Orientation')

    def on_rotate(self):
        label1 = StringProperty('Rotated')

    def my_callback(self):
        self.parent.pause = False
        self.parent.resume = False
        self.parent.rotate = False
        self.label1 = 'Orientation Reset'

class LifecycleApp(App):
    def __init__(self,**kwargs):
        super (LifecycleApp,self).__init__(**kwargs)
        self.screen = None
        self.pause = False
        self.resume = False
        self.rotate = False

    def build(self):
        self.screen = LifecycleScreen()
        return self.screen

    def label_text(self):
        self.screen.label0 = 'Got Call:{},{},{}.'.format(self.pause,
                                                         self.resume,
                                                         self.rotate)
        
    def on_pause(self):
        self.pause = True
        self.label_text()
        return True  
 
    def on_resume(self):
        self.resume = True
        self.label_text()

    def on_rotate(self):
        self.rotate = True
        self.label_text()


if __name__ == '__main__':
    LifecycleApp().run()





@RobertFlatt
Copy link
Contributor Author

First, the workaround I used is below. This depends on a heartbeat, there has to be a better way?!

Second, I notice Settings().add_kivy_panel() has a 'rotation' key. On Windows and Android this rotates the window on all subsequent window create(). The App class also has a on_config_change() callback which should respond to changes in this key, this callback is a new addition and not released.

From an Android/Java viewpoint rotation is (re) creation. I suggest if , on device rotation, the Kivy Android bootstrap were to set the value of the 'rotation' key (and the callback were released). There would be an effective on_rotate() callback. An enhancement request for your consideration.

class LifecycleApp(App):
    def __init__(self,**kwargs):
        super (LifecycleApp,self).__init__(**kwargs)
        self.screen = None
        self.x, self.y = Window.size

    def build(self):
        self.screen = LifecycleScreen()
        Clock.schedule_interval(self.potential_on_rotate,1)
        return self.screen

    def potential_on_rotate(self,dt):
        x,y = Window.size
        if self.x == y and self.y == x:
            self.x = x
            self.y = y
            self.on_rotate()

    def on_rotate(self):
        self.screen.canvas.ask_update()

@inclement
Copy link
Member

I think this is working better with SDL2.0.9, please feel free to reopen if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants