_platform_web.dart 1.76 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:html' as html;
6 7 8 9
import 'platform.dart' as platform;

/// The dart:html implementation of [platform.defaultTargetPlatform].
platform.TargetPlatform get defaultTargetPlatform {
10 11 12
  // To get a better guess at the targetPlatform we need to be able to reference
  // the window, but that won't be available until we fix the platforms
  // configuration for Flutter.
13
  platform.TargetPlatform result = _browserPlatform();
14
  if (platform.debugDefaultTargetPlatformOverride != null)
15
    result = platform.debugDefaultTargetPlatformOverride!;
16 17
  return result;
}
18 19

platform.TargetPlatform _browserPlatform() {
20
  final String navigatorPlatform = html.window.navigator.platform?.toLowerCase() ?? '';
21 22 23
  if (navigatorPlatform.startsWith('mac')) {
    return platform.TargetPlatform.macOS;
  }
24 25 26
  if (navigatorPlatform.startsWith('win')) {
    return platform.TargetPlatform.windows;
  }
27 28 29 30 31
  if (navigatorPlatform.contains('iphone') ||
      navigatorPlatform.contains('ipad') ||
      navigatorPlatform.contains('ipod')) {
    return platform.TargetPlatform.iOS;
  }
32 33 34 35 36 37 38 39 40 41 42
  if (navigatorPlatform.contains('android')) {
    return platform.TargetPlatform.android;
  }
  // Since some phones can report a window.navigator.platform as Linux, fall
  // back to use CSS to disambiguate Android vs Linux desktop. If the CSS
  // indicates that a device has a "fine pointer" (mouse) as the primary
  // pointing device, then we'll assume desktop linux, and otherwise we'll
  // assume Android.
  if (html.window.matchMedia('only screen and (pointer: fine)').matches) {
    return platform.TargetPlatform.linux;
  }
43 44
  return platform.TargetPlatform.android;
}