Widget.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. .contentMarginsDisabled()
  46. }
  47. }
  48. struct FilesWidget: Widget {
  49. let kind: String = "FilesWidget"
  50. var body: some WidgetConfiguration {
  51. IntentConfiguration(kind: kind, intent: AccountIntent.self, provider: FilesWidgetProvider()) { entry in
  52. FilesWidgetView(entry: entry)
  53. }
  54. .supportedFamilies([.systemLarge])
  55. .configurationDisplayName("Files")
  56. .description(NSLocalizedString("_description_fileswidget_", comment: ""))
  57. .contentMarginsDisabled()
  58. }
  59. }
  60. struct ToolbarWidget: Widget {
  61. let kind: String = "ToolbarWidget"
  62. var body: some WidgetConfiguration {
  63. StaticConfiguration(kind: kind, provider: ToolbarWidgetProvider()) { entry in
  64. ToolbarWidgetView(entry: entry)
  65. }
  66. .supportedFamilies([.systemMedium])
  67. .configurationDisplayName("Toolbar")
  68. .description(NSLocalizedString("_description_toolbarwidget_", comment: ""))
  69. .contentMarginsDisabled()
  70. }
  71. }
  72. struct LockscreenWidget: Widget {
  73. let kind: String = "LockscreenWidget"
  74. var body: some WidgetConfiguration {
  75. if #available(iOSApplicationExtension 16.0, *) {
  76. return IntentConfiguration(kind: kind, intent: AccountIntent.self, provider: LockscreenWidgetProvider()) { entry in
  77. LockscreenWidgetView(entry: entry)
  78. }
  79. .supportedFamilies([.accessoryRectangular, .accessoryCircular])
  80. .configurationDisplayName(NSLocalizedString("_title_lockscreenwidget_", comment: ""))
  81. .description(NSLocalizedString("_description_lockscreenwidget_", comment: ""))
  82. .contentMarginsDisabled()
  83. } else {
  84. return EmptyWidgetConfiguration()
  85. }
  86. }
  87. }
  88. extension View {
  89. func widgetBackground(_ backgroundView: some View) -> some View {
  90. if #available(iOSApplicationExtension 17.0, *) {
  91. return containerBackground(for: .widget) {
  92. backgroundView
  93. }
  94. } else {
  95. return background(backgroundView)
  96. }
  97. }
  98. }