Skip to content

Commit

Permalink
Merge pull request #48 from tauri-apps/webview2
Browse files Browse the repository at this point in the history
Adopt Webview2 on Windows
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) committed Feb 20, 2021
2 parents 95f9684 + e399de2 commit fbf0d17
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 188 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ gdk-pixbuf = "0.9"
glib = "0.10"

[target.'cfg(target_os = "windows")'.dependencies]
windows = "0.2"
webview2 = "0.1.0-beta.1"
winapi = { version = "0.3", features = ["libloaderapi"] }
winit = "0.24"

[target.'cfg(target_os = "windows")'.build-dependencies]
cc = "1"
windows = "0.2.1"

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.24"
core-graphics = "0.22"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> Result<()> {
There are also more samples under `examples`, you can enter commands like following to try them:

```
cargo run --example basic
cargo run --example multiwindow
```

For more information, please read the documentation below.
Expand Down
24 changes: 1 addition & 23 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,4 @@ fn main() {
}

#[cfg(target_os = "windows")]
fn main() {
windows::build!(
windows::foundation::collections::*
windows::foundation::{AsyncStatus, Rect, Uri}
windows::web::ui::{IWebViewControl, WebViewControlScriptNotifyEventArgs }
windows::web::ui::interop::{WebViewControl, WebViewControlProcess }
windows::win32::com::CoWaitForMultipleHandles
windows::win32::display_devices::RECT
windows::win32::system_services::{CreateEventA, SetEvent, INFINITE}
windows::win32::windows_and_messaging::{GetClientRect, HWND}
windows::win32::winrt::RoInitialize
);

let mut build = cc::Build::new();
build
.include("src/collections.h")
.file("src/collections.cpp")
.flag_if_supported("/std:c++17");

println!("cargo:rerun-if-changed=src/collections.h");
println!("cargo:rerun-if-changed=src/collections.cpp");
build.compile("collections");
}
fn main() {}
54 changes: 54 additions & 0 deletions examples/winit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use wry::{
platform::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
Result,
};

fn main() -> Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let webview = WebViewBuilder::new(window)
.unwrap()
.initialize_script("menacing = 'ゴ';")
.add_callback("world", |dispatcher, sequence, requests| {
dispatcher
.dispatch_script("console.log(menacing);")
.unwrap();
// Sequence is a number counting how many times this function being called.
if sequence < 8 {
println!("{} seconds has passed.", sequence);
} else {
// Requests is a vector of parameters passed from the caller.
println!("{:?}", requests);
}
0
})
.load_url("https://tauri.studio")?
.build()?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::WindowEvent {
event: WindowEvent::Resized(_),
..
} => {
webview.resize().unwrap();
}
Event::MainEventsCleared => {
webview.window().request_redraw();
}
Event::RedrawRequested(_) => {}
_ => (),
}
});
}
25 changes: 3 additions & 22 deletions src/application/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ use winit::{
window::{Fullscreen, Icon as WinitIcon, Window, WindowAttributes, WindowBuilder},
};

#[cfg(target_os = "windows")]
use libc::c_void;
#[cfg(target_os = "windows")]
use winit::platform::windows::WindowExtWindows;

#[allow(dead_code)]
#[cfg(target_os = "windows")]
mod bindings {
::windows::include_bindings!();
}
#[cfg(target_os = "windows")]
use bindings::windows::win32::windows_and_messaging::*;

use std::{collections::HashMap, sync::mpsc::channel};

type EventLoopProxy = winit::event_loop::EventLoopProxy<Message>;
Expand Down Expand Up @@ -149,7 +136,7 @@ impl App for InnerApplication {
}
}
WindowEvent::Resized(_) => {
windows[&window_id].resize();
windows[&window_id].resize().unwrap();
}
_ => {}
},
Expand Down Expand Up @@ -265,16 +252,10 @@ fn load_icon(icon: Icon) -> crate::Result<WinitIcon> {
let icon = WinitIcon::from_rgba(rgba, width, height)?;
Ok(icon)
}
#[cfg(target_os = "windows")]
extern "C" {
fn SkipTaskbar(window: HWND) -> c_void;
}

#[cfg(target_os = "windows")]
fn skip_taskbar(window: &Window) {
unsafe {
SkipTaskbar(HWND(window.hwnd() as isize));
}
fn skip_taskbar(_window: &Window) {
// TODO
}

fn _create_window(
Expand Down
13 changes: 3 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,14 @@ pub enum Error {
SendMessageError(#[from] SendError<Message>),
#[error(transparent)]
UrlError(#[from] ParseError),
#[cfg(target_os = "windows")]
#[error("Windows error: {0:?}")]
WinrtError(windows::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("image error: {0}")]
Image(#[from] image::ImageError),
#[cfg(not(target_os = "linux"))]
#[error("Icon error: {0}")]
Icon(#[from] BadIcon),
}

#[cfg(target_os = "windows")]
impl From<windows::Error> for Error {
fn from(error: windows::Error) -> Self {
Error::WinrtError(error)
}
#[cfg(target_os = "windows")]
#[error(transparent)]
WebView2Error(#[from] webview2::Error),
}
2 changes: 2 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub(crate) use win::*;

#[cfg(target_os = "linux")]
pub use gtk::*;
#[cfg(target_os = "windows")]
pub use winapi;
#[cfg(not(target_os = "linux"))]
pub use winit::*;

Expand Down
Loading

0 comments on commit fbf0d17

Please sign in to comment.