Skip to content

Commit

Permalink
Only show resize command when more than one buffer & focused
Browse files Browse the repository at this point in the history
  • Loading branch information
jhff committed Jul 31, 2023
1 parent d51c73a commit 4d7a8c4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
21 changes: 21 additions & 0 deletions data/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,24 @@ pub enum Color {
#[default]
Unique,
}

#[derive(Debug, Clone, Copy)]
pub enum Resize {
None,
Maximize,
Restore,
}

impl Resize {
pub fn action(can_resize: bool, maximized: bool) -> Self {
if can_resize {
if maximized {
Self::Restore
} else {
Self::Maximize
}
} else {
Self::None
}
}
}
13 changes: 9 additions & 4 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl Dashboard {
.view(
&all_buffers(clients, &self.history),
self.focus.is_some(),
self.is_pane_maximized(),
self.buffer_resize_action(),
config,
)
.map(Message::Command),
Expand Down Expand Up @@ -550,7 +550,7 @@ impl Dashboard {
// - Restore maximized pane
// - Unfocus
if self.command_bar.is_some() {
return self.toggle_command_bar(&closed_buffers(&self, clients), config, theme);
return self.toggle_command_bar(&closed_buffers(self, clients), config, theme);
} else if self.is_pane_maximized() {
self.panes.restore();
} else {
Expand Down Expand Up @@ -599,7 +599,7 @@ impl Dashboard {

Command::perform(task, |_| Message::Close)
}
CommandBar => self.toggle_command_bar(&closed_buffers(&self, clients), config, theme),
CommandBar => self.toggle_command_bar(&closed_buffers(self, clients), config, theme),
}
}

Expand Down Expand Up @@ -891,13 +891,18 @@ impl Dashboard {
buffers,
config,
self.focus.is_some(),
self.is_pane_maximized(),
self.buffer_resize_action(),
));
}

fn close_command_bar(&mut self) {
self.command_bar = None;
}

fn buffer_resize_action(&self) -> data::buffer::Resize {
let can_resize_buffer = self.focus.is_some() && self.panes.len() > 1;
data::buffer::Resize::action(can_resize_buffer, self.is_pane_maximized())
}
}

impl From<data::Dashboard> for Dashboard {
Expand Down
38 changes: 25 additions & 13 deletions src/screen/dashboard/command_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ impl CommandBar {
buffers: &[data::Buffer],
config: &Config,
is_focused_buffer: bool,
maximized: bool,
resize_buffer: data::buffer::Resize,
) -> Self {
let state =
combo_box::State::new(Command::list(buffers, config, is_focused_buffer, maximized));
let state = combo_box::State::new(Command::list(
buffers,
config,
is_focused_buffer,
resize_buffer,
));
state.focus();

Self { state }
Expand All @@ -48,7 +52,7 @@ impl CommandBar {
&'a self,
buffers: &[data::Buffer],
focused_buffer: bool,
maximized: bool,
resize_buffer: data::buffer::Resize,
config: &'a Config,
) -> Element<'a, Message> {
// 1px larger than default
Expand All @@ -75,7 +79,7 @@ impl CommandBar {
column(
std::iter::once(text("Type a command...").size(font_size))
.chain(
Command::list(buffers, config, focused_buffer, maximized)
Command::list(buffers, config, focused_buffer, resize_buffer)
.iter()
.map(|command| text(command).size(font_size)),
)
Expand Down Expand Up @@ -134,9 +138,9 @@ impl Command {
buffers: &[data::Buffer],
config: &Config,
is_focused_buffer: bool,
maximized: bool,
resize_buffer: data::buffer::Resize,
) -> Vec<Self> {
let buffers = Buffer::list(buffers, is_focused_buffer, maximized)
let buffers = Buffer::list(buffers, is_focused_buffer, resize_buffer)
.into_iter()
.map(Command::Buffer);

Expand Down Expand Up @@ -164,15 +168,23 @@ impl std::fmt::Display for Command {
}

impl Buffer {
fn list(buffers: &[data::Buffer], is_focused_buffer: bool, maximized: bool) -> Vec<Self> {
fn list(
buffers: &[data::Buffer],
is_focused_buffer: bool,
resize_buffer: data::buffer::Resize,
) -> Vec<Self> {
let mut list = vec![Buffer::New];

if is_focused_buffer {
list.extend(
[Buffer::Close, Buffer::Maximize(!maximized)]
.into_iter()
.chain(buffers.iter().cloned().map(Buffer::Replace)),
);
list.push(Buffer::Close);

match resize_buffer {
data::buffer::Resize::Maximize => list.push(Buffer::Maximize(true)),
data::buffer::Resize::Restore => list.push(Buffer::Maximize(false)),
data::buffer::Resize::None => {}
}

list.extend(buffers.iter().cloned().map(Buffer::Replace));
}

list
Expand Down

0 comments on commit 4d7a8c4

Please sign in to comment.