How to detect if a function has been fired on any page so scripts/styles can be loaded conditionally

For scripts this is no-brainer – just enqueue inside my_gallery() function and they will be output in footer (since header already passed).

However stylesheets are a challenge, they are only supposed to work in head section and enqueueing for footer is not supported.

Essentially you need to know if function was called before it was called. This has no simple solution for conditions given.

I would probably try to load stylesheet from JS code to achieve this in form you are describing.

tech