TUIThemeManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // TUIThemeManager.h
  3. // TUICore
  4. //
  5. // Created by harvy on 2022/1/5.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. /**
  9. *
  10. * Storage structure of theme for each module
  11. * | The root path of the theme resource(ThemeResourcePath)
  12. * |
  13. * |------ Theme one(named with theme identifier)
  14. * | |
  15. * | |------- manifest.plist (configuration information for the theme)
  16. * | |------- resource
  17. * |-------Theme two
  18. * | |
  19. * | |------ manifest.plist (configuration information for the theme)
  20. * | |------ resource
  21. */
  22. #import <Foundation/Foundation.h>
  23. #import "TUIDefine.h"
  24. @class TUITheme;
  25. @class TUIThemeManager;
  26. NS_ASSUME_NONNULL_BEGIN
  27. #define TUIShareThemeManager TUIThemeManager.shareManager
  28. /**
  29. * Notifications when the app's theme has changed
  30. */
  31. #define TUIDidApplyingThemeChangedNotfication @"TUIDidApplyingThemeChangedNotfication"
  32. #define TUIDidApplyingThemeChangedNotficationThemeKey @"TUIDidApplyingThemeChangedNotficationThemeKey"
  33. #define TUIDidApplyingThemeChangedNotficationModuleKey @"TUIDidApplyingThemeChangedNotficationModuleKey"
  34. /**
  35. * Register the theme resource root path of the module
  36. */
  37. #define TUIRegisterThemeResourcePath(path, module) [TUIShareThemeManager registerThemeResourcePath:path forModule:module];
  38. /**
  39. * Get the theme used by the module
  40. */
  41. #define TUICurrentTheme(module) [TUIShareThemeManager currentThemeForModule:module]
  42. /**
  43. * Get the dark night theme of the module
  44. */
  45. #define TUIDarkTheme(module) [TUIShareThemeManager darkThemeForModule:module]
  46. /**
  47. * Get dynamic colors that change with theme
  48. */
  49. #define TUIDynamicColor(colorKey, themeModule, defaultHex) [TUITheme dynamicColor:colorKey module:themeModule defaultColor:defaultHex]
  50. #define TUIDemoDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleDemo, defaultHex)
  51. #define TUICoreDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleCore, defaultHex)
  52. #define TUIChatDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleChat, defaultHex)
  53. #define TUIConversationDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleConversation, defaultHex)
  54. #define TUIConversationGroupDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleConversationGroup, defaultHex)
  55. #define TUIContactDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleContact, defaultHex)
  56. #define TUISearchDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleSearch, defaultHex)
  57. #define TUICallKitDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleCalling, defaultHex)
  58. #define TUIPollDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModulePoll, defaultHex)
  59. #define TUIGroupNoteDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleGroupNote, defaultHex)
  60. #define TIMCommonDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleTIMCommon, defaultHex)
  61. #define TUITranslationDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleTranslation, defaultHex)
  62. #define TUIVoiceToTextDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleVoiceToText, defaultHex)
  63. #define TUICustomerServicePluginDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleCustomerService, defaultHex)
  64. #define TUIMultimediaPluginDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleMultimedia, defaultHex)
  65. /**
  66. * Get dynamic image that change with theme
  67. */
  68. #define TUIDemoBundleThemeImage(imageKey, defaultImageName) TUIDemoDynamicImage(imageKey, TUIDemoCommonBundleImage(defaultImageName))
  69. #define TUICoreBundleThemeImage(imageKey, defaultImageName) TUICoreDynamicImage(imageKey, TUICoreCommonBundleImage(defaultImageName))
  70. #define TUIChatBundleThemeImage(imageKey, defaultImageName) TUIChatDynamicImage(imageKey, TUIChatCommonBundleImage(defaultImageName))
  71. #define TUIConversationBundleThemeImage(imageKey, defaultImageName) TUIConversationDynamicImage(imageKey, TUIConversationCommonBundleImage(defaultImageName))
  72. #define TUIContactBundleThemeImage(imageKey, defaultImageName) TUIContactDynamicImage(imageKey, TUIContactCommonBundleImage(defaultImageName))
  73. #define TUISearchBundleThemeImage(imageKey, defaultImageName) TUISearchDynamicImage(imageKey, TUISearchCommonBundleImage(defaultImageName))
  74. #define TUICallingBundleThemeImage(imageKey, defaultImageName) TUICallingDynamicImage(imageKey, TUICallingCommonBundleImage(defaultImageName))
  75. #define TUIPollBundleThemeImage(imageKey, defaultImageName) TUIPollDynamicImage(imageKey, TUIPollCommonBundleImage(defaultImageName))
  76. #define TUIGroupNoteBundleThemeImage(imageKey, defaultImageName) TUIGroupNoteDynamicImage(imageKey, TUIGroupNoteCommonBundleImage(defaultImageName))
  77. #define TIMCommonBundleThemeImage(imageKey, defaultImageName) TIMCommonDynamicImage(imageKey, TIMCommonBundleImage(defaultImageName))
  78. #define TUITranslationBundleThemeImage(imageKey, defaultImageName) TUITranslationDynamicImage(imageKey, TUITranslationCommonBundleImage(defaultImageName))
  79. #define TUIVoiceToTextBundleThemeImage(imageKey, defaultImageName) TUIVoiceToTextDynamicImage(imageKey, TUIVoiceToTextCommonBundleImage(defaultImageName))
  80. #define TUICustomerServicePluginBundleThemeImage(imageKey,defaultImageName) \
  81. TUICustomerServicePluginDynamicImage(imageKey,TUICustomerServicePluginCommonBundleImage(defaultImageName))
  82. #define TUIMultimediaPluginBundleThemeImage(imageKey, defaultImageName) \
  83. TUIMultimediaPluginDynamicImage(imageKey, TUIMultimediaPluginCommonBundleImage(defaultImageName))
  84. #define TUIDynamicImage(imageKey, themeModule, defaultImg) [TUITheme dynamicImage:imageKey module:themeModule defaultImage:defaultImg]
  85. #define TUIDemoDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleDemo, defaultImg)
  86. #define TUICoreDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleCore, defaultImg)
  87. #define TUIChatDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleChat, defaultImg)
  88. #define TUIConversationDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleConversation, defaultImg)
  89. #define TUIConversationGroupDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleConversationGroup, defaultImg)
  90. #define TUIContactDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleContact, defaultImg)
  91. #define TUISearchDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleSearch, defaultImg)
  92. #define TUICallingDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleCalling, defaultImg)
  93. #define TUIPollDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModulePoll, defaultImg)
  94. #define TUIGroupNoteDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleGroupNote, defaultImg)
  95. #define TIMCommonDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleTIMCommon, defaultImg)
  96. #define TUITranslationDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleTranslation, defaultImg)
  97. #define TUIVoiceToTextDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleVoiceToText, defaultImg)
  98. #define TUICustomerServicePluginDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleCustomerService, defaultImg)
  99. #define TUIMultimediaPluginDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleMultimedia, defaultImg)
  100. #define __TUIDefaultBundleImage(imageBundlePath) [UIImage imageWithContentsOfFile:imageBundlePath]
  101. #define TUIDemoCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIDemoImagePath(imageName))
  102. #define TUICoreCommonBundleImage(imageName) __TUIDefaultBundleImage(TUICoreImagePath(imageName))
  103. #define TUIChatCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIChatImagePath(imageName))
  104. #define TUIConversationCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIConversationImagePath(imageName))
  105. #define TUIContactCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIContactImagePath(imageName))
  106. #define TUISearchCommonBundleImage(imageName) __TUIDefaultBundleImage(TUISearchImagePath(imageName))
  107. #define TUICallingCommonBundleImage(imageName) __TUIDefaultBundleImage(TUICallingImagePath(imageName))
  108. #define TUIPollCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIPollImagePath(imageName))
  109. #define TUIGroupNoteCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIGroupNoteImagePath(imageName))
  110. #define TIMCommonBundleImage(imageName) __TUIDefaultBundleImage(TIMCommonImagePath(imageName))
  111. #define TUITranslationCommonBundleImage(imageName) __TUIDefaultBundleImage(TUITranslationImagePath(imageName))
  112. #define TUIVoiceToTextCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIVoiceToTextImagePath(imageName))
  113. #define TUICustomerServicePluginCommonBundleImage(imageName) __TUIDefaultBundleImage(TUICustomerServicePluginImagePath(imageName))
  114. #define TUIMultimediaPluginCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIMultimediaPluginImagePath(imageName))
  115. /**
  116. * The module of the theme
  117. */
  118. typedef NS_ENUM(NSInteger, TUIThemeModule) {
  119. TUIThemeModuleAll = 0xFFFFFF,
  120. TUIThemeModuleDemo = 0x1 << 0,
  121. TUIThemeModuleCore = 0x1 << 1,
  122. TUIThemeModuleChat = 0x1 << 2,
  123. TUIThemeModuleConversation = 0x1 << 3,
  124. TUIThemeModuleContact = 0x1 << 4,
  125. TUIThemeModuleGroup = 0x1 << 5, // The TUIGroup component has been removed in version 8.4
  126. TUIThemeModuleSearch = 0x1 << 6,
  127. TUIThemeModuleCalling = 0x1 << 7,
  128. TUIThemeModuleDemo_Minimalist = 0x1 << 8,
  129. TUIThemeModuleCore_Minimalist = 0x1 << 9,
  130. TUIThemeModuleChat_Minimalist = 0x1 << 10,
  131. TUIThemeModuleConversation_Minimalist = 0x1 << 11,
  132. TUIThemeModuleContact_Minimalist = 0x1 << 12,
  133. TUIThemeModuleGroup_Minimalist = 0x1 << 13, // The TUIGroup component has been removed in version 8.4
  134. TUIThemeModuleSearch_Minimalist = 0x1 << 14,
  135. TUIThemeModuleCalling_Minimalist = 0x1 << 15,
  136. // UI Plugins
  137. TUIThemeModulePoll = 0x1 << 16,
  138. TUIThemeModuleGroupNote = 0x1 << 17,
  139. TUIThemeModuleTIMCommon = 0x1 << 18,
  140. TUIThemeModuleTranslation = 0x1 << 19,
  141. TUIThemeModuleConversationGroup = 0x1 << 20,
  142. TUIThemeModuleVoiceToText = 0x1 << 21,
  143. TUIThemeModuleCustomerService = 0x1 << 22,
  144. TUIThemeModuleChatBot = 0x1 << 23,
  145. TUIThemeModuleRoomKit = 0x1 << 24,
  146. TUIThemeModuleMultimedia = 0x1 << 25,
  147. };
  148. @interface TUITheme : NSObject
  149. @property(nonatomic, copy) NSString *themeID;
  150. @property(nonatomic, assign) TUIThemeModule module;
  151. @property(nonatomic, copy, nullable) NSString *themeDesc;
  152. @property(nonatomic, strong) NSDictionary *manifest;
  153. @property(nonatomic, copy) NSString *resourcePath;
  154. + (UIColor *__nullable)dynamicColor:(NSString *)colorKey module:(TUIThemeModule)module defaultColor:(NSString *)hex;
  155. + (UIImage *__nullable)dynamicImage:(NSString *)imageKey module:(TUIThemeModule)module defaultImage:(UIImage *)image;
  156. @end
  157. @protocol TUIThemeManagerListener <NSObject>
  158. /**
  159. * Callback for theme changes, you can also listen to the notification named TUIDidApplyingThemeChangedNotfication
  160. */
  161. - (void)onApplyTheme:(TUITheme *)theme module:(TUIThemeModule)module;
  162. - (void)onError:(NSInteger)code message:(NSString *)message userInfo:(NSString *)userInfo;
  163. @end
  164. @interface TUIThemeManager : NSObject
  165. + (instancetype)shareManager;
  166. - (void)addListener:(id<TUIThemeManagerListener>)listener;
  167. - (void)removeListener:(id<TUIThemeManagerListener>)listener;
  168. /**
  169. * Register the theme resource root path of the module
  170. * If the ID of the dark theme is not specified, @"dark" is used internally by default to indicate the dark mode
  171. */
  172. - (void)registerThemeResourcePath:(NSString *)path forModule:(TUIThemeModule)module;
  173. - (void)registerThemeResourcePath:(NSString *)path darkThemeID:(NSString *)darkThemeID forModule:(TUIThemeModule)module;
  174. - (TUITheme *)currentThemeForModule:(TUIThemeModule)module;
  175. - (TUITheme *)darkThemeForModule:(TUIThemeModule)module;
  176. - (void)applyTheme:(NSString *)themeID forModule:(TUIThemeModule)module;
  177. - (void)unApplyThemeForModule:(TUIThemeModule)module;
  178. @end
  179. NS_ASSUME_NONNULL_END