From b2a874da29ddc77c10edec0b3556d7c6749bea41 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 1 Jul 2025 01:16:35 +0200 Subject: [PATCH] hello world --- src/app.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 23 ++++++++++++++--------- src/main.rs | 40 ++++------------------------------------ 3 files changed, 62 insertions(+), 45 deletions(-) create mode 100644 src/app.rs diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..dceea06 --- /dev/null +++ b/src/app.rs @@ -0,0 +1,44 @@ +use winit::application::ApplicationHandler; +use winit::dpi::LogicalSize; +use winit::event::WindowEvent; +use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop}; +use winit::window::{Window, WindowAttributes, WindowId}; + +struct App { + window: Option, + attr: WindowAttributes, +} + +impl App { + pub fn new(attr: WindowAttributes) -> App { + App { window: None, attr } + } +} + +impl ApplicationHandler for App { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { + self.window = Some(event_loop.create_window(self.attr.clone()).unwrap()); + } + + fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { + match event { + WindowEvent::CloseRequested => { + event_loop.exit(); + } + WindowEvent::RedrawRequested => { + self.window.as_ref().unwrap().request_redraw(); + } + _ => (), + } + } +} + +pub fn run(width: f64, height: f64) { + let event_loop = EventLoop::new().unwrap(); + event_loop.set_control_flow(ControlFlow::Poll); + event_loop.set_control_flow(ControlFlow::Wait); + let mut w = WindowAttributes::default(); + w.inner_size = Some(winit::dpi::Size::Logical(LogicalSize::new(width, height))); + let mut app = App::new(w); + event_loop.run_app(&mut app).unwrap(); +} diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..2232ee8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,19 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +mod app; + +pub struct Window { + width: f64, + height: f64, } -#[cfg(test)] -mod tests { - use super::*; +impl Window { + pub fn new() -> Window { + Window { + width: 1000.0, + height: 650.0, + } + } - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + pub fn run(&self) { + app::run(self.width, self.height); } } diff --git a/src/main.rs b/src/main.rs index a2b2726..5791696 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,39 +1,7 @@ -use winit::application::ApplicationHandler; -use winit::event::WindowEvent; -use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop}; -use winit::window::{Window, WindowId}; - -#[derive(Default)] -struct App { - window: Option, -} - -impl ApplicationHandler for App { - fn resumed(&mut self, event_loop: &ActiveEventLoop) { - self.window = Some( - event_loop - .create_window(Window::default_attributes()) - .unwrap(), - ); - } - - fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { - match event { - WindowEvent::CloseRequested => { - event_loop.exit(); - } - WindowEvent::RedrawRequested => { - self.window.as_ref().unwrap().request_redraw(); - } - _ => (), - } - } -} +use osgui::Window; fn main() { - let event_loop = EventLoop::new().unwrap(); - event_loop.set_control_flow(ControlFlow::Poll); - event_loop.set_control_flow(ControlFlow::Wait); - let mut app = App::default(); - event_loop.run_app(&mut app).unwrap(); + let win = Window::new(); + + win.run(); }