Skip to content

Commit

Permalink
eframe: add function to set, query and toggle fullscreen mode
Browse files Browse the repository at this point in the history
Closes #674
  • Loading branch information
emilk committed Jul 29, 2022
1 parent c6c6d2d commit 1c71f09
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
18 changes: 18 additions & 0 deletions eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ pub struct NativeOptions {
/// If false it will be difficult to move and resize the app.
pub decorated: bool,

/// Start in (borderless) fullscreen?
///
/// Default: `false`.
pub fullscreen: bool,

/// On Windows: enable drag and drop support. Drag and drop can
/// not be disabled on other platforms.
///
Expand Down Expand Up @@ -270,6 +275,7 @@ impl Default for NativeOptions {
always_on_top: false,
maximized: false,
decorated: true,
fullscreen: false,
drag_and_drop_support: true,
icon_data: None,
initial_window_pos: None,
Expand Down Expand Up @@ -547,11 +553,17 @@ impl Frame {
}

/// Set whether to show window decorations (i.e. a frame around you app).
///
/// If false it will be difficult to move and resize the app.
pub fn set_decorations(&mut self, decorated: bool) {
self.output.decorated = Some(decorated);
}

/// Turn borderless fullscreen on/off (native only).
pub fn set_fullscreen(&mut self, fullscreen: bool) {
self.output.fullscreen = Some(fullscreen);
}

/// set the position of the outer window
pub fn set_window_pos(&mut self, pos: egui::Pos2) {
self.output.window_pos = Some(pos);
Expand Down Expand Up @@ -591,6 +603,9 @@ pub struct WindowInfo {
/// Unit: egui points (logical pixels).
pub position: egui::Pos2,

/// Are we in fullscreen mode?
pub fullscreen: bool,

/// Window inner size in egui points (logical pixels).
pub size: egui::Vec2,
}
Expand Down Expand Up @@ -740,6 +755,9 @@ pub mod backend {
/// Set to some bool to change window decorations.
pub decorated: Option<bool>,

/// Set to some bool to change window fullscreen.
pub fullscreen: Option<bool>,

/// Set to true to drag window while primary mouse button is down.
pub drag_window: bool,

Expand Down
10 changes: 9 additions & 1 deletion eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn read_window_info(
.to_logical::<f32>(pixels_per_point.into());
Some(WindowInfo {
position: egui::Pos2 { x: pos.x, y: pos.y },
fullscreen: window.fullscreen().is_some(),
size: egui::Vec2 {
x: size.width,
y: size.height,
Expand All @@ -39,6 +40,7 @@ pub fn window_builder(
always_on_top,
maximized,
decorated,
fullscreen,
drag_and_drop_support,
icon_data,
initial_window_pos,
Expand All @@ -54,8 +56,9 @@ pub fn window_builder(

let mut window_builder = winit::window::WindowBuilder::new()
.with_always_on_top(*always_on_top)
.with_maximized(*maximized)
.with_decorations(*decorated)
.with_fullscreen(fullscreen.then(|| winit::window::Fullscreen::Borderless(None)))
.with_maximized(*maximized)
.with_resizable(*resizable)
.with_transparent(*transparent)
.with_window_icon(window_icon);
Expand Down Expand Up @@ -118,6 +121,7 @@ pub fn handle_app_output(
window_size,
window_title,
decorated,
fullscreen,
drag_window,
window_pos,
visible,
Expand All @@ -137,6 +141,10 @@ pub fn handle_app_output(
);
}

if let Some(fullscreen) = fullscreen {
window.set_fullscreen(fullscreen.then(|| winit::window::Fullscreen::Borderless(None)));
}

if let Some(window_title) = window_title {
window.set_title(&window_title);
}
Expand Down
1 change: 1 addition & 0 deletions eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl AppRunner {
window_size: _, // Can't resize a web page
window_title: _, // TODO(emilk): change title of window
decorated: _, // Can't toggle decorations
fullscreen: _, // TODO(emilk): fullscreen web window
drag_window: _, // Can't be dragged
window_pos: _, // Can't set position of a web page
visible: _, // Can't hide a web page
Expand Down
19 changes: 15 additions & 4 deletions egui-winit/src/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub struct WindowSettings {
/// the inner or outer position depending on the platform.
/// See [`winit::window::WindowAttributes`] for details.
position: Option<egui::Pos2>,

fullscreen: bool,

/// Inner size of window in logical pixels
inner_size_points: Option<egui::Vec2>,
}
Expand All @@ -29,6 +32,9 @@ impl WindowSettings {

Self {
position,

fullscreen: window.fullscreen().is_some(),

inner_size_points: Some(egui::vec2(
inner_size_points.width,
inner_size_points.height,
Expand All @@ -55,10 +61,15 @@ impl WindowSettings {
}

if let Some(inner_size_points) = self.inner_size_points {
window.with_inner_size(winit::dpi::LogicalSize {
width: inner_size_points.x as f64,
height: inner_size_points.y as f64,
})
window
.with_inner_size(winit::dpi::LogicalSize {
width: inner_size_points.x as f64,
height: inner_size_points.y as f64,
})
.with_fullscreen(
self.fullscreen
.then(|| winit::window::Fullscreen::Borderless(None)),
)
} else {
window
}
Expand Down
28 changes: 19 additions & 9 deletions egui_demo_app/src/backend_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,25 @@ impl BackendPanel {
}
}

if !frame.is_web()
&& ui
.button("📱 Phone Size")
.on_hover_text("Resize the window to be small like a phone.")
.clicked()
{
// frame.set_window_size(egui::Vec2::new(375.0, 812.0)); // iPhone 12 mini
frame.set_window_size(egui::Vec2::new(375.0, 667.0)); // iPhone SE 2nd gen
ui.close_menu();
if !frame.is_web() {
ui.horizontal(|ui| {
if let Some(window_info) = &frame.info().window_info {
let mut fullscreen = window_info.fullscreen;
ui.checkbox(&mut fullscreen, "🗖 Fullscreen")
.on_hover_text("Fullscreen the window");
frame.set_fullscreen(fullscreen);
}

if ui
.button("📱 Phone Size")
.on_hover_text("Resize the window to be small like a phone.")
.clicked()
{
// frame.set_window_size(egui::Vec2::new(375.0, 812.0)); // iPhone 12 mini
frame.set_window_size(egui::Vec2::new(375.0, 667.0)); // iPhone SE 2nd gen
ui.close_menu();
}
});
}
}

Expand Down

0 comments on commit 1c71f09

Please sign in to comment.