Skip to content

Commit

Permalink
wip: request_uri uses the store now
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Jun 2, 2024
1 parent 34773b6 commit 14d6f92
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 262 deletions.
2 changes: 1 addition & 1 deletion hac-client/benches/collection_viewer_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifi
use hac_client::{
pages::{
collection_viewer::{collection_store::CollectionStore, CollectionViewer},
Eventful, Page,
Eventful, Renderable,
},
utils::build_syntax_highlighted_lines,
};
Expand Down
2 changes: 1 addition & 1 deletion hac-client/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
event_pool::{Event, EventPool},
pages::{Eventful, Page},
pages::{Eventful, Renderable},
screen_manager::ScreenManager,
};
use hac_core::{collection::Collection, command::Command};
Expand Down
35 changes: 2 additions & 33 deletions hac-client/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use ratatui::{layout::Rect, Frame};
use tokio::sync::mpsc::UnboundedSender;

/// A `Page` is anything that is a top level page and can be drawn to the screen
pub trait Page {
pub trait Renderable {
fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()>;

/// pages need to adapt to change of sizes on the application, this function is called
/// by the top level event loop whenever a resize event is produced
#[allow(unused_variables)]
fn resize(&mut self, new_size: Rect);
fn resize(&mut self, new_size: Rect) {}

/// register a page to be a command handler, which means this page will now receive
/// commands from the channel to handle whatever the commands it is interested into
Expand Down Expand Up @@ -56,34 +56,3 @@ pub trait Eventful {
Ok(None)
}
}

/// An `EventfulWithContext` component is a component that can handle events but requires
/// additional information which can be specified by its implementer. Such as defining a
/// associate type required to re-render properly or information that it needs to decide
/// on how to behave
///
/// besides the contextful behavior, this is exactly as `Eventful`
pub trait EventfulWithContext {
type Result;
type Context;

fn handle_event(
&mut self,
event: Option<Event>,
context: Self::Context,
) -> anyhow::Result<Option<Self::Result>> {
match event {
Some(Event::Key(key_event)) => self.handle_key_event(key_event, context),
_ => Ok(None),
}
}

#[allow(unused_variables)]
fn handle_key_event(
&mut self,
key_event: KeyEvent,
context: Self::Context,
) -> anyhow::Result<Option<Self::Result>> {
Ok(None)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::pages::{
confirm_popup::ConfirmPopup,
error_popup::ErrorPopup,
overlay::draw_overlay,
Eventful, Page,
Eventful, Renderable,
};
use hac_core::{collection::types::Collection, command::Command};

Expand Down Expand Up @@ -520,7 +520,7 @@ impl<'a> CollectionDashboard<'a> {
}
}

impl Page for CollectionDashboard<'_> {
impl Renderable for CollectionDashboard<'_> {
fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> {
self.draw_background(size, frame);
self.draw_title(frame)?;
Expand Down
48 changes: 44 additions & 4 deletions hac-client/src/pages/collection_viewer/collection_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ use hac_core::collection::{
Collection,
};

use super::collection_viewer::PaneFocus;

#[derive(Debug)]
pub struct CollectionState {
collection: Rc<RefCell<Collection>>,
hovered_request: Option<String>,
selected_request: Option<Arc<RwLock<Request>>>,
dirs_expanded: Rc<RefCell<HashMap<String, bool>>>,
selected_pane: Option<PaneFocus>,
focused_pane: PaneFocus,
has_pending_request: bool,
}

#[derive(Debug, Default)]
Expand All @@ -30,6 +35,9 @@ pub enum CollectionStoreAction {
HoverPrev,
HoverNext,
ToggleDirectory(String),
SetFocusedPane(PaneFocus),
SetSelectedPane(Option<PaneFocus>),
SetPendingRequest(bool),
}

impl CollectionStore {
Expand All @@ -54,6 +62,9 @@ impl CollectionStore {
hovered_request,
dirs_expanded: Rc::new(RefCell::new(HashMap::default())),
collection: Rc::new(RefCell::new(collection)),
focused_pane: PaneFocus::Sidebar,
selected_pane: None,
has_pending_request: false,
};

self.state = Some(Rc::new(RefCell::new(state)));
Expand All @@ -79,14 +90,23 @@ impl CollectionStore {
.unwrap()
.push(request_kind);
}
CollectionStoreAction::HoverPrev => self.hover_prev(),
CollectionStoreAction::HoverNext => self.hover_next(),
CollectionStoreAction::HoverPrev => self.maybe_hover_prev(),
CollectionStoreAction::HoverNext => self.maybe_hover_next(),
CollectionStoreAction::ToggleDirectory(dir_id) => {
let state = state.borrow_mut();
let mut dirs = state.dirs_expanded.borrow_mut();
let entry = dirs.entry(dir_id).or_insert(false);
*entry = !*entry;
}
CollectionStoreAction::SetFocusedPane(pane) => {
state.borrow_mut().focused_pane = pane
}
CollectionStoreAction::SetSelectedPane(pane) => {
state.borrow_mut().selected_pane = pane
}
CollectionStoreAction::SetPendingRequest(is_pending) => {
state.borrow_mut().has_pending_request = is_pending;
}
}
}
}
Expand All @@ -97,6 +117,20 @@ impl CollectionStore {
.and_then(|state| state.borrow().selected_request.clone())
}

pub fn get_focused_pane(&self) -> PaneFocus {
self.state
.as_ref()
.map(|state| state.borrow().focused_pane)
.expect("tried to get the focused pane without a state")
}

pub fn get_selected_pane(&self) -> Option<PaneFocus> {
self.state
.as_ref()
.map(|state| state.borrow().selected_pane)
.expect("tried to get the selected pane without a state")
}

pub fn get_hovered_request(&self) -> Option<String> {
self.state
.as_ref()
Expand Down Expand Up @@ -127,7 +161,13 @@ impl CollectionStore {
})
}

fn hover_prev(&mut self) {
pub fn has_pending_request(&self) -> bool {
self.state
.as_ref()
.is_some_and(|state| state.borrow().has_pending_request)
}

fn maybe_hover_prev(&mut self) {
if self.get_hovered_request().is_some() && self.get_requests().is_some() {
let id = self.get_hovered_request().unwrap();
let requests = self.get_requests().unwrap();
Expand All @@ -145,7 +185,7 @@ impl CollectionStore {
}
}

fn hover_next(&mut self) {
fn maybe_hover_next(&mut self) {
if self.get_hovered_request().is_some() && self.get_requests().is_some() {
let id = self.get_hovered_request().unwrap();
let requests = self.get_requests().unwrap();
Expand Down
Loading

0 comments on commit 14d6f92

Please sign in to comment.