From e12870e0141631efb1c7a719aa301d7672436bb8 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Thu, 4 Apr 2024 15:40:41 +0200 Subject: [PATCH] C++: if a SLINT_SCALE_FACTOR is set at compile time, default to that --- api/cpp/esp-idf/slint/src/slint-esp.cpp | 16 ++++++++-------- api/cpp/platform.rs | 8 ++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/api/cpp/esp-idf/slint/src/slint-esp.cpp b/api/cpp/esp-idf/slint/src/slint-esp.cpp index 7620155ec4c..a8446bb708d 100644 --- a/api/cpp/esp-idf/slint/src/slint-esp.cpp +++ b/api/cpp/esp-idf/slint/src/slint-esp.cpp @@ -151,8 +151,8 @@ void EspPlatform::run_event_loop() } #endif - int last_touch_x = 0; - int last_touch_y = 0; + float last_touch_x = 0; + float last_touch_y = 0; bool touch_down = false; while (true) { @@ -192,21 +192,21 @@ void EspPlatform::run_event_loop() *touch_handle, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1); if (touchpad_pressed && touchpad_cnt > 0) { + auto scale_factor = m_window->window().scale_factor(); // ESP_LOGI(TAG, "x: %i, y: %i", touchpad_x[0], touchpad_y[0]); - last_touch_x = touchpad_x[0]; - last_touch_y = touchpad_y[0]; + last_touch_x = float(touchpad_x[0]) / scale_factor; + last_touch_y = float(touchpad_y[0]) / scale_factor; m_window->window().dispatch_pointer_move_event( - slint::LogicalPosition({ float(last_touch_x), float(last_touch_y) })); + slint::LogicalPosition({ last_touch_x, last_touch_y })); if (!touch_down) { m_window->window().dispatch_pointer_press_event( - slint::LogicalPosition( - { float(last_touch_x), float(last_touch_y) }), + slint::LogicalPosition({ last_touch_x, last_touch_y }), slint::PointerEventButton::Left); } touch_down = true; } else if (touch_down) { m_window->window().dispatch_pointer_release_event( - slint::LogicalPosition({ float(last_touch_x), float(last_touch_y) }), + slint::LogicalPosition({ last_touch_x, last_touch_y }), slint::PointerEventButton::Left); m_window->window().dispatch_pointer_exit_event(); touch_down = false; diff --git a/api/cpp/platform.rs b/api/cpp/platform.rs index b0cc50c6342..e2c399285b7 100644 --- a/api/cpp/platform.rs +++ b/api/cpp/platform.rs @@ -185,6 +185,14 @@ pub unsafe extern "C" fn slint_window_adapter_new( set_position, }); + if let Some(scale_factor) = + option_env!("SLINT_SCALE_FACTOR").and_then(|x| x.parse::().ok()).filter(|f| *f > 0.) + { + window.window().dispatch_event(i_slint_core::platform::WindowEvent::ScaleFactorChanged { + scale_factor, + }); + } + core::ptr::write(target as *mut Rc, window); }