Skip to content

Commit 8a43b39

Browse files
rich text: add an open-url function (#9936)
* Add an open-url function * Add cpp implementation * Add workspace dep * Add core::open_url function * Remove pub use webbrowser * Put open_url behind a flag * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 4ea3eab commit 8a43b39

File tree

9 files changed

+36
-0
lines changed

9 files changed

+36
-0
lines changed

api/cpp/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,8 @@ pub extern "C" fn slint_set_xdg_app_id(_app_id: &SharedString) {
230230
pub extern "C" fn slint_detect_operating_system() -> OperatingSystemType {
231231
i_slint_core::detect_operating_system()
232232
}
233+
234+
#[unsafe(no_mangle)]
235+
pub extern "C" fn open_url(url: &SharedString) {
236+
i_slint_core::open_url(url)
237+
}

internal/compiler/expression_tree.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub enum BuiltinFunction {
111111
StartTimer,
112112
StopTimer,
113113
RestartTimer,
114+
OpenUrl,
114115
}
115116

116117
#[derive(Debug, Clone)]
@@ -276,6 +277,7 @@ declare_builtin_function_types!(
276277
StartTimer: (Type::ElementReference) -> Type::Void,
277278
StopTimer: (Type::ElementReference) -> Type::Void,
278279
RestartTimer: (Type::ElementReference) -> Type::Void,
280+
OpenUrl: (Type::String) -> Type::Void
279281
);
280282

281283
impl BuiltinFunction {
@@ -371,6 +373,7 @@ impl BuiltinFunction {
371373
BuiltinFunction::StartTimer => false,
372374
BuiltinFunction::StopTimer => false,
373375
BuiltinFunction::RestartTimer => false,
376+
BuiltinFunction::OpenUrl => false,
374377
}
375378
}
376379

@@ -448,6 +451,7 @@ impl BuiltinFunction {
448451
BuiltinFunction::StartTimer => false,
449452
BuiltinFunction::StopTimer => false,
450453
BuiltinFunction::RestartTimer => false,
454+
BuiltinFunction::OpenUrl => false,
451455
}
452456
}
453457
}

internal/compiler/generator/cpp.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,6 +4198,10 @@ fn compile_builtin_function_call(
41984198
panic!("internal error: invalid args to RetartTimer {arguments:?}")
41994199
}
42004200
}
4201+
BuiltinFunction::OpenUrl => {
4202+
let url = a.next().unwrap();
4203+
format!("slint::cbindgen_private::open_url({})", url)
4204+
}
42014205
}
42024206
}
42034207

internal/compiler/generator/rust.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,6 +3448,10 @@ fn compile_builtin_function_call(
34483448
panic!("internal error: invalid args to RestartTimer {arguments:?}")
34493449
}
34503450
}
3451+
BuiltinFunction::OpenUrl => {
3452+
let url = a.next().unwrap();
3453+
quote!(sp::open_url(&#url))
3454+
}
34513455
}
34523456
}
34533457

internal/compiler/llr/optim_passes/inline_expressions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ fn builtin_function_cost(function: &BuiltinFunction) -> isize {
155155
BuiltinFunction::StartTimer => 10,
156156
BuiltinFunction::StopTimer => 10,
157157
BuiltinFunction::RestartTimer => 10,
158+
BuiltinFunction::OpenUrl => isize::MAX,
158159
}
159160
}
160161

internal/compiler/lookup.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ impl LookupObject for SlintInternal {
809809
.or_else(|| f("date-now", b(BuiltinFunction::DateNow)))
810810
.or_else(|| f("valid-date", b(BuiltinFunction::ValidDate)))
811811
.or_else(|| f("parse-date", b(BuiltinFunction::ParseDate)))
812+
.or_else(|| f("open-url", b(BuiltinFunction::OpenUrl)))
812813
}
813814
}
814815

internal/core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ std = [
4141
"chrono/wasmbind",
4242
"chrono/clock",
4343
"dep:sys-locale",
44+
"dep:webbrowser",
4445
]
4546
# Unsafe feature meaning that there is only one core running and all thread_local are static.
4647
# You can only enable this feature if you are sure that any API of this crate is only called
@@ -129,6 +130,8 @@ wgpu-27 = { workspace = true, optional = true }
129130

130131
tr = { workspace = true, optional = true }
131132

133+
webbrowser = { version = "1.0.6", optional = true }
134+
132135
[target.'cfg(target_family = "unix")'.dependencies]
133136
gettext-rs = { version = "0.7.1", optional = true, features = ["gettext-system"] }
134137

internal/core/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,11 @@ pub fn detect_operating_system() -> OperatingSystemType {
148148
pub fn is_apple_platform() -> bool {
149149
matches!(detect_operating_system(), OperatingSystemType::Macos | OperatingSystemType::Ios)
150150
}
151+
152+
#[cfg_attr(not(feature = "std"), allow(unused))]
153+
pub fn open_url(url: &str) {
154+
#[cfg(feature = "std")]
155+
if let Err(err) = webbrowser::open(url) {
156+
debug_log!("Error opening url {}: {}", url, err);
157+
}
158+
}

internal/interpreter/eval.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,12 @@ fn call_builtin_function(
13891389
panic!("internal error: argument to RestartTimer must be an element")
13901390
}
13911391
}
1392+
BuiltinFunction::OpenUrl => {
1393+
let url: SharedString =
1394+
eval_expression(&arguments[0], local_context).try_into().unwrap();
1395+
corelib::open_url(&url);
1396+
Value::Void
1397+
}
13921398
}
13931399
}
13941400

0 commit comments

Comments
 (0)