Skip to content

Commit

Permalink
Update ScrollComponent to master & fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ForLoveOfCats committed Aug 27, 2020
2 parents 5f7c985 + 8cdb145 commit 78b7700
Show file tree
Hide file tree
Showing 55 changed files with 2,147 additions and 704 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Kaur Kuut
Leopold Luley
Andrey Kabylin
Garrett Risley
Robert Wittams
24 changes: 22 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ You can find its changes [documented below](#060---2020-06-01).

### Added
- `OPEN_PANEL_CANCELLED` and `SAVE_PANEL_CANCELLED` commands. ([#1061] by @cmyr)

- Export `Image` and `ImageData` by default. ([#1011] by [@covercash2])
- Re-export `druid_shell::Scalable` under `druid` namespace. ([#1075] by [@ForLoveOfCats])
- `TextBox` now supports ctrl and shift hotkeys. ([#1076] by [@vkahl])
- Added selection text color to textbox. ([#1093] by [@sysint64])
- `ScrollComponent` for ease of adding consistent, customized, scrolling behavior to a widget. ([#1107] by [@ForLoveOfCats])
- Selection text color to textbox. ([#1093] by [@sysint64])
- `BoxConstraints::UNBOUNDED` constant. ([#1126] by [@danieldulaney])
- Close requests from the shell can now be intercepted ([#1118] by [@jneem])
- The Lens derive now supports an `ignore` attribute. ([#1133] by [@jneem])
- `request_update` in `EventCtx`. ([#1128] by [@raphlinus])
- `ExtEventSink`s can now be obtained from widget methods. ([#1152] by [@jneem])
- 'Scope' widget to allow encapsulation of reactive state. ([#1151] by [@rjwittams])

### Changed

- `Scale::from_scale` to `Scale::new`, and `Scale` methods `scale_x` / `scale_y` to `x` / `y`. ([#1042] by [@xStrom])
- Major rework of keyboard event handling. ([#1049] by [@raphlinus])
- `Container::rounded` takes `KeyOrValue<f64>` instead of `f64`. ([#1054] by [@binomial0])
- `List` handles its own scrolling on either the vertical or horizontal axis. ([#1107] by [@ForLoveOfCats])
- `request_anim_frame` no longer invalidates the entire window. ([#1057] by [@jneem])
- Use new Piet text api ([#1143] by [@cmyr])

### Deprecated

Expand All @@ -47,7 +55,9 @@ You can find its changes [documented below](#060---2020-06-01).
- `ViewSwitcher` now skips the update after switching widgets. ([#1113] by [@finnerale])
- Key and KeyOrValue derive Clone ([#1119] by [@rjwittams])
- Allow submit_command from the layout method in Widgets ([#1119] by [@rjwittams])
- Allow derivation of lenses for generic types ([#1120]) by [@rjwittams])
- Allow derivation of lenses for generic types ([#1120]) by [@rjwittams])
- Switch widget: Toggle animation being window refresh rate dependent ([#1145] by [@ForLoveOfCats])
- Multi-click on Windows, partial fix for #859 ([#1157] by [@raphlinus])

### Visual

Expand Down Expand Up @@ -372,6 +382,7 @@ Last release without a changelog :(
[#1049]: https://github.com/linebender/druid/pull/1049
[#1050]: https://github.com/linebender/druid/pull/1050
[#1054]: https://github.com/linebender/druid/pull/1054
[#1057]: https://github.com/linebender/druid/pull/1057
[#1058]: https://github.com/linebender/druid/pull/1058
[#1061]: https://github.com/linebender/druid/pull/1061
[#1062]: https://github.com/linebender/druid/pull/1062
Expand All @@ -385,8 +396,17 @@ Last release without a changelog :(
[#1100]: https://github.com/linebender/druid/pull/1100
[#1103]: https://github.com/linebender/druid/pull/1103
[#1107]: https://github.com/linebender/druid/pull/1107
[#1118]: https://github.com/linebender/druid/pull/1118
[#1119]: https://github.com/linebender/druid/pull/1119
[#1120]: https://github.com/linebender/druid/pull/1120
[#1126]: https://github.com/linebender/druid/pull/1120
[#1128]: https://github.com/linebender/druid/pull/1128
[#1133]: https://github.com/linebender/druid/pull/1133
[#1143]: https://github.com/linebender/druid/pull/1143
[#1145]: https://github.com/linebender/druid/pull/1145
[#1151]: https://github.com/linebender/druid/pull/1151
[#1152]: https://github.com/linebender/druid/pull/1152
[#1157]: https://github.com/linebender/druid/pull/1157

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
37 changes: 36 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ All submissions, including submissions by project members, require review. We
use GitHub pull requests for this purpose. Consult [GitHub Help] for more
information on using pull requests.


## Before opening a PR

Testing a patch on github can take 15+ minutes, and you can save a lot of time by
testing locally. We recommend using the following [git `pre-push` hook], by
copying it to `druid/.git/hooks/pre-push`:

```sh
#!/bin/sh

set -e

echo "cargo fmt"
cargo fmt --all -- --check
echo "cargo clippy druid-shell"
cargo clippy --manifest-path=druid-shell/Cargo.toml --all-targets -- -D warnings
echo "cargo clippy druid"
cargo clippy --manifest-path=druid/Cargo.toml --all-targets --features=svg,image,im -- -D warnings
echo "cargo clippy druid (wasm)"
cargo clippy --manifest-path=druid/Cargo.toml --all-targets --features=image,im --target wasm32-unknown-unknown -- -D warnings
echo "cargo clippy druid-derive"
cargo clippy --manifest-path=druid-derive/Cargo.toml --all-targets -- -D warnings
echo "cargo clippy book examples"
cargo clippy --manifest-path=docs/book_examples/Cargo.toml --all-targets -- -D warnings
echo "cargo test druid-shell"
cargo test --manifest-path=druid-shell/Cargo.toml
echo "cargo test druid"
cargo test --manifest-path=druid/Cargo.toml --features=svg,image,im
echo "cargo test druid-derive"
cargo test --manifest-path=druid-derive/Cargo.toml
echo "cargo test book examples"
cargo test --manifest-path=docs/book_examples/Cargo.toml
```

# How to maintain

## Preparing for a new release
Expand Down Expand Up @@ -134,4 +168,5 @@ plus how and if it makes sense to update to the newer version.
[AUTHORS]: AUTHORS
[changelog]: CHANGELOG.md
[cargo-edit]: https://github.com/killercup/cargo-edit
[semver]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
[semver]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
[git `pre-push` hook]: https://githooks.com
6 changes: 3 additions & 3 deletions druid-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ rustdoc-args = ["--cfg", "docsrs"]
default-target = "x86_64-pc-windows-msvc"

[dependencies]
syn = "1.0.29"
quote = "1.0.6"
proc-macro2 = "1.0.17"
syn = "1.0.38"
quote = "1.0.7"
proc-macro2 = "1.0.19"

[dev-dependencies]
druid = { version = "0.6.0", path = "../druid" }
Expand Down
118 changes: 94 additions & 24 deletions druid-derive/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const LENS_NAME_OVERRIDE_ATTR_PATH: &str = "name";

/// The fields for a struct or an enum variant.
#[derive(Debug)]
pub struct Fields {
pub struct Fields<Attrs> {
pub kind: FieldKind,
fields: Vec<Field>,
fields: Vec<Field<Attrs>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -60,18 +60,45 @@ impl FieldIdent {
}

#[derive(Debug)]
pub struct Field {
pub struct Field<Attrs> {
pub ident: FieldIdent,
pub ty: syn::Type,

pub attrs: Attrs,
}

#[derive(Debug)]
pub struct DataAttrs {
/// `true` if this field should be ignored.
pub ignore: bool,
pub same_fn: Option<ExprPath>,
}

#[derive(Debug)]
pub struct LensAttrs {
/// `true` if this field should be ignored.
pub ignore: bool,
pub lens_name_override: Option<Ident>,
//TODO: more attrs here
}

impl Fields {
impl Fields<DataAttrs> {
pub fn parse_ast(fields: &syn::Fields) -> Result<Self, Error> {
let kind = match fields {
syn::Fields::Named(_) => FieldKind::Named,
syn::Fields::Unnamed(_) | syn::Fields::Unit => FieldKind::Unnamed,
};

let fields = fields
.iter()
.enumerate()
.map(|(i, field)| Field::<DataAttrs>::parse_ast(field, i))
.collect::<Result<Vec<_>, _>>()?;

Ok(Fields { kind, fields })
}
}

impl Fields<LensAttrs> {
pub fn parse_ast(fields: &syn::Fields) -> Result<Self, Error> {
let kind = match fields {
syn::Fields::Named(_) => FieldKind::Named,
Expand All @@ -81,22 +108,24 @@ impl Fields {
let fields = fields
.iter()
.enumerate()
.map(|(i, field)| Field::parse_ast(field, i))
.map(|(i, field)| Field::<LensAttrs>::parse_ast(field, i))
.collect::<Result<Vec<_>, _>>()?;

Ok(Fields { kind, fields })
}
}

impl<Attrs> Fields<Attrs> {
pub fn len(&self) -> usize {
self.fields.len()
}

pub fn iter(&self) -> impl Iterator<Item = &Field> {
pub fn iter(&self) -> impl Iterator<Item = &Field<Attrs>> {
self.fields.iter()
}
}

impl Field {
impl Field<DataAttrs> {
pub fn parse_ast(field: &syn::Field, index: usize) -> Result<Self, Error> {
let ident = match field.ident.as_ref() {
Some(ident) => FieldIdent::Named(ident.to_string().trim_start_matches("r#").to_owned()),
Expand All @@ -107,7 +136,6 @@ impl Field {

let mut ignore = false;
let mut same_fn = None;
let mut lens_name_override = None;

for attr in field.attrs.iter() {
if attr.path.is_ident(BASE_DRUID_DEPRECATED_ATTR_PATH) {
Expand Down Expand Up @@ -152,11 +180,61 @@ impl Field {
));
}
}
}
}
Ok(Field {
ident,
ty,
attrs: DataAttrs { ignore, same_fn },
})
}

/// The tokens to be used as the function for 'same'.
pub fn same_fn_path_tokens(&self) -> TokenStream {
match self.attrs.same_fn {
Some(ref f) => quote!(#f),
None => {
let span = Span::call_site();
quote_spanned!(span=> druid::Data::same)
}
}
}
}

impl Field<LensAttrs> {
pub fn parse_ast(field: &syn::Field, index: usize) -> Result<Self, Error> {
let ident = match field.ident.as_ref() {
Some(ident) => FieldIdent::Named(ident.to_string().trim_start_matches("r#").to_owned()),
None => FieldIdent::Unnamed(index),
};

let ty = field.ty.clone();

let mut ignore = false;
let mut lens_name_override = None;

for attr in field.attrs.iter() {
if attr.path.is_ident(BASE_DRUID_DEPRECATED_ATTR_PATH) {
panic!(
"The 'druid' attribute has been replaced with separate \
'lens' and 'data' attributes.",
);
} else if attr.path.is_ident(BASE_LENS_ATTR_PATH) {
match attr.parse_meta()? {
Meta::List(meta) => {
for nested in meta.nested.iter() {
match nested {
NestedMeta::Meta(Meta::Path(path))
if path.is_ident(IGNORE_ATTR_PATH) =>
{
if ignore {
return Err(Error::new(
nested.span(),
"Duplicate attribute",
));
}
ignore = true;
}
NestedMeta::Meta(Meta::NameValue(meta))
if meta.path.is_ident(LENS_NAME_OVERRIDE_ATTR_PATH) =>
{
Expand All @@ -174,7 +252,7 @@ impl Field {
other => {
return Err(Error::new(
other.span(),
"Expected attribute list (the form #[data(one, two)])",
"Expected attribute list (the form #[lens(one, two)])",
));
}
}
Expand All @@ -183,12 +261,15 @@ impl Field {
Ok(Field {
ident,
ty,
ignore,
same_fn,
lens_name_override,
attrs: LensAttrs {
ignore,
lens_name_override,
},
})
}
}

impl<Attrs> Field<Attrs> {
pub fn ident_tokens(&self) -> TokenTree {
match self.ident {
FieldIdent::Named(ref s) => Ident::new(&s, Span::call_site()).into(),
Expand All @@ -202,17 +283,6 @@ impl Field {
FieldIdent::Unnamed(num) => num.to_string(),
}
}

/// The tokens to be used as the function for 'same'.
pub fn same_fn_path_tokens(&self) -> TokenStream {
match self.same_fn {
Some(ref f) => quote!(#f),
None => {
let span = Span::call_site();
quote_spanned!(span=> druid::Data::same)
}
}
}
}

fn parse_lit_into_expr_path(lit: &syn::Lit) -> Result<ExprPath, Error> {
Expand Down
17 changes: 10 additions & 7 deletions druid-derive/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! The implementation for #[derive(Data)]
use crate::attr::{Field, FieldKind, Fields};
use crate::attr::{DataAttrs, Field, FieldKind, Fields};

use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, Data, DataEnum, DataStruct};
Expand All @@ -40,14 +40,17 @@ fn derive_struct(
let impl_generics = generics_bounds(&input.generics);
let (_, ty_generics, where_clause) = &input.generics.split_for_impl();

let fields = Fields::parse_ast(&s.fields)?;
let fields = Fields::<DataAttrs>::parse_ast(&s.fields)?;

let diff = if fields.len() > 0 {
let same_fns = fields
.iter()
.filter(|f| !f.ignore)
.filter(|f| !f.attrs.ignore)
.map(Field::same_fn_path_tokens);
let fields = fields.iter().filter(|f| !f.ignore).map(Field::ident_tokens);
let fields = fields
.iter()
.filter(|f| !f.attrs.ignore)
.map(Field::ident_tokens);
quote!( #( #same_fns(&self.#fields, &other.#fields) )&&* )
} else {
quote!(true)
Expand Down Expand Up @@ -97,13 +100,13 @@ fn derive_enum(
.variants
.iter()
.map(|variant| {
let fields = Fields::parse_ast(&variant.fields)?;
let fields = Fields::<DataAttrs>::parse_ast(&variant.fields)?;
let variant = &variant.ident;

// the various inner `same()` calls, to the right of the match arm.
let tests: Vec<_> = fields
.iter()
.filter(|field| !field.ignore)
.filter(|field| !field.attrs.ignore)
.map(|field| {
let same_fn = field.same_fn_path_tokens();
let var_left = ident_from_str(&format!("__self_{}", field.ident_string()));
Expand Down Expand Up @@ -145,7 +148,7 @@ fn derive_enum(
.map(|field| ident_from_str(&format!("__other_{}", field.ident_string())))
.collect();

if fields.iter().filter(|field| !field.ignore).count() > 0 {
if fields.iter().count() > 0 {
Ok(quote! {
( #ident :: #variant( #(#vars_left),* ), #ident :: #variant( #(#vars_right),* )) => {
#( #tests )&&*
Expand Down
Loading

0 comments on commit 78b7700

Please sign in to comment.