-
Notifications
You must be signed in to change notification settings - Fork 2
/
TweetWindow.xaml.cs
121 lines (106 loc) · 3.84 KB
/
TweetWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.ComponentModel;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Drawing = System.Drawing;
namespace Screenshotter
{
/// <summary>
/// TweetWindow.xaml の相互作用ロジック
/// </summary>
public partial class TweetWindow : Window
{
private Drawing.Bitmap screenshot;
public TweetWindow(Drawing.Bitmap screenshot)
{
this.screenshot = screenshot;
InitializeComponent();
Keyboard.Focus(this.Message);
}
private async void TweetAsync(object sender, RoutedEventArgs e)
{
ShowStatus("ツイートしています...");
if (this.screenshot == null)
{
MessageBox.Show("スクリーンショットが失われたため、ツイートできません。");
Close();
}
using (var stream = new MemoryStream())
{
this.screenshot.Save(stream, Drawing.Imaging.ImageFormat.Jpeg);
await MainWindow.token.Statuses.UpdateWithMediaAsync(
this.Message.Text, stream.ToArray()
);
}
CloseStatus(async () =>
{
ShowStatus("ツイートを送信しました。", false);
await Task.Run(() => Thread.Sleep(1000));
CloseStatus();
Close();
});
}
private void CountText(object sender, TextChangedEventArgs e)
{
var temp = Regex.Replace(
this.Message.Text,
@"s?https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+",
"01234567890123"
).Replace(Environment.NewLine, "1");
this.Count.Content = 140 - temp.Length - 24;
if ((int)this.Count.Content < 0)
{
this.Count.Foreground = Brushes.Red;
this.TweetButton.IsEnabled = false;
}
else
{
this.Count.Foreground = Brushes.Black;
this.TweetButton.IsEnabled = true;
}
}
private void ParseKeyboardShortcut(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) Close();
if (e.Key != Key.Enter) return;
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.None) return;
if ((int)this.Count.Content < 0) return;
e.Handled = true;
TweetAsync(null, null);
}
private void ShowStatus(string message, bool isLoader = true)
{
this.Status.Content = message;
this.Loader.Visibility = (isLoader) ? Visibility.Visible
: Visibility.Collapsed;
this.Success.Visibility = (isLoader) ? Visibility.Collapsed
: Visibility.Visible;
var sb = FindResource("DialogShowAnimation") as Storyboard;
Storyboard.SetTarget(sb, this.StatusGrid);
sb.Completed += (s, a) => this.StatusGrid.IsHitTestVisible = true;
sb.Begin();
}
private void CloseStatus(Action doAfterClose = null)
{
var sb = FindResource("DialogCloseAnimation") as Storyboard;
Storyboard.SetTarget(sb, this.StatusGrid);
sb.Completed += (s, a) =>
{
this.StatusGrid.IsHitTestVisible = false;
doAfterClose?.Invoke();
};
sb.Begin();
}
private void OnClosing(object sender, CancelEventArgs e)
{
this.screenshot?.Dispose();
}
}
}