DiscordViewModel.cs 6.4 KB

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