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 4 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
5 changes: 3 additions & 2 deletions src/compositor.rs
Original file line number Diff line number Diff line change
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
28 changes: 5 additions & 23 deletions src/verso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use arboard::Clipboard;
use base::id::{PipelineNamespace, PipelineNamespaceId, WebViewId};
use base::id::{PipelineNamespace, PipelineNamespaceId};
use bluetooth::BluetoothThreadFactory;
use bluetooth_traits::BluetoothRequest;
use canvas::canvas_paint_thread::CanvasPaintThread;
Expand All @@ -26,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 @@ -43,7 +41,6 @@ use winit::{
use crate::{
compositor::{IOCompositor, InitialCompositorState, ShutdownState},
config::Config,
webview::WebView,
window::Window,
};

Expand Down Expand Up @@ -84,7 +81,7 @@ impl Verso {
config.init();
// Reserving a namespace to create TopLevelBrowsingContextId.
PipelineNamespace::install(PipelineNamespaceId(0));
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 @@ -362,14 +359,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 url = ServoUrl::parse("verso://panel.html").unwrap();
send_to_constellation(
&constellation_sender,
ConstellationMsg::NewWebView(url, panel_id),
);
window.init_panel_webview(&constellation_sender);

let mut windows = HashMap::new();
windows.insert(window.id(), (window, webrender_document));
Expand Down Expand Up @@ -437,16 +427,8 @@ impl Verso {
compositor,
) {
let mut window =
Window::new_with_compositor(evl, compositor);
let panel_id = WebViewId::new();
let url =
ServoUrl::parse("verso://panel.html").unwrap();
send_to_constellation(
&self.constellation_sender,
ConstellationMsg::NewWebView(url, panel_id),
);
let rect = DeviceIntRect::from_size(window.size());
window.panel = Some(WebView::new(panel_id, rect));
Window::new_with_compositor(evl, compositor, true);
window.init_panel_webview(&self.constellation_sender);
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 @@ -29,12 +29,8 @@ impl WebView {
Self { webview_id, rect }
}

/// 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 @@ -164,7 +160,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
59 changes: 50 additions & 9 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -61,7 +62,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 @@ -93,16 +94,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(
WebViewId::new(),
DeviceIntRect::from_size(size),
)),
panel,
webview: None,
mouse_position: Cell::new(PhysicalPosition::default()),
modifiers_state: Cell::new(ModifiersState::default()),
Expand All @@ -112,7 +114,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 @@ -133,10 +139,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 @@ -145,6 +156,36 @@ 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(WebViewId::new(), DeviceIntRect::from_size(size))
}

/// 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, constellation_sender: &Sender<ConstellationMsg>) {
if let Some(panel) = &self.panel {
let panel_id = panel.webview_id;
let url = ServoUrl::parse("verso://panel.html").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