feat(board): clock as joined minutes and seconds segments
public
feat(board): clock as joined minutes and seconds segments Fixed segment widths so the clock does not jump while ticking; green while running, red with tenths below 20 s, subtle grey when stopped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | a7d76ada3cddfb3889a8c361a644b85074dc6cb9 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | cf7be23127c4 |
| Children | b1251054cd58 |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/board.py +56 −26
| @@ -491,32 +491,7 @@ class BoardView(Gtk.DrawingArea): | ||
| 491 | 491 | y = oy + 8 * sq if color == bottom else oy - h |
| 492 | 492 | right = ox + 8 * sq |
| 493 | 493 | if self.clocks and color in self.clocks: |
| 494 | - ms = max(0, self.clocks[color]) | |
| 495 | - secs = int(ms // 1000) | |
| 496 | - text = f"{secs // 3600}:{secs // 60 % 60:02d}:{secs % 60:02d}" if secs >= 3600 else ( | |
| 497 | - f"{secs // 60}:{secs % 60:02d}" if secs >= 20 else f"{secs // 60}:{secs % 60:02d}.{int(ms % 1000) // 100}" | |
| 498 | - ) | |
| 499 | - cr.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) | |
| 500 | - cr.set_font_size(h * 0.55) | |
| 501 | - ext = cr.text_extents(text) | |
| 502 | - pad = h * 0.25 | |
| 503 | - bw, bh = ext.x_advance + 2 * pad, h * 0.8 | |
| 504 | - bx, by = right - bw, y + (h - bh) / 2 | |
| 505 | - running = self.clock_running == color | |
| 506 | - low = ms < 20000 | |
| 507 | - if running: | |
| 508 | - cr.set_source_rgb(*((0.80, 0.20, 0.18) if low else (0.25, 0.55, 0.30))) | |
| 509 | - else: | |
| 510 | - cr.set_source_rgba(fg.red, fg.green, fg.blue, 0.12) | |
| 511 | - _rounded(cr, bx, by, bw, bh, bh * 0.25) | |
| 512 | - cr.fill() | |
| 513 | - if running: | |
| 514 | - cr.set_source_rgb(1, 1, 1) | |
| 515 | - else: | |
| 516 | - cr.set_source_rgba(fg.red, fg.green, fg.blue, 0.85) | |
| 517 | - cr.move_to(bx + pad, by + bh / 2 + ext.height / 2) | |
| 518 | - cr.show_text(text) | |
| 519 | - right = bx - h * 0.3 | |
| 494 | + right = self._draw_clock(cr, right, y, h, self.clocks[color], self.clock_running == color, fg) | |
| 520 | 495 | if self.names and color in self.names: |
| 521 | 496 | cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) |
| 522 | 497 | cr.set_font_size(h * 0.45) |
| @@ -526,6 +501,45 @@ class BoardView(Gtk.DrawingArea): | ||
| 526 | 501 | cr.move_to(right - ext.x_advance, y + h / 2 + ext.height / 2) |
| 527 | 502 | cr.show_text(text) |
| 528 | 503 | |
| 504 | + def _draw_clock(self, cr, right, y, h, ms, running, fg): | |
| 505 | + """Uhr als zwei zusammengewachsene Felder: Minuten | Sekunden. Gibt die linke Kante zurück.""" | |
| 506 | + minutes, seconds = clock_parts(ms) | |
| 507 | + low = ms < LOW_TIME_MS | |
| 508 | + cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) | |
| 509 | + cr.set_font_size(h * 0.52) | |
| 510 | + pad = h * 0.22 | |
| 511 | + # feste Mindestbreiten, damit die Uhr beim Ticken nicht springt | |
| 512 | + left_w = max(cr.text_extents("00").x_advance, cr.text_extents(minutes).x_advance) + 2 * pad | |
| 513 | + right_w = max(cr.text_extents("00.0" if low else "00").x_advance, | |
| 514 | + cr.text_extents(seconds).x_advance) + 2 * pad | |
| 515 | + bh = h * 0.78 | |
| 516 | + by = y + (h - bh) / 2 | |
| 517 | + bx = right - left_w - right_w | |
| 518 | + radius = bh * 0.3 | |
| 519 | + | |
| 520 | + if running: | |
| 521 | + cr.set_source_rgb(*(CLOCK_LOW if low else CLOCK_RUNNING)) | |
| 522 | + else: | |
| 523 | + cr.set_source_rgba(fg.red, fg.green, fg.blue, 0.10) | |
| 524 | + _rounded(cr, bx, by, left_w + right_w, bh, radius) | |
| 525 | + cr.fill() | |
| 526 | + # Naht zwischen Minuten und Sekunden | |
| 527 | + cr.set_source_rgba(1, 1, 1, 0.35) if running else cr.set_source_rgba(fg.red, fg.green, fg.blue, 0.18) | |
| 528 | + cr.rectangle(bx + left_w - 0.75, by + bh * 0.18, 1.5, bh * 0.64) | |
| 529 | + cr.fill() | |
| 530 | + | |
| 531 | + if running: | |
| 532 | + cr.set_source_rgb(1, 1, 1) | |
| 533 | + else: | |
| 534 | + cr.set_source_rgba(fg.red, fg.green, fg.blue, 0.85) | |
| 535 | + digits = cr.text_extents("0") | |
| 536 | + baseline = by + bh / 2 + digits.height / 2 | |
| 537 | + for text, x0, width in ((minutes, bx, left_w), (seconds, bx + left_w, right_w)): | |
| 538 | + ext = cr.text_extents(text) | |
| 539 | + cr.move_to(x0 + (width - ext.x_advance) / 2, baseline) | |
| 540 | + cr.show_text(text) | |
| 541 | + return bx - h * 0.3 | |
| 542 | + | |
| 529 | 543 | def _draw_badge(self, cr, square, text, sq): |
| 530 | 544 | x, y = self._square_origin(square) |
| 531 | 545 | r = sq * 0.19 |
| @@ -544,6 +558,22 @@ class BoardView(Gtk.DrawingArea): | ||
| 544 | 558 | cr.show_text(text) |
| 545 | 559 | |
| 546 | 560 | |
| 561 | +LOW_TIME_MS = 20000 # darunter: rot und mit Zehntelsekunden | |
| 562 | +CLOCK_RUNNING = (0.18, 0.55, 0.34) | |
| 563 | +CLOCK_LOW = (0.80, 0.20, 0.18) | |
| 564 | + | |
| 565 | + | |
| 566 | +def clock_parts(ms): | |
| 567 | + """Minuten- und Sekundenfeld: ("4", "37"), ab einer Stunde ("1:02", "37"), knapp ("0", "19.4").""" | |
| 568 | + ms = max(0, int(ms)) | |
| 569 | + secs = ms // 1000 | |
| 570 | + if secs >= 3600: | |
| 571 | + return f"{secs // 3600}:{secs // 60 % 60:02d}", f"{secs % 60:02d}" | |
| 572 | + if ms < LOW_TIME_MS: | |
| 573 | + return f"{secs // 60}", f"{secs % 60:02d}.{ms % 1000 // 100}" | |
| 574 | + return f"{secs // 60}", f"{secs % 60:02d}" | |
| 575 | + | |
| 576 | + | |
| 547 | 577 | def _rounded(cr, x, y, w, h, r): |
| 548 | 578 | cr.new_sub_path() |
| 549 | 579 | cr.arc(x + w - r, y + r, r, -math.pi / 2, 0) |
modified tests/test_notation.py +11 −0
| @@ -35,3 +35,14 @@ class NotationTest(unittest.TestCase): | ||
| 35 | 35 | |
| 36 | 36 | if __name__ == "__main__": |
| 37 | 37 | unittest.main() |
| 38 | + | |
| 39 | + | |
| 40 | +class ClockPartsTest(unittest.TestCase): | |
| 41 | + def test_parts(self): | |
| 42 | + from schach.board import clock_parts | |
| 43 | + | |
| 44 | + self.assertEqual(clock_parts(300000), ("5", "00")) | |
| 45 | + self.assertEqual(clock_parts(61000), ("1", "01")) | |
| 46 | + self.assertEqual(clock_parts(19450), ("0", "19.4")) | |
| 47 | + self.assertEqual(clock_parts(3723000), ("1:02", "03")) | |
| 48 | + self.assertEqual(clock_parts(-5), ("0", "00.0")) | |