mirror of
https://github.com/home-assistant/iOS.git
synced 2026-04-22 23:39:53 -05:00
## Summary
macOS users cannot dismiss "Add to Widget" flows, requiring force quit.
`NavigationView`/`NavigationStack` missing
`.navigationViewStyle(.stack)` - required for Catalyst dismissal
controls (iOS uses gestures).
**Changed files:**
- `WidgetCreationView.swift` - Added `.stack` style to both
`NavigationStack` (iOS 16+) and `NavigationView` branches
- `WidgetSelectionView.swift` - Added `.stack` style to `NavigationView`
```swift
NavigationView {
content
}
.navigationViewStyle(.stack) // ← Enables dismissal controls on macOS
```
Pattern already established in `MagicItemAddView`, `AssistView`,
`SettingsView`, and 20+ other views.
## Screenshots
N/A - Fixes dismissal behavior only, no visual changes
## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#
## Any other notes
Agent instructions specified this exact fix. 3 lines added across 2
files.
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Unable to Dismiss `Add to Widget` window</issue_title>
> <issue_description><!-- Please READ THIS FIRST
> If your issue relates to something not looking right on Home Assistant
within the Companion App, please check if the error is present in Safari
on macOS too. If the issue is also seen in Safari, please open an issue
on the frontend repo
(https://github.com/home-assistant/frontend/issues/new?labels=bug&template=BUG_REPORT.md)
instead -->
>
> **Device model, version and app version**
> <!-- Please include your mac's model and version as listed in 'About
This Mac'. Please also give the app version listed in the Home
Assistant>About Home Assistant, please include the number in brackets.
Safari can be found by launching it in Safari > About Safari. -->
>
> Model Name: MacBook M1
> macOS Version: 26.3
> App Version: 2026.2 (2026.2.1758)
> Safari Version: 26.3 (21623.2.7.11.6)
>
> **Home Assistant Core Version**
> 2026.2.2
>
> **Describe the bug**
> There doesn’t appear to be a way to dismiss the “Add to Widget” for
both windows using the macOS app. I have to force close the app to close
the window. This seems like an oversight as on iOS it uses gestures
which isn't supported here.
>
> **To Reproduce**
> - Select Entity more-info
> - Click Add to Widget
> - See result
>
> **Expected behavior**
> There should be a close button to get out of the window.
> **Screenshots**
>
> <img width="732" height="934" alt="Image"
src="https://github.com/user-attachments/assets/12f28cc5-02c9-43ed-88c5-f96d386c17e3"
/>
> <img width="732" height="934" alt="Image"
src="https://github.com/user-attachments/assets/1c0b9b0e-600e-46db-94cf-15c1fbbec6d5"
/>
>
> **Additional context**
> <!--Add any other context about the problem here.-->
> Unrelated, but I can't seem to be to delete the widgets on this screen
either.</issue_description>
>
> <agent_instructions>When adding widget from mac catalyst webview, we
are opening the widget screen in a NavigationStack/NavigationView
without navigationViewStyle .stack defined, update
that</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes home-assistant/iOS#4369
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
293 lines
11 KiB
Swift
293 lines
11 KiB
Swift
import Foundation
|
|
import PromiseKit
|
|
@preconcurrency import Shared
|
|
import SwiftUI
|
|
|
|
/// Handles the "Add To" functionality for Home Assistant entities, allowing users to add entities
|
|
/// to various iOS platform features and connected devices.
|
|
///
|
|
/// This class provides two main capabilities:
|
|
/// 1. Determining which actions are available for a given entity based on its type and domain
|
|
/// 2. Executing the selected action to add the entity to the chosen platform feature
|
|
final class EntityAddToHandler {
|
|
weak var webViewController: WebViewControllerProtocol?
|
|
|
|
init(webViewController: WebViewControllerProtocol? = nil) {
|
|
self.webViewController = webViewController
|
|
}
|
|
|
|
/// Returns the list of available actions for the specified entity.
|
|
///
|
|
/// The available actions depend on the entity's domain and current system state.
|
|
///
|
|
/// - Parameter entityId: The entity ID to get available actions for (e.g., "light.living_room")
|
|
/// - Returns: Promise that resolves to a list of actions that can be performed for this entity
|
|
func actionsForEntity(entityId: String) -> Promise<[any EntityAddToAction]> {
|
|
Promise { seal in
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
|
var actions: [any EntityAddToAction] = []
|
|
|
|
// Extract the domain from the entity ID
|
|
let domain = Domain(entityId: entityId)
|
|
|
|
// CarPlay is available on iPhone only (not iPad) for supported domains
|
|
#if !targetEnvironment(macCatalyst)
|
|
if !Current.isCatalyst, UIDevice.current.userInterfaceIdiom == .phone {
|
|
let isCarPlaySupported = domain.map { CarPlaySupportedDomains.all.contains($0) } ?? false
|
|
if isCarPlaySupported {
|
|
actions.append(CarPlayQuickAccessAction())
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// Watch is available on iPhone for supported domains
|
|
#if os(iOS)
|
|
if !Current.isCatalyst {
|
|
let isWatchSupported = domain.map { WatchSupportedDomains.all.contains($0) } ?? false
|
|
if isWatchSupported {
|
|
actions.append(WatchItemAction())
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// Widgets are available on all platforms
|
|
actions.append(CustomWidgetAction())
|
|
|
|
seal.fulfill(actions)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Executes the specified action to add the entity to the chosen platform feature.
|
|
///
|
|
/// This function performs the appropriate operation based on the action type.
|
|
///
|
|
/// - Parameters:
|
|
/// - action: The action to execute
|
|
/// - entityId: The entity ID to add (e.g., "light.living_room")
|
|
/// - Returns: Promise that resolves when the action is executed
|
|
func execute(action: any EntityAddToAction, entityId: String) -> Promise<Void> {
|
|
Promise { seal in
|
|
DispatchQueue.main.async { [weak self] in
|
|
guard let self else {
|
|
seal.reject(EntityAddToError.handlerDeallocated)
|
|
return
|
|
}
|
|
|
|
guard let webViewController else {
|
|
seal.reject(EntityAddToError.webViewControllerUnavailable)
|
|
return
|
|
}
|
|
|
|
let actionType = EntityAddToActionType(rawValue: action.actionType)
|
|
|
|
switch actionType {
|
|
case .carPlayQuickAccess:
|
|
addToCarPlayQuickAccess(entityId: entityId, webViewController: webViewController)
|
|
seal.fulfill(())
|
|
|
|
case .watchItem:
|
|
addToWatchItems(entityId: entityId, webViewController: webViewController)
|
|
seal.fulfill(())
|
|
|
|
case .customWidget:
|
|
openWidgetBuilder(
|
|
actionType: actionType,
|
|
entityId: entityId,
|
|
webViewController: webViewController
|
|
)
|
|
seal.fulfill(())
|
|
|
|
case .none:
|
|
seal.reject(EntityAddToError.unknownActionType)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Private Methods
|
|
|
|
private func addToCarPlayQuickAccess(entityId: String, webViewController: WebViewControllerProtocol) {
|
|
// Navigate to CarPlay configuration screen
|
|
Current.Log.info("Adding entity \(entityId) to CarPlay quick access")
|
|
let viewModel = CarPlayConfigurationViewModel(prefilledItem: .init(
|
|
id: entityId,
|
|
serverId: webViewController.server.identifier.rawValue,
|
|
type: .entity
|
|
))
|
|
let carPlaySettingsView = CarPlayConfigurationView(viewModel: viewModel)
|
|
webViewController.presentOverlayController(
|
|
controller: carPlaySettingsView.embeddedInHostingController(),
|
|
animated: true
|
|
)
|
|
}
|
|
|
|
private func addToWatchItems(entityId: String, webViewController: WebViewControllerProtocol) {
|
|
// Navigate to Watch configuration screen
|
|
Current.Log.info("Adding entity \(entityId) to Watch")
|
|
let viewModel = WatchConfigurationViewModel(prefilledItem: .init(
|
|
id: entityId,
|
|
serverId: webViewController.server.identifier.rawValue,
|
|
type: .entity
|
|
))
|
|
let watchSettingsView = WatchConfigurationView(needsNavigationController: true, viewModel: viewModel)
|
|
.preferredColorScheme(.dark)
|
|
let viewController = watchSettingsView.embeddedInHostingController()
|
|
viewController.overrideUserInterfaceStyle = .dark
|
|
webViewController.presentOverlayController(controller: viewController, animated: true)
|
|
}
|
|
|
|
private func openWidgetBuilder(
|
|
actionType: EntityAddToActionType?,
|
|
entityId: String,
|
|
webViewController: WebViewControllerProtocol
|
|
) {
|
|
Current.Log.info("Opening widget selection for entity \(entityId)")
|
|
|
|
let serverId = webViewController.server.identifier.rawValue
|
|
|
|
let selectionView = WidgetSelectionView(
|
|
entityId: entityId,
|
|
serverId: serverId
|
|
) { [weak self] selectedWidget in
|
|
self?.handleWidgetSelection(
|
|
widget: selectedWidget,
|
|
entityId: entityId,
|
|
serverId: serverId,
|
|
webViewController: webViewController
|
|
)
|
|
}
|
|
.modify { view in
|
|
if Current.isCatalyst {
|
|
view.toolbar(content: {
|
|
ToolbarItem(placement: .topBarLeading) {
|
|
CloseButton {
|
|
webViewController.dismissOverlayController(animated: true, completion: nil)
|
|
}
|
|
}
|
|
})
|
|
} else {
|
|
view
|
|
}
|
|
}
|
|
|
|
let hostingController = selectionView.embeddedInHostingController()
|
|
|
|
if Current.isCatalyst {
|
|
let navigationController = UINavigationController(rootViewController: hostingController)
|
|
webViewController.presentOverlayController(controller: navigationController, animated: true)
|
|
} else {
|
|
// Present as a bottom sheet
|
|
if let sheet = hostingController.sheetPresentationController {
|
|
sheet.detents = [.medium(), .large()]
|
|
sheet.prefersGrabberVisible = true
|
|
sheet.prefersScrollingExpandsWhenScrolledToEdge = false
|
|
}
|
|
webViewController.presentOverlayController(controller: hostingController, animated: true)
|
|
}
|
|
}
|
|
|
|
private func handleWidgetSelection(
|
|
widget: CustomWidget?,
|
|
entityId: String,
|
|
serverId: String,
|
|
webViewController: WebViewControllerProtocol
|
|
) {
|
|
// Small delay to allow the selection sheet to dismiss
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
|
|
if let widget {
|
|
// Add entity to existing widget
|
|
self.addEntityToWidget(
|
|
widget: widget,
|
|
entityId: entityId,
|
|
serverId: serverId,
|
|
webViewController: webViewController
|
|
)
|
|
} else {
|
|
// Create new widget with the entity pre-filled
|
|
self.createNewWidgetWithEntity(
|
|
entityId: entityId,
|
|
serverId: serverId,
|
|
webViewController: webViewController
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func addEntityToWidget(
|
|
widget: CustomWidget,
|
|
entityId: String,
|
|
serverId: String,
|
|
webViewController: WebViewControllerProtocol
|
|
) {
|
|
Current.Log.info("Adding entity \(entityId) to widget '\(widget.name)'")
|
|
|
|
// Create a new MagicItem for the entity
|
|
let newItem = MagicItem(
|
|
id: entityId,
|
|
serverId: serverId,
|
|
type: .entity
|
|
)
|
|
|
|
// Create updated widget with the new item
|
|
var updatedWidget = widget
|
|
updatedWidget.items.append(newItem)
|
|
|
|
// Save to database
|
|
do {
|
|
try Current.database().write { db in
|
|
try updatedWidget.update(db)
|
|
}
|
|
|
|
// Open the widget creation view to let user see and further customize
|
|
let widgetCreationView = WidgetCreationView(widget: updatedWidget) {
|
|
// Reload widgets after changes
|
|
}
|
|
let hostingController = widgetCreationView
|
|
.embeddedInHostingController()
|
|
|
|
webViewController.presentOverlayController(controller: hostingController, animated: true)
|
|
} catch {
|
|
Current.Log.error("Failed to add entity to widget: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
private func createNewWidgetWithEntity(
|
|
entityId: String,
|
|
serverId: String,
|
|
webViewController: WebViewControllerProtocol
|
|
) {
|
|
Current.Log.info("Creating new widget with entity \(entityId)")
|
|
|
|
// Create a new widget with the entity pre-filled
|
|
let newItem = MagicItem(
|
|
id: entityId,
|
|
serverId: serverId,
|
|
type: .entity
|
|
)
|
|
|
|
let newWidget = CustomWidget(
|
|
id: UUID().uuidString,
|
|
name: "",
|
|
items: [newItem]
|
|
)
|
|
|
|
let widgetCreationView = WidgetCreationView(widget: newWidget) {
|
|
// Reload widgets after changes
|
|
}
|
|
|
|
let hostingController = widgetCreationView
|
|
.embeddedInHostingController()
|
|
|
|
webViewController.presentOverlayController(controller: hostingController, animated: true)
|
|
}
|
|
}
|
|
|
|
// MARK: - Error Types
|
|
|
|
extension EntityAddToError {
|
|
static let handlerDeallocated = EntityAddToError.decodingFailed
|
|
static let webViewControllerUnavailable = EntityAddToError.decodingFailed
|
|
static let unknownActionType = EntityAddToError.invalidPayload
|
|
}
|