Widget.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // NextcloudWidget.swift
  3. // NextcloudWidget
  4. //
  5. // Created by Marino Faggiana on 20/08/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import WidgetKit
  24. import Intents
  25. import SwiftUI
  26. @main
  27. struct NextcloudWidgetBundle: WidgetBundle {
  28. @WidgetBundleBuilder
  29. var body: some Widget {
  30. DashboardWidget()
  31. FilesWidget()
  32. ToolbarWidget()
  33. LockscreenWidget()
  34. }
  35. }
  36. struct DashboardWidget: Widget {
  37. let kind: String = "DashboardWidget"
  38. var body: some WidgetConfiguration {
  39. IntentConfiguration(kind: kind, intent: DashboardIntent.self, provider: DashboardWidgetProvider()) { entry in
  40. DashboardWidgetView(entry: entry)
  41. }
  42. .supportedFamilies([.systemLarge])
  43. .configurationDisplayName("Dashboard")
  44. .description(NSLocalizedString("_description_dashboardwidget_", comment: ""))
  45. #if !targetEnvironment(simulator)
  46. .contentMarginsDisabled()
  47. #endif
  48. }
  49. }
  50. struct FilesWidget: Widget {
  51. let kind: String = "FilesWidget"
  52. var body: some WidgetConfiguration {
  53. IntentConfiguration(kind: kind, intent: AccountIntent.self, provider: FilesWidgetProvider()) { entry in
  54. FilesWidgetView(entry: entry)
  55. }
  56. .supportedFamilies([.systemLarge])
  57. .configurationDisplayName("Files")
  58. .description(NSLocalizedString("_description_fileswidget_", comment: ""))
  59. #if !targetEnvironment(simulator)
  60. .contentMarginsDisabled()
  61. #endif
  62. }
  63. }
  64. struct ToolbarWidget: Widget {
  65. let kind: String = "ToolbarWidget"
  66. var body: some WidgetConfiguration {
  67. StaticConfiguration(kind: kind, provider: ToolbarWidgetProvider()) { entry in
  68. ToolbarWidgetView(entry: entry)
  69. }
  70. .supportedFamilies([.systemMedium])
  71. .configurationDisplayName("Toolbar")
  72. .description(NSLocalizedString("_description_toolbarwidget_", comment: ""))
  73. #if !targetEnvironment(simulator)
  74. .contentMarginsDisabled()
  75. #endif
  76. }
  77. }
  78. struct LockscreenWidget: Widget {
  79. let kind: String = "LockscreenWidget"
  80. var body: some WidgetConfiguration {
  81. if #available(iOSApplicationExtension 16.0, *) {
  82. return IntentConfiguration(kind: kind, intent: AccountIntent.self, provider: LockscreenWidgetProvider()) { entry in
  83. LockscreenWidgetView(entry: entry)
  84. }
  85. .supportedFamilies([.accessoryRectangular, .accessoryCircular])
  86. .configurationDisplayName(NSLocalizedString("_title_lockscreenwidget_", comment: ""))
  87. .description(NSLocalizedString("_description_lockscreenwidget_", comment: ""))
  88. #if !targetEnvironment(simulator)
  89. .contentMarginsDisabled()
  90. #endif
  91. } else {
  92. return EmptyWidgetConfiguration()
  93. }
  94. }
  95. }
  96. extension View {
  97. func widgetBackground(_ backgroundView: some View) -> some View {
  98. #if !targetEnvironment(simulator)
  99. if #available(iOSApplicationExtension 17.0, *) {
  100. return containerBackground(for: .widget) {
  101. backgroundView
  102. }
  103. } else {
  104. return background(backgroundView)
  105. }
  106. #else
  107. return background(backgroundView)
  108. #endif
  109. }
  110. }