Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add can-lock-orientation media feature to detect if locking is possible #206

Open
makotokato opened this issue Jan 31, 2022 · 24 comments
Open

Comments

@makotokato
Copy link

(If this is already discussed, please close this)

When I look this spec, I cannot find feature detection section. Even If orientation.lock isn't supported, Blink and Gecko have this method in orientation. Then when calling it, NotSupportedError is throw if browser doesn't support it.

For feature detection of orientation.lock, is there a way of feature detection without using orientation.lock like following? And I hope that we add feature detection section for lock.

async function isLockSupport() {
  try {
    await orientation.lock('any');
    orientation.unlock();
    return true;
  } catch (e) {
  }
  return false;
}
@makotokato
Copy link
Author

makotokato commented Jan 31, 2022 via email

@tomayac
Copy link

tomayac commented Jan 31, 2022

Sorry, yes, I realized this as well, which is why I deleted the comment. I guess it really boils down to try..catching.

@marcoscaceres
Copy link
Member

The only way to allow this would be to add a new method. However, this may never succeed for a variety of reasons. For example, the user may prevent the screen from being rotated for accessibility reasons. Then, privacy, we wouldn't want to reveal that fact about the user.

I think we should also consider gating this API on a user gesture, but it's a separate issue.

@marcoscaceres
Copy link
Member

So yeah, I think at a minimum, we might need a supportsLocking boolean or something, because locking to "any" is still a kind of locking.

The use case being, if you are building a UI that has rotate buttons, it doesn't make sense to enable them or show them if you can't rotate the screen.

Alternatively (I'm not sure this is better): we could tell user agents to not expose the .lock() on platforms they are sure don't support locking at all (e.g., most desktops).

@saschanaz
Copy link
Member

Alternatively (I'm not sure this is better): we could tell user agents to not expose the .lock() on platforms they are sure don't support locking at all (e.g., most desktops).

We might want to build a pattern for this. Could be great if we can have a list of APIs that always fail on certain session-permanent situation (that never changes in the current session).

@annevk
Copy link
Member

annevk commented Oct 10, 2022

@domenic is pushing a pattern of sorts whereby we use supports() static methods (see HTMLScriptElement for instance). That seems somewhat reasonable here. Could maybe also be used to determine whether a platform supports multiple portrait orientations or not (needs use cases first).

@saschanaz
Copy link
Member

Cool, but I mean specifically for things that always fail, since it doesn't quite make sense to have functions that are never going to work.

@domenic
Copy link

domenic commented Oct 11, 2022

That pattern is more for things which are not detectable in other ways, e.g. declarative markup features. In this case, just trying to call the function and seeing if it fails seems better... try/catch is just as good as if/else, in general.

@marcoscaceres
Copy link
Member

marcoscaceres commented Oct 11, 2022

@domenic, the problem here is that:

  • a web app might need to provide significant UI (rotate button(s)) before it knows if it can even rotate.
  • the only way to know if it will change orientation is by actually changing orientation.

So, imagine (this would be really bad for users):

// lets' figure out what's supported
const supported = [];
for (const o in orientations) {
   try {
       await screen.orientation.lock(o);
       supported.push(o);
    } catch { continue; }
}
screen.orientation.unlock();
showUI(supported);

@annevk
Copy link
Member

annevk commented Oct 11, 2022

Indeed, try/catch only works if you're guaranteed the try never side effects. That's not the case here. Anyway, it sounds like ScreenOrientation.supports() is a thing we could do.

@marcoscaceres
Copy link
Member

marcoscaceres commented Oct 18, 2022

Just thinking out loud here... a .canLock() method could be an alternative.

To return true, the .canLock() method presupposes that:

  • all orientations are supported in one way or another (e.g, "portrait-secondary" is treated as "portrait-primary").
  • all pre-lock conditions are met (e.g., is in full screen).
  • The OS supports locking and/or there isn't something else that would prevent the lock (without revealing what "something else" is, for privacy reasons).
  • The document fully active.

(The only thing it wouldn't check for is transient activation)

Additionally, I think all the preconditions of .canLock() can be checked synchronously: it would basically perform all the synchronous checks that .lock() performs before doing its "in parallel" steps.

@annevk
Copy link
Member

annevk commented Oct 18, 2022

Wouldn't that prevent using the API when you're not fullscreen to detect if you can go fullscreen and change the orientation?

@marcoscaceres
Copy link
Member

marcoscaceres commented Oct 27, 2022

yes, indeed. Ok, so scratch checking the preconditions.

If we assume the following invariant:

.lock() must support all orientations in one way or another, even if those orientations get mapped to something else (e.g., landscape-secondary -> landscape-primary, natural -> portrait-primary).

Then that only requires a single check: ".canProbablyLock" (boolean) which would be: "they're nothing preventing user agent from attempting to lock the screen orientation".

On desktop platforms, this would return false: it's not common convention to support lock()'ing.

On other platforms, it would return true - but .lock() might still fail for a number of reasons.

@marcoscaceres
Copy link
Member

Some down sides:

@marcoscaceres
Copy link
Member

Just noting that it is hypothetically possible that a user could transfer the document from one "desktop class" screen to a mobile device, which could allow changing orientation. So this may require a companion event.

@marcoscaceres
Copy link
Member

It might be prudent to go down the CSS route with this, as the primary use case revolves around showing/hiding bits of UI.

Would it be fair to say that the ability to change orientation is really describing a media feature? Like, can-lock-orientation:

@media screen and (can-lock-orientation) {
    
}

@annevk
Copy link
Member

annevk commented Nov 15, 2022

Doesn't seem entirely unreasonable. 😊

@marcoscaceres
Copy link
Member

@makotokato, what do you think? Should we pursue the CSS media feature route?

@marcoscaceres
Copy link
Member

@makotokato, gentle ping. Would like gauge your level of interest before I set about specifying it.

@marcoscaceres marcoscaceres changed the title Feature detection of orientation.lock Add can-lock-orientation media feature to detect if locking is possible Nov 28, 2022
@makotokato
Copy link
Author

I guess that it may not better to use media query. If using media query, I guess it is better to add orientation-lock media feature like display-mode of full screen. orientation feature doesn't have all information of orientation lock.

I vote orientation.canLock.

@michaelwasserman
Copy link
Member

While I'm agnostic to the method used, it does seem potentially useful to expose a 'can probably lock' signal. Based on my novice reading of underlying OS APIs, user agents may be incapable of representing signals stronger than 'probably', right?

@marcoscaceres
Copy link
Member

@makotokato, do you still feel strongly that this should be an API over the media query? Or are you ok with us going down the media query route?

@dmurph
Copy link

dmurph commented Sep 27, 2024

TPAC 2024: Media query is ok with @michaelwasserman from Chromium and @marcoscaceres from WebKit. @marcoscaceres is following up with Mozilla.

@makotokato
Copy link
Author

do you still feel strongly that this should be an API over the media query? Or are you ok with us going down the media query route?

It is also OK as media query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants