From 67282c4a9579fe73b75b473f0dcf47b119e3e86f Mon Sep 17 00:00:00 2001 From: Avii Date: Thu, 6 Aug 2026 12:56:06 +0200 Subject: [PATCH] 1786013766 --- src/main.rs | 10 +++- src/trekstor.rs | 126 +++++++++++++++-------------------------- src/trekstor/screen.rs | 1 + ui/main.slint | 2 +- 4 files changed, 56 insertions(+), 83 deletions(-) diff --git a/src/main.rs b/src/main.rs index 197f346..3e1c31d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,14 @@ mod trekstor; use crate::trekstor::Trekstor; -fn main() { - let mut trekstor = Trekstor::new(30); +slint::include_modules!(); - trekstor.update(); +fn main() -> Result<(), slint::PlatformError> { + slint::platform::set_platform(Box::new(Trekstor::new())).expect("set platform"); + + let ui = AppWindow::new()?; + + ui.run() // let red = (255, 0, 0, 255); // let green = (0, 255, 0, 255); diff --git a/src/trekstor.rs b/src/trekstor.rs index 9c57d16..0a95357 100644 --- a/src/trekstor.rs +++ b/src/trekstor.rs @@ -1,8 +1,5 @@ use std::rc::Rc; -use std::thread::sleep; -use std::time::{Duration, Instant}; -use embedded_graphics::prelude::DrawTarget; use evdev::KeyCode; use slint::LogicalPosition; use slint::platform::software_renderer::{MinimalSoftwareWindow, RepaintBufferType}; @@ -22,87 +19,14 @@ pub struct Context { } pub struct Trekstor { - ctx: Context, window: Rc, - // buffer: Vec, - last_time: Instant, - current_time: Instant, - update_timestep: Duration, } impl Trekstor { - pub fn new(update_fps: i32) -> Self { - let screen = Screen::new(); - let input = InputState::new(&screen); + pub fn new() -> Self { let window = MinimalSoftwareWindow::new(RepaintBufferType::ReusedBuffer); - Self { - ctx: Context { screen, input }, - window, - // buffer: Vec::new(), - current_time: Instant::now(), - last_time: Instant::now(), - update_timestep: Duration::from_nanos( - (1_000_000_000f64 / update_fps as f64).round() as u64 - ), - } - } - - pub fn update(&mut self) { - // dbg!(self.ctx.screen.width() * self.ctx.screen.height()); - - let mut buffer1 = [Rgba::from((0, 0, 0, 0)); 614400]; - - loop { - self.current_time = Instant::now(); - let dt = self.current_time - self.last_time; - self.last_time = self.current_time; - - if dt < self.update_timestep { - sleep(self.update_timestep - dt); - } - - self.ctx.input.handle_event(); - - slint::platform::update_timers_and_animations(); - - if self.ctx.input.is_just_pressed(KeyCode::BTN_TOUCH) { - self.window - .try_dispatch_event(WindowEvent::PointerPressed { - position: LogicalPosition::new( - self.ctx.input.x as f32, - self.ctx.input.y as f32, - ), - button: slint::platform::PointerEventButton::Left, - }) - .unwrap(); - } - - if self.ctx.input.is_just_released(KeyCode::BTN_TOUCH) { - self.window - .try_dispatch_event(WindowEvent::PointerReleased { - position: LogicalPosition::new( - self.ctx.input.x as f32, - self.ctx.input.y as f32, - ), - button: slint::platform::PointerEventButton::Left, - }) - .unwrap(); - } - - self.window.draw_if_needed(|renderer| { - dbg!("Rendering"); - renderer.render(&mut buffer1, self.ctx.screen.width() as usize); - - // self.ctx - // .screen - // .fill_contiguous(&self.ctx.screen.rect(), buffer1) - // .ok(); - }); - - self.ctx.screen.flush(); - self.ctx.input.finish(); - } + Self { window } } } @@ -112,5 +36,49 @@ impl Platform for Trekstor { ) -> Result, slint::PlatformError> { Ok(self.window.clone()) } - // .. + + fn run_event_loop(&self) -> Result<(), slint::PlatformError> { + let screen = Screen::new(); + let input = InputState::new(&screen); + let mut ctx = Context { screen, input }; + + let mut buffer1 = [Rgba::from((0, 0, 0, 0)); 614400]; + + loop { + ctx.input.handle_event(); + + slint::platform::update_timers_and_animations(); + + if ctx.input.is_just_pressed(KeyCode::BTN_TOUCH) { + self.window + .try_dispatch_event(WindowEvent::PointerPressed { + position: LogicalPosition::new(ctx.input.x as f32, ctx.input.y as f32), + button: slint::platform::PointerEventButton::Left, + }) + .unwrap(); + } + + if ctx.input.is_just_released(KeyCode::BTN_TOUCH) { + self.window + .try_dispatch_event(WindowEvent::PointerReleased { + position: LogicalPosition::new(ctx.input.x as f32, ctx.input.y as f32), + button: slint::platform::PointerEventButton::Left, + }) + .unwrap(); + } + + self.window.draw_if_needed(|renderer| { + dbg!("Rendering"); + renderer.render(&mut buffer1, ctx.screen.width() as usize); + + // ctx + // .screen + // .fill_contiguous(&ctx.screen.rect(), buffer1) + // .ok(); + }); + + ctx.screen.flush(); + ctx.input.finish(); + } + } } diff --git a/src/trekstor/screen.rs b/src/trekstor/screen.rs index 8b527ec..b39828e 100644 --- a/src/trekstor/screen.rs +++ b/src/trekstor/screen.rs @@ -9,6 +9,7 @@ pub struct Screen { height: u32, } +#[allow(unused)] impl Screen { pub fn new() -> Self { let framebuffer = Framebuffer::open().expect("Failed to open framebuffer"); diff --git a/ui/main.slint b/ui/main.slint index 87bea27..908d43d 100644 --- a/ui/main.slint +++ b/ui/main.slint @@ -1,6 +1,6 @@ import { AboutSlint, Button, VerticalBox } from "std-widgets.slint"; -export component Demo { +export component AppWindow inherits Window { VerticalBox { alignment: start; Text {