DiscordViewModel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using DiscordRPC;
  3. using PixiEditor.Helpers.Extensions;
  4. using PixiEditor.Models.DataHolders;
  5. using PixiEditor.Models.UserPreferences;
  6. namespace PixiEditor.ViewModels.SubViewModels.Main
  7. {
  8. public class DiscordViewModel : SubViewModel<ViewModelMain>, IDisposable
  9. {
  10. private DiscordRpcClient client;
  11. private string clientId;
  12. private Document currentDocument;
  13. public bool Enabled
  14. {
  15. get => client != null;
  16. set
  17. {
  18. if (Enabled != value)
  19. {
  20. if (value)
  21. {
  22. Start();
  23. }
  24. else
  25. {
  26. Stop();
  27. }
  28. }
  29. }
  30. }
  31. private bool showDocumentName = IPreferences.Current.GetPreference(nameof(ShowDocumentName), true);
  32. public bool ShowDocumentName
  33. {
  34. get => showDocumentName;
  35. set
  36. {
  37. if (showDocumentName != value)
  38. {
  39. showDocumentName = value;
  40. UpdatePresence(currentDocument);
  41. }
  42. }
  43. }
  44. private bool showDocumentSize = IPreferences.Current.GetPreference(nameof(ShowDocumentSize), true);
  45. public bool ShowDocumentSize
  46. {
  47. get => showDocumentSize;
  48. set
  49. {
  50. if (showDocumentSize != value)
  51. {
  52. showDocumentSize = value;
  53. UpdatePresence(currentDocument);
  54. }
  55. }
  56. }
  57. private bool showLayerCount = IPreferences.Current.GetPreference(nameof(ShowLayerCount), true);
  58. public bool ShowLayerCount
  59. {
  60. get => showLayerCount;
  61. set
  62. {
  63. if (showLayerCount != value)
  64. {
  65. showLayerCount = value;
  66. UpdatePresence(currentDocument);
  67. }
  68. }
  69. }
  70. public DiscordViewModel(ViewModelMain owner, string clientId)
  71. : base(owner)
  72. {
  73. Owner.BitmapManager.DocumentChanged += DocumentChanged;
  74. this.clientId = clientId;
  75. Enabled = IPreferences.Current.GetPreference("EnableRichPresence", true);
  76. IPreferences.Current.AddCallback("EnableRichPresence", x => Enabled = (bool)x);
  77. IPreferences.Current.AddCallback(nameof(ShowDocumentName), x => ShowDocumentName = (bool)x);
  78. IPreferences.Current.AddCallback(nameof(ShowDocumentSize), x => ShowDocumentSize = (bool)x);
  79. IPreferences.Current.AddCallback(nameof(ShowLayerCount), x => ShowLayerCount = (bool)x);
  80. AppDomain.CurrentDomain.ProcessExit += (_, _) => Enabled = false;
  81. }
  82. public void Start()
  83. {
  84. client = new DiscordRpcClient(clientId);
  85. client.OnReady += OnReady;
  86. client.Initialize();
  87. }
  88. public void Stop()
  89. {
  90. client.ClearPresence();
  91. client.Dispose();
  92. client = null;
  93. }
  94. public void UpdatePresence(Document document)
  95. {
  96. if (client == null)
  97. {
  98. return;
  99. }
  100. RichPresence richPresence = NewDefaultRP();
  101. if (document != null)
  102. {
  103. richPresence.WithTimestamps(new Timestamps(document.OpenedUTC));
  104. richPresence.Details = ShowDocumentName ? $"Editing {document.Name}".Limit(128) : "Editing something (incognito)";
  105. string state = string.Empty;
  106. if (ShowDocumentSize)
  107. {
  108. state = $"{document.Width}x{document.Height}";
  109. }
  110. if (ShowDocumentSize && ShowLayerCount)
  111. {
  112. state += ", ";
  113. }
  114. if (ShowLayerCount)
  115. {
  116. state += document.Layers.Count == 1 ? "1 Layer" : $"{document.Layers.Count} Layers";
  117. }
  118. richPresence.State = state;
  119. }
  120. client.SetPresence(richPresence);
  121. }
  122. public void Dispose()
  123. {
  124. Enabled = false;
  125. GC.SuppressFinalize(this);
  126. }
  127. private static RichPresence NewDefaultRP()
  128. {
  129. return new RichPresence
  130. {
  131. Details = "Staring at absolutely",
  132. State = "nothing",
  133. Buttons = new Button[]
  134. {
  135. new Button() { Label = "Download PixiEditor", Url = "https://www.github.com/PixiEditor/PixiEditor/releases/latest" },
  136. new Button() { Label = "Watch trailer", Url = "https://youtu.be/QKnXBUY0Pqk" }
  137. },
  138. Assets = new Assets
  139. {
  140. LargeImageKey = "editorlogo",
  141. LargeImageText = "You discovered PixiEditor's logo",
  142. SmallImageKey = "github",
  143. SmallImageText = "Download PixiEditor on GitHub (github.com/PixiEditor/PixiEditor)!"
  144. },
  145. Timestamps = new Timestamps()
  146. {
  147. Start = DateTime.UtcNow
  148. }
  149. };
  150. }
  151. private void DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
  152. {
  153. if (currentDocument != null)
  154. {
  155. currentDocument.PropertyChanged -= DocumentPropertyChanged;
  156. currentDocument.LayersChanged -= DocumentLayerChanged;
  157. }
  158. currentDocument = e.NewDocument;
  159. if (currentDocument != null)
  160. {
  161. UpdatePresence(currentDocument);
  162. currentDocument.PropertyChanged += DocumentPropertyChanged;
  163. currentDocument.LayersChanged += DocumentLayerChanged;
  164. }
  165. }
  166. private void DocumentLayerChanged(object sender, Models.Controllers.LayersChangedEventArgs e)
  167. {
  168. UpdatePresence(currentDocument);
  169. }
  170. private void DocumentPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  171. {
  172. if (e.PropertyName == "Name" || e.PropertyName == "Width" || e.PropertyName == "Height")
  173. {
  174. UpdatePresence(currentDocument);
  175. }
  176. }
  177. private void OnReady(object sender, DiscordRPC.Message.ReadyMessage args)
  178. {
  179. UpdatePresence(Owner.BitmapManager.ActiveDocument);
  180. }
  181. ~DiscordViewModel()
  182. {
  183. Enabled = false;
  184. }
  185. }
  186. }