2018-04-19 07:01:19 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/cocoa/electron_ns_window_delegate.h"
|
2018-04-19 07:01:19 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
#include "base/mac/mac_util.h"
|
2019-06-05 00:27:07 +00:00
|
|
|
#include "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/browser/native_window_mac.h"
|
|
|
|
#include "shell/browser/ui/cocoa/electron_preview_item.h"
|
|
|
|
#include "shell/browser/ui/cocoa/electron_touch_bar.h"
|
2021-06-02 02:37:10 +00:00
|
|
|
#include "ui/gfx/geometry/resize_utils.h"
|
2019-08-08 02:58:56 +00:00
|
|
|
#include "ui/gfx/mac/coordinate_conversion.h"
|
2019-06-05 00:27:07 +00:00
|
|
|
#include "ui/views/cocoa/native_widget_mac_ns_window_host.h"
|
2018-04-24 08:23:08 +00:00
|
|
|
#include "ui/views/widget/native_widget_mac.h"
|
2018-04-19 07:01:19 +00:00
|
|
|
|
2019-05-03 18:11:41 +00:00
|
|
|
using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle;
|
2021-04-21 14:56:25 +00:00
|
|
|
using FullScreenTransitionState =
|
|
|
|
electron::NativeWindowMac::FullScreenTransitionState;
|
2019-05-03 18:11:41 +00:00
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
@implementation ElectronNSWindowDelegate
|
|
|
|
|
|
|
|
- (id)initWithShell:(electron::NativeWindowMac*)shell {
|
2018-04-24 08:23:08 +00:00
|
|
|
// The views library assumes the window delegate must be an instance of
|
|
|
|
// ViewsNSWindowDelegate, since we don't have a way to override the creation
|
|
|
|
// of NSWindowDelegate, we have to dynamically replace the window delegate
|
|
|
|
// on the fly.
|
|
|
|
// TODO(zcbenz): Add interface in NativeWidgetMac to allow overriding creating
|
|
|
|
// window delegate.
|
2019-06-05 00:27:07 +00:00
|
|
|
auto* bridge_host = views::NativeWidgetMacNSWindowHost::GetFromNativeWindow(
|
2019-01-12 01:00:43 +00:00
|
|
|
shell->GetNativeWindow());
|
2019-07-03 01:22:09 +00:00
|
|
|
auto* bridged_view = bridge_host->GetInProcessNSWindowBridge();
|
2018-04-26 00:48:12 +00:00
|
|
|
if ((self = [super initWithBridgedNativeWidget:bridged_view])) {
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_ = shell;
|
|
|
|
is_zooming_ = false;
|
2019-01-09 20:25:19 +00:00
|
|
|
level_ = [shell_->GetNativeWindow().GetNativeNSWindow() level];
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:18:50 +00:00
|
|
|
#pragma mark - NSWindowDelegate
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)windowDidChangeOcclusionState:(NSNotification*)notification {
|
2018-04-19 07:01:19 +00:00
|
|
|
// notification.object is the window that changed its state.
|
2018-04-20 18:47:04 +00:00
|
|
|
// It's safe to use self.window instead if you don't assign one delegate to
|
|
|
|
// many windows
|
|
|
|
NSWindow* window = notification.object;
|
2018-04-19 07:01:19 +00:00
|
|
|
|
|
|
|
// check occlusion binary flag
|
2018-04-20 18:47:04 +00:00
|
|
|
if (window.occlusionState & NSWindowOcclusionStateVisible) {
|
|
|
|
// The app is visible
|
|
|
|
shell_->NotifyWindowShow();
|
|
|
|
} else {
|
|
|
|
// The app is not visible
|
|
|
|
shell_->NotifyWindowHide();
|
|
|
|
}
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the user clicks the zoom button or selects it from the Window
|
|
|
|
// menu to determine the "standard size" of the window.
|
|
|
|
- (NSRect)windowWillUseStandardFrame:(NSWindow*)window
|
|
|
|
defaultFrame:(NSRect)frame {
|
2021-09-21 10:04:32 +00:00
|
|
|
if (!shell_->zoom_to_page_width()) {
|
|
|
|
if (shell_->GetAspectRatio() > 0.0)
|
|
|
|
shell_->set_default_frame_for_zoom(frame);
|
2018-04-19 07:01:19 +00:00
|
|
|
return frame;
|
2021-09-21 10:04:32 +00:00
|
|
|
}
|
2018-04-19 07:01:19 +00:00
|
|
|
|
|
|
|
// If the shift key is down, maximize.
|
2022-06-01 06:12:47 +00:00
|
|
|
if ([[NSApp currentEvent] modifierFlags] & NSEventModifierFlagShift)
|
2018-04-19 07:01:19 +00:00
|
|
|
return frame;
|
|
|
|
|
|
|
|
// Get preferred width from observers. Usually the page width.
|
|
|
|
int preferred_width = 0;
|
2022-04-05 16:25:07 +00:00
|
|
|
shell_->NotifyWindowRequestPreferredWidth(&preferred_width);
|
2018-04-19 07:01:19 +00:00
|
|
|
|
|
|
|
// Never shrink from the current size on zoom.
|
|
|
|
NSRect window_frame = [window frame];
|
2018-04-20 18:47:04 +00:00
|
|
|
CGFloat zoomed_width =
|
|
|
|
std::max(static_cast<CGFloat>(preferred_width), NSWidth(window_frame));
|
2018-04-19 07:01:19 +00:00
|
|
|
|
|
|
|
// |frame| determines our maximum extents. We need to set the origin of the
|
|
|
|
// frame -- and only move it left if necessary.
|
|
|
|
if (window_frame.origin.x + zoomed_width > NSMaxX(frame))
|
|
|
|
frame.origin.x = NSMaxX(frame) - zoomed_width;
|
|
|
|
else
|
|
|
|
frame.origin.x = window_frame.origin.x;
|
|
|
|
|
|
|
|
// Set the width. Don't touch y or height.
|
|
|
|
frame.size.width = zoomed_width;
|
|
|
|
|
2021-09-21 10:04:32 +00:00
|
|
|
if (shell_->GetAspectRatio() > 0.0)
|
|
|
|
shell_->set_default_frame_for_zoom(frame);
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2019-07-12 08:46:35 +00:00
|
|
|
- (void)windowDidBecomeMain:(NSNotification*)notification {
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowFocus();
|
2021-01-30 23:15:10 +00:00
|
|
|
shell_->RedrawTrafficLights();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:46:35 +00:00
|
|
|
- (void)windowDidResignMain:(NSNotification*)notification {
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowBlur();
|
2021-01-30 23:15:10 +00:00
|
|
|
shell_->RedrawTrafficLights();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:15:28 +00:00
|
|
|
- (void)windowDidBecomeKey:(NSNotification*)notification {
|
|
|
|
shell_->NotifyWindowIsKeyChanged(true);
|
2021-01-30 23:15:10 +00:00
|
|
|
shell_->RedrawTrafficLights();
|
2020-06-29 20:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResignKey:(NSNotification*)notification {
|
|
|
|
// If our app is still active and we're still the key window, ignore this
|
|
|
|
// message, since it just means that a menu extra (on the "system status bar")
|
|
|
|
// was activated; we'll get another |-windowDidResignKey| if we ever really
|
|
|
|
// lose key window status.
|
|
|
|
if ([NSApp isActive] && ([NSApp keyWindow] == [notification object]))
|
|
|
|
return;
|
|
|
|
|
|
|
|
shell_->NotifyWindowIsKeyChanged(false);
|
2021-01-30 23:15:10 +00:00
|
|
|
shell_->RedrawTrafficLights();
|
2020-06-29 20:15:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
- (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize {
|
|
|
|
NSSize newSize = frameSize;
|
|
|
|
double aspectRatio = shell_->GetAspectRatio();
|
|
|
|
|
|
|
|
if (aspectRatio > 0.0) {
|
|
|
|
gfx::Size windowSize = shell_->GetSize();
|
|
|
|
gfx::Size contentSize = shell_->GetContentSize();
|
|
|
|
gfx::Size extraSize = shell_->GetAspectRatioExtraSize();
|
|
|
|
|
|
|
|
double extraWidthPlusFrame =
|
|
|
|
windowSize.width() - contentSize.width() + extraSize.width();
|
|
|
|
double extraHeightPlusFrame =
|
|
|
|
windowSize.height() - contentSize.height() + extraSize.height();
|
|
|
|
|
|
|
|
newSize.width =
|
|
|
|
roundf((frameSize.height - extraHeightPlusFrame) * aspectRatio +
|
|
|
|
extraWidthPlusFrame);
|
|
|
|
newSize.height =
|
|
|
|
roundf((newSize.width - extraWidthPlusFrame) / aspectRatio +
|
|
|
|
extraHeightPlusFrame);
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:37:10 +00:00
|
|
|
if (!resizingHorizontally_) {
|
|
|
|
NSWindow* window = shell_->GetNativeWindow().GetNativeNSWindow();
|
|
|
|
const auto widthDelta = frameSize.width - [window frame].size.width;
|
|
|
|
const auto heightDelta = frameSize.height - [window frame].size.height;
|
|
|
|
resizingHorizontally_ = std::abs(widthDelta) > std::abs(heightDelta);
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:53:01 +00:00
|
|
|
{
|
|
|
|
bool prevent_default = false;
|
2019-08-08 02:58:56 +00:00
|
|
|
NSRect new_bounds = NSMakeRect(sender.frame.origin.x, sender.frame.origin.y,
|
|
|
|
newSize.width, newSize.height);
|
|
|
|
shell_->NotifyWindowWillResize(gfx::ScreenRectFromNSRect(new_bounds),
|
2021-06-02 02:37:10 +00:00
|
|
|
*resizingHorizontally_
|
|
|
|
? gfx::ResizeEdge::kRight
|
|
|
|
: gfx::ResizeEdge::kBottom,
|
2019-08-08 02:58:56 +00:00
|
|
|
&prevent_default);
|
2018-07-27 09:53:01 +00:00
|
|
|
if (prevent_default) {
|
|
|
|
return sender.frame.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
return newSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResize:(NSNotification*)notification {
|
2018-04-24 10:00:44 +00:00
|
|
|
[super windowDidResize:notification];
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowResize();
|
2021-08-04 00:31:12 +00:00
|
|
|
shell_->RedrawTrafficLights();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 14:53:43 +00:00
|
|
|
- (void)windowWillMove:(NSNotification*)notification {
|
|
|
|
NSWindow* window = [notification object];
|
|
|
|
NSSize size = [[window contentView] frame].size;
|
|
|
|
NSRect new_bounds = NSMakeRect(window.frame.origin.x, window.frame.origin.y,
|
|
|
|
size.width, size.height);
|
|
|
|
bool prevent_default = false;
|
|
|
|
|
|
|
|
// prevent_default has no effect
|
|
|
|
shell_->NotifyWindowWillMove(gfx::ScreenRectFromNSRect(new_bounds),
|
|
|
|
&prevent_default);
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
- (void)windowDidMove:(NSNotification*)notification {
|
2018-04-24 10:00:44 +00:00
|
|
|
[super windowDidMove:notification];
|
2018-04-19 07:01:19 +00:00
|
|
|
// TODO(zcbenz): Remove the alias after figuring out a proper
|
|
|
|
// way to dispatch move.
|
|
|
|
shell_->NotifyWindowMove();
|
|
|
|
shell_->NotifyWindowMoved();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillMiniaturize:(NSNotification*)notification {
|
2019-01-09 20:25:19 +00:00
|
|
|
NSWindow* window = shell_->GetNativeWindow().GetNativeNSWindow();
|
2018-04-20 18:47:04 +00:00
|
|
|
// store the current status window level to be restored in
|
|
|
|
// windowDidDeminiaturize
|
2018-04-19 07:01:19 +00:00
|
|
|
level_ = [window level];
|
2019-10-18 19:57:34 +00:00
|
|
|
shell_->SetWindowLevel(NSNormalWindowLevel);
|
2022-06-09 08:48:50 +00:00
|
|
|
shell_->UpdateWindowOriginalFrame();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidMiniaturize:(NSNotification*)notification {
|
2018-04-24 10:00:44 +00:00
|
|
|
[super windowDidMiniaturize:notification];
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowMinimize();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidDeminiaturize:(NSNotification*)notification {
|
2018-04-24 10:00:44 +00:00
|
|
|
[super windowDidDeminiaturize:notification];
|
2019-10-18 19:57:34 +00:00
|
|
|
shell_->SetWindowLevel(level_);
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowRestore();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame {
|
|
|
|
is_zooming_ = true;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidEndLiveResize:(NSNotification*)notification {
|
2021-06-02 02:37:10 +00:00
|
|
|
resizingHorizontally_.reset();
|
2020-11-12 00:27:24 +00:00
|
|
|
shell_->NotifyWindowResized();
|
2018-04-19 07:01:19 +00:00
|
|
|
if (is_zooming_) {
|
|
|
|
if (shell_->IsMaximized())
|
|
|
|
shell_->NotifyWindowMaximize();
|
|
|
|
else
|
|
|
|
shell_->NotifyWindowUnmaximize();
|
|
|
|
is_zooming_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillEnterFullScreen:(NSNotification*)notification {
|
2022-06-03 07:47:19 +00:00
|
|
|
// Store resizable mask so it can be restored after exiting fullscreen.
|
|
|
|
is_resizable_ = shell_->HasStyleMask(NSWindowStyleMaskResizable);
|
|
|
|
|
2022-06-07 16:59:50 +00:00
|
|
|
shell_->set_fullscreen_transition_state(FullScreenTransitionState::ENTERING);
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2021-01-30 23:15:10 +00:00
|
|
|
shell_->NotifyWindowWillEnterFullScreen();
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2022-06-03 07:47:19 +00:00
|
|
|
// Set resizable to true before entering fullscreen.
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->SetResizable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidEnterFullScreen:(NSNotification*)notification {
|
2022-06-07 16:59:50 +00:00
|
|
|
shell_->set_fullscreen_transition_state(FullScreenTransitionState::NONE);
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowEnterFullScreen();
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2022-05-31 08:43:42 +00:00
|
|
|
if (shell_->HandleDeferredClose())
|
|
|
|
return;
|
|
|
|
|
2021-04-21 14:56:25 +00:00
|
|
|
shell_->HandlePendingFullscreenTransitions();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillExitFullScreen:(NSNotification*)notification {
|
2022-06-07 16:59:50 +00:00
|
|
|
shell_->set_fullscreen_transition_state(FullScreenTransitionState::EXITING);
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2021-01-30 23:15:10 +00:00
|
|
|
shell_->NotifyWindowWillLeaveFullScreen();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidExitFullScreen:(NSNotification*)notification {
|
2022-06-07 16:59:50 +00:00
|
|
|
shell_->set_fullscreen_transition_state(FullScreenTransitionState::NONE);
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->SetResizable(is_resizable_);
|
|
|
|
shell_->NotifyWindowLeaveFullScreen();
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2022-05-31 08:43:42 +00:00
|
|
|
if (shell_->HandleDeferredClose())
|
|
|
|
return;
|
|
|
|
|
2021-04-21 14:56:25 +00:00
|
|
|
shell_->HandlePendingFullscreenTransitions();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillClose:(NSNotification*)notification {
|
2020-09-23 01:53:32 +00:00
|
|
|
shell_->Cleanup();
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowClosed();
|
|
|
|
|
2020-03-30 16:48:20 +00:00
|
|
|
// Something called -[NSWindow close] on a sheet rather than calling
|
|
|
|
// -[NSWindow endSheet:] on its parent. If the modal session is not ended
|
|
|
|
// then the parent will never be able to show another sheet. But calling
|
|
|
|
// -endSheet: here will block the thread with an animation, so post a task.
|
|
|
|
if (shell_->is_modal() && shell_->parent() && shell_->IsVisible()) {
|
|
|
|
NSWindow* window = shell_->GetNativeWindow().GetNativeNSWindow();
|
|
|
|
NSWindow* sheetParent = [window sheetParent];
|
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(base::RetainBlock(^{
|
|
|
|
[sheetParent endSheet:window];
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
// Clears the delegate when window is going to be closed, since EL Capitan it
|
|
|
|
// is possible that the methods of delegate would get called after the window
|
|
|
|
// has been closed.
|
2019-06-05 00:27:07 +00:00
|
|
|
auto* bridge_host = views::NativeWidgetMacNSWindowHost::GetFromNativeWindow(
|
2019-01-12 01:00:43 +00:00
|
|
|
shell_->GetNativeWindow());
|
2019-07-03 01:22:09 +00:00
|
|
|
auto* bridged_view = bridge_host->GetInProcessNSWindowBridge();
|
2018-10-23 14:35:00 +00:00
|
|
|
bridged_view->OnWindowWillClose();
|
2018-04-19 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(id)window {
|
|
|
|
shell_->NotifyWindowCloseButtonClicked();
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect)window:(NSWindow*)window
|
2018-04-20 18:47:04 +00:00
|
|
|
willPositionSheet:(NSWindow*)sheet
|
|
|
|
usingRect:(NSRect)rect {
|
2018-04-19 07:01:19 +00:00
|
|
|
NSView* view = window.contentView;
|
|
|
|
|
|
|
|
rect.origin.x = shell_->GetSheetOffsetX();
|
|
|
|
rect.origin.y = view.frame.size.height - shell_->GetSheetOffsetY();
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)windowWillBeginSheet:(NSNotification*)notification {
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowSheetBegin();
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)windowDidEndSheet:(NSNotification*)notification {
|
2018-04-19 07:01:19 +00:00
|
|
|
shell_->NotifyWindowSheetEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)newWindowForTab:(id)sender {
|
|
|
|
shell_->NotifyNewWindowForTab();
|
|
|
|
electron::Browser::Get()->NewWindowForTab();
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:18:50 +00:00
|
|
|
#pragma mark - NSTouchBarDelegate
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
|
2022-06-02 18:43:40 +00:00
|
|
|
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
2018-04-19 07:41:36 +00:00
|
|
|
if (touchBar && shell_->touch_bar())
|
|
|
|
return [shell_->touch_bar() makeItemForIdentifier:identifier];
|
2018-04-19 07:18:50 +00:00
|
|
|
else
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:41:36 +00:00
|
|
|
#pragma mark - QLPreviewPanelDataSource
|
|
|
|
|
|
|
|
- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel*)panel {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (id<QLPreviewItem>)previewPanel:(QLPreviewPanel*)panel
|
|
|
|
previewItemAtIndex:(NSInteger)index {
|
2018-04-19 07:41:36 +00:00
|
|
|
return shell_->preview_item();
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:01:19 +00:00
|
|
|
@end
|