Skip to content

Commit 155f184

Browse files
committed
refactor: Update Window class to improve size and position handling with animation support
1 parent dfd4fea commit 155f184

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

examples/nswindow_example/main.mm

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
3232
[self.window center];
3333
[self.window makeKeyAndOrderFront:nil];
3434
[self.window makeMainWindow];
35-
[NSApp activateIgnoringOtherApps:YES];
3635

3736
// 延迟检查主窗口
3837
dispatch_async(dispatch_get_main_queue(), ^{
@@ -53,20 +52,6 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
5352
});
5453
displayManager.AddListener(&displayEventHandler);
5554

56-
std::shared_ptr<BroadcastCenter> broadcastCenter = std::make_shared<BroadcastCenter>();
57-
58-
BroadcastEventHandler broadcastEventHandler =
59-
BroadcastEventHandler([](const std::string& message) {
60-
std::cout << "Received broadcast: " << message << std::endl;
61-
});
62-
63-
broadcastCenter->RegisterReceiver("com.example.myNotification", &broadcastEventHandler);
64-
65-
// broadcastCenter.RegisterReceiver(
66-
// BroadcastEventHandler ([&](const std::string& message) {
67-
// std::cout << "Received broadcast: " << message << std::endl;
68-
// }));
69-
7055
[[NSNotificationCenter defaultCenter]
7156
addObserverForName:NSWindowDidBecomeMainNotification
7257
object:nil

src/window.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Window {
3232
// Color GetBackgroundColor() const;
3333
void SetBounds(Rectangle bounds);
3434
Rectangle GetBounds() const;
35-
void SetSize(Size size);
35+
void SetSize(Size size, bool animate);
3636
Size GetSize() const;
3737
void SetContentSize(Size size);
3838
Size GetContentSize() const;

src/window_macos.mm

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
void Window::Show() {
4242
[pimpl_->ns_window_ setIsVisible:YES];
43-
[pimpl_->ns_window_ makeKeyAndOrderFront:nil];
43+
[pimpl_->ns_window_ orderFrontRegardless];
4444
}
4545

4646
void Window::Hide() {
@@ -116,8 +116,16 @@
116116
return bounds;
117117
}
118118

119-
void Window::SetSize(Size size) {
120-
[pimpl_->ns_window_ setFrame:NSMakeRect(0, 0, size.width, size.height) display:YES];
119+
void Window::SetSize(Size size, bool animate) {
120+
NSRect frame = [pimpl_->ns_window_ frame];
121+
frame.origin.y += (frame.size.height - size.height);
122+
frame.size.width = size.width;
123+
frame.size.height = size.height;
124+
if (animate) {
125+
[[pimpl_->ns_window_ animator] setFrame:frame display:YES animate:YES];
126+
} else {
127+
[pimpl_->ns_window_ setFrame:frame display:YES];
128+
}
121129
}
122130

123131
Size Window::GetSize() const {
@@ -235,7 +243,11 @@
235243
}
236244

237245
void Window::SetPosition(Point point) {
238-
[pimpl_->ns_window_ setFrameOrigin:NSMakePoint(point.x, point.y)];
246+
NSRect screenFrameRect = [[NSScreen screens][0] frame];
247+
// Convert point to bottom left origin
248+
NSPoint bottomLeft =
249+
NSMakePoint(point.x, screenFrameRect.size.height - point.y - GetSize().height);
250+
[pimpl_->ns_window_ setFrameOrigin:bottomLeft];
239251
}
240252

241253
Point Window::GetPosition() {

0 commit comments

Comments
 (0)