-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linux): support adding to
gtk::Fixed
(#1128)
* feat(linux): support adding to `gtk::Fixed` * fix doctests * again
- Loading branch information
Showing
4 changed files
with
280 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wry": "patch" | ||
--- | ||
|
||
On Linux, apply passed webview bounds when using `WebView::new_gtk` or `WebViewBuilder::new_gtk` with `gtk::Fixed` widget. This allows to create multiple webviews inside `gtk::Fixed` in the same window. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2020-2023 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use tao::{ | ||
event::{Event, WindowEvent}, | ||
event_loop::{ControlFlow, EventLoop}, | ||
window::WindowBuilder, | ||
}; | ||
use wry::{Rect, WebViewBuilder}; | ||
|
||
fn main() -> wry::Result<()> { | ||
let event_loop = EventLoop::new(); | ||
let window = WindowBuilder::new().build(&event_loop).unwrap(); | ||
|
||
#[cfg(not(any( | ||
target_os = "windows", | ||
target_os = "macos", | ||
target_os = "ios", | ||
target_os = "android" | ||
)))] | ||
let fixed = { | ||
use gtk::prelude::*; | ||
use tao::platform::unix::WindowExtUnix; | ||
|
||
let fixed = gtk::Fixed::new(); | ||
let vbox = window.default_vbox().unwrap(); | ||
vbox.pack_start(&fixed, true, true, 0); | ||
fixed.show_all(); | ||
fixed | ||
}; | ||
|
||
let create_webview_builder = || { | ||
#[cfg(any( | ||
target_os = "windows", | ||
target_os = "macos", | ||
target_os = "ios", | ||
target_os = "android" | ||
))] | ||
return WebViewBuilder::new_as_child(&window); | ||
|
||
#[cfg(not(any( | ||
target_os = "windows", | ||
target_os = "macos", | ||
target_os = "ios", | ||
target_os = "android" | ||
)))] | ||
{ | ||
use wry::WebViewBuilderExtUnix; | ||
WebViewBuilder::new_gtk(&fixed) | ||
} | ||
}; | ||
|
||
let size = window.inner_size().to_logical::<u32>(window.scale_factor()); | ||
|
||
let webview = create_webview_builder() | ||
.with_bounds(Rect { | ||
x: 0, | ||
y: 0, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}) | ||
.with_url("https://tauri.app")? | ||
.build()?; | ||
let webview2 = create_webview_builder() | ||
.with_bounds(Rect { | ||
x: (size.width / 2) as i32, | ||
y: 0, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}) | ||
.with_url("https://github.com/tauri-apps/wry")? | ||
.build()?; | ||
let webview3 = create_webview_builder() | ||
.with_bounds(Rect { | ||
x: 0, | ||
y: (size.height / 2) as i32, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}) | ||
.with_url("https://twitter.com/TauriApps")? | ||
.build()?; | ||
let webview4 = create_webview_builder() | ||
.with_bounds(Rect { | ||
x: (size.width / 2) as i32, | ||
y: (size.height / 2) as i32, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}) | ||
.with_url("https://google.com")? | ||
.build()?; | ||
|
||
event_loop.run(move |event, _, control_flow| { | ||
*control_flow = ControlFlow::Wait; | ||
|
||
match event { | ||
Event::WindowEvent { | ||
event: WindowEvent::Resized(size), | ||
.. | ||
} => { | ||
let size = size.to_logical::<u32>(window.scale_factor()); | ||
webview.set_bounds(Rect { | ||
x: 0, | ||
y: 0, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}); | ||
webview2.set_bounds(Rect { | ||
x: (size.width / 2) as i32, | ||
y: 0, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}); | ||
webview3.set_bounds(Rect { | ||
x: 0, | ||
y: (size.height / 2) as i32, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}); | ||
webview4.set_bounds(Rect { | ||
x: (size.width / 2) as i32, | ||
y: (size.height / 2) as i32, | ||
width: size.width / 2, | ||
height: size.height / 2, | ||
}); | ||
} | ||
Event::WindowEvent { | ||
event: WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = ControlFlow::Exit, | ||
_ => {} | ||
} | ||
}); | ||
} |
Oops, something went wrong.