|
| 1 | +use winit::{ |
| 2 | + application::ApplicationHandler, |
| 3 | + event::WindowEvent, |
| 4 | + event_loop::{ActiveEventLoop, EventLoop}, |
| 5 | + window::{Window, WindowId}, |
| 6 | +}; |
| 7 | +use wry::{ |
| 8 | + Rect, WebViewBuilder, |
| 9 | + dpi::{LogicalPosition, LogicalSize}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Default)] |
| 13 | +struct State { |
| 14 | + window: Option<Window>, |
| 15 | + webview1: Option<wry::WebView>, |
| 16 | + webview2: Option<wry::WebView>, |
| 17 | + webview3: Option<wry::WebView>, |
| 18 | + webview4: Option<wry::WebView>, |
| 19 | +} |
| 20 | + |
| 21 | +impl ApplicationHandler for State { |
| 22 | + fn resumed(&mut self, event_loop: &ActiveEventLoop) { |
| 23 | + let mut attributes = Window::default_attributes(); |
| 24 | + attributes.inner_size = Some(LogicalSize::new(800, 800).into()); |
| 25 | + let window = event_loop.create_window(attributes).unwrap(); |
| 26 | + |
| 27 | + let size = window.inner_size().to_logical::<u32>(window.scale_factor()); |
| 28 | + |
| 29 | + let webview1 = WebViewBuilder::new() |
| 30 | + .with_bounds(Rect { |
| 31 | + position: LogicalPosition::new(0, 0).into(), |
| 32 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 33 | + }) |
| 34 | + .with_url("https://tauri.app") |
| 35 | + .build_as_child(&window) |
| 36 | + .unwrap(); |
| 37 | + |
| 38 | + let webview2 = WebViewBuilder::new() |
| 39 | + .with_bounds(Rect { |
| 40 | + position: LogicalPosition::new(size.width / 2, 0).into(), |
| 41 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 42 | + }) |
| 43 | + .with_url("https://github.com/tauri-apps/wry") |
| 44 | + .build_as_child(&window) |
| 45 | + .unwrap(); |
| 46 | + |
| 47 | + let webview3 = WebViewBuilder::new() |
| 48 | + .with_bounds(Rect { |
| 49 | + position: LogicalPosition::new(0, size.height / 2).into(), |
| 50 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 51 | + }) |
| 52 | + .with_url("https://twitter.com/TauriApps") |
| 53 | + .build_as_child(&window) |
| 54 | + .unwrap(); |
| 55 | + |
| 56 | + let webview4 = WebViewBuilder::new() |
| 57 | + .with_bounds(Rect { |
| 58 | + position: LogicalPosition::new(size.width / 2, size.height / 2).into(), |
| 59 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 60 | + }) |
| 61 | + .with_url("https://google.com") |
| 62 | + .build_as_child(&window) |
| 63 | + .unwrap(); |
| 64 | + |
| 65 | + self.window = Some(window); |
| 66 | + self.webview1 = Some(webview1); |
| 67 | + self.webview2 = Some(webview2); |
| 68 | + self.webview3 = Some(webview3); |
| 69 | + self.webview4 = Some(webview4); |
| 70 | + } |
| 71 | + |
| 72 | + fn window_event( |
| 73 | + &mut self, |
| 74 | + _event_loop: &ActiveEventLoop, |
| 75 | + _window_id: WindowId, |
| 76 | + event: WindowEvent, |
| 77 | + ) { |
| 78 | + match event { |
| 79 | + WindowEvent::Resized(size) => { |
| 80 | + if let ( |
| 81 | + Some(window), |
| 82 | + Some(webview1), |
| 83 | + Some(webview2), |
| 84 | + Some(webview3), |
| 85 | + Some(webview4), |
| 86 | + ) = |
| 87 | + (&self.window, &self.webview1, &self.webview2, &self.webview3, &self.webview4) |
| 88 | + { |
| 89 | + let size = size.to_logical::<u32>(window.scale_factor()); |
| 90 | + |
| 91 | + webview1 |
| 92 | + .set_bounds(Rect { |
| 93 | + position: LogicalPosition::new(0, 0).into(), |
| 94 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 95 | + }) |
| 96 | + .unwrap(); |
| 97 | + |
| 98 | + webview2 |
| 99 | + .set_bounds(Rect { |
| 100 | + position: LogicalPosition::new(size.width / 2, 0).into(), |
| 101 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 102 | + }) |
| 103 | + .unwrap(); |
| 104 | + |
| 105 | + webview3 |
| 106 | + .set_bounds(Rect { |
| 107 | + position: LogicalPosition::new(0, size.height / 2).into(), |
| 108 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 109 | + }) |
| 110 | + .unwrap(); |
| 111 | + |
| 112 | + webview4 |
| 113 | + .set_bounds(Rect { |
| 114 | + position: LogicalPosition::new(size.width / 2, size.height / 2).into(), |
| 115 | + size: LogicalSize::new(size.width / 2, size.height / 2).into(), |
| 116 | + }) |
| 117 | + .unwrap(); |
| 118 | + } |
| 119 | + }, |
| 120 | + WindowEvent::CloseRequested => { |
| 121 | + std::process::exit(0); |
| 122 | + }, |
| 123 | + _ => {}, |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { |
| 128 | + #[cfg(any( |
| 129 | + target_os = "linux", |
| 130 | + target_os = "dragonfly", |
| 131 | + target_os = "freebsd", |
| 132 | + target_os = "netbsd", |
| 133 | + target_os = "openbsd", |
| 134 | + ))] |
| 135 | + { |
| 136 | + while gtk::events_pending() { |
| 137 | + gtk::main_iteration_do(false); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +fn main() -> wry::Result<()> { |
| 144 | + #[cfg(any( |
| 145 | + target_os = "linux", |
| 146 | + target_os = "dragonfly", |
| 147 | + target_os = "freebsd", |
| 148 | + target_os = "netbsd", |
| 149 | + target_os = "openbsd", |
| 150 | + ))] |
| 151 | + { |
| 152 | + use gtk::prelude::DisplayExtManual; |
| 153 | + |
| 154 | + gtk::init()?; |
| 155 | + if gtk::gdk::Display::default().unwrap().backend().is_wayland() { |
| 156 | + panic!("This example doesn't support wayland!"); |
| 157 | + } |
| 158 | + |
| 159 | + winit::platform::x11::register_xlib_error_hook(Box::new(|_display, error| { |
| 160 | + let error = error as *mut x11_dl::xlib::XErrorEvent; |
| 161 | + (unsafe { (*error).error_code }) == 170 |
| 162 | + })); |
| 163 | + } |
| 164 | + |
| 165 | + let event_loop = EventLoop::new().unwrap(); |
| 166 | + let mut state = State::default(); |
| 167 | + event_loop.run_app(&mut state).unwrap(); |
| 168 | + |
| 169 | + Ok(()) |
| 170 | +} |
0 commit comments