Skip to content

Commit

Permalink
epi/eframe: move options out of trait App into new NativeOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed May 8, 2021
1 parent 5e46bd4 commit 7374ed9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 40 deletions.
1 change: 1 addition & 0 deletions eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to the `eframe` crate.


## Unreleased
* Moved options out of `trait App` into new `NativeOptions`.


## 0.11.0 - 2021-04-05
Expand Down
3 changes: 2 additions & 1 deletion eframe/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ impl epi::App for MyApp {
}

fn main() {
eframe::run_native(Box::new(MyApp::default()));
let options = eframe::NativeOptions::default();
eframe::run_native(Box::new(MyApp::default()), options);
}
7 changes: 5 additions & 2 deletions eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

pub use {egui, epi};

#[cfg(not(target_arch = "wasm32"))]
pub use epi::NativeOptions;

// ----------------------------------------------------------------------------
// When compiling for web

Expand Down Expand Up @@ -56,6 +59,6 @@ pub fn start_web(canvas_id: &str, app: Box<dyn epi::App>) -> Result<(), wasm_bin

/// Call from `fn main` like this: `eframe::run_native(Box::new(MyEguiApp::default()))`
#[cfg(not(target_arch = "wasm32"))]
pub fn run_native(app: Box<dyn epi::App>) -> ! {
egui_glium::run(app)
pub fn run_native(app: Box<dyn epi::App>, native_options: epi::NativeOptions) -> ! {
egui_glium::run(app, native_options)
}
7 changes: 6 additions & 1 deletion egui_demo_app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
// When compiling natively:
fn main() {
let app = egui_demo_lib::WrapApp::default();
eframe::run_native(Box::new(app));
let options = eframe::NativeOptions {
// Let's show off that we support transparent windows
transparent: true,
..Default::default()
};
eframe::run_native(Box::new(app), options);
}
4 changes: 0 additions & 4 deletions egui_demo_lib/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ impl epi::App for WrapApp {
self.backend_panel.max_size_points_active
}

// Let's show off that we support transparent windows (on native):
fn transparent(&self) -> bool {
true
}
fn clear_color(&self) -> egui::Rgba {
egui::Rgba::TRANSPARENT // we set a `CentralPanel` fill color in `demo_windows.rs`
}
Expand Down
23 changes: 13 additions & 10 deletions egui_glium/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,22 @@ fn window_builder_drag_and_drop(

fn create_display(
app: &dyn epi::App,
native_options: &epi::NativeOptions,
window_settings: Option<WindowSettings>,
window_icon: Option<glutin::window::Icon>,
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
) -> glium::Display {
let mut window_builder = glutin::window::WindowBuilder::new()
.with_decorations(app.decorated())
.with_resizable(app.is_resizable())
.with_decorations(native_options.decorated)
.with_resizable(native_options.resizable)
.with_title(app.name())
.with_window_icon(window_icon)
.with_transparent(app.transparent());
.with_transparent(native_options.transparent);

window_builder = window_builder_drag_and_drop(window_builder, app.drag_and_drop_support());
window_builder =
window_builder_drag_and_drop(window_builder, native_options.drag_and_drop_support);

let initial_size_points = app.initial_window_size();
let initial_size_points = native_options.initial_window_size;

if let Some(window_settings) = &window_settings {
window_builder = window_settings.initialize_size(window_builder);
Expand Down Expand Up @@ -154,13 +156,14 @@ fn integration_info(
}
}

fn load_icon(icon_data: Option<epi::IconData>) -> Option<glutin::window::Icon> {
let icon_data = icon_data?;
fn load_icon(icon_data: epi::IconData) -> Option<glutin::window::Icon> {
glutin::window::Icon::from_rgba(icon_data.rgba, icon_data.width, icon_data.height).ok()
}

// ----------------------------------------------------------------------------

/// Run an egui app
pub fn run(mut app: Box<dyn epi::App>) -> ! {
pub fn run(mut app: Box<dyn epi::App>, nativve_options: epi::NativeOptions) -> ! {

This comment has been minimized.

Copy link
@follower

follower May 8, 2021

Contributor

FYI: Argument name typo: nativve_options -> native_options

let mut storage = create_storage(app.name());

if let Some(storage) = &mut storage {
Expand All @@ -169,8 +172,8 @@ pub fn run(mut app: Box<dyn epi::App>) -> ! {

let window_settings = deserialize_window_settings(&storage);
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let icon = load_icon(app.icon_data());
let display = create_display(&*app, window_settings, icon, &event_loop);
let icon = nativve_options.icon_data.clone().and_then(load_icon);
let display = create_display(&*app, &nativve_options, window_settings, icon, &event_loop);

let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
event_loop.create_proxy(),
Expand Down
2 changes: 2 additions & 0 deletions egui_glium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub mod window_settings;
pub use backend::*;
pub use painter::Painter;

pub use epi::NativeOptions;

use {
copypasta::ClipboardProvider,
egui::*,
Expand Down
50 changes: 28 additions & 22 deletions epi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,11 @@ pub trait App {
/// The name of your App.
fn name(&self) -> &str;

/// The initial size of the native window in points (logical pixels).
fn initial_window_size(&self) -> Option<egui::Vec2> {
None
}

/// Time between automatic calls to `save()`
fn auto_save_interval(&self) -> std::time::Duration {
std::time::Duration::from_secs(30)
}

/// Returns true if this app window should be resizable.
fn is_resizable(&self) -> bool {
true
}

/// The size limit of the web app canvas
fn max_size_points(&self) -> egui::Vec2 {
// Some browsers get slow with huge WebGL canvases, so we limit the size:
Expand All @@ -137,33 +127,49 @@ pub trait App {
// `transparent()` option they get immediate results.
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).into()
}
}

/// Options controlling the behavior of a native window
#[derive(Clone)]
pub struct NativeOptions {
/// The application icon, e.g. in the Windows task bar etc.
fn icon_data(&self) -> Option<IconData> {
None
}
pub icon_data: Option<IconData>,

/// The initial size of the native window in points (logical pixels).
pub initial_window_size: Option<egui::Vec2>,

/// Should the app window be resizable?
pub resizable: bool,

/// On desktop: add window decorations (i.e. a frame around your app)?
/// If false it will be difficult to move and resize the app.
fn decorated(&self) -> bool {
true
}
pub decorated: bool,

/// On desktop: make the window transparent.
/// You control the transparency with [`Self::clear_color()`].
/// You control the transparency with [`App::clear_color()`].
/// You should avoid having a [`egui::CentralPanel`], or make sure its frame is also transparent.
fn transparent(&self) -> bool {
false
}
pub transparent: bool,

/// On Windows: enable drag and drop support.
/// Set to false to avoid issues with crates such as cpal which uses that use multi-threaded COM API <https://github.com/rust-windowing/winit/pull/1524>
fn drag_and_drop_support(&self) -> bool {
true
pub drag_and_drop_support: bool,
}

impl Default for NativeOptions {
fn default() -> Self {
Self {
icon_data: None,
initial_window_size: None,
resizable: true,
decorated: true,
transparent: false,
drag_and_drop_support: true,
}
}
}

/// Image data for the icon.
#[derive(Clone)]
pub struct IconData {
/// RGBA pixels.
pub rgba: Vec<u8>,
Expand Down

0 comments on commit 7374ed9

Please sign in to comment.