From 2b1105e441943b333ab29cd5800eb937fd44f5d0 Mon Sep 17 00:00:00 2001 From: Michae Mauderer Date: Tue, 19 Oct 2021 22:22:28 +0100 Subject: [PATCH 1/5] Apply `cargo fmt`. --- src/rust/build/src/lib.rs | 37 +- src/rust/ensogl/build.rs | 37 +- src/rust/ensogl/example/src/animation.rs | 11 +- .../example/src/complex_shape_system.rs | 137 +- src/rust/ensogl/example/src/dom_symbols.rs | 82 +- src/rust/ensogl/example/src/drop_manager.rs | 38 +- .../ensogl/example/src/easing_animator.rs | 224 +- src/rust/ensogl/example/src/glyph_system.rs | 24 +- src/rust/ensogl/example/src/leak.rs | 8 +- src/rust/ensogl/example/src/lib.rs | 10 +- src/rust/ensogl/example/src/list_view.rs | 78 +- src/rust/ensogl/example/src/mouse_events.rs | 83 +- src/rust/ensogl/example/src/scroll_area.rs | 43 +- src/rust/ensogl/example/src/shape_system.rs | 44 +- src/rust/ensogl/example/src/slider.rs | 45 +- src/rust/ensogl/example/src/sprite_system.rs | 43 +- .../example/src/sprite_system_benchmark.rs | 98 +- src/rust/ensogl/example/src/text_area.rs | 24 +- .../ensogl/lib/components/src/component.rs | 61 +- .../lib/components/src/drop_down_menu.rs | 103 +- .../ensogl/lib/components/src/file_browser.rs | 38 +- .../lib/components/src/file_browser/model.rs | 64 +- src/rust/ensogl/lib/components/src/label.rs | 92 +- src/rust/ensogl/lib/components/src/lib.rs | 4 +- .../ensogl/lib/components/src/list_view.rs | 245 +- .../lib/components/src/list_view/entry.rs | 197 +- .../components/src/list_view/entry/list.rs | 179 +- .../ensogl/lib/components/src/scroll_area.rs | 45 +- .../ensogl/lib/components/src/scrollbar.rs | 78 +- .../ensogl/lib/components/src/selector.rs | 32 +- .../lib/components/src/selector/bounds.rs | 466 +- .../src/selector/decimal_aligned.rs | 54 +- .../ensogl/lib/components/src/selector/frp.rs | 102 +- .../lib/components/src/selector/model.rs | 204 +- .../lib/components/src/selector/number.rs | 52 +- .../lib/components/src/selector/range.rs | 38 +- .../lib/components/src/selector/shape.rs | 123 +- src/rust/ensogl/lib/components/src/shadow.rs | 154 +- .../lib/components/src/toggle_button.rs | 145 +- src/rust/ensogl/lib/core/src/animation.rs | 9 +- .../ensogl/lib/core/src/animation/easing.rs | 234 +- .../lib/core/src/animation/frp/animation.rs | 73 +- .../src/animation/frp/animation/delayed.rs | 16 +- .../src/animation/frp/animation/hysteretic.rs | 22 +- .../lib/core/src/animation/frp/easing.rs | 52 +- .../ensogl/lib/core/src/animation/loops.rs | 229 +- .../lib/core/src/animation/physics/inertia.rs | 584 +- src/rust/ensogl/lib/core/src/application.rs | 66 +- .../ensogl/lib/core/src/application/args.rs | 52 +- .../lib/core/src/application/command.rs | 173 +- .../ensogl/lib/core/src/application/frp.rs | 60 +- .../lib/core/src/application/shortcut.rs | 247 +- .../ensogl/lib/core/src/application/view.rs | 48 +- .../ensogl/lib/core/src/control/io/mouse.rs | 56 +- .../lib/core/src/control/io/mouse/event.rs | 4 +- src/rust/ensogl/lib/core/src/data/color.rs | 4 +- .../lib/core/src/data/color/animation.rs | 25 +- .../lib/core/src/data/color/component.rs | 117 +- .../ensogl/lib/core/src/data/color/data.rs | 190 +- .../lib/core/src/data/color/gradient.rs | 67 +- .../ensogl/lib/core/src/data/color/mix.rs | 15 +- .../core/src/data/color/space/conversion.rs | 82 +- .../lib/core/src/data/color/space/def.rs | 399 +- .../core/src/data/color/space/white_point.rs | 8 +- .../ensogl/lib/core/src/data/container.rs | 38 +- src/rust/ensogl/lib/core/src/data/dirty.rs | 493 +- .../lib/core/src/data/function/traits.rs | 2 - src/rust/ensogl/lib/core/src/data/mix.rs | 46 +- .../lib/core/src/data/seq/observable.rs | 47 +- src/rust/ensogl/lib/core/src/debug/monitor.rs | 844 ++- src/rust/ensogl/lib/core/src/debug/stats.rs | 16 +- src/rust/ensogl/lib/core/src/display.rs | 12 +- .../lib/core/src/display/camera/camera2d.rs | 258 +- .../lib/core/src/display/layout/alignment.rs | 106 +- .../core/src/display/navigation/navigator.rs | 120 +- .../display/navigation/navigator/events.rs | 365 +- .../ensogl/lib/core/src/display/object.rs | 4 +- .../lib/core/src/display/object/class.rs | 888 +-- .../lib/core/src/display/object/transform.rs | 155 +- .../ensogl/lib/core/src/display/render.rs | 2 - .../lib/core/src/display/render/composer.rs | 26 +- .../lib/core/src/display/render/pass.rs | 150 +- .../lib/core/src/display/render/passes.rs | 6 +- .../src/display/render/passes/pixel_read.rs | 199 +- .../core/src/display/render/passes/screen.rs | 12 +- .../core/src/display/render/passes/symbols.rs | 228 +- .../lib/core/src/display/render/pipeline.rs | 6 +- src/rust/ensogl/lib/core/src/display/scene.rs | 872 +-- .../ensogl/lib/core/src/display/scene/dom.rs | 155 +- .../lib/core/src/display/scene/layer.rs | 662 +- src/rust/ensogl/lib/core/src/display/shape.rs | 1 - .../core/src/display/shape/compound/events.rs | 10 +- .../core/src/display/shape/compound/path.rs | 10 +- .../lib/core/src/display/shape/constants.rs | 2 +- .../lib/core/src/display/shape/primitive.rs | 4 +- .../core/src/display/shape/primitive/def.rs | 2 +- .../src/display/shape/primitive/def/class.rs | 99 +- .../display/shape/primitive/def/modifier.rs | 6 +- .../display/shape/primitive/def/primitive.rs | 114 +- .../src/display/shape/primitive/def/unit.rs | 10 +- .../src/display/shape/primitive/def/var.rs | 503 +- .../display/shape/primitive/shader/builder.rs | 64 +- .../display/shape/primitive/shader/canvas.rs | 235 +- .../shape/primitive/shader/overload.rs | 2 +- .../display/shape/primitive/style_watch.rs | 168 +- .../src/display/shape/primitive/system.rs | 160 +- src/rust/ensogl/lib/core/src/display/style.rs | 2 +- .../ensogl/lib/core/src/display/style/data.rs | 108 +- .../lib/core/src/display/style/javascript.rs | 98 +- .../ensogl/lib/core/src/display/style/path.rs | 76 +- .../lib/core/src/display/style/sheet.rs | 1018 +-- .../lib/core/src/display/style/theme.rs | 236 +- .../ensogl/lib/core/src/display/symbol/dom.rs | 89 +- .../ensogl/lib/core/src/display/symbol/gpu.rs | 422 +- .../core/src/display/symbol/gpu/geometry.rs | 3 +- .../display/symbol/gpu/geometry/compound.rs | 8 +- .../gpu/geometry/compound/mask_composer.rs | 33 +- .../symbol/gpu/geometry/compound/screen.rs | 33 +- .../symbol/gpu/geometry/compound/sprite.rs | 301 +- .../display/symbol/gpu/geometry/primitive.rs | 1 - .../symbol/gpu/geometry/primitive/mesh.rs | 22 +- .../core/src/display/symbol/gpu/material.rs | 53 +- .../core/src/display/symbol/gpu/registry.rs | 89 +- .../lib/core/src/display/symbol/gpu/shader.rs | 28 +- .../src/display/symbol/gpu/shader/builder.rs | 335 +- src/rust/ensogl/lib/core/src/display/world.rs | 161 +- .../lib/core/src/display/world/stats.rs | 2 - src/rust/ensogl/lib/core/src/gui/component.rs | 149 +- src/rust/ensogl/lib/core/src/gui/cursor.rs | 184 +- src/rust/ensogl/lib/core/src/gui/style.rs | 32 +- src/rust/ensogl/lib/core/src/lib.rs | 23 +- src/rust/ensogl/lib/core/src/system/gpu.rs | 1 - .../ensogl/lib/core/src/system/gpu/data.rs | 7 +- .../lib/core/src/system/gpu/data/attribute.rs | 32 +- .../lib/core/src/system/gpu/data/buffer.rs | 116 +- .../core/src/system/gpu/data/buffer/item.rs | 160 +- .../core/src/system/gpu/data/buffer/usage.rs | 4 +- .../lib/core/src/system/gpu/data/default.rs | 10 +- .../lib/core/src/system/gpu/data/gl_enum.rs | 27 +- .../lib/core/src/system/gpu/data/prim.rs | 9 +- .../lib/core/src/system/gpu/data/sized.rs | 39 +- .../lib/core/src/system/gpu/data/texture.rs | 6 +- .../core/src/system/gpu/data/texture/class.rs | 253 +- .../src/system/gpu/data/texture/storage.rs | 9 +- .../gpu/data/texture/storage/gpu_only.rs | 51 +- .../system/gpu/data/texture/storage/owned.rs | 29 +- .../gpu/data/texture/storage/remote_image.rs | 105 +- .../system/gpu/data/texture/types/format.rs | 4 +- .../system/gpu/data/texture/types/gl_enums.rs | 5 +- .../gpu/data/texture/types/internal_format.rs | 18 +- .../gpu/data/texture/types/item_type.rs | 7 +- .../gpu/data/texture/types/relations.rs | 6 +- .../system/gpu/data/texture/types/sampler.rs | 2 - .../lib/core/src/system/gpu/data/uniform.rs | 136 +- .../src/system/gpu/data/uniform/upload.rs | 251 +- .../ensogl/lib/core/src/system/gpu/shader.rs | 148 +- .../lib/core/src/system/gpu/shader/glsl.rs | 451 +- .../lib/core/src/system/js/typed_array.rs | 36 +- src/rust/ensogl/lib/core/src/system/web.rs | 6 +- .../lib/core/src/system/web/dom/shape.rs | 150 +- .../ensogl/lib/text/embedded-fonts/build.rs | 74 +- .../ensogl/lib/text/embedded-fonts/src/lib.rs | 15 +- src/rust/ensogl/lib/text/msdf-sys/build.rs | 10 +- .../ensogl/lib/text/msdf-sys/src/binding.rs | 91 +- .../lib/text/msdf-sys/src/emscripten_data.rs | 78 +- src/rust/ensogl/lib/text/msdf-sys/src/lib.rs | 201 +- src/rust/ensogl/lib/text/src/buffer.rs | 46 +- src/rust/ensogl/lib/text/src/buffer/data.rs | 4 +- .../ensogl/lib/text/src/buffer/data/range.rs | 96 +- .../ensogl/lib/text/src/buffer/data/rope.rs | 8 +- .../ensogl/lib/text/src/buffer/data/spans.rs | 42 +- .../ensogl/lib/text/src/buffer/data/text.rs | 516 +- .../ensogl/lib/text/src/buffer/data/unit.rs | 42 +- src/rust/ensogl/lib/text/src/buffer/style.rs | 108 +- src/rust/ensogl/lib/text/src/buffer/view.rs | 320 +- .../lib/text/src/buffer/view/movement.rs | 220 +- .../lib/text/src/buffer/view/selection.rs | 205 +- .../ensogl/lib/text/src/buffer/view/word.rs | 136 +- .../ensogl/lib/text/src/component/area.rs | 612 +- .../lib/text/src/component/selection.rs | 115 +- src/rust/ensogl/lib/text/src/lib.rs | 4 +- src/rust/ensogl/lib/text/src/typeface/font.rs | 256 +- .../ensogl/lib/text/src/typeface/font/msdf.rs | 68 +- .../ensogl/lib/text/src/typeface/glyph.rs | 149 +- src/rust/ensogl/lib/text/src/typeface/pen.rs | 87 +- src/rust/ensogl/lib/theme/src/lib.rs | 32 +- src/rust/ensogl/lib/web/src/drop.rs | 126 +- src/rust/ensogl/lib/web/src/lib.rs | 6 +- src/rust/ide/lib/analytics/src/data.rs | 39 +- src/rust/ide/lib/analytics/src/remote_log.rs | 29 +- src/rust/ide/lib/args/src/lib.rs | 6 +- src/rust/ide/lib/ast/impl/src/assoc.rs | 25 +- src/rust/ide/lib/ast/impl/src/crumbs.rs | 2035 +++--- src/rust/ide/lib/ast/impl/src/identifier.rs | 18 +- src/rust/ide/lib/ast/impl/src/internal.rs | 8 +- src/rust/ide/lib/ast/impl/src/known.rs | 139 +- src/rust/ide/lib/ast/impl/src/lib.rs | 1470 +++-- src/rust/ide/lib/ast/impl/src/macros.rs | 162 +- src/rust/ide/lib/ast/impl/src/opr.rs | 482 +- src/rust/ide/lib/ast/impl/src/prefix.rs | 167 +- src/rust/ide/lib/ast/impl/src/repr.rs | 289 +- src/rust/ide/lib/ast/impl/src/test_utils.rs | 38 +- src/rust/ide/lib/ast/macros/src/lib.rs | 144 +- src/rust/ide/lib/ast/macros/src/token.rs | 59 +- src/rust/ide/lib/enso-protocol/build.rs | 75 +- src/rust/ide/lib/enso-protocol/src/binary.rs | 4 +- .../lib/enso-protocol/src/binary/client.rs | 317 +- .../enso-protocol/src/binary/connection.rs | 55 +- .../lib/enso-protocol/src/binary/message.rs | 184 +- .../enso-protocol/src/binary/serialization.rs | 794 ++- .../ide/lib/enso-protocol/src/binary/uuid.rs | 37 +- .../ide/lib/enso-protocol/src/common/error.rs | 16 +- .../enso-protocol/src/common/ongoing_calls.rs | 52 +- .../generated/binary_protocol_generated.rs | 5509 ++++++++++------- src/rust/ide/lib/enso-protocol/src/handler.rs | 239 +- .../lib/enso-protocol/src/language_server.rs | 53 +- .../src/language_server/connection.rs | 80 +- .../src/language_server/constants.rs | 4 +- .../src/language_server/response.rs | 66 +- .../src/language_server/tests.rs | 339 +- .../src/language_server/types.rs | 833 +-- src/rust/ide/lib/enso-protocol/src/lib.rs | 21 +- .../lib/enso-protocol/src/project_manager.rs | 358 +- src/rust/ide/lib/enso-protocol/src/types.rs | 32 +- src/rust/ide/lib/json-rpc/src/api.rs | 20 +- src/rust/ide/lib/json-rpc/src/error.rs | 29 +- src/rust/ide/lib/json-rpc/src/handler.rs | 167 +- src/rust/ide/lib/json-rpc/src/lib.rs | 6 +- src/rust/ide/lib/json-rpc/src/messages.rs | 242 +- .../json-rpc/src/test_util/transport/mock.rs | 77 +- src/rust/ide/lib/json-rpc/src/transport.rs | 13 +- src/rust/ide/lib/json-rpc/tests/test.rs | 145 +- src/rust/ide/lib/parser/build.rs | 99 +- src/rust/ide/lib/parser/src/api.rs | 194 +- src/rust/ide/lib/parser/src/jsclient.rs | 51 +- src/rust/ide/lib/parser/src/lib.rs | 91 +- src/rust/ide/lib/parser/src/main.rs | 16 +- src/rust/ide/lib/parser/src/test_utils.rs | 29 +- src/rust/ide/lib/parser/src/wsclient.rs | 138 +- src/rust/ide/lib/parser/tests/ast.rs | 89 +- src/rust/ide/lib/parser/tests/bugs.rs | 20 +- src/rust/ide/lib/parser/tests/crumbs.rs | 13 +- src/rust/ide/lib/parser/tests/doc-gen.rs | 23 +- src/rust/ide/lib/parser/tests/id_map.rs | 22 +- src/rust/ide/lib/parser/tests/macros.rs | 36 +- src/rust/ide/lib/parser/tests/parsing.rs | 439 +- src/rust/ide/lib/parser/tests/web.rs | 29 +- src/rust/ide/lib/span-tree/example/src/lib.rs | 136 +- src/rust/ide/lib/span-tree/src/action.rs | 585 +- src/rust/ide/lib/span-tree/src/builder.rs | 83 +- src/rust/ide/lib/span-tree/src/generate.rs | 1334 ++-- .../ide/lib/span-tree/src/generate/context.rs | 20 +- .../ide/lib/span-tree/src/generate/macros.rs | 139 +- src/rust/ide/lib/span-tree/src/iter.rs | 162 +- src/rust/ide/lib/span-tree/src/lib.rs | 61 +- src/rust/ide/lib/span-tree/src/node.rs | 814 ++- src/rust/ide/lib/span-tree/src/node/kind.rs | 269 +- src/rust/ide/lib/utils/src/channel.rs | 32 +- src/rust/ide/lib/utils/src/env.rs | 8 +- src/rust/ide/lib/utils/src/fail.rs | 2 +- src/rust/ide/lib/utils/src/future.rs | 2 +- src/rust/ide/lib/utils/src/iter.rs | 17 +- src/rust/ide/lib/utils/src/serde.rs | 58 +- src/rust/ide/lib/utils/src/string.rs | 26 +- src/rust/ide/lib/utils/src/test.rs | 10 +- src/rust/ide/lib/utils/src/test/future.rs | 39 +- src/rust/ide/lib/utils/src/test/stream.rs | 45 +- src/rust/ide/lib/utils/src/vec.rs | 8 +- src/rust/ide/src/config.rs | 100 +- src/rust/ide/src/constants.rs | 24 +- src/rust/ide/src/controller.rs | 16 +- src/rust/ide/src/controller/graph.rs | 1428 +++-- src/rust/ide/src/controller/graph/executed.rs | 383 +- src/rust/ide/src/controller/ide.rs | 67 +- src/rust/ide/src/controller/ide/desktop.rs | 124 +- src/rust/ide/src/controller/ide/plain.rs | 87 +- src/rust/ide/src/controller/module.rs | 227 +- src/rust/ide/src/controller/project.rs | 233 +- src/rust/ide/src/controller/searcher.rs | 2143 ++++--- .../ide/src/controller/searcher/action.rs | 374 +- .../controller/searcher/action/hardcoded.rs | 194 +- src/rust/ide/src/controller/text.rs | 223 +- src/rust/ide/src/controller/upload.rs | 783 ++- src/rust/ide/src/controller/visualization.rs | 241 +- src/rust/ide/src/double_representation.rs | 112 +- .../double_representation/alias_analysis.rs | 266 +- .../alias_analysis/test_utils.rs | 135 +- .../src/double_representation/connection.rs | 209 +- .../src/double_representation/definition.rs | 526 +- .../ide/src/double_representation/graph.rs | 222 +- .../src/double_representation/identifier.rs | 149 +- .../ide/src/double_representation/module.rs | 677 +- .../ide/src/double_representation/node.rs | 351 +- .../ide/src/double_representation/project.rs | 89 +- .../refactorings/collapse.rs | 451 +- .../src/double_representation/test_utils.rs | 26 +- .../ide/src/double_representation/text.rs | 200 +- src/rust/ide/src/double_representation/tp.rs | 152 +- src/rust/ide/src/executor.rs | 2 +- src/rust/ide/src/executor/global.rs | 25 +- src/rust/ide/src/executor/test_utils.rs | 42 +- src/rust/ide/src/executor/web.rs | 42 +- src/rust/ide/src/ide.rs | 56 +- src/rust/ide/src/ide/initializer.rs | 231 +- src/rust/ide/src/ide/integration.rs | 91 +- .../ide/src/ide/integration/file_system.rs | 273 +- src/rust/ide/src/ide/integration/project.rs | 1957 ++++-- .../ide/src/ide/integration/visualization.rs | 527 +- src/rust/ide/src/lib.rs | 32 +- src/rust/ide/src/model.rs | 8 +- src/rust/ide/src/model/execution_context.rs | 274 +- .../ide/src/model/execution_context/plain.rs | 238 +- .../model/execution_context/synchronized.rs | 410 +- src/rust/ide/src/model/module.rs | 492 +- src/rust/ide/src/model/module/plain.rs | 275 +- src/rust/ide/src/model/module/synchronized.rs | 566 +- src/rust/ide/src/model/project.rs | 160 +- .../ide/src/model/project/synchronized.rs | 757 ++- src/rust/ide/src/model/registry.rs | 163 +- src/rust/ide/src/model/suggestion_database.rs | 608 +- .../src/model/suggestion_database/entry.rs | 778 ++- .../src/model/suggestion_database/example.rs | 60 +- src/rust/ide/src/model/undo_redo.rs | 320 +- src/rust/ide/src/notification.rs | 41 +- src/rust/ide/src/sync.rs | 81 +- src/rust/ide/src/test.rs | 550 +- src/rust/ide/src/tests.rs | 138 +- src/rust/ide/src/transport/test_utils.rs | 54 +- src/rust/ide/src/transport/web.rs | 272 +- src/rust/ide/tests/language_server.rs | 461 +- src/rust/ide/tests/project_manager.rs | 71 +- .../src/builtin/visualization/java_script.rs | 60 +- .../visualization/native/bubble_chart.rs | 120 +- .../src/builtin/visualization/native/error.rs | 202 +- .../builtin/visualization/native/raw_text.rs | 137 +- .../ide/view/graph-editor/src/component.rs | 4 +- .../graph-editor/src/component/breadcrumbs.rs | 344 +- .../src/component/breadcrumbs/breadcrumb.rs | 381 +- .../src/component/breadcrumbs/project_name.rs | 205 +- .../view/graph-editor/src/component/edge.rs | 1143 ++-- .../view/graph-editor/src/component/node.rs | 263 +- .../src/component/node/action_bar.rs | 186 +- .../src/component/node/action_bar/icon.rs | 44 +- .../graph-editor/src/component/node/error.rs | 132 +- .../src/component/node/expression.rs | 42 +- .../src/component/node/input/area.rs | 346 +- .../src/component/node/input/port.rs | 85 +- .../src/component/node/output/area.rs | 330 +- .../src/component/node/output/port.rs | 346 +- .../src/component/node/profiling.rs | 106 +- .../graph-editor/src/component/node/vcs.rs | 66 +- .../graph-editor/src/component/profiling.rs | 41 +- .../graph-editor/src/component/tooltip.rs | 117 +- .../src/component/type_coloring.rs | 50 +- .../src/component/visualization.rs | 2 +- .../src/component/visualization/container.rs | 314 +- .../visualization/container/action_bar.rs | 189 +- .../visualization/container/fullscreen.rs | 70 +- .../container/visualization_chooser.rs | 58 +- .../src/component/visualization/data.rs | 56 +- .../src/component/visualization/definition.rs | 75 +- .../visualization/foreign/java_script.rs | 2 +- .../foreign/java_script/binding.rs | 69 +- .../foreign/java_script/definition.rs | 128 +- .../java_script/definition/function.rs | 8 +- .../foreign/java_script/instance.rs | 284 +- .../src/component/visualization/instance.rs | 137 +- .../src/component/visualization/layer.rs | 12 +- .../src/component/visualization/metadata.rs | 9 +- .../src/component/visualization/path.rs | 34 +- .../src/component/visualization/registry.rs | 87 +- src/rust/ide/view/graph-editor/src/data.rs | 5 +- src/rust/ide/view/graph-editor/src/lib.rs | 1638 +++-- .../ide/view/graph-editor/src/profiling.rs | 45 +- .../ide/view/graph-editor/src/selection.rs | 142 +- .../src/selection/bounding_box.rs | 100 +- src/rust/ide/view/graph-editor/src/view.rs | 12 +- src/rust/ide/view/src/code_editor.rs | 63 +- .../ide/view/src/debug_scenes/interface.rs | 591 +- .../view/src/debug_scenes/visualization.rs | 84 +- src/rust/ide/view/src/documentation.rs | 280 +- src/rust/ide/view/src/lib.rs | 4 +- src/rust/ide/view/src/open_dialog.rs | 56 +- .../ide/view/src/open_dialog/project_list.rs | 77 +- src/rust/ide/view/src/project.rs | 300 +- src/rust/ide/view/src/searcher.rs | 198 +- src/rust/ide/view/src/searcher/icons.rs | 197 +- src/rust/ide/view/src/searcher/new.rs | 48 +- src/rust/ide/view/src/status_bar.rs | 168 +- .../ide/view/src/window_control_buttons.rs | 201 +- .../view/src/window_control_buttons/close.rs | 12 +- .../view/src/window_control_buttons/common.rs | 179 +- .../src/window_control_buttons/fullscreen.rs | 12 +- src/rust/lib/callback/src/lib.rs | 205 +- src/rust/lib/code-builder/src/lib.rs | 65 +- src/rust/lib/config/build.rs | 41 +- src/rust/lib/eval-tt/src/lib.rs | 6 +- src/rust/lib/frp/src/data/bitfield.rs | 103 +- src/rust/lib/frp/src/data/watch.rs | 26 +- src/rust/lib/frp/src/debug.rs | 239 +- src/rust/lib/frp/src/io/js.rs | 137 +- src/rust/lib/frp/src/io/keyboard.rs | 272 +- src/rust/lib/frp/src/io/mouse.rs | 247 +- src/rust/lib/frp/src/lib.rs | 39 +- src/rust/lib/frp/src/macros.rs | 4 - src/rust/lib/frp/src/network.rs | 154 +- src/rust/lib/frp/src/node.rs | 17 +- src/rust/lib/frp/src/nodes.rs | 4335 +++++++++---- src/rust/lib/frp/src/stream.rs | 452 +- src/rust/lib/fuzzly/src/lib.rs | 9 +- src/rust/lib/fuzzly/src/metric.rs | 145 +- src/rust/lib/fuzzly/src/score.rs | 260 +- src/rust/lib/fuzzly/src/subsequence_graph.rs | 184 +- src/rust/lib/shortcuts/example/src/lib.rs | 8 +- src/rust/lib/shortcuts/src/lib.rs | 924 +-- src/rust/lib/system/web/src/clipboard.rs | 16 +- .../lib/system/web/src/closure/storage.rs | 32 +- src/rust/lib/system/web/src/event.rs | 26 +- src/rust/lib/system/web/src/event/listener.rs | 44 +- src/rust/lib/system/web/src/lib.rs | 415 +- src/rust/lib/system/web/src/platform.rs | 145 +- .../lib/system/web/src/resize_observer.rs | 22 +- src/rust/lib/system/web/src/stream.rs | 25 +- src/rust/lib/types/src/algebra.rs | 172 +- src/rust/lib/types/src/num/saturating.rs | 2 +- src/rust/lib/types/src/topology.rs | 69 +- src/rust/lib/types/src/unit.rs | 29 +- src/rust/lib/web-test-proc-macro/src/lib.rs | 16 +- src/rust/lib/web-test/src/bench_container.rs | 65 +- src/rust/lib/web-test/src/bencher.rs | 99 +- src/rust/lib/web-test/src/container.rs | 43 +- src/rust/lib/web-test/src/group.rs | 29 +- src/rust/lib/web-test/src/lib.rs | 10 +- 433 files changed, 49425 insertions(+), 33412 deletions(-) diff --git a/src/rust/build/src/lib.rs b/src/rust/build/src/lib.rs index 88c6b2db5c..6c49541068 100644 --- a/src/rust/build/src/lib.rs +++ b/src/rust/build/src/lib.rs @@ -1,36 +1,41 @@ #![feature(trait_alias)] -use std::path; -use std::io::ErrorKind; use std::fmt::Display; +use std::io::ErrorKind; +use std::path; /// Types that can yield a reference to std::path::Path. pub trait PathRef = AsRef; /// A structure describing a concrete release package on GitHub. pub struct GithubRelease { - pub project_url : T, - pub version : T, - pub filename : T, + pub project_url: T, + pub version: T, + pub filename: T, } -impl+Display> GithubRelease { +impl + Display> GithubRelease { /// Download the release package from GitHub if the target file was missing. Returns true if /// the file was downloaded or false if it already existed. /// /// The project_url should be a project's main page on GitHub. - pub fn download(&self, destination_dir:&path::Path) { - let url = format!("{}/releases/download/{}/{}",self.project_url,self.version,self.filename); + pub fn download(&self, destination_dir: &path::Path) { + let url = format!( + "{}/releases/download/{}/{}", + self.project_url, self.version, self.filename + ); let destination_file = destination_dir.join(self.filename.as_ref()); Self::remove_old_file(&destination_file); let mut resp = reqwest::blocking::get(&url).expect("Download failed."); - let mut out = std::fs::File::create(destination_file).expect("Failed to create file."); - std::io::copy(&mut resp, &mut out).expect("Failed to copy file content."); + let mut out = std::fs::File::create(destination_file) + .expect("Failed to create file."); + std::io::copy(&mut resp, &mut out) + .expect("Failed to copy file content."); } - fn remove_old_file(file:&path::Path) { - let result = std::fs::remove_file(&file); - let error = result.err(); + fn remove_old_file(file: &path::Path) { + let result = std::fs::remove_file(&file); + let error = result.err(); let fatal_error = error.filter(|err| err.kind() != ErrorKind::NotFound); assert!(fatal_error.is_none()); } @@ -48,10 +53,12 @@ pub fn absolute_path(path: impl PathRef) -> std::io::Result { } /// Get the environment variable or panic if not available. -pub fn env_var_or_panic(var_name:&str) -> String { +pub fn env_var_or_panic(var_name: &str) -> String { match std::env::var(var_name) { Ok(var) => var, - Err(e) => panic!("Failed to read environment variable {}: {}.", var_name, e), + Err(e) => { + panic!("Failed to read environment variable {}: {}.", var_name, e) + } } } diff --git a/src/rust/ensogl/build.rs b/src/rust/ensogl/build.rs index 3f0b255703..57983ca868 100644 --- a/src/rust/ensogl/build.rs +++ b/src/rust/ensogl/build.rs @@ -1,18 +1,18 @@ -use std::{path, env}; +use std::{env, path}; /// A module with functions generating huge chunk of texts for text component benchmarks. mod huge_text_generator { use std::collections::hash_map::DefaultHasher; use std::fs::File; - use std::hash::{Hash,Hasher}; + use std::hash::{Hash, Hasher}; use std::io::Write; use std::path::Path; - const MANY : usize = 100000; - const NOT_SO_MANY : usize = 100; + const MANY: usize = 100000; + const NOT_SO_MANY: usize = 100; /// Create a file with many lines. - pub fn make_long_text_file(name:&Path) { + pub fn make_long_text_file(name: &Path) { let mut file = File::create(name).unwrap(); for i in (1..MANY).rev() { write_verse(&mut file, i); @@ -21,28 +21,28 @@ mod huge_text_generator { } /// Create a file with not so many long lines - pub fn make_wide_text_file(name:&Path) { - let mut file = File::create(name).unwrap(); - let verses_in_line = MANY/NOT_SO_MANY; + pub fn make_wide_text_file(name: &Path) { + let mut file = File::create(name).unwrap(); + let verses_in_line = MANY / NOT_SO_MANY; for i in (1..MANY).rev() { write_verse(&mut file, i); if i % verses_in_line == 0 { let line_index = i / verses_in_line; - let offset = hash_from(line_index) % 32; - let prefix = (0..offset).map(|_| '|').collect::(); + let offset = hash_from(line_index) % 32; + let prefix = (0..offset).map(|_| '|').collect::(); writeln!(file).unwrap(); - write!(file,"{}",prefix).unwrap(); + write!(file, "{}", prefix).unwrap(); } } } - fn hash_from(i:usize) -> u64 { + fn hash_from(i: usize) -> u64 { let mut hasher = DefaultHasher::new(); i.hash(&mut hasher); hasher.finish() } - fn write_verse(file:&mut File, i:usize) { + fn write_verse(file: &mut File, i: usize) { write!(file, "{i} bottles of beer on the wall, {i} bottles of beer.\ Take one down and pass it around, {j} bottles of beer on the wall. ", @@ -52,11 +52,14 @@ mod huge_text_generator { } } - fn main() { - let out = env::var("OUT_DIR").unwrap(); + let out = env::var("OUT_DIR").unwrap(); let out_dir = path::Path::new(&out); - huge_text_generator::make_long_text_file(out_dir.join("long.txt").as_path()); - huge_text_generator::make_wide_text_file(out_dir.join("wide.txt").as_path()); + huge_text_generator::make_long_text_file( + out_dir.join("long.txt").as_path(), + ); + huge_text_generator::make_wide_text_file( + out_dir.join("wide.txt").as_path(), + ); println!("cargo:rerun-if-changed=build.rs"); } diff --git a/src/rust/ensogl/example/src/animation.rs b/src/rust/ensogl/example/src/animation.rs index 75f4c5791e..85faeedb01 100644 --- a/src/rust/ensogl/example/src/animation.rs +++ b/src/rust/ensogl/example/src/animation.rs @@ -2,15 +2,13 @@ use crate::prelude::*; -use ensogl_core::system::web; use ensogl_core::application::Application; +use ensogl_core::system::web; use ensogl_core::DEPRECATED_Animation; use ensogl_text_msdf_sys::run_once_initialized; use logger::TraceLogger as Logger; use wasm_bindgen::prelude::*; - - // =================== // === Entry Point === // =================== @@ -22,20 +20,19 @@ pub fn entry_point_animation() { web::forward_panic_hook_to_console(); web::set_stack_trace_limit(); run_once_initialized(|| { - let app = Application::new(&web::get_html_element_by_id("root").unwrap()); + let app = + Application::new(&web::get_html_element_by_id("root").unwrap()); init(); mem::forget(app); }); } - // ======================== // === Init Application === // ======================== fn init() { - - let logger : Logger = Logger::new("AnimationTest"); + let logger: Logger = Logger::new("AnimationTest"); let network = enso_frp::Network::new("test"); let animation = DEPRECATED_Animation::::new(&network); animation.set_target_value(-259_830.0); diff --git a/src/rust/ensogl/example/src/complex_shape_system.rs b/src/rust/ensogl/example/src/complex_shape_system.rs index a66646ce2c..294843c390 100644 --- a/src/rust/ensogl/example/src/complex_shape_system.rs +++ b/src/rust/ensogl/example/src/complex_shape_system.rs @@ -2,17 +2,15 @@ use ensogl_core::prelude::*; +use ensogl_core::data::color; use ensogl_core::display::navigation::navigator::Navigator; -use ensogl_core::system::web; -use wasm_bindgen::prelude::*; use ensogl_core::display::object::ObjectOps; -use ensogl_core::display::world::*; use ensogl_core::display::scene; use ensogl_core::display::shape::*; -use ensogl_core::data::color; use ensogl_core::display::style::theme; - - +use ensogl_core::display::world::*; +use ensogl_core::system::web; +use wasm_bindgen::prelude::*; // ============== // === Shapes === @@ -45,8 +43,6 @@ mod mask { } } - - // =================== // === Entry Point === // =================== @@ -58,34 +54,35 @@ pub fn entry_point_complex_shape_system() { web::forward_panic_hook_to_console(); web::set_stack_trace_limit(); - let world = World::new(&web::get_html_element_by_id("root").unwrap()); - let scene = world.scene(); - let camera = scene.camera().clone_ref(); - let navigator = Navigator::new(scene,&camera); - let logger = Logger::new("ShapeView"); + let world = World::new(&web::get_html_element_by_id("root").unwrap()); + let scene = world.scene(); + let camera = scene.camera().clone_ref(); + let navigator = Navigator::new(scene, &camera); + let logger = Logger::new("ShapeView"); let theme_manager = theme::Manager::from(&scene.style_sheet); let theme1 = theme::Theme::new(); - theme1.set("base_color", color::Rgba::new(0.0,0.0,1.0,1.0)); + theme1.set("base_color", color::Rgba::new(0.0, 0.0, 1.0, 1.0)); theme1.set("animation.duration", 0.5); theme1.set("graph.node.shadow.color", 5.0); theme1.set("graph.node.shadow.size", 5.0); - theme1.set("mouse.pointer.color", color::Rgba::new(0.3,0.3,0.3,1.0)); + theme1.set("mouse.pointer.color", color::Rgba::new(0.3, 0.3, 0.3, 1.0)); let theme2 = theme::Theme::new(); - theme2.set("base_color", color::Rgba::new(0.0,1.0,0.0,1.0)); + theme2.set("base_color", color::Rgba::new(0.0, 1.0, 0.0, 1.0)); theme2.set("animation.duration", 0.7); theme2.set("graph.node.shadow.color", 5.0); theme2.set("graph.node.shadow.size", 5.0); - theme2.set("mouse.pointer.color", color::Rgba::new(0.3,0.3,0.3,1.0)); + theme2.set("mouse.pointer.color", color::Rgba::new(0.3, 0.3, 0.3, 1.0)); - theme_manager.register("theme1",theme1); - theme_manager.register("theme2",theme2); + theme_manager.register("theme1", theme1); + theme_manager.register("theme2", theme2); theme_manager.set_enabled(&["theme1".to_string()]); - let style_watch = ensogl_core::display::shape::StyleWatch::new(&scene.style_sheet); + let style_watch = + ensogl_core::display::shape::StyleWatch::new(&scene.style_sheet); // style_watch.set_on_style_change(|| DEBUG!("Style changed!")); style_watch.get("base_color"); @@ -97,7 +94,10 @@ pub fn entry_point_complex_shape_system() { mask.size.set(Vector2::new(300.0, 300.0)); mask.mod_position(|t| *t = Vector3::new(-50.0, 0.0, 0.0)); - let scissor_box = scene::layer::ScissorBox::new_with_position_and_size(default(),Vector2(600,600)); + let scissor_box = scene::layer::ScissorBox::new_with_position_and_size( + default(), + Vector2(600, 600), + ); scene.layers.main.set_scissor_box(Some(&scissor_box)); let view2 = shape::View::new(&logger); @@ -111,51 +111,52 @@ pub fn entry_point_complex_shape_system() { let scene = world.scene().clone_ref(); let mut frame = 0; - world.on_frame(move |_time| { - mask.set_position_x(((frame as f32)/30.0).sin()*100.0); - - let _keep_alive = &navigator; - let _keep_alive = &style_watch; - let _keep_alive = &theme_manager; - if frame == 50 { - // These comments are left for easy debugging in the future. - // DEBUG!("---------------"); - // DEBUG!("{scene.layers.node_searcher:#?}"); - // DEBUG!("{scene.layers.main:#?}"); - // DEBUG!("{scene.layers.mask:#?}"); - // DEBUG!("{scene.layers.node_searcher_mask:#?}"); - // DEBUG!("{scene.layers.viz:#?}"); - } - if frame == 100 { - DEBUG!("Adding previously hidden element."); - scene.add_child(&view2); - // These comments are left for easy debugging in the future. - // DEBUG!("---------------"); - // DEBUG!("{scene.layers.node_searcher:#?}"); - // DEBUG!("{scene.layers.main:#?}"); - // DEBUG!("{scene.layers.mask:#?}"); - // DEBUG!("{scene.layers.node_searcher_mask:#?}"); - // DEBUG!("{scene.layers.viz:#?}"); - } - if frame == 150 { - DEBUG!("Enabling masking."); - // These comments are left for easy debugging in the future. - // DEBUG!("---------------"); - // DEBUG!("{scene.layers.node_searcher:#?}"); - // DEBUG!("{scene.layers.main:#?}"); - // DEBUG!("{scene.layers.mask:#?}"); - // DEBUG!("{scene.layers.node_searcher_mask:#?}"); - // DEBUG!("{scene.layers.viz:#?}"); - - scene.layers.node_searcher.add_exclusive(&view1); - scene.layers.node_searcher.add_exclusive(&view2); - scene.layers.node_searcher_mask.add_exclusive(&mask); - } - if frame == 200 { - DEBUG!("Changing the theme."); - theme_manager.set_enabled(&["theme2".to_string()]); - } - frame += 1; - }).forget(); - + world + .on_frame(move |_time| { + mask.set_position_x(((frame as f32) / 30.0).sin() * 100.0); + + let _keep_alive = &navigator; + let _keep_alive = &style_watch; + let _keep_alive = &theme_manager; + if frame == 50 { + // These comments are left for easy debugging in the future. + // DEBUG!("---------------"); + // DEBUG!("{scene.layers.node_searcher:#?}"); + // DEBUG!("{scene.layers.main:#?}"); + // DEBUG!("{scene.layers.mask:#?}"); + // DEBUG!("{scene.layers.node_searcher_mask:#?}"); + // DEBUG!("{scene.layers.viz:#?}"); + } + if frame == 100 { + DEBUG!("Adding previously hidden element."); + scene.add_child(&view2); + // These comments are left for easy debugging in the future. + // DEBUG!("---------------"); + // DEBUG!("{scene.layers.node_searcher:#?}"); + // DEBUG!("{scene.layers.main:#?}"); + // DEBUG!("{scene.layers.mask:#?}"); + // DEBUG!("{scene.layers.node_searcher_mask:#?}"); + // DEBUG!("{scene.layers.viz:#?}"); + } + if frame == 150 { + DEBUG!("Enabling masking."); + // These comments are left for easy debugging in the future. + // DEBUG!("---------------"); + // DEBUG!("{scene.layers.node_searcher:#?}"); + // DEBUG!("{scene.layers.main:#?}"); + // DEBUG!("{scene.layers.mask:#?}"); + // DEBUG!("{scene.layers.node_searcher_mask:#?}"); + // DEBUG!("{scene.layers.viz:#?}"); + + scene.layers.node_searcher.add_exclusive(&view1); + scene.layers.node_searcher.add_exclusive(&view2); + scene.layers.node_searcher_mask.add_exclusive(&mask); + } + if frame == 200 { + DEBUG!("Changing the theme."); + theme_manager.set_enabled(&["theme2".to_string()]); + } + frame += 1; + }) + .forget(); } diff --git a/src/rust/ensogl/example/src/dom_symbols.rs b/src/rust/ensogl/example/src/dom_symbols.rs index fec452aee4..e9d7ce68a3 100644 --- a/src/rust/ensogl/example/src/dom_symbols.rs +++ b/src/rust/ensogl/example/src/dom_symbols.rs @@ -1,15 +1,15 @@ #![allow(missing_docs)] -use ensogl_core::traits::*; use ensogl_core::prelude::*; +use ensogl_core::traits::*; -use ensogl_core::system::web; -use ensogl_core::system::web::NodeInserter; -use ensogl_core::display::symbol::DomSymbol; +use ensogl_core::display::navigation::navigator::Navigator; use ensogl_core::display::symbol::geometry::Sprite; use ensogl_core::display::symbol::geometry::SpriteSystem; +use ensogl_core::display::symbol::DomSymbol; use ensogl_core::display::world::*; -use ensogl_core::display::navigation::navigator::Navigator; +use ensogl_core::system::web; +use ensogl_core::system::web::NodeInserter; use web::StyleSetter; use nalgebra::Vector2; @@ -21,49 +21,49 @@ use wasm_bindgen::prelude::*; #[allow(clippy::many_single_char_names)] pub fn entry_point_dom_symbols() { web::forward_panic_hook_to_console(); - let world = World::new(&web::get_html_element_by_id("root").unwrap()); - let scene = world.scene(); - let camera = scene.camera(); - let screen = camera.screen(); - let navigator = Navigator::new(scene,&camera); + let world = World::new(&web::get_html_element_by_id("root").unwrap()); + let scene = world.scene(); + let camera = scene.camera(); + let screen = camera.screen(); + let navigator = Navigator::new(scene, &camera); let sprite_system = SpriteSystem::new(&world); world.add_child(&sprite_system); let dom_front_layer = &scene.dom.layers.front; - let dom_back_layer = &scene.dom.layers.back; + let dom_back_layer = &scene.dom.layers.back; - let mut sprites: Vec = default(); + let mut sprites: Vec = default(); let mut css3d_objects: Vec = default(); let count = 10; - for i in 0 .. count { - let x = i as f32; - let width = screen.width * 1.5 / count as f32; + for i in 0..count { + let x = i as f32; + let width = screen.width * 1.5 / count as f32; let height = screen.height; - let y = height / 2.0; + let y = height / 2.0; if i % 2 == 0 { - let height = height * 0.75; - let size = Vector2::new(width, height); + let height = height * 0.75; + let size = Vector2::new(width, height); let position = Vector3::new(width / 1.5 * x + width / 2.0, y, 0.0); - let sprite = sprite_system.new_instance(); + let sprite = sprite_system.new_instance(); sprite.size.set(size); sprite.mod_position(|t| *t = position); sprites.push(sprite); } else { let div = web::create_div(); - div.set_style_or_panic("width" , "100%"); - div.set_style_or_panic("height" , "100%"); + div.set_style_or_panic("width", "100%"); + div.set_style_or_panic("height", "100%"); div.set_inner_html("top-left"); - let size = Vector2::new(width, height); + let size = Vector2::new(width, height); let position = Vector3::new(width / 1.5 * x + width / 2.0, y, 0.0); - let object = DomSymbol::new(&div); + let object = DomSymbol::new(&div); dom_front_layer.manage(&object); world.add_child(&object); - let r = ((x + 0.0) * 16.0) as u8; - let g = ((x + 2.0) * 32.0) as u8; - let b = ((x + 4.0) * 64.0) as u8; + let r = ((x + 0.0) * 16.0) as u8; + let g = ((x + 2.0) * 32.0) as u8; + let b = ((x + 4.0) * 64.0) as u8; let color = iformat!("rgb({r},{g},{b})"); - div.set_style_or_panic("background-color",color); + div.set_style_or_panic("background-color", color); object.dom().append_or_panic(&div); object.set_size(size); @@ -71,23 +71,25 @@ pub fn entry_point_dom_symbols() { css3d_objects.push(object); } } - let layers = vec![dom_front_layer.clone_ref(),dom_back_layer.clone_ref()]; + let layers = vec![dom_front_layer.clone_ref(), dom_back_layer.clone_ref()]; let mut iter_to_change = 0; let mut i = 0; world.keep_alive_forever(); - world.on_frame(move |_| { - let _keep_alive = &navigator; - let _keep_alive = &sprites; - let _keep_alive = &sprite_system; + world + .on_frame(move |_| { + let _keep_alive = &navigator; + let _keep_alive = &sprites; + let _keep_alive = &sprite_system; - if iter_to_change == 0 { - iter_to_change = 50; - i = (i + 1) % 2; - for (j, object) in css3d_objects.iter_mut().enumerate() { - layers[(i + j) % 2].manage(object); + if iter_to_change == 0 { + iter_to_change = 50; + i = (i + 1) % 2; + for (j, object) in css3d_objects.iter_mut().enumerate() { + layers[(i + j) % 2].manage(object); + } } - } - iter_to_change -= 1; - }).forget(); + iter_to_change -= 1; + }) + .forget(); } diff --git a/src/rust/ensogl/example/src/drop_manager.rs b/src/rust/ensogl/example/src/drop_manager.rs index b62c2f1014..ac433cc72b 100644 --- a/src/rust/ensogl/example/src/drop_manager.rs +++ b/src/rust/ensogl/example/src/drop_manager.rs @@ -3,24 +3,24 @@ use enso_prelude::*; -use ensogl_core::frp::web; use ensogl_core::display::world::World; +use ensogl_core::frp::web; +use ensogl_web::drop; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::spawn_local; -use ensogl_web::drop; -fn download_file(file:drop::File) { +fn download_file(file: drop::File) { spawn_local(async move { INFO!("Received file: {file:?}"); loop { match file.read_chunk().await { Ok(Some(chunk)) => { INFO!("Received chunk: {chunk:?}"); - }, + } Ok(None) => { INFO!("All chunks received successfully"); - break - }, + break; + } Err(err) => { ERROR!("Error in receiving chunk promise: {err:?}"); break; @@ -36,25 +36,27 @@ fn download_file(file:drop::File) { pub fn entry_point_drop_manager() { web::forward_panic_hook_to_console(); - let world = World::new(&web::get_html_element_by_id("root").unwrap()); + let world = World::new(&web::get_html_element_by_id("root").unwrap()); let drop_manager = drop::Manager::new(world.scene().dom.root.as_ref()); - let network = enso_frp::Network::new("Debug Scene"); + let network = enso_frp::Network::new("Debug Scene"); enso_frp::extend! { network let file_received = drop_manager.files_received().clone_ref(); eval file_received ([](files) for file in files { download_file(file.clone_ref())}); } let mut loader_hidden = false; - world.on_frame(move |_| { - if !loader_hidden { - web::get_element_by_id("loader").map(|t| { - t.parent_node().map(|p| { - p.remove_child(&t).unwrap() - }) - }).ok(); - loader_hidden = true; - } - }).forget(); + world + .on_frame(move |_| { + if !loader_hidden { + web::get_element_by_id("loader") + .map(|t| { + t.parent_node().map(|p| p.remove_child(&t).unwrap()) + }) + .ok(); + loader_hidden = true; + } + }) + .forget(); std::mem::forget(world); std::mem::forget(network); diff --git a/src/rust/ensogl/example/src/easing_animator.rs b/src/rust/ensogl/example/src/easing_animator.rs index 84839db74d..526bb33dc4 100644 --- a/src/rust/ensogl/example/src/easing_animator.rs +++ b/src/rust/ensogl/example/src/easing_animator.rs @@ -2,106 +2,104 @@ use crate::prelude::*; -use ensogl_core::animation::easing::*; use ensogl_core::animation; -use ensogl_core::system::web::AttributeSetter; +use ensogl_core::animation::easing::*; +use ensogl_core::system::web; use ensogl_core::system::web::create_element; use ensogl_core::system::web::get_element_by_id; +use ensogl_core::system::web::AttributeSetter; use ensogl_core::system::web::NodeInserter; use ensogl_core::system::web::StyleSetter; -use ensogl_core::system::web; use js_sys::Math; use nalgebra::Vector2; use std::ops::Add; use std::ops::Mul; use std::rc::Rc; -use wasm_bindgen::JsCast; use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; use web_sys::CanvasRenderingContext2d; use web_sys::HtmlCanvasElement; use web_sys::HtmlElement; - - // ================== // === SpriteData === // ================== /// Look and feel properties of sprite objects. -#[derive(Clone,Copy,Debug,PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq)] #[allow(missing_docs)] pub struct SpriteData { - pub position : Vector2, - pub size : f64 + pub position: Vector2, + pub size: f64, } impl SpriteData { /// Constructor. - pub fn new(position:Vector2, size:f64) -> Self { - Self {position,size} + pub fn new(position: Vector2, size: f64) -> Self { + Self { position, size } } /// Creates random SpriteData. pub fn random() -> Self { let x = ((Math::random() - 0.5) * 2.0) as f32; let y = ((Math::random() - 0.5) * 2.0) as f32; - let position = Vector2::new(x,y); - let size = Math::random() * 100.0; - Self::new(position,size) + let position = Vector2::new(x, y); + let size = Math::random() * 100.0; + Self::new(position, size) } } impl Mul for SpriteData { type Output = Self; - fn mul(self, rhs:f32) -> Self { + fn mul(self, rhs: f32) -> Self { let position = self.position * rhs; - let size = self.size * rhs as f64; - SpriteData {position,size} + let size = self.size * rhs as f64; + SpriteData { position, size } } } impl Add for SpriteData { type Output = Self; - fn add(self, rhs:Self) -> Self { + fn add(self, rhs: Self) -> Self { let position = self.position + rhs.position; - let size = self.size + rhs.size; - SpriteData {position,size} + let size = self.size + rhs.size; + SpriteData { position, size } } } - - // ============== // === Canvas === // ============== /// A simplified Canvas object used in the EasingAnimator example. -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] pub struct Canvas { - canvas : HtmlCanvasElement, - context : CanvasRenderingContext2d + canvas: HtmlCanvasElement, + context: CanvasRenderingContext2d, } impl Canvas { /// Constructor. - pub fn new(container_id:&str) -> Self { + pub fn new(container_id: &str) -> Self { let canvas = web::create_canvas(); canvas.set_style_or_panic("border", "1px solid black"); - canvas.set_width (256); + canvas.set_width(256); canvas.set_height(256); let context = canvas.get_context("2d").unwrap().unwrap(); - let context : CanvasRenderingContext2d = context.dyn_into().unwrap(); + let context: CanvasRenderingContext2d = context.dyn_into().unwrap(); - let app : HtmlElement = get_element_by_id(container_id).unwrap().dyn_into().unwrap(); + let app: HtmlElement = + get_element_by_id(container_id).unwrap().dyn_into().unwrap(); app.append_or_panic(&canvas); - Self {canvas,context} + Self { canvas, context } } /// Clears the canvas. pub fn clear(&self) { - self.context.clear_rect(0.0,0.0,self.width(),self.height()) + self.context + .clear_rect(0.0, 0.0, self.width(), self.height()) } /// Gets Canvas' width. @@ -115,23 +113,35 @@ impl Canvas { } /// Draw sprite of the provided properties. - pub fn draw_sprite(&self, data:SpriteData, color:&str) { + pub fn draw_sprite(&self, data: SpriteData, color: &str) { let size = (20.0 + data.size) / self.height(); let point = data.position; self.context.save(); self.context.set_fill_style(&color.into()); - self.context.scale(self.width() / 2.0, self.height() / 2.0).ok(); + self.context + .scale(self.width() / 2.0, self.height() / 2.0) + .ok(); self.context.set_line_width(2.0 / self.height()); self.context.translate(1.0, 1.0).ok(); - self.context.fill_rect(point.x as f64 - size / 2.0,point.y as f64 - size / 2.0,size,size); + self.context.fill_rect( + point.x as f64 - size / 2.0, + point.y as f64 - size / 2.0, + size, + size, + ); self.context.restore(); } /// Draw a 2D graph of the provided easing function. - pub fn draw_graphf32>(&self, f:F, color:&str, time_ms:f32) { + pub fn draw_graph f32>( + &self, + f: F, + color: &str, + time_ms: f32, + ) { let time_ms = time_ms as f64; - let width = self.width() - 1.0; - let height = self.height(); + let width = self.width() - 1.0; + let height = self.height(); self.context.set_stroke_style(&color.into()); self.context.begin_path(); @@ -148,47 +158,71 @@ impl Canvas { self.context.stroke(); self.context.set_fill_style(&color.into()); - let width = 8.0 / width; + let width = 8.0 / width; let height = 16.0 / height; let time_s = time_ms / 1000.0; - let x = time_s / 2.0; - let y = f(x as f32) as f64; - self.context.fill_rect(x - width / 2.0, y - height / 2.0, width, height); + let x = time_s / 2.0; + let y = f(x as f32) as f64; + self.context.fill_rect( + x - width / 2.0, + y - height / 2.0, + width, + height, + ); self.context.restore(); } } #[allow(clippy::type_complexity)] struct Sampler { - color : &'static str, - time : f32, - left_canvas : Canvas, - right_canvas : Canvas, - easing_animator : Animatorf32>, Box>, - properties : Rc>, - easing_function : Boxf32>, + color: &'static str, + time: f32, + left_canvas: Canvas, + right_canvas: Canvas, + easing_animator: + Animator f32>, Box>, + properties: Rc>, + easing_function: Box f32>, } impl Sampler { #[allow(trivial_casts)] - fn new(color:&'static str, left_canvas:&Canvas, right_canvas:&Canvas, f:F) -> Self - where F:CloneableFnEasing { - let left_canvas = left_canvas.clone(); - let right_canvas = right_canvas.clone(); - let properties = Rc::new(Cell::new(SpriteData::new(Vector2::new(0.0,0.0),1.0))); - let start = SpriteData::random(); - let end = SpriteData::random(); - let prop = properties.clone(); - let easing_function = Box::new(f.clone()) as Boxf32>; - let easing_function2 = Box::new(f) as Boxf32>; - let animation_cb = Box::new(move |t| prop.set(t)) as Box; - let easing_animator = Animator::new(start,end,easing_function2,animation_cb,()); - let time = 0.0; + fn new( + color: &'static str, + left_canvas: &Canvas, + right_canvas: &Canvas, + f: F, + ) -> Self + where + F: CloneableFnEasing, + { + let left_canvas = left_canvas.clone(); + let right_canvas = right_canvas.clone(); + let properties = + Rc::new(Cell::new(SpriteData::new(Vector2::new(0.0, 0.0), 1.0))); + let start = SpriteData::random(); + let end = SpriteData::random(); + let prop = properties.clone(); + let easing_function = Box::new(f.clone()) as Box f32>; + let easing_function2 = Box::new(f) as Box f32>; + let animation_cb = + Box::new(move |t| prop.set(t)) as Box; + let easing_animator = + Animator::new(start, end, easing_function2, animation_cb, ()); + let time = 0.0; easing_animator.set_duration(2000.0); - Self {color,time,left_canvas,right_canvas,easing_animator,properties,easing_function} + Self { + color, + time, + left_canvas, + right_canvas, + easing_animator, + properties, + easing_function, + } } - fn render(&mut self, time_diff:f32) { + fn render(&mut self, time_diff: f32) { self.time += time_diff; if self.time > 3000.0 { self.time = 0.0; @@ -197,53 +231,63 @@ impl Sampler { animator.set_target_value_no_restart(SpriteData::random()); animator.reset(); } - self.left_canvas.draw_graph(&self.easing_function,self.color,self.time); - self.right_canvas.draw_sprite(self.properties.get(),self.color); + self.left_canvas.draw_graph( + &self.easing_function, + self.color, + self.time, + ); + self.right_canvas + .draw_sprite(self.properties.get(), self.color); } } - - // =============== // === Example === // =============== struct Example { - _animator : animation::Loop> + _animator: animation::Loop>, } impl Example { #[allow(trivial_casts)] - pub fn new - ( name : &str - , ease_in : impl CloneableFnEasing - , ease_out : impl CloneableFnEasing - , ease_in_out : impl CloneableFnEasing + pub fn new( + name: &str, + ease_in: impl CloneableFnEasing, + ease_out: impl CloneableFnEasing, + ease_in_out: impl CloneableFnEasing, ) -> Self { let example = web::create_div(); example.set_attribute_or_panic("id", name); example.set_style_or_panic("margin", "10px"); - let container : HtmlElement = get_element_by_id("examples").unwrap().dyn_into().unwrap(); - let header : HtmlElement = create_element("center").dyn_into().unwrap(); + let container: HtmlElement = + get_element_by_id("examples").unwrap().dyn_into().unwrap(); + let header: HtmlElement = create_element("center").dyn_into().unwrap(); header.set_style_or_panic("background-color", "black"); header.set_style_or_panic("color", "white"); header.set_inner_html(name); example.append_or_panic(&header); container.append_or_panic(&example); - let left_canvas = Canvas::new(name); + let left_canvas = Canvas::new(name); let right_canvas = Canvas::new(name); - let mut sampler1 = Sampler::new("green",&left_canvas,&right_canvas,ease_in); - let mut sampler2 = Sampler::new("blue" ,&left_canvas,&right_canvas,ease_out); - let mut sampler3 = Sampler::new("red" ,&left_canvas,&right_canvas,ease_in_out); - - let _animator = animation::Loop::new(Box::new(move |time_info:animation::TimeInfo| { - left_canvas.clear(); - right_canvas.clear(); - sampler1.render(time_info.frame); - sampler2.render(time_info.frame); - sampler3.render(time_info.frame); - }) as Box); - Self {_animator} + let mut sampler1 = + Sampler::new("green", &left_canvas, &right_canvas, ease_in); + let mut sampler2 = + Sampler::new("blue", &left_canvas, &right_canvas, ease_out); + let mut sampler3 = + Sampler::new("red", &left_canvas, &right_canvas, ease_in_out); + + let _animator = animation::Loop::new(Box::new( + move |time_info: animation::TimeInfo| { + left_canvas.clear(); + right_canvas.clear(); + sampler1.render(time_info.frame); + sampler2.render(time_info.frame); + sampler3.render(time_info.frame); + }, + ) + as Box); + Self { _animator } } } @@ -271,5 +315,7 @@ pub fn entry_point_easing_animator() { container.set_style_or_panic("position", "absolute"); container.set_style_or_panic("top", "0px"); web::body().append_or_panic(&container); - examples![expo,bounce,circ,quad,cubic,quart,quint,sine,back,elastic]; + examples![ + expo, bounce, circ, quad, cubic, quart, quint, sine, back, elastic + ]; } diff --git a/src/rust/ensogl/example/src/glyph_system.rs b/src/rust/ensogl/example/src/glyph_system.rs index e0c0591744..bb20d43a12 100644 --- a/src/rust/ensogl/example/src/glyph_system.rs +++ b/src/rust/ensogl/example/src/glyph_system.rs @@ -5,30 +5,30 @@ use ensogl_core::prelude::*; use ensogl_core::data::color; use ensogl_core::display::world::*; use ensogl_core::system::web; -use ensogl_text_msdf_sys::run_once_initialized; use ensogl_text::typeface::*; +use ensogl_text_msdf_sys::run_once_initialized; use wasm_bindgen::prelude::*; - - /// Main example runner. #[wasm_bindgen] #[allow(dead_code)] pub fn entry_point_glyph_system() { web::forward_panic_hook_to_console(); - run_once_initialized(|| init(&World::new(&web::get_html_element_by_id("root").unwrap()))); + run_once_initialized(|| { + init(&World::new(&web::get_html_element_by_id("root").unwrap())) + }); } -fn init(world:&World) { - let fonts = world.scene().extension::(); - let font = fonts.load("DejaVuSans"); - let glyph_system = glyph::System::new(world.scene(),font); - let height = 32.0; - let color = color::Rgba::new(0.5,0.0,0.0,1.0); - let glyph = glyph_system.new_glyph(); +fn init(world: &World) { + let fonts = world.scene().extension::(); + let font = fonts.load("DejaVuSans"); + let glyph_system = glyph::System::new(world.scene(), font); + let height = 32.0; + let color = color::Rgba::new(0.5, 0.0, 0.0, 1.0); + let glyph = glyph_system.new_glyph(); glyph.set_char('Q'); glyph.set_color(color); - glyph.size.set(Vector2(height,height)); + glyph.size.set(Vector2(height, height)); world.add_child(&glyph_system); world.add_child(&glyph); diff --git a/src/rust/ensogl/example/src/leak.rs b/src/rust/ensogl/example/src/leak.rs index 73e7b3986d..1ed3b830f2 100644 --- a/src/rust/ensogl/example/src/leak.rs +++ b/src/rust/ensogl/example/src/leak.rs @@ -3,8 +3,6 @@ //! drop implementation of `Leak`. Can bue used for examples to keep components alive for the whole //! lifetime of the application. - - // ============ // === Leak === // ============ @@ -13,14 +11,14 @@ /// leaked when the `Leak` is dropped. #[derive(Debug)] pub struct Leak { - value: Option + value: Option, } impl Leak { /// Constructor. The passed value will be prevented from being dropped. This will cause memory /// leaks. - pub fn new(value:T) -> Self { - Self{value:Some(value)} + pub fn new(value: T) -> Self { + Self { value: Some(value) } } /// Return a reference to the wrapped value. diff --git a/src/rust/ensogl/example/src/lib.rs b/src/rust/ensogl/example/src/lib.rs index b31e365e02..4c1f42d292 100644 --- a/src/rust/ensogl/example/src/lib.rs +++ b/src/rust/ensogl/example/src/lib.rs @@ -20,18 +20,16 @@ #![warn(unsafe_code)] #![warn(unused_import_braces)] #![warn(unused_qualifications)] +#![recursion_limit = "1024"] -#![recursion_limit="1024"] - -#[allow(clippy::option_map_unit_fn)] - -mod leak; pub mod animation; pub mod complex_shape_system; pub mod dom_symbols; pub mod drop_manager; pub mod easing_animator; pub mod glyph_system; +#[allow(clippy::option_map_unit_fn)] +mod leak; pub mod list_view; pub mod mouse_events; pub mod scroll_area; @@ -43,6 +41,6 @@ pub mod text_area; /// Common types that should be visible across the whole crate. pub mod prelude { - pub use ensogl_core::prelude::*; pub use super::leak::*; + pub use ensogl_core::prelude::*; } diff --git a/src/rust/ensogl/example/src/list_view.rs b/src/rust/ensogl/example/src/list_view.rs index e0552242cb..96175ecab5 100644 --- a/src/rust/ensogl/example/src/list_view.rs +++ b/src/rust/ensogl/example/src/list_view.rs @@ -2,18 +2,16 @@ use crate::prelude::*; -use ensogl_core::system::web; use ensogl_core::application::Application; use ensogl_core::display::object::ObjectOps; -use ensogl_text_msdf_sys::run_once_initialized; -use ensogl_gui_components::list_view; -use logger::TraceLogger as Logger; -use wasm_bindgen::prelude::*; use ensogl_core::display::Scene; +use ensogl_core::system::web; +use ensogl_gui_components::list_view; use ensogl_text::buffer::data::unit::Bytes; +use ensogl_text_msdf_sys::run_once_initialized; use ensogl_theme as theme; - - +use logger::TraceLogger as Logger; +use wasm_bindgen::prelude::*; // =================== // === Entry Point === @@ -26,67 +24,87 @@ pub fn entry_point_list_view() { web::forward_panic_hook_to_console(); web::set_stack_trace_limit(); run_once_initialized(|| { - let app = Application::new(&web::get_html_element_by_id("root").unwrap()); + let app = + Application::new(&web::get_html_element_by_id("root").unwrap()); init(&app); mem::forget(app); }); } - - // ==================== // === Mock Entries === // ==================== -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] struct MockEntries { - logger : Logger, - scene : Scene, - entries_count : usize, + logger: Logger, + scene: Scene, + entries_count: usize, } impl MockEntries { - fn new(app:&Application, entries_count:usize) -> Self { - let logger = Logger::new("MockEntries"); - let scene = app.display.scene().clone_ref(); - Self {logger,scene,entries_count} + fn new(app: &Application, entries_count: usize) -> Self { + let logger = Logger::new("MockEntries"); + let scene = app.display.scene().clone_ref(); + Self { + logger, + scene, + entries_count, + } } } -impl list_view::entry::ModelProvider for MockEntries { - fn entry_count(&self) -> usize { self.entries_count } +impl list_view::entry::ModelProvider + for MockEntries +{ + fn entry_count(&self) -> usize { + self.entries_count + } - fn get(&self, id:usize) -> Option { + fn get( + &self, + id: usize, + ) -> Option { if id >= self.entries_count { None } else { let label = iformat!("Entry {id}"); - let highlighted = if id == 10 { vec![(Bytes(1)..Bytes(3)).into()] } else { vec![] }; - Some(list_view::entry::GlyphHighlightedLabelModel {label,highlighted}) + let highlighted = if id == 10 { + vec![(Bytes(1)..Bytes(3)).into()] + } else { + vec![] + }; + Some(list_view::entry::GlyphHighlightedLabelModel { + label, + highlighted, + }) } } } - - // ======================== // === Init Application === // ======================== -fn init(app:&Application) { +fn init(app: &Application) { theme::builtin::dark::register(&app); theme::builtin::light::register(&app); theme::builtin::light::enable(&app); let list_view = app.new_view::>(); - let provider = list_view::entry::AnyModelProvider::new(MockEntries::new(app,1000)); - list_view.frp.resize(Vector2(100.0,160.0)); + let provider = + list_view::entry::AnyModelProvider::new(MockEntries::new(app, 1000)); + list_view.frp.resize(Vector2(100.0, 160.0)); list_view.frp.set_entries(provider); app.display.add_child(&list_view); // FIXME[WD]: This should not be needed after text gets proper depth-handling. - app.display.scene().layers.below_main.add_exclusive(&list_view); + app.display + .scene() + .layers + .below_main + .add_exclusive(&list_view); - let logger : Logger = Logger::new("SelectDebugScene"); + let logger: Logger = Logger::new("SelectDebugScene"); let network = enso_frp::Network::new("test"); enso_frp::extend! {network eval list_view.chosen_entry([logger](entry) { diff --git a/src/rust/ensogl/example/src/mouse_events.rs b/src/rust/ensogl/example/src/mouse_events.rs index 691cf28b97..9c59613afa 100644 --- a/src/rust/ensogl/example/src/mouse_events.rs +++ b/src/rust/ensogl/example/src/mouse_events.rs @@ -3,7 +3,6 @@ use ensogl_core::prelude::*; use wasm_bindgen::prelude::*; -use ensogl_core::system::web; use ensogl_core::application; use ensogl_core::application::Application; use ensogl_core::data::color; @@ -12,6 +11,7 @@ use ensogl_core::display; use ensogl_core::display::navigation::navigator::Navigator; use ensogl_core::display::object::ObjectOps; use ensogl_core::display::shape::*; +use ensogl_core::system::web; use enso_frp as frp; use ensogl_text_msdf_sys::run_once_initialized; @@ -30,34 +30,35 @@ mod shape { } } - - // ============= // === Model === // ============= -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] struct Model { - app : Application, - logger : DefaultTraceLogger, - display_object : display::object::Instance, - shape : shape::View, + app: Application, + logger: DefaultTraceLogger, + display_object: display::object::Instance, + shape: shape::View, } impl Model { - fn new(app:&Application) -> Self { - let app = app.clone_ref(); - let logger = DefaultTraceLogger::new("Button"); + fn new(app: &Application) -> Self { + let app = app.clone_ref(); + let logger = DefaultTraceLogger::new("Button"); let display_object = display::object::Instance::new(&logger); - let shape = shape::View::new(&logger); + let shape = shape::View::new(&logger); shape.size.set(Vector2::new(100.0, 100.0)); display_object.add_child(&shape); - Self{app,logger,display_object,shape} + Self { + app, + logger, + display_object, + shape, + } } } - - // =========== // === FRP === // =========== @@ -69,24 +70,22 @@ ensogl_core::define_endpoints! { [TRACE_ALL] } } - - // ============ // === View === // ============ -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] struct View { - frp : Frp, - model : Model, + frp: Frp, + model: Model, } impl View { /// Constructor. pub fn new(app: &Application) -> Self { - let frp = Frp::new(); - let model = Model::new(app); - let events = &model.shape.events; + let frp = Frp::new(); + let model = Model::new(app); + let events = &model.shape.events; let network = &events.network; frp::extend! { network // FIXME [mwu] Currently only `mouse_over` and `mouse_out` events are delivered. @@ -98,30 +97,41 @@ impl View { trace model.shape.events.on_drop; } - Self {frp,model } + Self { frp, model } } } impl display::Object for View { - fn display_object(&self) -> &display::object::Instance { &self.model.display_object } + fn display_object(&self) -> &display::object::Instance { + &self.model.display_object + } } impl Deref for View { type Target = Frp; - fn deref(&self) -> &Self::Target { &self.frp } + fn deref(&self) -> &Self::Target { + &self.frp + } } impl application::command::FrpNetworkProvider for View { - fn network(&self) -> &frp::Network { &self.frp.network } + fn network(&self) -> &frp::Network { + &self.frp.network + } } impl application::View for View { - fn label() -> &'static str { "Circul" } - fn new(app:&Application) -> Self { View::new(app) } - fn app(&self) -> &Application { &self.model.app } + fn label() -> &'static str { + "Circul" + } + fn new(app: &Application) -> Self { + View::new(app) + } + fn app(&self) -> &Application { + &self.model.app + } } - // =================== // === Entry Point === // =================== @@ -132,15 +142,16 @@ impl application::View for View { pub fn entry_point_mouse_events() { web::forward_panic_hook_to_console(); run_once_initialized(|| { - let app = Application::new(&web::get_html_element_by_id("root").unwrap()); + let app = + Application::new(&web::get_html_element_by_id("root").unwrap()); - let shape:View = app.new_view(); + let shape: View = app.new_view(); shape.model.shape.size.set(Vector2::new(300.0, 300.0)); app.display.add_child(&shape); - let scene = app.display.scene(); - let camera = scene.camera().clone_ref(); - let navigator = Navigator::new(scene,&camera); + let scene = app.display.scene(); + let camera = scene.camera().clone_ref(); + let navigator = Navigator::new(scene, &camera); std::mem::forget(shape); std::mem::forget(navigator); diff --git a/src/rust/ensogl/example/src/scroll_area.rs b/src/rust/ensogl/example/src/scroll_area.rs index e4d7e4fea6..7ab9471159 100644 --- a/src/rust/ensogl/example/src/scroll_area.rs +++ b/src/rust/ensogl/example/src/scroll_area.rs @@ -6,16 +6,14 @@ use wasm_bindgen::prelude::*; use ensogl_core::application::Application; use ensogl_core::data::color; use ensogl_core::display::object::ObjectOps; -use ensogl_core::system::web; -use ensogl_text_msdf_sys::run_once_initialized; -use ensogl_theme as theme; -use ensogl_gui_components::scroll_area::ScrollArea; -use ensogl_core::display::shape::{Circle, Rect, ShapeSystem}; use ensogl_core::display::shape::PixelDistance; use ensogl_core::display::shape::ShapeOps; +use ensogl_core::display::shape::{Circle, Rect, ShapeSystem}; use ensogl_core::display::Sprite; - - +use ensogl_core::system::web; +use ensogl_gui_components::scroll_area::ScrollArea; +use ensogl_text_msdf_sys::run_once_initialized; +use ensogl_theme as theme; // =================== // === Entry Point === @@ -27,60 +25,57 @@ pub fn entry_point_scroll_area() { web::forward_panic_hook_to_console(); web::set_stack_trace_limit(); run_once_initialized(|| { - let app = Application::new(&web::get_html_element_by_id("root").unwrap()); + let app = + Application::new(&web::get_html_element_by_id("root").unwrap()); init(&app); mem::forget(app); }); } - - // ======================== // === Init Application === // ======================== -fn init(app:&Application) { +fn init(app: &Application) { theme::builtin::dark::register(&app); theme::builtin::light::register(&app); theme::builtin::light::enable(&app); let scene = app.display.scene(); - scene.camera().set_position_xy(Vector2(100.0,-100.0)); - + scene.camera().set_position_xy(Vector2(100.0, -100.0)); // === Background === - let background_color = color::Rgba::new(0.9,0.9,0.9,1.0); - let background_size = (200.px(), 200.px()); - let background_shape = Rect(background_size).corners_radius(5.5.px()).fill(background_color); - let background_system = ShapeSystem::new(scene,background_shape); + let background_color = color::Rgba::new(0.9, 0.9, 0.9, 1.0); + let background_size = (200.px(), 200.px()); + let background_shape = Rect(background_size) + .corners_radius(5.5.px()) + .fill(background_color); + let background_system = ShapeSystem::new(scene, background_shape); let background: Sprite = background_system.new_instance(); scene.add_child(&background); - background.size.set(Vector2::new(200.0,200.0)); + background.size.set(Vector2::new(200.0, 200.0)); background.set_position_x(100.0); background.set_position_y(-100.0); std::mem::forget(background); - // === Scroll Area === let scroll_area = ScrollArea::new(app); app.display.add_child(&scroll_area); - scroll_area.resize(Vector2(200.0,200.0)); + scroll_area.resize(Vector2(200.0, 200.0)); scroll_area.set_content_width(300.0); scroll_area.set_content_height(1000.0); - // === Content === - let sprite_system = ShapeSystem::new(scene,&Circle(50.px())); + let sprite_system = ShapeSystem::new(scene, &Circle(50.px())); let sprite: Sprite = sprite_system.new_instance(); scroll_area.content.add_child(&sprite); - sprite.size.set(Vector2::new(100.0,100.0)); + sprite.size.set(Vector2::new(100.0, 100.0)); sprite.set_position_x(100.0); sprite.set_position_y(-100.0); std::mem::forget(sprite); - std::mem::forget(scroll_area); } diff --git a/src/rust/ensogl/example/src/shape_system.rs b/src/rust/ensogl/example/src/shape_system.rs index d8a804590b..caf3fcd3bc 100644 --- a/src/rust/ensogl/example/src/shape_system.rs +++ b/src/rust/ensogl/example/src/shape_system.rs @@ -2,16 +2,14 @@ use ensogl_core::prelude::*; +use ensogl_core::data::color; use ensogl_core::display::navigation::navigator::Navigator; -use ensogl_core::system::web; -use wasm_bindgen::prelude::*; use ensogl_core::display::object::ObjectOps; use ensogl_core::display::shape::ShapeSystem; -use ensogl_core::display::world::*; use ensogl_core::display::shape::*; -use ensogl_core::data::color; - - +use ensogl_core::display::world::*; +use ensogl_core::system::web; +use wasm_bindgen::prelude::*; // ============== // === Shapes === @@ -19,17 +17,15 @@ use ensogl_core::data::color; /// The shape definition. pub fn shape() -> AnyShape { - let circle1 = Circle(50.px()); - let circle_bg = circle1.translate_x(-(50.0.px())); + let circle1 = Circle(50.px()); + let circle_bg = circle1.translate_x(-(50.0.px())); let circle_sub = circle1.translate_y(-(50.0.px())); - let rect = Rect((100.0.px(),100.0.px())); - let shape = circle_bg + rect - circle_sub; - let shape = shape.fill(color::Rgb::new(1.0,0.0,0.0)); + let rect = Rect((100.0.px(), 100.0.px())); + let shape = circle_bg + rect - circle_sub; + let shape = shape.fill(color::Rgb::new(1.0, 0.0, 0.0)); shape.into() } - - // =================== // === Entry Point === // =================== @@ -40,12 +36,12 @@ pub fn shape() -> AnyShape { pub fn entry_point_shape_system() { web::forward_panic_hook_to_console(); - let world = World::new(&web::get_html_element_by_id("root").unwrap()); - let scene = world.scene(); - let camera = scene.camera().clone_ref(); - let navigator = Navigator::new(scene,&camera); - let sprite_system = ShapeSystem::new(&world,&shape()); - let sprite = sprite_system.new_instance(); + let world = World::new(&web::get_html_element_by_id("root").unwrap()); + let scene = world.scene(); + let camera = scene.camera().clone_ref(); + let navigator = Navigator::new(scene, &camera); + let sprite_system = ShapeSystem::new(&world, &shape()); + let sprite = sprite_system.new_instance(); sprite.size.set(Vector2::new(300.0, 300.0)); sprite.mod_position(|t| *t = Vector3::new(50.0, 50.0, 0.0)); @@ -53,8 +49,10 @@ pub fn entry_point_shape_system() { world.add_child(&sprite_system); world.keep_alive_forever(); - world.on_frame(move |_time| { - let _keep_alive = &sprite; - let _keep_alive = &navigator; - }).forget(); + world + .on_frame(move |_time| { + let _keep_alive = &sprite; + let _keep_alive = &navigator; + }) + .forget(); } diff --git a/src/rust/ensogl/example/src/slider.rs b/src/rust/ensogl/example/src/slider.rs index 72eac2af88..e227263f91 100644 --- a/src/rust/ensogl/example/src/slider.rs +++ b/src/rust/ensogl/example/src/slider.rs @@ -7,13 +7,11 @@ use ensogl_core::application::Application; use ensogl_core::data::color; use ensogl_core::display::object::ObjectOps; use ensogl_core::system::web; -use ensogl_gui_components::selector::Bounds; use ensogl_gui_components::selector; +use ensogl_gui_components::selector::Bounds; use ensogl_text_msdf_sys::run_once_initialized; use ensogl_theme as theme; - - // =================== // === Entry Point === // =================== @@ -25,33 +23,32 @@ pub fn entry_point_slider() { web::forward_panic_hook_to_console(); web::set_stack_trace_limit(); run_once_initialized(|| { - let app = Application::new(&web::get_html_element_by_id("root").unwrap()); + let app = + Application::new(&web::get_html_element_by_id("root").unwrap()); init(&app); mem::forget(app); }); } -fn make_number_picker(app:&Application) -> Leak { +fn make_number_picker(app: &Application) -> Leak { let slider = app.new_view::(); - slider.frp.resize(Vector2(200.0,50.0)); + slider.frp.resize(Vector2(200.0, 50.0)); app.display.add_child(&slider); Leak::new(slider) } -fn make_range_picker(app:&Application) -> Leak { +fn make_range_picker(app: &Application) -> Leak { let slider = app.new_view::(); - slider.frp.resize(Vector2(400.0,50.0)); + slider.frp.resize(Vector2(400.0, 50.0)); app.display.add_child(&slider); Leak::new(slider) } - - // ======================== // === Init Application === // ======================== -fn init(app:&Application) { +fn init(app: &Application) { theme::builtin::dark::register(&app); theme::builtin::light::register(&app); theme::builtin::light::enable(&app); @@ -60,19 +57,33 @@ fn init(app:&Application) { slider1.inner().frp.allow_click_selection(true); let slider2 = make_number_picker(app); - slider2.inner().frp.resize(Vector2(400.0,50.0)); - slider2.inner().frp.set_bounds.emit(Bounds::new(-100.0,100.0)); + slider2.inner().frp.resize(Vector2(400.0, 50.0)); + slider2 + .inner() + .frp + .set_bounds + .emit(Bounds::new(-100.0, 100.0)); slider2.inner().set_position_y(50.0); - slider2.inner().frp.use_overflow_bounds(Bounds::new(-150.0,200.0)); + slider2 + .inner() + .frp + .use_overflow_bounds(Bounds::new(-150.0, 200.0)); slider2.inner().frp.set_caption(Some("Value:".to_string())); let slider3 = make_range_picker(app); slider3.inner().set_position_y(-100.0); - slider3.inner().set_track_color(color::Rgba::new(0.0,0.80,0.80,1.0)); + slider3 + .inner() + .set_track_color(color::Rgba::new(0.0, 0.80, 0.80, 1.0)); let slider4 = make_range_picker(app); slider4.inner().set_position_y(-200.0); - slider4.inner().frp.use_overflow_bounds(Bounds::new(-2.0,3.0)); + slider4 + .inner() + .frp + .use_overflow_bounds(Bounds::new(-2.0, 3.0)); slider4.inner().frp.set_caption(Some("Caption".to_string())); - slider4.inner().set_track_color(color::Rgba::new(0.5,0.70,0.70,1.0)); + slider4 + .inner() + .set_track_color(color::Rgba::new(0.5, 0.70, 0.70, 1.0)); } diff --git a/src/rust/ensogl/example/src/sprite_system.rs b/src/rust/ensogl/example/src/sprite_system.rs index a824a69d78..dc4c9d79e4 100644 --- a/src/rust/ensogl/example/src/sprite_system.rs +++ b/src/rust/ensogl/example/src/sprite_system.rs @@ -6,39 +6,40 @@ use wasm_bindgen::prelude::*; use ensogl_core::display::navigation::navigator::Navigator; use ensogl_core::display::symbol::geometry::SpriteSystem; use ensogl_core::display::world::*; -use ensogl_core::system::web::forward_panic_hook_to_console; use ensogl_core::system::web; - +use ensogl_core::system::web::forward_panic_hook_to_console; #[wasm_bindgen] #[allow(dead_code)] pub fn entry_point_sprite_system() { forward_panic_hook_to_console(); - let world = World::new(&web::get_html_element_by_id("root").unwrap()); - let scene = world.scene(); - let camera = scene.camera().clone_ref(); - let navigator = Navigator::new(scene,&camera); + let world = World::new(&web::get_html_element_by_id("root").unwrap()); + let scene = world.scene(); + let camera = scene.camera().clone_ref(); + let navigator = Navigator::new(scene, &camera); let sprite_system = SpriteSystem::new(&world); - let sprite2 = sprite_system.new_instance(); - let sprite1 = sprite_system.new_instance(); - sprite1.size.set(Vector2::new(15.0,15.0)); - sprite2.size.set(Vector2::new(15.0,15.0)); + let sprite2 = sprite_system.new_instance(); + let sprite1 = sprite_system.new_instance(); + sprite1.size.set(Vector2::new(15.0, 15.0)); + sprite2.size.set(Vector2::new(15.0, 15.0)); scene.add_child(&sprite_system); world.keep_alive_forever(); let mut i = 0; - world.on_frame(move |_| { - i += 1; - let _keep_alive = &navigator; - let _keep_alive = &sprite1; - let _keep_alive = &sprite2; - let _keep_alive = &sprite_system; - if i <= 100 { - sprite1.mod_position(|p| p.x += 1.0); - sprite2.mod_position(|p| p.y += 1.0); - } - }).forget(); + world + .on_frame(move |_| { + i += 1; + let _keep_alive = &navigator; + let _keep_alive = &sprite1; + let _keep_alive = &sprite2; + let _keep_alive = &sprite_system; + if i <= 100 { + sprite1.mod_position(|p| p.x += 1.0); + sprite2.mod_position(|p| p.y += 1.0); + } + }) + .forget(); } diff --git a/src/rust/ensogl/example/src/sprite_system_benchmark.rs b/src/rust/ensogl/example/src/sprite_system_benchmark.rs index 58a664b668..301f2bee30 100644 --- a/src/rust/ensogl/example/src/sprite_system_benchmark.rs +++ b/src/rust/ensogl/example/src/sprite_system_benchmark.rs @@ -2,84 +2,92 @@ use ensogl_core::traits::*; +use ensogl_core::animation; use ensogl_core::display::camera::Camera2d; use ensogl_core::display::navigation::navigator::Navigator; use ensogl_core::display::symbol::geometry::Sprite; use ensogl_core::display::symbol::geometry::SpriteSystem; use ensogl_core::display::world::*; use ensogl_core::prelude::*; -use ensogl_core::system::web::forward_panic_hook_to_console; use ensogl_core::system::web; -use ensogl_core::animation; +use ensogl_core::system::web::forward_panic_hook_to_console; use nalgebra::Vector2; use nalgebra::Vector3; use wasm_bindgen::prelude::*; - #[wasm_bindgen] #[allow(dead_code)] pub fn entry_point_sprite_system_benchmark() { forward_panic_hook_to_console(); - let world = World::new(&web::get_html_element_by_id("root").unwrap()); - let scene = world.scene(); - let camera = scene.camera().clone_ref(); - let navigator = Navigator::new(scene,&camera); + let world = World::new(&web::get_html_element_by_id("root").unwrap()); + let scene = world.scene(); + let camera = scene.camera().clone_ref(); + let navigator = Navigator::new(scene, &camera); let sprite_system = SpriteSystem::new(&world); - let sprite1 = sprite_system.new_instance(); - sprite1.size.set(Vector2::new(10.0,10.0)); - sprite1.mod_position(|t| *t = Vector3::new(5.0,5.0,0.0)); + let sprite1 = sprite_system.new_instance(); + sprite1.size.set(Vector2::new(10.0, 10.0)); + sprite1.mod_position(|t| *t = Vector3::new(5.0, 5.0, 0.0)); scene.add_child(&sprite_system); let mut sprites: Vec = default(); let count = 100; - for _ in 0 .. count { + for _ in 0..count { let sprite = sprite_system.new_instance(); - sprite.size.set(Vector2::new(1.0,1.0)); + sprite.size.set(Vector2::new(1.0, 1.0)); sprites.push(sprite); } world.keep_alive_forever(); - let mut iter:i32 = 0; + let mut iter: i32 = 0; let mut i = 0; - world.on_frame(move |time| { - i += 1; - if i <= 100 { - sprite1.mod_position(|p| p.x += 1.0); - } - let _keep_alive = &camera; - let _keep_alive = &iter; - let _keep_alive = &sprite1; - let _keep_alive = &sprites; - let _keep_alive = &sprite_system; - let _keep_alive = &navigator; - // FIXME: these logs crash gui after some time! - - // println!("sprite count: {:?}",sprites.len()); - // println!("sprite_system is visible? {:?}",sprite_system.is_visible()); - // println!("sprite[5] is visible? {:?}",sprites[5].is_visible()); - - on_frame(&camera,time,&mut iter,&sprite1,&mut sprites,&sprite_system) - }).forget(); + world + .on_frame(move |time| { + i += 1; + if i <= 100 { + sprite1.mod_position(|p| p.x += 1.0); + } + let _keep_alive = &camera; + let _keep_alive = &iter; + let _keep_alive = &sprite1; + let _keep_alive = &sprites; + let _keep_alive = &sprite_system; + let _keep_alive = &navigator; + // FIXME: these logs crash gui after some time! + + // println!("sprite count: {:?}",sprites.len()); + // println!("sprite_system is visible? {:?}",sprite_system.is_visible()); + // println!("sprite[5] is visible? {:?}",sprites[5].is_visible()); + + on_frame( + &camera, + time, + &mut iter, + &sprite1, + &mut sprites, + &sprite_system, + ) + }) + .forget(); } #[allow(clippy::too_many_arguments)] #[allow(clippy::many_single_char_names)] -pub fn on_frame -( camera : &Camera2d -, time : animation::TimeInfo -, iter : &mut i32 -, sprite1 : &Sprite -, sprites : &mut Vec -, sprite_system : &SpriteSystem +pub fn on_frame( + camera: &Camera2d, + time: animation::TimeInfo, + iter: &mut i32, + sprite1: &Sprite, + sprites: &mut Vec, + sprite_system: &SpriteSystem, ) { *iter += 1; - let cycle_duration = 300; - let pause_duration = 100; + let cycle_duration = 300; + let pause_duration = 100; let sprite_diff_per_cycle = 100; let mut frozen = false; @@ -87,7 +95,7 @@ pub fn on_frame if *iter < cycle_duration { for _ in 0..sprite_diff_per_cycle { let sprite = sprite_system.new_instance(); - sprite.size.set(Vector2::new(1.0,1.0)); + sprite.size.set(Vector2::new(1.0, 1.0)); sprites.push(sprite); } } else if *iter < pause_duration + cycle_duration { @@ -122,7 +130,11 @@ pub fn on_frame y += (z * 1.25 + t * 2.00).cos() * 0.5; z += (x * 1.25 + t * 3.25).cos() * 0.5; - let position = Vector3::new(x * 150.0 + half_width - 75.0, y * 150.0 + half_height - 75.0, z * 150.0); + let position = Vector3::new( + x * 150.0 + half_width - 75.0, + y * 150.0 + half_height - 75.0, + z * 150.0, + ); sprite.set_position(position); } } diff --git a/src/rust/ensogl/example/src/text_area.rs b/src/rust/ensogl/example/src/text_area.rs index 10ca2d1689..55f2dffea1 100644 --- a/src/rust/ensogl/example/src/text_area.rs +++ b/src/rust/ensogl/example/src/text_area.rs @@ -2,13 +2,12 @@ use ensogl_core::prelude::*; +use ensogl_core::application::Application; +use ensogl_core::display::navigation::navigator::Navigator; use ensogl_core::system::web; +use ensogl_text::Area; use ensogl_text_msdf_sys::run_once_initialized; use wasm_bindgen::prelude::*; -use ensogl_core::application::Application; -use ensogl_text::Area; -use ensogl_core::display::navigation::navigator::Navigator; - /// Main example runner. #[wasm_bindgen] @@ -17,13 +16,14 @@ pub fn entry_point_text_area() { web::forward_panic_hook_to_console(); web::set_stack_trace_limit(); run_once_initialized(|| { - let app = Application::new(&web::get_html_element_by_id("root").unwrap()); + let app = + Application::new(&web::get_html_element_by_id("root").unwrap()); init(&app); mem::forget(app); }); } -fn init(app:&Application) { +fn init(app: &Application) { let area = app.new_view::(); area.set_position_x(-100.0); area.set_content("Et Eärello Endorenna utúlien.\nSinome maruvan ar Hildinyar tenn' Ambar-metta"); @@ -31,13 +31,15 @@ fn init(app:&Application) { area.hover(); area.set_cursor_at_end(); - let scene = app.display.scene(); - let navigator = Navigator::new(scene,&scene.camera()); + let scene = app.display.scene(); + let navigator = Navigator::new(scene, &scene.camera()); app.display.scene().add_child(&area); let keep = Some(area); - app.display.on_frame(move |_frame| { - let _ = &keep; - }).forget(); + app.display + .on_frame(move |_frame| { + let _ = &keep; + }) + .forget(); std::mem::forget(navigator); } diff --git a/src/rust/ensogl/lib/components/src/component.rs b/src/rust/ensogl/lib/components/src/component.rs index fde2e3d4f0..1792d9e83e 100644 --- a/src/rust/ensogl/lib/components/src/component.rs +++ b/src/rust/ensogl/lib/components/src/component.rs @@ -9,13 +9,11 @@ use crate::prelude::*; use enso_frp as frp; -use ensogl_core::application::Application; -use ensogl_core::application::command::CommandApi; use ensogl_core::application; -use ensogl_core::display::shape::*; +use ensogl_core::application::command::CommandApi; +use ensogl_core::application::Application; use ensogl_core::display; - - +use ensogl_core::display::shape::*; // ============= // === Model === @@ -26,11 +24,9 @@ use ensogl_core::display; /// `Component`. pub trait Model { /// Constructor. - fn new(app:&Application) -> Self; + fn new(app: &Application) -> Self; } - - // =========== // === FRP === // =========== @@ -38,56 +34,59 @@ pub trait Model { /// Frp that can be used in a Component. The FRP requires an initializer that will be called during /// the construction of the component. `Default` + `CommandApi` are usually implemented when using /// the `ensogl_core::define_endpoints!` macro to create an FRP API. -pub trait Frp : Default + CommandApi { +pub trait Frp: Default + CommandApi { /// Frp initializer. - fn init(&self, app:&Application, model:&Model, style:&StyleWatchFrp); + fn init(&self, app: &Application, model: &Model, style: &StyleWatchFrp); } - - // ================= // === Component === // ================= /// Base struct for UI components in EnsoGL. Contains the Data/Shape model and the FPR exposing its /// behaviour. -#[derive(CloneRef,Debug,Derivative)] -#[derivative(Clone(bound=""))] -pub struct Component { +#[derive(CloneRef, Debug, Derivative)] +#[derivative(Clone(bound = ""))] +pub struct Component { /// Public FRP api of the Component. - pub frp : Rc, - model : Rc, + pub frp: Rc, + model: Rc, /// Reference to the application the Component belongs to. Generally required for implementing /// `application::View` and initialising the `Mode`l and `Frp` and thus provided by the /// `Component`. - pub app : Application, + pub app: Application, } -impl> Component { +impl> Component { /// Constructor. - pub fn new(app:&Application) -> Self { - let app = app.clone_ref(); + pub fn new(app: &Application) -> Self { + let app = app.clone_ref(); let model = Rc::new(M::new(&app)); - let frp = F::default(); + let frp = F::default(); let style = StyleWatchFrp::new(&app.display.scene().style_sheet); - frp.init(&app,&model,&style); - let frp = Rc::new(frp); - Self{frp,model,app} + frp.init(&app, &model, &style); + let frp = Rc::new(frp); + Self { frp, model, app } } } -impl display::Object for Component { +impl display::Object for Component { fn display_object(&self) -> &display::object::Instance { self.model.display_object() } } -impl> Deref for Component { +impl> Deref for Component { type Target = F; - fn deref(&self) -> &Self::Target { &self.frp } + fn deref(&self) -> &Self::Target { + &self.frp + } } -impl application::command::FrpNetworkProvider -for Component { - fn network(&self) -> &frp::Network { self.frp.network() } +impl + application::command::FrpNetworkProvider for Component +{ + fn network(&self) -> &frp::Network { + self.frp.network() + } } diff --git a/src/rust/ensogl/lib/components/src/drop_down_menu.rs b/src/rust/ensogl/lib/components/src/drop_down_menu.rs index 10e24f152d..28fce3d899 100644 --- a/src/rust/ensogl/lib/components/src/drop_down_menu.rs +++ b/src/rust/ensogl/lib/components/src/drop_down_menu.rs @@ -6,26 +6,23 @@ use crate::list_view::entry::ModelProvider; use enso_frp as frp; use enso_frp; -use ensogl_core::DEPRECATED_Animation; use ensogl_core::application::Application; use ensogl_core::data::color; -use ensogl_core::display::shape::*; -use ensogl_core::display::shape::primitive::StyleWatch; use ensogl_core::display; +use ensogl_core::display::shape::primitive::StyleWatch; +use ensogl_core::display::shape::*; +use ensogl_core::DEPRECATED_Animation; use ensogl_text as text; use ensogl_theme as theme; - // ================= // === Constants === // ================= /// Invisible dummy color to catch hover events. -const HOVER_COLOR : color::Rgba = color::Rgba::new(1.0,0.0,0.0,0.000_001); +const HOVER_COLOR: color::Rgba = color::Rgba::new(1.0, 0.0, 0.0, 0.000_001); /// The width of the visualisation selection menu. -const MENU_WIDTH : f32 = 180.0; - - +const MENU_WIDTH: f32 = 180.0; // ============== // === Shapes === @@ -66,8 +63,6 @@ pub mod chooser_hover_area { } } - - // =========== // === FRP === // =========== @@ -90,8 +85,6 @@ ensogl_core::define_endpoints! { } } - - // ============= // === Model === // ============= @@ -99,34 +92,44 @@ ensogl_core::define_endpoints! { /// A type of Entry used in DropDownMenu's ListView. pub type Entry = list_view::entry::Label; -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] struct Model { - logger : Logger, - app : Application, - display_object : display::object::Instance, + logger: Logger, + app: Application, + display_object: display::object::Instance, - icon : arrow::View, - icon_overlay : chooser_hover_area::View, + icon: arrow::View, + icon_overlay: chooser_hover_area::View, - label : text::Area, - selection_menu : list_view::ListView, + label: text::Area, + selection_menu: list_view::ListView, // `SingleMaskedProvider` allows us to hide the selected element. - content : RefCell>>, + content: RefCell>>, } impl Model { - fn new(app:&Application) -> Self { - let logger = Logger::new("drop_down_menu"); - let app = app.clone_ref(); + fn new(app: &Application) -> Self { + let logger = Logger::new("drop_down_menu"); + let app = app.clone_ref(); let display_object = display::object::Instance::new(&logger); - let icon = arrow::View::new(&logger); - let icon_overlay = chooser_hover_area::View::new(&logger); + let icon = arrow::View::new(&logger); + let icon_overlay = chooser_hover_area::View::new(&logger); let selection_menu = list_view::ListView::new(&app); - let label = app.new_view::(); - let content = default(); - - Self{logger,app,display_object,icon,icon_overlay,label,selection_menu,content}.init() + let label = app.new_view::(); + let content = default(); + + Self { + logger, + app, + display_object, + icon, + icon_overlay, + label, + selection_menu, + content, + } + .init() } fn init(self) -> Self { @@ -138,7 +141,7 @@ impl Model { self } - fn set_label(&self, label:&str) { + fn set_label(&self, label: &str) { self.label.set_cursor(&default()); self.label.select_all(); self.label.insert(label); @@ -153,8 +156,10 @@ impl Model { self.selection_menu.unset_parent() } - fn get_content_item - (&self, id:Option) -> Option<::Model> { + fn get_content_item( + &self, + id: Option, + ) -> Option<::Model> { self.content.borrow().as_ref()?.get(id?) } @@ -166,7 +171,7 @@ impl Model { /// Item list [A, B, C] /// Unmasked index [0, 1, 2] /// Masked indices [0, na, 1] - fn get_unmasked_index(&self, ix:Option) -> Option { + fn get_unmasked_index(&self, ix: Option) -> Option { Some(self.content.borrow().as_ref()?.unmasked_index(ix?)) } } @@ -177,40 +182,40 @@ impl display::Object for Model { } } - - // ============================ // === VisualisationChooser === // ============================ /// UI entity that shows a button that opens a list of visualisations that can be selected from. #[allow(missing_docs)] -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] pub struct DropDownMenu { - model : Rc, - pub frp : Frp, + model: Rc, + pub frp: Frp, } impl Deref for DropDownMenu { type Target = Frp; - fn deref(&self) -> &Self::Target { &self.frp } + fn deref(&self) -> &Self::Target { + &self.frp + } } impl DropDownMenu { /// Constructor. - pub fn new(app:&Application) -> Self { - let frp = Frp::new(); + pub fn new(app: &Application) -> Self { + let frp = Frp::new(); let model = Rc::new(Model::new(app)); - Self {model,frp}.init(app) + Self { model, frp }.init(app) } - fn init(self, app:&Application) -> Self { + fn init(self, app: &Application) -> Self { let network = &self.frp.network; - let frp = &self.frp; - let model = &self.model; + let frp = &self.frp; + let model = &self.model; - let scene = app.display.scene(); - let mouse = &scene.mouse.frp; + let scene = app.display.scene(); + let mouse = &scene.mouse.frp; frp::extend! { network @@ -357,7 +362,7 @@ impl DropDownMenu { // FIXME : StyleWatch is unsuitable here, as it was designed as an internal tool for // shape system (#795) - let styles = StyleWatch::new(&app.display.scene().style_sheet); + let styles = StyleWatch::new(&app.display.scene().style_sheet); let text_color = styles.get_color(theme::widget::list_view::text); model.label.set_default_color(text_color); diff --git a/src/rust/ensogl/lib/components/src/file_browser.rs b/src/rust/ensogl/lib/components/src/file_browser.rs index fe59090a24..5ed7d30933 100644 --- a/src/rust/ensogl/lib/components/src/file_browser.rs +++ b/src/rust/ensogl/lib/components/src/file_browser.rs @@ -7,11 +7,10 @@ use crate::prelude::*; use crate::file_browser::model::*; -use ensogl_core::display::shape::*; -use std::path::PathBuf; use ensogl_core::display; +use ensogl_core::display::shape::*; use ensogl_core::display::Scene; - +use std::path::PathBuf; // =========== // === FRP === @@ -39,41 +38,48 @@ ensogl_core::define_endpoints! { } } - - // =================== // === FileBrowser === // =================== /// A file browser component. It allows to browse the content of a folder and it's subfolders and /// emits an event when an entry is chosen. -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] pub struct FileBrowser { - logger : Logger, - frp : Frp, - display_object : display::object::Instance, + logger: Logger, + frp: Frp, + display_object: display::object::Instance, } impl Deref for FileBrowser { type Target = Frp; - fn deref(&self) -> &Self::Target { &self.frp } + fn deref(&self) -> &Self::Target { + &self.frp + } } - impl FileBrowser { /// Constructore pub fn new() -> Self { - let logger = Logger::new("FileBrowser"); - let frp = Frp::new(); + let logger = Logger::new("FileBrowser"); + let frp = Frp::new(); let display_object = display::object::Instance::new(&logger); - Self {logger,frp,display_object} + Self { + logger, + frp, + display_object, + } } } impl Default for FileBrowser { - fn default() -> Self { Self::new() } + fn default() -> Self { + Self::new() + } } impl display::Object for FileBrowser { - fn display_object(&self) -> &display::object::Instance {&self.display_object } + fn display_object(&self) -> &display::object::Instance { + &self.display_object + } } diff --git a/src/rust/ensogl/lib/components/src/file_browser/model.rs b/src/rust/ensogl/lib/components/src/file_browser/model.rs index 721994e27f..55d723b053 100644 --- a/src/rust/ensogl/lib/components/src/file_browser/model.rs +++ b/src/rust/ensogl/lib/components/src/file_browser/model.rs @@ -4,9 +4,8 @@ use crate::prelude::*; use enso_frp as frp; -use std::path::PathBuf; use std::cmp::Ordering; - +use std::path::PathBuf; // ============= // === Model === @@ -16,7 +15,7 @@ use std::cmp::Ordering; /// The type of a folder. This is used to distinguish standard folders from the different kinds of /// content roots. -#[derive(Debug,Copy,Clone,Eq,Ord,PartialEq,PartialOrd)] +#[derive(Debug, Copy, Clone, Eq, Ord, PartialEq, PartialOrd)] pub enum FolderType { /// A normal sufolder in the file system. Standard, @@ -34,52 +33,58 @@ pub enum FolderType { /// The type of a file system entry. Distinguishes files from the different kind of folders. The /// `EntryType` of a folder also caries the folder's content. -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] pub enum EntryType { /// A file. File, /// A folder. This can also mean a content root. Folder { /// The folder type. - type_ : FolderType, + type_: FolderType, /// The folder's content. - content : AnyFolderContent, + content: AnyFolderContent, }, } impl Ord for EntryType { - fn cmp(&self, other:&Self) -> Ordering { - match (self,other) { - (Self::File,Self::File) => Ordering::Equal, - (Self::File,Self::Folder {..}) => Ordering::Greater, - (Self::Folder {..},Self::File) => Ordering::Less, - (Self::Folder {type_:type1,..},Self::Folder {type_:type2,..}) => type1.cmp(type2), + fn cmp(&self, other: &Self) -> Ordering { + match (self, other) { + (Self::File, Self::File) => Ordering::Equal, + (Self::File, Self::Folder { .. }) => Ordering::Greater, + (Self::Folder { .. }, Self::File) => Ordering::Less, + ( + Self::Folder { type_: type1, .. }, + Self::Folder { type_: type2, .. }, + ) => type1.cmp(type2), } } } impl PartialOrd for EntryType { - fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } } impl PartialEq for EntryType { - fn eq(&self, other: &Self) -> bool { self.cmp(other) == Ordering::Equal } + fn eq(&self, other: &Self) -> bool { + self.cmp(other) == Ordering::Equal + } } impl Eq for EntryType {} /// A file system entry. Either a file or a folder. -#[derive(Debug,Clone,Eq,Ord,PartialEq,PartialOrd)] +#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)] pub struct Entry { /// The entry type. - pub type_ : EntryType, + pub type_: EntryType, /// The entrie's name. - pub name : String, + pub name: String, /// The entrie's global path in the file system. - pub path : PathBuf, + pub path: PathBuf, } - // === FolderContent === /// Values implementing this trait describe the content of folders. They can be seen as a lazy, @@ -87,13 +92,16 @@ pub struct Entry { pub trait FolderContent: Debug { /// Request the list of entries inside the folder. When the list is ready, it is emitted at /// `entries_loaded`. If an error occurs then the error message is emitted at `error_occurred`. - fn request_entries - (&self, entries_loaded:frp::Any>>, error_occurred:frp::Any); + fn request_entries( + &self, + entries_loaded: frp::Any>>, + error_occurred: frp::Any, + ); } /// A wrapper around `Rc`. Necessary to implement the `Default` trait on this /// type, which we need to pass it through FRP networks. -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct AnyFolderContent(Rc); impl Deref for AnyFolderContent { @@ -104,22 +112,24 @@ impl Deref for AnyFolderContent { } } -impl From for AnyFolderContent { +impl From for AnyFolderContent { fn from(dir: D) -> Self { AnyFolderContent(Rc::new(dir)) } } - // === EmptyFolder === /// `FolderContent` that immediately provides an empty content list on request. -#[derive(Debug,Copy,Clone)] +#[derive(Debug, Copy, Clone)] pub struct EmptyFolderContent; impl FolderContent for EmptyFolderContent { - fn request_entries - (&self, entries_loaded:frp::Any>>, _error_occured:frp::Any) { + fn request_entries( + &self, + entries_loaded: frp::Any>>, + _error_occured: frp::Any, + ) { entries_loaded.emit(Rc::new(vec![])); } } diff --git a/src/rust/ensogl/lib/components/src/label.rs b/src/rust/ensogl/lib/components/src/label.rs index 8e31f140bf..7c48b452ce 100644 --- a/src/rust/ensogl/lib/components/src/label.rs +++ b/src/rust/ensogl/lib/components/src/label.rs @@ -7,15 +7,13 @@ use enso_frp as frp; use enso_frp; use ensogl_core::application::Application; use ensogl_core::data::color; +use ensogl_core::display; use ensogl_core::display::shape::*; use ensogl_core::display::traits::*; -use ensogl_core::display; use ensogl_text as text; use ensogl_theme::component::label as theme; - - // ========================== // === Shapes Definitions === // ========================== @@ -42,8 +40,6 @@ mod background { } } - - // =========== // === FRP === // =========== @@ -58,29 +54,27 @@ ensogl_core::define_endpoints! { } } - - // ============= // === Model === // ============= -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] struct Model { - background : background::View, - label : text::Area, - display_object : display::object::Instance, - app : Application, - style : StyleWatch, + background: background::View, + label: text::Area, + display_object: display::object::Instance, + app: Application, + style: StyleWatch, } impl Model { fn new(app: Application) -> Self { - let app = app.clone_ref(); - let scene = app.display.scene(); - let logger = Logger::new("TextLabel"); + let app = app.clone_ref(); + let scene = app.display.scene(); + let logger = Logger::new("TextLabel"); let display_object = display::object::Instance::new(&logger); - let label = app.new_view::(); - let background = background::View::new(&logger); + let label = app.new_view::(); + let background = background::View::new(&logger); // FIXME[MM/WD]: Depth sorting of labels to in front of everything else in the scene. // Temporary solution. The depth management needs to allow defining relative position of @@ -94,73 +88,79 @@ impl Model { let style = StyleWatch::new(&app.display.scene().style_sheet); - Model {background,label,display_object,app,style} + Model { + background, + label, + display_object, + app, + style, + } } pub fn height(&self) -> f32 { self.style.get_number(theme::height) } - fn set_width(&self, width:f32) -> Vector2 { - let padding_outer = self.style.get_number(theme::padding_outer); + fn set_width(&self, width: f32) -> Vector2 { + let padding_outer = self.style.get_number(theme::padding_outer); let padding_inner_x = self.style.get_number(theme::padding_inner_x); let padding_inner_y = self.style.get_number(theme::padding_inner_y); - let padding_x = padding_outer + padding_inner_x; - let padding_y = padding_outer + padding_inner_y; - let padding = Vector2(padding_x,padding_y); - let text_size = self.style.get_number(theme::text::size); - let text_offset = self.style.get_number(theme::text::offset); - let height = self.height(); - let size = Vector2(width,height); - let padded_size = size + padding * 2.0; + let padding_x = padding_outer + padding_inner_x; + let padding_y = padding_outer + padding_inner_y; + let padding = Vector2(padding_x, padding_y); + let text_size = self.style.get_number(theme::text::size); + let text_offset = self.style.get_number(theme::text::offset); + let height = self.height(); + let size = Vector2(width, height); + let padded_size = size + padding * 2.0; self.background.size.set(padded_size); - let text_origin = Vector2(text_offset - size.x/2.0, text_size /2.0); + let text_origin = Vector2(text_offset - size.x / 2.0, text_size / 2.0); self.label.set_position_xy(text_origin); padded_size } - fn set_content(&self, t:&str) -> Vector2 { + fn set_content(&self, t: &str) -> Vector2 { self.label.set_content(t); self.set_width(self.label.width.value()) } - fn set_opacity(&self, value:f32) { + fn set_opacity(&self, value: f32) { let text_color_path = theme::text; - let text_color = self.style.get_color(text_color_path).multiply_alpha(value); + let text_color = + self.style.get_color(text_color_path).multiply_alpha(value); self.label.frp.set_color_all.emit(text_color); self.label.frp.set_default_color.emit(text_color); let bg_color_path = theme::background; - let bg_color = self.style.get_color(bg_color_path).multiply_alpha(value); + let bg_color = + self.style.get_color(bg_color_path).multiply_alpha(value); self.background.bg_color.set(bg_color.into()) } } - - // ======================= // === Label Component === // ======================= #[allow(missing_docs)] -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] pub struct Label { - model : Rc, - pub frp : Rc, + model: Rc, + pub frp: Rc, } impl Label { /// Constructor. - pub fn new(app:&Application) -> Self { - let frp = Rc::new(Frp::new()); + pub fn new(app: &Application) -> Self { + let frp = Rc::new(Frp::new()); let model = Rc::new(Model::new(app.clone_ref())); - Label {model,frp}.init() + Label { model, frp }.init() } fn init(self) -> Self { - let frp = &self.frp; + let frp = &self.frp; let network = &frp.network; - let model = &self.model; + let model = &self.model; frp::extend! { network frp.source.size <+ frp.set_content.map(f!((t) @@ -175,5 +175,7 @@ impl Label { } impl display::Object for Label { - fn display_object(&self) -> &display::object::Instance { &self.model.display_object } + fn display_object(&self) -> &display::object::Instance { + &self.model.display_object + } } diff --git a/src/rust/ensogl/lib/components/src/lib.rs b/src/rust/ensogl/lib/components/src/lib.rs index a20ccded88..427b2eafde 100644 --- a/src/rust/ensogl/lib/components/src/lib.rs +++ b/src/rust/ensogl/lib/components/src/lib.rs @@ -4,7 +4,6 @@ #![feature(option_result_contains)] #![feature(trait_alias)] - #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] #![warn(missing_docs)] @@ -13,8 +12,7 @@ #![warn(unsafe_code)] #![warn(unused_import_braces)] #![warn(unused_qualifications)] - -#![recursion_limit="512"] +#![recursion_limit = "512"] pub mod component; pub mod drop_down_menu; diff --git a/src/rust/ensogl/lib/components/src/list_view.rs b/src/rust/ensogl/lib/components/src/list_view.rs index 691eb97fa5..c44a5c773d 100644 --- a/src/rust/ensogl/lib/components/src/list_view.rs +++ b/src/rust/ensogl/lib/components/src/list_view.rs @@ -10,8 +10,8 @@ use crate::shadow; use enso_frp as frp; use ensogl_core::application; -use ensogl_core::application::Application; use ensogl_core::application::shortcut; +use ensogl_core::application::Application; use ensogl_core::display; use ensogl_core::display::scene::layer::LayerId; use ensogl_core::display::shape::*; @@ -20,8 +20,6 @@ use ensogl_theme as theme; pub use entry::Entry; - - // ========================== // === Shapes Definitions === // ========================== @@ -29,9 +27,8 @@ pub use entry::Entry; // === Constants === /// The size of shadow under element. It is not counted in the component width and height. -pub const SHADOW_PX:f32 = 10.0; -const SHAPE_PADDING:f32 = 5.0; - +pub const SHADOW_PX: f32 = 10.0; +const SHAPE_PADDING: f32 = 5.0; // === Selection === @@ -40,7 +37,7 @@ pub mod selection { use super::*; /// The corner radius in pixels. - pub const CORNER_RADIUS_PX:f32 = 12.0; + pub const CORNER_RADIUS_PX: f32 = 12.0; ensogl_core::define_shape_system! { (style:Style) { @@ -58,7 +55,6 @@ pub mod selection { } } - // === Background === /// The default list view background. @@ -66,7 +62,7 @@ pub mod background { use super::*; /// The corner radius in pixels. - pub const CORNER_RADIUS_PX:f32 = selection::CORNER_RADIUS_PX; + pub const CORNER_RADIUS_PX: f32 = selection::CORNER_RADIUS_PX; ensogl_core::define_shape_system! { below = [selection]; @@ -86,113 +82,138 @@ pub mod background { } } - - // ============= // === Model === // ============= /// Information about displayed fragment of entries list. -#[derive(Copy,Clone,Debug,Default)] +#[derive(Copy, Clone, Debug, Default)] struct View { - position_y : f32, - size : Vector2, + position_y: f32, + size: Vector2, } /// The Model of Select Component. -#[derive(Clone,CloneRef,Debug)] -struct Model { - app : Application, - entries : entry::List, - selection : selection::View, - background : background::View, - scrolled_area : display::object::Instance, - display_object : display::object::Instance, +#[derive(Clone, CloneRef, Debug)] +struct Model { + app: Application, + entries: entry::List, + selection: selection::View, + background: background::View, + scrolled_area: display::object::Instance, + display_object: display::object::Instance, } -impl Model { - - fn new(app:&Application) -> Self { - let app = app.clone_ref(); - let logger = Logger::new("SelectionContainer"); +impl Model { + fn new(app: &Application) -> Self { + let app = app.clone_ref(); + let logger = Logger::new("SelectionContainer"); let display_object = display::object::Instance::new(&logger); - let scrolled_area = display::object::Instance::new(&logger); - let entries = entry::List::new(&logger,&app); - let background = background::View::new(&logger); - let selection = selection::View::new(&logger); + let scrolled_area = display::object::Instance::new(&logger); + let entries = entry::List::new(&logger, &app); + let background = background::View::new(&logger); + let selection = selection::View::new(&logger); display_object.add_child(&background); display_object.add_child(&scrolled_area); scrolled_area.add_child(&entries); scrolled_area.add_child(&selection); - Model{app,entries,selection,background,scrolled_area,display_object} + Model { + app, + entries, + selection, + background, + scrolled_area, + display_object, + } } fn padding(&self) -> f32 { // FIXME : StyleWatch is unsuitable here, as it was designed as an internal tool for shape // system (#795) - let styles = StyleWatch::new(&self.app.display.scene().style_sheet); + let styles = StyleWatch::new(&self.app.display.scene().style_sheet); styles.get_number(ensogl_theme::application::searcher::padding) } /// Update the displayed entries list when _view_ has changed - the list was scrolled or /// resized. - fn update_after_view_change(&self, view:&View) { - let visible_entries = Self::visible_entries(view,self.entries.entry_count()); - let padding_px = self.padding(); - let padding = 2.0 * padding_px + SHAPE_PADDING; - let padding = Vector2(padding, padding); - let shadow = Vector2(2.0 * SHADOW_PX, 2.0 * SHADOW_PX); + fn update_after_view_change(&self, view: &View) { + let visible_entries = + Self::visible_entries(view, self.entries.entry_count()); + let padding_px = self.padding(); + let padding = 2.0 * padding_px + SHAPE_PADDING; + let padding = Vector2(padding, padding); + let shadow = Vector2(2.0 * SHADOW_PX, 2.0 * SHADOW_PX); self.entries.set_position_x(-view.size.x / 2.0); self.background.size.set(view.size + padding + shadow); - self.scrolled_area.set_position_y(view.size.y / 2.0 - view.position_y); + self.scrolled_area + .set_position_y(view.size.y / 2.0 - view.position_y); self.entries.update_entries(visible_entries); } - fn set_entries(&self, provider:entry::AnyModelProvider, view:&View) { - let visible_entries = Self::visible_entries(view,provider.entry_count()); - self.entries.update_entries_new_provider(provider,visible_entries); + fn set_entries(&self, provider: entry::AnyModelProvider, view: &View) { + let visible_entries = + Self::visible_entries(view, provider.entry_count()); + self.entries + .update_entries_new_provider(provider, visible_entries); } - fn visible_entries(View {position_y,size}:&View, entry_count:usize) -> Range { + fn visible_entries( + View { position_y, size }: &View, + entry_count: usize, + ) -> Range { if entry_count == 0 { 0..0 } else { - let entry_at_y_saturating = |y:f32| { - match entry::List::::entry_at_y_position(y,entry_count) { + let entry_at_y_saturating = + |y: f32| match entry::List::::entry_at_y_position( + y, + entry_count, + ) { entry::list::IdAtYPosition::AboveFirst => 0, - entry::list::IdAtYPosition::UnderLast => entry_count - 1, - entry::list::IdAtYPosition::Entry(id) => id, - } - }; + entry::list::IdAtYPosition::UnderLast => entry_count - 1, + entry::list::IdAtYPosition::Entry(id) => id, + }; let first = entry_at_y_saturating(*position_y); - let last = entry_at_y_saturating(position_y - size.y) + 1; + let last = entry_at_y_saturating(position_y - size.y) + 1; first..last } } /// Check if the `point` is inside component assuming that it have given `size`. - fn is_inside(&self, point:Vector2, size:Vector2) -> bool { - let pos_obj_space = self.app.display.scene().screen_to_object_space(&self.background,point); - let x_range = (-size.x / 2.0)..=(size.x / 2.0); - let y_range = (-size.y / 2.0)..=(size.y / 2.0); + fn is_inside(&self, point: Vector2, size: Vector2) -> bool { + let pos_obj_space = self + .app + .display + .scene() + .screen_to_object_space(&self.background, point); + let x_range = (-size.x / 2.0)..=(size.x / 2.0); + let y_range = (-size.y / 2.0)..=(size.y / 2.0); x_range.contains(&pos_obj_space.x) && y_range.contains(&pos_obj_space.y) } - fn selected_entry_after_jump - (&self, current_entry:Option, jump:isize) -> Option { + fn selected_entry_after_jump( + &self, + current_entry: Option, + jump: isize, + ) -> Option { if jump < 0 { let current_entry = current_entry?; - if current_entry == 0 { None } - else { Some(current_entry.saturating_sub(-jump as usize)) } + if current_entry == 0 { + None + } else { + Some(current_entry.saturating_sub(-jump as usize)) + } } else { let max_entry = self.entries.entry_count().checked_sub(1)?; - Some(current_entry.map_or(0, |id| id+(jump as usize)).min(max_entry)) + Some( + current_entry + .map_or(0, |id| id + (jump as usize)) + .min(max_entry), + ) } } } - - // =========== // === FRP === // =========== @@ -232,8 +253,6 @@ ensogl_core::define_endpoints! { } } - - // ========================== // === ListView Component === // ========================== @@ -243,40 +262,44 @@ ensogl_core::define_endpoints! { /// This is a displayed list of entries (of any type `E`) with possibility of selecting one and /// "choosing" by clicking or pressing enter. The basic entry types are defined in [`entry`] module. #[allow(missing_docs)] -#[derive(Clone,CloneRef,Debug)] -pub struct ListView { - model : Model, - pub frp : Frp, +#[derive(Clone, CloneRef, Debug)] +pub struct ListView { + model: Model, + pub frp: Frp, } -impl Deref for ListView { +impl Deref for ListView { type Target = Frp; - fn deref(&self) -> &Self::Target { &self.frp } + fn deref(&self) -> &Self::Target { + &self.frp + } } -impl ListView -where E::Model : Default { +impl ListView +where + E::Model: Default, +{ /// Constructor. - pub fn new(app:&Application) -> Self { - let frp = Frp::new(); + pub fn new(app: &Application) -> Self { + let frp = Frp::new(); let model = Model::new(app); - ListView {model,frp}.init(app) + ListView { model, frp }.init(app) } - fn init(self, app:&Application) -> Self { - const MAX_SCROLL:f32 = entry::HEIGHT/2.0; - const MOUSE_MOVE_THRESHOLD:f32 = std::f32::EPSILON; - - let frp = &self.frp; - let network = &frp.network; - let model = &self.model; - let scene = app.display.scene(); - let mouse = &scene.mouse.frp; - let view_y = DEPRECATED_Animation::::new(network); - let selection_y = DEPRECATED_Animation::::new(network); + fn init(self, app: &Application) -> Self { + const MAX_SCROLL: f32 = entry::HEIGHT / 2.0; + const MOUSE_MOVE_THRESHOLD: f32 = std::f32::EPSILON; + + let frp = &self.frp; + let network = &frp.network; + let model = &self.model; + let scene = app.display.scene(); + let mouse = &scene.mouse.frp; + let view_y = DEPRECATED_Animation::::new(network); + let selection_y = DEPRECATED_Animation::::new(network); let selection_height = DEPRECATED_Animation::::new(network); - frp::extend!{ network + frp::extend! { network // === Mouse Position === @@ -424,32 +447,46 @@ where E::Model : Default { } /// Sets the scene layer where the labels will be placed. - pub fn set_label_layer(&self, layer:LayerId) { + pub fn set_label_layer(&self, layer: LayerId) { self.model.entries.set_label_layer(layer); } } -impl display::Object for ListView { - fn display_object(&self) -> &display::object::Instance { &self.model.display_object } +impl display::Object for ListView { + fn display_object(&self) -> &display::object::Instance { + &self.model.display_object + } } -impl application::command::FrpNetworkProvider for ListView { - fn network(&self) -> &frp::Network { &self.frp.network } +impl application::command::FrpNetworkProvider for ListView { + fn network(&self) -> &frp::Network { + &self.frp.network + } } -impl application::View for ListView { - fn label() -> &'static str { "ListView" } - fn new(app:&Application) -> Self { ListView::new(app) } - fn app(&self) -> &Application { &self.model.app } +impl application::View for ListView { + fn label() -> &'static str { + "ListView" + } + fn new(app: &Application) -> Self { + ListView::new(app) + } + fn app(&self) -> &Application { + &self.model.app + } fn default_shortcuts() -> Vec { use shortcut::ActionType::*; - (&[ (PressAndRepeat , "up" , "move_selection_up") - , (PressAndRepeat , "down" , "move_selection_down") - , (Press , "page-up" , "move_selection_page_up") - , (Press , "page-down" , "move_selection_page_down") - , (Press , "home" , "move_selection_to_first") - , (Press , "end" , "move_selection_to_last") - , (Press , "enter" , "chose_selected_entry") - ]).iter().map(|(a,b,c)|Self::self_shortcut(*a,*b,*c)).collect() + (&[ + (PressAndRepeat, "up", "move_selection_up"), + (PressAndRepeat, "down", "move_selection_down"), + (Press, "page-up", "move_selection_page_up"), + (Press, "page-down", "move_selection_page_down"), + (Press, "home", "move_selection_to_first"), + (Press, "end", "move_selection_to_last"), + (Press, "enter", "chose_selected_entry"), + ]) + .iter() + .map(|(a, b, c)| Self::self_shortcut(*a, *b, *c)) + .collect() } } diff --git a/src/rust/ensogl/lib/components/src/list_view/entry.rs b/src/rust/ensogl/lib/components/src/list_view/entry.rs index 1f5ef0bdfc..6419a3ddd4 100644 --- a/src/rust/ensogl/lib/components/src/list_view/entry.rs +++ b/src/rust/ensogl/lib/components/src/list_view/entry.rs @@ -10,18 +10,14 @@ use ensogl_core::display::shape::StyleWatchFrp; use ensogl_text as text; use ensogl_theme as theme; - - // ================= // === Constants === // ================= /// Padding inside entry in pixels. -pub const PADDING:f32 = 14.0; +pub const PADDING: f32 = 14.0; /// The overall entry's height (including padding). -pub const HEIGHT:f32 = 30.0; - - +pub const HEIGHT: f32 = 30.0; // ================================== // === Type Aliases and Reexports === @@ -32,8 +28,6 @@ pub type Id = usize; pub use list::List; - - // ============= // === Trait === // ============= @@ -52,21 +46,20 @@ pub trait Entry: CloneRef + Debug + display::Object + 'static { /// The model of this entry. The entry should be a representation of data from the Model. /// For example, the entry being just a caption can have [`String`] as its model - the text to /// be displayed. - type Model : Debug + Default; + type Model: Debug + Default; /// An Object constructor. - fn new(app:&Application) -> Self; + fn new(app: &Application) -> Self; /// Update content with new model. - fn update(&self, model:&Self::Model); + fn update(&self, model: &Self::Model); /// Set the layer of all [`text::Area`] components inside. The [`text::Area`] component is /// handled in a special way, and is often in different layer than shapes. See TODO comment /// in [`text::Area::add_to_scene_layer`] method. - fn set_label_layer(&self, label_layer:&display::scene::Layer); + fn set_label_layer(&self, label_layer: &display::scene::Layer); } - // ======================= // === Implementations === // ======================= @@ -74,25 +67,25 @@ pub trait Entry: CloneRef + Debug + display::Object + 'static { // === Label === /// The [`Entry`] being a single text field displaying String. -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] pub struct Label { - display_object : display::object::Instance, - label : text::Area, - network : enso_frp::Network, - style_watch : StyleWatchFrp, + display_object: display::object::Instance, + label: text::Area, + network: enso_frp::Network, + style_watch: StyleWatchFrp, } impl Entry for Label { type Model = String; fn new(app: &Application) -> Self { - let logger = Logger::new("list_view::entry::Label"); + let logger = Logger::new("list_view::entry::Label"); let display_object = display::object::Instance::new(logger); - let label = app.new_view::(); - let network = frp::Network::new("list_view::entry::Label"); - let style_watch = StyleWatchFrp::new(&app.display.scene().style_sheet); - let color = style_watch.get_color(theme::widget::list_view::text); - let size = style_watch.get_number(theme::widget::list_view::text::size); + let label = app.new_view::(); + let network = frp::Network::new("list_view::entry::Label"); + let style_watch = StyleWatchFrp::new(&app.display.scene().style_sheet); + let color = style_watch.get_color(theme::widget::list_view::text); + let size = style_watch.get_number(theme::widget::list_view::text::size); display_object.add_child(&label); frp::extend! { network @@ -105,50 +98,58 @@ impl Entry for Label { eval size ((size) label.set_position_y(size/2.0)); } init.emit(()); - Self {display_object,label,network,style_watch} + Self { + display_object, + label, + network, + style_watch, + } } fn update(&self, model: &Self::Model) { self.label.set_content(model); } - fn set_label_layer(&self, label_layer:&display::scene::Layer) { + fn set_label_layer(&self, label_layer: &display::scene::Layer) { self.label.add_to_scene_layer(label_layer); } } impl display::Object for Label { - fn display_object(&self) -> &display::object::Instance { &self.display_object } + fn display_object(&self) -> &display::object::Instance { + &self.display_object + } } - // === HighlightedLabel === /// The model for [`HighlightedLabel`], being an entry displayed as a single label with highlighted /// some parts of text. -#[derive(Clone,Debug,Default)] +#[derive(Clone, Debug, Default)] pub struct GlyphHighlightedLabelModel { /// Displayed text. - pub label:String, + pub label: String, /// A list of ranges of highlighted bytes. - pub highlighted:Vec>, + pub highlighted: Vec>, } /// The [`Entry`] similar to the [`Label`], but allows highlighting some parts of text. -#[derive(Clone,CloneRef,Debug)] +#[derive(Clone, CloneRef, Debug)] pub struct GlyphHighlightedLabel { - inner : Label, - highlight : frp::Source>>, + inner: Label, + highlight: frp::Source>>, } impl Entry for GlyphHighlightedLabel { type Model = GlyphHighlightedLabelModel; fn new(app: &Application) -> Self { - let inner = Label::new(app); - let network = &inner.network; - let highlight_color = inner.style_watch.get_color(theme::widget::list_view::text::highlight); - let label = &inner.label; + let inner = Label::new(app); + let network = &inner.network; + let highlight_color = inner + .style_watch + .get_color(theme::widget::list_view::text::highlight); + let label = &inner.label; frp::extend! { network highlight <- source::>>(); @@ -159,7 +160,7 @@ impl Entry for GlyphHighlightedLabel { } }); } - Self {inner,highlight} + Self { inner, highlight } } fn update(&self, model: &Self::Model) { @@ -167,17 +168,17 @@ impl Entry for GlyphHighlightedLabel { self.highlight.emit(&model.highlighted); } - fn set_label_layer(&self, layer:&display::scene::Layer) { + fn set_label_layer(&self, layer: &display::scene::Layer) { self.inner.set_label_layer(layer); } } impl display::Object for GlyphHighlightedLabel { - fn display_object(&self) -> &display::object::Instance { self.inner.display_object() } + fn display_object(&self) -> &display::object::Instance { + self.inner.display_object() + } } - - // ======================= // === Model Providers === // ======================= @@ -189,95 +190,116 @@ impl display::Object for GlyphHighlightedLabel { /// The [`crate::ListView`] component does not display all entries at once, instead it lazily ask /// for models of entries when they're about to be displayed. So setting the select content is /// essentially providing an implementor of this trait. -pub trait ModelProvider : Debug { +pub trait ModelProvider: Debug { /// Number of all entries. fn entry_count(&self) -> usize; /// Get the model of entry with given id. The implementors should return `None` only when /// requested id greater or equal to entries count. - fn get(&self, id:Id) -> Option - where E : Entry; + fn get(&self, id: Id) -> Option + where + E: Entry; } - // === AnyModelProvider === /// A wrapper for shared instance of some Provider of models for `E` entries. -#[derive(Debug,Shrinkwrap)] +#[derive(Debug, Shrinkwrap)] pub struct AnyModelProvider(Rc>); -impl Clone for AnyModelProvider { fn clone (&self) -> Self { Self(self.0.clone()) }} -impl CloneRef for AnyModelProvider { fn clone_ref(&self) -> Self { Self(self.0.clone_ref()) }} +impl Clone for AnyModelProvider { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} +impl CloneRef for AnyModelProvider { + fn clone_ref(&self) -> Self { + Self(self.0.clone_ref()) + } +} impl AnyModelProvider { /// Create from typed provider. - pub fn new+'static>(provider:T) -> Self { Self(Rc::new(provider)) } + pub fn new + 'static>(provider: T) -> Self { + Self(Rc::new(provider)) + } } -impl+'static> From> for AnyModelProvider { - fn from(provider:Rc) -> Self { Self(provider) } +impl + 'static> From> for AnyModelProvider { + fn from(provider: Rc) -> Self { + Self(provider) + } } impl Default for AnyModelProvider { - fn default() -> Self { Self::new(EmptyProvider) } + fn default() -> Self { + Self::new(EmptyProvider) + } } - // === EmptyProvider === /// An Entry Model Provider giving no entries. /// /// This is the default provider for new select components. -#[derive(Clone,CloneRef,Copy,Debug)] +#[derive(Clone, CloneRef, Copy, Debug)] pub struct EmptyProvider; impl ModelProvider for EmptyProvider { - fn entry_count(&self) -> usize { 0 } - fn get (&self, _:usize) -> Option where E : Entry { None } + fn entry_count(&self) -> usize { + 0 + } + fn get(&self, _: usize) -> Option + where + E: Entry, + { + None + } } - // === ModelProvider for Vectors === -impl ModelProvider for Vec -where E : Entry, - T : Debug + Clone + Into { +impl ModelProvider for Vec +where + E: Entry, + T: Debug + Clone + Into, +{ fn entry_count(&self) -> usize { self.len() } - fn get(&self, id:usize) -> Option { - Some(<[T]>::get(self, id)?.clone().into()) + fn get(&self, id: usize) -> Option { + Some(<[T]>::get(self, id)?.clone().into()) } } - // === SingleMaskedProvider === /// An Entry Model Provider that wraps a `AnyModelProvider` and allows the masking of a single item. -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] pub struct SingleMaskedProvider { - content : AnyModelProvider, - mask : Cell>, + content: AnyModelProvider, + mask: Cell>, } -impl ModelProvider for SingleMaskedProvider { +impl ModelProvider for SingleMaskedProvider { fn entry_count(&self) -> usize { match self.mask.get() { - None => self.content.entry_count(), + None => self.content.entry_count(), Some(_) => self.content.entry_count().saturating_sub(1), } } - fn get(&self, ix:usize) -> Option - where E : Entry { + fn get(&self, ix: usize) -> Option + where + E: Entry, + { let internal_ix = self.unmasked_index(ix); self.content.get(internal_ix) } } impl SingleMaskedProvider { - /// Return the index to the unmasked underlying data. Will only be valid to use after /// calling `clear_mask`. /// @@ -294,11 +316,11 @@ impl SingleMaskedProvider { /// Masked indices [0, 1, 2, 3] /// Unmasked Index [0, 1, 2, 3] /// ``` - pub fn unmasked_index(&self, ix:Id) -> Id { + pub fn unmasked_index(&self, ix: Id) -> Id { match self.mask.get() { - None => ix, - Some(id) if ix < id => ix, - Some(_) => ix+1, + None => ix, + Some(id) if ix < id => ix, + Some(_) => ix + 1, } } @@ -306,7 +328,7 @@ impl SingleMaskedProvider { /// will behave as if it was not there. /// /// *Important:* The index is interpreted according to the _masked_ position of elements. - pub fn set_mask(&self, ix:Id) { + pub fn set_mask(&self, ix: Id) { let internal_ix = self.unmasked_index(ix); self.mask.set(Some(internal_ix)); } @@ -315,7 +337,7 @@ impl SingleMaskedProvider { /// will behave as if it was not there. /// /// *Important:* The index is interpreted according to the _unmasked_ position of elements. - pub fn set_mask_raw(&self, ix:Id) { + pub fn set_mask_raw(&self, ix: Id) { self.mask.set(Some(ix)); } @@ -326,14 +348,12 @@ impl SingleMaskedProvider { } impl From> for SingleMaskedProvider { - fn from(content:AnyModelProvider) -> Self { + fn from(content: AnyModelProvider) -> Self { let mask = default(); - SingleMaskedProvider{content,mask} + SingleMaskedProvider { content, mask } } } - - // ============= // === Tests === // ============= @@ -344,10 +364,13 @@ mod tests { #[test] fn test_masked_provider() { - let test_data = vec!["A", "B", "C", "D"]; - let test_models = test_data.into_iter().map(|label| label.to_owned()).collect_vec(); - let provider = AnyModelProvider::