Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make control panel optional #201

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use webrender_api::{
};
use webrender_traits::display_list::{HitTestInfo, ScrollTree};
use webrender_traits::{
CompositorHitTestResult, CrossProcessCompositorMessage, ImageUpdate, UntrustedNodeAddress
CompositorHitTestResult, CrossProcessCompositorMessage, ImageUpdate, UntrustedNodeAddress,
};
use winit::window::WindowId;

Expand Down Expand Up @@ -572,7 +572,7 @@ impl IOCompositor {

CompositorMsg::CrossProcess(cross_proces_message) => {
self.handle_cross_process_message(cross_proces_message);
},
}
}

true
Expand Down Expand Up @@ -707,11 +707,11 @@ impl IOCompositor {
match update {
ImageUpdate::AddImage(key, desc, data) => {
txn.add_image(key, desc, data.into(), None)
},
}
ImageUpdate::DeleteImage(key) => txn.delete_image(key),
ImageUpdate::UpdateImage(key, desc, data) => {
txn.update_image(key, desc, data.into(), &DirtyRect::All)
},
}
}
}
self.webrender_api
Expand All @@ -727,7 +727,7 @@ impl IOCompositor {
transaction.add_native_font(font_key, native_handle);
self.webrender_api
.send_transaction(self.webrender_document, transaction);
},
}

CrossProcessCompositorMessage::AddFontInstance(
font_instance_key,
Expand Down Expand Up @@ -777,13 +777,13 @@ impl IOCompositor {
if let Err(e) = req.send(self.viewport.into()) {
warn!("Sending response to get client window failed ({:?}).", e);
}
},
}

CrossProcessCompositorMessage::GetScreenSize(req) => {
if let Err(e) = req.send(self.viewport) {
warn!("Sending response to get screen size failed ({:?}).", e);
}
},
}

CrossProcessCompositorMessage::GetAvailableScreenSize(req) => {
if let Err(e) = req.send(self.viewport) {
Expand Down Expand Up @@ -833,7 +833,7 @@ impl IOCompositor {
.map(|_| self.webrender_api.generate_font_instance_key())
.collect();
let _ = result_sender.send((font_keys, font_instance_keys));
},
}
CompositorMsg::CrossProcess(CrossProcessCompositorMessage::GetClientWindowRect(
req,
)) => {
Expand Down Expand Up @@ -1240,9 +1240,10 @@ impl IOCompositor {
self.on_resize_webview_event(panel.webview_id, rect);
}

let rect = DeviceIntRect::from_size(size);
let content_size = window.get_content_rect(rect);
if let Some(w) = &mut window.webview {
let rect = DeviceIntRect::from_size(size);
w.set_size(rect);
w.set_size(content_size);
self.on_resize_webview_event(w.webview_id, w.rect);
}

Expand Down
36 changes: 8 additions & 28 deletions src/verso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ use std::{
};

use arboard::Clipboard;
use base::id::WebViewId;
use bluetooth::BluetoothThreadFactory;
use bluetooth_traits::BluetoothRequest;
use canvas::canvas_paint_thread::CanvasPaintThread;
use compositing_traits::{
CompositorMsg, CompositorProxy, CompositorReceiver, ConstellationMsg,
};
use compositing_traits::{CompositorMsg, CompositorProxy, CompositorReceiver, ConstellationMsg};
use constellation::{Constellation, FromCompositorLogger, InitialConstellationState};
use crossbeam_channel::{unbounded, Sender};
use devtools;
Expand All @@ -29,9 +26,7 @@ use profile;
use script::{self, JSEngineSetup};
use script_traits::WindowSizeData;
use servo_config::{opts, pref};
use servo_url::ServoUrl;
use style;
use units::DeviceIntRect;
use webgpu;
use webrender::{create_webrender_instance, ShaderPrecacheFlags, WebRenderOptions};
use webrender_api::*;
Expand All @@ -46,7 +41,6 @@ use winit::{
use crate::{
compositor::{IOCompositor, InitialCompositorState, ShutdownState},
config::Config,
webview::WebView,
window::Window,
};

Expand Down Expand Up @@ -86,7 +80,7 @@ impl Verso {
// Initialize configurations and Verso window
let resource_dir = config.resource_dir.clone();
config.init();
let (window, rendering_context) = Window::new(evl);
let (mut window, rendering_context) = Window::new(evl, true);
let event_loop_waker = Box::new(Waker(proxy));
let opts = opts::get();

Expand Down Expand Up @@ -282,7 +276,8 @@ impl Verso {

// Create font cache thread
let system_font_service = Arc::new(
SystemFontService::spawn(compositor_sender.cross_process_compositor_api.clone()).to_proxy(),
SystemFontService::spawn(compositor_sender.cross_process_compositor_api.clone())
.to_proxy(),
);

// Create canvas thread
Expand Down Expand Up @@ -366,15 +361,7 @@ impl Verso {
opts.debug.convert_mouse_to_touch,
);

// Send the constellation message to start Panel UI
// TODO: Should become a window method
let panel_id = window.panel.as_ref().unwrap().webview_id;
let path = resource_dir.join("panel.html");
let url = ServoUrl::from_file_path(path.to_str().unwrap()).unwrap();
send_to_constellation(
&constellation_sender,
ConstellationMsg::NewWebView(url, panel_id),
);
window.init_panel_webview(&resource_dir, &constellation_sender);

let mut windows = HashMap::new();
windows.insert(window.id(), (window, webrender_document));
Expand Down Expand Up @@ -443,18 +430,11 @@ impl Verso {
compositor,
) {
let mut window =
Window::new_with_compositor(evl, compositor);
let panel_id = WebViewId::new();
let path = self.resource_dir.join("panel.html");
let url =
ServoUrl::from_file_path(path.to_str().unwrap())
.unwrap();
send_to_constellation(
Window::new_with_compositor(evl, compositor, true);
window.init_panel_webview(
&self.resource_dir,
&self.constellation_sender,
ConstellationMsg::NewWebView(url, panel_id),
);
let rect = DeviceIntRect::from_size(window.size());
window.panel = Some(WebView::new(panel_id, rect));
let webrender_document = document.clone();
self.windows
.insert(window.id(), (window, webrender_document));
Expand Down
10 changes: 3 additions & 7 deletions src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ impl WebView {
}
}

/// Set the webview size corresponding to the window size.
pub fn set_size(&mut self, mut rect: DeviceIntRect) {
rect.min.y = rect.max.y.min(100);
rect.min.x += 10;
rect.max.y -= 10;
rect.max.x -= 10;
/// Set the webview size.
pub fn set_size(&mut self, rect: DeviceIntRect) {
self.rect = rect;
}
}
Expand Down Expand Up @@ -185,7 +181,7 @@ impl Window {
let size = self.size();
let rect = DeviceIntRect::from_size(size);
let mut webview = WebView::new(demo_id, rect);
webview.set_size(rect);
webview.set_size(self.get_content_rect(rect));
self.webview = Some(webview);
send_to_constellation(sender, ConstellationMsg::NewWebView(demo_url, demo_id));
log::debug!("Verso Window {:?} adds webview {}", self.id(), demo_id);
Expand Down
63 changes: 56 additions & 7 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::Cell;
use std::{cell::Cell, path::Path};

use base::id::WebViewId;
use compositing_traits::ConstellationMsg;
Expand All @@ -11,6 +11,7 @@ use glutin::{
};
use glutin_winit::DisplayBuilder;
use script_traits::{TouchEventType, WheelDelta, WheelMode};
use servo_url::ServoUrl;
use webrender_api::{
units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize, DevicePoint, LayoutVector2D},
ScrollLocation,
Expand Down Expand Up @@ -51,7 +52,7 @@ pub struct Window {

impl Window {
/// Create a Verso window from Winit window and return the rendering context.
pub fn new(evl: &ActiveEventLoop) -> (Self, RenderingContext) {
pub fn new(evl: &ActiveEventLoop, with_panel: bool) -> (Self, RenderingContext) {
let window_attributes = WinitWindow::default_attributes()
.with_transparent(true)
.with_decorations(false);
Expand Down Expand Up @@ -83,13 +84,17 @@ impl Window {
.expect("Failed to create rendering context");
log::trace!("Created rendering context for window {:?}", window);

let size = window.inner_size();
let size = Size2D::new(size.width as i32, size.height as i32);
let panel = if with_panel {
Some(Self::create_control_panel(&window))
} else {
None
};

(
Self {
window,
surface,
panel: Some(WebView::new_panel(DeviceIntRect::from_size(size))),
panel,
webview: None,
mouse_position: Cell::new(PhysicalPosition::default()),
modifiers_state: Cell::new(ModifiersState::default()),
Expand All @@ -99,7 +104,11 @@ impl Window {
}

/// Create a Verso window with the rendering context.
pub fn new_with_compositor(evl: &ActiveEventLoop, compositor: &mut IOCompositor) -> Self {
pub fn new_with_compositor(
evl: &ActiveEventLoop,
compositor: &mut IOCompositor,
with_panel: bool,
) -> Self {
let window = evl
.create_window(WinitWindow::default_attributes())
// .with_transparent(true)
Expand All @@ -120,10 +129,15 @@ impl Window {
.rendering_context
.create_surface(&window)
.unwrap();
let panel = if with_panel {
Some(Self::create_control_panel(&window))
} else {
None
};
let mut window = Self {
window,
surface,
panel: None,
panel,
webview: None,
mouse_position: Cell::new(PhysicalPosition::default()),
modifiers_state: Cell::new(ModifiersState::default()),
Expand All @@ -132,6 +146,41 @@ impl Window {
window
}

/// Create the control panel
fn create_control_panel(window: &winit::window::Window) -> WebView {
let size = window.inner_size();
let size = Size2D::new(size.width as i32, size.height as i32);
WebView::new_panel(DeviceIntRect::from_size(size))
}
Legend-Master marked this conversation as resolved.
Show resolved Hide resolved

/// Get the content area size for the webview to draw on
pub fn get_content_rect(&self, mut size: DeviceIntRect) -> DeviceIntRect {
if self.panel.is_some() {
size.min.y = size.max.y.min(100);
size.min.x += 10;
size.max.y -= 10;
size.max.x -= 10;
}
size
}

/// Send the constellation message to start Panel UI
pub fn init_panel_webview(
&mut self,
Legend-Master marked this conversation as resolved.
Show resolved Hide resolved
resource_dir: &Path,
constellation_sender: &Sender<ConstellationMsg>,
) {
if let Some(panel) = &self.panel {
let panel_id = panel.webview_id;
let path = resource_dir.join("panel.html");
let url = ServoUrl::from_file_path(path.to_str().unwrap()).unwrap();
send_to_constellation(
constellation_sender,
ConstellationMsg::NewWebView(url, panel_id),
);
}
}

/// Handle Winit window event and return a boolean to indicate if the compositor should repaint immediately.
pub fn handle_winit_window_event(
&mut self,
Expand Down
Loading