Skip to content

Commit

Permalink
refactor(world_map): remove unnecessary struct fields and use constants
Browse files Browse the repository at this point in the history
- Remove satellit_symbol and trajectory_color fields from WorldMap struct
- Add MAP_COLOR, TRAJECTORY_COLOR, and SATELLIT_SYMBOL constants to WorldMap
  • Loading branch information
ShenMian committed Dec 24, 2024
1 parent b033186 commit 8a41683
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent};
use ratatui::{
layout::{Constraint, Layout},
prelude::CrosstermBackend,
style::Color,
Terminal,
};

Expand Down Expand Up @@ -75,8 +74,6 @@ impl App {

let world_map = WorldMap {
satellites_state: &self.satellites_state,
satellit_symbol: "+".to_string(),
trajectory_color: Color::LightBlue,
};
frame.render_stateful_widget(world_map, left, &mut self.world_map_state);

Expand Down
24 changes: 12 additions & 12 deletions src/widgets/world_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use super::satellites::SatellitesState;

pub struct WorldMap<'a> {
pub satellites_state: &'a SatellitesState,
pub satellit_symbol: String,
pub trajectory_color: Color,
}

#[derive(Default)]
Expand All @@ -29,6 +27,10 @@ pub struct WorldMapState {
}

impl WorldMap<'_> {
const MAP_COLOR: Color = Color::Gray;
const TRAJECTORY_COLOR: Color = Color::LightBlue;
const SATELLIT_SYMBOL: &'static str = "+";

fn render_block(&self, area: Rect, buf: &mut Buffer, state: &mut WorldMapState) {
let block = Block::bordered().title("World map".blue());
state.inner_area = block.inner(area);
Expand All @@ -40,18 +42,16 @@ impl WorldMap<'_> {
.paint(|ctx| {
// Draw the world map
ctx.draw(&Map {
color: Color::Gray,
color: Self::MAP_COLOR,
resolution: MapResolution::High,
});

// Draw satellites
for object in self.satellites_state.objects.iter() {
let line = if state.selected_object.is_none() {
self.satellit_symbol.clone().light_red()
+ format!(" {}", object.name()).white()
Self::SATELLIT_SYMBOL.light_red() + format!(" {}", object.name()).white()
} else {
self.satellit_symbol.clone().red()
+ format!(" {}", object.name()).dark_gray()
Self::SATELLIT_SYMBOL.red() + format!(" {}", object.name()).dark_gray()
};
let state = object.predict(Utc::now()).unwrap();
ctx.print(state.position[0], state.position[1], line);
Expand Down Expand Up @@ -85,22 +85,22 @@ impl WorldMap<'_> {
// Handle trajectory crossing the international date line
if (x1 - x2).abs() >= 180.0 {
let x_edge = if x1 > 0.0 { 180.0 } else { -180.0 };
ctx.draw(&Line::new(x1, y1, x_edge, y2, self.trajectory_color));
ctx.draw(&Line::new(-x_edge, y1, x2, y2, self.trajectory_color));
ctx.draw(&Line::new(x1, y1, x_edge, y2, Self::TRAJECTORY_COLOR));
ctx.draw(&Line::new(-x_edge, y1, x2, y2, Self::TRAJECTORY_COLOR));
continue;
}
if (y1 - y2).abs() >= 90.0 {
// TEMPSAT 1 (1512), CALSPHERE 4A (1520)
continue;
}
ctx.draw(&Line::new(x1, y1, x2, y2, self.trajectory_color));
ctx.draw(&Line::new(x1, y1, x2, y2, Self::TRAJECTORY_COLOR));
}

// Highlight the selected satellite
ctx.print(
state.position[0],
state.position[1],
self.satellit_symbol.clone().light_green().slow_blink()
Self::SATELLIT_SYMBOL.light_green().slow_blink()
+ format!(" {}", selected.name()).white(),
);
} else if let Some(hovered_object_index) = state.hovered_object {
Expand All @@ -111,7 +111,7 @@ impl WorldMap<'_> {
ctx.print(
state.position[0],
state.position[1],
self.satellit_symbol.clone().light_red().reversed()
Self::SATELLIT_SYMBOL.light_red().reversed()
+ " ".into()
+ hovered.name().clone().white().reversed(),
);
Expand Down

0 comments on commit 8a41683

Please sign in to comment.