Repository: kivy/kivy

Issue:
ShapedWindow doesn't work without button click
<!--
The issue tracker is a tool to address bugs.
Please use the #kivy IRC channel on freenode or Stack Overflow for
support questions, more information at https://git.io/vM1yQ.

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://git.io/vM1iE
    * prepare a short, runnable example that reproduces the issue
    * reproduce the problem with the latest development version of Kivy
    * double-check that the issue is indeed a bug and not a support request
-->

### Versions

* Python: 3.6.3
* OS: Windows 10 64 bit
* Kivy: 1.10.1.dev0
* Kivy installation method: wheel

### Description

I was using [this](https://github.com/kivy/kivy/blob/master/examples/miscellaneous/shapedwindow.py) example and wanted to create a shaped window from the starting without any button click. But apparently it only works when I place `win.shape_mode = 'default'` inside any button event (pressed or released) and perform the click event with mouse. I also tried with `trigger_action` to trigger button event automatically but it hangs the window. Same thing happens if I try with threading.Timer to hit `win.shape_mode = 'default'` again after few seconds.


Discussion:
I'm not quite sure what are you talking about. Could you elaborate? Here's a very simple example of how to run an already shaped window.

```python
from kivy.config import Config
Config.set('graphics', 'shaped', 1)

from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.properties import StringProperty


class ShapedWindow(App):
    def build(self):
        Window.shape_mode = 'colorkey'

        return Button()


if __name__ == '__main__':
    ShapedWindow().run()
```

Edit: removed unnecessary line that did nothing + added sccreenshot

![image](https://user-images.githubusercontent.com/8825439/32137914-50c9c178-bc29-11e7-997d-2bb4f39d4d47.png)

Well, looks like with `Window.shape_mode = 'colorkey'` it's working. But what about shape_mode 'default' or 'binalpha'? I was hoping to make it work without excluding any color; with an Alpha image.
@NafisAbdullahKhan it actually works, but it doesn't load properly unless your `Window` is the same size as the image you want to use for the shape (otherwise you get **errors** in the console). For example you can get this state after changing the image to `alpha_shape` and clicking on the `default` button. The same applies for `binalpha`. 

The first error you will see is after you press the `alpha_shape` button, that's because the `Window`'s size wasn't `(800, 600)` i.e. the size of `default_shape`. That's why you would probably see some 512x512 `Window` that isn't affected by any shape although from the first look it "should" (no, it really shouldn't).

These are the steps you need to use to actually shape the `Window` (I'm going to put them to docs asap):

* set `Window` size to the size of the shape (with `Config`!)
* set `Window` shape to some picture you want to use
* set the `shape_mode` for the `Window`

![image](https://user-images.githubusercontent.com/8825439/32137733-e3ed979e-bc25-11e7-996f-8d88ca7559e4.png)

```python
from kivy.config import Config
Config.set('graphics', 'shaped', 1)

# size first if you don't want to use Clock
#Config.set('graphics', 'width', 512)
#Config.set('graphics', 'height', 512)

from kivy.resources import resource_find
alpha_shape = resource_find(
    'data/logo/kivy-icon-512.png'
)

from kivy.clock import Clock

from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window


class ShapedWindow(App):
    def build(self):
        Window.size = (512, 512)
        Clock.schedule_once(self.set_shape)
        return Button()

    def set_shape(self, *args):
        Window.shape_image = alpha_shape
        Window.shape_mode = 'default'


if __name__ == '__main__':
    ShapedWindow().run()
```

Note: Notice the blue-ish strip in the circular Window. That's because of resizing Window after it has been created. Windows for whatever reason apparently applies some style to the Window although it shouldn't. That happens only if you resize the Window and isn't present if you set the size properly with `Config`

Resolving patch:
diff --git a/kivy/core/window/__init__.py b/kivy/core/window/__init__.py
--- a/kivy/core/window/__init__.py
+++ b/kivy/core/window/__init__.py
@@ -1134,6 +1134,14 @@ def _set_shape_mode(self, value):
        - `binalpha` - hides an alpha channel of the :attr:`shape_image`
        - `reversebinalpha` - shows only the alpha of the :attr:`shape_image`
 
+    .. note::
+        Before actually setting the mode make sure the Window has the same
+        size like the :attr:`shape_image`, preferably via Config before
+        the Window is actually created.
+
+        If the :attr:`shape_image` isn't set, the default one will be used
+        and the mode might not take the desired visual effect.
+
     .. versionadded:: 1.10.1
 
     :attr:`shape_mode` is an :class:`~kivy.properties.AliasProperty`.


Test patch: