2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-12 07:04:46 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_
|
2013-04-12 07:04:46 +00:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2014-12-18 23:40:35 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-08-08 07:33:16 +00:00
|
|
|
#include "base/mac/scoped_nsobject.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
2022-10-12 16:05:45 +00:00
|
|
|
#include "shell/common/api/api.mojom.h"
|
2021-03-16 09:41:59 +00:00
|
|
|
#include "ui/display/display_observer.h"
|
2020-03-05 20:19:05 +00:00
|
|
|
#include "ui/native_theme/native_theme_observer.h"
|
2018-05-02 06:39:43 +00:00
|
|
|
#include "ui/views/controls/native/native_view_host.h"
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
@class ElectronNSWindow;
|
|
|
|
@class ElectronNSWindowDelegate;
|
|
|
|
@class ElectronPreviewItem;
|
|
|
|
@class ElectronTouchBar;
|
2021-08-04 00:31:12 +00:00
|
|
|
@class WindowButtonsProxy;
|
2013-09-05 15:52:29 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2018-05-02 06:39:43 +00:00
|
|
|
class RootViewMac;
|
|
|
|
|
2021-03-16 09:41:59 +00:00
|
|
|
class NativeWindowMac : public NativeWindow,
|
|
|
|
public ui::NativeThemeObserver,
|
|
|
|
public display::DisplayObserver {
|
2013-04-12 07:04:46 +00:00
|
|
|
public:
|
2019-10-25 13:03:28 +00:00
|
|
|
NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent);
|
2015-06-25 01:47:57 +00:00
|
|
|
~NativeWindowMac() override;
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2015-06-25 03:07:23 +00:00
|
|
|
// NativeWindow:
|
2018-05-08 03:51:27 +00:00
|
|
|
void SetContentView(views::View* view) override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void Close() override;
|
|
|
|
void CloseImmediately() override;
|
|
|
|
void Focus(bool focus) override;
|
|
|
|
bool IsFocused() override;
|
|
|
|
void Show() override;
|
|
|
|
void ShowInactive() override;
|
|
|
|
void Hide() override;
|
|
|
|
bool IsVisible() override;
|
2016-06-17 08:38:44 +00:00
|
|
|
bool IsEnabled() override;
|
2018-02-06 14:16:22 +00:00
|
|
|
void SetEnabled(bool enable) override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void Maximize() override;
|
|
|
|
void Unmaximize() override;
|
|
|
|
bool IsMaximized() override;
|
|
|
|
void Minimize() override;
|
|
|
|
void Restore() override;
|
|
|
|
bool IsMinimized() override;
|
|
|
|
void SetFullScreen(bool fullscreen) override;
|
2015-04-21 13:35:36 +00:00
|
|
|
bool IsFullscreen() const override;
|
2016-01-15 16:31:31 +00:00
|
|
|
void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
|
2015-05-01 14:40:46 +00:00
|
|
|
gfx::Rect GetBounds() override;
|
2018-08-24 21:33:27 +00:00
|
|
|
bool IsNormal() override;
|
|
|
|
gfx::Rect GetNormalBounds() override;
|
2015-10-05 08:19:01 +00:00
|
|
|
void SetContentSizeConstraints(
|
|
|
|
const extensions::SizeConstraints& size_constraints) override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void SetResizable(bool resizable) override;
|
2019-08-15 06:51:15 +00:00
|
|
|
bool MoveAbove(const std::string& sourceId) override;
|
2018-04-03 13:04:32 +00:00
|
|
|
void MoveTop() override;
|
2014-12-23 23:31:39 +00:00
|
|
|
bool IsResizable() override;
|
2016-01-18 22:46:35 +00:00
|
|
|
void SetMovable(bool movable) override;
|
|
|
|
bool IsMovable() override;
|
|
|
|
void SetMinimizable(bool minimizable) override;
|
|
|
|
bool IsMinimizable() override;
|
2016-01-22 21:24:33 +00:00
|
|
|
void SetMaximizable(bool maximizable) override;
|
|
|
|
bool IsMaximizable() override;
|
2016-01-23 07:47:37 +00:00
|
|
|
void SetFullScreenable(bool fullscreenable) override;
|
|
|
|
bool IsFullScreenable() override;
|
2016-01-18 22:46:35 +00:00
|
|
|
void SetClosable(bool closable) override;
|
|
|
|
bool IsClosable() override;
|
2019-07-24 22:58:51 +00:00
|
|
|
void SetAlwaysOnTop(ui::ZOrderLevel z_order,
|
2018-04-18 01:44:10 +00:00
|
|
|
const std::string& level,
|
2021-06-01 01:46:25 +00:00
|
|
|
int relative_level) override;
|
2021-06-23 06:09:09 +00:00
|
|
|
std::string GetAlwaysOnTopLevel() override;
|
2019-07-24 22:58:51 +00:00
|
|
|
ui::ZOrderLevel GetZOrderLevel() override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void Center() override;
|
2017-02-14 03:41:24 +00:00
|
|
|
void Invalidate() override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void SetTitle(const std::string& title) override;
|
|
|
|
std::string GetTitle() override;
|
|
|
|
void FlashFrame(bool flash) override;
|
|
|
|
void SetSkipTaskbar(bool skip) override;
|
2019-03-27 12:10:23 +00:00
|
|
|
void SetExcludedFromShownWindowsMenu(bool excluded) override;
|
|
|
|
bool IsExcludedFromShownWindowsMenu() override;
|
2017-08-13 06:28:33 +00:00
|
|
|
void SetSimpleFullScreen(bool simple_fullscreen) override;
|
|
|
|
bool IsSimpleFullScreen() override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void SetKiosk(bool kiosk) override;
|
|
|
|
bool IsKiosk() override;
|
2018-03-06 04:21:47 +00:00
|
|
|
void SetBackgroundColor(SkColor color) override;
|
2019-12-13 16:08:30 +00:00
|
|
|
SkColor GetBackgroundColor() override;
|
2022-12-01 18:24:44 +00:00
|
|
|
void InvalidateShadow() override;
|
2016-01-23 00:15:49 +00:00
|
|
|
void SetHasShadow(bool has_shadow) override;
|
|
|
|
bool HasShadow() override;
|
2017-09-29 02:26:02 +00:00
|
|
|
void SetOpacity(const double opacity) override;
|
2017-10-02 15:08:10 +00:00
|
|
|
double GetOpacity() override;
|
2014-12-23 23:31:39 +00:00
|
|
|
void SetRepresentedFilename(const std::string& filename) override;
|
|
|
|
std::string GetRepresentedFilename() override;
|
|
|
|
void SetDocumentEdited(bool edited) override;
|
|
|
|
bool IsDocumentEdited() override;
|
2018-04-09 10:35:05 +00:00
|
|
|
void SetIgnoreMouseEvents(bool ignore, bool forward) override;
|
2022-11-01 20:43:42 +00:00
|
|
|
bool IsHiddenInMissionControl() override;
|
|
|
|
void SetHiddenInMissionControl(bool hidden) override;
|
2016-06-22 08:40:01 +00:00
|
|
|
void SetContentProtection(bool enable) override;
|
2019-07-08 02:34:18 +00:00
|
|
|
void SetFocusable(bool focusable) override;
|
2021-04-21 10:32:19 +00:00
|
|
|
bool IsFocusable() override;
|
2018-12-22 01:49:26 +00:00
|
|
|
void AddBrowserView(NativeBrowserView* browser_view) override;
|
|
|
|
void RemoveBrowserView(NativeBrowserView* browser_view) override;
|
2021-02-10 07:23:35 +00:00
|
|
|
void SetTopBrowserView(NativeBrowserView* browser_view) override;
|
2016-06-17 06:28:43 +00:00
|
|
|
void SetParentWindow(NativeWindow* parent) override;
|
2019-08-15 06:51:15 +00:00
|
|
|
content::DesktopMediaID GetDesktopMediaID() const override;
|
2017-05-23 09:41:59 +00:00
|
|
|
gfx::NativeView GetNativeView() const override;
|
|
|
|
gfx::NativeWindow GetNativeWindow() const override;
|
|
|
|
gfx::AcceleratedWidget GetAcceleratedWidget() const override;
|
2018-12-11 17:45:55 +00:00
|
|
|
NativeWindowHandle GetNativeWindowHandle() const override;
|
2016-08-09 23:05:44 +00:00
|
|
|
void SetProgressBar(double progress, const ProgressState state) override;
|
2015-02-07 19:56:03 +00:00
|
|
|
void SetOverlayIcon(const gfx::Image& overlay,
|
2015-02-11 01:14:26 +00:00
|
|
|
const std::string& description) override;
|
2020-08-19 20:31:25 +00:00
|
|
|
void SetVisibleOnAllWorkspaces(bool visible,
|
2021-02-02 12:24:04 +00:00
|
|
|
bool visibleOnFullScreen,
|
|
|
|
bool skipTransformProcessType) override;
|
2015-03-26 06:18:37 +00:00
|
|
|
bool IsVisibleOnAllWorkspaces() override;
|
2016-11-29 21:36:08 +00:00
|
|
|
void SetAutoHideCursor(bool auto_hide) override;
|
2020-12-18 00:50:51 +00:00
|
|
|
void SetVibrancy(const std::string& type) override;
|
2021-01-19 10:12:09 +00:00
|
|
|
void SetWindowButtonVisibility(bool visible) override;
|
|
|
|
bool GetWindowButtonVisibility() const override;
|
2023-02-17 10:06:32 +00:00
|
|
|
void SetWindowButtonPosition(absl::optional<gfx::Point> position) override;
|
|
|
|
absl::optional<gfx::Point> GetWindowButtonPosition() const override;
|
2020-12-18 00:50:51 +00:00
|
|
|
void RedrawTrafficLights() override;
|
2021-03-16 09:41:59 +00:00
|
|
|
void UpdateFrame() override;
|
2020-12-18 00:50:51 +00:00
|
|
|
void SetTouchBar(
|
|
|
|
std::vector<gin_helper::PersistentDictionary> items) override;
|
|
|
|
void RefreshTouchBarItem(const std::string& item_id) override;
|
|
|
|
void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) override;
|
2017-08-21 04:46:10 +00:00
|
|
|
void SelectPreviousTab() override;
|
|
|
|
void SelectNextTab() override;
|
|
|
|
void MergeAllWindows() override;
|
|
|
|
void MoveTabToNewWindow() override;
|
|
|
|
void ToggleTabBar() override;
|
2018-02-27 21:00:42 +00:00
|
|
|
bool AddTabbedWindow(NativeWindow* window) override;
|
2020-12-18 00:50:51 +00:00
|
|
|
void SetAspectRatio(double aspect_ratio,
|
|
|
|
const gfx::Size& extra_size) override;
|
|
|
|
void PreviewFile(const std::string& path,
|
|
|
|
const std::string& display_name) override;
|
|
|
|
void CloseFilePreview() override;
|
2018-04-17 23:03:51 +00:00
|
|
|
gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
|
|
|
|
gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
|
2021-07-01 19:25:40 +00:00
|
|
|
gfx::Rect GetWindowControlsOverlayRect() override;
|
2021-01-30 23:15:10 +00:00
|
|
|
void NotifyWindowEnterFullScreen() override;
|
|
|
|
void NotifyWindowLeaveFullScreen() override;
|
2021-05-19 09:27:35 +00:00
|
|
|
void SetActive(bool is_key) override;
|
|
|
|
bool IsActive() const override;
|
2023-05-31 09:57:44 +00:00
|
|
|
// Remove the specified child window without closing it.
|
|
|
|
void RemoveChildWindow(NativeWindow* child) override;
|
2023-06-08 10:18:37 +00:00
|
|
|
void RemoveChildFromParentWindow(NativeWindow* child);
|
2023-05-31 09:57:44 +00:00
|
|
|
// Attach child windows, if the window is visible.
|
|
|
|
void AttachChildren() override;
|
|
|
|
// Detach window from parent without destroying it.
|
|
|
|
void DetachChildren() override;
|
2021-01-30 23:15:10 +00:00
|
|
|
|
|
|
|
void NotifyWindowWillEnterFullScreen();
|
|
|
|
void NotifyWindowWillLeaveFullScreen();
|
2018-02-22 06:09:38 +00:00
|
|
|
|
2020-12-18 00:50:51 +00:00
|
|
|
// Cleanup observers when window is getting closed. Note that the destructor
|
|
|
|
// can be called much later after window gets closed, so we should not do
|
|
|
|
// cleanup in destructor.
|
|
|
|
void Cleanup();
|
|
|
|
|
2021-05-11 07:45:21 +00:00
|
|
|
void UpdateVibrancyRadii(bool fullscreen);
|
|
|
|
|
2022-06-09 08:48:50 +00:00
|
|
|
void UpdateWindowOriginalFrame();
|
|
|
|
|
2016-05-17 07:19:28 +00:00
|
|
|
// Set the attribute of NSWindow while work around a bug of zoom button.
|
2022-06-03 07:47:19 +00:00
|
|
|
bool HasStyleMask(NSUInteger flag) const;
|
2016-05-17 07:19:28 +00:00
|
|
|
void SetStyleMask(bool on, NSUInteger flag);
|
|
|
|
void SetCollectionBehavior(bool on, NSUInteger flag);
|
2019-10-18 19:57:34 +00:00
|
|
|
void SetWindowLevel(int level);
|
2016-05-17 07:19:28 +00:00
|
|
|
|
2022-05-31 08:43:42 +00:00
|
|
|
bool HandleDeferredClose();
|
|
|
|
void SetHasDeferredWindowClose(bool defer_close) {
|
|
|
|
has_deferred_window_close_ = defer_close;
|
|
|
|
}
|
2021-04-21 14:56:25 +00:00
|
|
|
|
2020-08-24 15:44:48 +00:00
|
|
|
enum class VisualEffectState {
|
2020-10-27 17:51:45 +00:00
|
|
|
kFollowWindow,
|
|
|
|
kActive,
|
|
|
|
kInactive,
|
2020-08-24 15:44:48 +00:00
|
|
|
};
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronPreviewItem* preview_item() const { return preview_item_.get(); }
|
|
|
|
ElectronTouchBar* touch_bar() const { return touch_bar_.get(); }
|
2016-10-27 18:29:51 +00:00
|
|
|
bool zoom_to_page_width() const { return zoom_to_page_width_; }
|
2019-09-12 17:38:16 +00:00
|
|
|
bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
|
2015-10-20 23:33:43 +00:00
|
|
|
|
2021-09-21 10:04:32 +00:00
|
|
|
// We need to save the result of windowWillUseStandardFrame:defaultFrame
|
|
|
|
// because macOS calls it with what it refers to as the "best fit" frame for a
|
|
|
|
// zoom. This means that even if an aspect ratio is set, macOS might adjust it
|
|
|
|
// to better fit the screen.
|
|
|
|
//
|
|
|
|
// Thus, we can't just calculate the maximized aspect ratio'd sizing from
|
|
|
|
// the current visible screen and compare that to the current window's frame
|
|
|
|
// to determine whether a window is maximized.
|
|
|
|
NSRect default_frame_for_zoom() const { return default_frame_for_zoom_; }
|
|
|
|
void set_default_frame_for_zoom(NSRect frame) {
|
|
|
|
default_frame_for_zoom_ = frame;
|
|
|
|
}
|
|
|
|
|
2018-04-24 08:23:08 +00:00
|
|
|
protected:
|
|
|
|
// views::WidgetDelegate:
|
2018-05-02 06:39:43 +00:00
|
|
|
views::View* GetContentsView() override;
|
2021-09-17 13:54:15 +00:00
|
|
|
bool CanMaximize() const override;
|
2022-10-12 16:05:45 +00:00
|
|
|
std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameView(
|
|
|
|
views::Widget* widget) override;
|
2018-04-24 08:23:08 +00:00
|
|
|
|
2020-12-18 00:50:51 +00:00
|
|
|
// ui::NativeThemeObserver:
|
|
|
|
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
|
|
|
|
|
2021-03-16 09:41:59 +00:00
|
|
|
// display::DisplayObserver:
|
|
|
|
void OnDisplayMetricsChanged(const display::Display& display,
|
|
|
|
uint32_t changed_metrics) override;
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
private:
|
2018-07-10 01:43:42 +00:00
|
|
|
// Add custom layers to the content view.
|
2021-01-30 23:15:10 +00:00
|
|
|
void AddContentViewLayers();
|
2018-07-10 01:43:42 +00:00
|
|
|
|
2021-01-19 10:12:09 +00:00
|
|
|
void InternalSetWindowButtonVisibility(bool visible);
|
2017-05-01 20:08:27 +00:00
|
|
|
void InternalSetParentWindow(NativeWindow* parent, bool attach);
|
2018-04-09 10:35:05 +00:00
|
|
|
void SetForwardMouseMessages(bool forward);
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronNSWindow* window_; // Weak ref, managed by widget_.
|
2018-04-24 08:23:08 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
base::scoped_nsobject<ElectronNSWindowDelegate> window_delegate_;
|
|
|
|
base::scoped_nsobject<ElectronPreviewItem> preview_item_;
|
|
|
|
base::scoped_nsobject<ElectronTouchBar> touch_bar_;
|
2016-01-25 07:02:43 +00:00
|
|
|
|
2019-08-22 21:16:56 +00:00
|
|
|
// The views::View that fills the client area.
|
2018-05-02 06:39:43 +00:00
|
|
|
std::unique_ptr<RootViewMac> root_view_;
|
|
|
|
|
2023-05-09 16:28:37 +00:00
|
|
|
bool fullscreen_before_kiosk_ = false;
|
2018-05-21 22:18:38 +00:00
|
|
|
bool is_kiosk_ = false;
|
|
|
|
bool zoom_to_page_width_ = false;
|
2021-06-03 08:05:04 +00:00
|
|
|
absl::optional<gfx::Point> traffic_light_position_;
|
2017-06-18 00:45:29 +00:00
|
|
|
|
2022-05-31 08:43:42 +00:00
|
|
|
// Trying to close an NSWindow during a fullscreen transition will cause the
|
|
|
|
// window to lock up. Use this to track if CloseWindow was called during a
|
|
|
|
// fullscreen transition, to defer the -[NSWindow close] call until the
|
|
|
|
// transition is complete.
|
|
|
|
bool has_deferred_window_close_ = false;
|
|
|
|
|
2018-05-21 22:18:38 +00:00
|
|
|
NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2014-06-09 05:04:59 +00:00
|
|
|
// The presentation options before entering kiosk mode.
|
|
|
|
NSApplicationPresentationOptions kiosk_options_;
|
|
|
|
|
2020-08-24 15:44:48 +00:00
|
|
|
// The "visualEffectState" option.
|
2020-10-27 17:51:45 +00:00
|
|
|
VisualEffectState visual_effect_state_ = VisualEffectState::kFollowWindow;
|
2020-08-24 15:44:48 +00:00
|
|
|
|
2018-07-03 08:20:56 +00:00
|
|
|
// The visibility mode of window button controls when explicitly set through
|
|
|
|
// setWindowButtonVisibility().
|
2021-06-03 08:05:04 +00:00
|
|
|
absl::optional<bool> window_button_visibility_;
|
2018-07-03 08:20:56 +00:00
|
|
|
|
2021-08-04 00:31:12 +00:00
|
|
|
// Controls the position and visibility of window buttons.
|
|
|
|
base::scoped_nsobject<WindowButtonsProxy> buttons_proxy_;
|
|
|
|
|
2022-10-12 16:05:45 +00:00
|
|
|
std::unique_ptr<SkRegion> draggable_region_;
|
|
|
|
|
2020-03-17 17:58:57 +00:00
|
|
|
// Maximizable window state; necessary for persistence through redraws.
|
|
|
|
bool maximizable_ = true;
|
|
|
|
|
2022-03-30 22:29:02 +00:00
|
|
|
bool user_set_bounds_maximized_ = false;
|
|
|
|
|
2017-08-13 06:28:33 +00:00
|
|
|
// Simple (pre-Lion) Fullscreen Settings
|
2018-05-21 22:18:38 +00:00
|
|
|
bool always_simple_fullscreen_ = false;
|
|
|
|
bool is_simple_fullscreen_ = false;
|
|
|
|
bool was_maximizable_ = false;
|
|
|
|
bool was_movable_ = false;
|
2021-05-19 09:27:35 +00:00
|
|
|
bool is_active_ = false;
|
2017-08-13 06:28:33 +00:00
|
|
|
NSRect original_frame_;
|
2018-10-10 05:38:52 +00:00
|
|
|
NSInteger original_level_;
|
2017-09-12 18:27:30 +00:00
|
|
|
NSUInteger simple_fullscreen_mask_;
|
2021-09-21 10:04:32 +00:00
|
|
|
NSRect default_frame_for_zoom_;
|
2017-08-13 06:28:33 +00:00
|
|
|
|
2021-05-11 07:45:21 +00:00
|
|
|
std::string vibrancy_type_;
|
2018-02-12 18:38:37 +00:00
|
|
|
|
2017-08-13 06:28:33 +00:00
|
|
|
// The presentation options before entering simple fullscreen mode.
|
|
|
|
NSApplicationPresentationOptions simple_fullscreen_options_;
|
2013-04-12 07:04:46 +00:00
|
|
|
};
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_
|