diff --git a/src/textual_paint/paint.py b/src/textual_paint/paint.py index 822612d..a2e3dd6 100755 --- a/src/textual_paint/paint.py +++ b/src/textual_paint/paint.py @@ -66,6 +66,8 @@ from textual_paint.windows import (CharacterSelectorDialogWindow, DialogWindow, MAX_FILE_SIZE = 500000 # 500 KB +DOUBLE_CLICK_TIME = 0.8 # seconds; overridden in tests to avoid flakiness + # Most arguments are handled at the end of the file, # but it may be important to do this one early. load_language(args.language) @@ -397,7 +399,7 @@ class CharInput(Input, inherit_bindings=False): if event.ctrl or event.button == 3: # right click self.app.action_swap_colors() return - if event.time - self.last_click_time < 0.8: + if event.time - self.last_click_time < DOUBLE_CLICK_TIME: self.app.action_open_character_selector() self.last_click_time = event.time @@ -446,7 +448,7 @@ class ColorsBox(Container): secondary = event.ctrl or event.button == 3 self.post_message(self.ColorSelected(self.color_by_button[button], secondary)) # Detect double click and open Edit Colors dialog. - if event.time - self.last_click_time < 0.8 and button == self.last_click_button: + if event.time - self.last_click_time < DOUBLE_CLICK_TIME and button == self.last_click_button: assert isinstance(self.app, PaintApp) self.app.action_edit_colors(self.query(".color_button").nodes.index(button), secondary) self.last_click_time = event.time diff --git a/tests/test_snapshots.py b/tests/test_snapshots.py index 52fa66a..0a670c6 100644 --- a/tests/test_snapshots.py +++ b/tests/test_snapshots.py @@ -1,8 +1,11 @@ from pathlib import Path import pytest +from textual.pilot import Pilot from textual.widgets import Input +from textual_paint import paint + # These paths are treated as relative to this file. APPS_DIR = Path("../src/textual_paint") PAINT = APPS_DIR / "paint.py" @@ -10,9 +13,12 @@ GALLERY = APPS_DIR / "gallery.py" LARGER = (81, 38) """Large enough to show the entire paint app.""" +LARGEST = (107, 42) +"""Large enough to show the Edit Colors dialog, which is a bit oversized.""" -# Prevent flaky tests due to cursor blinking +# Prevent flaky tests due to timing issues. Input.cursor_blink = False +paint.DOUBLE_CLICK_TIME = 2.0 @pytest.fixture(params=[ {"theme": "light", "ascii_only": False}, @@ -61,6 +67,27 @@ def test_paint_view_bitmap(snap_compare): def test_paint_invert_and_exit(snap_compare, each_theme): assert snap_compare(PAINT, press=["ctrl+i", "ctrl+q"]) +def test_swap_selected_colors(snap_compare): + async def swap_selected_colors(pilot: Pilot): + await pilot.click("CharInput", control=True) + + assert snap_compare(PAINT, run_before=swap_selected_colors) + +def test_paint_character_picker_dialog(snap_compare, each_theme): + async def open_character_picker(pilot: Pilot): + await pilot.click("CharInput") + await pilot.click("CharInput") + + assert snap_compare(PAINT, run_before=open_character_picker, terminal_size=LARGER) + +def test_paint_edit_colors_dialog(snap_compare, each_theme): + async def open_edit_colors(pilot: Pilot): + pilot.app.query("ColorsBox Button")[0].id = "a_color_button" + await pilot.click("#a_color_button") + await pilot.click("#a_color_button") + + assert snap_compare(PAINT, run_before=open_edit_colors, terminal_size=LARGEST) + def test_gallery_app(snap_compare): assert snap_compare(GALLERY)