Coverage for valmix/knob_widget.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2026-10-25 09:28 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# 

4# SPDX-License-Identifier: Apache-2.0 

5# Copyright 2024 Inria 

6 

7"""KnobWidget widget definition.""" 

8 

9from textual.app import ComposeResult 

10from textual.containers import Widget 

11from textual.widgets import Button, Label, ProgressBar 

12 

13from .knob import Knob 

14 

15 

16class KnobWidget(Widget): 

17 """Widget combining a focus button, a progress bar and a value label. 

18 

19 Attributes: 

20 knob: Knob associated with the widget. 

21 """ 

22 

23 DEFAULT_CSS = r""" 

24 

25 KnobWidget { 

26 height: 3; 

27 layout: horizontal; 

28 } 

29 

30 KnobWidget Button, 

31 KnobWidget Button:enabled, { 

32 border: wide $primary; 

33 margin: 0 2; 

34 width: 3; 

35 } 

36 

37 KnobWidget Button:focus { 

38 background: $accent; 

39 } 

40 

41 KnobWidget ProgressBar { 

42 margin: 1 3; 

43 width: 6fr; 

44 } 

45 

46 KnobWidget Bar { 

47 width: 1fr; 

48 } 

49 

50 KnobWidget PercentageStatus { 

51 display: none; 

52 } 

53 

54 KnobWidget Label { 

55 margin: 1 2; 

56 width: 8; 

57 } 

58 

59 """ 

60 

61 knob: Knob 

62 label: Label 

63 progress_bar: ProgressBar 

64 

65 def __init__(self, knob: Knob): 

66 """Create a new widget. 

67 

68 Args: 

69 knob: Knob associated with the widget. 

70 """ 

71 self.knob = knob 

72 self.label = Label() # updated by compose() 

73 self.progress_bar = ProgressBar() # updated by compose() 

74 super().__init__(id=f"{knob.name}-knob") 

75 

76 def compose(self) -> ComposeResult: 

77 """Render the widget.""" 

78 yield Button(self.knob.name, id=self.knob.name) 

79 

80 self.progress_bar = ProgressBar( 

81 total=self.knob.nb_values - 1, 

82 show_eta=False, 

83 show_percentage=True, 

84 id=f"{self.knob.name}-bar", 

85 ) 

86 self.progress_bar.advance(self.knob.current_index) 

87 yield self.progress_bar 

88 

89 self.label = Label(repr(self.knob), id=f"{self.knob.name}-value") 

90 yield self.label 

91 

92 def advance(self, step: int): 

93 """Advance the underlying knob by a number of steps. 

94 

95 Args: 

96 step: Number of steps to add to advance the knob by. 

97 """ 

98 previous_index = self.knob.current_index 

99 self.knob.advance(step) 

100 if self.knob.current_index == previous_index: 

101 return 

102 self.progress_bar.advance(step) # can advance beyond its total 

103 self.label.update(repr(self.knob))