Skip to content

Commit

Permalink
feat: added basic commands to phone page
Browse files Browse the repository at this point in the history
  • Loading branch information
zaghaghi committed May 1, 2024
1 parent 7846fff commit 2c84e9a
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/pages/phone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl Page for Phone {
KeyCode::Char(']') => EventResponse::Stop(Action::TabNext),
KeyCode::Char('[') => EventResponse::Stop(Action::TabPrev),
KeyCode::Enter => EventResponse::Stop(Action::Submit),
KeyCode::Char(':') => EventResponse::Stop(Action::FocusFooter(":".into(), None)),
_ => {
return Ok(None);
},
Expand All @@ -157,33 +158,35 @@ impl Page for Phone {
}

fn update(&mut self, action: Action, state: &mut State) -> Result<Option<Action>> {
let mut actions: Vec<Option<Action>> = vec![];

match action {
Action::FocusNext => {
let next_index = self.focused_pane_index.saturating_add(1) % self.panes.len();
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
pane.update(Action::UnFocus, state)?;
actions.push(pane.update(Action::UnFocus, state)?);
}
self.focused_pane_index = next_index;
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
pane.update(Action::Focus, state)?;
actions.push(pane.update(Action::Focus, state)?);
}
},
Action::FocusPrev => {
let prev_index = self.focused_pane_index.saturating_add(self.panes.len() - 1) % self.panes.len();
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
pane.update(Action::UnFocus, state)?;
actions.push(pane.update(Action::UnFocus, state)?);
}
self.focused_pane_index = prev_index;
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
pane.update(Action::Focus, state)?;
actions.push(pane.update(Action::Focus, state)?);
}
},
Action::ToggleFullScreen => {
self.fullscreen_pane_index = self.fullscreen_pane_index.map_or(Some(self.focused_pane_index), |_| None);
},
Action::Update => {
for pane in self.panes.iter_mut() {
pane.update(action.clone(), state)?;
actions.push(pane.update(action.clone(), state)?);
}
},
Action::Dial => {
Expand All @@ -194,13 +197,39 @@ impl Page for Phone {
})?;
}
},

Action::FocusFooter(..) => {
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
actions.push(pane.update(Action::UnFocus, state)?);
}
},
Action::FooterResult(cmd, Some(args)) if cmd.eq(":") => {
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
pane.update(Action::Focus, state)?;
}
if args.eq("q") {
actions.push(Some(Action::Quit));
} else if args.eq("send") || args.eq("s") {
actions.push(Some(Action::Dial));
} else {
actions.push(Some(Action::TimedStatusLine("unknown command".into(), 1)));
}
},
Action::FooterResult(_cmd, None) => {
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
actions.push(pane.update(Action::Focus, state)?);
}
},
_ => {
if let Some(pane) = self.panes.get_mut(self.focused_pane_index) {
return pane.update(action, state);
actions.push(pane.update(action, state)?);
}
},
}
if let Some(tx) = &mut self.command_tx {
actions.into_iter().flatten().for_each(|action| {
tx.send(action).ok();
});
}
Ok(None)
}

Expand Down

0 comments on commit 2c84e9a

Please sign in to comment.