TUIThemeManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * | 主题资源根路径(ThemeResourcePath)
  11. * |
  12. * |------ 主题1(以主题 id 命名)
  13. * | |
  14. * | |------- manifest.plist (主题的配置信息)
  15. * | |------- resource (资源文件)
  16. * |-------主题2
  17. * | |
  18. * | |------ manifest.plist (主题的配置信息)
  19. * | |------ resource(资源文件)
  20. *
  21. *
  22. *
  23. * Storage structure of theme for each module
  24. * | The root path of the theme resource(ThemeResourcePath)
  25. * |
  26. * |------ Theme one(named with theme identifier)
  27. * | |
  28. * | |------- manifest.plist (configuration information for the theme)
  29. * | |------- resource
  30. * |-------Theme two
  31. * | |
  32. * | |------ manifest.plist (configuration information for the theme)
  33. * | |------ resource
  34. */
  35. #import <Foundation/Foundation.h>
  36. #import "TUIDefine.h"
  37. @class TUITheme;
  38. @class TUIThemeManager;
  39. NS_ASSUME_NONNULL_BEGIN
  40. #define TUIShareThemeManager TUIThemeManager.shareManager
  41. /**
  42. * 应用的主题发生了变化的通知
  43. * Notifications when the app's theme has changed
  44. */
  45. #define TUIDidApplyingThemeChangedNotfication @"TUIDidApplyingThemeChangedNotfication"
  46. #define TUIDidApplyingThemeChangedNotficationThemeKey @"TUIDidApplyingThemeChangedNotficationThemeKey"
  47. #define TUIDidApplyingThemeChangedNotficationModuleKey @"TUIDidApplyingThemeChangedNotficationModuleKey"
  48. /**
  49. * 注册模块对应的主题资源根路径
  50. * Register the theme resource root path of the module
  51. */
  52. #define TUIRegisterThemeResourcePath(path, module) [TUIShareThemeManager registerThemeResourcePath:path forModule:module];
  53. /**
  54. * 获取当前使用的主题
  55. * Get the theme used by the module
  56. */
  57. #define TUICurrentTheme(module) [TUIShareThemeManager currentThemeForModule:module]
  58. /**
  59. * 获取对应的黑夜主题
  60. * Get the dark night theme of the module
  61. */
  62. #define TUIDarkTheme(module) [TUIShareThemeManager darkThemeForModule:module]
  63. /**
  64. * 获取动态颜色
  65. * Get dynamic colors that change with theme
  66. */
  67. #define TUIDynamicColor(colorKey, themeModule, defaultHex) [TUITheme dynamicColor:colorKey module:themeModule defaultColor:defaultHex]
  68. #define TUIDemoDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleDemo, defaultHex)
  69. #define TUICoreDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleCore, defaultHex)
  70. #define TUIChatDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleChat, defaultHex)
  71. #define TUIConversationDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleConversation, defaultHex)
  72. #define TUIConversationGroupDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleConversationGroup, defaultHex)
  73. #define TUIContactDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleContact, defaultHex)
  74. #define TUIGroupDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleGroup, defaultHex)
  75. #define TUISearchDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleSearch, defaultHex)
  76. #define TUICallKitDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleCalling, defaultHex)
  77. #define TUIPollDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModulePoll, defaultHex)
  78. #define TUIGroupNoteDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleGroupNote, defaultHex)
  79. #define TIMCommonDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleTIMCommon, defaultHex)
  80. #define TUITranslationDynamicColor(colorKey, defaultHex) TUIDynamicColor(colorKey, TUIThemeModuleTranslation, defaultHex)
  81. /**
  82. * 动态获取图片
  83. * Get dynamic image that change with theme
  84. */
  85. #define TUIDemoBundleThemeImage(imageKey, defaultImageName) TUIDemoDynamicImage(imageKey, TUIDemoCommonBundleImage(defaultImageName))
  86. #define TUICoreBundleThemeImage(imageKey, defaultImageName) TUICoreDynamicImage(imageKey, TUICoreCommonBundleImage(defaultImageName))
  87. #define TUIChatBundleThemeImage(imageKey, defaultImageName) TUIChatDynamicImage(imageKey, TUIChatCommonBundleImage(defaultImageName))
  88. #define TUIConversationBundleThemeImage(imageKey, defaultImageName) TUIConversationDynamicImage(imageKey, TUIConversationCommonBundleImage(defaultImageName))
  89. #define TUIContactBundleThemeImage(imageKey, defaultImageName) TUIContactDynamicImage(imageKey, TUIContactCommonBundleImage(defaultImageName))
  90. #define TUIGroupBundleThemeImage(imageKey, defaultImageName) TUIGroupDynamicImage(imageKey, TUIGroupCommonBundleImage(defaultImageName))
  91. #define TUISearchBundleThemeImage(imageKey, defaultImageName) TUISearchDynamicImage(imageKey, TUISearchCommonBundleImage(defaultImageName))
  92. #define TUICallingBundleThemeImage(imageKey, defaultImageName) TUICallingDynamicImage(imageKey, TUICallingCommonBundleImage(defaultImageName))
  93. #define TUIPollBundleThemeImage(imageKey, defaultImageName) TUIPollDynamicImage(imageKey, TUIPollCommonBundleImage(defaultImageName))
  94. #define TUIGroupNoteBundleThemeImage(imageKey, defaultImageName) TUIGroupNoteDynamicImage(imageKey, TUIGroupNoteCommonBundleImage(defaultImageName))
  95. #define TIMCommonBundleThemeImage(imageKey, defaultImageName) TIMCommonDynamicImage(imageKey, TIMCommonBundleImage(defaultImageName))
  96. #define TUITranslationBundleThemeImage(imageKey, defaultImageName) TUITranslationDynamicImage(imageKey, TUITranslationCommonBundleImage(defaultImageName))
  97. #define TUIDynamicImage(imageKey, themeModule, defaultImg) [TUITheme dynamicImage:imageKey module:themeModule defaultImage:defaultImg]
  98. #define TUIDemoDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleDemo, defaultImg)
  99. #define TUICoreDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleCore, defaultImg)
  100. #define TUIChatDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleChat, defaultImg)
  101. #define TUIConversationDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleConversation, defaultImg)
  102. #define TUIConversationGroupDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleConversationGroup, defaultImg)
  103. #define TUIContactDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleContact, defaultImg)
  104. #define TUIGroupDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleGroup, defaultImg)
  105. #define TUISearchDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleSearch, defaultImg)
  106. #define TUICallingDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleCalling, defaultImg)
  107. #define TUIPollDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModulePoll, defaultImg)
  108. #define TUIGroupNoteDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleGroupNote, defaultImg)
  109. #define TIMCommonDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleTIMCommon, defaultImg)
  110. #define TUITranslationDynamicImage(imageKey, defaultImg) TUIDynamicImage(imageKey, TUIThemeModuleTranslation, defaultImg)
  111. #define __TUIDefaultBundleImage(imageBundlePath) [UIImage imageWithContentsOfFile:imageBundlePath]
  112. #define TUIDemoCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIDemoImagePath(imageName))
  113. #define TUICoreCommonBundleImage(imageName) __TUIDefaultBundleImage(TUICoreImagePath(imageName))
  114. #define TUIChatCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIChatImagePath(imageName))
  115. #define TUIConversationCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIConversationImagePath(imageName))
  116. #define TUIContactCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIContactImagePath(imageName))
  117. #define TUIGroupCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIGroupImagePath(imageName))
  118. #define TUISearchCommonBundleImage(imageName) __TUIDefaultBundleImage(TUISearchImagePath(imageName))
  119. #define TUICallingCommonBundleImage(imageName) __TUIDefaultBundleImage(TUICallingImagePath(imageName))
  120. #define TUIPollCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIPollImagePath(imageName))
  121. #define TUIGroupNoteCommonBundleImage(imageName) __TUIDefaultBundleImage(TUIGroupNoteImagePath(imageName))
  122. #define TIMCommonBundleImage(imageName) __TUIDefaultBundleImage(TIMCommonImagePath(imageName))
  123. #define TUITranslationCommonBundleImage(imageName) __TUIDefaultBundleImage(TUITranslationImagePath(imageName))
  124. /**
  125. * 主题模块
  126. * The module of the theme
  127. */
  128. typedef NS_ENUM(NSInteger, TUIThemeModule) {
  129. TUIThemeModuleAll = 0xFFFFFF,
  130. TUIThemeModuleDemo = 0x1 << 0,
  131. TUIThemeModuleCore = 0x1 << 1,
  132. TUIThemeModuleChat = 0x1 << 2,
  133. TUIThemeModuleConversation = 0x1 << 3,
  134. TUIThemeModuleContact = 0x1 << 4,
  135. TUIThemeModuleGroup = 0x1 << 5,
  136. TUIThemeModuleSearch = 0x1 << 6,
  137. TUIThemeModuleCalling = 0x1 << 7,
  138. TUIThemeModuleDemo_Minimalist = 0x1 << 8,
  139. TUIThemeModuleCore_Minimalist = 0x1 << 9,
  140. TUIThemeModuleChat_Minimalist = 0x1 << 10,
  141. TUIThemeModuleConversation_Minimalist = 0x1 << 11,
  142. TUIThemeModuleContact_Minimalist = 0x1 << 12,
  143. TUIThemeModuleGroup_Minimalist = 0x1 << 13,
  144. TUIThemeModuleSearch_Minimalist = 0x1 << 14,
  145. TUIThemeModuleCalling_Minimalist = 0x1 << 15,
  146. // UI Plugins
  147. TUIThemeModulePoll = 0x1 << 16,
  148. TUIThemeModuleGroupNote = 0x1 << 17,
  149. TUIThemeModuleTIMCommon = 0x1 << 18,
  150. TUIThemeModuleTranslation = 0x1 << 19,
  151. TUIThemeModuleConversationGroup = 0x1 << 20,
  152. };
  153. @interface TUITheme : NSObject
  154. @property(nonatomic, copy) NSString *themeID;
  155. @property(nonatomic, assign) TUIThemeModule module;
  156. @property(nonatomic, copy, nullable) NSString *themeDesc;
  157. @property(nonatomic, strong) NSDictionary *manifest;
  158. @property(nonatomic, copy) NSString *resourcePath;
  159. + (UIColor *__nullable)dynamicColor:(NSString *)colorKey module:(TUIThemeModule)module defaultColor:(NSString *)hex;
  160. + (UIImage *__nullable)dynamicImage:(NSString *)imageKey module:(TUIThemeModule)module defaultImage:(UIImage *)image;
  161. @end
  162. @protocol TUIThemeManagerListener <NSObject>
  163. /**
  164. * 主题发生了变化,也可以监听 @ref TUIDidApplyingThemeChangedNotfication 通知
  165. * Callback for theme changes, you can also listen to the notification named TUIDidApplyingThemeChangedNotfication
  166. */
  167. - (void)onApplyTheme:(TUITheme *)theme module:(TUIThemeModule)module;
  168. - (void)onError:(NSInteger)code message:(NSString *)message userInfo:(NSString *)userInfo;
  169. @end
  170. @interface TUIThemeManager : NSObject
  171. + (instancetype)shareManager;
  172. - (void)addListener:(id<TUIThemeManagerListener>)listener;
  173. - (void)removeListener:(id<TUIThemeManagerListener>)listener;
  174. /**
  175. * 注册主题资源根路径
  176. * 如果不指定黑夜主题的ID的话,内部默认使用 @"dark" 来表示黑夜模式
  177. *
  178. * Register the theme resource root path of the module
  179. * If the ID of the dark theme is not specified, @"dark" is used internally by default to indicate the dark mode
  180. */
  181. - (void)registerThemeResourcePath:(NSString *)path forModule:(TUIThemeModule)module;
  182. - (void)registerThemeResourcePath:(NSString *)path darkThemeID:(NSString *)darkThemeID forModule:(TUIThemeModule)module;
  183. - (TUITheme *)currentThemeForModule:(TUIThemeModule)module;
  184. - (TUITheme *)darkThemeForModule:(TUIThemeModule)module;
  185. - (void)applyTheme:(NSString *)themeID forModule:(TUIThemeModule)module;
  186. - (void)unApplyThemeForModule:(TUIThemeModule)module;
  187. @end
  188. NS_ASSUME_NONNULL_END