Skip to content

Commit

Permalink
Fullscreen improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Nov 28, 2023
1 parent a0e3306 commit 6198bad
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 104 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
### Changed

- Always show help text in footer, regardless of notification state
- Add highlight border to fullscreened pane
- Allow exiting fullscreen mode with ESC

## [0.8.0] - 2023-11-21

Expand Down
4 changes: 2 additions & 2 deletions src/tui/view/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub mod response;
pub mod root;
pub mod settings;

pub use root::Root;

use crate::tui::view::{
draw::{Draw, DrawContext},
event::{Event, EventHandler, Update, UpdateContext},
};
use crossterm::event::MouseEvent;
use derive_more::{Deref, DerefMut};
pub use primary::FullscreenMode;
use ratatui::layout::Rect;
pub use root::Root;
use std::cell::Cell;

/// A wrapper around the various component types. The main job of this is to
Expand Down
61 changes: 40 additions & 21 deletions src/tui/view/component/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum PrimaryPane {
/// shown in fullscreen. If one of these is requested while not available, we
/// simply won't show it.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FullscreenMode {
enum FullscreenMode {
/// Fullscreen the active request recipe
Request,
/// Fullscreen the active response
Expand Down Expand Up @@ -106,6 +106,15 @@ impl PrimaryView {
fn selected_profile(&self) -> Option<&Profile> {
self.profile_list_pane.profiles.selected()
}

fn toggle_fullscreen(&mut self, mode: FullscreenMode) {
// If we're already in the given mode, exit
self.fullscreen_mode = if Some(mode) == self.fullscreen_mode {
None
} else {
Some(mode)
};
}
}

impl EventHandler for PrimaryView {
Expand All @@ -127,10 +136,10 @@ impl EventHandler for PrimaryView {

// Input messages
Event::Input {
action,
action: Some(action),
event: term_event,
} => match action {
Some(Action::LeftClick) => {
Action::LeftClick => {
let crossterm::event::Event::Mouse(mouse) = term_event
else {
unreachable!("Mouse action must have mouse event")
Expand All @@ -151,47 +160,57 @@ impl EventHandler for PrimaryView {
}
Update::Consumed
}
Some(Action::PreviousPane) => {
Action::PreviousPane => {
self.selected_pane.previous(context);
Update::Consumed
}
Some(Action::NextPane) => {
Action::NextPane => {
self.selected_pane.next(context);
Update::Consumed
}
Some(Action::SendRequest) => {
Action::SendRequest => {
// Send a request from anywhere
context.queue_event(Event::HttpSendRequest);
Update::Consumed
}
Some(Action::OpenSettings) => {
Action::OpenSettings => {
context.queue_event(Event::OpenModal {
modal: Box::<SettingsModal>::default(),
priority: ModalPriority::Low,
});
Update::Consumed
}
Some(Action::OpenHelp) => {
Action::OpenHelp => {
context.queue_event(Event::OpenModal {
modal: Box::<HelpModal>::default(),
priority: ModalPriority::Low,
});
Update::Consumed
}

// Toggle fullscreen
Action::Fullscreen => {
match self.selected_pane.selected() {
// These aren't fullscreenable. Still consume the event
// though, no one else will need it anyway
PrimaryPane::ProfileList | PrimaryPane::RecipeList => {}
PrimaryPane::Request => {
self.toggle_fullscreen(FullscreenMode::Request)
}
PrimaryPane::Response => {
self.toggle_fullscreen(FullscreenMode::Response)
}
}
Update::Consumed
}
// Exit fullscreen
Action::Cancel if self.fullscreen_mode.is_some() => {
self.fullscreen_mode = None;
Update::Consumed
}
_ => Update::Propagate(event),
},

Event::ToggleFullscreen(mode) => {
// If we're already in the given mode, exit
self.fullscreen_mode =
if Some(mode) == self.fullscreen_mode.as_ref() {
None
} else {
Some(*mode)
};
Update::Consumed
}

_ => Update::Propagate(event),
}
}
Expand Down Expand Up @@ -294,7 +313,7 @@ impl<'a> Draw<PrimaryViewProps<'a>> for PrimaryView {
self.request_pane.draw(
context,
RequestPaneProps {
is_selected: false,
is_selected: true,
selected_recipe: self.selected_recipe(),
selected_profile_id: self
.selected_profile()
Expand All @@ -307,7 +326,7 @@ impl<'a> Draw<PrimaryViewProps<'a>> for PrimaryView {
self.response_pane.draw(
context,
ResponsePaneProps {
is_selected: false,
is_selected: true,
active_request: props.active_request,
},
area,
Expand Down
40 changes: 10 additions & 30 deletions src/tui/view/component/request.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use crate::{
collection::{ProfileId, RequestRecipe, RequestRecipeId},
template::Template,
tui::{
input::Action,
view::{
common::{
table::Table, tabs::Tabs, template_preview::TemplatePreview,
text_window::TextWindow, Block,
},
component::primary::{FullscreenMode, PrimaryPane},
draw::{Draw, DrawContext, Generate},
event::{Event, EventHandler, Update, UpdateContext},
state::StateCell,
util::layout,
Component,
tui::view::{
common::{
table::Table, tabs::Tabs, template_preview::TemplatePreview,
text_window::TextWindow, Block,
},
component::primary::PrimaryPane,
draw::{Draw, DrawContext, Generate},
event::EventHandler,
state::StateCell,
util::layout,
Component,
},
};
use derive_more::Display;
Expand Down Expand Up @@ -65,23 +62,6 @@ enum Tab {
}

impl EventHandler for RequestPane {
fn update(&mut self, context: &mut UpdateContext, event: Event) -> Update {
match event {
// Toggle fullscreen
Event::Input {
action: Some(Action::Fullscreen),
..
} => {
context.queue_event(Event::ToggleFullscreen(
FullscreenMode::Request,
));
Update::Consumed
}

_ => Update::Propagate(event),
}
}

fn children(&mut self) -> Vec<Component<&mut dyn EventHandler>> {
let mut children = vec![self.tabs.as_child()];
// If the body is initialized and present, send events there too
Expand Down
55 changes: 8 additions & 47 deletions src/tui/view/component/response.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::{
http::{RequestId, RequestRecord},
tui::{
input::Action,
view::{
common::{
table::Table, tabs::Tabs, text_window::TextWindow, Block,
},
component::primary::{FullscreenMode, PrimaryPane},
draw::{Draw, DrawContext, Generate},
event::{Event, EventHandler, Update, UpdateContext},
state::{RequestState, StateCell},
util::layout,
Component,
},
tui::view::{
common::{table::Table, tabs::Tabs, text_window::TextWindow, Block},
component::primary::PrimaryPane,
draw::{Draw, DrawContext, Generate},
event::EventHandler,
state::{RequestState, StateCell},
util::layout,
Component,
},
};
use derive_more::Display;
Expand Down Expand Up @@ -45,23 +40,6 @@ enum Tab {
}

impl EventHandler for ResponsePane {
fn update(&mut self, context: &mut UpdateContext, event: Event) -> Update {
match event {
// Toggle fullscreen
Event::Input {
action: Some(Action::Fullscreen),
..
} => {
context.queue_event(Event::ToggleFullscreen(
FullscreenMode::Response,
));
Update::Consumed
}

_ => Update::Propagate(event),
}
}

fn children(&mut self) -> Vec<Component<&mut dyn EventHandler>> {
vec![self.content.as_child()]
}
Expand Down Expand Up @@ -185,23 +163,6 @@ struct ResponseContentProps<'a> {
}

impl EventHandler for ResponseContent {
fn update(&mut self, context: &mut UpdateContext, event: Event) -> Update {
match event {
// Toggle fullscreen
Event::Input {
action: Some(Action::Fullscreen),
..
} => {
context.queue_event(Event::ToggleFullscreen(
FullscreenMode::Response,
));
Update::Consumed
}

_ => Update::Propagate(event),
}
}

fn children(&mut self) -> Vec<Component<&mut dyn EventHandler>> {
let mut children = vec![self.tabs.as_child()];
if let Some(body) = self.body.get_mut() {
Expand Down
4 changes: 0 additions & 4 deletions src/tui/view/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
message::{Message, MessageSender},
view::{
common::modal::{Modal, ModalPriority},
component::FullscreenMode,
state::{Notification, RequestState},
Component, ViewConfig,
},
Expand Down Expand Up @@ -108,9 +107,6 @@ pub enum Event {
state: RequestState,
},

/// Enter a particular fullscreen mode. If we're already in that mode, exit
ToggleFullscreen(FullscreenMode),

/// Show a modal to the user
OpenModal {
modal: Box<dyn Modal>,
Expand Down

0 comments on commit 6198bad

Please sign in to comment.