Exemplo n.º 1
0
    def start(self, args: dict):
        from alleycat.ui.blender import UI

        self.context = UI().create_context()

        window = Frame(self.context, BorderLayout())
        window.bounds = Bounds(160, 70, 280, 200)

        panel = Panel(self.context, HBoxLayout())
        panel.set_color(StyleKeys.Background, RGBA(0.3, 0.3, 0.3, 0.8))

        window.add(panel, padding=Insets(10, 10, 10, 10))

        icon = Canvas(self.context, self.context.toolkit.images["cat.png"])
        icon.minimum_size_override = Some(Dimension(64, 64))

        panel.add(icon)

        label = Label(self.context, text_size=18)
        label.set_color(StyleKeys.Text, RGBA(1, 1, 1, 1))

        panel.add(label)

        button1 = LabelButton(self.context, text_size=16, text="Button 1")
        button2 = LabelButton(self.context, text_size=16, text="Button 2")

        buttons = Panel(self.context,
                        HBoxLayout(spacing=10, direction=BoxDirection.Reverse))

        buttons.add(button2)
        buttons.add(button1)

        window.add(buttons, Border.Bottom, Insets(0, 10, 10, 10))

        def handle_button(button: str):
            if len(button) > 0:
                label.text = f"{button} is pressed"
                panel.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1))
            else:
                label.text = ""
                panel.set_color(StyleKeys.Background, RGBA(0.1, 0.1, 0.1, 0.8))

        button1_active = button1.observe("active").pipe(
            ops.map(lambda v: "Button 1" if v else ""))
        button2_active = button2.observe("active").pipe(
            ops.map(lambda v: "Button 2" if v else ""))

        button_active = rx.combine_latest(button1_active, button2_active).pipe(
            ops.map(lambda v: v[0] + v[1]))

        button_active.subscribe(handle_button,
                                on_error=self.context.error_handler)

        window.draggable = True
        window.resizable = True
Exemplo n.º 2
0
    def test_nested_layout(self):
        box = Frame(self.context, VBoxLayout())
        box.bounds = Bounds(0, 0, 100, 100)

        child1 = Panel(self.context)
        child1.preferred_size_override = Some(Dimension(50, 20))
        child1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1))

        box.add(child1)

        child2 = Panel(self.context, BorderLayout())

        top = Panel(self.context)
        top.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1))
        top.preferred_size_override = Some(Dimension(0, 20))

        child2.add(top, Border.Top)

        right = Panel(self.context)
        right.set_color(StyleKeys.Background, RGBA(0, 1, 0, 1))
        right.preferred_size_override = Some(Dimension(15, 0))

        child2.add(right, Border.Right)

        bottom = Panel(self.context)
        bottom.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1))
        bottom.preferred_size_override = Some(Dimension(0, 15))

        child2.add(bottom, Border.Bottom)

        left = Panel(self.context)
        left.set_color(StyleKeys.Background, RGBA(1, 1, 1, 1))
        left.preferred_size_override = Some(Dimension(5, 0))

        child2.add(left, Border.Left)

        center = Panel(self.context)
        center.set_color(StyleKeys.Background, RGBA(0, 0, 0, 1))
        center.preferred_size_override = Some(Dimension(60, 20))

        child2.add(center, Border.Center)

        box.add(child2)

        child3 = Panel(self.context)
        child3.preferred_size_override = Some(Dimension(40, 20))
        child3.minimum_size_override = Some(Dimension(20, 10))
        child3.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1))

        box.add(child3)

        self.assertEqual(True, box.layout_pending)
        self.context.process()
        self.assertEqual(False, box.layout_pending)

        self.assertImage("nested_layout", self.context)

        left.minimum_size_override = Some(Dimension(40, 0))
        top.preferred_size_override = Some(Dimension(0, 10))

        self.assertEqual(True, box.layout_pending)
        self.context.process()
        self.assertEqual(False, box.layout_pending)

        self.assertImage("nested_layout_resize_nested_child", self.context)

        bottom.visible = False

        self.assertEqual(True, box.layout_pending)
        self.context.process()
        self.assertEqual(False, box.layout_pending)

        self.assertImage("nested_layout_hide_nested_child", self.context)