Skip to content

Commit

Permalink
refactor: simplify the Component trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Feb 9, 2025
1 parent 5061940 commit 8b4f01d
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 36 deletions.
3 changes: 1 addition & 2 deletions crates/tui/src/component/footer_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ impl FooterComponent {
}

impl Component for FooterComponent {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx);
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
4 changes: 2 additions & 2 deletions crates/tui/src/component/issue_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Component for IssueComponent {
.border_type(BorderType::Rounded);

let block = self.make_block_focused(state, block);
let pp = Paragraph::new(vec![
let paragraph = Paragraph::new(vec![
Line::from(""),
Line::from("ʕノ•ᴥ•ʔノ ︵ ┻━┻"),
Line::from("Alt: Your mate flipping the desk").italic(),
Expand All @@ -50,7 +50,7 @@ impl Component for IssueComponent {
.areas(area);

f.render_widget(Clear, area);
f.render_widget(pp.block(block), area);
f.render_widget(paragraph.block(block), area);

Ok(())
}
Expand Down
13 changes: 5 additions & 8 deletions crates/tui/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ static BUFFER: ConcurrentRecordsBuffer =
LazyLock::new(|| Arc::new(Mutex::new(RecordsBuffer::new())));

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub enum FocusDirection {
pub(crate) enum FocusDirection {
Top,
Left,
Right,
Bottom,
}

#[derive(Debug, Clone, Display, Hash, PartialEq, Eq, Deserialize, PartialOrd, Ord)]
pub enum ComponentName {
pub(crate) enum ComponentName {
Records,
Topics,
Footer,
Expand Down Expand Up @@ -81,17 +81,14 @@ impl Default for ComponentName {
}
}

pub trait WithHeight: Component {
pub(crate) trait WithHeight: Component {
fn content_height(&self) -> usize {
0
}
}

pub trait Component {
#[allow(unused_variables)]
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
Ok(())
}
pub(crate) trait Component {
fn register_action_handler(&mut self, _tx: UnboundedSender<Action>) {}

fn id(&self) -> ComponentName;

Expand Down
3 changes: 1 addition & 2 deletions crates/tui/src/component/record_details_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ impl RecordDetailsComponent<'_> {
}

impl Component for RecordDetailsComponent<'_> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx);
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
3 changes: 1 addition & 2 deletions crates/tui/src/component/records_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ impl<'a> RecordsComponent<'a> {
}

impl Component for RecordsComponent<'_> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx.clone());
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
15 changes: 9 additions & 6 deletions crates/tui/src/component/root_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,21 @@ impl RootComponent {
}

impl Component for RootComponent {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx.clone());
for component in self.components.values_mut() {
component
.lock()
.unwrap()
.register_action_handler(tx.clone())?;
.register_action_handler(tx.clone());
}
}

fn id(&self) -> ComponentName {
ComponentName::Main
}

fn init(&mut self) -> Result<(), TuiError> {
self.notify_footer()?;
self.action_tx.as_ref().unwrap().send(Action::Shortcuts(
self.components
Expand All @@ -208,10 +215,6 @@ impl Component for RootComponent {
Ok(())
}

fn id(&self) -> ComponentName {
ComponentName::Main
}

fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>, TuiError> {
match key.code {
KeyCode::Tab => {
Expand Down
3 changes: 1 addition & 2 deletions crates/tui/src/component/schemas_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ impl SchemasComponent<'_> {
}

impl Component for SchemasComponent<'_> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx);
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
4 changes: 1 addition & 3 deletions crates/tui/src/component/search_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ impl SearchComponent {
}

impl Component for SearchComponent {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx);
//self.search()?;
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
4 changes: 2 additions & 2 deletions crates/tui/src/component/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::ComponentName;

#[derive(Clone)]
pub struct State {
pub focused: ComponentName,
pub(crate) focused: ComponentName,
pub cluster: String,
pub themes: Vec<String>,
pub theme: Theme,
Expand All @@ -33,7 +33,7 @@ impl State {
}
}

pub fn is_focused(&self, component_name: ComponentName) -> bool {
pub(crate) fn is_focused(&self, component_name: ComponentName) -> bool {
self.focused == component_name
}
}
3 changes: 1 addition & 2 deletions crates/tui/src/component/topic_details_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ impl WithHeight for TopicDetailsComponent {
}

impl Component for TopicDetailsComponent {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx);
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
3 changes: 1 addition & 2 deletions crates/tui/src/component/topics_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ impl TopicsComponent {
}

impl Component for TopicsComponent {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.action_tx = Some(tx);
Ok(())
}

fn id(&self) -> ComponentName {
Expand Down
2 changes: 1 addition & 1 deletion crates/tui/src/component/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl Ui {
self.load_topics(action_tx.clone())?;
let mut tui = tui::Tui::new()?;
tui.enter()?;
self.root.register_action_handler(action_tx.clone())?;
self.root.register_action_handler(action_tx.clone());
self.root.init()?;
if !topics.is_empty() {
action_tx.send(Action::SelectedTopics(topics))?;
Expand Down
3 changes: 1 addition & 2 deletions crates/tui/src/component/vertical_scrollable_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ impl<C> Component for VerticalScrollableBlock<C>
where
C: WithHeight,
{
#[allow(unused_variables)]
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<(), TuiError> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) {
self.component.register_action_handler(tx)
}

Expand Down

0 comments on commit 8b4f01d

Please sign in to comment.