From 36cebb95c6b450b071962cc0d740a02ab77c0c42 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 6 Aug 2024 16:41:49 -0400 Subject: [PATCH] refactor: simplify window `moveAbove/moveTop` impl on macOS (#43201) refactor: simplify window moveAbove/moveTop impl on macOS Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- shell/browser/native_window_mac.mm | 58 ++---------------------------- 1 file changed, 2 insertions(+), 56 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 29c6fd59731c..774c2d8f2be4 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -117,50 +117,6 @@ struct Converter { namespace electron { -namespace { -// -[NSWindow orderWindow] does not handle reordering for children -// windows. Their order is fixed to the attachment order (the last attached -// window is on the top). Therefore, work around it by re-parenting in our -// desired order. -void ReorderChildWindowAbove(NSWindow* child_window, NSWindow* other_window) { - NSWindow* parent = [child_window parentWindow]; - DCHECK(parent); - - // `ordered_children` sorts children windows back to front. - NSArray* children = [[child_window parentWindow] childWindows]; - std::vector> ordered_children; - for (NSWindow* child in children) - ordered_children.push_back({[child orderedIndex], child}); - std::sort(ordered_children.begin(), ordered_children.end(), std::greater<>()); - - // If `other_window` is nullptr, place `child_window` in front of - // all other children windows. - if (other_window == nullptr) - other_window = ordered_children.back().second; - - if (child_window == other_window) - return; - - for (NSWindow* child in children) - [parent removeChildWindow:child]; - - const bool relative_to_parent = parent == other_window; - if (relative_to_parent) - [parent addChildWindow:child_window ordered:NSWindowAbove]; - - // Re-parent children windows in the desired order. - for (auto [ordered_index, child] : ordered_children) { - if (child != child_window && child != other_window) { - [parent addChildWindow:child ordered:NSWindowAbove]; - } else if (child == other_window && !relative_to_parent) { - [parent addChildWindow:other_window ordered:NSWindowAbove]; - [parent addChildWindow:child_window ordered:NSWindowAbove]; - } - } -} - -} // namespace - NativeWindowMac::NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent) : NativeWindow(options, parent), root_view_(new RootViewMac(this)) { @@ -786,22 +742,12 @@ bool NativeWindowMac::MoveAbove(const std::string& sourceId) { if (!webrtc::GetWindowOwnerPid(window_id)) return false; - if (!parent() || is_modal()) { - [window_ orderWindow:NSWindowAbove relativeTo:window_id]; - } else { - NSWindow* other_window = [NSApp windowWithWindowNumber:window_id]; - ReorderChildWindowAbove(window_, other_window); - } - + [window_ orderWindowByShuffling:NSWindowAbove relativeTo:window_id]; return true; } void NativeWindowMac::MoveTop() { - if (!parent() || is_modal()) { - [window_ orderWindow:NSWindowAbove relativeTo:0]; - } else { - ReorderChildWindowAbove(window_, nullptr); - } + [window_ orderWindowByShuffling:NSWindowAbove relativeTo:0]; } void NativeWindowMac::SetResizable(bool resizable) {