michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "MetroUtils.h" michael@0: #include michael@0: #include "nsICommandLineRunner.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIBrowserDOMWindow.h" michael@0: #include "nsIWebNavigation.h" michael@0: #include "nsIDocShellTreeItem.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIDOMChromeWindow.h" michael@0: #include "nsIWindowMediator.h" michael@0: #include "nsIURI.h" michael@0: #include "prlog.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsRect.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "DisplayInfo_sdk81.h" michael@0: michael@0: using namespace ABI::Windows::UI::ApplicationSettings; michael@0: michael@0: using namespace mozilla; michael@0: michael@0: using namespace ABI::Windows::Foundation; michael@0: using namespace Microsoft::WRL; michael@0: using namespace Microsoft::WRL::Wrappers; michael@0: using namespace ABI::Windows::UI::ViewManagement; michael@0: using namespace ABI::Windows::Graphics::Display; michael@0: michael@0: // Conversion between logical and physical coordinates michael@0: michael@0: double michael@0: MetroUtils::LogToPhysFactor() michael@0: { michael@0: ComPtr dispInfoStatics; michael@0: if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayInformation).Get(), michael@0: dispInfoStatics.GetAddressOf()))) { michael@0: ComPtr dispInfo; michael@0: if (SUCCEEDED(dispInfoStatics->GetForCurrentView(&dispInfo))) { michael@0: FLOAT dpi; michael@0: if (SUCCEEDED(dispInfo->get_LogicalDpi(&dpi))) { michael@0: return (double)dpi / 96.0f; michael@0: } michael@0: } michael@0: } michael@0: michael@0: ComPtr dispProps; michael@0: if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(), michael@0: dispProps.GetAddressOf()))) { michael@0: FLOAT dpi; michael@0: if (SUCCEEDED(dispProps->get_LogicalDpi(&dpi))) { michael@0: return (double)dpi / 96.0f; michael@0: } michael@0: } michael@0: michael@0: return 1.0; michael@0: } michael@0: michael@0: double michael@0: MetroUtils::PhysToLogFactor() michael@0: { michael@0: return 1.0 / LogToPhysFactor(); michael@0: } michael@0: michael@0: double michael@0: MetroUtils::ScaleFactor() michael@0: { michael@0: // Return the resolution scale factor reported by the metro environment. michael@0: // XXX TODO: also consider the desktop resolution setting, as IE appears to do? michael@0: ComPtr dispInfoStatics; michael@0: if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayInformation).Get(), michael@0: dispInfoStatics.GetAddressOf()))) { michael@0: ComPtr dispInfo; michael@0: if (SUCCEEDED(dispInfoStatics->GetForCurrentView(&dispInfo))) { michael@0: ResolutionScale scale; michael@0: if (SUCCEEDED(dispInfo->get_ResolutionScale(&scale))) { michael@0: return (double)scale / 100.0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: ComPtr dispProps; michael@0: if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(), michael@0: dispProps.GetAddressOf()))) { michael@0: ResolutionScale scale; michael@0: if (SUCCEEDED(dispProps->get_ResolutionScale(&scale))) { michael@0: return (double)scale / 100.0; michael@0: } michael@0: } michael@0: michael@0: return 1.0; michael@0: } michael@0: michael@0: nsIntPoint michael@0: MetroUtils::LogToPhys(const Point& aPt) michael@0: { michael@0: double factor = LogToPhysFactor(); michael@0: return nsIntPoint(int32_t(NS_round(aPt.X * factor)), int32_t(NS_round(aPt.Y * factor))); michael@0: } michael@0: michael@0: nsIntRect michael@0: MetroUtils::LogToPhys(const Rect& aRect) michael@0: { michael@0: double factor = LogToPhysFactor(); michael@0: return nsIntRect(int32_t(NS_round(aRect.X * factor)), michael@0: int32_t(NS_round(aRect.Y * factor)), michael@0: int32_t(NS_round(aRect.Width * factor)), michael@0: int32_t(NS_round(aRect.Height * factor))); michael@0: } michael@0: michael@0: Point michael@0: MetroUtils::PhysToLog(const nsIntPoint& aPt) michael@0: { michael@0: // Points contain FLOATs michael@0: FLOAT factor = (FLOAT)PhysToLogFactor(); michael@0: Point p = { FLOAT(aPt.x) * factor, FLOAT(aPt.y) * factor }; michael@0: return p; michael@0: } michael@0: michael@0: nsresult michael@0: MetroUtils::FireObserver(const char* aMessage, const char16_t* aData) michael@0: { michael@0: nsCOMPtr observerService = michael@0: mozilla::services::GetObserverService(); michael@0: if (observerService) { michael@0: return observerService->NotifyObservers(nullptr, aMessage, aData); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: HRESULT MetroUtils::CreateUri(HSTRING aUriStr, ComPtr& aUriOut) michael@0: { michael@0: HRESULT hr; michael@0: ComPtr uriFactory; michael@0: hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory); michael@0: AssertRetHRESULT(hr, hr); michael@0: ComPtr uri; michael@0: return uriFactory->CreateUri(aUriStr, &aUriOut); michael@0: } michael@0: michael@0: HRESULT MetroUtils::CreateUri(HString& aHString, ComPtr& aUriOut) michael@0: { michael@0: return MetroUtils::CreateUri(aHString.Get(), aUriOut); michael@0: } michael@0: michael@0: HRESULT michael@0: MetroUtils::GetViewState(ApplicationViewState& aState) michael@0: { michael@0: HRESULT hr; michael@0: ComPtr appViewStatics; michael@0: hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ViewManagement_ApplicationView).Get(), michael@0: appViewStatics.GetAddressOf()); michael@0: AssertRetHRESULT(hr, hr); michael@0: hr = appViewStatics->get_Value(&aState); michael@0: return hr; michael@0: } michael@0: michael@0: HRESULT michael@0: MetroUtils::TryUnsnap(bool* aResult) michael@0: { michael@0: HRESULT hr; michael@0: ComPtr appViewStatics; michael@0: hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ViewManagement_ApplicationView).Get(), michael@0: appViewStatics.GetAddressOf()); michael@0: AssertRetHRESULT(hr, hr); michael@0: boolean success = false; michael@0: hr = appViewStatics->TryUnsnap(&success); michael@0: if (aResult) michael@0: *aResult = success; michael@0: return hr; michael@0: } michael@0: michael@0: HRESULT michael@0: MetroUtils::ShowSettingsFlyout() michael@0: { michael@0: ComPtr settingsPaneStatics; michael@0: HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ApplicationSettings_SettingsPane).Get(), michael@0: settingsPaneStatics.GetAddressOf()); michael@0: if (SUCCEEDED(hr)) { michael@0: settingsPaneStatics->Show(); michael@0: } michael@0: michael@0: return hr; michael@0: }