shithub: puzzles

Download patch

ref: fa58dd85b7ee06fed8d7a5ecc14fda8146f7521b
parent: 9783bbfbc01f2faff86daaa53e46bf68848b8b7c
author: Ben Harris <bjh21@bjh21.me.uk>
date: Thu Oct 27 10:30:23 EDT 2022

js: Distinguish manual resizes from device pixel ratio changes

This adds a new callback, rescale_puzzle(), that's called when the
device pixel ratio changes.  This means that resize_puzzle() can safely
set the nominal canvas size, which means that manual resizing of the
puzzle now sticks.

Still missing: paying attention to the device pixel ratio when choosing
the initial (or reset) size.

--- a/cmake/platforms/emscripten.cmake
+++ b/cmake/platforms/emscripten.cmake
@@ -26,6 +26,8 @@
   # Callbacks when the resizing controls are used
   _resize_puzzle
   _restore_puzzle_size
+  # Callback when device pixel ratio changes
+  _rescale_puzzle
   # Main program, run at initialisation time
   _main)
 
--- a/emcc.c
+++ b/emcc.c
@@ -195,8 +195,8 @@
     canvas_h = h;
 }
 
-/* Called from JS when the user uses the resize handle */
-void resize_puzzle(int w, int h)
+/* Called from JS when the device pixel ratio changes */
+void rescale_puzzle(int w, int h)
 {
     midend_size(me, &w, &h, true);
     if (canvas_w != w || canvas_h != h) { 
@@ -205,6 +205,13 @@
         canvas_h = h;
         midend_force_redraw(me);
     }
+}
+
+/* Called from JS when the user uses the resize handle */
+void resize_puzzle(int w, int h)
+{
+    rescale_puzzle(w, h);
+    js_canvas_set_nominal_size();
 }
 
 /* Called from JS when the user uses the restore button */
--- a/emccpre.js
+++ b/emccpre.js
@@ -535,12 +535,14 @@
      * <https://developer.mozilla.org/en-US/docs/Web/API/Window/
      * devicePixelRatio> (CC0) to work on older browsers.
      */
+    var rescale_puzzle = Module.cwrap('rescale_puzzle',
+                                      'void', ['number', 'number']);
     var mql = null;
     var update_pixel_ratio = function() {
         var dpr = window.devicePixelRatio;
         if (mql !== null)
             mql.removeListener(update_pixel_ratio);
-        resize_puzzle(nominal_width * dpr, nominal_height * dpr);
+        rescale_puzzle(nominal_width * dpr, nominal_height * dpr);
         mql = window.matchMedia(`(resolution: ${dpr}dppx)`);
         mql.addListener(update_pixel_ratio);
     }