Skip to content

Commit ccdf912

Browse files
authored
fix: build without x11 feature (#1603)
* fix: build without x11 feature * fix tests * fix example * fix async protocol example * fix other examples * pub fn
1 parent a6fbc57 commit ccdf912

File tree

9 files changed

+490
-427
lines changed

9 files changed

+490
-427
lines changed

.changes/fix-build-without-x11.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wry": patch
3+
---
4+
5+
Fix the build when not enabling the `x11` feature.

.github/workflows/build.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ jobs:
1818
- { target: aarch64-apple-ios, os: macos-latest }
1919
- { target: x86_64-apple-darwin, os: macos-latest }
2020
- { target: aarch64-linux-android, os: ubuntu-latest }
21+
features:
22+
- {
23+
args: --no-default-features --features os-webview,
24+
key: no-default,
25+
}
26+
- { args: --all-features, key: all }
2127

2228
runs-on: ${{ matrix.platform.os }}
2329

@@ -42,18 +48,20 @@ jobs:
4248
cmd /C start /wait installwebview.exe /silent /install
4349
4450
- uses: Swatinem/rust-cache@v2
51+
with:
52+
save-if: ${{ matrix.features.key == 'all' }}
4553

4654
- name: build wry
47-
run: cargo build --target ${{ matrix.platform.target }} --all-features
55+
run: cargo build --target ${{ matrix.platform.target }} ${{ matrix.features.args }}
4856

4957
- name: build tests and examples
5058
shell: bash
5159
if: (!contains(matrix.platform.target, 'android') && !contains(matrix.platform.target, 'ios'))
52-
run: cargo test --no-run --verbose --target ${{ matrix.platform.target }}
60+
run: cargo test --no-run --verbose --target ${{ matrix.platform.target }} ${{ matrix.features.args }}
5361

5462
- name: run tests
5563
if: (!contains(matrix.platform.target, 'android') && !contains(matrix.platform.target, 'ios'))
56-
run: cargo test --verbose --target ${{ matrix.platform.target }} --features linux-body
64+
run: cargo test --verbose --target ${{ matrix.platform.target }} --features linux-body ${{ matrix.features.args }}
5765

5866
- name: install nightly
5967
uses: dtolnay/rust-toolchain@nightly
@@ -63,7 +71,7 @@ jobs:
6371

6472
- name: Run tests with miri
6573
if: (!contains(matrix.platform.target, 'android') && !contains(matrix.platform.target, 'ios'))
66-
run: cargo +nightly miri test --verbose --target ${{ matrix.platform.target }} --features linux-body
74+
run: cargo +nightly miri test --verbose --target ${{ matrix.platform.target }} --features linux-body ${{ matrix.features.args }}
6775

6876
doc:
6977
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ gdkx11 = { version = "0.18", optional = true }
6767
percent-encoding = "2.3"
6868
dirs = "6"
6969

70+
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dev-dependencies]
71+
x11-dl = { version = "2.21" }
72+
7073
[target."cfg(target_os = \"windows\")".dependencies]
7174
webview2-com = "0.38"
7275
windows-version = "0.1"

examples/async_custom_protocol.rs

Lines changed: 103 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,116 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::path::PathBuf;
6-
7-
use tao::{
8-
event::{Event, WindowEvent},
9-
event_loop::{ControlFlow, EventLoop},
10-
window::WindowBuilder,
11-
};
12-
use wry::{
13-
http::{header::CONTENT_TYPE, Request, Response},
14-
WebViewBuilder,
15-
};
16-
175
fn main() -> wry::Result<()> {
18-
let event_loop = EventLoop::new();
19-
let window = WindowBuilder::new().build(&event_loop).unwrap();
6+
imp::main()
7+
}
208

21-
let builder = WebViewBuilder::new()
22-
.with_asynchronous_custom_protocol("wry".into(), move |_webview_id, request, responder| {
23-
match get_wry_response(request) {
24-
Ok(http_response) => responder.respond(http_response),
25-
Err(e) => responder.respond(
26-
http::Response::builder()
27-
.header(CONTENT_TYPE, "text/plain")
28-
.status(500)
29-
.body(e.to_string().as_bytes().to_vec())
30-
.unwrap(),
31-
),
32-
}
33-
})
34-
// tell the webview to load the custom protocol
35-
.with_url("wry://localhost");
9+
#[cfg(not(feature = "protocol"))]
10+
mod imp {
11+
pub fn main() -> wry::Result<()> {
12+
unimplemented!()
13+
}
14+
}
15+
16+
#[cfg(feature = "protocol")]
17+
mod imp {
18+
use std::path::PathBuf;
3619

37-
#[cfg(any(
38-
target_os = "windows",
39-
target_os = "macos",
40-
target_os = "ios",
41-
target_os = "android"
42-
))]
43-
let _webview = builder.build(&window)?;
44-
#[cfg(not(any(
45-
target_os = "windows",
46-
target_os = "macos",
47-
target_os = "ios",
48-
target_os = "android"
49-
)))]
50-
let _webview = {
51-
use tao::platform::unix::WindowExtUnix;
52-
use wry::WebViewBuilderExtUnix;
53-
let vbox = window.default_vbox().unwrap();
54-
builder.build_gtk(vbox)?
20+
use tao::{
21+
event::{Event, WindowEvent},
22+
event_loop::{ControlFlow, EventLoop},
23+
window::WindowBuilder,
24+
};
25+
use wry::{
26+
http::{header::CONTENT_TYPE, Request, Response},
27+
WebViewBuilder,
5528
};
5629

57-
event_loop.run(move |event, _, control_flow| {
58-
*control_flow = ControlFlow::Wait;
30+
pub fn main() -> wry::Result<()> {
31+
let event_loop = EventLoop::new();
32+
let window = WindowBuilder::new().build(&event_loop).unwrap();
5933

60-
if let Event::WindowEvent {
61-
event: WindowEvent::CloseRequested,
62-
..
63-
} = event
64-
{
65-
*control_flow = ControlFlow::Exit
66-
}
67-
});
68-
}
34+
let builder = WebViewBuilder::new()
35+
.with_asynchronous_custom_protocol("wry".into(), move |_webview_id, request, responder| {
36+
match get_wry_response(request) {
37+
Ok(http_response) => responder.respond(http_response),
38+
Err(e) => responder.respond(
39+
http::Response::builder()
40+
.header(CONTENT_TYPE, "text/plain")
41+
.status(500)
42+
.body(e.to_string().as_bytes().to_vec())
43+
.unwrap(),
44+
),
45+
}
46+
})
47+
// tell the webview to load the custom protocol
48+
.with_url("wry://localhost");
6949

70-
fn get_wry_response(
71-
request: Request<Vec<u8>>,
72-
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
73-
let path = request.uri().path();
74-
// Read the file content from file path
75-
let root = PathBuf::from("examples/custom_protocol");
76-
let path = if path == "/" {
77-
"index.html"
78-
} else {
79-
// removing leading slash
80-
&path[1..]
81-
};
82-
let content = std::fs::read(std::fs::canonicalize(root.join(path))?)?;
50+
#[cfg(any(
51+
target_os = "windows",
52+
target_os = "macos",
53+
target_os = "ios",
54+
target_os = "android"
55+
))]
56+
let _webview = builder.build(&window)?;
57+
#[cfg(not(any(
58+
target_os = "windows",
59+
target_os = "macos",
60+
target_os = "ios",
61+
target_os = "android"
62+
)))]
63+
let _webview = {
64+
use tao::platform::unix::WindowExtUnix;
65+
use wry::WebViewBuilderExtUnix;
66+
let vbox = window.default_vbox().unwrap();
67+
builder.build_gtk(vbox)?
68+
};
8369

84-
// Return asset contents and mime types based on file extentions
85-
// If you don't want to do this manually, there are some crates for you.
86-
// Such as `infer` and `mime_guess`.
87-
let mimetype = if path.ends_with(".html") || path == "/" {
88-
"text/html"
89-
} else if path.ends_with(".js") {
90-
"text/javascript"
91-
} else if path.ends_with(".png") {
92-
"image/png"
93-
} else if path.ends_with(".wasm") {
94-
"application/wasm"
95-
} else {
96-
unimplemented!();
97-
};
70+
event_loop.run(move |event, _, control_flow| {
71+
*control_flow = ControlFlow::Wait;
72+
73+
if let Event::WindowEvent {
74+
event: WindowEvent::CloseRequested,
75+
..
76+
} = event
77+
{
78+
*control_flow = ControlFlow::Exit
79+
}
80+
});
81+
}
82+
83+
fn get_wry_response(
84+
request: Request<Vec<u8>>,
85+
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
86+
let path = request.uri().path();
87+
// Read the file content from file path
88+
let root = PathBuf::from("examples/custom_protocol");
89+
let path = if path == "/" {
90+
"index.html"
91+
} else {
92+
// removing leading slash
93+
&path[1..]
94+
};
95+
let content = std::fs::read(std::fs::canonicalize(root.join(path))?)?;
96+
97+
// Return asset contents and mime types based on file extentions
98+
// If you don't want to do this manually, there are some crates for you.
99+
// Such as `infer` and `mime_guess`.
100+
let mimetype = if path.ends_with(".html") || path == "/" {
101+
"text/html"
102+
} else if path.ends_with(".js") {
103+
"text/javascript"
104+
} else if path.ends_with(".png") {
105+
"image/png"
106+
} else if path.ends_with(".wasm") {
107+
"application/wasm"
108+
} else {
109+
unimplemented!();
110+
};
98111

99-
Response::builder()
100-
.header(CONTENT_TYPE, mimetype)
101-
.body(content)
102-
.map_err(Into::into)
112+
Response::builder()
113+
.header(CONTENT_TYPE, mimetype)
114+
.body(content)
115+
.map_err(Into::into)
116+
}
103117
}

0 commit comments

Comments
 (0)