Skip to content

Commit

Permalink
added full screen handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenton Hamaluik committed Mar 23, 2020
1 parent 1e0c962 commit cf4a184
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
34 changes: 34 additions & 0 deletions examples/fullscreen/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use cef_simple::{Cef, WindowOptions};
use simplelog::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let cef = Cef::initialize(None, true)?;

CombinedLogger::init(vec![TermLogger::new(
LevelFilter::Trace,
Config::default(),
TerminalMode::Mixed,
)
.unwrap()])
.unwrap();

let page = urlencoding::encode(include_str!("page.html"));

cef.open_window(WindowOptions {
url: format!("data:text/html,{}", page),
title: Some("CEF Simple—Fullscreen Demo".to_string()),
window_icon: Some(include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/icon.png"
))),
window_app_icon: Some(include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/icon.png"
))),
..WindowOptions::default()
})?;

cef.run()?;

Ok(())
}
17 changes: 17 additions & 0 deletions examples/fullscreen/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CEF Fullscreen Demo</title>
</head>
<body>
<script>
function toggleFullscreen() {
if(document.fullscreenElement === null) document.documentElement.requestFullscreen();
else document.exitFullscreen();
}
</script>
<a href="#" onclick="toggleFullscreen(); return false;">Click here</a> to go fullscreen.
</body>
</html>
6 changes: 3 additions & 3 deletions src/imp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::bindings::{
cef_base_ref_counted_t, cef_browser_t, cef_client_t, cef_context_menu_handler_t,
cef_display_handler_t, cef_frame_t, cef_life_span_handler_t, cef_process_id_t,
cef_process_message_t, cef_request_handler_t, cef_string_t, cef_string_userfree_t,
cef_string_userfree_utf16_free,
cef_string_userfree_utf16_free, cef_window_t,
};
use super::context_menu_handler::{self, ContextMenuHandler};
use super::display_handler::{self, DisplayHandler};
Expand Down Expand Up @@ -225,7 +225,7 @@ unsafe extern "C" fn on_process_message_received(
}
}

pub fn allocate() -> *mut Client {
pub fn allocate(window: *mut cef_window_t) -> *mut Client {
let client = Client {
client: cef_client_t {
base: cef_base_ref_counted_t {
Expand Down Expand Up @@ -254,7 +254,7 @@ pub fn allocate() -> *mut Client {
life_span_handler: life_span_handler::allocate(),
context_menu_handler: context_menu_handler::allocate(),
request_handler: request_handler::allocate(),
display_handler: display_handler::allocate(),
display_handler: display_handler::allocate(window),
};

Box::into_raw(Box::from(client))
Expand Down
17 changes: 14 additions & 3 deletions src/imp/display_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use super::bindings::{
cef_base_ref_counted_t, cef_browser_t, cef_display_handler_t, cef_log_severity_t,
cef_log_severity_t_LOGSEVERITY_DEBUG, cef_log_severity_t_LOGSEVERITY_DEFAULT,
cef_log_severity_t_LOGSEVERITY_ERROR, cef_log_severity_t_LOGSEVERITY_FATAL,
cef_log_severity_t_LOGSEVERITY_INFO, cef_log_severity_t_LOGSEVERITY_WARNING, cef_string_t,
cef_log_severity_t_LOGSEVERITY_INFO, cef_log_severity_t_LOGSEVERITY_WARNING, cef_string_t, cef_window_t,
};

#[repr(C)]
pub struct DisplayHandler {
display_handler: cef_display_handler_t,
ref_count: AtomicUsize,
window: *mut cef_window_t,
}

impl DisplayHandler {
Expand All @@ -21,6 +22,15 @@ impl DisplayHandler {
}
}

unsafe extern "C" fn on_fullscreen_mode_change(
slf: *mut cef_display_handler_t,
_browser: *mut cef_browser_t,
fullscreen: i32,
) {
let handler = slf as *mut DisplayHandler;
(*(*handler).window).set_fullscreen.expect("set_fullscreen exists")((*handler).window, fullscreen);
}

extern "C" fn on_console_message(
_slf: *mut cef_display_handler_t,
_browser: *mut cef_browser_t,
Expand Down Expand Up @@ -50,7 +60,7 @@ extern "C" fn on_console_message(
1
}

pub fn allocate() -> *mut DisplayHandler {
pub fn allocate(window: *mut cef_window_t) -> *mut DisplayHandler {
let handler = DisplayHandler {
display_handler: cef_display_handler_t {
base: cef_base_ref_counted_t {
Expand All @@ -63,13 +73,14 @@ pub fn allocate() -> *mut DisplayHandler {
on_address_change: None,
on_title_change: None,
on_favicon_urlchange: None,
on_fullscreen_mode_change: None,
on_fullscreen_mode_change: Some(on_fullscreen_mode_change),
on_tooltip: None,
on_status_message: None,
on_console_message: Some(on_console_message),
on_auto_resize: None,
on_loading_progress_change: None,
},
window,
ref_count: AtomicUsize::new(1),
};

Expand Down
2 changes: 1 addition & 1 deletion src/imp/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extern "C" fn window_delegate_created(slf: *mut cef_window_delegate_t, window: *
browser_settings.local_storage = cef_state_t_STATE_DISABLED;
browser_settings.application_cache = cef_state_t_STATE_DISABLED;

let client = client::allocate();
let client = client::allocate(window);
let browser_view_delegate = browser_view_delegate::allocate();

let browser_view = unsafe {
Expand Down

0 comments on commit cf4a184

Please sign in to comment.