Skip to content

Commit 0e0b9a2

Browse files
authored
Merge pull request #25 from nini22P/move-focus
add move_focus for windows
2 parents 10341b2 + aacd8a9 commit 0e0b9a2

4 files changed

Lines changed: 91 additions & 8 deletions

File tree

fltk-webview-sys/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "fltk-webview-sys"
33
version = "0.4.1"
4-
authors = ["MoAlyousef <mohammed.alyousef@neurosrg.com>", "Adia Robbie <adyaro37@gmail.com>"]
4+
authors = [
5+
"MoAlyousef <mohammed.alyousef@neurosrg.com>",
6+
"Adia Robbie <adyaro37@gmail.com>",
7+
]
58
edition = "2018"
69
description = "Webview for embedded fltk windows"
710
repository = "https://github.com/fltk-rs/fltk-webview"
@@ -16,4 +19,6 @@ wv = "0.2.1"
1619

1720
[build-dependencies]
1821
pkg-config = "0.3"
19-
cc = "1"
22+
cc = "1"
23+
serde = { version = "1", features = ["derive"] }
24+
serde_json = "1"

fltk-webview-sys/build.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
use serde::Deserialize;
2+
use std::path::PathBuf;
3+
use std::process::Command;
4+
15
fn main() {
26
println!("cargo:rerun-if-changed=src/gtk_helper.c");
37
println!("cargo:rerun-if-changed=src/cocoa_helper.m");
8+
println!("cargo:rerun-if-changed=src/win32_helper.c");
49

510
#[cfg(target_os = "macos")]
611
compile_cocoa_helper();
712

8-
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
13+
#[cfg(target_os = "linux")]
914
compile_gtk_helper();
15+
16+
#[cfg(target_os = "windows")]
17+
compile_win32_helper();
1018
}
1119

12-
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
20+
#[cfg(target_os = "linux")]
1321
fn compile_gtk_helper() {
1422
let cflags = std::process::Command::new("pkg-config")
1523
.args(&["--cflags", "gtk+-3.0"])
@@ -31,3 +39,53 @@ fn compile_cocoa_helper() {
3139
build.file("src/cocoa_helper.m");
3240
build.compile("cocoa");
3341
}
42+
43+
#[cfg(target_os = "windows")]
44+
fn compile_win32_helper() {
45+
let wv_sys_path = find_wv_sys_path();
46+
let mut build = cc::Build::new();
47+
build.file("src/win32_helper.c");
48+
build.include(&wv_sys_path.join("libs\\include"));
49+
build.include(&wv_sys_path.join("webview\\core\\include"));
50+
build.compile("windows");
51+
}
52+
53+
#[derive(Debug, Deserialize)]
54+
struct Metadata {
55+
packages: Vec<Package>,
56+
}
57+
#[derive(Debug, Deserialize)]
58+
struct Package {
59+
name: String,
60+
manifest_path: PathBuf,
61+
}
62+
63+
fn find_wv_sys_path() -> PathBuf {
64+
let output = Command::new("cargo")
65+
.arg("metadata")
66+
.output()
67+
.expect("Failed to run cargo metadata");
68+
69+
let metadata: Metadata =
70+
serde_json::from_slice(&output.stdout).expect("Failed to parse cargo metadata");
71+
72+
let wv_sys_pkg = metadata
73+
.packages
74+
.into_iter()
75+
.find(|pkg| pkg.name == "wv-sys")
76+
.expect("Could not find 'wv-sys' package in metadata");
77+
78+
let path = wv_sys_pkg
79+
.manifest_path
80+
.parent()
81+
.expect("manifest_path has no parent");
82+
83+
if !path.exists() {
84+
panic!(
85+
"Failed to find headers at: {}. wv-sys internal layout may have changed.",
86+
path.display()
87+
);
88+
}
89+
90+
path.to_path_buf()
91+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "webview.h"
2+
#include "WebView2.h"
3+
4+
void move_focus(webview_t webview)
5+
{
6+
if (webview)
7+
{
8+
ICoreWebView2Controller *controller_ptr =
9+
(ICoreWebView2Controller *)webview_get_native_handle(
10+
webview, WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER);
11+
if (controller_ptr)
12+
{
13+
controller_ptr->lpVtbl->MoveFocus(
14+
controller_ptr, COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
15+
}
16+
}
17+
}

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ impl FromFltkWindow for Webview {
2727
unsafe {
2828
#[cfg(target_os = "windows")]
2929
{
30+
extern "C" {
31+
pub fn move_focus(wv: *mut wv::webview_t);
32+
}
3033
extern "system" {
3134
pub fn SetFocus(child: *mut ()) -> *mut ();
3235
pub fn CoInitializeEx(pvReserved: *mut (), dwCoInit: u32) -> i32;
@@ -76,28 +79,28 @@ impl FromFltkWindow for Webview {
7679
win.h(),
7780
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED,
7881
);
82+
move_focus(inner as *mut _);
7983
win.resize_callback(move |w, _, _, _, _| {
8084
wv::webview_set_size(inner, w.w(), w.h(), 3);
8185
});
8286
win.resize(win.x(), win.y(), win.w(), win.h());
83-
win.handle(|w, ev| {
87+
win.handle(move |_w, ev| {
8488
if ev == enums::Event::Focus {
85-
SetFocus(w.raw_handle() as _);
89+
move_focus(inner as *mut _);
8690
true
8791
} else {
8892
false
8993
}
9094
});
9195
let mut topwin =
9296
window::Window::from_widget_ptr(win.top_window().unwrap().as_widget_ptr());
93-
// SetFocus(topwin.raw_handle() as _);
9497
topwin.set_callback(|t| {
9598
if app::event() == enums::Event::Close {
9699
t.hide();
97100
}
98101
});
99102
topwin.assume_derived();
100-
topwin.handle(|w, ev| match ev {
103+
topwin.handle(move |w, ev| match ev {
101104
fltk::enums::Event::Push => {
102105
SetFocus(w.raw_handle() as _);
103106
true

0 commit comments

Comments
 (0)