mirror of
https://github.com/electron/electron.git
synced 2026-02-19 03:14:51 -05:00
Compare commits
5 Commits
trash-erro
...
gsoc-2025
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
444448507d | ||
|
|
04d4505004 | ||
|
|
aaf813a0f6 | ||
|
|
a6093b1575 | ||
|
|
96c28c3325 |
@@ -387,6 +387,14 @@ Returns `BaseWindow | null` - The window that is focused in this application, ot
|
||||
|
||||
Returns `BaseWindow | null` - The window with the given `id`.
|
||||
|
||||
#### `BaseWindow.clearWindowState(windowName)`
|
||||
|
||||
* `windowName` string - The window `name` to clear state for (see [BaseWindowConstructorOptions](structures/base-window-options.md)).
|
||||
|
||||
Clears the saved state for a window with the given name. This removes all persisted window bounds, display mode, and work area information that was previously saved when `windowStatePersistence` was enabled.
|
||||
|
||||
If the window name is empty or the window state doesn't exist, the method will log a warning.
|
||||
|
||||
### Instance Properties
|
||||
|
||||
Objects created with `new BaseWindow` have the following properties:
|
||||
|
||||
@@ -42,6 +42,8 @@
|
||||
Default is `false`.
|
||||
* `hiddenInMissionControl` boolean (optional) _macOS_ - Whether window should be hidden when the user toggles into mission control.
|
||||
* `kiosk` boolean (optional) - Whether the window is in kiosk mode. Default is `false`.
|
||||
* `name` string (optional) - A unique identifier for the window, used to enable features such as state persistence. Each window must have a distinct name. It can only be reused after the corresponding window has been destroyed.
|
||||
* `windowStatePersistence` ([WindowStatePersistence](window-state-persistence.md) | boolean) (optional) - Configures or enables the persistence of window state (position, size, maximized state, etc.) across application restarts. Has no effect if window `name` is not provided. Automatically disabled when there is no available display. _Experimental_
|
||||
* `title` string (optional) - Default window title. Default is `"Electron"`. If the HTML tag `<title>` is defined in the HTML file loaded by `loadURL()`, this property will be ignored.
|
||||
* `icon` ([NativeImage](../native-image.md) | string) (optional) - The window icon. On Windows it is
|
||||
recommended to use `ICO` icons to get best visual effects, you can also
|
||||
@@ -91,7 +93,7 @@
|
||||
title bar and a full size content window, the traffic light buttons will
|
||||
display when being hovered over in the top left of the window.
|
||||
**Note:** This option is currently experimental.
|
||||
* `titleBarOverlay` Object | Boolean (optional) - When using a frameless window in conjunction with `win.setWindowButtonVisibility(true)` on macOS or using a `titleBarStyle` so that the standard window controls ("traffic lights" on macOS) are visible, this property enables the Window Controls Overlay [JavaScript APIs][overlay-javascript-apis] and [CSS Environment Variables][overlay-css-env-vars]. Specifying `true` will result in an overlay with default system colors. Default is `false`.
|
||||
* `titleBarOverlay` Object | boolean (optional) - When using a frameless window in conjunction with `win.setWindowButtonVisibility(true)` on macOS or using a `titleBarStyle` so that the standard window controls ("traffic lights" on macOS) are visible, this property enables the Window Controls Overlay [JavaScript APIs][overlay-javascript-apis] and [CSS Environment Variables][overlay-css-env-vars]. Specifying `true` will result in an overlay with default system colors. Default is `false`.
|
||||
* `color` String (optional) _Windows_ _Linux_ - The CSS color of the Window Controls Overlay when enabled. Default is the system color.
|
||||
* `symbolColor` String (optional) _Windows_ _Linux_ - The CSS color of the symbols on the Window Controls Overlay when enabled. Default is the system color.
|
||||
* `height` Integer (optional) - The height of the title bar and Window Controls Overlay in pixels. Default is system height.
|
||||
|
||||
4
docs/api/structures/window-state-persistence.md
Normal file
4
docs/api/structures/window-state-persistence.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# WindowStatePersistence Object
|
||||
|
||||
* `bounds` boolean (optional) - Whether to persist window position and size across application restarts. Defaults to `true` if not specified.
|
||||
* `displayMode` boolean (optional) - Whether to persist display modes (fullscreen, kiosk, maximized, etc.) across application restarts. Defaults to `true` if not specified.
|
||||
94
docs/development/multi-monitor-testing.md
Normal file
94
docs/development/multi-monitor-testing.md
Normal file
@@ -0,0 +1,94 @@
|
||||
# Multi-Monitor Testing
|
||||
|
||||
The `virtualDisplay` addon leverages macOS CoreGraphics APIs to create virtual displays, allowing you to write and run multi-monitor tests without the need for physical monitors.
|
||||
|
||||
## Methods
|
||||
|
||||
#### `virtualDisplay.create([options])`
|
||||
|
||||
Creates a virtual display and returns a display ID.
|
||||
|
||||
```js @ts-nocheck
|
||||
const virtualDisplay = require('@electron-ci/virtual-display')
|
||||
// Default: 1920×1080 at origin (0, 0)
|
||||
const displayId = virtualDisplay.create()
|
||||
```
|
||||
|
||||
```js @ts-nocheck
|
||||
const virtualDisplay = require('@electron-ci/virtual-display')
|
||||
// Custom options (all parameters optional and have default values)
|
||||
const displayId = virtualDisplay.create({
|
||||
width: 2560, // Display width in pixels
|
||||
height: 1440, // Display height in pixels
|
||||
x: 1920, // X position (top-left corner)
|
||||
y: 0 // Y position (top-left corner)
|
||||
})
|
||||
```
|
||||
|
||||
**Returns:** `number` - Unique display ID used to identify the display. Returns `0` on failure to create display.
|
||||
|
||||
#### `virtualDisplay.destroy(displayId)`
|
||||
|
||||
Removes the virtual display.
|
||||
|
||||
```js @ts-nocheck
|
||||
const success = virtualDisplay.destroy(displayId)
|
||||
```
|
||||
|
||||
**Returns:** `boolean` - Success status
|
||||
|
||||
#### `virtualDisplay.forceCleanup()`
|
||||
|
||||
Performs a complete cleanup of all virtual displays and resets the macOS CoreGraphics display system.
|
||||
|
||||
It is recommended to call this before every test to prevent test failures. macOS CoreGraphics maintains an internal display ID allocation pool that can become corrupted when virtual displays are created and destroyed rapidly during testing. Without proper cleanup, subsequent display creation may fail with inconsistent display IDs, resulting in test flakiness.
|
||||
|
||||
```js @ts-nocheck
|
||||
// Recommended test pattern
|
||||
beforeEach(() => {
|
||||
virtualDisplay.forceCleanup()
|
||||
})
|
||||
```
|
||||
|
||||
**Returns:** `boolean` - Success status
|
||||
|
||||
## Display Constraints
|
||||
|
||||
### Size Limits
|
||||
|
||||
Virtual displays are constrained to 720×720 pixels minimum and 8192×8192 pixels maximum. Actual limits may vary depending on your Mac's graphics capabilities, so sizes outside this range (like 9000×6000) may fail on some systems.
|
||||
|
||||
```js @ts-nocheck
|
||||
// Safe sizes for testing
|
||||
virtualDisplay.create({ width: 1920, height: 1080 }) // Full HD
|
||||
virtualDisplay.create({ width: 3840, height: 2160 }) // 4K
|
||||
```
|
||||
|
||||
### Positioning Behavior
|
||||
|
||||
macOS maintains a contiguous desktop space by automatically adjusting display positions if there are any overlaps or gaps. In case of either, the placement of the new origin is as close as possible to the requested location, without overlapping or leaving a gap between displays.
|
||||
|
||||
**Overlap:**
|
||||
|
||||
```js @ts-nocheck
|
||||
// Requested positions
|
||||
const display1 = virtualDisplay.create({ x: 0, y: 0, width: 1920, height: 1080 })
|
||||
const display2 = virtualDisplay.create({ x: 500, y: 0, width: 1920, height: 1080 })
|
||||
|
||||
// macOS automatically repositions display2 to x: 1920 to prevent overlap
|
||||
const actualBounds = screen.getAllDisplays().map(d => d.bounds)
|
||||
// Result: [{ x: 0, y: 0, width: 1920, height: 1080 },
|
||||
// { x: 1920, y: 0, width: 1920, height: 1080 }]
|
||||
```
|
||||
|
||||
**Gap:**
|
||||
|
||||
```js @ts-nocheck
|
||||
// Requested: gap between displays
|
||||
const display1 = virtualDisplay.create({ width: 1920, height: 1080, x: 0, y: 0 })
|
||||
const display2 = virtualDisplay.create({ width: 1920, height: 1080, x: 2000, y: 0 })
|
||||
// macOS snaps display2 to x: 1920 (eliminates 80px gap)
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> Always verify actual positions with `screen.getAllDisplays()` after creation, as macOS may adjust coordinates from the set values.
|
||||
@@ -95,3 +95,11 @@ To configure display scaling:
|
||||
|
||||
1. Push the Windows key and search for _Display settings_.
|
||||
2. Under _Scale and layout_, make sure that the device is set to 100%.
|
||||
|
||||
## Multi-Monitor Tests
|
||||
|
||||
Some Electron APIs require testing across multiple displays, such as screen detection, window positioning, and display-related events. For contributors working on these features, the `virtualDisplay` native addon enables you to create and position virtual displays programmatically, making it possible to test multi-monitor scenarios without any physical hardware.
|
||||
|
||||
For detailed information on using virtual displays in your tests, see [Multi-Monitor Testing](multi-monitor-testing.md).
|
||||
|
||||
**Platform support:** macOS only
|
||||
|
||||
@@ -164,6 +164,7 @@ auto_filenames = {
|
||||
"docs/api/structures/web-source.md",
|
||||
"docs/api/structures/window-open-handler-response.md",
|
||||
"docs/api/structures/window-session-end-event.md",
|
||||
"docs/api/structures/window-state-persistence.md",
|
||||
]
|
||||
|
||||
sandbox_bundle_deps = [
|
||||
|
||||
@@ -111,6 +111,8 @@ BrowserWindow.getAllWindows = () => {
|
||||
return BaseWindow.getAllWindows().filter(isBrowserWindow) as any[] as BWT[];
|
||||
};
|
||||
|
||||
BrowserWindow.clearWindowState = BaseWindow.clearWindowState;
|
||||
|
||||
BrowserWindow.getFocusedWindow = () => {
|
||||
for (const window of BrowserWindow.getAllWindows()) {
|
||||
if (!window.isDestroyed() && window.webContents && !window.webContents.isDestroyed()) {
|
||||
|
||||
@@ -10,15 +10,20 @@
|
||||
#include <vector>
|
||||
|
||||
#include "base/task/single_thread_task_runner.h"
|
||||
#include "components/prefs/scoped_user_pref_update.h"
|
||||
#include "content/public/common/color_parser.h"
|
||||
#include "electron/buildflags/buildflags.h"
|
||||
#include "gin/dictionary.h"
|
||||
#include "shell/browser/api/electron_api_menu.h"
|
||||
#include "shell/browser/api/electron_api_view.h"
|
||||
#include "shell/browser/api/electron_api_web_contents.h"
|
||||
#include "shell/browser/browser_process_impl.h"
|
||||
#include "shell/browser/electron_browser_main_parts.h"
|
||||
#include "shell/browser/javascript_environment.h"
|
||||
#include "shell/browser/native_window.h"
|
||||
#include "shell/browser/window_list.h"
|
||||
#include "shell/common/color_util.h"
|
||||
#include "shell/common/electron_constants.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_converters/gfx_converter.h"
|
||||
@@ -178,7 +183,7 @@ void BaseWindow::OnWindowClosed() {
|
||||
// We can not call Destroy here because we need to call Emit first, but we
|
||||
// also do not want any method to be used, so just mark as destroyed here.
|
||||
MarkDestroyed();
|
||||
|
||||
window_->FlushWindowState();
|
||||
Emit("closed");
|
||||
|
||||
RemoveFromParentChildWindows();
|
||||
@@ -267,6 +272,7 @@ void BaseWindow::OnWindowWillResize(const gfx::Rect& new_bounds,
|
||||
}
|
||||
|
||||
void BaseWindow::OnWindowResize() {
|
||||
window_->DebouncedSaveWindowState();
|
||||
Emit("resize");
|
||||
}
|
||||
|
||||
@@ -282,6 +288,7 @@ void BaseWindow::OnWindowWillMove(const gfx::Rect& new_bounds,
|
||||
}
|
||||
|
||||
void BaseWindow::OnWindowMove() {
|
||||
window_->DebouncedSaveWindowState();
|
||||
Emit("move");
|
||||
}
|
||||
|
||||
@@ -1141,14 +1148,64 @@ void BaseWindow::RemoveFromParentChildWindows() {
|
||||
parent->child_windows_.Remove(weak_map_id());
|
||||
}
|
||||
|
||||
// static
|
||||
void BaseWindow::ClearWindowState(const std::string& window_name) {
|
||||
if (window_name.empty()) {
|
||||
LOG(WARNING) << "Cannot clear window state: window name is empty";
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto* browser_process =
|
||||
electron::ElectronBrowserMainParts::Get()->browser_process()) {
|
||||
DCHECK(browser_process);
|
||||
if (auto* prefs = browser_process->local_state()) {
|
||||
ScopedDictPrefUpdate update(prefs, electron::kWindowStates);
|
||||
|
||||
if (!update->Remove(window_name)) {
|
||||
LOG(WARNING) << "Window state '" << window_name
|
||||
<< "' not found, nothing to clear";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
gin_helper::WrappableBase* BaseWindow::New(gin_helper::Arguments* args) {
|
||||
auto options = gin_helper::Dictionary::CreateEmpty(args->isolate());
|
||||
args->GetNext(&options);
|
||||
|
||||
std::string error_message;
|
||||
if (!IsWindowNameValid(options, &error_message)) {
|
||||
// Window name is already in use throw an error and do not create the window
|
||||
args->ThrowError(error_message);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return new BaseWindow(args, options);
|
||||
}
|
||||
|
||||
// static
|
||||
bool BaseWindow::IsWindowNameValid(const gin_helper::Dictionary& options,
|
||||
std::string* error_message) {
|
||||
std::string window_name;
|
||||
if (options.Get(options::kName, &window_name) && !window_name.empty()) {
|
||||
// Check if window name is already in use by another window
|
||||
// Window names must be unique for state persistence to work correctly
|
||||
const auto& windows = electron::WindowList::GetWindows();
|
||||
bool name_in_use = std::any_of(windows.begin(), windows.end(),
|
||||
[&window_name](const auto* const window) {
|
||||
return window->GetName() == window_name;
|
||||
});
|
||||
|
||||
if (name_in_use) {
|
||||
*error_message = "Window name '" + window_name +
|
||||
"' is already in use. Window names must be unique.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// static
|
||||
void BaseWindow::BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
@@ -1340,6 +1397,7 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||
.ToLocalChecked());
|
||||
constructor.SetMethod("fromId", &BaseWindow::FromWeakMapID);
|
||||
constructor.SetMethod("getAllWindows", &BaseWindow::GetAll);
|
||||
constructor.SetMethod("clearWindowState", &BaseWindow::ClearWindowState);
|
||||
|
||||
gin_helper::Dictionary dict(isolate, exports);
|
||||
dict.Set("BaseWindow", constructor);
|
||||
|
||||
@@ -42,6 +42,13 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype);
|
||||
|
||||
// Clears window state from the Local State JSON file in
|
||||
// app.getPath('userData') via PrefService.
|
||||
static void ClearWindowState(const std::string& window_name);
|
||||
|
||||
static bool IsWindowNameValid(const gin_helper::Dictionary& options,
|
||||
std::string* error_message);
|
||||
|
||||
const NativeWindow* window() const { return window_.get(); }
|
||||
NativeWindow* window() { return window_.get(); }
|
||||
|
||||
|
||||
@@ -307,6 +307,13 @@ gin_helper::WrappableBase* BrowserWindow::New(gin_helper::ErrorThrower thrower,
|
||||
options = gin::Dictionary::CreateEmpty(args->isolate());
|
||||
}
|
||||
|
||||
std::string error_message;
|
||||
if (!IsWindowNameValid(options, &error_message)) {
|
||||
// Window name is already in use throw an error and do not create the window
|
||||
thrower.ThrowError(error_message);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return new BrowserWindow(args, options);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "services/device/public/cpp/geolocation/geolocation_system_permission_manager.h"
|
||||
#include "services/network/public/cpp/network_switches.h"
|
||||
#include "shell/browser/net/resolve_proxy_helper.h"
|
||||
#include "shell/common/electron_constants.h"
|
||||
#include "shell/common/electron_paths.h"
|
||||
#include "shell/common/thread_restrictions.h"
|
||||
|
||||
@@ -106,12 +107,12 @@ void BrowserProcessImpl::PostEarlyInitialization() {
|
||||
OSCrypt::RegisterLocalPrefs(pref_registry.get());
|
||||
#endif
|
||||
|
||||
pref_registry->RegisterDictionaryPref(electron::kWindowStates);
|
||||
|
||||
in_memory_pref_store_ = base::MakeRefCounted<ValueMapPrefStore>();
|
||||
ApplyProxyModeFromCommandLine(in_memory_pref_store());
|
||||
prefs_factory.set_command_line_prefs(in_memory_pref_store());
|
||||
|
||||
// Only use a persistent prefs store when cookie encryption is enabled as that
|
||||
// is the only key that needs it
|
||||
base::FilePath prefs_path;
|
||||
CHECK(base::PathService::Get(electron::DIR_SESSION_DATA, &prefs_path));
|
||||
prefs_path = prefs_path.Append(FILE_PATH_LITERAL("Local State"));
|
||||
|
||||
@@ -10,22 +10,31 @@
|
||||
|
||||
#include "base/containers/contains.h"
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "base/values.h"
|
||||
#include "components/prefs/pref_service.h"
|
||||
#include "components/prefs/scoped_user_pref_update.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#include "include/core/SkColor.h"
|
||||
#include "shell/browser/background_throttling_source.h"
|
||||
#include "shell/browser/browser.h"
|
||||
#include "shell/browser/browser_process_impl.h"
|
||||
#include "shell/browser/draggable_region_provider.h"
|
||||
#include "shell/browser/electron_browser_main_parts.h"
|
||||
#include "shell/browser/native_window_features.h"
|
||||
#include "shell/browser/ui/drag_util.h"
|
||||
#include "shell/browser/window_list.h"
|
||||
#include "shell/common/color_util.h"
|
||||
#include "shell/common/electron_constants.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/persistent_dictionary.h"
|
||||
#include "shell/common/options_switches.h"
|
||||
#include "ui/base/hit_test.h"
|
||||
#include "ui/compositor/compositor.h"
|
||||
#include "ui/display/display.h"
|
||||
#include "ui/display/screen.h"
|
||||
#include "ui/display/types/display_constants.h"
|
||||
#include "ui/views/widget/widget.h"
|
||||
|
||||
#if !BUILDFLAG(IS_MAC)
|
||||
@@ -95,6 +104,12 @@ gfx::Size GetExpandedWindowSize(const NativeWindow* window,
|
||||
}
|
||||
#endif
|
||||
|
||||
// Check if display is fake (default display ID) or has invalid dimensions
|
||||
bool hasInvalidDisplay(const display::Display& display) {
|
||||
return display.id() == display::kDefaultDisplayId ||
|
||||
display.size().width() == 0 || display.size().height() == 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NativeWindow::NativeWindow(const gin_helper::Dictionary& options,
|
||||
@@ -114,6 +129,38 @@ NativeWindow::NativeWindow(const gin_helper::Dictionary& options,
|
||||
options.Get(options::kVibrancyType, &vibrancy_);
|
||||
#endif
|
||||
|
||||
options.Get(options::kName, &window_name_);
|
||||
|
||||
if (gin_helper::Dictionary persistence_options;
|
||||
options.Get(options::kWindowStatePersistence, &persistence_options)) {
|
||||
// Restore bounds by default
|
||||
restore_bounds_ = true;
|
||||
persistence_options.Get(options::kBounds, &restore_bounds_);
|
||||
// Restore display mode by default
|
||||
restore_display_mode_ = true;
|
||||
persistence_options.Get(options::kDisplayMode, &restore_display_mode_);
|
||||
window_state_persistence_enabled_ = true;
|
||||
} else if (bool flag; options.Get(options::kWindowStatePersistence, &flag)) {
|
||||
restore_bounds_ = flag;
|
||||
restore_display_mode_ = flag;
|
||||
window_state_persistence_enabled_ = flag;
|
||||
}
|
||||
|
||||
// Initialize prefs_ to save/restore window bounds if we have a valid window
|
||||
// name and window state persistence is enabled.
|
||||
if (window_state_persistence_enabled_ && !window_name_.empty()) {
|
||||
// Move this out if there's a need to initialize prefs_ for other features
|
||||
if (auto* browser_process =
|
||||
electron::ElectronBrowserMainParts::Get()->browser_process()) {
|
||||
DCHECK(browser_process);
|
||||
prefs_ = browser_process->local_state();
|
||||
}
|
||||
} else if (window_state_persistence_enabled_ && window_name_.empty()) {
|
||||
window_state_persistence_enabled_ = false;
|
||||
LOG(WARNING) << "Window state persistence enabled but no window name "
|
||||
"provided. Window state will not be persisted.";
|
||||
}
|
||||
|
||||
if (gin_helper::Dictionary dict;
|
||||
options.Get(options::ktitleBarOverlay, &dict)) {
|
||||
titlebar_overlay_ = true;
|
||||
@@ -213,7 +260,14 @@ void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
|
||||
options.Get(options::kFullScreenable, &fullscreenable);
|
||||
SetFullScreenable(fullscreenable);
|
||||
|
||||
if (fullscreen)
|
||||
// Restore window state (bounds and display mode) at this point in
|
||||
// initialization. We deliberately restore bounds before display modes
|
||||
// (fullscreen/kiosk) since the target display for these states depends on the
|
||||
// window's initial bounds. Also, restoring here ensures we respect min/max
|
||||
// width/height and fullscreenable constraints.
|
||||
RestoreWindowState(options);
|
||||
|
||||
if (fullscreen && !restore_display_mode_)
|
||||
SetFullScreen(true);
|
||||
|
||||
if (bool val; options.Get(options::kResizable, &val))
|
||||
@@ -222,7 +276,8 @@ void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
|
||||
if (bool val; options.Get(options::kSkipTaskbar, &val))
|
||||
SetSkipTaskbar(val);
|
||||
|
||||
if (bool val; options.Get(options::kKiosk, &val) && val)
|
||||
if (bool val;
|
||||
options.Get(options::kKiosk, &val) && val && !restore_display_mode_)
|
||||
SetKiosk(val);
|
||||
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
@@ -242,7 +297,9 @@ void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
|
||||
SetBackgroundColor(background_color);
|
||||
|
||||
SetTitle(options.ValueOrDefault(options::kTitle, Browser::Get()->GetName()));
|
||||
|
||||
// Save updated window state after restoration adjustments are complete if
|
||||
// any.
|
||||
SaveWindowState();
|
||||
// Then show it.
|
||||
if (options.ValueOrDefault(options::kShow, true))
|
||||
Show();
|
||||
@@ -757,10 +814,14 @@ void NativeWindow::SetAccessibleTitle(const std::string& title) {
|
||||
WidgetDelegate::SetAccessibleTitle(base::UTF8ToUTF16(title));
|
||||
}
|
||||
|
||||
std::string NativeWindow::GetAccessibleTitle() {
|
||||
std::string NativeWindow::GetAccessibleTitle() const {
|
||||
return base::UTF16ToUTF8(GetAccessibleWindowTitle());
|
||||
}
|
||||
|
||||
std::string NativeWindow::GetName() const {
|
||||
return window_name_;
|
||||
}
|
||||
|
||||
void NativeWindow::HandlePendingFullscreenTransitions() {
|
||||
if (pending_transitions_.empty()) {
|
||||
set_fullscreen_transition_type(FullScreenTransitionType::kNone);
|
||||
@@ -793,6 +854,250 @@ bool NativeWindow::IsTranslucent() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void NativeWindow::DebouncedSaveWindowState() {
|
||||
save_window_state_timer_.Start(
|
||||
FROM_HERE, base::Milliseconds(200),
|
||||
base::BindOnce(&NativeWindow::SaveWindowState, base::Unretained(this)));
|
||||
}
|
||||
|
||||
void NativeWindow::SaveWindowState() {
|
||||
if (!window_state_persistence_enabled_ || is_being_restored_)
|
||||
return;
|
||||
|
||||
gfx::Rect bounds = GetBounds();
|
||||
|
||||
if (bounds.width() == 0 || bounds.height() == 0) {
|
||||
LOG(WARNING) << "Window state not saved - window bounds are invalid";
|
||||
return;
|
||||
}
|
||||
|
||||
const display::Screen* screen = display::Screen::GetScreen();
|
||||
DCHECK(screen);
|
||||
// GetDisplayMatching returns a fake display with 1920x1080 resolution at
|
||||
// (0,0) when no physical displays are attached.
|
||||
// https://source.chromium.org/chromium/chromium/src/+/main:ui/display/display.cc;l=184;drc=e4f1aef5f3ec30a28950d766612cc2c04c822c71
|
||||
const display::Display display = screen->GetDisplayMatching(bounds);
|
||||
|
||||
// Skip window state persistence when display has invalid dimensions (0x0) or
|
||||
// is fake (ID 0xFF). Invalid displays could cause incorrect window bounds to
|
||||
// be saved, leading to positioning issues during restoration.
|
||||
// https://source.chromium.org/chromium/chromium/src/+/main:ui/display/types/display_constants.h;l=28;drc=e4f1aef5f3ec30a28950d766612cc2c04c822c71
|
||||
if (hasInvalidDisplay(display)) {
|
||||
LOG(WARNING)
|
||||
<< "Window state not saved - no physical display attached or current "
|
||||
"display has invalid bounds";
|
||||
return;
|
||||
}
|
||||
|
||||
ScopedDictPrefUpdate update(prefs_, electron::kWindowStates);
|
||||
const base::Value::Dict* existing_prefs = update->FindDict(window_name_);
|
||||
|
||||
// When the window is in a special display mode (fullscreen, kiosk, or
|
||||
// maximized), save the previously stored window bounds instead of
|
||||
// the current bounds. This ensures that when the window is restored, it can
|
||||
// be restored to its original position and size if display mode is not
|
||||
// preserved via windowStatePersistence.
|
||||
if (!IsNormal() && existing_prefs) {
|
||||
std::optional<int> left = existing_prefs->FindInt(electron::kLeft);
|
||||
std::optional<int> top = existing_prefs->FindInt(electron::kTop);
|
||||
std::optional<int> right = existing_prefs->FindInt(electron::kRight);
|
||||
std::optional<int> bottom = existing_prefs->FindInt(electron::kBottom);
|
||||
|
||||
if (left && top && right && bottom) {
|
||||
bounds = gfx::Rect(*left, *top, *right - *left, *bottom - *top);
|
||||
}
|
||||
}
|
||||
|
||||
base::Value::Dict window_preferences;
|
||||
window_preferences.Set(electron::kLeft, bounds.x());
|
||||
window_preferences.Set(electron::kTop, bounds.y());
|
||||
window_preferences.Set(electron::kRight, bounds.right());
|
||||
window_preferences.Set(electron::kBottom, bounds.bottom());
|
||||
|
||||
window_preferences.Set(electron::kMaximized, IsMaximized());
|
||||
window_preferences.Set(electron::kFullscreen, IsFullscreen());
|
||||
window_preferences.Set(electron::kKiosk, IsKiosk());
|
||||
|
||||
gfx::Rect work_area = display.work_area();
|
||||
|
||||
window_preferences.Set(electron::kWorkAreaLeft, work_area.x());
|
||||
window_preferences.Set(electron::kWorkAreaTop, work_area.y());
|
||||
window_preferences.Set(electron::kWorkAreaRight, work_area.right());
|
||||
window_preferences.Set(electron::kWorkAreaBottom, work_area.bottom());
|
||||
|
||||
update->Set(window_name_, std::move(window_preferences));
|
||||
}
|
||||
|
||||
void NativeWindow::FlushWindowState() {
|
||||
if (save_window_state_timer_.IsRunning()) {
|
||||
save_window_state_timer_.FireNow();
|
||||
} else {
|
||||
SaveWindowState();
|
||||
}
|
||||
}
|
||||
|
||||
void NativeWindow::RestoreWindowState(const gin_helper::Dictionary& options) {
|
||||
if (!window_state_persistence_enabled_)
|
||||
return;
|
||||
|
||||
const base::Value& value = prefs_->GetValue(electron::kWindowStates);
|
||||
const base::Value::Dict* window_preferences =
|
||||
value.is_dict() ? value.GetDict().FindDict(window_name_) : nullptr;
|
||||
|
||||
if (!window_preferences)
|
||||
return;
|
||||
|
||||
std::optional<int> saved_left = window_preferences->FindInt(electron::kLeft);
|
||||
std::optional<int> saved_top = window_preferences->FindInt(electron::kTop);
|
||||
std::optional<int> saved_right =
|
||||
window_preferences->FindInt(electron::kRight);
|
||||
std::optional<int> saved_bottom =
|
||||
window_preferences->FindInt(electron::kBottom);
|
||||
|
||||
std::optional<int> work_area_left =
|
||||
window_preferences->FindInt(electron::kWorkAreaLeft);
|
||||
std::optional<int> work_area_top =
|
||||
window_preferences->FindInt(electron::kWorkAreaTop);
|
||||
std::optional<int> work_area_right =
|
||||
window_preferences->FindInt(electron::kWorkAreaRight);
|
||||
std::optional<int> work_area_bottom =
|
||||
window_preferences->FindInt(electron::kWorkAreaBottom);
|
||||
|
||||
if (!saved_left || !saved_top || !saved_right || !saved_bottom ||
|
||||
!work_area_left || !work_area_top || !work_area_right ||
|
||||
!work_area_bottom) {
|
||||
LOG(WARNING) << "Window state not restored - corrupted values found";
|
||||
return;
|
||||
}
|
||||
|
||||
gfx::Rect saved_bounds =
|
||||
gfx::Rect(*saved_left, *saved_top, *saved_right - *saved_left,
|
||||
*saved_bottom - *saved_top);
|
||||
|
||||
display::Screen* screen = display::Screen::GetScreen();
|
||||
DCHECK(screen);
|
||||
|
||||
// Set the primary display as the target display for restoration.
|
||||
display::Display display = screen->GetPrimaryDisplay();
|
||||
|
||||
// We identify the display with the minimal Manhattan distance to the saved
|
||||
// bounds and set it as the target display for restoration.
|
||||
int min_displacement = std::numeric_limits<int>::max();
|
||||
|
||||
for (const auto& candidate : screen->GetAllDisplays()) {
|
||||
gfx::Rect test_bounds = saved_bounds;
|
||||
test_bounds.AdjustToFit(candidate.work_area());
|
||||
int displacement = std::abs(test_bounds.x() - saved_bounds.x()) +
|
||||
std::abs(test_bounds.y() - saved_bounds.y());
|
||||
|
||||
if (displacement < min_displacement) {
|
||||
min_displacement = displacement;
|
||||
display = candidate;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip window state restoration if current display has invalid dimensions or
|
||||
// is fake. Restoring from invalid displays (0x0) or fake displays (ID 0xFF)
|
||||
// could cause incorrect window positioning when later moved to real displays.
|
||||
// https://source.chromium.org/chromium/chromium/src/+/main:ui/display/types/display_constants.h;l=28;drc=e4f1aef5f3ec30a28950d766612cc2c04c822c71
|
||||
if (hasInvalidDisplay(display)) {
|
||||
LOG(WARNING) << "Window state not restored - no physical display attached "
|
||||
"or current display has invalid bounds";
|
||||
return;
|
||||
}
|
||||
|
||||
gfx::Rect saved_work_area = gfx::Rect(*work_area_left, *work_area_top,
|
||||
*work_area_right - *work_area_left,
|
||||
*work_area_bottom - *work_area_top);
|
||||
|
||||
// Set this to true before RestoreBounds to prevent SaveWindowState from being
|
||||
// inadvertently triggered during the restoration process.
|
||||
is_being_restored_ = true;
|
||||
|
||||
if (restore_bounds_) {
|
||||
RestoreBounds(display, saved_work_area, saved_bounds);
|
||||
}
|
||||
|
||||
if (restore_display_mode_) {
|
||||
restore_display_mode_callback_ = base::BindOnce(
|
||||
[](NativeWindow* window, base::Value::Dict prefs) {
|
||||
if (auto kiosk = prefs.FindBool(electron::kKiosk); kiosk && *kiosk) {
|
||||
window->SetKiosk(true);
|
||||
} else if (auto fs = prefs.FindBool(electron::kFullscreen);
|
||||
fs && *fs) {
|
||||
window->SetFullScreen(true);
|
||||
} else if (auto max = prefs.FindBool(electron::kMaximized);
|
||||
max && *max) {
|
||||
window->Maximize();
|
||||
}
|
||||
},
|
||||
base::Unretained(this), window_preferences->Clone());
|
||||
}
|
||||
|
||||
is_being_restored_ = false;
|
||||
}
|
||||
|
||||
void NativeWindow::FlushPendingDisplayMode() {
|
||||
if (restore_display_mode_callback_) {
|
||||
std::move(restore_display_mode_callback_).Run();
|
||||
}
|
||||
}
|
||||
|
||||
// This function is similar to Chromium's window bounds adjustment logic
|
||||
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/window_sizer/window_sizer.cc;l=350;drc=0ec56065ba588552f21633aa47280ba02c3cd160
|
||||
void NativeWindow::RestoreBounds(const display::Display& display,
|
||||
const gfx::Rect& saved_work_area,
|
||||
gfx::Rect& saved_bounds) {
|
||||
if (saved_bounds.width() == 0 || saved_bounds.height() == 0) {
|
||||
LOG(WARNING) << "Window bounds not restored - values are invalid";
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure that the window is at least kMinVisibleHeight * kMinVisibleWidth.
|
||||
saved_bounds.set_height(std::max(kMinVisibleHeight, saved_bounds.height()));
|
||||
saved_bounds.set_width(std::max(kMinVisibleWidth, saved_bounds.width()));
|
||||
|
||||
const gfx::Rect work_area = display.work_area();
|
||||
// Ensure that the title bar is not above the work area.
|
||||
if (saved_bounds.y() < work_area.y()) {
|
||||
saved_bounds.set_y(work_area.y());
|
||||
}
|
||||
|
||||
// Reposition and resize the bounds if the saved_work_area is different from
|
||||
// the current work area and the current work area doesn't completely contain
|
||||
// the bounds.
|
||||
if (!saved_work_area.IsEmpty() && saved_work_area != work_area &&
|
||||
!work_area.Contains(saved_bounds)) {
|
||||
saved_bounds.AdjustToFit(work_area);
|
||||
}
|
||||
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
// On mac, we want to be aggressive about repositioning windows that are
|
||||
// partially offscreen. If the window is partially offscreen horizontally,
|
||||
// snap to the nearest edge of the work area. This call also adjusts the
|
||||
// height, width if needed to make the window fully visible.
|
||||
saved_bounds.AdjustToFit(work_area);
|
||||
#else
|
||||
// On non-Mac platforms, we are less aggressive about repositioning. Simply
|
||||
// ensure that at least kMinVisibleWidth * kMinVisibleHeight is visible
|
||||
|
||||
const int min_y = work_area.y() + kMinVisibleHeight - saved_bounds.height();
|
||||
const int min_x = work_area.x() + kMinVisibleWidth - saved_bounds.width();
|
||||
const int max_y = work_area.bottom() - kMinVisibleHeight;
|
||||
const int max_x = work_area.right() - kMinVisibleWidth;
|
||||
// Reposition and resize the bounds to make it fully visible inside the work
|
||||
// area. `min_x >= max_x` happens when work area and bounds are both small.
|
||||
if (min_x >= max_x || min_y >= max_y) {
|
||||
saved_bounds.AdjustToFit(work_area);
|
||||
} else {
|
||||
saved_bounds.set_y(std::clamp(saved_bounds.y(), min_y, max_y));
|
||||
saved_bounds.set_x(std::clamp(saved_bounds.x(), min_x, max_x));
|
||||
}
|
||||
#endif // BUILDFLAG(IS_MAC)
|
||||
|
||||
SetBounds(saved_bounds);
|
||||
}
|
||||
|
||||
// static
|
||||
bool NativeWindow::PlatformHasClientFrame() {
|
||||
#if defined(USE_OZONE)
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "base/observer_list.h"
|
||||
#include "base/strings/cstring_view.h"
|
||||
#include "base/supports_user_data.h"
|
||||
#include "base/timer/timer.h"
|
||||
#include "content/public/browser/desktop_media_id.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#include "extensions/browser/app_window/size_constraints.h"
|
||||
@@ -27,6 +28,7 @@
|
||||
|
||||
class SkRegion;
|
||||
class DraggableRegionProvider;
|
||||
class PrefService;
|
||||
|
||||
namespace input {
|
||||
struct NativeWebKeyboardEvent;
|
||||
@@ -169,9 +171,11 @@ class NativeWindow : public base::SupportsUserData,
|
||||
void SetTitle(std::string_view title);
|
||||
[[nodiscard]] std::string GetTitle() const;
|
||||
|
||||
[[nodiscard]] std::string GetName() const;
|
||||
|
||||
// Ability to augment the window title for the screen readers.
|
||||
void SetAccessibleTitle(const std::string& title);
|
||||
std::string GetAccessibleTitle();
|
||||
[[nodiscard]] std::string GetAccessibleTitle() const;
|
||||
|
||||
virtual void FlashFrame(bool flash) = 0;
|
||||
virtual void SetSkipTaskbar(bool skip) = 0;
|
||||
@@ -433,6 +437,28 @@ class NativeWindow : public base::SupportsUserData,
|
||||
// throttling, then throttling in the `ui::Compositor` will be disabled.
|
||||
void UpdateBackgroundThrottlingState();
|
||||
|
||||
// Saves current window state to the Local State JSON file in
|
||||
// app.getPath('userData') via PrefService.
|
||||
// This does NOT immediately write to disk - it updates the in-memory
|
||||
// preference store and queues an asynchronous write operation. The actual
|
||||
// disk write is batched and flushed later.
|
||||
void SaveWindowState();
|
||||
void DebouncedSaveWindowState();
|
||||
// Flushes save_window_state_timer_ that was queued by
|
||||
// DebouncedSaveWindowState. This does NOT flush the actual disk write.
|
||||
void FlushWindowState();
|
||||
|
||||
// Restores window state - bounds first and then display mode.
|
||||
void RestoreWindowState(const gin_helper::Dictionary& options);
|
||||
// Applies saved bounds to the window.
|
||||
void RestoreBounds(const display::Display& display,
|
||||
const gfx::Rect& saved_work_area,
|
||||
gfx::Rect& saved_bounds);
|
||||
// Flushes pending display mode restoration (fullscreen, maximized, kiosk)
|
||||
// that was deferred during initialization to respect show=false. This
|
||||
// consumes and clears the restore_display_mode_callback_.
|
||||
void FlushPendingDisplayMode();
|
||||
|
||||
protected:
|
||||
friend class api::BrowserView;
|
||||
|
||||
@@ -494,6 +520,10 @@ class NativeWindow : public base::SupportsUserData,
|
||||
static inline int32_t next_id_ = 0;
|
||||
const int32_t window_id_ = ++next_id_;
|
||||
|
||||
// Identifier for the window provided by the application.
|
||||
// Used by Electron internally for features such as state persistence.
|
||||
std::string window_name_;
|
||||
|
||||
// The "titleBarStyle" option.
|
||||
const TitleBarStyle title_bar_style_;
|
||||
|
||||
@@ -552,6 +582,32 @@ class NativeWindow : public base::SupportsUserData,
|
||||
|
||||
gfx::Rect overlay_rect_;
|
||||
|
||||
// Flag to prevent SaveWindowState calls during window restoration.
|
||||
bool is_being_restored_ = false;
|
||||
|
||||
// The boolean parsing of the "windowStatePersistence" option
|
||||
bool window_state_persistence_enabled_ = false;
|
||||
|
||||
// PrefService is used to persist window bounds and state.
|
||||
// Only populated when windowStatePersistence is enabled and window has a
|
||||
// valid name.
|
||||
raw_ptr<PrefService> prefs_ = nullptr;
|
||||
|
||||
// Whether to restore bounds.
|
||||
bool restore_bounds_ = false;
|
||||
// Whether to restore display mode.
|
||||
bool restore_display_mode_ = false;
|
||||
// Callback to restore display mode.
|
||||
base::OnceCallback<void()> restore_display_mode_callback_;
|
||||
|
||||
// Timer to debounce window state saving operations.
|
||||
base::OneShotTimer save_window_state_timer_;
|
||||
|
||||
// Minimum height of the visible part of a window.
|
||||
const int kMinVisibleHeight = 100;
|
||||
// Minimum width of the visible part of a window.
|
||||
const int kMinVisibleWidth = 100;
|
||||
|
||||
base::WeakPtrFactory<NativeWindow> weak_factory_{this};
|
||||
};
|
||||
|
||||
|
||||
@@ -416,6 +416,8 @@ void NativeWindowMac::Show() {
|
||||
return;
|
||||
}
|
||||
|
||||
FlushPendingDisplayMode();
|
||||
|
||||
set_wants_to_be_visible(true);
|
||||
|
||||
// Reattach the window to the parent to actually show it.
|
||||
|
||||
@@ -558,6 +558,8 @@ void NativeWindowViews::Show() {
|
||||
if (is_modal() && NativeWindow::parent() && !widget()->IsVisible())
|
||||
static_cast<NativeWindowViews*>(parent())->IncrementChildModals();
|
||||
|
||||
FlushPendingDisplayMode();
|
||||
|
||||
widget()->native_widget_private()->Show(GetRestoredState(), gfx::Rect());
|
||||
|
||||
// explicitly focus the window
|
||||
|
||||
@@ -21,6 +21,23 @@ inline constexpr std::string_view kDeviceVendorIdKey = "vendorId";
|
||||
inline constexpr std::string_view kDeviceProductIdKey = "productId";
|
||||
inline constexpr std::string_view kDeviceSerialNumberKey = "serialNumber";
|
||||
|
||||
// Window state preference keys
|
||||
inline constexpr std::string_view kLeft = "left";
|
||||
inline constexpr std::string_view kTop = "top";
|
||||
inline constexpr std::string_view kRight = "right";
|
||||
inline constexpr std::string_view kBottom = "bottom";
|
||||
|
||||
inline constexpr std::string_view kMaximized = "maximized";
|
||||
inline constexpr std::string_view kFullscreen = "fullscreen";
|
||||
inline constexpr std::string_view kKiosk = "kiosk";
|
||||
|
||||
inline constexpr std::string_view kWorkAreaLeft = "workAreaLeft";
|
||||
inline constexpr std::string_view kWorkAreaTop = "workAreaTop";
|
||||
inline constexpr std::string_view kWorkAreaRight = "workAreaRight";
|
||||
inline constexpr std::string_view kWorkAreaBottom = "workAreaBottom";
|
||||
|
||||
inline constexpr std::string_view kWindowStates = "windowStates";
|
||||
|
||||
inline constexpr base::cstring_view kRunAsNode = "ELECTRON_RUN_AS_NODE";
|
||||
|
||||
// Per-profile UUID to distinguish global shortcut sessions for
|
||||
|
||||
@@ -107,6 +107,19 @@ inline constexpr std::string_view kFocusable = "focusable";
|
||||
// The WebPreferences.
|
||||
inline constexpr std::string_view kWebPreferences = "webPreferences";
|
||||
|
||||
// Window state persistence for BaseWindow
|
||||
inline constexpr std::string_view kWindowStatePersistence =
|
||||
"windowStatePersistence";
|
||||
|
||||
// Identifier for the window provided by the application
|
||||
inline constexpr std::string_view kName = "name";
|
||||
|
||||
// Whether to save the window bounds
|
||||
inline constexpr std::string_view kBounds = "bounds";
|
||||
|
||||
// Whether to save the window display mode
|
||||
inline constexpr std::string_view kDisplayMode = "displayMode";
|
||||
|
||||
// Add a vibrancy effect to the browser window
|
||||
inline constexpr std::string_view kVibrancyType = "vibrancy";
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
27
spec/fixtures/api/window-state-save/close-save/index.js
vendored
Normal file
27
spec/fixtures/api/window-state-save/close-save/index.js
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-close-save',
|
||||
windowStatePersistence: true,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.on('close', () => {
|
||||
app.quit();
|
||||
});
|
||||
|
||||
w.close();
|
||||
// Timeout of 10s to ensure app exits
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 10000);
|
||||
});
|
||||
28
spec/fixtures/api/window-state-save/fullscreen-save/index.js
vendored
Normal file
28
spec/fixtures/api/window-state-save/fullscreen-save/index.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-fullscreen-save',
|
||||
windowStatePersistence: true
|
||||
});
|
||||
|
||||
w.on('enter-full-screen', () => {
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
w.setFullScreen(true);
|
||||
// Timeout of 10s to ensure app exits
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 10000);
|
||||
});
|
||||
28
spec/fixtures/api/window-state-save/kiosk-save/index.js
vendored
Normal file
28
spec/fixtures/api/window-state-save/kiosk-save/index.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-kiosk-save',
|
||||
windowStatePersistence: true
|
||||
});
|
||||
|
||||
w.on('enter-full-screen', () => {
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
w.setKiosk(true);
|
||||
// Timeout of 10s to ensure app exits
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 10000);
|
||||
});
|
||||
28
spec/fixtures/api/window-state-save/maximize-save/index.js
vendored
Normal file
28
spec/fixtures/api/window-state-save/maximize-save/index.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-maximize-save',
|
||||
windowStatePersistence: true
|
||||
});
|
||||
|
||||
w.on('maximize', () => {
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
w.maximize();
|
||||
// Timeout of 10s to ensure app exits
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 10000);
|
||||
});
|
||||
28
spec/fixtures/api/window-state-save/minimize-save/index.js
vendored
Normal file
28
spec/fixtures/api/window-state-save/minimize-save/index.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-minimize-save',
|
||||
windowStatePersistence: true
|
||||
});
|
||||
|
||||
w.on('minimize', () => {
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
w.minimize();
|
||||
// Timeout of 10s to ensure app exits
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 10000);
|
||||
});
|
||||
23
spec/fixtures/api/window-state-save/move-save/index.js
vendored
Normal file
23
spec/fixtures/api/window-state-save/move-save/index.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-move-save',
|
||||
windowStatePersistence: true,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.setPosition(100, 150);
|
||||
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
23
spec/fixtures/api/window-state-save/resize-save/index.js
vendored
Normal file
23
spec/fixtures/api/window-state-save/resize-save/index.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-resize-save',
|
||||
windowStatePersistence: true,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.setSize(500, 400);
|
||||
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
23
spec/fixtures/api/window-state-save/schema-check/index.js
vendored
Normal file
23
spec/fixtures/api/window-state-save/schema-check/index.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const w = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 300,
|
||||
name: 'test-window-state-schema',
|
||||
windowStatePersistence: true,
|
||||
show: false
|
||||
});
|
||||
|
||||
w.close();
|
||||
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 1000);
|
||||
});
|
||||
41
spec/fixtures/api/window-state-save/work-area-primary/index.js
vendored
Normal file
41
spec/fixtures/api/window-state-save/work-area-primary/index.js
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
const { app, BrowserWindow, screen } = require('electron');
|
||||
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
|
||||
const sharedUserData = path.join(os.tmpdir(), 'electron-window-state-test');
|
||||
app.setPath('userData', sharedUserData);
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
const primaryDisplay = screen.getPrimaryDisplay();
|
||||
const workArea = primaryDisplay.workArea;
|
||||
|
||||
const maxWidth = Math.max(200, Math.floor(workArea.width * 0.8));
|
||||
const maxHeight = Math.max(150, Math.floor(workArea.height * 0.8));
|
||||
const windowWidth = Math.min(400, maxWidth);
|
||||
const windowHeight = Math.min(300, maxHeight);
|
||||
|
||||
const w = new BrowserWindow({
|
||||
width: windowWidth,
|
||||
height: windowHeight,
|
||||
name: 'test-work-area-primary',
|
||||
windowStatePersistence: true
|
||||
});
|
||||
|
||||
// Center the window on the primary display to prevent overflow
|
||||
const centerX = workArea.x + Math.floor((workArea.width - windowWidth) / 2);
|
||||
const centerY = workArea.y + Math.floor((workArea.height - windowHeight) / 2);
|
||||
|
||||
w.setPosition(centerX, centerY);
|
||||
|
||||
w.on('close', () => {
|
||||
app.quit();
|
||||
});
|
||||
|
||||
w.close();
|
||||
|
||||
// Timeout of 10s to ensure app exits
|
||||
setTimeout(() => {
|
||||
app.quit();
|
||||
}, 10000);
|
||||
});
|
||||
90
spec/fixtures/native-addon/virtual-display/binding.gyp
vendored
Normal file
90
spec/fixtures/native-addon/virtual-display/binding.gyp
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
{
|
||||
"targets": [{
|
||||
"target_name": "virtual_display",
|
||||
"conditions": [
|
||||
['OS=="mac"', {
|
||||
"sources": [
|
||||
"src/addon.mm",
|
||||
"src/VirtualDisplayBridge.m"
|
||||
],
|
||||
"include_dirs": [
|
||||
"<!@(node -p \"require('node-addon-api').include\")",
|
||||
"include",
|
||||
"build_swift"
|
||||
],
|
||||
"dependencies": [
|
||||
"<!(node -p \"require('node-addon-api').gyp\")"
|
||||
],
|
||||
"libraries": [
|
||||
"<(PRODUCT_DIR)/libVirtualDisplay.dylib"
|
||||
],
|
||||
"defines": [
|
||||
"NODE_ADDON_API_CPP_EXCEPTIONS"
|
||||
],
|
||||
"cflags!": [ "-fno-exceptions" ],
|
||||
"cflags_cc!": [ "-fno-exceptions" ],
|
||||
"xcode_settings": {
|
||||
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
|
||||
"CLANG_ENABLE_OBJC_ARC": "YES",
|
||||
"CLANG_CXX_LIBRARY": "libc++",
|
||||
"SWIFT_OBJC_BRIDGING_HEADER": "include/VirtualDisplayBridge.h",
|
||||
"SWIFT_VERSION": "5.0",
|
||||
"SWIFT_OBJC_INTERFACE_HEADER_NAME": "virtual_display-Swift.h",
|
||||
"MACOSX_DEPLOYMENT_TARGET": "11.0",
|
||||
"OTHER_CFLAGS": [
|
||||
"-ObjC++",
|
||||
"-fobjc-arc"
|
||||
],
|
||||
"OTHER_LDFLAGS": [
|
||||
"-lswiftCore",
|
||||
"-lswiftFoundation",
|
||||
"-lswiftObjectiveC",
|
||||
"-lswiftDarwin",
|
||||
"-lswiftDispatch",
|
||||
"-L/usr/lib/swift",
|
||||
"-Wl,-rpath,/usr/lib/swift",
|
||||
"-Wl,-rpath,@loader_path"
|
||||
]
|
||||
},
|
||||
"actions": [
|
||||
{
|
||||
"action_name": "build_swift",
|
||||
"inputs": [
|
||||
"src/VirtualDisplay.swift",
|
||||
"src/Dummy.swift",
|
||||
"include/VirtualDisplayBridge.h"
|
||||
],
|
||||
"outputs": [
|
||||
"build_swift/libVirtualDisplay.dylib",
|
||||
"build_swift/virtual_display-Swift.h"
|
||||
],
|
||||
"action": [
|
||||
"swiftc",
|
||||
"src/VirtualDisplay.swift",
|
||||
"src/Dummy.swift",
|
||||
"-import-objc-header", "include/VirtualDisplayBridge.h",
|
||||
"-emit-objc-header-path", "./build_swift/virtual_display-Swift.h",
|
||||
"-emit-library", "-o", "./build_swift/libVirtualDisplay.dylib",
|
||||
"-emit-module", "-module-name", "virtual_display",
|
||||
"-module-link-name", "VirtualDisplay"
|
||||
]
|
||||
},
|
||||
{
|
||||
"action_name": "copy_swift_lib",
|
||||
"inputs": [
|
||||
"<(module_root_dir)/build_swift/libVirtualDisplay.dylib"
|
||||
],
|
||||
"outputs": [
|
||||
"<(PRODUCT_DIR)/libVirtualDisplay.dylib"
|
||||
],
|
||||
"action": [
|
||||
"sh",
|
||||
"-c",
|
||||
"cp -f <(module_root_dir)/build_swift/libVirtualDisplay.dylib <(PRODUCT_DIR)/libVirtualDisplay.dylib && install_name_tool -id @rpath/libVirtualDisplay.dylib <(PRODUCT_DIR)/libVirtualDisplay.dylib"
|
||||
]
|
||||
}
|
||||
]
|
||||
}]
|
||||
]
|
||||
}]
|
||||
}
|
||||
121
spec/fixtures/native-addon/virtual-display/include/VirtualDisplayBridge.h
vendored
Normal file
121
spec/fixtures/native-addon/virtual-display/include/VirtualDisplayBridge.h
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
#ifndef VirtualDisplayBridge_h
|
||||
#define VirtualDisplayBridge_h
|
||||
|
||||
#import <CoreGraphics/CoreGraphics.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface VirtualDisplayBridge : NSObject
|
||||
|
||||
+ (NSInteger)create:(int)width height:(int)height x:(int)x y:(int)y;
|
||||
+ (BOOL)destroy:(NSInteger)displayId;
|
||||
+ (BOOL)forceCleanup;
|
||||
|
||||
@end
|
||||
|
||||
@interface CGVirtualDisplay : NSObject {
|
||||
unsigned int _vendorID;
|
||||
unsigned int _productID;
|
||||
unsigned int _serialNum;
|
||||
NSString* _name;
|
||||
struct CGSize _sizeInMillimeters;
|
||||
unsigned int _maxPixelsWide;
|
||||
unsigned int _maxPixelsHigh;
|
||||
struct CGPoint _redPrimary;
|
||||
struct CGPoint _greenPrimary;
|
||||
struct CGPoint _bluePrimary;
|
||||
struct CGPoint _whitePoint;
|
||||
id _queue;
|
||||
id _terminationHandler;
|
||||
void* _client;
|
||||
unsigned int _displayID;
|
||||
unsigned int _hiDPI;
|
||||
NSArray* _modes;
|
||||
unsigned int _serverRPC_port;
|
||||
unsigned int _proxyRPC_port;
|
||||
unsigned int _clientHandler_port;
|
||||
}
|
||||
|
||||
@property(readonly, nonatomic) NSArray* modes;
|
||||
@property(readonly, nonatomic) unsigned int hiDPI;
|
||||
@property(readonly, nonatomic) unsigned int displayID;
|
||||
@property(readonly, nonatomic) id terminationHandler;
|
||||
@property(readonly, nonatomic) id queue;
|
||||
@property(readonly, nonatomic) struct CGPoint whitePoint;
|
||||
@property(readonly, nonatomic) struct CGPoint bluePrimary;
|
||||
@property(readonly, nonatomic) struct CGPoint greenPrimary;
|
||||
@property(readonly, nonatomic) struct CGPoint redPrimary;
|
||||
@property(readonly, nonatomic) unsigned int maxPixelsHigh;
|
||||
@property(readonly, nonatomic) unsigned int maxPixelsWide;
|
||||
@property(readonly, nonatomic) struct CGSize sizeInMillimeters;
|
||||
@property(readonly, nonatomic) NSString* name;
|
||||
@property(readonly, nonatomic) unsigned int serialNum;
|
||||
@property(readonly, nonatomic) unsigned int productID;
|
||||
@property(readonly, nonatomic) unsigned int vendorID;
|
||||
- (BOOL)applySettings:(id)arg1;
|
||||
- (void)dealloc;
|
||||
- (id)initWithDescriptor:(id)arg1;
|
||||
|
||||
@end
|
||||
|
||||
@interface CGVirtualDisplayDescriptor : NSObject {
|
||||
unsigned int _vendorID;
|
||||
unsigned int _productID;
|
||||
unsigned int _serialNum;
|
||||
NSString* _name;
|
||||
struct CGSize _sizeInMillimeters;
|
||||
unsigned int _maxPixelsWide;
|
||||
unsigned int _maxPixelsHigh;
|
||||
struct CGPoint _redPrimary;
|
||||
struct CGPoint _greenPrimary;
|
||||
struct CGPoint _bluePrimary;
|
||||
struct CGPoint _whitePoint;
|
||||
id _queue;
|
||||
id _terminationHandler;
|
||||
}
|
||||
|
||||
@property(retain, nonatomic) id queue;
|
||||
@property(retain, nonatomic) NSString* name;
|
||||
@property(nonatomic) struct CGPoint whitePoint;
|
||||
@property(nonatomic) struct CGPoint bluePrimary;
|
||||
@property(nonatomic) struct CGPoint greenPrimary;
|
||||
@property(nonatomic) struct CGPoint redPrimary;
|
||||
@property(nonatomic) unsigned int maxPixelsHigh;
|
||||
@property(nonatomic) unsigned int maxPixelsWide;
|
||||
@property(nonatomic) struct CGSize sizeInMillimeters;
|
||||
@property(nonatomic) unsigned int serialNum;
|
||||
@property(nonatomic) unsigned int productID;
|
||||
@property(nonatomic) unsigned int vendorID;
|
||||
- (void)dealloc;
|
||||
- (id)init;
|
||||
@property(copy, nonatomic) id terminationHandler;
|
||||
|
||||
@end
|
||||
|
||||
@interface CGVirtualDisplayMode : NSObject {
|
||||
unsigned int _width;
|
||||
unsigned int _height;
|
||||
double _refreshRate;
|
||||
}
|
||||
|
||||
@property(readonly, nonatomic) double refreshRate;
|
||||
@property(readonly, nonatomic) unsigned int height;
|
||||
@property(readonly, nonatomic) unsigned int width;
|
||||
- (id)initWithWidth:(unsigned int)arg1
|
||||
height:(unsigned int)arg2
|
||||
refreshRate:(double)arg3;
|
||||
|
||||
@end
|
||||
|
||||
@interface CGVirtualDisplaySettings : NSObject {
|
||||
NSArray* _modes;
|
||||
unsigned int _hiDPI;
|
||||
}
|
||||
|
||||
@property(nonatomic) unsigned int hiDPI;
|
||||
- (void)dealloc;
|
||||
- (id)init;
|
||||
@property(retain, nonatomic) NSArray* modes;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
6
spec/fixtures/native-addon/virtual-display/lib/virtual-display.js
vendored
Normal file
6
spec/fixtures/native-addon/virtual-display/lib/virtual-display.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = process.platform === 'darwin'
|
||||
? require('../build/Release/virtual_display.node')
|
||||
: {
|
||||
create: () => { throw new Error('Virtual displays only supported on macOS'); },
|
||||
destroy: () => { throw new Error('Virtual displays only supported on macOS'); }
|
||||
};
|
||||
20
spec/fixtures/native-addon/virtual-display/package.json
vendored
Normal file
20
spec/fixtures/native-addon/virtual-display/package.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@electron-ci/virtual-display",
|
||||
"version": "1.0.0",
|
||||
"description": "Virtual display for multi-monitor testing",
|
||||
"main": "./lib/virtual-display.js",
|
||||
"scripts": {
|
||||
"clean": "rm -rf build",
|
||||
"build-electron": "electron-rebuild",
|
||||
"build": "node-gyp configure && node-gyp build"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"bindings": "^1.5.0",
|
||||
"node-addon-api": "^8.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^30.0.0",
|
||||
"node-gyp": "^11.1.0"
|
||||
}
|
||||
}
|
||||
181
spec/fixtures/native-addon/virtual-display/src/Dummy.swift
vendored
Normal file
181
spec/fixtures/native-addon/virtual-display/src/Dummy.swift
vendored
Normal file
@@ -0,0 +1,181 @@
|
||||
import Foundation
|
||||
import Cocoa
|
||||
import os.log
|
||||
|
||||
class DummyManager {
|
||||
struct DefinedDummy {
|
||||
var dummy: Dummy
|
||||
}
|
||||
|
||||
static var definedDummies: [Int: DefinedDummy] = [:]
|
||||
static var dummyCounter: Int = 0
|
||||
|
||||
static func createDummy(_ dummyDefinition: DummyDefinition, isPortrait _: Bool = false, serialNum: UInt32 = 0, doConnect: Bool = true) -> Int? {
|
||||
let dummy = Dummy(dummyDefinition: dummyDefinition, serialNum: serialNum, doConnect: doConnect)
|
||||
|
||||
if !dummy.isConnected {
|
||||
print("[DummyManager.createDummy:\(#line)] Failed to create virtual display - not connected")
|
||||
return nil
|
||||
}
|
||||
self.dummyCounter += 1
|
||||
self.definedDummies[self.dummyCounter] = DefinedDummy(dummy: dummy)
|
||||
return self.dummyCounter
|
||||
}
|
||||
|
||||
static func discardDummyByNumber(_ number: Int) {
|
||||
if let definedDummy = self.definedDummies[number] {
|
||||
if definedDummy.dummy.isConnected {
|
||||
definedDummy.dummy.disconnect()
|
||||
}
|
||||
}
|
||||
self.definedDummies[number] = nil
|
||||
}
|
||||
|
||||
static func forceCleanup() {
|
||||
for (_, definedDummy) in self.definedDummies {
|
||||
if definedDummy.dummy.isConnected {
|
||||
definedDummy.dummy.virtualDisplay = nil
|
||||
definedDummy.dummy.displayIdentifier = 0
|
||||
definedDummy.dummy.isConnected = false
|
||||
}
|
||||
}
|
||||
|
||||
self.definedDummies.removeAll()
|
||||
self.dummyCounter = 0
|
||||
|
||||
var config: CGDisplayConfigRef? = nil
|
||||
if CGBeginDisplayConfiguration(&config) == .success {
|
||||
CGCompleteDisplayConfiguration(config, .permanently)
|
||||
}
|
||||
|
||||
usleep(2000000)
|
||||
|
||||
if CGBeginDisplayConfiguration(&config) == .success {
|
||||
CGCompleteDisplayConfiguration(config, .forSession)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DummyDefinition {
|
||||
let aspectWidth, aspectHeight, multiplierStep, minMultiplier, maxMultiplier: Int
|
||||
let refreshRates: [Double]
|
||||
let description: String
|
||||
let addSeparatorAfter: Bool
|
||||
|
||||
init(_ aspectWidth: Int, _ aspectHeight: Int, _ step: Int, _ refreshRates: [Double], _ description: String, _ addSeparatorAfter: Bool = false) {
|
||||
let minX: Int = 720
|
||||
let minY: Int = 720
|
||||
let maxX: Int = 8192
|
||||
let maxY: Int = 8192
|
||||
let minMultiplier = max(Int(ceil(Float(minX) / (Float(aspectWidth) * Float(step)))), Int(ceil(Float(minY) / (Float(aspectHeight) * Float(step)))))
|
||||
let maxMultiplier = min(Int(floor(Float(maxX) / (Float(aspectWidth) * Float(step)))), Int(floor(Float(maxY) / (Float(aspectHeight) * Float(step)))))
|
||||
|
||||
self.aspectWidth = aspectWidth
|
||||
self.aspectHeight = aspectHeight
|
||||
self.minMultiplier = minMultiplier
|
||||
self.maxMultiplier = maxMultiplier
|
||||
self.multiplierStep = step
|
||||
self.refreshRates = refreshRates
|
||||
self.description = description
|
||||
self.addSeparatorAfter = addSeparatorAfter
|
||||
}
|
||||
}
|
||||
|
||||
class Dummy: Equatable {
|
||||
var virtualDisplay: CGVirtualDisplay?
|
||||
var dummyDefinition: DummyDefinition
|
||||
let serialNum: UInt32
|
||||
var isConnected: Bool = false
|
||||
var displayIdentifier: CGDirectDisplayID = 0
|
||||
|
||||
static func == (lhs: Dummy, rhs: Dummy) -> Bool {
|
||||
lhs.serialNum == rhs.serialNum
|
||||
}
|
||||
|
||||
init(dummyDefinition: DummyDefinition, serialNum: UInt32 = 0, doConnect: Bool = true) {
|
||||
var storedSerialNum: UInt32 = serialNum
|
||||
if storedSerialNum == 0 {
|
||||
storedSerialNum = UInt32.random(in: 0 ... UInt32.max)
|
||||
}
|
||||
self.dummyDefinition = dummyDefinition
|
||||
self.serialNum = storedSerialNum
|
||||
if doConnect {
|
||||
_ = self.connect()
|
||||
}
|
||||
}
|
||||
|
||||
func getName() -> String {
|
||||
"Dummy \(self.dummyDefinition.description.components(separatedBy: " ").first ?? self.dummyDefinition.description)"
|
||||
}
|
||||
|
||||
func connect() -> Bool {
|
||||
if self.virtualDisplay != nil || self.isConnected {
|
||||
self.disconnect()
|
||||
}
|
||||
let name: String = self.getName()
|
||||
if let virtualDisplay = Dummy.createVirtualDisplay(self.dummyDefinition, name: name, serialNum: self.serialNum) {
|
||||
self.virtualDisplay = virtualDisplay
|
||||
self.displayIdentifier = virtualDisplay.displayID
|
||||
self.isConnected = true
|
||||
print("[Dummy.connect:\(#line)] Successfully connected virtual display: \(name)")
|
||||
return true
|
||||
} else {
|
||||
print("[Dummy.connect:\(#line)] Failed to connect virtual display: \(name)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func disconnect() {
|
||||
self.virtualDisplay = nil
|
||||
self.isConnected = false
|
||||
print("[Dummy.disconnect:\(#line)] Disconnected virtual display: \(self.getName())")
|
||||
}
|
||||
|
||||
private static func waitForDisplayRegistration(_ displayId: CGDirectDisplayID) -> Bool {
|
||||
for _ in 0..<20 {
|
||||
var count: UInt32 = 0, displays = [CGDirectDisplayID](repeating: 0, count: 32)
|
||||
if CGGetActiveDisplayList(32, &displays, &count) == .success && displays[0..<Int(count)].contains(displayId) {
|
||||
return true
|
||||
}
|
||||
usleep(100000)
|
||||
}
|
||||
print("[Dummy.waitForDisplayRegistration:\(#line)] Failed to register virtual display: \(displayId)")
|
||||
return false
|
||||
}
|
||||
|
||||
static func createVirtualDisplay(_ definition: DummyDefinition, name: String, serialNum: UInt32, hiDPI: Bool = false) -> CGVirtualDisplay? {
|
||||
if let descriptor = CGVirtualDisplayDescriptor() {
|
||||
descriptor.queue = DispatchQueue.global(qos: .userInteractive)
|
||||
descriptor.name = name
|
||||
descriptor.whitePoint = CGPoint(x: 0.950, y: 1.000)
|
||||
descriptor.redPrimary = CGPoint(x: 0.454, y: 0.242)
|
||||
descriptor.greenPrimary = CGPoint(x: 0.353, y: 0.674)
|
||||
descriptor.bluePrimary = CGPoint(x: 0.157, y: 0.084)
|
||||
descriptor.maxPixelsWide = UInt32(definition.aspectWidth * definition.multiplierStep * definition.maxMultiplier)
|
||||
descriptor.maxPixelsHigh = UInt32(definition.aspectHeight * definition.multiplierStep * definition.maxMultiplier)
|
||||
let diagonalSizeRatio: Double = (24 * 25.4) / sqrt(Double(definition.aspectWidth * definition.aspectWidth + definition.aspectHeight * definition.aspectHeight))
|
||||
descriptor.sizeInMillimeters = CGSize(width: Double(definition.aspectWidth) * diagonalSizeRatio, height: Double(definition.aspectHeight) * diagonalSizeRatio)
|
||||
descriptor.serialNum = serialNum
|
||||
descriptor.productID = UInt32(min(definition.aspectWidth - 1, 255) * 256 + min(definition.aspectHeight - 1, 255))
|
||||
descriptor.vendorID = UInt32(0xF0F0)
|
||||
if let display = CGVirtualDisplay(descriptor: descriptor) {
|
||||
var modes = [CGVirtualDisplayMode?](repeating: nil, count: definition.maxMultiplier - definition.minMultiplier + 1)
|
||||
for multiplier in definition.minMultiplier ... definition.maxMultiplier {
|
||||
for refreshRate in definition.refreshRates {
|
||||
let width = UInt32(definition.aspectWidth * multiplier * definition.multiplierStep)
|
||||
let height = UInt32(definition.aspectHeight * multiplier * definition.multiplierStep)
|
||||
modes[multiplier - definition.minMultiplier] = CGVirtualDisplayMode(width: width, height: height, refreshRate: refreshRate)!
|
||||
}
|
||||
}
|
||||
if let settings = CGVirtualDisplaySettings() {
|
||||
settings.hiDPI = hiDPI ? 1 : 0
|
||||
settings.modes = modes as [Any]
|
||||
if display.applySettings(settings) {
|
||||
return waitForDisplayRegistration(display.displayID) ? display : nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
59
spec/fixtures/native-addon/virtual-display/src/VirtualDisplay.swift
vendored
Normal file
59
spec/fixtures/native-addon/virtual-display/src/VirtualDisplay.swift
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
import Foundation
|
||||
import Cocoa
|
||||
import os.log
|
||||
|
||||
@objc public class VirtualDisplay: NSObject {
|
||||
@objc public static func create(width: Int, height: Int, x: Int, y: Int) -> Int {
|
||||
let refreshRates: [Double] = [60.0] // Always 60Hz default
|
||||
let description = "\(width)x\(height) Display"
|
||||
let definition = DummyDefinition(width, height, 1, refreshRates, description, false)
|
||||
let displayId = DummyManager.createDummy(definition) ?? 0
|
||||
positionDisplay(displayId: displayId, x: x, y: y)
|
||||
|
||||
return displayId
|
||||
}
|
||||
|
||||
@objc public static func destroy(id: Int) -> Bool {
|
||||
DummyManager.discardDummyByNumber(id)
|
||||
return true
|
||||
}
|
||||
|
||||
@objc public static func forceCleanup() -> Bool {
|
||||
DummyManager.forceCleanup()
|
||||
return true
|
||||
}
|
||||
|
||||
private static func positionDisplay(displayId: Int, x: Int, y: Int) {
|
||||
guard let definedDummy = DummyManager.definedDummies[displayId],
|
||||
definedDummy.dummy.isConnected else {
|
||||
os_log("VirtualDisplay: Cannot position display %{public}@: display not found or not connected", type: .error, "\(displayId)")
|
||||
return
|
||||
}
|
||||
|
||||
let cgDisplayId = definedDummy.dummy.displayIdentifier
|
||||
|
||||
var config: CGDisplayConfigRef? = nil
|
||||
let beginResult = CGBeginDisplayConfiguration(&config)
|
||||
|
||||
if beginResult != .success {
|
||||
os_log("VirtualDisplay: Cannot position display, failed to begin display configuration via CGBeginDisplayConfiguration: error %{public}@", type: .error, "\(beginResult.rawValue)")
|
||||
return
|
||||
}
|
||||
|
||||
let configResult = CGConfigureDisplayOrigin(config, cgDisplayId, Int32(x), Int32(y))
|
||||
|
||||
if configResult != .success {
|
||||
os_log("VirtualDisplay: Cannot position display, failed to configure display origin via CGConfigureDisplayOrigin: error %{public}@", type: .error, "\(configResult.rawValue)")
|
||||
CGCancelDisplayConfiguration(config)
|
||||
return
|
||||
}
|
||||
|
||||
let completeResult = CGCompleteDisplayConfiguration(config, .permanently)
|
||||
|
||||
if completeResult == .success {
|
||||
os_log("VirtualDisplay: Successfully positioned display %{public}@ at (%{public}@, %{public}@)", type: .info, "\(displayId)", "\(x)", "\(y)")
|
||||
} else {
|
||||
os_log("VirtualDisplay: Cannot position display, failed to complete display configuration via CGCompleteDisplayConfiguration: error %{public}@", type: .error, "\(completeResult.rawValue)")
|
||||
}
|
||||
}
|
||||
}
|
||||
18
spec/fixtures/native-addon/virtual-display/src/VirtualDisplayBridge.m
vendored
Normal file
18
spec/fixtures/native-addon/virtual-display/src/VirtualDisplayBridge.m
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
#import "VirtualDisplayBridge.h"
|
||||
#import "../build_swift/virtual_display-Swift.h"
|
||||
|
||||
@implementation VirtualDisplayBridge
|
||||
|
||||
+ (NSInteger)create:(int)width height:(int)height x:(int)x y:(int)y {
|
||||
return [VirtualDisplay createWithWidth:width height:height x:x y:y];
|
||||
}
|
||||
|
||||
+ (BOOL)destroy:(NSInteger)displayId {
|
||||
return [VirtualDisplay destroyWithId:(int)displayId];
|
||||
}
|
||||
|
||||
+ (BOOL)forceCleanup {
|
||||
return [VirtualDisplay forceCleanup];
|
||||
}
|
||||
|
||||
@end
|
||||
197
spec/fixtures/native-addon/virtual-display/src/addon.mm
vendored
Normal file
197
spec/fixtures/native-addon/virtual-display/src/addon.mm
vendored
Normal file
@@ -0,0 +1,197 @@
|
||||
#include <js_native_api.h>
|
||||
#include <node_api.h>
|
||||
#include "VirtualDisplayBridge.h"
|
||||
|
||||
namespace {
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
int default_val;
|
||||
int* ptr;
|
||||
} PropertySpec;
|
||||
|
||||
// Helper function to get an integer property from an object
|
||||
bool GetIntProperty(napi_env env,
|
||||
napi_value object,
|
||||
const char* prop_name,
|
||||
int* result,
|
||||
int default_value) {
|
||||
*result = default_value;
|
||||
|
||||
bool has_prop;
|
||||
if (napi_has_named_property(env, object, prop_name, &has_prop) != napi_ok ||
|
||||
!has_prop) {
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value prop_value;
|
||||
if (napi_get_named_property(env, object, prop_name, &prop_value) != napi_ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (napi_get_value_int32(env, prop_value, result) != napi_ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper function to validate and parse object properties
|
||||
bool ParseObjectProperties(napi_env env,
|
||||
napi_value object,
|
||||
PropertySpec props[],
|
||||
size_t prop_count) {
|
||||
// Process all properties
|
||||
for (size_t i = 0; i < prop_count; i++) {
|
||||
if (!GetIntProperty(env, object, props[i].name, props[i].ptr,
|
||||
props[i].default_val)) {
|
||||
char error_msg[50];
|
||||
snprintf(error_msg, sizeof(error_msg), "%s must be a number",
|
||||
props[i].name);
|
||||
napi_throw_error(env, NULL, error_msg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for unknown properties
|
||||
napi_value prop_names;
|
||||
uint32_t count;
|
||||
napi_get_property_names(env, object, &prop_names);
|
||||
napi_get_array_length(env, prop_names, &count);
|
||||
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
napi_value prop_name;
|
||||
napi_get_element(env, prop_names, i, &prop_name);
|
||||
size_t len;
|
||||
char name[20];
|
||||
napi_get_value_string_utf8(env, prop_name, name, sizeof(name), &len);
|
||||
|
||||
bool found = false;
|
||||
for (size_t j = 0; j < prop_count; j++) {
|
||||
if (strcmp(name, props[j].name) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
napi_throw_error(env, NULL, "Object contains unknown properties");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// virtualDisplay.create()
|
||||
napi_value create(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 1;
|
||||
napi_value args[1];
|
||||
|
||||
if (napi_get_cb_info(env, info, &argc, args, NULL, NULL) != napi_ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int width = 1920, height = 1080, x = 0, y = 0;
|
||||
|
||||
PropertySpec props[] = {{"width", 1920, &width},
|
||||
{"height", 1080, &height},
|
||||
{"x", 0, &x},
|
||||
{"y", 0, &y}};
|
||||
|
||||
if (argc >= 1) {
|
||||
napi_valuetype valuetype;
|
||||
if (napi_typeof(env, args[0], &valuetype) != napi_ok) {
|
||||
napi_throw_error(env, NULL, "Failed to get argument type");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (valuetype == napi_object) {
|
||||
if (!ParseObjectProperties(env, args[0], props,
|
||||
sizeof(props) / sizeof(props[0]))) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
napi_throw_error(env, NULL, "Expected an object as the argument");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
NSInteger displayId = [VirtualDisplayBridge create:width
|
||||
height:height
|
||||
x:x
|
||||
y:y];
|
||||
|
||||
if (displayId == 0) {
|
||||
napi_throw_error(env, NULL, "Failed to create virtual display");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
napi_value result;
|
||||
if (napi_create_int64(env, displayId, &result) != napi_ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// virtualDisplay.forceCleanup()
|
||||
napi_value forceCleanup(napi_env env, napi_callback_info info) {
|
||||
BOOL result = [VirtualDisplayBridge forceCleanup];
|
||||
|
||||
napi_value js_result;
|
||||
if (napi_get_boolean(env, result, &js_result) != napi_ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return js_result;
|
||||
}
|
||||
|
||||
// virtualDisplay.destroy()
|
||||
napi_value destroy(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 1;
|
||||
napi_value args[1];
|
||||
|
||||
if (napi_get_cb_info(env, info, &argc, args, NULL, NULL) != napi_ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (argc < 1) {
|
||||
napi_throw_error(env, NULL, "Expected number argument");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int64_t displayId;
|
||||
if (napi_get_value_int64(env, args[0], &displayId) != napi_ok) {
|
||||
napi_throw_error(env, NULL, "Expected number argument");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL result = [VirtualDisplayBridge destroy:(NSInteger)displayId];
|
||||
|
||||
napi_value js_result;
|
||||
if (napi_get_boolean(env, result, &js_result) != napi_ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return js_result;
|
||||
}
|
||||
|
||||
napi_value Init(napi_env env, napi_value exports) {
|
||||
napi_property_descriptor descriptors[] = {
|
||||
{"create", NULL, create, NULL, NULL, NULL, napi_default, NULL},
|
||||
{"destroy", NULL, destroy, NULL, NULL, NULL, napi_default, NULL},
|
||||
{"forceCleanup", NULL, forceCleanup, NULL, NULL, NULL, napi_default,
|
||||
NULL}};
|
||||
|
||||
if (napi_define_properties(env, exports,
|
||||
sizeof(descriptors) / sizeof(*descriptors),
|
||||
descriptors) != napi_ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return exports;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
|
||||
@@ -24,6 +24,7 @@
|
||||
"@electron-ci/uv-dlopen": "file:./fixtures/native-addon/uv-dlopen/",
|
||||
"@electron-ci/osr-gpu": "file:./fixtures/native-addon/osr-gpu/",
|
||||
"@electron-ci/external-ab": "file:./fixtures/native-addon/external-ab/",
|
||||
"@electron-ci/virtual-display": "file:./fixtures/native-addon/virtual-display/",
|
||||
"@electron/fuses": "^1.8.0",
|
||||
"@electron/packager": "^18.3.2",
|
||||
"@types/sinon": "^9.0.4",
|
||||
|
||||
@@ -19,6 +19,12 @@
|
||||
"@electron-ci/uv-dlopen@file:./fixtures/native-addon/uv-dlopen":
|
||||
version "0.0.1"
|
||||
|
||||
"@electron-ci/virtual-display@file:./fixtures/native-addon/virtual-display":
|
||||
version "1.0.0"
|
||||
dependencies:
|
||||
bindings "^1.5.0"
|
||||
node-addon-api "^8.3.0"
|
||||
|
||||
"@electron/asar@^3.2.1", "@electron/asar@^3.2.7":
|
||||
version "3.2.10"
|
||||
resolved "https://registry.yarnpkg.com/@electron/asar/-/asar-3.2.10.tgz#615cf346b734b23cafa4e0603551010bd0e50aa8"
|
||||
@@ -517,7 +523,7 @@ binary-extensions@^2.0.0:
|
||||
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.3.0.tgz#f6e14a97858d327252200242d4ccfe522c445522"
|
||||
integrity sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==
|
||||
|
||||
bindings@^1.2.1:
|
||||
bindings@^1.2.1, bindings@^1.5.0:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
|
||||
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
|
||||
@@ -1892,6 +1898,11 @@ node-addon-api@8.0.0:
|
||||
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-8.0.0.tgz#5453b7ad59dd040d12e0f1a97a6fa1c765c5c9d2"
|
||||
integrity sha512-ipO7rsHEBqa9STO5C5T10fj732ml+5kLN1cAG8/jdHd56ldQeGj3Q7+scUS+VHK/qy1zLEwC4wMK5+yM0btPvw==
|
||||
|
||||
node-addon-api@^8.3.0:
|
||||
version "8.5.0"
|
||||
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-8.5.0.tgz#c91b2d7682fa457d2e1c388150f0dff9aafb8f3f"
|
||||
integrity sha512-/bRZty2mXUIFY/xU5HLvveNHlswNJej+RnxBjOMkidWfwZzgTbPG1E3K5TOxRLOR+5hX7bSofy8yf1hZevMS8A==
|
||||
|
||||
node-fetch@^2.6.7:
|
||||
version "2.7.0"
|
||||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
|
||||
|
||||
Reference in New Issue
Block a user