Skip to content

Commit

Permalink
Merge pull request #1504 from tarkah/feat/pane-grid-maximize
Browse files Browse the repository at this point in the history
Add pane maximize / restore for `PaneGrid`
  • Loading branch information
hecrj authored Nov 8, 2022
2 parents ac737cb + 7de9d24 commit d9f408d
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 86 deletions.
40 changes: 35 additions & 5 deletions examples/pane_grid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ enum Message {
Dragged(pane_grid::DragEvent),
Resized(pane_grid::ResizeEvent),
TogglePin(pane_grid::Pane),
Maximize(pane_grid::Pane),
Restore,
Close(pane_grid::Pane),
CloseFocused,
}
Expand Down Expand Up @@ -114,6 +116,10 @@ impl Application for Example {
*is_pinned = !*is_pinned;
}
}
Message::Maximize(pane) => self.panes.maximize(&pane),
Message::Restore => {
self.panes.restore();
}
Message::Close(pane) => {
if let Some((_, sibling)) = self.panes.close(&pane) {
self.focus = Some(sibling);
Expand Down Expand Up @@ -157,7 +163,7 @@ impl Application for Example {
let focus = self.focus;
let total_panes = self.panes.len();

let pane_grid = PaneGrid::new(&self.panes, |id, pane| {
let pane_grid = PaneGrid::new(&self.panes, |id, pane, is_maximized| {
let is_focused = focus == Some(id);

let pin_button = button(
Expand All @@ -178,7 +184,12 @@ impl Application for Example {
.spacing(5);

let title_bar = pane_grid::TitleBar::new(title)
.controls(view_controls(id, total_panes, pane.is_pinned))
.controls(view_controls(
id,
total_panes,
pane.is_pinned,
is_maximized,
))
.padding(10)
.style(if is_focused {
style::title_bar_focused
Expand Down Expand Up @@ -314,16 +325,35 @@ fn view_controls<'a>(
pane: pane_grid::Pane,
total_panes: usize,
is_pinned: bool,
is_maximized: bool,
) -> Element<'a, Message> {
let mut button = button(text("Close").size(14))
let mut row = row![].spacing(5);

if total_panes > 1 {
let toggle = {
let (content, message) = if is_maximized {
("Restore", Message::Restore)
} else {
("Maximize", Message::Maximize(pane))
};
button(text(content).size(14))
.style(theme::Button::Secondary)
.padding(3)
.on_press(message)
};

row = row.push(toggle);
}

let mut close = button(text("Close").size(14))
.style(theme::Button::Destructive)
.padding(3);

if total_panes > 1 && !is_pinned {
button = button.on_press(Message::Close(pane));
close = close.on_press(Message::Close(pane));
}

button.into()
row.push(close).into()
}

mod style {
Expand Down
Loading

0 comments on commit d9f408d

Please sign in to comment.