Skip to content

Commit

Permalink
Merge pull request #2555 from mtkennerly/feature/pane-grid-compact-co…
Browse files Browse the repository at this point in the history
…ntrols

Add compact variant for pane grid controls
  • Loading branch information
hecrj authored Aug 23, 2024
2 parents bb6fa42 + 3a434c9 commit 84e766f
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 74 deletions.
22 changes: 17 additions & 5 deletions examples/pane_grid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,23 @@ impl Example {
.spacing(5);

let title_bar = pane_grid::TitleBar::new(title)
.controls(view_controls(
id,
total_panes,
pane.is_pinned,
is_maximized,
.controls(pane_grid::Controls::dynamic(
view_controls(
id,
total_panes,
pane.is_pinned,
is_maximized,
),
button(text("X").size(14))
.style(button::danger)
.padding(3)
.on_press_maybe(
if total_panes > 1 && !pane.is_pinned {
Some(Message::Close(id))
} else {
None
},
),
))
.padding(10)
.style(if is_focused {
Expand Down
2 changes: 2 additions & 0 deletions widget/src/pane_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
mod axis;
mod configuration;
mod content;
mod controls;
mod direction;
mod draggable;
mod node;
Expand All @@ -22,6 +23,7 @@ pub mod state;
pub use axis::Axis;
pub use configuration::Configuration;
pub use content::Content;
pub use controls::Controls;
pub use direction::Direction;
pub use draggable::Draggable;
pub use node::Node;
Expand Down
59 changes: 59 additions & 0 deletions widget/src/pane_grid/controls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::container;
use crate::core::{self, Element};

/// The controls of a [`Pane`].
///
/// [`Pane`]: super::Pane
#[allow(missing_debug_implementations)]
pub struct Controls<
'a,
Message,
Theme = crate::Theme,
Renderer = crate::Renderer,
> where
Theme: container::Catalog,
Renderer: core::Renderer,
{
pub(super) full: Element<'a, Message, Theme, Renderer>,
pub(super) compact: Option<Element<'a, Message, Theme, Renderer>>,
}

impl<'a, Message, Theme, Renderer> Controls<'a, Message, Theme, Renderer>
where
Theme: container::Catalog,
Renderer: core::Renderer,
{
/// Creates a new [`Controls`] with the given content.
pub fn new(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self {
Self {
full: content.into(),
compact: None,
}
}

/// Creates a new [`Controls`] with a full and compact variant.
/// If there is not enough room to show the full variant without overlap,
/// then the compact variant will be shown instead.
pub fn dynamic(
full: impl Into<Element<'a, Message, Theme, Renderer>>,
compact: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self {
Self {
full: full.into(),
compact: Some(compact.into()),
}
}
}

impl<'a, Message, Theme, Renderer> From<Element<'a, Message, Theme, Renderer>>
for Controls<'a, Message, Theme, Renderer>
where
Theme: container::Catalog,
Renderer: core::Renderer,
{
fn from(value: Element<'a, Message, Theme, Renderer>) -> Self {
Self::new(value)
}
}
Loading

0 comments on commit 84e766f

Please sign in to comment.