-
Notifications
You must be signed in to change notification settings - Fork 121
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
Experimental composable widgets for styling #14
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use piet_scene::Color; | ||
|
||
use crate::{ | ||
id::Id, | ||
widget::{ | ||
compose_style::{background::BackgroundWidget, padding::PaddingWidget}, | ||
Pod, | ||
}, | ||
View, | ||
}; | ||
|
||
pub fn padding<V: View<T, A>, T, A>(width: f64, view: V) -> impl View<T, A> | ||
where | ||
V::Element: 'static, | ||
{ | ||
PaddingView::new(width, view) | ||
} | ||
|
||
pub struct PaddingView<V> { | ||
width: f64, | ||
view: V, | ||
} | ||
|
||
impl<T, A, V: View<T, A>> View<T, A> for PaddingView<V> | ||
where | ||
V::Element: 'static, | ||
{ | ||
type State = (Id, V::State); | ||
|
||
type Element = PaddingWidget; | ||
|
||
fn build(&self, cx: &mut crate::view::Cx) -> (crate::id::Id, Self::State, Self::Element) { | ||
let (id, (child_id, state, element)) = cx.with_new_id(|cx| self.view.build(cx)); | ||
let element = PaddingWidget::new(Pod::new(element), self.width); | ||
(id, (child_id, state), element) | ||
} | ||
|
||
fn rebuild( | ||
&self, | ||
cx: &mut crate::view::Cx, | ||
prev: &Self, | ||
id: &mut crate::id::Id, | ||
(child_id, state): &mut Self::State, | ||
element: &mut Self::Element, | ||
) -> bool { | ||
let mut changed = prev.width != self.width; | ||
if changed { | ||
element.set_width(self.width); | ||
} | ||
cx.with_id(*id, |cx| { | ||
let child_element = element.widget.downcast_mut().unwrap(); | ||
let child_changed = self | ||
.view | ||
.rebuild(cx, &prev.view, child_id, state, child_element); | ||
if child_changed { | ||
changed = true; | ||
element.widget.request_update(); | ||
} | ||
}); | ||
changed | ||
} | ||
|
||
fn event( | ||
&self, | ||
id_path: &[crate::id::Id], | ||
(child_id, state): &mut Self::State, | ||
event: Box<dyn std::any::Any>, | ||
app_state: &mut T, | ||
) -> crate::event::EventResult<A> { | ||
let (left, right) = id_path.split_at(1); | ||
assert!(left[0] == *child_id); | ||
self.view.event(right, state, event, app_state) | ||
} | ||
} | ||
|
||
impl<V> PaddingView<V> { | ||
fn new(width: f64, view: V) -> Self { | ||
PaddingView { width, view } | ||
} | ||
} | ||
|
||
pub fn background<V: View<T, A>, T, A>(color: Color, view: V) -> impl View<T, A> | ||
where | ||
V::Element: 'static, | ||
{ | ||
BackgroundView::new(color, view) | ||
} | ||
|
||
pub struct BackgroundView<V> { | ||
color: Color, | ||
view: V, | ||
} | ||
|
||
impl<T, A, V: View<T, A>> View<T, A> for BackgroundView<V> | ||
where | ||
V::Element: 'static, | ||
{ | ||
type State = (Id, V::State); | ||
|
||
type Element = BackgroundWidget; | ||
|
||
fn build(&self, cx: &mut crate::view::Cx) -> (crate::id::Id, Self::State, Self::Element) { | ||
let (id, (child_id, state, element)) = cx.with_new_id(|cx| self.view.build(cx)); | ||
let element = BackgroundWidget::new(Pod::new(element), self.color); | ||
(id, (child_id, state), element) | ||
} | ||
|
||
fn rebuild( | ||
&self, | ||
cx: &mut crate::view::Cx, | ||
prev: &Self, | ||
id: &mut crate::id::Id, | ||
(child_id, state): &mut Self::State, | ||
element: &mut Self::Element, | ||
) -> bool { | ||
let mut changed = prev.color != self.color; | ||
if changed { | ||
element.set_color(self.color); | ||
} | ||
cx.with_id(*id, |cx| { | ||
let child_element = element.widget.downcast_mut().unwrap(); | ||
let child_changed = self | ||
.view | ||
.rebuild(cx, &prev.view, child_id, state, child_element); | ||
if child_changed { | ||
changed = true; | ||
element.widget.request_update(); | ||
} | ||
}); | ||
changed | ||
} | ||
|
||
fn event( | ||
&self, | ||
id_path: &[crate::id::Id], | ||
(child_id, state): &mut Self::State, | ||
event: Box<dyn std::any::Any>, | ||
app_state: &mut T, | ||
) -> crate::event::EventResult<A> { | ||
let (left, right) = id_path.split_at(1); | ||
assert!(left[0] == *child_id); | ||
self.view.event(right, state, event, app_state) | ||
} | ||
} | ||
|
||
impl<V> BackgroundView<V> { | ||
fn new(color: Color, view: V) -> Self { | ||
BackgroundView { color, view } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod background; | ||
pub mod padding; |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use glazier::kurbo::Affine; | ||
use piet_scene::{Color, Fill}; | ||
|
||
use crate::{id::Id, widget::Pod, View, Widget}; | ||
|
||
pub struct BackgroundWidget { | ||
pub(crate) widget: Pod, | ||
color: Color, | ||
} | ||
|
||
impl BackgroundWidget { | ||
pub fn new(widget: Pod, color: Color) -> Self { | ||
Self { widget, color } | ||
} | ||
pub(crate) fn set_color(&mut self, color: Color) { | ||
self.color = color; | ||
} | ||
} | ||
|
||
impl Widget for BackgroundWidget { | ||
fn event(&mut self, cx: &mut crate::widget::EventCx, event: &crate::widget::RawEvent) { | ||
self.widget.event(cx, event) | ||
} | ||
|
||
fn lifecycle( | ||
&mut self, | ||
cx: &mut crate::widget::contexts::LifeCycleCx, | ||
event: &crate::widget::LifeCycle, | ||
) { | ||
self.widget.lifecycle(cx, event) | ||
} | ||
|
||
fn update(&mut self, cx: &mut crate::widget::UpdateCx) { | ||
self.widget.update(cx) | ||
} | ||
|
||
fn measure( | ||
&mut self, | ||
cx: &mut crate::widget::LayoutCx, | ||
) -> (glazier::kurbo::Size, glazier::kurbo::Size) { | ||
self.widget.measure(cx) | ||
} | ||
|
||
fn layout( | ||
&mut self, | ||
cx: &mut crate::widget::LayoutCx, | ||
proposed_size: glazier::kurbo::Size, | ||
) -> glazier::kurbo::Size { | ||
self.widget.layout(cx, proposed_size) | ||
} | ||
|
||
fn paint(&mut self, cx: &mut crate::widget::PaintCx, builder: &mut piet_scene::SceneBuilder) { | ||
self.widget.paint(cx); | ||
let fragment = self.widget.fragment(); | ||
builder.fill( | ||
Fill::NonZero, | ||
Affine::IDENTITY, | ||
self.color, | ||
None, | ||
&cx.size().to_rect(), | ||
); | ||
builder.append(fragment, None) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an implicit decision about naming convention, that I really like;
It would be grammatically more obvious to name this
PaddedView
.But I think this way it's way nicer, and we should adopt this as a naming convention.
E.g.
BorderView
instead ofBorderedView
etc.This way it's a little easier to navigate the stuff xilem has to offer, even if your english is good. But more than anything, we make the library a little more accessible to non native speakers, who struggle with the language barrier