ai_code_buddy/widget_states/
overview.rs

1use bevy::prelude::*;
2use ratatui::layout::{Position, Rect};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, Eq, PartialEq, Hash)]
6pub enum OverviewComponent {
7    StartAnalysis,
8    ViewReports,
9    Settings,
10    Help,
11    Exit,
12}
13
14#[derive(Debug, Clone, Resource)]
15pub struct OverviewWidgetState {
16    pub selected_component: OverviewComponent,
17    pub hovered_component: Option<OverviewComponent>,
18    pub registered_components: HashMap<OverviewComponent, Rect>,
19    pub repo_info: RepoInfo,
20    pub show_help: bool,
21}
22
23#[derive(Debug, Clone)]
24pub struct RepoInfo {
25    pub path: String,
26    pub source_branch: String,
27    pub target_branch: String,
28    pub files_to_analyze: usize,
29}
30
31impl Default for OverviewWidgetState {
32    fn default() -> Self {
33        Self {
34            selected_component: OverviewComponent::StartAnalysis,
35            hovered_component: None,
36            registered_components: HashMap::new(),
37            show_help: false,
38            repo_info: RepoInfo {
39                path: ".".to_string(),
40                source_branch: "main".to_string(),
41                target_branch: "HEAD".to_string(),
42                files_to_analyze: 0,
43            },
44        }
45    }
46}
47
48impl OverviewWidgetState {
49    pub fn is_over(&self, component: OverviewComponent, x: u16, y: u16) -> bool {
50        if let Some(rect) = self.registered_components.get(&component) {
51            rect.contains(Position { x, y })
52        } else {
53            false
54        }
55    }
56
57    pub fn update_hover(&mut self, x: u16, y: u16) {
58        self.hovered_component = None;
59        for (component, rect) in &self.registered_components {
60            if rect.contains(Position { x, y }) {
61                self.hovered_component = Some(component.clone());
62                break;
63            }
64        }
65    }
66
67    pub fn move_selection(&mut self, direction: SelectionDirection) {
68        self.selected_component = match direction {
69            SelectionDirection::Next => match self.selected_component {
70                OverviewComponent::StartAnalysis => OverviewComponent::ViewReports,
71                OverviewComponent::ViewReports => OverviewComponent::Settings,
72                OverviewComponent::Settings => OverviewComponent::Help,
73                OverviewComponent::Help => OverviewComponent::Exit,
74                OverviewComponent::Exit => OverviewComponent::StartAnalysis,
75            },
76            SelectionDirection::Previous => match self.selected_component {
77                OverviewComponent::StartAnalysis => OverviewComponent::Exit,
78                OverviewComponent::ViewReports => OverviewComponent::StartAnalysis,
79                OverviewComponent::Settings => OverviewComponent::ViewReports,
80                OverviewComponent::Help => OverviewComponent::Settings,
81                OverviewComponent::Exit => OverviewComponent::Help,
82            },
83        }
84    }
85}
86
87#[derive(Debug)]
88pub enum SelectionDirection {
89    Next,
90    Previous,
91}