def test_drag_overlapping(self): bottom = Frame(self.context) bottom.draggable = True bottom.bounds = Bounds(10, 10, 50, 50) bottom.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) top = Frame(self.context) top.draggable = True top.bounds = Bounds(20, 20, 50, 50) top.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) self.mouse.move_to(Point(30, 30)) self.mouse.press(MouseButton.LEFT) self.mouse.move_to(Point(50, 50)) self.mouse.release(MouseButton.LEFT) self.context.process() self.assertImage("drag_overlapping_top", self.context) self.mouse.move_to(Point(20, 20)) self.mouse.press(MouseButton.LEFT) self.mouse.move_to(Point(40, 40)) self.mouse.release(MouseButton.LEFT) self.context.process() self.assertImage("drag_overlapping_bottom", self.context)
def test_layout(self): container = Frame(self.context, AbsoluteLayout()) container.bounds = Bounds(30, 30, 200, 200) child1 = Panel(self.context) child1.bounds = Bounds(10, 10, 20, 20) child2 = Panel(self.context) child2.bounds = Bounds(50, 60, 20, 20) container.add(child1) container.add(child2) self.assertEqual(False, container.valid) self.context.process() self.assertEqual(True, container.valid) self.assertEqual(Bounds(10, 10, 20, 20), child1.bounds) self.assertEqual(Bounds(50, 60, 20, 20), child2.bounds) self.assertEqual(Dimension(0, 0), container.minimum_size) self.assertEqual(Dimension(0, 0), container.preferred_size) container.bounds = Bounds(20, 20, 100, 100) child1.minimum_size_override = Some(Dimension(400, 400)) child2.bounds = Bounds(-30, -40, 50, 50) self.assertEqual(False, container.valid) self.context.process() self.assertEqual(True, container.valid) self.assertEqual(Bounds(10, 10, 400, 400), child1.bounds) self.assertEqual(Bounds(-30, -40, 50, 50), child2.bounds) self.assertEqual(Dimension(0, 0), container.minimum_size)
def test_drag(self): window = Frame(self.context) window.draggable = True window.resizable = True window.bounds = Bounds(10, 10, 50, 50) self.mouse.move_to(Point(30, 30)) self.mouse.press(MouseButton.RIGHT) self.mouse.move_to(Point(40, 40)) self.mouse.release(MouseButton.RIGHT) self.context.process() self.assertImage("drag_with_right_button", self.context) window.bounds = Bounds(10, 10, 50, 50) self.mouse.move_to(Point(20, 20)) self.mouse.press(MouseButton.LEFT) self.mouse.move_to(Point(30, 40)) self.mouse.release(MouseButton.LEFT) self.context.process() self.assertImage("drag_with_left_button", self.context) window.bounds = Bounds(10, 10, 50, 50) self.mouse.move_to(Point(10, 10)) self.mouse.press(MouseButton.LEFT) self.mouse.press(MouseButton.MIDDLE) self.mouse.move_to(Point(40, 30)) self.mouse.release(MouseButton.MIDDLE) self.mouse.move_to(Point(20, 50)) self.context.process() self.assertImage("drag_with_2_buttons", self.context) self.mouse.release(MouseButton.LEFT) window.bounds = Bounds(10, 10, 50, 50) window.draggable = False self.mouse.move_to(Point(30, 30)) self.mouse.press(MouseButton.LEFT) self.mouse.move_to(Point(50, 30)) self.mouse.release(MouseButton.LEFT) self.context.process() self.assertImage("drag_non_draggable", self.context)
def test_draw(self): window1 = Frame(self.context) window1.bounds = Bounds(10, 20, 80, 60) window2 = Frame(self.context) window2.bounds = Bounds(50, 40, 50, 50) window2.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) self.context.process() self.assertImage("draw", self.context)
def test_align(self): window = Frame(self.context) window.bounds = Bounds(0, 0, 100, 100) label = Label(self.context) label.text = "AlleyCat" label.text_size = 18 label.bounds = Bounds(0, 0, 100, 100) window.add(label) self.context.process() self.assertImage("align_default", self.context, tolerance=Tolerance) for align in TextAlign: for vertical_align in TextAlign: label.text_align = align label.text_vertical_align = vertical_align test_name = f"align_{align}_{vertical_align}".replace( "TextAlign.", "") self.context.process() self.assertImage(test_name, self.context, tolerance=Tolerance)
def test_layout_with_insets(self): layout = StackLayout() container = Frame(self.context, layout) container.bounds = Bounds(0, 0, 100, 100) child1 = Panel(self.context) child1.bounds = Bounds(10, 10, 20, 20) child1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) child2 = Panel(self.context) child2.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) child2.preferred_size_override = Some(Dimension(80, 60)) container.add(child1) container.add(child2, fill=False) def test(padding: Insets): layout.padding = padding self.assertEqual(True, container.layout_pending) self.context.process() self.assertEqual(False, container.layout_pending) name = f"stack-{padding.top},{padding.right},{padding.bottom},{padding.left}" self.assertImage(name, self.context) for p in [ Insets(10, 10, 10, 10), Insets(15, 0, 15, 0), Insets(0, 10, 20, 0) ]: test(p)
def test_draw_with_padding(self): image = self.context.toolkit.images[FixturePath] window = Frame(self.context) window.bounds = Bounds(0, 0, 100, 100) canvas = Canvas(self.context, image) canvas.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) window.add(canvas) def assert_padding(size: Dimension, padding: Insets): (w, h) = size.tuple (top, right, bottom, left) = padding.tuple canvas.bounds = Bounds(0, 0, w, h) canvas.padding = padding self.context.process() self.assertImage( f"draw_with_padding-{top},{right},{bottom},{left}-{w}x{h}", self.context, tolerance=Tolerance) assert_padding(Dimension(100, 100), Insets(0, 0, 0, 0)) assert_padding(Dimension(100, 100), Insets(10, 5, 3, 15)) assert_padding(Dimension(64, 64), Insets(0, 0, 0, 0)) assert_padding(Dimension(64, 64), Insets(10, 5, 3, 15))
def test_hbox_layout(self): layout = HBoxLayout() container = Frame(self.context, layout) container.bounds = Bounds(5, 5, 90, 90) child1 = Panel(self.context) child1.preferred_size_override = Some(Dimension(20, 50)) child1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) container.add(child1) child2 = Panel(self.context) child2.preferred_size_override = Some(Dimension(15, 60)) child2.minimum_size_override = Some(Dimension(15, 60)) child2.set_color(StyleKeys.Background, RGBA(0, 1, 0, 1)) container.add(child2) child3 = Panel(self.context) child3.preferred_size_override = Some(Dimension(30, 40)) child3.minimum_size_override = Some(Dimension(10, 20)) child3.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) container.add(child3) def test(direction: BoxDirection, spacing: float, padding: Insets, align: BoxAlign): container.bounds = Bounds(5, 5, 90, 90) layout.spacing = spacing layout.padding = padding layout.align = align self.assertEqual(True, container.layout_pending) self.context.process() self.assertEqual(False, container.layout_pending) (top, right, bottom, left) = padding.tuple prefix = f"hbox-{direction.name}-{spacing}-{top},{right},{bottom},{left}-{align.name}-" self.assertImage(prefix + "full-size", self.context) container.bounds = Bounds(5, 5, 45, 45) self.assertEqual(True, container.layout_pending) self.context.process() self.assertEqual(False, container.layout_pending) self.assertImage(prefix + "half-size", self.context) for d in BoxDirection: for s in [0, 10]: for p in [Insets(0, 0, 0, 0), Insets(15, 20, 10, 5)]: for a in BoxAlign: test(d, s, p, a)
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
def test_window_at(self): manager = self.context.window_manager bottom = Frame(self.context) bottom.bounds = Bounds(0, 0, 100, 100) middle = Frame(self.context) middle.bounds = Bounds(100, 100, 100, 100) top = Frame(self.context) top.bounds = Bounds(50, 50, 100, 100) self.assertEqual(Some(bottom), manager.window_at(Point(0, 0))) self.assertEqual(Some(bottom), manager.window_at(Point(100, 0))) self.assertEqual(Some(bottom), manager.window_at(Point(0, 100))) self.assertEqual(Some(middle), manager.window_at(Point(200, 100))) self.assertEqual(Some(middle), manager.window_at(Point(200, 200))) self.assertEqual(Some(middle), manager.window_at(Point(100, 200))) self.assertEqual(Some(top), manager.window_at(Point(100, 100))) self.assertEqual(Some(top), manager.window_at(Point(150, 150))) self.assertEqual(Some(top), manager.window_at(Point(150, 50))) self.assertEqual(Some(top), manager.window_at(Point(50, 150)))
def test_hbox_hide_child(self): container = Frame(self.context, HBoxLayout()) container.bounds = Bounds(0, 0, 100, 100) child1 = Panel(self.context) child1.preferred_size_override = Some(Dimension(20, 50)) child1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) container.add(child1) child2 = Panel(self.context) child2.preferred_size_override = Some(Dimension(15, 60)) child2.minimum_size_override = Some(Dimension(15, 60)) child2.set_color(StyleKeys.Background, RGBA(0, 1, 0, 1)) container.add(child2) child3 = Panel(self.context) child3.preferred_size_override = Some(Dimension(30, 40)) child3.minimum_size_override = Some(Dimension(10, 20)) child3.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) container.add(child3) def test(visibility: Sequence[bool]): child1.visible = visibility[0] child2.visible = visibility[1] child3.visible = visibility[2] self.assertEqual(True, container.layout_pending) self.context.process() self.assertEqual(False, container.layout_pending) name = f"hbox-visibility-{'-'.join(map(str, visibility))}" self.assertImage(name, self.context) for v1 in [True, False]: for v2 in [True, False]: for v3 in [True, False]: test((v1, v2, v3))
def test_draw_children(self): window = Frame(self.context) window.bounds = Bounds(10, 20, 80, 60) window.set_color(StyleKeys.Background, RGBA(0.5, 0.5, 0.5, 1)) child1 = Panel(self.context) child1.bounds = Bounds(10, 10, 40, 40) child1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) child2 = Panel(self.context) child2.bounds = Bounds(30, 30, 40, 40) child2.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) window.add(child1) window.add(child2) self.context.process() self.assertImage("draw_children", self.context)
def test_draw(self): window = Frame(self.context) window.bounds = Bounds(0, 0, 100, 60) label = Label(self.context) label.text = "Text" label.bounds = Bounds(0, 30, 60, 30) label2 = Label(self.context) label2.text = "AlleyCat" label2.text_size = 18 label2.set_color(StyleKeys.Text, RGBA(1, 0, 0, 1)) label2.bounds = Bounds(20, 0, 80, 60) window.add(label) window.add(label2) self.context.process() self.assertImage("draw", self.context, tolerance=Tolerance)
def create_container(self, areas: Set[Border]) -> Container: container = Frame(self.context, BorderLayout()) container.bounds = Bounds(5, 5, 90, 90) if Border.Top in areas: top = Panel(self.context) top.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) top.preferred_size_override = Some(Dimension(0, 20)) container.add(top, Border.Top) if Border.Right in areas: right = Panel(self.context) right.set_color(StyleKeys.Background, RGBA(0, 1, 0, 1)) right.preferred_size_override = Some(Dimension(15, 0)) container.add(right, Border.Right) if Border.Bottom in areas: bottom = Panel(self.context) bottom.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) bottom.preferred_size_override = Some(Dimension(0, 20)) container.add(bottom, Border.Bottom) if Border.Left in areas: left = Panel(self.context) left.set_color(StyleKeys.Background, RGBA(1, 1, 1, 1)) left.preferred_size_override = Some(Dimension(5, 0)) container.add(left, Border.Left) if Border.Center in areas: center = Panel(self.context) center.set_color(StyleKeys.Background, RGBA(0, 0, 0, 1)) container.add(center, Border.Center) return container
def test_validation(self): container = Frame(self.context) container.bounds = Bounds(30, 30, 200, 200) child = Panel(self.context) child.bounds = Bounds(10, 10, 20, 20) container.validate() self.assertEqual(True, container.valid) container.add(child) self.assertEqual(False, container.valid) container.validate() child.bounds = Bounds(10, 10, 30, 30) self.assertEqual(False, container.valid) container.validate() child.preferred_size_override = Some(Dimension(60, 60)) self.assertEqual(False, container.valid) container.validate() child.minimum_size_override = Some(Dimension(10, 10)) self.assertEqual(False, container.valid) container.validate() child.visible = False self.assertEqual(False, container.valid) container.validate() container.remove(child) self.assertEqual(False, container.valid)
def test_layout(self): container = Frame(self.context, StackLayout()) container.bounds = Bounds(0, 0, 100, 100) child1 = Panel(self.context) child1.bounds = Bounds(10, 10, 20, 20) child1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) child2 = Panel(self.context) child2.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) child2.preferred_size_override = Some(Dimension(80, 60)) child3 = Panel(self.context) child3.set_color(StyleKeys.Background, RGBA(0, 1, 0, 1)) child3.preferred_size_override = Some(Dimension(60, 40)) container.add(child1) container.add(child2, fill=False) container.add(child3, False) self.context.process() self.assertImage("stack", self.context) child4 = Panel(self.context) child4.set_color(StyleKeys.Background, RGBA(0, 1, 1, 1)) container.add(child4, fill=True) self.context.process() self.assertImage("stack-fill", self.context) container.add(child3, True) self.context.process() self.assertImage("stack-fill2", self.context)
def test_draw(self): image = self.context.toolkit.images[FixturePath] window = Frame(self.context) window.bounds = Bounds(0, 0, 100, 100) canvas1 = Canvas(self.context) canvas1.bounds = Bounds(30, 40, 80, 30) canvas1.set_color(StyleKeys.Background, RGBA(1, 0, 0, 1)) canvas2 = Canvas(self.context, image) canvas2.bounds = Bounds(0, 10, 64, 64) canvas3 = Canvas(self.context, image) canvas3.bounds = Bounds(10, 70, 80, 20) canvas3.set_color(StyleKeys.Background, RGBA(0, 0, 1, 1)) window.add(canvas1) window.add(canvas2) window.add(canvas3) self.context.process() self.assertImage("draw", self.context, tolerance=Tolerance)
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)