feat: Added cocoa APIs, b=(no-bug), c=common

This commit is contained in:
Mr. M
2025-05-12 21:53:15 +02:00
parent b4c7a64631
commit f58dbd71b6
9 changed files with 133 additions and 42 deletions

View File

@@ -3,13 +3,14 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ZenCommonUtils.h"
#include "ZenShareInternal.h"
#include "nsGlobalWindowOuter.h"
#include "nsQueryObject.h"
#include "nsIWindowMediator.h"
#include "nsServiceManagerUtils.h"
#include "nsISharePicker.h"
#include "mozilla/dom/Promise.h"
#if defined(XP_WIN)
# include "mozilla/WindowsVersion.h"
#endif
@@ -51,7 +52,6 @@ static nsCOMPtr<mozIDOMWindowProxy> GetMostRecentWindow() {
}
using mozilla::dom::WindowGlobalChild;
using Promise = mozilla::dom::Promise;
#define NS_ZEN_CAN_SHARE_FAILURE() \
*canShare = false; \
@@ -74,7 +74,7 @@ ZenCommonUtils::CanShare(bool* canShare) {
NS_IMETHODIMP
ZenCommonUtils::Share(nsIURI* url, const nsACString& title,
const nsACString& text, mozilla::dom::Promise** _retval) {
const nsACString& text, uint32_t aX, uint32_t aY) {
auto aWindow = GetMostRecentWindow();
if (!aWindow) {
return NS_ERROR_NOT_AVAILABLE;
@@ -82,37 +82,25 @@ ZenCommonUtils::Share(nsIURI* url, const nsACString& title,
if (!IsSharingSupported()) {
return NS_OK; // We don't want to throw an error here
}
if (!IsSharingSupported()) {
return NS_ERROR_NOT_AVAILABLE;
}
*_retval = ShareInternal(aWindow, url, title, text);
*_retval = ShareInternal(aWindow, url, title, text, aX, aY);
return NS_OK;
}
mozilla::dom::Promise* ZenCommonUtils::ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url,
const nsACString& title, const nsACString& text) {
void ZenCommonUtils::ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url,
const nsACString& title, const nsACString& text, uint32_t aX, uint32_t aY) {
// We shoud've had done pointer checks before, so we can assume
// aWindow is valid.
nsCOMPtr<nsISharePicker> sharePicker =
do_GetService("@mozilla.org/sharepicker;1");
if (!sharePicker) {
return nullptr;
}
sharePicker->Init(aWindow);
RefPtr<Promise> promise;
nsresult rv = sharePicker->Share(title, text, url, getter_AddRefs(promise));
if (NS_FAILED(rv)) {
return nullptr;
}
return promise;
#ifdef ZEN_CAN_SHARE_NATIVE
::nsZenNativeShareInternal(aWindow, url, title, text, aX, aY);
#else
}
auto ZenCommonUtils::IsSharingSupported() -> bool {
#if defined(XP_UNIX) && !defined(XP_MACOSX)
return true;
#elif defined(XP_WIN) && !defined(__MINGW32__)
#if defined(XP_WIN) && !defined(__MINGW32__)
// The first public build that supports ShareCanceled API
return IsWindows10BuildOrLater(18956);
#elif defined(NS_ZEN_CAN_SHARE_NATIVE)
return NS_ZEN_CAN_SHARE_NATIVE;
#else
return true;
#endif

View File

@@ -40,10 +40,10 @@ class ZenCommonUtils final : public nsIZenCommonUtils {
* @returns A promise that resolves when the share is complete.
*/
static auto ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url,
const nsACString& title, const nsACString& text)
-> mozilla::dom::Promise*;
const nsACString& title, const nsACString& text, uint32_t aX, uint32_t aY)
-> void;
};
} // namespace zen
} // namespace zen
#endif

View File

@@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_nsZenShareInternal_h__
#define mozilla_nsZenShareInternal_h__
#include "nsIZenCommonUtils.h"
#include "nsIDOMWindow.h"
#include "nsGlobalWindowOuter.h"
#include "nsIURI.h"
#if defined(XP_WIN) || defined(XP_MACOSX)
#define NS_ZEN_CAN_SHARE_NATIVE true
class nsZenNativeShareInternal final {
public:
/**
* @brief Use the native share dialog. This only works on Windows and MacOS
* since the native share dialog is not available on other platforms.
* Macos does need pointer coordinates to show the share dialog while
* Windows does not since it just displays a dialog on the middle of the
* screen.
* @param aWindow The window to use for the share dialog.
* @param aUrl The URL to share.
* @param aTitle The title of the share.
* @param aText The text to share.
* @returns void
*/
auto ShowNativeDialog(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl,
const nsACString& aTitle, const nsACString& aText,
uint32_t aX = 0, uint32_t aY = 0) const
-> void;
nsZenNativeShareInternal() = default;
~nsZenNativeShareInternal() = default;
};
#endif
#endif

View File

@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ZenShareInternal.h"
#include "nsIWindowsUIUtils.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface nsZenNativeShareInternal : NSObject
- (void)ShowNativeDialog:(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl,
const nsACString& aTitle, const nsACString& aText,
uint32_t aX, uint32_t aY) {
// Just use the URL since apple doesn't support sharing text
// and title in the share dialog
NSSharingServicePicker* sharingPicker =
[[NSSharingServicePicker alloc] initWithItems:@[ aUrl ]];
sharingPicker.delegate = self;
sharingPicker.showRelativeToRect:NSMakeRect(aX, aY, 0, 0)
ofView:(NSView*)aWindow
preferredEdge:NSMinYEdge;
}
@end

View File

@@ -0,0 +1,9 @@
FINAL_LIBRARY = "xul"
SOURCES += [
"ZenShareInternal.mm",
]
LOCAL_INCLUDES += [
"../",
]

View File

@@ -5,6 +5,7 @@ XPIDL_SOURCES += [
EXPORTS.mozilla += [
"ZenCommonUtils.h",
"ZenShareInternal.h
]
SOURCES += [
@@ -15,20 +16,11 @@ XPCOM_MANIFESTS += [
"components.conf",
]
LOCAL_INCLUDES += [
"/dom/base",
"/dom/ipc",
"/gfx/2d",
"/layout/base",
"/layout/forms",
"/layout/generic",
"/layout/painting",
"/layout/xul",
"/layout/xul/tree/",
"/view",
"/widget",
"/widget/headless",
]
if CONFIG["MOZ_WIDGET_TOOLKIT"] == "windows":
DIRS += ["windows"]
if CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
DIRS += ["cocoa"]
FINAL_LIBRARY = "xul"
XPIDL_MODULE = "zen"

View File

@@ -16,9 +16,12 @@ interface nsIZenCommonUtils : nsISupports {
* @param url The URL to share.
* @param title The title of the share.
* @param text The text to share.
* @param x The x coordinate of the share dialog.
* @param y The y coordinate of the share dialog.
* @returns A promise that resolves when the share is complete.
*/
Promise share(in nsIURI url, in ACString title, in ACString text);
void share(in nsIURI url, in ACString title, in ACString text,
in uint32_t x, in uint32_t y);
/*
* @brief Check if the current context can share data.
* @param window The window to check.

View File

@@ -0,0 +1,24 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ZenShareInternal.h"
#include "nsIWindowsUIUtils.h"
auto nsZenNativeShareInternal::ShowNativeDialog(
nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl,
const nsACString& aTitle, const nsACString& aText,
uint32_t aX, uint32_t aY) const
-> void {
nsAutoCString urlString;
if (aUrl) {
nsresult rv = aUrl->GetSpec(urlString);
MOZ_ASSERT(NS_SUCCEEDED(rv));
mozilla::Unused << rv;
} else {
urlString.SetIsVoid(true);
}
(void)WindowsUIUtils::Share(NS_ConvertUTF8toUTF16_MaybeVoid(aTitle),
NS_ConvertUTF8toUTF16_MaybeVoid(aText),
NS_ConvertUTF8toUTF16_MaybeVoid(urlString));
}

View File

@@ -0,0 +1,9 @@
FINAL_LIBRARY = "xul"
SOURCES += [
"ZenShareInternal.cpp",
]
LOCAL_INCLUDES += [
"../",
]