Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better ways of initializing an ApplicationHandler #3808

Closed
Astralchroma opened this issue Jul 22, 2024 · 1 comment
Closed

Better ways of initializing an ApplicationHandler #3808

Astralchroma opened this issue Jul 22, 2024 · 1 comment
Labels
S - enhancement Wouldn't this be the coolest?

Comments

@Astralchroma
Copy link

Description

The ApplicationHandler API presents a problem for any application where a window is required before much can be done as you can't create a window until the first event, by which time you will have already created the struct to represent your application. This means you now have to use Option<T> to handle the possibility of no window at some point. The most sane way I have found to do this is to create my own separate application struct, which is stored as an Option<T> in the ApplicationHandler implementation and simply pass through all the events. This is somewhat messy, and not ideal.

As a solution, I would suggest changing how an event loop is started, the best way I can think of acomplishing this would be to introduce a new ApplicationHandler::init method, a rough overview of the idea is as follows:

struct Application {
	window: Window
}

impl ApplicationHandler<()> for Application {
	type PassedData = String;

	// The addition of PassedData is to make it convenient to pass anything created before the event loop, any API wouldn't necessarily need this, but it would be a useful thing to have.
	fn init(event_loop: &ActiveEventLoop, passed_data: PassedData) -> Self {
		println!("{passed_data}");
		Self {
			window: event_loop.create_window(Window::default_attributes()).unwrap(),
		}
	}
	
	// *All other functions would be same as they are currently*
}

fn main() {
	EventLoop::new()
		.unwrap()
		.run_app::<Application>(String::from("Hello World!"))
		.unwrap();
}

Relevant platforms

No response

@Astralchroma Astralchroma added the S - enhancement Wouldn't this be the coolest? label Jul 22, 2024
@daxpedda
Copy link
Member

Agreed, this could be improved.
I believe this is a duplicate of #2903.

@daxpedda daxpedda closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S - enhancement Wouldn't this be the coolest?
Development

No branches or pull requests

2 participants