widget/windows/winrt/MetroUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/windows/winrt/MetroUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,190 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "MetroUtils.h"
    1.10 +#include <windows.h>
    1.11 +#include "nsICommandLineRunner.h"
    1.12 +#include "nsNetUtil.h"
    1.13 +#include "nsIBrowserDOMWindow.h"
    1.14 +#include "nsIWebNavigation.h"
    1.15 +#include "nsIDocShellTreeItem.h"
    1.16 +#include "nsIDOMWindow.h"
    1.17 +#include "nsIDOMChromeWindow.h"
    1.18 +#include "nsIWindowMediator.h"
    1.19 +#include "nsIURI.h"
    1.20 +#include "prlog.h"
    1.21 +#include "nsIObserverService.h"
    1.22 +#include "nsRect.h"
    1.23 +
    1.24 +#include <wrl/wrappers/corewrappers.h>
    1.25 +#include <windows.ui.applicationsettings.h>
    1.26 +#include <windows.graphics.display.h>
    1.27 +#include "DisplayInfo_sdk81.h"
    1.28 +
    1.29 +using namespace ABI::Windows::UI::ApplicationSettings;
    1.30 +
    1.31 +using namespace mozilla;
    1.32 +
    1.33 +using namespace ABI::Windows::Foundation;
    1.34 +using namespace Microsoft::WRL;
    1.35 +using namespace Microsoft::WRL::Wrappers;
    1.36 +using namespace ABI::Windows::UI::ViewManagement;
    1.37 +using namespace ABI::Windows::Graphics::Display;
    1.38 +
    1.39 +// Conversion between logical and physical coordinates
    1.40 +
    1.41 +double
    1.42 +MetroUtils::LogToPhysFactor()
    1.43 +{
    1.44 +  ComPtr<IDisplayInformationStatics> dispInfoStatics;
    1.45 +  if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayInformation).Get(),
    1.46 +                                      dispInfoStatics.GetAddressOf()))) {
    1.47 +    ComPtr<IDisplayInformation> dispInfo;
    1.48 +    if (SUCCEEDED(dispInfoStatics->GetForCurrentView(&dispInfo))) {
    1.49 +      FLOAT dpi;
    1.50 +      if (SUCCEEDED(dispInfo->get_LogicalDpi(&dpi))) {
    1.51 +        return (double)dpi / 96.0f;
    1.52 +      }
    1.53 +    }
    1.54 +  }
    1.55 +
    1.56 +  ComPtr<IDisplayPropertiesStatics> dispProps;
    1.57 +  if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(),
    1.58 +                                      dispProps.GetAddressOf()))) {
    1.59 +    FLOAT dpi;
    1.60 +    if (SUCCEEDED(dispProps->get_LogicalDpi(&dpi))) {
    1.61 +      return (double)dpi / 96.0f;
    1.62 +    }
    1.63 +  }
    1.64 +
    1.65 +  return 1.0;
    1.66 +}
    1.67 +
    1.68 +double
    1.69 +MetroUtils::PhysToLogFactor()
    1.70 +{
    1.71 +  return 1.0 / LogToPhysFactor();
    1.72 +}
    1.73 +
    1.74 +double
    1.75 +MetroUtils::ScaleFactor()
    1.76 +{
    1.77 +  // Return the resolution scale factor reported by the metro environment.
    1.78 +  // XXX TODO: also consider the desktop resolution setting, as IE appears to do?
    1.79 +  ComPtr<IDisplayInformationStatics> dispInfoStatics;
    1.80 +  if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayInformation).Get(),
    1.81 +                                      dispInfoStatics.GetAddressOf()))) {
    1.82 +    ComPtr<IDisplayInformation> dispInfo;
    1.83 +    if (SUCCEEDED(dispInfoStatics->GetForCurrentView(&dispInfo))) {
    1.84 +      ResolutionScale scale;
    1.85 +      if (SUCCEEDED(dispInfo->get_ResolutionScale(&scale))) {
    1.86 +        return (double)scale / 100.0;
    1.87 +      }
    1.88 +    }
    1.89 +  }
    1.90 +
    1.91 +  ComPtr<IDisplayPropertiesStatics> dispProps;
    1.92 +  if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(),
    1.93 +                                     dispProps.GetAddressOf()))) {
    1.94 +    ResolutionScale scale;
    1.95 +    if (SUCCEEDED(dispProps->get_ResolutionScale(&scale))) {
    1.96 +      return (double)scale / 100.0;
    1.97 +    }
    1.98 +  }
    1.99 +
   1.100 +  return 1.0;
   1.101 +}
   1.102 +
   1.103 +nsIntPoint
   1.104 +MetroUtils::LogToPhys(const Point& aPt)
   1.105 +{
   1.106 +  double factor = LogToPhysFactor();
   1.107 +  return nsIntPoint(int32_t(NS_round(aPt.X * factor)), int32_t(NS_round(aPt.Y * factor)));
   1.108 +}
   1.109 +
   1.110 +nsIntRect
   1.111 +MetroUtils::LogToPhys(const Rect& aRect)
   1.112 +{
   1.113 +  double factor = LogToPhysFactor();
   1.114 +  return nsIntRect(int32_t(NS_round(aRect.X * factor)),
   1.115 +                   int32_t(NS_round(aRect.Y * factor)),
   1.116 +                   int32_t(NS_round(aRect.Width * factor)),
   1.117 +                   int32_t(NS_round(aRect.Height * factor)));
   1.118 +}
   1.119 +
   1.120 +Point
   1.121 +MetroUtils::PhysToLog(const nsIntPoint& aPt)
   1.122 +{
   1.123 +  // Points contain FLOATs
   1.124 +  FLOAT factor = (FLOAT)PhysToLogFactor();
   1.125 +  Point p = { FLOAT(aPt.x) * factor, FLOAT(aPt.y) * factor };
   1.126 +  return p;
   1.127 +}
   1.128 +
   1.129 +nsresult
   1.130 +MetroUtils::FireObserver(const char* aMessage, const char16_t* aData)
   1.131 +{
   1.132 +  nsCOMPtr<nsIObserverService> observerService =
   1.133 +    mozilla::services::GetObserverService();
   1.134 +  if (observerService) {
   1.135 +    return observerService->NotifyObservers(nullptr, aMessage, aData);
   1.136 +  }
   1.137 +  return NS_ERROR_FAILURE;
   1.138 +}
   1.139 +
   1.140 +HRESULT MetroUtils::CreateUri(HSTRING aUriStr, ComPtr<IUriRuntimeClass>& aUriOut)
   1.141 +{
   1.142 +  HRESULT hr;
   1.143 +  ComPtr<IUriRuntimeClassFactory> uriFactory;
   1.144 +  hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);
   1.145 +  AssertRetHRESULT(hr, hr);
   1.146 +  ComPtr<IUriRuntimeClass> uri;
   1.147 +  return uriFactory->CreateUri(aUriStr, &aUriOut);
   1.148 +}
   1.149 +
   1.150 +HRESULT MetroUtils::CreateUri(HString& aHString, ComPtr<IUriRuntimeClass>& aUriOut)
   1.151 +{
   1.152 +  return MetroUtils::CreateUri(aHString.Get(), aUriOut);
   1.153 +}
   1.154 +
   1.155 +HRESULT
   1.156 +MetroUtils::GetViewState(ApplicationViewState& aState)
   1.157 +{
   1.158 +  HRESULT hr;
   1.159 +  ComPtr<IApplicationViewStatics> appViewStatics;
   1.160 +  hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ViewManagement_ApplicationView).Get(),
   1.161 +    appViewStatics.GetAddressOf());
   1.162 +  AssertRetHRESULT(hr, hr);
   1.163 +  hr = appViewStatics->get_Value(&aState);
   1.164 +  return hr;
   1.165 +}
   1.166 +
   1.167 +HRESULT
   1.168 +MetroUtils::TryUnsnap(bool* aResult)
   1.169 +{
   1.170 +  HRESULT hr;
   1.171 +  ComPtr<IApplicationViewStatics> appViewStatics;
   1.172 +  hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ViewManagement_ApplicationView).Get(),
   1.173 +    appViewStatics.GetAddressOf());
   1.174 +  AssertRetHRESULT(hr, hr);
   1.175 +  boolean success = false;
   1.176 +  hr = appViewStatics->TryUnsnap(&success);
   1.177 +  if (aResult)
   1.178 +    *aResult = success;
   1.179 +  return hr;
   1.180 +}
   1.181 +
   1.182 +HRESULT
   1.183 +MetroUtils::ShowSettingsFlyout()
   1.184 +{
   1.185 +  ComPtr<ISettingsPaneStatics> settingsPaneStatics;
   1.186 +  HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ApplicationSettings_SettingsPane).Get(),
   1.187 +                                    settingsPaneStatics.GetAddressOf());
   1.188 +  if (SUCCEEDED(hr)) {
   1.189 +    settingsPaneStatics->Show();
   1.190 +  }
   1.191 +
   1.192 +  return hr;
   1.193 +}

mercurial