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

Allow setting initial window size from cli #228

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 44 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use net_traits::{
ResourceFetchTiming,
};
use servo_config::opts::{default_opts, set_options, Opts};
use winit::{dpi, window::WindowAttributes};

/// Command line arguments.
#[derive(Clone, Debug, Default)]
Expand All @@ -19,6 +20,8 @@ pub struct CliArgs {
pub ipc_channel: Option<String>,
/// Should launch without control panel
pub no_panel: bool,
/// Window settings for the initial winit window
pub window_attributes: WindowAttributes,
}

/// Configuration of Verso instance.
Expand All @@ -45,6 +48,19 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
);
opts.optflag("", "no-panel", "Launch Verso without control panel");

opts.optopt(
"w",
"width",
"Initial window's width in physical unit, the height command line arg much also be set",
"",
);
opts.optopt(
"h",
"height",
"Initial window's height in physical unit, the width command line arg much also be set",
"",
);

let matches: getopts::Matches = opts.parse(&args[1..])?;
let url = matches
.opt_str("url")
Expand All @@ -63,10 +79,38 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
let ipc_channel = matches.opt_str("ipc-channel");
let no_panel = matches.opt_present("no-panel");

let width = matches.opt_get::<u32>("width").unwrap_or_else(|e| {
log::error!("Failed to parse width command line argument: {e}");
wusyong marked this conversation as resolved.
Show resolved Hide resolved
None
});
let height = matches.opt_get::<u32>("height").unwrap_or_else(|e| {
log::error!("Failed to parse height command line argument: {e}");
None
});

let size = match (width, height) {
(None, Some(_height)) => {
log::error!("Invalid size command line argument, height is present but not width");
None
}
(Some(_width), None) => {
log::error!("Invalid size command line argument, width is present but not height");
None
}
(Some(width), Some(height)) => Some(dpi::PhysicalSize::new(width, height)),
_ => None,
};
let mut window_attributes = winit::window::Window::default_attributes();

if let Some(size) = size {
window_attributes = window_attributes.with_inner_size(size);
}

Ok(CliArgs {
url,
ipc_channel,
no_panel,
window_attributes,
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/verso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ impl Verso {
let protocols = config.create_protocols();
let initial_url = config.args.url.clone();
let with_panel = !config.args.no_panel;
let window_settings = config.args.window_attributes.clone();

config.init();
// Reserving a namespace to create TopLevelBrowsingContextId.
PipelineNamespace::install(PipelineNamespaceId(0));
let (mut window, rendering_context) = Window::new(evl);
let (mut window, rendering_context) = Window::new(evl, window_settings);

let event_loop_waker = Box::new(Waker(proxy));
let opts = opts::get();
Expand Down
9 changes: 6 additions & 3 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use winit::{
event::{ElementState, TouchPhase, WindowEvent},
event_loop::ActiveEventLoop,
keyboard::ModifiersState,
window::{CursorIcon, Window as WinitWindow, WindowId},
window::{CursorIcon, Window as WinitWindow, WindowAttributes, WindowId},
};

use crate::{
Expand Down Expand Up @@ -56,8 +56,11 @@ pub struct Window {

impl Window {
/// Create a Verso window from Winit window and return the rendering context.
pub fn new(evl: &ActiveEventLoop) -> (Self, RenderingContext) {
let window_attributes = WinitWindow::default_attributes()
pub fn new(
evl: &ActiveEventLoop,
window_attributes: WindowAttributes,
) -> (Self, RenderingContext) {
let window_attributes = window_attributes
.with_transparent(true)
.with_decorations(false);

Expand Down