Skip to content
Permalink
Browse files

WR update: remove dependency on icon_loading feature

  • Loading branch information...
paulrouget committed Mar 7, 2019
1 parent a20455f commit 92a1336264a9c5521926c45874a17341a565614c
Showing with 58 additions and 69 deletions.
  1. +1 −32 Cargo.lock
  2. +3 −1 ports/servo/Cargo.toml
  3. +3 −3 ports/servo/glutin_app/keyutils.rs
  4. +51 −33 ports/servo/glutin_app/window.rs

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -53,7 +53,9 @@ libc = "0.2"
log = "0.4"
rust-webvr = { version = "0.10.2", features = ["glwindow"] }
tinyfiledialogs = "3.0"
winit = {version = "0.18", features = ["icon_loading"]}

[target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies]
image = "0.21"

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
osmesa-sys = "0.1.2"
@@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use keyboard_types::{Code, Key, KeyboardEvent, KeyState, Modifiers, Location};
use winit::{ElementState, KeyboardInput, ModifiersState, VirtualKeyCode};
use glutin::{ElementState, KeyboardInput, ModifiersState, VirtualKeyCode};

// Some shortcuts use Cmd on Mac and Control on other systems.
#[cfg(target_os = "macos")]
@@ -18,7 +18,7 @@ pub const CMD_OR_ALT: Modifiers = Modifiers::META;
pub const CMD_OR_ALT: Modifiers = Modifiers::ALT;

fn get_servo_key_from_winit_key(key: Option<VirtualKeyCode>) -> Key {
use winit::VirtualKeyCode::*;
use glutin::VirtualKeyCode::*;
// TODO: figure out how to map NavigateForward, NavigateBackward
// TODO: map the remaining keys if possible
let key = if let Some(key) = key {
@@ -127,7 +127,7 @@ fn get_servo_key_from_winit_key(key: Option<VirtualKeyCode>) -> Key {
}

fn get_servo_location_from_winit_key(key: Option<VirtualKeyCode>) -> Location {
use winit::VirtualKeyCode::*;
use glutin::VirtualKeyCode::*;
// TODO: add more numpad keys
let key = if let Some(key) = key {
key
@@ -7,6 +7,8 @@
use euclid::{TypedPoint2D, TypedVector2D, TypedScale, TypedSize2D};
use gleam::gl;
use glutin::{Api, ContextBuilder, GlContext, GlRequest, GlWindow};
#[cfg(any(target_os = "linux", target_os = "windows"))]
use image;
use keyboard_types::{Key, KeyboardEvent, KeyState};
use rust_webvr::GlWindowVRService;
use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent};
@@ -32,18 +34,20 @@ use std::time;
use super::keyutils::keyboard_event_from_winit;
#[cfg(target_os = "windows")]
use winapi;
use winit::{ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, KeyboardInput};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
use glutin::{ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, KeyboardInput};
#[cfg(any(target_os = "linux", target_os = "windows"))]
use glutin::Icon;
use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")]
use winit::os::macos::{ActivationPolicy, WindowBuilderExt};
use glutin::os::macos::{ActivationPolicy, WindowBuilderExt};

// This should vary by zoom level and maybe actual text size (focused or under cursor)
pub const LINE_HEIGHT: f32 = 38.0;

const MULTISAMPLES: u16 = 16;

#[cfg(target_os = "macos")]
fn builder_with_platform_options(mut builder: winit::WindowBuilder) -> winit::WindowBuilder {
fn builder_with_platform_options(mut builder: glutin::WindowBuilder) -> glutin::WindowBuilder {
if opts::get().headless || opts::get().output_file.is_some() {
// Prevent the window from showing in Dock.app, stealing focus,
// or appearing at all when running in headless mode or generating an
@@ -54,7 +58,7 @@ fn builder_with_platform_options(mut builder: winit::WindowBuilder) -> winit::Wi
}

#[cfg(not(target_os = "macos"))]
fn builder_with_platform_options(builder: winit::WindowBuilder) -> winit::WindowBuilder {
fn builder_with_platform_options(builder: glutin::WindowBuilder) -> glutin::WindowBuilder {
builder
}

@@ -132,7 +136,7 @@ impl HeadlessContext {
}

enum WindowKind {
Window(GlWindow, RefCell<winit::EventsLoop>),
Window(GlWindow, RefCell<glutin::EventsLoop>),
Headless(HeadlessContext),
}

@@ -141,7 +145,7 @@ pub struct Window {
kind: WindowKind,
screen_size: TypedSize2D<u32, DeviceIndependentPixel>,
inner_size: Cell<TypedSize2D<u32, DeviceIndependentPixel>>,
mouse_down_button: Cell<Option<winit::MouseButton>>,
mouse_down_button: Cell<Option<glutin::MouseButton>>,
mouse_down_point: Cell<TypedPoint2D<i32, DevicePixel>>,
event_queue: RefCell<Vec<WindowEvent>>,
mouse_pos: Cell<TypedPoint2D<i32, DevicePixel>>,
@@ -187,22 +191,15 @@ impl Window {
inner_size = TypedSize2D::new(width as u32, height as u32);
WindowKind::Headless(HeadlessContext::new(width as u32, height as u32))
} else {
let events_loop = winit::EventsLoop::new();
let mut window_builder = winit::WindowBuilder::new()
let events_loop = glutin::EventsLoop::new();
let mut window_builder = glutin::WindowBuilder::new()
.with_title("Servo".to_string())
.with_decorations(!opts::get().no_native_titlebar)
.with_transparency(opts::get().no_native_titlebar)
.with_dimensions(LogicalSize::new(width as f64, height as f64))
.with_visibility(visible)
.with_multitouch();

#[cfg(any(target_os = "linux", target_os = "windows"))]
{
let icon_bytes = include_bytes!("../../../resources/servo64.png");
let icon = Some(winit::Icon::from_bytes(icon_bytes).expect("Failed to open icon"));
window_builder = window_builder.with_window_icon(icon);
}

window_builder = builder_with_platform_options(window_builder);

let mut context_builder = ContextBuilder::new()
@@ -216,6 +213,12 @@ impl Window {
let glutin_window = GlWindow::new(window_builder, context_builder, &events_loop)
.expect("Failed to create window.");

#[cfg(any(target_os = "linux", target_os = "windows"))]
{
let icon_bytes = include_bytes!("../../../resources/servo64.png");
glutin_window.set_window_icon(Some(load_icon(icon_bytes)));
}

unsafe {
glutin_window
.context()
@@ -362,9 +365,9 @@ impl Window {
}
}
if stop || self.is_animating() {
winit::ControlFlow::Break
glutin::ControlFlow::Break
} else {
winit::ControlFlow::Continue
glutin::ControlFlow::Continue
}
});
}
@@ -442,7 +445,7 @@ impl Window {
}
}

fn winit_event_to_servo_event(&self, event: winit::Event) {
fn winit_event_to_servo_event(&self, event: glutin::Event) {
if let WindowKind::Window(ref window, _) = self.kind {
if let Event::WindowEvent { window_id, .. } = event {
if window.id() != window_id {
@@ -452,27 +455,27 @@ impl Window {
}
match event {
Event::WindowEvent {
event: winit::WindowEvent::ReceivedCharacter(ch),
event: glutin::WindowEvent::ReceivedCharacter(ch),
..
} => self.handle_received_character(ch),
Event::WindowEvent {
event:
winit::WindowEvent::KeyboardInput {
glutin::WindowEvent::KeyboardInput {
input,
..
},
..
} => self.handle_keyboard_input(input),
Event::WindowEvent {
event: winit::WindowEvent::MouseInput { state, button, .. },
event: glutin::WindowEvent::MouseInput { state, button, .. },
..
} => {
if button == MouseButton::Left || button == MouseButton::Right {
self.handle_mouse(button, state, self.mouse_pos.get());
}
},
Event::WindowEvent {
event: winit::WindowEvent::CursorMoved { position, .. },
event: glutin::WindowEvent::CursorMoved { position, .. },
..
} => {
let pos = position.to_physical(self.device_hidpi_factor().get() as f64);
@@ -485,7 +488,7 @@ impl Window {
)));
},
Event::WindowEvent {
event: winit::WindowEvent::MouseWheel { delta, phase, .. },
event: glutin::WindowEvent::MouseWheel { delta, phase, .. },
..
} => {
let (mut dx, mut dy) = match delta {
@@ -510,7 +513,7 @@ impl Window {
self.event_queue.borrow_mut().push(event);
},
Event::WindowEvent {
event: winit::WindowEvent::Touch(touch),
event: glutin::WindowEvent::Touch(touch),
..
} => {
use servo::script_traits::TouchId;
@@ -526,17 +529,17 @@ impl Window {
.push(WindowEvent::Touch(phase, id, point));
},
Event::WindowEvent {
event: winit::WindowEvent::Refresh,
event: glutin::WindowEvent::Refresh,
..
} => self.event_queue.borrow_mut().push(WindowEvent::Refresh),
Event::WindowEvent {
event: winit::WindowEvent::CloseRequested,
event: glutin::WindowEvent::CloseRequested,
..
} => {
self.event_queue.borrow_mut().push(WindowEvent::Quit);
},
Event::WindowEvent {
event: winit::WindowEvent::Resized(size),
event: glutin::WindowEvent::Resized(size),
..
} => {
// size is DeviceIndependentPixel.
@@ -569,8 +572,8 @@ impl Window {
/// Helper function to handle a click
fn handle_mouse(
&self,
button: winit::MouseButton,
action: winit::ElementState,
button: glutin::MouseButton,
action: glutin::ElementState,
coords: TypedPoint2D<i32, DevicePixel>,
) {
use servo::script_traits::MouseButton;
@@ -629,7 +632,7 @@ impl Window {
pub fn set_cursor(&self, cursor: Cursor) {
match self.kind {
WindowKind::Window(ref window, ..) => {
use winit::MouseCursor;
use glutin::MouseCursor;

let winit_cursor = match cursor {
Cursor::Default => MouseCursor::Default,
@@ -741,7 +744,7 @@ impl WindowMethods for Window {

fn create_event_loop_waker(&self) -> Box<dyn EventLoopWaker> {
struct GlutinEventLoopWaker {
proxy: Option<Arc<winit::EventsLoopProxy>>,
proxy: Option<Arc<glutin::EventsLoopProxy>>,
}
impl GlutinEventLoopWaker {
fn new(window: &Window) -> GlutinEventLoopWaker {
@@ -799,7 +802,7 @@ impl WindowMethods for Window {
let name = String::from("Test VR Display");
let size = self.inner_size.get().to_f64();
let size = LogicalSize::new(size.width, size.height);
let mut window_builder = winit::WindowBuilder::new()
let mut window_builder = glutin::WindowBuilder::new()
.with_title(name.clone())
.with_dimensions(size)
.with_visibility(false)
@@ -828,3 +831,18 @@ fn winit_phase_to_touch_event_type(phase: TouchPhase) -> TouchEventType {
TouchPhase::Cancelled => TouchEventType::Cancel,
}
}

#[cfg(any(target_os = "linux", target_os = "windows"))]
fn load_icon(icon_bytes: &[u8]) -> Icon {
let (icon_rgba, icon_width, icon_height) = {
use image::{GenericImageView, Pixel};
let image = image::load_from_memory(icon_bytes).expect("Failed to load icon");;
let (width, height) = image.dimensions();
let mut rgba = Vec::with_capacity((width * height) as usize * 4);
for (_, _, pixel) in image.pixels() {
rgba.extend_from_slice(&pixel.to_rgba().data);
}
(rgba, width, height)
};
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to load icon")
}

0 comments on commit 92a1336

Please sign in to comment.
You can’t perform that action at this time.