feat: Completed API for cocoa, b=(no-bug), c=common

This commit is contained in:
mr. m 2025-05-13 01:25:55 +02:00
parent 0b34cc3033
commit 3566289cb5
No known key found for this signature in database
GPG key ID: 419302196C23B258
8 changed files with 92 additions and 34 deletions

View file

@ -451,17 +451,7 @@ var gZenUIManager = {
const [toast, reused] = this._createToastElement(messageId, options); const [toast, reused] = this._createToastElement(messageId, options);
this._toastContainer.removeAttribute('hidden'); this._toastContainer.removeAttribute('hidden');
this._toastContainer.appendChild(toast); this._toastContainer.appendChild(toast);
if (reused) { const timeoutFunction = () => {
await this.motion.animate(toast, { scale: 0.2 }, { duration: 0.1, bounce: 0 });
}
if (!toast.style.hasOwnProperty('transform')) {
toast.style.transform = 'scale(0)';
}
await this.motion.animate(toast, { scale: 1 }, { type: 'spring', bounce: 0.2, duration: 0.5 });
if (this._toastTimeouts[messageId]) {
clearTimeout(this._toastTimeouts[messageId]);
}
this._toastTimeouts[messageId] = setTimeout(() => {
this.motion this.motion
.animate(toast, { opacity: [1, 0], scale: [1, 0.5] }, { duration: 0.2, bounce: 0 }) .animate(toast, { opacity: [1, 0], scale: [1, 0.5] }, { duration: 0.2, bounce: 0 })
.then(() => { .then(() => {
@ -470,7 +460,30 @@ var gZenUIManager = {
this._toastContainer.setAttribute('hidden', true); this._toastContainer.setAttribute('hidden', true);
} }
}); });
}, options.timeout || 2000); };
if (reused) {
await this.motion.animate(toast, { scale: 0.2 }, { duration: 0.1, bounce: 0 });
} else {
toast.addEventListener('mouseover', () => {
if (this._toastTimeouts[messageId]) {
clearTimeout(this._toastTimeouts[messageId]);
}
});
toast.addEventListener('mouseout', () => {
if (this._toastTimeouts[messageId]) {
clearTimeout(this._toastTimeouts[messageId]);
}
this._toastTimeouts[messageId] = setTimeout(timeoutFunction, options.timeout || 2000);
});
}
if (!toast.style.hasOwnProperty('transform')) {
toast.style.transform = 'scale(0)';
}
await this.motion.animate(toast, { scale: 1 }, { type: 'spring', bounce: 0.2, duration: 0.5 });
if (this._toastTimeouts[messageId]) {
clearTimeout(this._toastTimeouts[messageId]);
}
this._toastTimeouts[messageId] = setTimeout(timeoutFunction, options.timeout || 2000);
}, },
get panelUIPosition() { get panelUIPosition() {

View file

@ -363,6 +363,7 @@ menuitem {
translate: 100%; translate: 100%;
} }
gap: 10px;
z-index: 1000; z-index: 1000;
padding: 10px; padding: 10px;
border-radius: 12px; border-radius: 12px;
@ -387,10 +388,16 @@ menuitem {
font-size: smaller; font-size: smaller;
} }
& label {
margin-top: 0;
margin-bottom: 0;
}
& button { & button {
width: min-content; width: min-content;
padding: 0 12px !important; padding: 0 12px !important;
min-width: unset !important; min-width: unset !important;
margin: 0px !important;
} }
} }
} }

View file

@ -82,16 +82,17 @@ ZenCommonUtils::Share(nsIURI* url, const nsACString& title,
if (!IsSharingSupported()) { if (!IsSharingSupported()) {
return NS_OK; // We don't want to throw an error here return NS_OK; // We don't want to throw an error here
} }
ShareInternal(aWindow, url, title, text, aX, aY); return ShareInternal(aWindow, url, title, text, aX, aY);
return NS_OK;
} }
void ZenCommonUtils::ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url, nsresult ZenCommonUtils::ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url,
const nsACString& title, const nsACString& text, uint32_t aX, uint32_t aY) { const nsACString& title, const nsACString& text, uint32_t aX, uint32_t aY) {
// We shoud've had done pointer checks before, so we can assume // We shoud've had done pointer checks before, so we can assume
// aWindow is valid. // aWindow is valid.
#ifdef ZEN_CAN_SHARE_NATIVE #ifdef NS_ZEN_CAN_SHARE_NATIVE
::nsZenNativeShareInternal(aWindow, url, title, text, aX, aY); return ::nsZenNativeShareInternal::ShowNativeDialog(aWindow, url, title, text, aX, aY);
#else
return NS_ERROR_NOT_IMPLEMENTED;
#endif #endif
} }
@ -102,7 +103,7 @@ auto ZenCommonUtils::IsSharingSupported() -> bool {
#elif defined(NS_ZEN_CAN_SHARE_NATIVE) #elif defined(NS_ZEN_CAN_SHARE_NATIVE)
return NS_ZEN_CAN_SHARE_NATIVE; return NS_ZEN_CAN_SHARE_NATIVE;
#else #else
return true; return false;
#endif #endif
} }

View file

@ -41,7 +41,7 @@ class ZenCommonUtils final : public nsIZenCommonUtils {
*/ */
static auto ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url, static auto ShareInternal(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* url,
const nsACString& title, const nsACString& text, uint32_t aX, uint32_t aY) const nsACString& title, const nsACString& text, uint32_t aX, uint32_t aY)
-> void; -> nsresult;
}; };
} // namespace zen } // namespace zen

View file

@ -28,10 +28,9 @@ class nsZenNativeShareInternal final {
* @param aText The text to share. * @param aText The text to share.
* @returns void * @returns void
*/ */
auto ShowNativeDialog(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl, static auto ShowNativeDialog(nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl,
const nsACString& aTitle, const nsACString& aText, const nsACString& aTitle, const nsACString& aText, uint32_t aX = 0, uint32_t aY = 0)
uint32_t aX = 0, uint32_t aY = 0) const -> nsresult;
-> void;
nsZenNativeShareInternal() = default; nsZenNativeShareInternal() = default;
~nsZenNativeShareInternal() = default; ~nsZenNativeShareInternal() = default;

View file

@ -5,18 +5,44 @@
#include "ZenShareInternal.h" #include "ZenShareInternal.h"
#include "nsCocoaUtils.h" #include "nsCocoaUtils.h"
#include "nsPIDOMWindow.h"
#include "WidgetUtils.h"
#include "nsIWidget.h"
extern mozilla::LazyLogModule gCocoaUtilsLog; extern mozilla::LazyLogModule gCocoaUtilsLog;
#undef LOG #undef LOG
#define LOG(...) MOZ_LOG(gCocoaUtilsLog, mozilla::LogLevel::Debug, (__VA_ARGS__)) #define LOG(...) MOZ_LOG(gCocoaUtilsLog, mozilla::LogLevel::Info, (__VA_ARGS__))
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <AppKit/AppKit.h> #import <AppKit/AppKit.h>
auto nsZenNativeShareInternal::ShowNativeDialog( namespace zen {
nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl, using ::mozilla::widget::WidgetUtils;
const nsACString& aTitle, const nsACString& aText,
uint32_t aX, uint32_t aY) const /**
-> void { * Get the native NSWindow pointer from a DOM window.
*
* @param a_window The DOM window to get the native window from.
* @param a_nativeWindow The pointer to the native NSWindow.
* @return NS_OK on success, or an error code on failure.
*/
static nsresult GetNativeWindowPointerFromDOMWindow(mozIDOMWindowProxy* a_window,
NSWindow** a_nativeWindow) {
*a_nativeWindow = nil;
if (!a_window) return NS_ERROR_INVALID_ARG;
nsPIDOMWindowOuter* win = nsPIDOMWindowOuter::From(a_window);
nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(win);
if (!widget) {
return NS_ERROR_FAILURE;
}
*a_nativeWindow = (NSWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
return NS_OK;
}
}
auto nsZenNativeShareInternal::ShowNativeDialog(nsCOMPtr<mozIDOMWindowProxy>& aWindow,
nsIURI* aUrl, const nsACString& aTitle, const nsACString& aText, uint32_t aX, uint32_t aY)
-> nsresult {
// Just use the URL since apple doesn't support sharing text // Just use the URL since apple doesn't support sharing text
// and title in the share dialog // and title in the share dialog
nsAutoCString pageUrlAsStringTemp; nsAutoCString pageUrlAsStringTemp;
@ -30,14 +56,22 @@ auto nsZenNativeShareInternal::ShowNativeDialog(
NSURL* pageUrl = nsCocoaUtils::ToNSURL( NSURL* pageUrl = nsCocoaUtils::ToNSURL(
NS_ConvertUTF8toUTF16(pageUrlAsStringTemp) NS_ConvertUTF8toUTF16(pageUrlAsStringTemp)
); );
LOG("pageUrl: %s", pageUrlAsStringTemp.get());
if (!pageUrl || (![pageUrl.scheme isEqualToString:@"https"] && if (!pageUrl || (![pageUrl.scheme isEqualToString:@"https"] &&
![pageUrl.scheme isEqualToString:@"http"])) { ![pageUrl.scheme isEqualToString:@"http"])) {
return; return NS_ERROR_FAILURE;
} }
NSSharingServicePicker* sharingPicker = NSSharingServicePicker* sharingPicker =
[[NSSharingServicePicker alloc] initWithItems:@[ pageUrl ]]; [[NSSharingServicePicker alloc] initWithItems:@[ pageUrl ]];
NSWindow* cocoaMru = nil;
zen::GetNativeWindowPointerFromDOMWindow(aWindow, &cocoaMru);
if (!cocoaMru) {
LOG("ERROR: failed to get native window pointer");
return NS_ERROR_FAILURE;
}
// Create a rect for the sharing picker // Create a rect for the sharing picker
NSRect rect = NSMakeRect(aX, aY, 0, 0); NSRect rect = NSMakeRect(aX, aY, 0, 0);
[sharingPicker showRelativeToRect:rect]; [sharingPicker showRelativeToRect:rect
ofView:cocoaMru.contentView
preferredEdge:NSMinYEdge];
return NS_OK;
} }

View file

@ -6,4 +6,7 @@ SOURCES += [
LOCAL_INCLUDES += [ LOCAL_INCLUDES += [
"../", "../",
] "/widget",
"/widget/cocoa",
"/xpcom/base",
]

View file

@ -9,7 +9,7 @@ auto nsZenNativeShareInternal::ShowNativeDialog(
nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl, nsCOMPtr<mozIDOMWindowProxy>& aWindow, nsIURI* aUrl,
const nsACString& aTitle, const nsACString& aText, const nsACString& aTitle, const nsACString& aText,
uint32_t aX, uint32_t aY) const uint32_t aX, uint32_t aY) const
-> void { -> nsresult {
nsAutoCString urlString; nsAutoCString urlString;
if (aUrl) { if (aUrl) {
nsresult rv = aUrl->GetSpec(urlString); nsresult rv = aUrl->GetSpec(urlString);
@ -21,4 +21,5 @@ auto nsZenNativeShareInternal::ShowNativeDialog(
(void)WindowsUIUtils::Share(NS_ConvertUTF8toUTF16_MaybeVoid(aTitle), (void)WindowsUIUtils::Share(NS_ConvertUTF8toUTF16_MaybeVoid(aTitle),
NS_ConvertUTF8toUTF16_MaybeVoid(aText), NS_ConvertUTF8toUTF16_MaybeVoid(aText),
NS_ConvertUTF8toUTF16_MaybeVoid(urlString)); NS_ConvertUTF8toUTF16_MaybeVoid(urlString));
return NS_OK;
} }