epi/eframe: move options out of trait App
into new NativeOptions
This commit is contained in:
parent
5e46bd404c
commit
7374ed9d00
8 changed files with 57 additions and 40 deletions
|
@ -3,6 +3,7 @@ All notable changes to the `eframe` crate.
|
||||||
|
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
* Moved options out of `trait App` into new `NativeOptions`.
|
||||||
|
|
||||||
|
|
||||||
## 0.11.0 - 2021-04-05
|
## 0.11.0 - 2021-04-05
|
||||||
|
|
|
@ -41,5 +41,6 @@ impl epi::App for MyApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
eframe::run_native(Box::new(MyApp::default()));
|
let options = eframe::NativeOptions::default();
|
||||||
|
eframe::run_native(Box::new(MyApp::default()), options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@
|
||||||
|
|
||||||
pub use {egui, epi};
|
pub use {egui, epi};
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub use epi::NativeOptions;
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// When compiling for web
|
// When compiling for web
|
||||||
|
|
||||||
|
@ -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()))`
|
/// Call from `fn main` like this: `eframe::run_native(Box::new(MyEguiApp::default()))`
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub fn run_native(app: Box<dyn epi::App>) -> ! {
|
pub fn run_native(app: Box<dyn epi::App>, native_options: epi::NativeOptions) -> ! {
|
||||||
egui_glium::run(app)
|
egui_glium::run(app, native_options)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,5 +8,10 @@
|
||||||
// When compiling natively:
|
// When compiling natively:
|
||||||
fn main() {
|
fn main() {
|
||||||
let app = egui_demo_lib::WrapApp::default();
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,10 +54,6 @@ impl epi::App for WrapApp {
|
||||||
self.backend_panel.max_size_points_active
|
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 {
|
fn clear_color(&self) -> egui::Rgba {
|
||||||
egui::Rgba::TRANSPARENT // we set a `CentralPanel` fill color in `demo_windows.rs`
|
egui::Rgba::TRANSPARENT // we set a `CentralPanel` fill color in `demo_windows.rs`
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,20 +76,22 @@ fn window_builder_drag_and_drop(
|
||||||
|
|
||||||
fn create_display(
|
fn create_display(
|
||||||
app: &dyn epi::App,
|
app: &dyn epi::App,
|
||||||
|
native_options: &epi::NativeOptions,
|
||||||
window_settings: Option<WindowSettings>,
|
window_settings: Option<WindowSettings>,
|
||||||
window_icon: Option<glutin::window::Icon>,
|
window_icon: Option<glutin::window::Icon>,
|
||||||
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
|
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
|
||||||
) -> glium::Display {
|
) -> glium::Display {
|
||||||
let mut window_builder = glutin::window::WindowBuilder::new()
|
let mut window_builder = glutin::window::WindowBuilder::new()
|
||||||
.with_decorations(app.decorated())
|
.with_decorations(native_options.decorated)
|
||||||
.with_resizable(app.is_resizable())
|
.with_resizable(native_options.resizable)
|
||||||
.with_title(app.name())
|
.with_title(app.name())
|
||||||
.with_window_icon(window_icon)
|
.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 {
|
if let Some(window_settings) = &window_settings {
|
||||||
window_builder = window_settings.initialize_size(window_builder);
|
window_builder = window_settings.initialize_size(window_builder);
|
||||||
|
@ -154,13 +156,14 @@ fn integration_info(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_icon(icon_data: Option<epi::IconData>) -> Option<glutin::window::Icon> {
|
fn load_icon(icon_data: epi::IconData) -> Option<glutin::window::Icon> {
|
||||||
let icon_data = icon_data?;
|
|
||||||
glutin::window::Icon::from_rgba(icon_data.rgba, icon_data.width, icon_data.height).ok()
|
glutin::window::Icon::from_rgba(icon_data.rgba, icon_data.width, icon_data.height).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Run an egui app
|
/// 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) -> ! {
|
||||||
let mut storage = create_storage(app.name());
|
let mut storage = create_storage(app.name());
|
||||||
|
|
||||||
if let Some(storage) = &mut storage {
|
if let Some(storage) = &mut storage {
|
||||||
|
@ -169,8 +172,8 @@ pub fn run(mut app: Box<dyn epi::App>) -> ! {
|
||||||
|
|
||||||
let window_settings = deserialize_window_settings(&storage);
|
let window_settings = deserialize_window_settings(&storage);
|
||||||
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
||||||
let icon = load_icon(app.icon_data());
|
let icon = nativve_options.icon_data.clone().and_then(load_icon);
|
||||||
let display = create_display(&*app, window_settings, icon, &event_loop);
|
let display = create_display(&*app, &nativve_options, window_settings, icon, &event_loop);
|
||||||
|
|
||||||
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
|
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
|
||||||
event_loop.create_proxy(),
|
event_loop.create_proxy(),
|
||||||
|
|
|
@ -25,6 +25,8 @@ pub mod window_settings;
|
||||||
pub use backend::*;
|
pub use backend::*;
|
||||||
pub use painter::Painter;
|
pub use painter::Painter;
|
||||||
|
|
||||||
|
pub use epi::NativeOptions;
|
||||||
|
|
||||||
use {
|
use {
|
||||||
copypasta::ClipboardProvider,
|
copypasta::ClipboardProvider,
|
||||||
egui::*,
|
egui::*,
|
||||||
|
|
|
@ -108,21 +108,11 @@ pub trait App {
|
||||||
/// The name of your App.
|
/// The name of your App.
|
||||||
fn name(&self) -> &str;
|
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()`
|
/// Time between automatic calls to `save()`
|
||||||
fn auto_save_interval(&self) -> std::time::Duration {
|
fn auto_save_interval(&self) -> std::time::Duration {
|
||||||
std::time::Duration::from_secs(30)
|
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
|
/// The size limit of the web app canvas
|
||||||
fn max_size_points(&self) -> egui::Vec2 {
|
fn max_size_points(&self) -> egui::Vec2 {
|
||||||
// Some browsers get slow with huge WebGL canvases, so we limit the size:
|
// Some browsers get slow with huge WebGL canvases, so we limit the size:
|
||||||
|
@ -137,33 +127,49 @@ pub trait App {
|
||||||
// `transparent()` option they get immediate results.
|
// `transparent()` option they get immediate results.
|
||||||
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).into()
|
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The application icon, e.g. in the Windows task bar etc.
|
|
||||||
fn icon_data(&self) -> Option<IconData> {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Options controlling the behavior of a native window
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct NativeOptions {
|
||||||
|
/// The application icon, e.g. in the Windows task bar etc.
|
||||||
|
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)?
|
/// On desktop: add window decorations (i.e. a frame around your app)?
|
||||||
/// If false it will be difficult to move and resize the app.
|
/// If false it will be difficult to move and resize the app.
|
||||||
fn decorated(&self) -> bool {
|
pub decorated: bool,
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
/// On desktop: make the window transparent.
|
/// 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.
|
/// You should avoid having a [`egui::CentralPanel`], or make sure its frame is also transparent.
|
||||||
fn transparent(&self) -> bool {
|
pub transparent: bool,
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
/// On Windows: enable drag and drop support.
|
/// 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>
|
/// 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 {
|
pub drag_and_drop_support: bool,
|
||||||
true
|
}
|
||||||
|
|
||||||
|
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.
|
/// Image data for the icon.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct IconData {
|
pub struct IconData {
|
||||||
/// RGBA pixels.
|
/// RGBA pixels.
|
||||||
pub rgba: Vec<u8>,
|
pub rgba: Vec<u8>,
|
||||||
|
|
Loading…
Reference in a new issue