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

Cards update #29

Merged
merged 2 commits into from
Dec 6, 2024
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
16 changes: 12 additions & 4 deletions src/keyframes/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@

/// Used by [`crate::anim!`] macro
#[allow(clippy::too_many_arguments)]
pub fn as_widget<'a, Message, F>(
pub fn as_widget<'a, Message, F, G>(
self,
timeline: &crate::Timeline,
card_inner_elements: Vec<Element<'a, Message>>,
on_clear_all: Message,
on_show_more: F,
on_show_more: Option<F>,
on_activate: Option<G>,
show_more_label: &'a str,
show_less_label: &'a str,
clear_all_label: &'a str,
Expand All @@ -53,6 +54,8 @@
) -> crate::widget::Cards<'a, Message, cosmic::Renderer>
where
F: 'a + Fn(Chain, bool) -> Message,
G: 'a + Fn(usize) -> Message,

Message: 'static + Clone,
{
Cards::as_widget(
Expand All @@ -61,6 +64,7 @@
card_inner_elements,
on_clear_all,
on_show_more,
on_activate,
show_more_label,
show_less_label,
clear_all_label,
Expand All @@ -85,9 +89,9 @@
}

impl Chain {
/// Crate a new [`Cards`] animation chain.

Check warning on line 92 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

public documentation for `new` links to private item `Cards`

Check warning on line 92 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

public documentation for `new` links to private item `Cards`
/// You probably don't want to use use directly, and should
/// use the [`chain`] macro.

Check warning on line 94 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

`chain` is both a module and a macro

Check warning on line 94 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

`chain` is both a module and a macro
#[must_use]
pub fn new(id: Id) -> Self {
Chain {
Expand All @@ -99,7 +103,7 @@

/// Create a chain pre-fulled with children.
/// You probably don't want to use use directly, and should
/// use the [`chain`] macro.

Check warning on line 106 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

`chain` is both a module and a macro

Check warning on line 106 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

`chain` is both a module and a macro
#[must_use]
pub fn with_children(id: Id, children: Vec<Cards>) -> Self {
Chain {
Expand All @@ -111,7 +115,7 @@

/// Link another keyframe, (very similar to push)
/// You probably don't want to use use directly, and should
/// use the [`chain`] macro.

Check warning on line 118 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

`chain` is both a module and a macro

Check warning on line 118 in src/keyframes/cards.rs

View workflow job for this annotation

GitHub Actions / all

`chain` is both a module and a macro
#[must_use]
pub fn link(mut self, toggler: Cards) -> Self {
self.links.push(toggler);
Expand Down Expand Up @@ -203,12 +207,13 @@
}

#[allow(clippy::too_many_arguments)]
pub fn as_widget<'a, Message, F>(
pub fn as_widget<'a, Message, F, G>(
id: Id,
timeline: &crate::Timeline,
card_inner_elements: Vec<Element<'a, Message>>,
on_clear_all: Message,
on_show_more: F,
on_show_more: Option<F>,
on_activate: Option<G>,
show_more_label: &'a str,
show_less_label: &'a str,
clear_all_label: &'a str,
Expand All @@ -217,13 +222,16 @@
) -> crate::widget::Cards<'a, Message, cosmic::Renderer>
where
F: 'a + Fn(Chain, bool) -> Message,
G: 'a + Fn(usize) -> Message,

Message: Clone + 'static,
{
crate::widget::Cards::new(
id.clone(),
card_inner_elements,
on_clear_all,
on_show_more,
on_activate,
show_more_label,
show_less_label,
clear_all_label,
Expand Down
71 changes: 45 additions & 26 deletions src/widget/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

/// get an expandable stack of cards
#[allow(clippy::too_many_arguments)]
pub fn cards<'a, Message, F>(
pub fn cards<'a, Message, F, G>(
id: id::Cards,
card_inner_elements: Vec<Element<'a, Message, cosmic::Theme, cosmic::Renderer>>,
on_clear_all: Message,
on_show_more: F,
on_show_more: Option<F>,
on_activate: Option<G>,
show_more_label: &'a str,
show_less_label: &'a str,
clear_all_label: &'a str,
Expand All @@ -35,12 +36,14 @@
where
Message: 'static + Clone,
F: 'a + Fn(chain::Cards, bool) -> Message,
G: 'a + Fn(usize) -> Message,
{
Cards::new(
id,
card_inner_elements,
on_clear_all,
on_show_more,
on_activate,
show_more_label,
show_less_label,
clear_all_label,
Expand All @@ -49,16 +52,20 @@
)
}

impl<'a, Message, Renderer> Cards<'a, Message, Renderer>

Check failure on line 55 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / all

the following explicit lifetimes could be elided: 'a

Check failure on line 55 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / all

the following explicit lifetimes could be elided: 'a
where
Renderer: iced_core::text::Renderer,
{
fn fully_expanded(&self) -> bool {
self.expanded && self.elements.len() > 1 && approx_eq!(f32, self.percent, 1.0)
self.expanded
&& self.elements.len() > 1
&& self.can_show_more
&& approx_eq!(f32, self.percent, 1.0)
}

fn fully_unexpanded(&self) -> bool {
self.elements.len() == 1 || (!self.expanded && approx_eq!(f32, self.percent, 0.0))
self.elements.len() == 1
|| (!self.expanded && (!self.can_show_more || approx_eq!(f32, self.percent, 0.0)))
}
}

Expand All @@ -73,6 +80,7 @@
clear_all_button: Element<'a, Message, cosmic::Theme, Renderer>,
elements: Vec<Element<'a, Message, cosmic::Theme, Renderer>>,
expanded: bool,
can_show_more: bool,
width: Length,
percent: f32,
anim_multiplier: f32,
Expand All @@ -84,11 +92,12 @@
{
/// Get an expandable stack of cards
#[allow(clippy::too_many_arguments)]
pub fn new<F>(
pub fn new<F, G>(
id: id::Cards,
card_inner_elements: Vec<Element<'a, Message, cosmic::Theme, cosmic::Renderer>>,
on_clear_all: Message,
on_show_more: F,
on_show_more: Option<F>,
on_activate: Option<G>,
show_more_label: &'a str,
show_less_label: &'a str,
clear_all_label: &'a str,
Expand All @@ -97,9 +106,12 @@
) -> Self
where
F: 'a + Fn(chain::Cards, bool) -> Message,
G: 'a + Fn(usize) -> Message,
{
let can_show_more = card_inner_elements.len() > 1;
let can_show_more = card_inner_elements.len() > 1 && on_show_more.is_some();

Self {
can_show_more,
_id: Id::unique(),
show_less_button: {
let mut show_less_children = Vec::with_capacity(3);
Expand All @@ -124,7 +136,7 @@
button::custom(button_content)
.class(cosmic::theme::Button::Text)
.width(Length::Shrink)
.on_press(on_show_more(off_animation, false))
.on_press_maybe(on_show_more.as_ref().map(|f| f(off_animation, false)))
.padding([PADDING / 2, PADDING]),
)
},
Expand Down Expand Up @@ -155,9 +167,9 @@
.padding(PADDING);
if i == 0 && !expanded && can_show_more {
let on_animation = chain::Cards::on(id.clone(), 1.0);
b.on_press(on_show_more(on_animation, true))
b.on_press_maybe(on_show_more.as_ref().map(|f| f(on_animation, true)))
} else {
b
b.on_press_maybe(on_activate.as_ref().map(|f| f(i)))
}
.into()
})
Expand Down Expand Up @@ -239,7 +251,7 @@
let mut size = Size::new(0.0, 0.0);
let tree_children = &mut tree.children;
if self.elements.is_empty() {
return Node::with_children(size, children);
return Node::with_children(Size::new(1., 1.), children);
}

let fully_expanded: bool = self.fully_expanded();
Expand All @@ -248,9 +260,13 @@
let show_less = &self.show_less_button;
let clear_all = &self.clear_all_button;

let show_less_node = show_less
.as_widget()
.layout(&mut tree_children[0], renderer, limits);
let show_less_node = if self.can_show_more {
show_less
.as_widget()
.layout(&mut tree_children[0], renderer, limits)
} else {
Node::new(Size::default())
};
let clear_all_node = clear_all
.as_widget()
.layout(&mut tree_children[1], renderer, limits);
Expand All @@ -266,23 +282,26 @@
let show_less = &self.show_less_button;
let clear_all = &self.clear_all_button;

let show_less_node =
let show_less_node = if self.can_show_more {
show_less
.as_widget()
.layout(&mut tree_children[0], renderer, limits);
let mut clear_all_node =
clear_all
.layout(&mut tree_children[0], renderer, limits)
} else {
Node::new(Size::default())
};
let clear_all_node = if self.can_show_more {
let mut n = clear_all
.as_widget()
.layout(&mut tree_children[1], renderer, limits);
let clear_all_node_size = n.size();
n = clear_all_node
.translate(Vector::new(size.width - clear_all_node_size.width, 0.0));
size.height += show_less_node.size().height.max(n.size().height) + VERTICAL_SPACING;
n
} else {
Node::new(Size::default())
};

let clear_all_node_size = clear_all_node.size();
clear_all_node =
clear_all_node.translate(Vector::new(size.width - clear_all_node_size.width, 0.0));
size.height += show_less_node
.size()
.height
.max(clear_all_node.size().height)
+ VERTICAL_SPACING;
children.push(show_less_node);
children.push(clear_all_node);
}
Expand Down
Loading