We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Fix octal literals in constants.py on the Windows platform, so it will work in Python 3.
The literals in the file won't work in Python 3, and by fixing them you can get as far as an empty window on Windows.

P.S. This is my first time doing a pull request on GitHub, hope this is ok.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/winforms/toga_winforms/libs.py`
Content:
```
1 import ctypes
2 
3 import clr
4 
5 clr.AddReference("System.Windows.Forms")
6 
7 import System.Windows.Forms as WinForms  # noqa: E402
8 from System import Decimal as ClrDecimal  # noqa: E402, F401
9 from System import Single  # noqa: E402, F401
10 from System import Convert  # noqa: E402, F401
11 from System import DateTime as WinDateTime  # noqa: E402, F401
12 from System import Threading  # noqa: E402, F401
13 from System import Uri  # noqa: E402, F401
14 from System import Environment  # noqa: E402, F401
15 
16 from System.Drawing import Icon as WinIcon  # noqa: E402, F401
17 from System.Drawing import Image as WinImage  # noqa: E402, F401
18 from System.Drawing import Font as WinFont  # noqa: E402, F401
19 from System.Drawing import ContentAlignment, Size, Point  # noqa: E402, F401
20 from System.Drawing import FontFamily, FontStyle, SystemFonts  # noqa: E402, F401
21 from System.Drawing import Text, Color, Bitmap  # noqa: E402, F401
22 from toga.constants import LEFT, RIGHT, CENTER, JUSTIFY  # noqa: E402
23 from toga.fonts import (
24     MESSAGE,
25     SYSTEM,
26     SERIF,
27     SANS_SERIF,
28     CURSIVE,
29     FANTASY,
30     MONOSPACE,
31 )  # noqa: E402
32 
33 user32 = ctypes.windll.user32
34 win_version = Environment.OSVersion.Version.Major
35 
36 
37 def TextAlignment(value):
38     return {
39         LEFT: ContentAlignment.MiddleLeft,
40         RIGHT: ContentAlignment.MiddleRight,
41         CENTER: ContentAlignment.MiddleCenter,
42         JUSTIFY: ContentAlignment.MiddleLeft,
43     }[value]
44 
45 
46 # Justify simply sets Left alignment. Is this the best option?
47 def HorizontalTextAlignment(value):
48     return {
49         LEFT: WinForms.HorizontalAlignment.Left,
50         RIGHT: WinForms.HorizontalAlignment.Right,
51         CENTER: WinForms.HorizontalAlignment.Center,
52         JUSTIFY: WinForms.HorizontalAlignment.Left,
53     }[value]
54 
55 
56 def add_handler(cmd):
57     action = cmd.action
58 
59     def handler(sender, event):
60         return action(None)
61 
62     return handler
63 
64 
65 def win_font_family(value):
66     win_families = {
67         SYSTEM: SystemFonts.DefaultFont.FontFamily,
68         MESSAGE: SystemFonts.MenuFont.FontFamily,
69         SERIF: FontFamily.GenericSerif,
70         SANS_SERIF: FontFamily.GenericSansSerif,
71         CURSIVE: FontFamily("Comic Sans MS"),
72         FANTASY: FontFamily("Impact"),
73         MONOSPACE: FontFamily.GenericMonospace,
74     }
75     for key in win_families:
76         if value in key:
77             return win_families[key]
78     if value in Text.InstalledFontCollection().Families:
79         return FontFamily(value)
80     else:
81         print(
82             "Unable to load font-family '{}', loading {} instead".format(
83                 value, SystemFonts.DefaultFont.FontFamily)
84         )
85         return SystemFonts.DefaultFont.FontFamily
86
```

Path: `src/winforms/toga_winforms/app.py`
Content:
```
1 import sys
2 
3 import toga
4 
5 from .libs import Threading, WinForms, add_handler, user32, win_version
6 from .window import Window
7 
8 
9 class MainWindow(Window):
10     def on_close(self):
11         pass
12 
13 
14 class App:
15     _MAIN_WINDOW_CLASS = MainWindow
16 
17     def __init__(self, interface):
18         self.interface = interface
19         self.interface._impl = self
20 
21     def create(self):
22         self.native = WinForms.Application
23 
24         if win_version >= 6:
25             user32.SetProcessDPIAware(True)
26         self.native.EnableVisualStyles()
27         self.native.SetCompatibleTextRenderingDefault(False)
28 
29         self.interface.commands.add(
30             toga.Command(None, 'About ' + self.interface.name, group=toga.Group.HELP),
31             toga.Command(None, 'Preferences', group=toga.Group.FILE),
32             # Quit should always be the last item, in a section on it's own
33             toga.Command(lambda s: self.exit(), 'Exit ' + self.interface.name, shortcut='q', group=toga.Group.FILE,
34                          section=sys.maxsize),
35             toga.Command(None, 'Visit homepage', group=toga.Group.HELP)
36         )
37         self._create_app_commands()
38 
39         # Call user code to populate the main window
40         self.interface.startup()
41         self._menu_items = {}
42         self.create_menus()
43         self.interface.main_window._impl.native.Icon = \
44             self.interface.icon.bind(self.interface.factory).native
45 
46     def create_menus(self):
47         toga.Group.FILE.order = 0
48         # Only create the menu if the menu item index has been created.
49         if hasattr(self, '_menu_items'):
50             menubar = WinForms.MenuStrip()
51             submenu = None
52             for cmd in self.interface.commands:
53                 if cmd == toga.GROUP_BREAK:
54                     menubar.Items.Add(submenu)
55                     submenu = None
56                 elif cmd == toga.SECTION_BREAK:
57                     submenu.DropDownItems.Add('-')
58                 else:
59                     if submenu is None:
60                         submenu = WinForms.ToolStripMenuItem(cmd.group.label)
61                     item = WinForms.ToolStripMenuItem(cmd.label)
62                     if cmd.action:
63                         item.Click += add_handler(cmd)
64                     else:
65                         item.Enabled = False
66                     cmd._widgets.append(item)
67                     self._menu_items[item] = cmd
68                     submenu.DropDownItems.Add(item)
69             if submenu:
70                 menubar.Items.Add(submenu)
71             self.interface.main_window._impl.native.Controls.Add(menubar)
72             self.interface.main_window._impl.native.MainMenuStrip = menubar
73         self.interface.main_window.content.refresh()
74 
75     def _create_app_commands(self):
76         # No extra menus
77         pass
78 
79     def open_document(self, fileURL):
80         '''Add a new document to this app.'''
81         print("STUB: If you want to handle opening documents, implement App.open_document(fileURL)")
82 
83     def run_app(self):
84         self.create()
85         self.native.Run(self.interface.main_window._impl.native)
86 
87     def main_loop(self):
88         thread = Threading.Thread(Threading.ThreadStart(self.run_app))
89         thread.SetApartmentState(Threading.ApartmentState.STA)
90         thread.Start()
91         thread.Join()
92 
93     def exit(self):
94         self.native.Exit()
95 
96     def set_on_exit(self, value):
97         pass
98 
99     def current_window(self):
100         self.interface.factory.not_implemented('App.current_window()')
101 
102     def enter_full_screen(self, windows):
103         self.interface.factory.not_implemented('App.enter_full_screen()')
104 
105     def exit_full_screen(self, windows):
106         self.interface.factory.not_implemented('App.exit_full_screen()')
107 
108     def set_cursor(self, value):
109         self.interface.factory.not_implemented('App.set_cursor()')
110 
111     def show_cursor(self):
112         self.interface.factory.not_implemented('App.show_cursor()')
113 
114     def hide_cursor(self):
115         self.interface.factory.not_implemented('App.hide_cursor()')
116 
117 
118 class DocumentApp(App):
119     def _create_app_commands(self):
120         self.interface.commands.add(
121             toga.Command(
122                 lambda w: self.open_file,
123                 label='Open...',
124                 shortcut='o',
125                 group=toga.Group.FILE,
126                 section=0
127             ),
128         )
129 
130     def open_document(self, fileURL):
131         """Open a new document in this app.
132 
133         Args:
134             fileURL (str): The URL/path to the file to add as a document.
135         """
136         self.interface.factory.not_implemented('DocumentApp.open_document()')
137
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/winforms/toga_winforms/app.py b/src/winforms/toga_winforms/app.py
--- a/src/winforms/toga_winforms/app.py
+++ b/src/winforms/toga_winforms/app.py
@@ -2,7 +2,7 @@
 
 import toga
 
-from .libs import Threading, WinForms, add_handler, user32, win_version
+from .libs import Threading, WinForms, add_handler, user32, win_version, shcore
 from .window import Window
 
 
@@ -21,8 +21,24 @@
     def create(self):
         self.native = WinForms.Application
 
-        if win_version >= 6:
-            user32.SetProcessDPIAware(True)
+        # Check the version of windows and make sure we are setting the DPI mode
+        # with the most up to date API
+        # Windows Versioning Check Sources : https://www.lifewire.com/windows-version-numbers-2625171
+        # and https://docs.microsoft.com/en-us/windows/release-information/
+        if win_version.Major >= 6:  # Checks for Windows Vista or later
+            # Represents Windows 8.1 up to Windows 10 before Build 1703 which should use
+            # SetProcessDpiAwareness(True)
+            if ((win_version.Major == 6 and win_version.Minor == 3) or
+                    (win_version.Major == 10 and win_version.Build < 15063)):
+                shcore.SetProcessDpiAwareness(True)
+            # Represents Windows 10 Build 1703 and beyond which should use
+            # SetProcessDpiAwarenessContext(-2)
+            elif win_version.Major == 10 and win_version.Build >= 15063:
+                user32.SetProcessDpiAwarenessContext(-2)
+            # Any other version of windows should use SetProcessDPIAware(True)
+            else:
+                user32.SetProcessDPIAware(True)
+
         self.native.EnableVisualStyles()
         self.native.SetCompatibleTextRenderingDefault(False)
 
diff --git a/src/winforms/toga_winforms/libs.py b/src/winforms/toga_winforms/libs.py
--- a/src/winforms/toga_winforms/libs.py
+++ b/src/winforms/toga_winforms/libs.py
@@ -31,7 +31,8 @@
 )  # noqa: E402
 
 user32 = ctypes.windll.user32
-win_version = Environment.OSVersion.Version.Major
+shcore = ctypes.windll.shcore
+win_version = Environment.OSVersion.Version
 
 
 def TextAlignment(value):

{"golden_diff": "diff --git a/src/winforms/toga_winforms/app.py b/src/winforms/toga_winforms/app.py\n--- a/src/winforms/toga_winforms/app.py\n+++ b/src/winforms/toga_winforms/app.py\n@@ -2,7 +2,7 @@\n \n import toga\n \n-from .libs import Threading, WinForms, add_handler, user32, win_version\n+from .libs import Threading, WinForms, add_handler, user32, win_version, shcore\n from .window import Window\n \n \n@@ -21,8 +21,24 @@\n     def create(self):\n         self.native = WinForms.Application\n \n-        if win_version >= 6:\n-            user32.SetProcessDPIAware(True)\n+        # Check the version of windows and make sure we are setting the DPI mode\n+        # with the most up to date API\n+        # Windows Versioning Check Sources : https://www.lifewire.com/windows-version-numbers-2625171\n+        # and https://docs.microsoft.com/en-us/windows/release-information/\n+        if win_version.Major >= 6:  # Checks for Windows Vista or later\n+            # Represents Windows 8.1 up to Windows 10 before Build 1703 which should use\n+            # SetProcessDpiAwareness(True)\n+            if ((win_version.Major == 6 and win_version.Minor == 3) or\n+                    (win_version.Major == 10 and win_version.Build < 15063)):\n+                shcore.SetProcessDpiAwareness(True)\n+            # Represents Windows 10 Build 1703 and beyond which should use\n+            # SetProcessDpiAwarenessContext(-2)\n+            elif win_version.Major == 10 and win_version.Build >= 15063:\n+                user32.SetProcessDpiAwarenessContext(-2)\n+            # Any other version of windows should use SetProcessDPIAware(True)\n+            else:\n+                user32.SetProcessDPIAware(True)\n+\n         self.native.EnableVisualStyles()\n         self.native.SetCompatibleTextRenderingDefault(False)\n \ndiff --git a/src/winforms/toga_winforms/libs.py b/src/winforms/toga_winforms/libs.py\n--- a/src/winforms/toga_winforms/libs.py\n+++ b/src/winforms/toga_winforms/libs.py\n@@ -31,7 +31,8 @@\n )  # noqa: E402\n \n user32 = ctypes.windll.user32\n-win_version = Environment.OSVersion.Version.Major\n+shcore = ctypes.windll.shcore\n+win_version = Environment.OSVersion.Version\n \n \n def TextAlignment(value):\n", "issue": "Fix octal literals in constants.py on the Windows platform, so it will work in Python 3.\nThe literals in the file won't work in Python 3, and by fixing them you can get as far as an empty window on Windows.\n\nP.S. This is my first time doing a pull request on GitHub, hope this is ok.\n\n", "before_files": [{"content": "import ctypes\n\nimport clr\n\nclr.AddReference(\"System.Windows.Forms\")\n\nimport System.Windows.Forms as WinForms  # noqa: E402\nfrom System import Decimal as ClrDecimal  # noqa: E402, F401\nfrom System import Single  # noqa: E402, F401\nfrom System import Convert  # noqa: E402, F401\nfrom System import DateTime as WinDateTime  # noqa: E402, F401\nfrom System import Threading  # noqa: E402, F401\nfrom System import Uri  # noqa: E402, F401\nfrom System import Environment  # noqa: E402, F401\n\nfrom System.Drawing import Icon as WinIcon  # noqa: E402, F401\nfrom System.Drawing import Image as WinImage  # noqa: E402, F401\nfrom System.Drawing import Font as WinFont  # noqa: E402, F401\nfrom System.Drawing import ContentAlignment, Size, Point  # noqa: E402, F401\nfrom System.Drawing import FontFamily, FontStyle, SystemFonts  # noqa: E402, F401\nfrom System.Drawing import Text, Color, Bitmap  # noqa: E402, F401\nfrom toga.constants import LEFT, RIGHT, CENTER, JUSTIFY  # noqa: E402\nfrom toga.fonts import (\n    MESSAGE,\n    SYSTEM,\n    SERIF,\n    SANS_SERIF,\n    CURSIVE,\n    FANTASY,\n    MONOSPACE,\n)  # noqa: E402\n\nuser32 = ctypes.windll.user32\nwin_version = Environment.OSVersion.Version.Major\n\n\ndef TextAlignment(value):\n    return {\n        LEFT: ContentAlignment.MiddleLeft,\n        RIGHT: ContentAlignment.MiddleRight,\n        CENTER: ContentAlignment.MiddleCenter,\n        JUSTIFY: ContentAlignment.MiddleLeft,\n    }[value]\n\n\n# Justify simply sets Left alignment. Is this the best option?\ndef HorizontalTextAlignment(value):\n    return {\n        LEFT: WinForms.HorizontalAlignment.Left,\n        RIGHT: WinForms.HorizontalAlignment.Right,\n        CENTER: WinForms.HorizontalAlignment.Center,\n        JUSTIFY: WinForms.HorizontalAlignment.Left,\n    }[value]\n\n\ndef add_handler(cmd):\n    action = cmd.action\n\n    def handler(sender, event):\n        return action(None)\n\n    return handler\n\n\ndef win_font_family(value):\n    win_families = {\n        SYSTEM: SystemFonts.DefaultFont.FontFamily,\n        MESSAGE: SystemFonts.MenuFont.FontFamily,\n        SERIF: FontFamily.GenericSerif,\n        SANS_SERIF: FontFamily.GenericSansSerif,\n        CURSIVE: FontFamily(\"Comic Sans MS\"),\n        FANTASY: FontFamily(\"Impact\"),\n        MONOSPACE: FontFamily.GenericMonospace,\n    }\n    for key in win_families:\n        if value in key:\n            return win_families[key]\n    if value in Text.InstalledFontCollection().Families:\n        return FontFamily(value)\n    else:\n        print(\n            \"Unable to load font-family '{}', loading {} instead\".format(\n                value, SystemFonts.DefaultFont.FontFamily)\n        )\n        return SystemFonts.DefaultFont.FontFamily\n", "path": "src/winforms/toga_winforms/libs.py"}, {"content": "import sys\n\nimport toga\n\nfrom .libs import Threading, WinForms, add_handler, user32, win_version\nfrom .window import Window\n\n\nclass MainWindow(Window):\n    def on_close(self):\n        pass\n\n\nclass App:\n    _MAIN_WINDOW_CLASS = MainWindow\n\n    def __init__(self, interface):\n        self.interface = interface\n        self.interface._impl = self\n\n    def create(self):\n        self.native = WinForms.Application\n\n        if win_version >= 6:\n            user32.SetProcessDPIAware(True)\n        self.native.EnableVisualStyles()\n        self.native.SetCompatibleTextRenderingDefault(False)\n\n        self.interface.commands.add(\n            toga.Command(None, 'About ' + self.interface.name, group=toga.Group.HELP),\n            toga.Command(None, 'Preferences', group=toga.Group.FILE),\n            # Quit should always be the last item, in a section on it's own\n            toga.Command(lambda s: self.exit(), 'Exit ' + self.interface.name, shortcut='q', group=toga.Group.FILE,\n                         section=sys.maxsize),\n            toga.Command(None, 'Visit homepage', group=toga.Group.HELP)\n        )\n        self._create_app_commands()\n\n        # Call user code to populate the main window\n        self.interface.startup()\n        self._menu_items = {}\n        self.create_menus()\n        self.interface.main_window._impl.native.Icon = \\\n            self.interface.icon.bind(self.interface.factory).native\n\n    def create_menus(self):\n        toga.Group.FILE.order = 0\n        # Only create the menu if the menu item index has been created.\n        if hasattr(self, '_menu_items'):\n            menubar = WinForms.MenuStrip()\n            submenu = None\n            for cmd in self.interface.commands:\n                if cmd == toga.GROUP_BREAK:\n                    menubar.Items.Add(submenu)\n                    submenu = None\n                elif cmd == toga.SECTION_BREAK:\n                    submenu.DropDownItems.Add('-')\n                else:\n                    if submenu is None:\n                        submenu = WinForms.ToolStripMenuItem(cmd.group.label)\n                    item = WinForms.ToolStripMenuItem(cmd.label)\n                    if cmd.action:\n                        item.Click += add_handler(cmd)\n                    else:\n                        item.Enabled = False\n                    cmd._widgets.append(item)\n                    self._menu_items[item] = cmd\n                    submenu.DropDownItems.Add(item)\n            if submenu:\n                menubar.Items.Add(submenu)\n            self.interface.main_window._impl.native.Controls.Add(menubar)\n            self.interface.main_window._impl.native.MainMenuStrip = menubar\n        self.interface.main_window.content.refresh()\n\n    def _create_app_commands(self):\n        # No extra menus\n        pass\n\n    def open_document(self, fileURL):\n        '''Add a new document to this app.'''\n        print(\"STUB: If you want to handle opening documents, implement App.open_document(fileURL)\")\n\n    def run_app(self):\n        self.create()\n        self.native.Run(self.interface.main_window._impl.native)\n\n    def main_loop(self):\n        thread = Threading.Thread(Threading.ThreadStart(self.run_app))\n        thread.SetApartmentState(Threading.ApartmentState.STA)\n        thread.Start()\n        thread.Join()\n\n    def exit(self):\n        self.native.Exit()\n\n    def set_on_exit(self, value):\n        pass\n\n    def current_window(self):\n        self.interface.factory.not_implemented('App.current_window()')\n\n    def enter_full_screen(self, windows):\n        self.interface.factory.not_implemented('App.enter_full_screen()')\n\n    def exit_full_screen(self, windows):\n        self.interface.factory.not_implemented('App.exit_full_screen()')\n\n    def set_cursor(self, value):\n        self.interface.factory.not_implemented('App.set_cursor()')\n\n    def show_cursor(self):\n        self.interface.factory.not_implemented('App.show_cursor()')\n\n    def hide_cursor(self):\n        self.interface.factory.not_implemented('App.hide_cursor()')\n\n\nclass DocumentApp(App):\n    def _create_app_commands(self):\n        self.interface.commands.add(\n            toga.Command(\n                lambda w: self.open_file,\n                label='Open...',\n                shortcut='o',\n                group=toga.Group.FILE,\n                section=0\n            ),\n        )\n\n    def open_document(self, fileURL):\n        \"\"\"Open a new document in this app.\n\n        Args:\n            fileURL (str): The URL/path to the file to add as a document.\n        \"\"\"\n        self.interface.factory.not_implemented('DocumentApp.open_document()')\n", "path": "src/winforms/toga_winforms/app.py"}], "after_files": [{"content": "import ctypes\n\nimport clr\n\nclr.AddReference(\"System.Windows.Forms\")\n\nimport System.Windows.Forms as WinForms  # noqa: E402\nfrom System import Decimal as ClrDecimal  # noqa: E402, F401\nfrom System import Single  # noqa: E402, F401\nfrom System import Convert  # noqa: E402, F401\nfrom System import DateTime as WinDateTime  # noqa: E402, F401\nfrom System import Threading  # noqa: E402, F401\nfrom System import Uri  # noqa: E402, F401\nfrom System import Environment  # noqa: E402, F401\n\nfrom System.Drawing import Icon as WinIcon  # noqa: E402, F401\nfrom System.Drawing import Image as WinImage  # noqa: E402, F401\nfrom System.Drawing import Font as WinFont  # noqa: E402, F401\nfrom System.Drawing import ContentAlignment, Size, Point  # noqa: E402, F401\nfrom System.Drawing import FontFamily, FontStyle, SystemFonts  # noqa: E402, F401\nfrom System.Drawing import Text, Color, Bitmap  # noqa: E402, F401\nfrom toga.constants import LEFT, RIGHT, CENTER, JUSTIFY  # noqa: E402\nfrom toga.fonts import (\n    MESSAGE,\n    SYSTEM,\n    SERIF,\n    SANS_SERIF,\n    CURSIVE,\n    FANTASY,\n    MONOSPACE,\n)  # noqa: E402\n\nuser32 = ctypes.windll.user32\nshcore = ctypes.windll.shcore\nwin_version = Environment.OSVersion.Version\n\n\ndef TextAlignment(value):\n    return {\n        LEFT: ContentAlignment.MiddleLeft,\n        RIGHT: ContentAlignment.MiddleRight,\n        CENTER: ContentAlignment.MiddleCenter,\n        JUSTIFY: ContentAlignment.MiddleLeft,\n    }[value]\n\n\n# Justify simply sets Left alignment. Is this the best option?\ndef HorizontalTextAlignment(value):\n    return {\n        LEFT: WinForms.HorizontalAlignment.Left,\n        RIGHT: WinForms.HorizontalAlignment.Right,\n        CENTER: WinForms.HorizontalAlignment.Center,\n        JUSTIFY: WinForms.HorizontalAlignment.Left,\n    }[value]\n\n\ndef add_handler(cmd):\n    action = cmd.action\n\n    def handler(sender, event):\n        return action(None)\n\n    return handler\n\n\ndef win_font_family(value):\n    win_families = {\n        SYSTEM: SystemFonts.DefaultFont.FontFamily,\n        MESSAGE: SystemFonts.MenuFont.FontFamily,\n        SERIF: FontFamily.GenericSerif,\n        SANS_SERIF: FontFamily.GenericSansSerif,\n        CURSIVE: FontFamily(\"Comic Sans MS\"),\n        FANTASY: FontFamily(\"Impact\"),\n        MONOSPACE: FontFamily.GenericMonospace,\n    }\n    for key in win_families:\n        if value in key:\n            return win_families[key]\n    if value in Text.InstalledFontCollection().Families:\n        return FontFamily(value)\n    else:\n        print(\n            \"Unable to load font-family '{}', loading {} instead\".format(\n                value, SystemFonts.DefaultFont.FontFamily)\n        )\n        return SystemFonts.DefaultFont.FontFamily\n", "path": "src/winforms/toga_winforms/libs.py"}, {"content": "import sys\n\nimport toga\n\nfrom .libs import Threading, WinForms, add_handler, user32, win_version, shcore\nfrom .window import Window\n\n\nclass MainWindow(Window):\n    def on_close(self):\n        pass\n\n\nclass App:\n    _MAIN_WINDOW_CLASS = MainWindow\n\n    def __init__(self, interface):\n        self.interface = interface\n        self.interface._impl = self\n\n    def create(self):\n        self.native = WinForms.Application\n\n        # Check the version of windows and make sure we are setting the DPI mode\n        # with the most up to date API\n        # Windows Versioning Check Sources : https://www.lifewire.com/windows-version-numbers-2625171\n        # and https://docs.microsoft.com/en-us/windows/release-information/\n        if win_version.Major >= 6:  # Checks for Windows Vista or later\n            # Represents Windows 8.1 up to Windows 10 before Build 1703 which should use\n            # SetProcessDpiAwareness(True)\n            if ((win_version.Major == 6 and win_version.Minor == 3) or\n                    (win_version.Major == 10 and win_version.Build < 15063)):\n                shcore.SetProcessDpiAwareness(True)\n            # Represents Windows 10 Build 1703 and beyond which should use\n            # SetProcessDpiAwarenessContext(-2)\n            elif win_version.Major == 10 and win_version.Build >= 15063:\n                user32.SetProcessDpiAwarenessContext(-2)\n            # Any other version of windows should use SetProcessDPIAware(True)\n            else:\n                user32.SetProcessDPIAware(True)\n\n        self.native.EnableVisualStyles()\n        self.native.SetCompatibleTextRenderingDefault(False)\n\n        self.interface.commands.add(\n            toga.Command(None, 'About ' + self.interface.name, group=toga.Group.HELP),\n            toga.Command(None, 'Preferences', group=toga.Group.FILE),\n            # Quit should always be the last item, in a section on it's own\n            toga.Command(lambda s: self.exit(), 'Exit ' + self.interface.name, shortcut='q', group=toga.Group.FILE,\n                         section=sys.maxsize),\n            toga.Command(None, 'Visit homepage', group=toga.Group.HELP)\n        )\n        self._create_app_commands()\n\n        # Call user code to populate the main window\n        self.interface.startup()\n        self._menu_items = {}\n        self.create_menus()\n        self.interface.main_window._impl.native.Icon = \\\n            self.interface.icon.bind(self.interface.factory).native\n\n    def create_menus(self):\n        toga.Group.FILE.order = 0\n        # Only create the menu if the menu item index has been created.\n        if hasattr(self, '_menu_items'):\n            menubar = WinForms.MenuStrip()\n            submenu = None\n            for cmd in self.interface.commands:\n                if cmd == toga.GROUP_BREAK:\n                    menubar.Items.Add(submenu)\n                    submenu = None\n                elif cmd == toga.SECTION_BREAK:\n                    submenu.DropDownItems.Add('-')\n                else:\n                    if submenu is None:\n                        submenu = WinForms.ToolStripMenuItem(cmd.group.label)\n                    item = WinForms.ToolStripMenuItem(cmd.label)\n                    if cmd.action:\n                        item.Click += add_handler(cmd)\n                    else:\n                        item.Enabled = False\n                    cmd._widgets.append(item)\n                    self._menu_items[item] = cmd\n                    submenu.DropDownItems.Add(item)\n            if submenu:\n                menubar.Items.Add(submenu)\n            self.interface.main_window._impl.native.Controls.Add(menubar)\n            self.interface.main_window._impl.native.MainMenuStrip = menubar\n        self.interface.main_window.content.refresh()\n\n    def _create_app_commands(self):\n        # No extra menus\n        pass\n\n    def open_document(self, fileURL):\n        '''Add a new document to this app.'''\n        print(\"STUB: If you want to handle opening documents, implement App.open_document(fileURL)\")\n\n    def run_app(self):\n        self.create()\n        self.native.Run(self.interface.main_window._impl.native)\n\n    def main_loop(self):\n        thread = Threading.Thread(Threading.ThreadStart(self.run_app))\n        thread.SetApartmentState(Threading.ApartmentState.STA)\n        thread.Start()\n        thread.Join()\n\n    def exit(self):\n        self.native.Exit()\n\n    def set_on_exit(self, value):\n        pass\n\n    def current_window(self):\n        self.interface.factory.not_implemented('App.current_window()')\n\n    def enter_full_screen(self, windows):\n        self.interface.factory.not_implemented('App.enter_full_screen()')\n\n    def exit_full_screen(self, windows):\n        self.interface.factory.not_implemented('App.exit_full_screen()')\n\n    def set_cursor(self, value):\n        self.interface.factory.not_implemented('App.set_cursor()')\n\n    def show_cursor(self):\n        self.interface.factory.not_implemented('App.show_cursor()')\n\n    def hide_cursor(self):\n        self.interface.factory.not_implemented('App.hide_cursor()')\n\n\nclass DocumentApp(App):\n    def _create_app_commands(self):\n        self.interface.commands.add(\n            toga.Command(\n                lambda w: self.open_file,\n                label='Open...',\n                shortcut='o',\n                group=toga.Group.FILE,\n                section=0\n            ),\n        )\n\n    def open_document(self, fileURL):\n        \"\"\"Open a new document in this app.\n\n        Args:\n            fileURL (str): The URL/path to the file to add as a document.\n        \"\"\"\n        self.interface.factory.not_implemented('DocumentApp.open_document()')\n", "path": "src/winforms/toga_winforms/app.py"}]}