Skip to content

Instantly share code, notes, and snippets.

@freakboy3742
Created May 23, 2017 23:26
Show Gist options
  • Save freakboy3742/d90e4504c00eaab2c61fbca1708c1b47 to your computer and use it in GitHub Desktop.
Save freakboy3742/d90e4504c00eaab2c61fbca1708c1b47 to your computer and use it in GitHub Desktop.
Winforms menus
from toga.interface.window import Window as WindowInterface
from .libs import *
from .container import Container
from . import dialogs
from .command import SEPARATOR, SPACER, EXPANDING_SPACER
class Window(WindowInterface):
# _IMPL_CLASS = WinForms.Form
_CONTAINER_CLASS = Container
_DIALOG_MODULE = dialogs
def __init__(self, title=None, position=(100, 100), size=(640, 480), toolbar=None, resizeable=True, closeable=True, minimizable=True):
super().__init__(title=title, position=position, size=size, toolbar=toolbar, resizeable=resizeable, closeable=closeable, minimizable=minimizable)
self._create()
def create(self):
self._impl = WinForms.Form(self)
self._impl.ClientSize = Size(self._size[0], self._size[1])
self._impl.Resize += self._on_resize
mainMenu = WinForms.MainMenu()
menuHelpAbout = WinForms.MenuItem()
menuHelpAbout.Text = "&About"
menuHelpAbout.Index = 0
menuHelpAbout.Click += self.OnClickHelpAbout
aboutMenu = WinForms.MenuItem()
aboutMenu.Text = "&Help"
aboutMenu.Index = 3
aboutMenu.MenuItems.Add(menuHelpAbout)
items = (aboutMenu,)
mainMenu.MenuItems.AddRange(items)
self._impl.Menu = mainMenu
def _set_toolbar(self, items):
self._toolbar_impl = WinForms.ToolStrip()
for toolbar_item in items:
if toolbar_item == SEPARATOR:
item_impl = WinForms.ToolStripSeparator()
elif toolbar_item == SPACER:
item_impl = WinForms.ToolStripSeparator()
elif toolbar_item == EXPANDING_SPACER:
item_impl = WinForms.ToolStripSeparator() # todo: check how this behaves on other platforms
else:
item_impl = WinForms.ToolStripButton()
self._toolbar_impl.Items.Add(item_impl)
def _set_content(self, widget):
if (self.toolbar):
self._impl.Controls.Add(self._toolbar_impl)
self._impl.Controls.Add(widget._container._impl)
def _set_title(self, title):
self._impl.Text = title
def show(self):
# The first render of the content will establish the
# minimum possible content size; use that to enforce
# a minimum window size.
TITLEBAR_HEIGHT = 36
self._impl.MinimumSize = Size(
int(self.content.layout.width),
int(self.content.layout.height) + TITLEBAR_HEIGHT
)
# Set the size of the container to be the same as the window
self._container._impl.Size = self._impl.ClientSize
# Do the first layout render.
self._container._update_layout(
width=self._impl.ClientSize.Width,
height=self._impl.ClientSize.Height,
)
def close(self):
self._impl.Close()
def _on_resize(self, sender, args):
if self.content:
# Set the size of the container to be the same as the window
self._container._impl.Size = self._impl.ClientSize
# Re-layout the content
self.content._update_layout(
width=sender.ClientSize.Width,
height=sender.ClientSize.Height,
)
def OnClickHelpAbout(self, sender, args):
print("Hello app")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment