Skip to content

Commit

Permalink
Add share to X (Twitter) with web intents. (#2485)
Browse files Browse the repository at this point in the history
* Add share to X (Twitter) with web intent.

* Fix wording

* Fix wording again

* Remove share settings designer config

* Update src/LiveSplit.Core/Web/Share/Twitter.cs

Co-authored-by: Ero <56401411+just-ero@users.noreply.github.com>

* Fix typo

---------

Co-authored-by: Kuhaku369 <kuhaku.o.9893@gmail.com>
Co-authored-by: wooferzfg <spapushin@gmail.com>
Co-authored-by: Ero <56401411+just-ero@users.noreply.github.com>
  • Loading branch information
4 people committed Apr 25, 2024
1 parent bcce0d8 commit 52c7af9
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ LiveSplit is a timer program for speedrunners that is both easy to use and full

**Dynamic Resizing:** LiveSplit can be resized to any size so that it looks good on stream. As LiveSplit’s size is changed, all of its parts are automatically scaled up in order to preserve its appearance.

**Sharing Runs:** Any run can be shared to [Speedrun.com](http://speedrun.com/). Splits can also be distributed using [splits i/o](https://splits.io/) and imported from a URL. You can also share a screenshot of your splits to [Imgur](http://imgur.com/) or save it as a file. Your [Twitch](http://www.twitch.tv/) title can be updated as well based on the game you are playing.
**Sharing Runs:** Any run can be shared to [Speedrun.com](http://speedrun.com/) and [X (Twitter)](https://twitter.com/). Splits can also be distributed using [splits i/o](https://splits.io/) and imported from a URL. You can also share a screenshot of your splits to [Imgur](http://imgur.com/) or save it as a file. Your [Twitch](http://www.twitch.tv/) title can be updated as well based on the game you are playing.

**Component Development:** Anyone can develop their own components that can easily be shared and used with LiveSplit. Additional downloadable components can be found in the [Components Section](https://livesplit.org/components/).

Expand Down
30 changes: 30 additions & 0 deletions src/LiveSplit.Core/Web/Share/ShareSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/LiveSplit.Core/Web/Share/ShareSettings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
<Setting Name="TwitchFormat" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="TwitterFormat" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="TwitterFormatRunning" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
71 changes: 71 additions & 0 deletions src/LiveSplit.Core/Web/Share/Twitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using LiveSplit.Model;
using LiveSplit.Options;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace LiveSplit.Web.Share
{
public class Twitter : IRunUploadPlatform
{
public ISettings Settings { get; set; }

protected static readonly Twitter _Instance = new Twitter();
public static Twitter Instance => _Instance;

public static readonly Uri BaseUri = new Uri("https://twitter.com/intent/tweet");

protected Twitter() { }

public string PlatformName => "X (Twitter)";

public string Description =>
@"X (Twitter) allows you to share your run with the world.
When sharing, a screenshot of LiveSplit is automatically copied to the clipboard.
When you click share, LiveSplit opens a Tweet composition window in your default browser.";

public bool VerifyLogin()
{
return true;
}

public bool SubmitRun(IRun run, Func<Image> screenShotFunction = null, bool attachSplits = false, TimingMethod method = TimingMethod.RealTime, string comment = "", params string[] additionalParams)
{
if (attachSplits)
comment += " " + SplitsIO.Instance.Share(run, screenShotFunction);

ImageToClipboard(screenShotFunction());
var uri = MakeUri(comment);
Process.Start(uri);

return true;
}

private void ImageToClipboard(Image image)
{
if (image is null)
return;

Image pngImage;
using (var stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Png);
pngImage = Image.FromStream(stream);
}
Clipboard.SetDataObject(pngImage);
}
private string MakeUri(string text)
{
var intentText = "";
if (!String.IsNullOrEmpty(text))
{
intentText = "?text=" + Uri.EscapeDataString(text);
}

return BaseUri + intentText;
}
}
}
8 changes: 7 additions & 1 deletion src/LiveSplit.Core/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
<userSettings>
<LiveSplit.Web.Share.ShareSettings>
<setting name="TwitchFormat" serializeAs="String">
<value/>
<value />
</setting>
<setting name="TwitterFormat" serializeAs="String">
<value />
</setting>
<setting name="TwitterFormatRunning" serializeAs="String">
<value />
</setting>
</LiveSplit.Web.Share.ShareSettings>
</userSettings>
Expand Down
37 changes: 34 additions & 3 deletions src/LiveSplit.View/View/ShareRunDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private bool HasPersonalBest(IRun run)

private void SubmitDialog_Load(object sender, EventArgs e)
{
cbxPlatform.Items.Add("X (Twitter)");

if (State.CurrentPhase == TimerPhase.NotRunning || State.CurrentPhase == TimerPhase.Ended)
{
if (HasPersonalBest(Run))
Expand Down Expand Up @@ -72,6 +74,7 @@ private void cbxPlatform_SelectionChangeCommitted(object sender, EventArgs e)
switch (cbxPlatform.SelectedItem.ToString())
{
case "Splits.io": CurrentPlatform = SplitsIO.Instance; break;
case "X (Twitter)": CurrentPlatform = Twitter.Instance; break;
case "Twitch": CurrentPlatform = Twitch.Instance; break;
case "Screenshot": CurrentPlatform = Screenshot.Instance; break;
case "Imgur": CurrentPlatform = Imgur.Instance; break;
Expand All @@ -84,7 +87,7 @@ private void cbxPlatform_SelectionChangeCommitted(object sender, EventArgs e)
txtNotes.Enabled = btnInsertCategory.Enabled = btnInsertDeltaTime.Enabled = btnInsertGame.Enabled
= btnInsertPB.Enabled = btnInsertSplitName.Enabled = btnInsertSplitTime.Enabled
= btnInsertStreamLink.Enabled = btnInsertTitle.Enabled = btnPreview.Enabled =
(CurrentPlatform == Twitch.Instance || CurrentPlatform == Imgur.Instance);
(CurrentPlatform == Twitter.Instance || CurrentPlatform == Twitch.Instance || CurrentPlatform == Imgur.Instance);

if (State.CurrentPhase == TimerPhase.NotRunning || State.CurrentPhase == TimerPhase.Ended)
chkAttachSplits.Enabled = !(CurrentPlatform == Screenshot.Instance || CurrentPlatform == SplitsIO.Instance
Expand Down Expand Up @@ -164,7 +167,27 @@ private string FormatNotes(string notePlaceholder)

private void RefreshNotes()
{
if (CurrentPlatform == Twitch.Instance)
if (CurrentPlatform == Twitter.Instance)
{
ShareSettings.Default.Reload();
if (State.CurrentPhase == TimerPhase.NotRunning || State.CurrentPhase == TimerPhase.Ended)
{
txtNotes.Text = ShareSettings.Default.TwitterFormat;
if (string.IsNullOrEmpty(txtNotes.Text))
{
txtNotes.Text = "I got a $pb in $title.";
}
}
else
{
txtNotes.Text = ShareSettings.Default.TwitterFormatRunning;
if (string.IsNullOrEmpty(txtNotes.Text))
{
txtNotes.Text = "I'm $delta in $title.";
}
}
}
else if (CurrentPlatform == Twitch.Instance)
{
ShareSettings.Default.Reload();
txtNotes.Text = ShareSettings.Default.TwitchFormat;
Expand All @@ -179,7 +202,15 @@ private void RefreshNotes()

private void SaveNotesFormat()
{
if (CurrentPlatform == Twitch.Instance)
if (CurrentPlatform == Twitter.Instance)
{
if (State.CurrentPhase == TimerPhase.NotRunning || State.CurrentPhase == TimerPhase.Ended)
ShareSettings.Default.TwitterFormat = txtNotes.Text;
else
ShareSettings.Default.TwitterFormatRunning = txtNotes.Text;
ShareSettings.Default.Save();
}
else if (CurrentPlatform == Twitch.Instance)
{
ShareSettings.Default.TwitchFormat = txtNotes.Text;
ShareSettings.Default.Save();
Expand Down

0 comments on commit 52c7af9

Please sign in to comment.