Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add align option to modal #161

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl Application for ModalExample {
)
.backdrop(Message::CloseModal)
.on_esc(Message::CloseModal)
.align_y(alignment::Vertical::Top)
.into()
}
}
Expand Down
32 changes: 26 additions & 6 deletions src/native/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! *This API requires the following crate features to be activated: modal*
use iced_widget::core::{
self, event,
self, alignment, event,
layout::{Limits, Node},
mouse::{self, Cursor},
overlay, renderer,
Expand Down Expand Up @@ -55,6 +55,8 @@ where
esc: Option<Message>,
/// The style of the [`ModalOverlay`](ModalOverlay).
style: <Renderer::Theme as StyleSheet>::Style,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
}

impl<'a, Message, Renderer> Modal<'a, Message, Renderer>
Expand All @@ -74,18 +76,20 @@ where
/// * the underlay [`Element`] on which this [`Modal`](Modal)
/// will be wrapped around.
/// * the content [`Element`] of the [`Modal`](Modal).
pub fn new<U, C>(show_modal: bool, underlay: U, content: C) -> Self
where
U: Into<Element<'a, Message, Renderer>>,
C: Into<Element<'a, Message, Renderer>>,
{
pub fn new(
show_modal: bool,
underlay: impl Into<Element<'a, Message, Renderer>>,
content: impl Into<Element<'a, Message, Renderer>>,
) -> Self {
Modal {
show_modal,
underlay: underlay.into(),
content: content.into(),
backdrop: None,
esc: None,
style: <Renderer::Theme as StyleSheet>::Style::default(),
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
}
}

Expand Down Expand Up @@ -113,6 +117,20 @@ where
self.style = style;
self
}

/// Sets the content alignment for the horizontal axis of the [`Modal`](Modal).
#[must_use]
pub fn align_x(mut self, alignment: alignment::Horizontal) -> Self {
self.horizontal_alignment = alignment;
self
}

/// Sets the content alignment for the vertical axis of the [`Modal`](Modal).
#[must_use]
pub fn align_y(mut self, alignment: alignment::Vertical) -> Self {
self.vertical_alignment = alignment;
self
}
}

impl<'a, Message, Renderer> Widget<Message, Renderer> for Modal<'a, Message, Renderer>
Expand Down Expand Up @@ -229,6 +247,8 @@ where
self.backdrop.clone(),
self.esc.clone(),
self.style,
self.horizontal_alignment,
self.vertical_alignment,
)),
))
} else {
Expand Down
31 changes: 14 additions & 17 deletions src/native/overlay/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//!
//! *This API requires the following crate features to be activated: modal*
use iced_widget::core::{
self, event, keyboard, layout,
self, alignment, event, keyboard, layout,
mouse::{self, Cursor},
renderer, touch,
widget::Tree,
Clipboard, Color, Element, Event, Layout, Overlay, Point, Rectangle, Shell, Size, Vector,
Alignment, Clipboard, Color, Element, Event, Layout, Overlay, Point, Rectangle, Shell, Size,
};

use crate::style::modal::StyleSheet;
Expand All @@ -29,6 +29,8 @@ where
esc: Option<Message>,
/// The style of the [`ModalOverlay`](ModalOverlay).
style: <Renderer::Theme as StyleSheet>::Style,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
}

impl<'a, 'b, Message, Renderer> ModalOverlay<'a, 'b, Message, Renderer>
Expand All @@ -44,13 +46,17 @@ where
backdrop: Option<Message>,
esc: Option<Message>,
style: <Renderer::Theme as StyleSheet>::Style,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
) -> Self {
ModalOverlay {
state,
content,
backdrop,
esc,
style,
horizontal_alignment,
vertical_alignment,
}
}
}
Expand All @@ -62,25 +68,16 @@ where
Renderer: core::Renderer,
Renderer::Theme: StyleSheet,
{
fn layout(&self, renderer: &Renderer, bounds: Size, position: Point) -> layout::Node {
fn layout(&self, renderer: &Renderer, bounds: Size, _position: Point) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, bounds);

let mut content = self.content.as_widget().layout(renderer, &limits);

// Center position
let max_size = limits.max();
let container_half_width = max_size.width / 2.0;
let container_half_height = max_size.height / 2.0;
let content_half_width = content.bounds().width / 2.0;
let content_half_height = content.bounds().height / 2.0;

let position = position
+ Vector::new(
container_half_width - content_half_width,
container_half_height - content_half_height,
);

content.move_to(position);
content.align(
Alignment::from(self.horizontal_alignment),
Alignment::from(self.vertical_alignment),
max_size,
);

layout::Node::with_children(max_size, vec![content])
}
Expand Down