def test_gauge(self):
        cases  = [
            # {'uri': None},
            {},
            {'uri': images.red_uri},
            {'uri': images.red_uri, 'size': (64, 64)},
            {'uri': images.red_uri, 'size': (16, 16)}
        ]
        # expected_state = {
        #     'uri': images.kirbs_uri
        # }
        app = wx.App()
        for props in cases:
            with self.subTest(props):
                component = wsx(
                    [c.Frame, {'show': True, 'size': (100,100)},
                     [c.Block, {},
                      [c.StaticBitmap, {**props, 'proportion': 0}]]])
            frame = render(component, None)
            bitmap = frame.Children[0].Children[0]

            if 'uri' in props:
                self.assertIsNotNone(bitmap.GetBitmap().RefData)
            else:
                # when no URI is defined, there should similarly
                # be no bitmap data present
                self.assertEqual(bitmap.GetBitmap().RefData, None)
Exemple #2
0
    def test_textctrl(self):
        mocked_change_handler = MagicMock()
        mocked_enter_handler = MagicMock()
        expected_state = {
            'value': '123',
            'name': 'My Cool Control',
            'style': wx.TE_PROCESS_ENTER,
            'on_change': mocked_change_handler,
            'size': (100, 20)
        }

        app = wx.App()
        component = wsx([
            c.Frame,
            {
                'show': True,
                'size': (999, 999)
            },
            # note: the inner block is required so that the widget's
            # size prop can be honored (without it, it'd get resized to
            # the parent frame's size after `Layout` gets called.
            [c.Block, {}, [c.TextCtrl, {
                **expected_state, 'proportion': 0
            }]]
        ])
        frame = render(component, None)
        instance = frame.Children[0].Children[0]

        try:
            instance.Bind(wx.EVT_TEXT_ENTER, mocked_enter_handler)
        except Exception:
            self.fail("""
                Unexpected failure. 
                Either the test definition has regressed, and it is now missing 
                `wx.TE_PROCESS_ENTER` as a style argument, or the widget dispatcher 
                has regressed and is no longer applying `style` props correctly"""
                      )

        # our props are honored
        self.assertEqual(instance.Name, expected_state['name'])
        self.assertEqual(instance.Value, expected_state['value'])
        self.assertEqual(instance.Size, expected_state['size'])

        # our enter has picked up and installed
        self.assertFalse(mocked_enter_handler.called)
        event = wx.KeyEvent(wx.wxEVT_TEXT_ENTER)
        instance.ProcessEvent(event)
        self.assertTrue(mocked_enter_handler.called)

        # our change handler prop is honored
        self.assertFalse(mocked_change_handler.called)
        event = wx.CommandEvent(wx.wxEVT_TEXT, wx.Window.NewControlId())
        event.SetString('a')
        instance.ProcessEvent(event)
        self.assertTrue(mocked_change_handler.called)
Exemple #3
0
def _build_app(build_spec, app) -> Tuple[Any, wx.Frame]:
    """
    Note: this method is broken out with app as
    an argument to facilitate testing.
    """
    # use actual program name instead of script file name in macOS menu
    app.SetAppDisplayName(build_spec['program_name'])

    i18n.load(build_spec['language_dir'], build_spec['language'],
              build_spec['encoding'])
    imagesPaths = image_repository.loadImages(build_spec['image_dir'])
    gapp2 = render(create_element(RGooey, merge(build_spec, imagesPaths)),
                   None)
    # wx.lib.inspection.InspectionTool().Show()
    # gapp.Show()
    gapp2.Show()
    return (app, gapp2)
Exemple #4
0
 def test_update_component(self):
     app = wx.App()
     frame = render(create_element(Parent, {}), None)
     # app.MainLoop()
     # sanity checking that state/props align for initial render
     initial_parent_state = frame._instance.state['label']
     initial_child_props = frame.Children[0]._instance.props['label']
     self.assertEqual(initial_parent_state, initial_child_props)
     # bug #0002 was that child component weren't receiving new props
     # up update. So now we fire one of the rewx handlers:
     frame._instance.update_label('foobar')
     next_parent_state = frame._instance.state['label']
     next_child_props = frame.Children[0]._instance.props['label']
     # and expect that both are still in sync
     self.assertEqual(next_parent_state, 'foobar')
     self.assertEqual(next_parent_state, next_child_props)
     app.Destroy()
Exemple #5
0
    def __init__(self, *args, **kwargs):
        caption = kwargs.pop('caption', '')
        html = kwargs.pop('html', '')
        super(HtmlDialog, self).__init__(None, *args, **kwargs)

        wx.InitAllImageHandlers()

        self.SetTitle(caption)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(render(_html_window(html), self), 1, wx.EXPAND)

        # in addition to creating the sizer, this actually attached
        # a few common handlers which makes it feel more dialog-y. Thus
        # it being done here rather than in rewx
        btnSizer = self.CreateStdDialogButtonSizer(wx.OK)
        sizer.Add(btnSizer, 0, wx.ALL | wx.EXPAND, 9)
        self.SetSizer(sizer)
        self.Layout()
Exemple #6
0
    def test_gauge(self):
        expected_state = {
            'value': 123,
            'range': 3000,
            'name': 'Counter',
            'size': (500, 30),
        }
        app = wx.App()
        component = wsx(
            [c.Frame, {'show': True, 'size': (999,999)},
             [c.Block, {},
              [c.Gauge, {**expected_state, 'proportion': 0}]]])
        frame = render(component, None)
        gauge = frame.Children[0].Children[0]

        self.assertEqual(gauge.Size, expected_state['size'])
        self.assertEqual(gauge.Range, expected_state['range'])
        self.assertEqual(gauge.Name, expected_state['name'])
        self.assertEqual(gauge.Value, expected_state['value'])
    def testUriUpdatesChangeImage(self):
        """
        Testing that swapping URI leads to the
        new images being loaded.
        """
        app = wx.App()

        with self.subTest("initializing with no URI"):
            component = wsx(
                [c.Frame, {'show': True, 'size': (100, 100)},
                 [c.StaticBitmap, {}]])
            frame = render(component, None)
            bitmap = frame.Children[0]
            # no images loaded
            self.assertEqual(bitmap.GetBitmap().RefData, None)

        with self.subTest('Adding a URI loads the image'):
            patch(bitmap, create_element(c.StaticBitmap, {'uri': images.red_uri}))
            original_rgb = self.get_rgb(bitmap)
            self.assertEqual(original_rgb, images.red_rgb)

        with self.subTest("updating props with new URI"):
            patch(bitmap, create_element(c.StaticBitmap, {'uri': images.pink_uri}))
            updated_rgb = self.get_rgb(bitmap)
            # colors should now be different because the image was updated
            self.assertNotEqual(original_rgb, updated_rgb)
            # and we should have our pink color:
            self.assertEqual(updated_rgb, images.pink_rgb)

        with self.subTest("updating props with same URI keeps existing image"):
            # attaching a mock so we can assert the same URI doesn't cause
            # a reloading of the image
            mock = MagicMock()
            bitmap.SetBitmap = mock

            patch(bitmap, create_element(c.StaticBitmap, {'uri': images.pink_uri}))
            mock.assert_not_called()
Exemple #8
0
                  'click_handler': self.update_value,
                  'label': 'Click me!'
              }]]]
        )


class ChildProps(TypedDict):
    """Optionally typing the props of your component"""
    label: str
    click_handler: Callable[[wx.CommandEvent], None]


class Child(Component):
    """
    An arbitrary component. Note that this is still pure -- it holds
    no state of its own and all behaviors, like the click handler, are
    passed in via props.
    """
    def __init__(self, props: ChildProps):
        super().__init__(props)

    def render(self):
        return wsx([c.Button, {
            'on_click': self.props['click_handler'],
            'label': self.props['label']
        }])

if __name__ == '__main__':
    app = wx.App()
    frame = render(create_element(Parent, {}), None)
    app.MainLoop()
Exemple #9
0
            results = list(executor.map(fake_download, args))
            wx.CallAfter(self.finish_download)


    def render(self):
        return wsx(
            [Block, {},
             [Block, {'orient': wx.HORIZONTAL},
              [StaticText, {'label': 'Download #1'}],
              [Gauge, {"value": self.state["download_1"],
                       "range": 100}]],
             [Block, {'orient': wx.HORIZONTAL},
              [StaticText, {'label': 'Download #2'}],
              [Gauge, {"value": self.state["download_2"],
                       "range": 100}]],
             [Button, {"label": "Download" if self.state['status'] == 'READY' else 'In Progress',
                       'enabled': self.state['status'] == 'READY',
                       "on_click": self.start_download,
                       "flag": wx.CENTER | wx.ALL}],
            ]
        )


if __name__ == "__main__":
    app = wx.App()

    frame = wx.Frame(None, title="Gauge With Update")
    body = render(create_element(FooGauge, {}), frame)

    frame.Show()
    app.MainLoop()
Exemple #10
0
        self.state = {'value': ''}
        self.input_ref = Ref()

    def component_did_mount(self):
        wx_ctrl = self.input_ref.instance
        wx_ctrl.Bind(wx.EVT_TEXT_ENTER, self.on_enter)

    def on_input(self, event):
        self.set_state({'value': event.EventObject.Value})

    def on_enter(self, event):
        wx.MessageDialog(None, "Enter pressed!").ShowModal()

    def render(self):
        return wsx(
            [c.Frame, {'title': 'My Cool Application', 'show': True},
             [c.Block, {'orient': wx.HORIZONTAL},
              [c.TextCtrl, {
                  'style': wx.TE_PROCESS_ENTER,
                  'value': self.state['value'],
                  'on_change': self.on_input,
                  'ref': self.input_ref,
              }]]]
        )



if __name__ == '__main__':
    app = wx.App()
    frame = render(create_element(MyComponent, {}), None)
    app.MainLoop()