Repository: kivy/kivy

Issue:
Live window resizing
Currently, a Kivy window supports resizing by allowing the user to click and drag the window border, similar to most other applications.
However, the user can not see how the new size affects the application until they release the mouse button, so Kivy applications do not feel to be as responsive or fluid as other applications are.
I would like to have the application resize event to occur while the user is dragging the mouse border, rather than only when the user releases the mouse pointer.
## <bountysource-plugin>

Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/7667215-live-window-reszing?utm_campaign=plugin&utm_content=tracker%2F42681&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F42681&utm_medium=issues&utm_source=github).
</bountysource-plugin>



Discussion:
This appears to be the case in Kivy 1.8, using pygame on windows and mac os x.

This also occurs in Kivy 1.9 (Latest master, as of yesterday), using SDL2 on mac os x.

Hello. Just for the fun of it I went into the trouble writing some C++ code (it's been 17 yrs lol). Also I understand it's a nice touch of polish for any application.
**Short answer**: Not possible.
**Long answer**: Requires a lot of platform specific code.

[Example with pygame](http://www.pygame.org/wiki/WindowResizing?parent=CookBook), this what Kivy does (99% certain, see [on_resize(width, height)](http://kivy.org/docs/api-kivy.core.window.html)) and as you said it does not work.

On Windows, we have two events:
[WM_SIZE](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632646%28v=vs.85%29.aspx)
[WM_SIZING](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632647%28v=vs.85%29.aspx)
WM_SIZING is the one we want, but despite being declared in [SDL](https://fossies.org/dox/SDL2-2.0.3/wmmsg_8h_source.html) it's likely that it's not wrapped on SDL_WINDOWEVENT which includes [SDL_WINDOW_RESIZED and SDL_WINDOW_SIZE_CHANGED](https://wiki.libsdl.org/SDL_WindowEventID)

If SDL2 fires the event SDL_WINDOW_SIZE_CHANGED when it receives WM_SIZING from Windows operating system, then we might have a solution for Windows only. But that's platform specific and probably against the cross-platform library philosophy. The root of the problem is that there are many different event messages to handle, and this one message seems to be implemented very differently every time.

Here is the log:

```
INFO: Window 1 gained keyboard focus
INFO: Window 1 shown
INFO: Window 1 exposed
INFO: Window 1 moved to 538,197
INFO: Mouse entered window 1
INFO: Mouse left window 1
INFO: Window 1 exposed
...
INFO: Window 1 exposed
INFO: Window 1 size changed to 731x566
INFO: Window 1 resized to 731x566
INFO: Mouse entered window 1
INFO: Mouse left window 1
INFO: Window 1 closed
The program '[4688] Print_SDL_WindowEvent.exe' has exited with code 0 (0x0).
```

Hello victor-rene,

I have it working using SDL2 on the mac, with changes to window_sdl2.py and corresponding pyx files, although it is probably a bit of a hack.
- So it's definitely working for the mac.
- And will probably work for windows as well, assuming that the platform sends the event (which is likely).

I think it may well work on windows because I am not using anything mac-specific (only SDL2), however I am hoping to avoid compiling kivy for windows at this stage.

The problem with SDL2 is that the events stop firing until the resize event has completed:
- My workaround is to filter the SDL event loop (ie, catch the event before it enters the SDL event system), SDL allows these hooks using SDL_SetEventFilter:

The code I have in _window_sdl2.pyx is:

```
cdef extern from "SDL_events.h":
     ctypedef int (*SDL_EventFilter)(void *, const SDL_Event*)
     cdef void SDL_SetEventFilter(SDL_EventFilter filter, void * userdata)

EVENT_FILTER_HANDLERS = [] # A bit of a hack (I'm very new to kivy)

cdef int eventFilter(void * userdata, SDL_Event * event):
    for handler in EVENT_FILTER_HANDLERS:
        if event.type == SDL_WINDOWEVENT:
            if event.window.event == SDL_WINDOWEVENT_RESIZED:
                action = ('windowresized', event.window.data1, event.window.data2)
                handler(action)
    return 1
```

This event filter needs to be installed after the SDL_Init routine has completed, like follows:

```
    if SDL_Init(SDL_INIT_VIDEO| SDL_INIT_JOYSTICK) < 0:
        self.die()
    SDL_SetEventFilter(eventFilter, <void*>0)
```

So, on the mac at least, using SDL2, it's certainly possible to grab the event, it should be possible on windows as well without any additional code.

In window_sdl2.py, I add a resize handler to the EVENT_FILTER_HANDLERS within the create_window method, and this is implemented as follows:

```
def _onEventFilterEvent(self, action):
    if action[0] == 'windowresized':
        self._size = action[1:]
        self._win.resize_window(*self._size)
        EventLoop.idle() # <- Force kivy to render the frame now, so that the canvas is drawn.
```

@arafangion Gratz :+1:. That's what I was describing, you have to write additional code to get it to work. Now, is it something that will be implemented as default in Kivy, I can not answer. And it must be tested on other platforms.

@victor-rene Awesome! 

Would we be able to reduce the testing overhead by only applying this patch for platforms where it makes sense? (Namely: Windows and Mac OS X - it is possible that linux does not need this, and this whole issue is unneccessary on iOS and Android).

I am happy to volunteer to write the code for the SDL2 backend, restricted to mac os x and windows, and then send a pull request.
However, I am unable to do testing: I only have one mac laptop, and all my windows dev is in a virtual machine.

@arafangion My time schedule is a bit tight. But I volunteer to test anything related to Windows. I will follow this. Regards.

@arafagion, i believe sdl2 takes care of the platform differences, so we could support all desktop OSes, including Linux distros. You can ping me too if you need more testers.

@dessant, @victor-rene: Sadly I've lost the opportunity to investigate this further, as I am not working with kivy any more (Mostly due to not being able to rely on having OpenGL 2.0 or newer, though there were other factors).

Hello, I'd love to see this approached again, it's a valuable feature for folks targeting desktop applications with kivy. I know users would mention this if I used it. 

What next steps need to happen here? I'm not experienced with the platform yet but seems like the core research has at least been started. If there is a straight-forward contribution path I can put some effort in or help validate/test. 

Thank you!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Any further updates on this? I am still interested in seeing this work. This would help the professional look and feel of the framework. Does someone have a starting point that I could begin looking at? I am new to kivy, But, I noticed this issue immediately upon running demo applications. I think this issue is key to showing the maturity of the platform.
I also have this issue on macOS HighSierra
Seems like the code is mostly ready to make a PR from it, and to test. Linux doesn't seem to be affected to me (i never saw this issue here), i did see this on OSX and Windows, i can adapt and make the PR, but needs over to test it in these platforms and give feedback.
```
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
[INFO   ] [GL          ] Backend used <gl>
[INFO   ] [GL          ] OpenGL version <b'2.1 ATI-1.66.31'>
[INFO   ] [GL          ] OpenGL vendor <b'ATI Technologies Inc.'>
[INFO   ] [GL          ] OpenGL renderer <b'AMD Radeon Pro 560 OpenGL Engine'>
[INFO   ] [GL          ] OpenGL parsed version: 2, 1
[INFO   ] [GL          ] Shading version <b'1.20'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...
```

macOS High Sierra 10.13.4

No pygame installed. Provider is SDL2. Same problem as described.
Following. Same problem windows.
Same problem with MacOS X 10.13.6, MacBookPro 15" (2017)
Same problem with MacOS X 10.13.6, python 3.6.5, kivy 1.9

Resolving patch:
diff --git a/kivy/core/window/window_sdl2.py b/kivy/core/window/window_sdl2.py
--- a/kivy/core/window/window_sdl2.py
+++ b/kivy/core/window/window_sdl2.py
@@ -204,7 +204,7 @@ def _set_minimum_size(self, *args):
     def _set_allow_screensaver(self, *args):
         self._win.set_allow_screensaver(self.allow_screensaver)
 
-    def _event_filter(self, action):
+    def _event_filter(self, action, *largs):
         from kivy.app import App
         if action == 'app_terminating':
             EventLoop.quit = True
@@ -237,6 +237,12 @@ def _event_filter(self, action):
                 app = App.get_running_app()
                 app.dispatch('on_resume')
 
+        elif action == 'windowresized':
+            self._size = largs
+            self._win.resize_window(*self._size)
+            # Force kivy to render the frame now, so that the canvas is drawn.
+            EventLoop.idle()
+
         return 0
 
     def create_window(self, *largs):


Test patch: