From e829af3e1862151df1a6b277633657bf64b79680 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Thu, 26 Nov 2026 15:24:29 +0100 Subject: [PATCH 1/2] fix: keep open menus from closing on background updates Menus closed by themselves when a transfer, sync or capability update refreshed the screen: a reload recreated the button that owns the menu, or code replaced the button's menu while it was open. The previous mitigation only covered the file list and relied on a global touch monitor and tap-gesture observers instead of the real menu lifecycle. NCMenuButton now tracks its own menu through the UIControl context-menu hooks and applies a replaced menu only after the menu closes. NCMenuPresentationMonitor counts open menus app-wide, so the file list, Trash and the share panel can hold back their reloads until the user has closed the menu. The navigation overflow menu is installed once and reads the latest options when opened, instead of being replaced on every reload. The custom UIApplication subclass is no longer needed, so the app starts through @main on AppDelegate. Fixes #4500 Assisted-by: Claude Code:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Marino Faggiana --- Nextcloud.xcodeproj/project.pbxproj | 16 ++- .../NCMenuPresentationMonitorTests.swift | 117 ++++++++++++++++++ iOSClient/AppDelegate.swift | 1 + .../Files/NCFilesNavigationController.swift | 13 +- .../Collection Common/Cell/NCGridCell.swift | 18 +-- .../Collection Common/Cell/NCGridCell.xib | 2 +- .../Collection Common/Cell/NCListCell.swift | 25 +--- .../Collection Common/Cell/NCListCell.xib | 2 +- .../Cell/NCRecommendationsCell.swift | 18 +-- .../Cell/NCRecommendationsCell.xib | 2 +- .../NCCollectionViewCommon+CellDelegate.swift | 6 - ...ionViewCommon+CollectionViewDelegate.swift | 15 +++ .../NCCollectionViewCommon.swift | 31 ++++- .../NCSectionFirstHeader.swift | 24 ++-- .../Main/NCMainNavigationController.swift | 30 ++++- iOSClient/Menu/NCMenuButton.swift | 98 +++++++++++++++ .../Menu/NCMenuPresentationMonitor.swift | 90 ++++++++++++++ iOSClient/NCGlobal.swift | 2 - iOSClient/Share/NCShare.swift | 14 ++- iOSClient/Share/NCShareCommentsCell.swift | 6 +- iOSClient/Share/NCShareCommentsCell.xib | 2 +- iOSClient/Share/NCShareLinkCell.swift | 2 +- iOSClient/Share/NCShareLinkCell.xib | 2 +- iOSClient/Share/NCShareUserCell.swift | 6 +- iOSClient/Share/NCShareUserCell.xib | 2 +- iOSClient/Trash/Cell/NCTrashGridCell.swift | 2 +- iOSClient/Trash/Cell/NCTrashGridCell.xib | 2 +- iOSClient/Trash/Cell/NCTrashListCell.swift | 2 +- iOSClient/Trash/Cell/NCTrashListCell.xib | 2 +- iOSClient/Trash/NCTrash+Networking.swift | 4 +- iOSClient/Trash/NCTrash.swift | 42 ++++--- iOSClient/main.swift | 80 ------------ 32 files changed, 464 insertions(+), 214 deletions(-) create mode 100644 Tests/NextcloudUnitTests/NCMenuPresentationMonitorTests.swift create mode 100644 iOSClient/Menu/NCMenuButton.swift create mode 100644 iOSClient/Menu/NCMenuPresentationMonitor.swift delete mode 100644 iOSClient/main.swift diff --git a/Nextcloud.xcodeproj/project.pbxproj b/Nextcloud.xcodeproj/project.pbxproj index 3cd942a24e..b7ed851831 100644 --- a/Nextcloud.xcodeproj/project.pbxproj +++ b/Nextcloud.xcodeproj/project.pbxproj @@ -378,6 +378,9 @@ F72EC7242F45C81600A2135C /* NotificationCenter+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72EC7232F45C81600A2135C /* NotificationCenter+Extension.swift */; }; F72EC7262F45C91E00A2135C /* NCContextMenuNavigation.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72EC7252F45C90600A2135C /* NCContextMenuNavigation.swift */; }; F72EC7282F45FF1400A2135C /* NCContextMenuPlus.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72EC7272F45FF0600A2135C /* NCContextMenuPlus.swift */; }; + 4E3A0C13000D000100A00002 /* NCMenuButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E3A0C13000D000100A00001 /* NCMenuButton.swift */; }; + 4E3A0C13000D000100A00004 /* NCMenuPresentationMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E3A0C13000D000100A00003 /* NCMenuPresentationMonitor.swift */; }; + 4E3A0C13000D000100A00006 /* NCMenuPresentationMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E3A0C13000D000100A00005 /* NCMenuPresentationMonitorTests.swift */; }; F72EC7292F4617F600A2135C /* NotificationCenter+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72EC7232F45C81600A2135C /* NotificationCenter+Extension.swift */; }; F72FD3B5297ED49A00075D28 /* NCManageDatabase+E2EE.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72FD3B4297ED49A00075D28 /* NCManageDatabase+E2EE.swift */; }; F72FD3B6297ED49A00075D28 /* NCManageDatabase+E2EE.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72FD3B4297ED49A00075D28 /* NCManageDatabase+E2EE.swift */; }; @@ -953,7 +956,6 @@ F7C9B9232B582F550064EA91 /* NCManageDatabase+SecurityGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7C9B91C2B582F550064EA91 /* NCManageDatabase+SecurityGuard.swift */; }; F7CADEFD2EA159210057849E /* NCMetadataUploadTranfersSuccess.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7CADEFA2EA1591D0057849E /* NCMetadataUploadTranfersSuccess.swift */; }; F7CAFE182F164B9500DB35A5 /* NCCollectionViewCommon+CellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7CAFE172F164B9200DB35A5 /* NCCollectionViewCommon+CellDelegate.swift */; }; - F7CAFE1B2F16AA8D00DB35A5 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7CAFE1A2F16AA8600DB35A5 /* main.swift */; }; F7CAFE1D2F17A35F00DB35A5 /* ProgressQuantizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7CAFE1C2F17A34F00DB35A5 /* ProgressQuantizer.swift */; }; F7CAFE1E2F17A37C00DB35A5 /* ProgressQuantizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7CAFE1C2F17A34F00DB35A5 /* ProgressQuantizer.swift */; }; F7CAFE1F2F17A37C00DB35A5 /* ProgressQuantizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7CAFE1C2F17A34F00DB35A5 /* ProgressQuantizer.swift */; }; @@ -1594,6 +1596,9 @@ F72EC7232F45C81600A2135C /* NotificationCenter+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NotificationCenter+Extension.swift"; sourceTree = ""; }; F72EC7252F45C90600A2135C /* NCContextMenuNavigation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCContextMenuNavigation.swift; sourceTree = ""; }; F72EC7272F45FF0600A2135C /* NCContextMenuPlus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCContextMenuPlus.swift; sourceTree = ""; }; + 4E3A0C13000D000100A00001 /* NCMenuButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMenuButton.swift; sourceTree = ""; }; + 4E3A0C13000D000100A00003 /* NCMenuPresentationMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMenuPresentationMonitor.swift; sourceTree = ""; }; + 4E3A0C13000D000100A00005 /* NCMenuPresentationMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMenuPresentationMonitorTests.swift; sourceTree = ""; }; F72FD3B4297ED49A00075D28 /* NCManageDatabase+E2EE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCManageDatabase+E2EE.swift"; sourceTree = ""; }; F7313A71302F0A15000C8E98 /* NCRichWorkspaceCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCRichWorkspaceCoordinator.swift; sourceTree = ""; }; F7320934201B812F008A0888 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ko; path = ko.lproj/Localizable.strings; sourceTree = ""; }; @@ -2030,7 +2035,6 @@ F7C9B91C2B582F550064EA91 /* NCManageDatabase+SecurityGuard.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NCManageDatabase+SecurityGuard.swift"; sourceTree = ""; }; F7CADEFA2EA1591D0057849E /* NCMetadataUploadTranfersSuccess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMetadataUploadTranfersSuccess.swift; sourceTree = ""; }; F7CAFE172F164B9200DB35A5 /* NCCollectionViewCommon+CellDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCCollectionViewCommon+CellDelegate.swift"; sourceTree = ""; }; - F7CAFE1A2F16AA8600DB35A5 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; F7CAFE1C2F17A34F00DB35A5 /* ProgressQuantizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgressQuantizer.swift; sourceTree = ""; }; F7CB77632F5843D700DE649A /* UIFont+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Extension.swift"; sourceTree = ""; }; F7CBC1212BAC8B0000EC1D55 /* NCSectionFirstHeaderEmptyData.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NCSectionFirstHeaderEmptyData.xib; sourceTree = ""; }; @@ -2282,6 +2286,8 @@ AF93471127E2341B002537EE /* NCContextMenuShare.swift */, F75D19E225EFE09000D74598 /* NCContextMenuTrash.swift */, F710D2012405826100A6033D /* NCContextMenuViewer.swift */, + 4E3A0C13000D000100A00001 /* NCMenuButton.swift */, + 4E3A0C13000D000100A00003 /* NCMenuPresentationMonitor.swift */, ); path = Menu; sourceTree = ""; @@ -2317,6 +2323,7 @@ C0DEDA7E30C1000100A00001 /* NCAutoUploadDateTests.swift */, 4463A0C1300D000100A00003 /* NCAutoUploadKnownFilesTests.swift */, 4437B0C1300D000100A00001 /* NCBackupExclusionTests.swift */, + 4E3A0C13000D000100A00005 /* NCMenuPresentationMonitorTests.swift */, 320C20803A58DAF7575440A8 /* NKErrorUserFriendlyMessageTests.swift */, C0DEA070306D000100000007 /* NCAutoUploadAssetTests.swift */, F76BED7B306D0000002E1442 /* NCPhotoLibraryAssetTests.swift */, @@ -3801,7 +3808,6 @@ F7BDC1DD300F4FF800C5D9FA /* Processor */, F7BDC1DE300F502C00C5D9FA /* Refresh */, F702F2CC25EE5B4F008F8E80 /* AppDelegate.swift */, - F7CAFE1A2F16AA8600DB35A5 /* main.swift */, F794E13E2BBC0F70003693D7 /* SceneDelegate.swift */, F7CF067A2E0FF38F0063AD04 /* NCAppStateManager.swift */, F77DD6A72C5CC093009448FB /* NCSession.swift */, @@ -4698,6 +4704,7 @@ C0DEDA7E30C1000100A00002 /* NCAutoUploadDateTests.swift in Sources */, 4463A0C1300D000100A00004 /* NCAutoUploadKnownFilesTests.swift in Sources */, 4437B0C1300D000100A00002 /* NCBackupExclusionTests.swift in Sources */, + 4E3A0C13000D000100A00006 /* NCMenuPresentationMonitorTests.swift in Sources */, C0DEA070306D000100000008 /* NCAutoUploadAssetTests.swift in Sources */, F76BED7E306D0000002E1442 /* NCPhotoLibraryAssetTests.swift in Sources */, A1E2EE01000000000000000A /* NCEndToEndKeySetArchiveTests.swift in Sources */, @@ -5122,6 +5129,8 @@ AFCE353727E4ED7B00FEA6C2 /* NCShareCells.swift in Sources */, F75A9EE623796C6F0044CFCE /* NCNetworking.swift in Sources */, F72EC7282F45FF1400A2135C /* NCContextMenuPlus.swift in Sources */, + 4E3A0C13000D000100A00002 /* NCMenuButton.swift in Sources */, + 4E3A0C13000D000100A00004 /* NCMenuPresentationMonitor.swift in Sources */, AA8D31552D41052300FE2775 /* NCManageDatabase+DownloadLimit.swift in Sources */, F758B460212C56A400515F55 /* NCScan.swift in Sources */, F76882262C0DD1E7001CF441 /* NCSettingsView.swift in Sources */, @@ -5288,7 +5297,6 @@ F73EF7DF2B02266D0087E6E9 /* NCManageDatabase+Trash.swift in Sources */, F79B646026CA661600838ACA /* UIControl+Extension.swift in Sources */, F768823E2C0DD305001CF441 /* LazyView.swift in Sources */, - F7CAFE1B2F16AA8D00DB35A5 /* main.swift in Sources */, F3E173B02C9AF637006D177A /* ScreenAwakeManager.swift in Sources */, F764C3E52FFB7DFA00029FD5 /* NCManageDatabase+MediaMetadataBackfill.swift in Sources */, F747EB0D2C4AC1FF00F959A8 /* NCCollectionViewCommon+CollectionViewDelegateFlowLayout.swift in Sources */, diff --git a/Tests/NextcloudUnitTests/NCMenuPresentationMonitorTests.swift b/Tests/NextcloudUnitTests/NCMenuPresentationMonitorTests.swift new file mode 100644 index 0000000000..6938d68e3e --- /dev/null +++ b/Tests/NextcloudUnitTests/NCMenuPresentationMonitorTests.swift @@ -0,0 +1,117 @@ +// SPDX-FileCopyrightText: Nextcloud GmbH +// SPDX-FileCopyrightText: 2026 Marino Faggiana +// SPDX-License-Identifier: GPL-3.0-or-later + +import Foundation +import Testing +@testable import Nextcloud + +@Suite("Menu presentation monitor") +@MainActor +struct NCMenuPresentationMonitorTests { + /// Records the monitor notifications posted on a private notification center. + private final class Recorder: NSObject { + let center = NotificationCenter() + private(set) var names: [Notification.Name] = [] + + override init() { + super.init() + for name in [NCMenuPresentationMonitor.menuWillShowNotification, NCMenuPresentationMonitor.menuDidHideNotification] { + center.addObserver(self, selector: #selector(record(_:)), name: name, object: nil) + } + } + + @objc private func record(_ notification: Notification) { + names.append(notification.name) + } + } + + @Test("No menu is visible at start") + func startsHidden() { + let monitor = NCMenuPresentationMonitor(notificationCenter: NotificationCenter()) + + #expect(!monitor.isMenuVisible) + } + + @Test("Opening and closing one menu posts show and hide once") + func singleMenu() { + let recorder = Recorder() + let monitor = NCMenuPresentationMonitor(notificationCenter: recorder.center) + let owner = NSObject() + + monitor.menuWillShow(owner) + #expect(monitor.isMenuVisible) + + monitor.menuDidHide(owner) + #expect(!monitor.isMenuVisible) + + #expect(recorder.names == [NCMenuPresentationMonitor.menuWillShowNotification, NCMenuPresentationMonitor.menuDidHideNotification]) + } + + @Test("Hide is posted only when the last open menu closes") + func overlappingMenus() { + let recorder = Recorder() + let monitor = NCMenuPresentationMonitor(notificationCenter: recorder.center) + let first = NSObject() + let second = NSObject() + + monitor.menuWillShow(first) + monitor.menuWillShow(second) + monitor.menuDidHide(first) + #expect(monitor.isMenuVisible) + + monitor.menuDidHide(second) + #expect(!monitor.isMenuVisible) + + #expect(recorder.names == [NCMenuPresentationMonitor.menuWillShowNotification, NCMenuPresentationMonitor.menuDidHideNotification]) + } + + @Test("Deferred actions run immediately when no menu is open") + func actionRunsImmediately() { + let monitor = NCMenuPresentationMonitor(notificationCenter: NotificationCenter()) + var runs = 0 + + monitor.performWhenNoMenuVisible { runs += 1 } + + #expect(runs == 1) + } + + @Test("Deferred actions wait for the last menu to close and run in order") + func actionsWaitForLastMenu() { + let monitor = NCMenuPresentationMonitor(notificationCenter: NotificationCenter()) + let first = NSObject() + let second = NSObject() + var runs: [Int] = [] + + monitor.menuWillShow(first) + monitor.menuWillShow(second) + monitor.performWhenNoMenuVisible { runs.append(1) } + monitor.performWhenNoMenuVisible { runs.append(2) } + + monitor.menuDidHide(first) + #expect(runs.isEmpty) + + monitor.menuDidHide(second) + #expect(runs == [1, 2]) + + monitor.menuWillShow(first) + monitor.menuDidHide(first) + #expect(runs == [1, 2]) + } + + @Test("Repeated or unmatched calls are ignored") + func unbalancedCalls() { + let recorder = Recorder() + let monitor = NCMenuPresentationMonitor(notificationCenter: recorder.center) + let owner = NSObject() + + monitor.menuDidHide(owner) + monitor.menuWillShow(owner) + monitor.menuWillShow(owner) + monitor.menuDidHide(owner) + monitor.menuDidHide(owner) + + #expect(!monitor.isMenuVisible) + #expect(recorder.names == [NCMenuPresentationMonitor.menuWillShowNotification, NCMenuPresentationMonitor.menuDidHideNotification]) + } +} diff --git a/iOSClient/AppDelegate.swift b/iOSClient/AppDelegate.swift index b284185865..6644dec8e6 100644 --- a/iOSClient/AppDelegate.swift +++ b/iOSClient/AppDelegate.swift @@ -14,6 +14,7 @@ import SwiftUI import RealmSwift import Photos +@main class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { var backgroundSessionCompletionHandler: (() -> Void)? var isUiTestingEnabled: Bool { diff --git a/iOSClient/Files/NCFilesNavigationController.swift b/iOSClient/Files/NCFilesNavigationController.swift index df7a32f05f..ce5444558d 100644 --- a/iOSClient/Files/NCFilesNavigationController.swift +++ b/iOSClient/Files/NCFilesNavigationController.swift @@ -70,15 +70,6 @@ class NCFilesNavigationController: NCMainNavigationController { } let image = utility.loadUserImage(for: tableAccount.user, displayName: tableAccount.displayName, urlBase: tableAccount.urlBase) - class AccountSwitcherButton: UIButton { - var onMenuOpened: (() -> Void)? - - override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willDisplayMenuFor configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) { - super.contextMenuInteraction(interaction, willDisplayMenuFor: configuration, animator: animator) - onMenuOpened?() - } - } - @MainActor func createLeftMenu() async -> UIMenu? { var childrenAccountSubmenu: [UIMenuElement] = [] @@ -145,7 +136,7 @@ class NCFilesNavigationController: NCMainNavigationController { let leftItems = groupedItems.isEmpty ? navigationItem?.leftBarButtonItems ?? [] : groupedItems let accountItem = leftItems.first { $0.customView?.accessibilityIdentifier == "accountSwitcher" } if accountItem == nil { - let accountButton = AccountSwitcherButton(type: .custom) + let accountButton = NCMenuButton(type: .custom) accountButton.accessibilityIdentifier = "accountSwitcher" accountButton.setImage(image, for: .normal) @@ -155,7 +146,7 @@ class NCFilesNavigationController: NCMainNavigationController { accountButton.menu = await createLeftMenu() accountButton.showsMenuAsPrimaryAction = true - accountButton.onMenuOpened = { + accountButton.onMenuWillShow = { self.collectionViewCommon?.dismissTip() } diff --git a/iOSClient/Main/Collection Common/Cell/NCGridCell.swift b/iOSClient/Main/Collection Common/Cell/NCGridCell.swift index 7213f941ec..5077dddb22 100644 --- a/iOSClient/Main/Collection Common/Cell/NCGridCell.swift +++ b/iOSClient/Main/Collection Common/Cell/NCGridCell.swift @@ -8,11 +8,10 @@ import NextcloudKit import RealmSwift protocol NCGridCellDelegate: AnyObject { - func onMenuIntent(with metadata: tableMetadata?) func openContextMenu(with metadata: tableMetadata?, button: UIButton, sender: Any) } -class NCGridCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellMainProtocol { +class NCGridCell: UICollectionViewCell, NCCellMainProtocol { @IBOutlet weak var imageItem: UIImageView! @IBOutlet weak var imageSelect: UIImageView! @IBOutlet weak var imageStatus: UIImageView! @@ -24,7 +23,7 @@ class NCGridCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellMainP @IBOutlet weak var labelSubinfo: UILabel! @IBOutlet weak var labelExtension: UILabel! - @IBOutlet weak var buttonMore: UIButton! + @IBOutlet weak var buttonMore: NCMenuButton! @IBOutlet weak var imageVisualEffect: UIVisualEffectView! @IBOutlet weak var iconsStackView: UIStackView! @@ -57,11 +56,6 @@ class NCGridCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellMainP override func awakeFromNib() { super.awakeFromNib() - let tapObserver = UITapGestureRecognizer(target: self, action: #selector(handleTapObserver(_:))) - tapObserver.cancelsTouchesInView = false - tapObserver.delegate = self - contentView.addGestureRecognizer(tapObserver) - initCell() } @@ -146,14 +140,6 @@ class NCGridCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellMainP return nil } - @objc private func handleTapObserver(_ g: UITapGestureRecognizer) { - let location = g.location(in: contentView) - - if buttonMore.frame.contains(location) { - delegate?.onMenuIntent(with: metadata) - } - } - func setButtonMore(image: UIImage) { buttonMore.setImage(image, for: .normal) } diff --git a/iOSClient/Main/Collection Common/Cell/NCGridCell.xib b/iOSClient/Main/Collection Common/Cell/NCGridCell.xib index 7cb8863c41..98d85fd044 100644 --- a/iOSClient/Main/Collection Common/Cell/NCGridCell.xib +++ b/iOSClient/Main/Collection Common/Cell/NCGridCell.xib @@ -92,7 +92,7 @@ - -