83 Commits

Author SHA1 Message Date
ec4335e07f js: Hide type menu if there's only one preset and no configuration
It seems a bit silly to display it when there's only one option.
2023-02-16 19:15:42 +00:00
b0203e8f72 js: Quicker keyboard access to menu items
Now pressing "1" to "9" or "0" activate the first ten items of the
current menu, to save so much pressing of the arrow keys.  There isn't
any visible indication of this, so it's a bit of an Easter egg.
2023-01-19 20:34:48 +00:00
b8e9bfa7f6 kaios: Make F10 open and close the menu
This is no use on KaiOS itself, but provides a way to open the menu from
the keyboard on desktop browsers for testing (and for
keyboard-accessibility in general).
2023-01-19 20:34:48 +00:00
5ba1bf5560 js: Tolerate the absence of various UI elements from the HTML
To make things more consistent, the static buttons now all have their
own variables.
2023-01-19 20:34:48 +00:00
48ded126a9 js: Look up elements in the DOM as early as possible
Now that our script is loaded using <script defer>, we can rely on the
DOM's being complete as soon as it starts running.  So when we declare a
variable to point to a DOM element, we can initialise it with that
element.  This saves having these odd initialisations scattered around
the code, usually but not always at the site of first use.

I'd like to be able to do the same thing with the variables that point
to C functions, but the Module.cwrap() call isn't entirely safe before
Emscripten has finished loading the C code.
2023-01-19 20:34:48 +00:00
9d7b044c01 js: Simpler and more robust startup procedure
Previously, we initialised all of the JavaScript event handlers as soon
at the DOM was loaded, and then called main() ourselves once the
Emscripten runtime was ready.  This was slightly dangerous since it
depended on none of those event handlers' being called before main().
In practice this was difficult because most of the elements the event
handlers were attached to were invisible, but it did limit what event
handlers could safely be used.

Now, the event handlers are initialised from main().  This makes things
work in a sufficiently conventional way that we can just let the
Emscripten run-time call main() in its usual way, rather than involving
ourselves in the minutiae of Emscripten's startup.
2023-01-19 20:34:48 +00:00
f693794ff5 js: Make soft-key labels generate key events when clicked
This makes the app page a little easier to test on desktop browsers that
don't have SoftLeft and SoftRight keys.
2023-01-19 20:34:48 +00:00
f9449af87a kaios: Major parts of a build for KaiOS
KaiOS (which is based on Firefox OS, formerly Boot to Gecko) runs its
"native" apps in a Web browser, so this is essentially a rather
specialised version of the JavaScript front-end.  Indeed, the JavaScript
and C parts are the same as the Web version.

There are three major parts that are specific to the KaiOS build.
First, there's manifest.pl, which generates a KaiOS-specific JSON
manifest describing each puzzle.

Second, there's a new HTML page generator, apppage.pl, that generates an
HTML page that is much less like a Web page, and much more like an
application, than the one generated by jspage.pl. It expects to build a
single HTML page at a time and gets all its limited knowledge of the
environment from its command line.  This makes it gratuitously different
from jspage.pl and javapage.pl, but makes it easier to run from the
build system.

And finally, there's the CMake glue that assembles the necessary parts
for each application in a directory.  This includes the manifest, the
HTML, the JavaScript, the KaiOS-specific icons (generated as part of the
GTK build) and a copy of the HTML documentation.  The directory is
assembled using CMake's install() function, and can be installed on a
KaiOS device using the developer tools.
2023-01-19 20:34:48 +00:00
1d509dc819 js: Have the "SoftRight" key open the menu by focussing it 2023-01-19 20:34:48 +00:00
3150d72800 js: When opening a dialogue box, try to focus it
This will make using menus from the keyboard more convenient.
2022-12-10 21:54:11 +00:00
9bcb06ee30 js: Add a mode where the puzzle tries to fill the viewport
This is activated by putting the puzzle in an element with id
"puzzlecanvascontain".  In that case, the puzzle's default size is as
close to filling that element as is achievable.  Unlike in the normal
mode, this sets the CSS size of the canvas directly.

Because it might take a little while for the page to settle down after
loading, and because the size of the viewport might change, this listens
for "resize" and "load" events, and only bothers changing anything when
the page is fully loaded.

Waiting for the document to be complete might be a problem if we had
images and so forth that we could plausibly be waiting for, but we
don't.
2022-12-10 18:33:00 +00:00
02f1d55a02 js: Allow CSS to set the font used by the puzzle
This means that the calculated font properties of the HTML canvas now
control what font is used.  The size is overridden, and for monospaced
text so is the family.

I'd like to be able to also specify the monospaced font, maybe using a
CSS variable, but that looks like being quite a lot of extra complexity.

My experience when testing this was that constructing a valid "font"
string for a canvas context is prone to breakage, but broke in a way
that left the font unchanged, so we always set a simple specification
first before trying to construct one from CSS.
2022-12-10 15:30:49 +00:00
f8ed76f815 js: Bypass our own dialogue box when loading
By constructing the <input type=file> off screen and activating it from
JavaScript, we can jump straight to the browser's upload dialogue box
without interposing our own one.  This gives a smoother experience, and
also avoids the difficult-to-handle <input type=file> ever being
visible.
2022-12-05 14:07:03 +00:00
b04a2cba98 js: Correct a comment describing timer_callback 2022-12-03 15:47:15 +00:00
d3ef8e65dc js: Simplify drawing context management
There's not much point in re-requesting the drawing context from the
offscreen canvas at the start of each drawing operation.  The canvas
keeps the context around and returns it on every call to getContext(),
so we may as well just keep our reference to it too.  This does mean
that the front-end won't detect puzzles drawing outside of a redraw
operation, but I think it's the mid-end's job to assert things like
that.

Rumours that I'm doing this because I had a mysterious bug whereby ctx
was unexpectedly null are entirely true.
2022-12-03 15:46:58 +00:00
f0d4705364 js: Switch to using the resize handle in the HTML 2022-12-02 23:49:00 +00:00
2522dd249b js: Don't bother resizing offscreen canvas at startup
It will get its size set soon enough once we know the size of the
puzzle anyway.
2022-12-02 23:49:00 +00:00
66a927920a js: Remove a JavaScript construct that confused emcc -O3 2022-12-02 02:10:19 +00:00
dbb2d2adb2 js: Allow for putting a resize handle in HTML 2022-11-29 23:34:43 +00:00
a6a799720f js: Correct co-ordinate-mapping function for what CSS actually does
By default, CSS uses "object-fit: fill", which means that an object is
independently scaled in both dimensions to fit its containing box.
This is simpler than what I'd assumed (which was "object-fill:
contain").  Obviously, the HTML could be changed to use a different
object-fit, in which case this code would have to detect it, but for
now following the CSS default is more correct than not.
2022-11-25 18:42:44 +00:00
38e17ebab2 js: If the HTML contains a dialogue-box form, delete it
This is so that (given time for caches to expire) I can switch to having
a persistent dialogue box in HTML rather than fabricating it from
scratch in JavaScript each time it's used.
2022-11-24 21:03:31 +00:00
36c9062cbd js: Disable menu keyboard controls when dialogue box is active
I think this is broadly the wrong approach, but it's an improvement
until I implement the right one.
2022-11-24 18:26:59 +00:00
b5ccb0c9af js: Add actions for more keys in menus
I expect Escape to exit the menu, and SoftRight should do that as well
for KaiOS.  Backspace goes up one level through the menus, again because
that's conventional on KaiOS and not too confusing elsewhere.
2022-11-23 22:27:54 +00:00
e79270368b js: Move global keyboard handler to capturing phase
In the bubbling phase it managed to catch the "Enter" keypress that
opened a dialogue box from the menu and use it to close the dialogue
box again.  I think it's probably reasonable to have it run earlier and
just permanently steal any keypresses it wants.
2022-11-23 21:56:42 +00:00
2d439dd00e js: Move focus-tracking to entirely "focus" events
When we disable a button, it loses focus but doesn't generate a "blur"
event.  This means our "focus-within" class goes wrong.  Instead of
relying on "blur" events to remove the class, remove it from any
inappropriate elements in the "focus" handler.  This requires attaching
the handler to the root element of the document, but I've got plans that
need that anyway.
2022-11-23 21:56:42 +00:00
8445f07827 js: Replace :focus-within with JS-maintained .focus-within
Old browsers (like KaiOS 2.5) don't have :focus-within, but it's pretty
easy to replace the pseudo-class with a real .focus-within class
maintained by JavaScript event handlers.  This is made only marginally
fiddlier by the odd fact that "focus" and "blur" events don't bubble.
2022-11-23 21:56:42 +00:00
52cd58043a js: Add keyboard navigation for menus
Once the input focus is in the menu system (for instance by Shift+Tab
from the puzzle), you can move left and right through the menu bar and
up and down within each menu.  Enter selects a menu item.  The current
menu item is tracked by giving it the input focus.
2022-11-23 21:53:59 +00:00
f7557852b5 js: Tiny comment fix 2022-11-21 14:55:17 +00:00
77c8b50834 js: Allow status bar to be present in the HTML
I'm generally in favour of putting HTML in HTML rather the constructing
it in JavaScript, and this will allow for simplifying the code
eventually.  This only changes the JavaScript to make sure that's in
people's caches before I change the HTML itself.
2022-11-20 19:10:23 +00:00
1b3a6bd204 js: Create the puzzle resize handle only if the puzzle is resizable
If there's no resizable div to attach it to, there's not much point in
creating the handle and the doing nothing with it.
2022-11-15 23:01:36 +00:00
5a225bf585 js: Substantially simplify timer code
The C code in the Emscripten front-end already keeps a timer_active
variable to ensure that the timer can be activated only when it's
inactive, and deactivated only when it's active.  Adjusting the
JavaScript side to rely on this makes the code much simpler.  The only
oddity is that it now requests a new animation frame before calling the
callback so that it's ready to be cancelled if the callback decides to
deactivate the timer.
2022-11-13 00:15:00 +00:00
5a90dd9312 js: Give keyboard focus to the puzzle canvas at startup again
I think this has been broken since a752e73, when the canvas changed to
being hidden, and hence unable to receive keyboard focus, when the page
loaded.  I've now moved the focus() call to after the canvas gets
displayed.
2022-11-12 17:21:03 +00:00
208e2508d3 js: Add a way to have environment variables
They can now be specified by sticking some JSON in a <script> element in
the Web page:

<script id="environment" type="application/json">
   { "LOOPY_DEFAULT": "20x10t11dh" }
</script>

This isn't brilliantly useful, but it does allow for changing settings
without recompiling.
2022-11-12 15:03:40 +00:00
a9c783ed4e js: Label all form controls and put controls inside labels
This should help with accessibility and means we don't need to give IDs
to tick-boxes.
2022-11-12 12:07:35 +00:00
60d2bf5930 js: Convert menus to use semantically appropriate HTML elements
Presets are now radio buttons with labels, and menu items that take
actions are now buttons.  The <li> representing each menu item is now a
thin wrapper around another element: a <label> for radio buttons, a
<button> for other buttons, and a <div> for submenu headings.  All of
the things that previously applied to the <li> now apply to that inner
element instead.

This means that presets can now use the standard "checked" attribute to
indicate which one is selected, and buttons can be disabled using the
standard "disabled" attribute.  It also means that we can query and set
the state of all the presets at once through their RadioNodeList.

I think this should also make the menus more accessible, and make it
easier to make them keyboard-controllable.
2022-11-12 09:48:31 +00:00
2115799094 js: Add various missing variable declarations 2022-11-10 00:13:59 +00:00
f7957d3aa0 js: Reinstate a missing variable declaration
... and then decide there was no excuse for renaming the variable, so
now it has the same name it had before I started using
Window.requestAnimationFrame().
2022-11-09 23:44:26 +00:00
7982002a64 js: Switch to window.requestAnimationFrame() for timing
This is an API specifically designed for the purposes of timing
animations.  Unlike setInterval, it tries to synchronise with the screen
refresh rate.  It naturally passes us timing information, saving the
need to construct a Date object every frame.  It has the nice feature
that browsers (at least Firefox 91) will call it less frequently when
the puzzle page isn't visible, which saves CPU time in puzzles that run
a timer continuously.
2022-11-09 21:40:27 +00:00
c5a2446fae js: Cancel UI events when the mid end says they've been handled
This means that if a key doesn't do anything in a puzzle, it can operate
the browser instead.
2022-11-08 10:27:19 +00:00
ad9ee5a524 js: Move much of the handling of device pixel ratios to the mid-end
Now that the mid-end knows how to do it, we can remove some complexity
from the front end.
2022-11-08 10:27:02 +00:00
fba22f04d6 js: Make update_pixel_ratio() more robust
With very small tile sizes, js_canvas_find_font_midpoint() can throw an
exception.  When it was called from update_pixel_ratio(), this prevented
the new MediaQueryList from being created, which meant that the puzzle
stopped noticing changes of device pixel ratio.

Now update_pixel_ratio() establishes a new MediaQueryList before calling
rescale_puzzle(), so the exception can't break it.  Catching the
exception properly would be even better, of course.
2022-11-08 00:57:32 +00:00
06f6e878a0 js: Tolerate the non-existence of some HTML elements
Specifically, the permalinks, the apology, and the resizable div.
2022-10-29 11:58:37 +01:00
fa58dd85b7 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.
2022-10-27 22:51:54 +01:00
532d662722 js: Very bad attempt at making puzzles change size when zooming
This has the entertaining consequence that repeatedly zooming in and out
causes puzzles to gradually shrink, thus demonstrating that recording
the nominal size correctly will be necessary.
2022-10-27 22:51:48 +01:00
6f5debe417 js: Make update_pixel_ratio more backward-compatible
Despite my stylistic downgrades, it still used two features not present
in Firefox 48, and hence KaiOS 2.5: passing options to addEventListener,
and calling addEventListener on a MediaQueryList at all.  Now it uses
the older addListener method and explicitly removes each listener as
soon as it's called.
2022-10-26 21:57:53 +01:00
5fae5ca0db js: Be more subtle about cancelling keydown events
Now we only cancel a keydown event if the C keyboard handler recognises
the key and passes it on to the midend.  This doesn't necessarily mean
that the midend has actually done anything with it, of course.  Still,
this is enough to allow F12 to open the developer tools even when the
input focus is on the puzzle.  It also allows for tabbing out of the
puzzle into the links below it.
2022-10-25 20:38:37 +01:00
e6faebeb9a js: Remove keypress handler
At least in modern browsers (and I suspect in all browsers), cancelling
a keydown event ensures that the subsequent keypress event doesn't fire.
See <https://w3c.github.io/uievents/#keys-cancelable-keys>.

So there's no point in having a handler on keypress events that just
tries to cancel them as well.  Removing the handler doesn't do much now,
but it opens the possibility of being a bit more selective about which
keydown events we cancel.
2022-10-25 20:13:59 +01:00
768ef770a3 js: Use KeyboardEvent.key for ASCII keystrokes
This requires passing in KeyboardEvent.location from JavaScript so
that we can detect the numeric keypad properly.  Out of caution we
currently only set MOD_NUM_KEYPAD on numbers, like we always have,
but we have enough information to set it on arrow keys, Enter, "+",
etc.

This finally gets '/' and '\' working in Slant again.
2022-10-24 23:13:33 +01:00
0db5fb525b js: Remove the charCode argument from key()
It hasn't been used in a while.
2022-10-24 22:37:30 +01:00
a62217e9b4 js: Use less-modern syntax in update_pixel_ratio
Stealing code from the MDN has the consequence that it uses shiny ES6
features like "const", "let", and "=>".  This looks a bit odd among
the more conservative style of the rest of Puzzles, so I've
downgraded it to "var" and "function".  I'll let the template string
stay because that actually helps readability.
2022-10-23 11:15:31 +01:00