-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a3cc4b
commit 1a5c487
Showing
5 changed files
with
148 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,87 @@ | ||
use iced::button::State; | ||
use iced_lazy::Component; | ||
use iced::{ | ||
alignment, | ||
pure::{button, column, row, text}, | ||
Alignment, Length, | ||
}; | ||
use iced_lazy::pure::{self, Component}; | ||
use iced_native::text; | ||
use iced_pure::Element; | ||
use std::marker::PhantomData; | ||
|
||
pub struct Collapse<'a> { | ||
button: &'a mut State, | ||
collapsed: bool, | ||
pub fn collapse< | ||
'a, | ||
Message: 'a, | ||
T: Into<Message> + Clone + 'a, | ||
Renderer: text::Renderer + 'static, | ||
H: Fn() -> Element<'a, T, Renderer> + 'a, | ||
C: Fn() -> Element<'a, T, Renderer> + 'a, | ||
>( | ||
header: H, | ||
content: C, | ||
) -> impl Into<Element<'a, Message, Renderer>> { | ||
Collapse { | ||
header, | ||
content, | ||
phantom: PhantomData, | ||
} | ||
} | ||
|
||
struct Collapse<'a, H, C> { | ||
header: H, | ||
content: C, | ||
phantom: PhantomData<&'a H>, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
enum Event<T> { | ||
Internal(T), | ||
Collapse(bool), | ||
} | ||
|
||
impl<'a, Message, Renderer, T, H, C> Component<Message, Renderer> for Collapse<'a, H, C> | ||
where | ||
T: Into<Message> + Clone + 'a, | ||
H: Fn() -> Element<'a, T, Renderer>, | ||
C: Fn() -> Element<'a, T, Renderer>, | ||
Renderer: text::Renderer + 'static, | ||
{ | ||
type State = bool; | ||
type Event = Event<T>; | ||
|
||
fn update(&mut self, state: &mut Self::State, event: Event<T>) -> Option<Message> { | ||
match event { | ||
Event::Internal(e) => Some(e.into()), | ||
Event::Collapse(s) => { | ||
*state = s; | ||
None | ||
} | ||
} | ||
} | ||
|
||
fn view(&self, state: &Self::State) -> Element<Self::Event, Renderer> { | ||
if *state { | ||
column() | ||
.push(button((self.header)().map(Event::Internal)).on_press(Event::Collapse(false))) | ||
.push((self.content)().map(Event::Internal)) | ||
.into() | ||
} else { | ||
column() | ||
.push(button((self.header)().map(Event::Internal)).on_press(Event::Collapse(true))) | ||
.into() | ||
} | ||
} | ||
} | ||
|
||
impl<'a, Message, Renderer, T, H: 'a, C: 'a> From<Collapse<'a, H, C>> | ||
for Element<'a, Message, Renderer> | ||
where | ||
Message: 'a, | ||
Renderer: 'static + text::Renderer, | ||
T: Into<Message> + Clone + 'a, | ||
H: Fn() -> Element<'a, T, Renderer>, | ||
C: Fn() -> Element<'a, T, Renderer>, | ||
{ | ||
fn from(c: Collapse<'a, H, C>) -> Self { | ||
pure::component(c).into() | ||
} | ||
} |