Skip to content

Commit 0a55ff5

Browse files
ahkohdsentialx
authored andcommitted
feature: MacOS maximize (#19)
* Fix setWindowMinimized issue * Resolve setWindowMinimized to two params * Code clean up * Implement maximize window support for MacOS * Safety check * Clean up * Update Doc
1 parent c18a80d commit 0a55ff5

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Minimizes the window.
123123

124124
Restores the window.
125125

126-
#### win.maximize() `Windows`
126+
#### win.maximize() `Windows` `macOS`
127127

128128
Maximizes the window.
129129

example.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ console.log(window.getBounds());
1313
console.timeEnd("getBounds");
1414

1515
console.time("setBounds");
16-
window.setBounds({ x: 0, y: 0 });
16+
// window.setBounds({ x: 0, y: 0 });
17+
window.maximize();
18+
1719
console.timeEnd("setBounds");
1820

1921
console.log("[info]: Visible Windows List");

lib/macos.mm

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,29 @@ AXUIElementRef getAXWindow(int pid, int handle) {
185185
return Napi::Boolean::New(env, true);
186186
}
187187

188+
Napi::Boolean setWindowMaximized(const Napi::CallbackInfo &info) {
189+
Napi::Env env{info.Env()};
190+
auto handle = info[0].As<Napi::Number>().Int32Value();
191+
auto win = m[handle];
192+
193+
if(win) {
194+
NSRect screenSizeRect = [[NSScreen mainScreen] frame];
195+
int screenWidth = screenSizeRect.size.width;
196+
int screenHeight = screenSizeRect.size.height;
197+
198+
NSPoint point = NSMakePoint((CGFloat) 0, (CGFloat) 0);
199+
NSSize size = NSMakeSize((CGFloat) screenWidth, (CGFloat) screenHeight);
200+
201+
CFTypeRef positionStorage = (CFTypeRef)(AXValueCreate((AXValueType)kAXValueCGPointType, (const void *)&point));
202+
AXUIElementSetAttributeValue(win, kAXPositionAttribute, positionStorage);
203+
204+
CFTypeRef sizeStorage = (CFTypeRef)(AXValueCreate((AXValueType)kAXValueCGSizeType, (const void *)&size));
205+
AXUIElementSetAttributeValue(win, kAXSizeAttribute, sizeStorage);
206+
}
207+
208+
return Napi::Boolean::New(env, true);
209+
}
210+
188211
Napi::Object Init(Napi::Env env, Napi::Object exports) {
189212
exports.Set(Napi::String::New(env, "getWindows"),
190213
Napi::Function::New(env, getWindows));
@@ -198,7 +221,8 @@ AXUIElementRef getAXWindow(int pid, int handle) {
198221
Napi::Function::New(env, bringWindowToTop));
199222
exports.Set(Napi::String::New(env, "setWindowMinimized"),
200223
Napi::Function::New(env, setWindowMinimized));
201-
224+
exports.Set(Napi::String::New(env, "setWindowMaximized"),
225+
Napi::Function::New(env, setWindowMaximized));
202226
return exports;
203227
}
204228

src/classes/window.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ export class Window {
124124
}
125125

126126
maximize() {
127-
if (!addon || !addon.showWindow) return;
128-
addon.showWindow(this.id, "maximize");
127+
if(platform() === "win32") {
128+
if (!addon || !addon.showWindow) return;
129+
addon.showWindow(this.id, "maximize");
130+
} else if(platform() === "darwin") {
131+
if (!addon) return;
132+
addon.setWindowMaximized(this.id);
133+
}
129134
}
130135

131136
bringToTop() {

0 commit comments

Comments
 (0)