Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hyperlink in ui and text example #568

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ ambient_gpu = { path = "../gpu" }
ambient_renderer = { path = "../renderer" }
ambient_procedurals = { path = "../procedurals" }

ambient_guest_bridge = { path = "../../shared_crates/guest_bridge", default-features = false, version = "0.3.0-dev" }

ambient_project = { path = "../../shared_crates/project" }
ambient_shared_types = { path = "../../shared_crates/shared_types", features = [
"native",
] }


anyhow = { workspace = true }
async-trait = { workspace = true }
byteorder = { workspace = true }
Expand All @@ -39,6 +42,7 @@ indoc = { workspace = true }
itertools = { workspace = true }
log = { workspace = true }
once_cell = { workspace = true }
open = { workspace = true }
parking_lot = { workspace = true }
paste = { workspace = true }
ambient_profiling = { workspace = true }
Expand Down
13 changes: 13 additions & 0 deletions crates/wasm/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::shared::{self, client_bytecode_from_url, module_bytecode, ModuleBytecode};
use ambient_core::{asset_cache, async_ecs::async_run, runtime};
use ambient_ecs::{query, EntityId, SystemGroup, World};
use ambient_guest_bridge::components::text::hyperlink;
use ambient_std::{
asset_cache::AsyncAssetKeyExt, asset_url::AbsAssetUrl, download_asset::BytesFromUrl,
};
Expand All @@ -27,6 +28,18 @@ pub fn systems() -> SystemGroup {
SystemGroup::new(
"core/wasm/client",
vec![
query(hyperlink().changed()).to_system(move |q, world, qs, _| {
for (id, url) in q.collect_cloned(world, qs) {
#[cfg(not(target_os = "unknown"))]
match open::that(&url) {
Ok(()) => log::info!("Opened '{}'", &url),
Err(err) => {
log::error!("An error occurred when opening '{}': {}", &url, err)
}
}
world.despawn(id);
}
}),
query(client_bytecode_from_url().changed()).to_system(move |q, world, qs, _| {
for (id, url) in q.collect_cloned(world, qs) {
let url = match AbsAssetUrl::from_str(&url) {
Expand Down
2 changes: 2 additions & 0 deletions guest/rust/examples/ui/text/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ fn App(_hooks: &mut Hooks) -> Element {
Text::el("Custom size").with(font_size(), 40.),
Text::el("Custom color").with(color(), vec4(1., 0., 0., 1.)),
Text::el("Multi\n\nLine"),
Text::el("Hyperlink:"),
Hyperlink::el("https://ambient.run".to_string()),
])
.with_padding_even(STREET)
.with(space_between_items(), 10.)
Expand Down
11 changes: 11 additions & 0 deletions shared_crates/schema/src/schema/text.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ type = "String"
name = "Text"
description = "Create a text mesh on this entity."
attributes = ["Debuggable", "Networked", "Store"]


[components."core::text::hyperlink"]
type = "String"
name = "Hyperlink"
description = """
This is a hyperlink text. With this component, the entity is a special one.
Once spawned, the system will open the URL String in the browser.
Then the entity will be destroyed.
"""
attributes = ["Debuggable", "Networked", "Store"]
30 changes: 24 additions & 6 deletions shared_crates/ui/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

use crate::{UIBase, UIElement};
use ambient_element::{element_component, Element, ElementComponentExt, Hooks};
use ambient_guest_bridge::components::{
app::{name, ui_scene},
layout::{height, width},
rendering::color,
text::{font_family, font_size, text},
transform::mesh_to_local,
use ambient_guest_bridge::{
components::{
app::{name, ui_scene},
layout::{height, width},
rendering::color,
text::{font_family, font_size, hyperlink, text},
transform::mesh_to_local,
},
ecs::Entity,
};
use glam::{vec4, Mat4};

Expand Down Expand Up @@ -68,3 +71,18 @@ pub fn FontAwesomeIcon(
.to_string(),
)
}

#[element_component]
/// Hyperlink to a URL. This will open browser when clicked.
pub fn Hyperlink(
_hooks: &mut Hooks,
/// The URL to open
url: String,
) -> Element {
crate::prelude::Button::new(&url.clone(), move |world| {
let link = Entity::new().with(hyperlink(), url.clone());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a function for this instead of an entity. That would also allow us to return an error if we find the thing that's being opened to be invalid/questionable.

Copy link
Contributor Author

@chaosprint chaosprint Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a function for this instead of an entity. That would also allow us to return an error if we find the thing that's being opened to be invalid/questionable.

I tried before to call webbrowser here but get a 'wbindgen_placeholder' error. That's why I have to send to the world and watch it. Perhaps we can do the check there and then decide whether we want to open it or not?

--- edit

cannot put open here as well. so it has to communicate with backend?

world.spawn(link);
})
.style(crate::prelude::ButtonStyle::Inline)
.el()
}
61 changes: 61 additions & 0 deletions web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.