Skip to content

Commit

Permalink
feat: make cards optionally pressable, and un-expandable or un-shrink…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
wash2 committed Dec 6, 2024
1 parent 442410a commit 706586b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
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 @@ impl Id {

/// 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 @@ impl Id {
) -> 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 @@ impl Id {
card_inner_elements,
on_clear_all,
on_show_more,
on_activate,
show_more_label,
show_less_label,
clear_all_label,
Expand Down Expand Up @@ -203,12 +207,13 @@ impl Cards {
}

#[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 @@ impl Cards {
) -> 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 @@ const BG_CARD_MARGIN_STEP: f32 = 8.0;

/// 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 @@ pub fn cards<'a, Message, F>(
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 @@ -54,11 +57,15 @@ 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 @@ where
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 @@ where
{
/// 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 @@ where
) -> 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 @@ where
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 @@ where
.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 @@ where
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 @@ where
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 @@ where
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

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, stable)

value assigned to `n` is never read

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

View workflow job for this annotation

GitHub Actions / all

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, beta)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, stable)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, beta)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, stable)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, beta)

value assigned to `n` is never read

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

View workflow job for this annotation

GitHub Actions / all

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, stable)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, beta)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, stable)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, beta)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, stable)

value assigned to `n` is never read

Check warning on line 293 in src/widget/cards.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, beta)

value assigned to `n` is never read
.as_widget()
.layout(&mut tree_children[1], renderer, limits);
let clear_all_node_size = clear_all_node.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

0 comments on commit 706586b

Please sign in to comment.