Commit Graph

42 Commits

Author SHA1 Message Date
Olivier FAURE 25218291d0
Reserve ability to create Pod to ViewCtx (#597)
This is another intermediary PR for the "restrict widget creation to
Mutate pass" feature.

The basic idea is that, in the near future, it will be impossible to
create a WidgetPod without a handle to the arena. In my current WIP
code, that handle is passed through ViewCtx. Therefore, this PR changes
all the sites in xilem that create a Pod and has them use a ViewCtx
method instead.

I've tested most xilem examples and they all worked (except for
variable_clock, which currently panics in the last main commit).
2024-09-18 20:53:31 +00:00
Daniel McNab 0d56c592f5
Add the `lens` component (#587)
See
https://xi.zulipchat.com/#narrow/stream/354396-xilem/topic/Lens.20View

Usage:
```rust
fn app_logic(state: &mut FlightPlanner) -> impl WidgetView<FlightPlanner> {
    lens(date_picker, state, |state| &mut state.date)
}

struct FlightPlanner {
    date: Date,
    available_flights: Vec<Flight>,
}
```

Also extends the docs features in Xilem Core, and increases the
complexity threshold

---------

Co-authored-by: Kaur Kuut <strom@nevermore.ee>
Co-authored-by: Philipp Mildenberger <philipp@mildenberger.me>
2024-09-18 08:10:50 +00:00
Philipp Mildenberger 2ccd9d4712
xilem_web: Optimize element casting, and intern often used strings (e.g. "div") (#594)
Some micro-optimizations, e.g. avoids a js call `instanceof` (via
`dyn_into`/`dyn_ref`) when constructing elements.

Leads to a small speed increase when constructing/visiting elements
(roughly 2%) and more importantly a leaner wasm binary (around 5 %),
respectively tested with the js-framework-benchmark suite.
2024-09-17 18:49:15 +00:00
Bruce Mitchener 0914079d0c
xilem: Remove lingering `cx` (use `ctx`) (#547) 2024-08-25 09:41:10 +00:00
Andrii Zymohliad f90772a0d2
Rename async_repeat into task (#528)
Following [the discussion in
Zulip](https://xi.zulipchat.com/#narrow/stream/354396-xilem/topic/.60async_repeat.60.20name.20brainstorming),
this PR renames `async_repeat` view into `task`, and its `future_future`
argument into `init_future`. I also assumed we would want to do a
corresponding rename in `xilem_web` for consistency.
2024-08-20 08:50:47 +00:00
Philipp Mildenberger 88cc3eed3d
xilem_web: Apply props when the element type changes in `replace_inner` (#519)
Fixes #518
2024-08-16 10:42:33 +00:00
Philipp Mildenberger 5ae36200b3
xilem_web: Document a lot and remove `start_<modifier>` as it doesn't do anything in `View::build` and may be misleading (#517)
I think I've covered every pub item at the top level.

It also renames (taking `Attributes` as example)
`start_attribute_modifier` -> `rebuild_attribute_modifier`,
and `end_attribute_modifier` -> `mark_end_of_attribute_modifier`, as I
think that makes it more clear what these methods do.
`start_attribute_modifier` was a noop in `View::build` and may lead to
buggy behavior when `DomNode::apply_props` is called before every parent
`View::build` has done modifying the props. This should make
`DomNode::apply_props` robust. And `AfterBuild` is fixed now with that.
2024-08-16 09:51:33 +00:00
Philipp Mildenberger 411acc12aa
xilem_web: Add hydration support, and a new view using it `Templated` (#495)
This is mostly a first step towards SSR.
But we can use hydration to speed up creation of long (non-virtualized)
lists with the `Templated` view.
`Templated` stores a deep copy of the DOM node of the first occurence of
its `impl DomView` based on the `TypeId` in the `ViewCtx`,
and reuses it on every further invocation, where it will be hydrated and
updated based on the new attributes.
As it uses an `Rc` to achieve this, it also supports memoization,
similar to an `Arc<impl View>`.

Hydration is currently feature-gated, as it produces a little bit more
binary bloat. Though it's little enough to this being justified as
default.

---------

Co-authored-by: Markus Kohlhase <markus.kohlhase@slowtec.de>
2024-08-15 15:37:57 +00:00
Markus Kohlhase 7d137a765d
xilem_web: Add event_target_value helper function (#506) 2024-08-12 11:27:30 +00:00
Philipp Mildenberger c9dbb211a8
xilem_web: Fix issue with downcasting to a wrong type transitively from `AnyView` (#505)
This should fix the issue described in #440 by adding the type of the
`Children` `ViewSequence` to the type of element, which results in
unique types, so `AnyView` should not cause issues anymore. Fortunately
there's no regression with the compilation times here, so an
`AnyViewSequence` doesn't seem to be necessary in that case.
2024-08-12 08:38:11 +00:00
Markus Kohlhase b7f98babd6
xilem_web: Make MessageThunk public (#504) 2024-08-11 00:09:47 +00:00
Philipp Mildenberger 8ecdd876dc
Fix doc links by either correcting the item name, or making these public (#499)
I don't think anything speaks against making the items public in
masonry?
2024-08-09 16:11:02 +00:00
Philipp Mildenberger a52c3c7b3c
xilem_web: Add support for opacity in `svg::Fill` and dashes, opacity in `svg::Stroke` (#493)
Uses `ViewState` for a little bit of optimization (of allocations
mostly).
2024-08-09 09:28:52 +00:00
Philipp Mildenberger 2f23ee117a
xilem_web: Change `events::OnResize` to be a `ResizeObserver` (#494)
Since
[`on_resize`](https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event)
on an `Element` is not really useful as it won't be called, we can reuse
this event listener with a
[`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver)
on itself, to be actually useful.
2024-08-09 08:40:24 +00:00
Markus Kohlhase 5b6ebc324f
xilem_web: Add an `AfterBuild`, `AfterRebuild` and `BeforeTeardown` view, which reflects `Ref` in React (#481)
Co-authored-by: Philipp Mildenberger <philipp@mildenberger.me>
2024-08-08 22:44:49 +00:00
Markus Kohlhase 2eda5a2a50
xilem_web: Add async_repeat (#485)
Co-authored-by: Philipp Mildenberger <philipp@mildenberger.me>
2024-08-08 17:51:04 +00:00
Philipp Mildenberger 99d61603d2
xilem_web: Add `interval` `View`, which somewhat reflects `setInterval` (#486)
Simple but convenient `View`, e.g. for some kind of ticker. (Maybe needs
an additional example)
2024-08-05 13:36:18 +00:00
Philipp Mildenberger bb13f1a760
xilem_web: Allow `DomFragment` instead of `DomView` as `app_logic` (#482)
Should fix #461. This allows a `ViewSequence` (called `DomFragment`) of
`DomView`s as root component.

The `counter` example is updated to show this new behavior.
2024-08-05 13:03:19 +00:00
Philipp Mildenberger 19454e3dc8
Improve `calc` and xilem_web's `mathml_svg` example (#487)
Reduces a little bit of the boilerplate in `calc`
2024-08-05 12:54:22 +00:00
Philipp Mildenberger 24427bbb44
Use `ViewMarker` trait instead of the generic `Marker` to allow `impl ViewSequence` as return type (#472)
This allows returning `impl ViewSequence` as this was previously not
possibly as the `Marker` generic parameter couldn't be named (easily) in
`ViewSequence<..., Marker>`.

This also provides specialized `ViewSequence`s for xilem
(`WidgetViewSequence` and `FlexSequence`) as well as for xilem_web
(`DomFragment`). Additional doc-tests/documentation and a small example
(in `counter`) for xilem_web is provided as well.

This has the drawback to not being able to reeimplement `ViewSequence`
for types that already implement `View`, which was previously possible
(as seen by the now removed `NoElementView` `ViewSequence`
implementation).
And additionally by introducing more boilerplate by having to implement
`ViewMarker` for every type that implements `View`.

---------

Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
2024-08-05 10:53:19 +00:00
Philipp Mildenberger 544a4a1ca9
xilem_web: Fix `DomChildrenSplice::with_scratch` (#484)
I'm surprised that this was not noticed yet,
`DomChildrenSplice::with_scratch` previously only appended new elements
to the end of the children of an element in the DOM tree, whereas it
should've inserted it at the current index of the `ElementSplice`. This
should fix this, using a `DocumentFragment` which is shared on the
`ViewCtx` to avoid unnecessary allocations.
2024-08-05 09:08:34 +00:00
Bruce Mitchener dd938d4af4
Fix doc warnings in xilem_core, xilem_web (#478) 2024-08-02 06:50:10 +00:00
Casey Rodarmor 4c991dd3e9
xilem_web: Add HtmlLiElement `value` attribute setter (#471)
From manually testing in Chrome, values between `2**31` and `2**31 - 1`
work, so this takes an `i32`.

It's pretty trivial code, but I think it would be really nice to add
some kind of test, or even just an example which uses it.

What do you think about adding a "misc" example for `xilem_web`, which
exercises a bunch of small features? I was thinking a layout with a
column on the left with a list of items with a larger area on the right.
When an item on the left is clicked, the right side is populated with
whatever that item is.

In this case, the item name would be `ol-li-value`, which when clicked
would show something like this on the right:

```rust
ol((
  li("foo"),
  li("bar"),
  li("baz").value(10),
  li("qux"),
));
```

Ideally, this would be compiled as part of CI, to make sure that no
compilation errors are introduced. Any time a small feature is added, a
new item in the "misc" example could be added which uses it.

It would also be nice for development, to have a place where new, small
features like this can be exercised during development. An actual test
would be great, but from quick googling, I couldn't find an obvious way
to test WASM code which runs in the browser. There's
`wasm_bindgen_test`, but that only runs tests in WASM outside of a
browser environment.
2024-08-01 21:05:09 +00:00
Casey Rodarmor 741c36b13a
xilem_web: Add function to set HtmlIFrameElement src attribute (#466)
Apropos of #462. Let me know if this should have a test or an example!
2024-07-31 08:01:56 +00:00
Philipp Mildenberger b71f533571
xilem_web: Fix #465, by actually replacing the old element in the DOM-tree (#468)
Fixes #465
2024-07-29 12:50:10 +00:00
Philipp Mildenberger 1b1a1d3238
xilem_web: Fix message type of `AnyDomView` and add `.boxed()` combinator (#460)
I guess I missed this when adding #408, the doc-test should ensure that
`Box<AnyDomView>` is now really a `DomView`.

---------

Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
2024-07-29 09:23:31 +00:00
Philipp Mildenberger a2f136079c
xilem_web: Remove hardcoded `prevent_default`/`stop_propagation` as the user should decide that behavior (#458)
I think we could also create some additional sugar around that, but
since the event is given to the user, they can handle that behavior
anyway.

Fixes #457
2024-07-29 09:15:21 +00:00
Markus Kohlhase 727d696488
xilem_web: Use separate FetchState in fetch example (#454) 2024-07-28 10:30:26 +00:00
Philipp Mildenberger 981fcc4b5a
xilem_web: Add a `MemoizedAwait` view (#448)
This is the `rerun_on_change` view described in #440, I named it
`MemoizedAwait` as I think that fits its functionality.

It got also additional features `debounce_ms` (default `0`) and
`reset_debounce_on_update` (default `true`) as I think that's quite
useful, in case a lot of updates are happening.

When `reset_debounce_on_update == false`, `debounce` is more a throttle
than an actual debounce.

This also adds a more heavily modified version of the example added in
#427, also showing `OneOf` in xilem_web (which didn't have an example
yet).

This *could* be considered as alternative to #440 (at the very least the
example) but those approaches could also well live next to each other.

---------

Co-authored-by: Markus Kohlhase <markus.kohlhase@slowtec.de>
2024-07-26 16:11:34 +00:00
Daniel McNab d70076262d
Update dependencies and bump to Rust 1.80 (#450)
There is one "semi-false-positive" lint triggered, which I have fixed.
Otherwise, the required
```
cargo upgrade --ignore-rust-version
cargo update
```
2024-07-26 13:02:41 +00:00
Daniel McNab 5e07dcebad
Support `OneOf` in Xilem (#445)
This uses the interface in Xilem Core from #394 and #436

Some thoughts:
1) You only need to specify the number of items in a single match arm.
I.e.
    ```rust
    fn my_view(){
        match x {
            0 => OneOf3(...),
            1=> OneOf9(...),
            _=> OneOf9(...),
        }
    }
    ```
    works. We probably should rename `OneOf9` back again in that case.
2) The example currently isn't that interesting. Any suggestions for a
more suitable state machine would be welcome.
2024-07-24 09:12:04 +00:00
Philipp Mildenberger c53d15472c
xilem_web: Support `Fill` for `SvgPathElement` instead of `SvgmPathElement` (#447)
Fixes #446 

I guess I missed the m before the path
2024-07-24 09:02:26 +00:00
Daniel McNab 5f38922d1b
Correctly set `rust-version` in all `Cargo.toml`s (#438)
Discovered in #432; this would otherwise cause errors for users of
nightly (or 1.81 beta, due to be released next week)
2024-07-19 14:42:27 +00:00
Daniel McNab 0844e3c431
Change the `OneOf` views to not use macros (#436)
This still needs to be implemented in Xilem.

But I think this is an improvement overall
2024-07-19 14:14:30 +00:00
Daniel McNab f11b64892c
Import our intended set of lints (#432)
See discussion in [#linebender > Standard Lint
set](https://xi.zulipchat.com/#narrow/stream/419691-linebender/topic/Standard.20Lint.20set)

Of note: I have taken steps to ensure that this can be practically
reviewed by *not* applying most of the lints.
The commented out lints make good follow-ups

---------

Co-authored-by: Olivier FAURE <couteaubleu@gmail.com>
2024-07-18 08:58:16 +00:00
Markus Kohlhase 62c06c9f98
xilem_web: Add Elm Architecture example (#425)
I have intentionally left out the `xilem_core::adapt` variant here in
order to have a clear classic Elm example. I would rather show the
`adapt` variant in a separate example.
2024-07-12 11:39:37 +00:00
Daniel McNab df85194c88
Fix the `head` elements of Xilem Web (#422)
These got mixed up in https://github.com/linebender/xilem/pull/142
2024-07-02 10:07:13 +00:00
Daniel McNab 422c0da5be
Use the cleaner format for READMEs (#415) 2024-06-28 10:46:35 +00:00
Philipp Mildenberger b33a2a680d
Rewrite xilem_web to support new xilem_core (#403)
This ports xilem_web to the new xilem_core.

There's also a lot of cleanup internally:
* Get rid of all of the complex macros to support DOM interfaces, and
instead use associated type bounds on the `View::Element`.
* Introduce an extendable modifier based system, which should also work
on top of memoization (`Arc`, `Memoize`) and `OneOf` views with an
intersection of the modifiable properties.
* This modifier based system gets rid of the hacky way to propagate
attributes to elements, and takes inspiration by masonrys `WidgetMut`
type to apply changes.
* Currently that means `Attributes`, `Classes` and `Styles` to reflect
what xilem_web previously offered.

Downsides (currently, needs some investigation):

~~Due to more internal type complexity via associated types this suffers
from https://github.com/rust-lang/rust/issues/105900. The new trait
solver should hopefully mitigate some of that, but it seems currently it
completely stalls in the todomvc example (not in other examples).~~
~~The deep, possibly completely static composition via associated
type-bounds of the view and element tree unfortunately can take some
time to compile, this gets (already) obvious in the todomvc example. The
other examples don't seem to suffer that bad yet from that issue,
probably because they're quite simple.~~

~~I really hope we can mitigate this mostly, because I think this is the
idiomatic (and more correct) way to implement what the previous API has
offered.~~

One idea is to add a `Box<dyn AnyViewSequence>`, as every element takes
a "type-erased" `ViewSequence` as parameter, so this may solve most of
the issues (at the slight cost of dynamic dispatch/allocations).

Edit: idea was mostly successful, see comment right below.

I think it also closes #274

It's a draft, as there's a lot of changes in xilem_core that should be
upstreamed (and cleaned up) via separate PRs and I would like to
(mostly) fix the slow-compile time issue.

---------

Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
2024-06-28 08:30:18 +00:00
Daniel McNab 86d9592a3e
Move `xilem` onto a new `xilem_core`, which uses a generic View trait (#310)
This:
1) Renames the current/old `xilem_core` to `xilem_web_core` and moves it
to the `xilem_web/xilem_web_core` folder
2) Creates a new `xilem_core`, which does not use (non-tuple) macros and
instead contains a `View` trait which is generic over the `Context` type
3) Ports `xilem` to this `xilem_core`, but with some functionality
missing (namely a few of the extra views; I expect these to
straightforward to port)
4) Ports the `mason` and `mason_android` examples to this new `xilem`,
with less functionality.

This continues ideas first explored in #235 

The advantages of this new View trait are:
1) Improved support for ad-hoc views, such as views with additional
attributes.
This will be very useful for layout algorithms, and will also enable
native *good* multi-window (and potentially menus?)
2) A lack of macros, to better enable using go-to-definition and other
IDE features on the traits

Possible disadvantages:
1) There are a few more traits to enable the flexibility
2) It can be less clear what `Self::Element::Mut` is in the `rebuild`
function, because of how the resolution works
3) When implementing `View`, you need to specify the context (i.e.
`impl<State, Action> View<State, Action, [new] ViewCtx> for
Button<State, Action>`.

---------

Co-authored-by: Philipp Mildenberger <philipp@mildenberger.me>
2024-06-06 15:16:36 +00:00
Alex Pyattaev c12aa851bf
fix invalid HTML in examples (#304)
For some reason the recent version of Trunk refused to serve some of the
examples due to them missing the <html> tags. Adding the tags calms it
down and allows it to work again. Trivial change, but would save someone
the pain of having to fix examples before running them with trunk serve.

Co-authored-by: Alex Pyattaev <me@example.com>
2024-05-12 06:03:24 +00:00
Olivier FAURE ef5d36e8fc
Move crates to the repository root (#302)
Follows the convention proposed in this discussion:

https://xi.zulipchat.com/#narrow/stream/419691-linebender/topic/Standardizing.20multi-package.20repos
2024-05-11 21:59:03 +00:00