Refactor: Extract additional constants for better code clarity

- Extract MOUSE_BUTTON_LEFT constant to replace magic number 1
- Extract ZOOM_PERCENTAGE_MULTIPLIER constant for zoom display
- Extract HSV_HUE_MAX_DEGREES constant for color generation
- Improve zoom display calculation readability

These changes improve code maintainability by replacing magic numbers
with named constants that clearly express their purpose.
This commit is contained in:
Daniel Viegas 2025-11-30 01:22:36 +01:00
parent 99f0cd3236
commit 26bca526c3

View File

@ -130,6 +130,10 @@ FONT_SIZE_LARGE = 24
MARKER_HOVER_SIZE_MULTIPLIER = 1.3 MARKER_HOVER_SIZE_MULTIPLIER = 1.3
MARKER_SELECTED_SIZE_MULTIPLIER = 1.2 MARKER_SELECTED_SIZE_MULTIPLIER = 1.2
DEFAULT_EVENT_SHAPE = 'square' # Default shape for events without specific shape mapping DEFAULT_EVENT_SHAPE = 'square' # Default shape for events without specific shape mapping
# Mouse Button Constants
MOUSE_BUTTON_LEFT = 1 # Left mouse button
ZOOM_PERCENTAGE_MULTIPLIER = 100 # Multiplier to convert zoom level (0.0-1.0) to percentage
GRADIENT_BRIGHTNESS_OFFSET = 0.2 GRADIENT_BRIGHTNESS_OFFSET = 0.2
GRADIENT_DARKNESS_OFFSET = 0.1 GRADIENT_DARKNESS_OFFSET = 0.1
SHADOW_OFFSET_X = 1 SHADOW_OFFSET_X = 1
@ -157,6 +161,7 @@ CONNECTION_LINE_MIN_DISTANCE = EVENT_MARKER_SIZE * 2 # Minimum distance between
PERSON_COLOR_SATURATION = 0.7 # High saturation for vibrant colors PERSON_COLOR_SATURATION = 0.7 # High saturation for vibrant colors
PERSON_COLOR_LIGHTNESS = 0.5 # Medium lightness for good visibility PERSON_COLOR_LIGHTNESS = 0.5 # Medium lightness for good visibility
PERSON_COLOR_ALPHA = 0.75 # Semi-transparent PERSON_COLOR_ALPHA = 0.75 # Semi-transparent
HSV_HUE_MAX_DEGREES = 360 # Maximum value for hue in HSV color space (degrees)
# Marker State Colors # Marker State Colors
SELECTED_MARKER_COLOR = (0.2, 0.4, 0.9) # Blue highlight for selected person's events SELECTED_MARKER_COLOR = (0.2, 0.4, 0.9) # Blue highlight for selected person's events
@ -2287,7 +2292,7 @@ class MyTimelineView(NavigationView):
# Generate color using HSV: vary hue, keep saturation and lightness constant # Generate color using HSV: vary hue, keep saturation and lightness constant
# Use hash of handle for consistent color even when selection order changes # Use hash of handle for consistent color even when selection order changes
handle_hash = hash(person_handle) handle_hash = hash(person_handle)
hue = (abs(handle_hash) % 360) / 360.0 # 0-1 range hue = (abs(handle_hash) % HSV_HUE_MAX_DEGREES) / float(HSV_HUE_MAX_DEGREES) # 0-1 range
# Convert HSV to RGB (HSV is Hue, Saturation, Value) # Convert HSV to RGB (HSV is Hue, Saturation, Value)
r, g, b = colorsys.hsv_to_rgb(hue, PERSON_COLOR_SATURATION, PERSON_COLOR_LIGHTNESS) r, g, b = colorsys.hsv_to_rgb(hue, PERSON_COLOR_SATURATION, PERSON_COLOR_LIGHTNESS)
@ -2526,7 +2531,8 @@ class MyTimelineView(NavigationView):
def update_zoom_display(self) -> None: def update_zoom_display(self) -> None:
"""Update the zoom level display.""" """Update the zoom level display."""
if hasattr(self, 'zoom_label'): if hasattr(self, 'zoom_label'):
self.zoom_label.set_text(f"{int(self.zoom_level * 100)}%") zoom_percentage = int(self.zoom_level * ZOOM_PERCENTAGE_MULTIPLIER)
self.zoom_label.set_text(f"{zoom_percentage}%")
def on_button_press(self, widget: Gtk.Widget, event: Gdk.Event) -> bool: def on_button_press(self, widget: Gtk.Widget, event: Gdk.Event) -> bool:
""" """
@ -2539,7 +2545,7 @@ class MyTimelineView(NavigationView):
Returns: Returns:
bool: False to allow other handlers to process the event. bool: False to allow other handlers to process the event.
""" """
if event.button == 1: # Left click if event.button == MOUSE_BUTTON_LEFT:
# Find which event was clicked # Find which event was clicked
clicked_index = self.find_event_at_position(event.x, event.y) clicked_index = self.find_event_at_position(event.x, event.y)
if clicked_index is not None: if clicked_index is not None: