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