Skip to content

Commit ebe61db

Browse files
committed
埋め込みブラウザに表示するHTMLの生成を別クラスに分離
1 parent de60cb4 commit ebe61db

5 files changed

Lines changed: 202 additions & 63 deletions

File tree

OpenTween/MediaHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public async Task OpenMediaInLightViewer(IWin32Window owner, ThumbnailInfo[] thu
8686

8787
public void OpenMediaInWebBrowserViewer(IWin32Window owner, ThumbnailInfo[] thumbnails, int displayIndex)
8888
{
89-
using (var viewerDialog = new MediaViewerWebBrowserDialog())
90-
{
91-
viewerDialog.SetMediaItem(thumbnails[displayIndex]);
89+
var viewer = new MediaViewerWebBrowser();
90+
viewer.SetMediaItem(thumbnails[displayIndex]);
91+
92+
using (var viewerDialog = new MediaViewerWebBrowserDialog(viewer))
9293
viewerDialog.ShowDialog(owner);
93-
}
9494
}
9595
}
9696
}

OpenTween/MediaViewerWebBrowserDialog.cs

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,50 @@
1919
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
2020
// Boston, MA 02110-1301, USA.
2121

22-
using OpenTween.Thumbnail;
23-
using System;
24-
using System.Net;
22+
using OpenTween.Models;
23+
using System.ComponentModel;
2524

2625
namespace OpenTween
2726
{
2827
public partial class MediaViewerWebBrowserDialog : OTBaseForm
2928
{
30-
public MediaViewerWebBrowserDialog()
29+
private readonly MediaViewerWebBrowser model;
30+
31+
public MediaViewerWebBrowserDialog(MediaViewerWebBrowser model)
3132
{
3233
InitializeComponent();
33-
}
3434

35-
public void SetMediaItem(ThumbnailInfo media)
36-
=> this.webBrowser.DocumentText = this.CreateDocument(media);
35+
this.model = model;
36+
this.model.SetBackColor(new ColorRGB(this.BackColor));
3737

38-
internal string CreateDocument(ThumbnailInfo media)
39-
{
40-
const string TEMPLATE_HEAD = @"
41-
<!DOCTYPE html>
42-
<meta charset='utf-8'/>
43-
<meta http-equiv='X-UA-Compatible' content='IE=edge'/>
44-
<title>MediaViewerWebBrowserDialog</title>
45-
<style>
46-
html, body {
47-
height: 100%;
48-
margin: 0;
49-
}
50-
.media-panel {
51-
height: 100%;
52-
background: rgb(###BG_COLOR###);
53-
}
54-
.media-image {
55-
height: 100%;
56-
background-size: contain;
57-
background-position: center;
58-
background-repeat: no-repeat;
59-
}
60-
.media-video {
61-
width: 100%;
62-
height: 100%;
63-
}
64-
</style>
65-
";
66-
var bgColor = this.BackColor;
67-
var html = TEMPLATE_HEAD
68-
.Replace("###BG_COLOR###", $"{bgColor.R},{bgColor.G},{bgColor.B}");
38+
this.model.PropertyChanged +=
39+
(s, e) => this.InvokeAsync(() => this.Model_PropertyChanged(s, e));
6940

70-
if (media.VideoUrl != null)
71-
{
72-
const string TEMPLATE_VIDEO_BODY = @"
73-
<div class='media-panel'>
74-
<video class='media-video' preload='metadata' controls>
75-
<source src='###VIDEO_URI###' type='video/mp4'/>
76-
</video>
77-
</div>
78-
";
79-
html += TEMPLATE_VIDEO_BODY
80-
.Replace("###VIDEO_URI###", WebUtility.HtmlEncode(media.VideoUrl));
81-
}
82-
else
41+
this.UpdateAll();
42+
}
43+
44+
private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
45+
{
46+
switch (e.PropertyName)
8347
{
84-
const string TEMPLATE_IMAGE_BODY = @"
85-
<div class='media-panel media'>
86-
<div class='media-image' style='background-image: url(###IMAGE_URI###)'></div>
87-
</div>
88-
";
89-
html += TEMPLATE_IMAGE_BODY
90-
.Replace("###IMAGE_URI###", WebUtility.HtmlEncode(Uri.EscapeUriString(media.FullSizeImageUrl ?? media.ThumbnailImageUrl ?? "")));
48+
case nameof(MediaViewerWebBrowser.DisplayHTML):
49+
this.UpdateHTML();
50+
break;
51+
case "":
52+
case null:
53+
this.UpdateAll();
54+
break;
55+
default:
56+
break;
9157
}
58+
}
9259

93-
return html;
60+
private void UpdateAll()
61+
{
62+
this.UpdateHTML();
9463
}
64+
65+
private void UpdateHTML()
66+
=> this.webBrowser.DocumentText = this.model.DisplayHTML;
9567
}
9668
}

OpenTween/Models/ColorRGB.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// OpenTween - Client of Twitter
2+
// Copyright (c) 2018 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+
// All rights reserved.
4+
//
5+
// This file is part of OpenTween.
6+
//
7+
// This program is free software; you can redistribute it and/or modify it
8+
// under the terms of the GNU General Public License as published by the Free
9+
// Software Foundation; either version 3 of the License, or (at your option)
10+
// any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful, but
13+
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
// for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License along
18+
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+
// Boston, MA 02110-1301, USA.
21+
22+
namespace OpenTween.Models
23+
{
24+
public struct ColorRGB
25+
{
26+
public int R;
27+
public int G;
28+
public int B;
29+
30+
public ColorRGB(int R, int G, int B)
31+
{
32+
this.R = R;
33+
this.G = G;
34+
this.B = B;
35+
}
36+
37+
public ColorRGB(System.Drawing.Color color)
38+
: this(color.R, color.G, color.B)
39+
{
40+
}
41+
}
42+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// OpenTween - Client of Twitter
2+
// Copyright (c) 2018 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+
// All rights reserved.
4+
//
5+
// This file is part of OpenTween.
6+
//
7+
// This program is free software; you can redistribute it and/or modify it
8+
// under the terms of the GNU General Public License as published by the Free
9+
// Software Foundation; either version 3 of the License, or (at your option)
10+
// any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful, but
13+
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
// for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License along
18+
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+
// Boston, MA 02110-1301, USA.
21+
22+
using OpenTween.Thumbnail;
23+
using System;
24+
using System.Net;
25+
26+
namespace OpenTween.Models
27+
{
28+
public class MediaViewerWebBrowser : NotifyPropertyChangedBase
29+
{
30+
public ThumbnailInfo DisplayMedia
31+
{
32+
get => this._displayMedia;
33+
private set => this.SetProperty(ref this._displayMedia, value);
34+
}
35+
private ThumbnailInfo _displayMedia;
36+
37+
public string DisplayHTML
38+
{
39+
get => this._displayHTML;
40+
private set => this.SetProperty(ref this._displayHTML, value);
41+
}
42+
private string _displayHTML = "";
43+
44+
public ColorRGB BackColor
45+
{
46+
get => this._backColor;
47+
private set => this.SetProperty(ref this._backColor, value);
48+
}
49+
private ColorRGB _backColor = new ColorRGB(0, 0, 0);
50+
51+
public void SetMediaItem(ThumbnailInfo media)
52+
{
53+
this.DisplayMedia = media;
54+
this.DisplayHTML = this.CreateDocument();
55+
}
56+
57+
public void SetBackColor(ColorRGB color)
58+
{
59+
this.BackColor = color;
60+
this.DisplayHTML = this.CreateDocument();
61+
}
62+
63+
private string CreateDocument()
64+
{
65+
const string TEMPLATE_HEAD = @"
66+
<!DOCTYPE html>
67+
<meta charset='utf-8'/>
68+
<meta http-equiv='X-UA-Compatible' content='IE=edge'/>
69+
<title>MediaViewerWebBrowserDialog</title>
70+
<style>
71+
html, body {
72+
height: 100%;
73+
margin: 0;
74+
}
75+
.media-panel {
76+
height: 100%;
77+
background: rgb(###BG_COLOR###);
78+
}
79+
.media-image {
80+
height: 100%;
81+
background-size: contain;
82+
background-position: center;
83+
background-repeat: no-repeat;
84+
}
85+
.media-video {
86+
width: 100%;
87+
height: 100%;
88+
}
89+
</style>
90+
";
91+
var bgColor = this.BackColor;
92+
var html = TEMPLATE_HEAD
93+
.Replace("###BG_COLOR###", $"{bgColor.R},{bgColor.G},{bgColor.B}");
94+
95+
var media = this.DisplayMedia;
96+
97+
if (media.VideoUrl != null)
98+
{
99+
const string TEMPLATE_VIDEO_BODY = @"
100+
<div class='media-panel'>
101+
<video class='media-video' preload='metadata' controls>
102+
<source src='###VIDEO_URI###' type='video/mp4'/>
103+
</video>
104+
</div>
105+
";
106+
html += TEMPLATE_VIDEO_BODY
107+
.Replace("###VIDEO_URI###", WebUtility.HtmlEncode(media.VideoUrl));
108+
}
109+
else
110+
{
111+
const string TEMPLATE_IMAGE_BODY = @"
112+
<div class='media-panel media'>
113+
<div class='media-image' style='background-image: url(###IMAGE_URI###)'></div>
114+
</div>
115+
";
116+
html += TEMPLATE_IMAGE_BODY
117+
.Replace("###IMAGE_URI###", WebUtility.HtmlEncode(Uri.EscapeUriString(media.FullSizeImageUrl ?? media.ThumbnailImageUrl ?? "")));
118+
}
119+
120+
return html;
121+
}
122+
}
123+
}

OpenTween/OpenTween.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<DependentUpon>LoginDialog.cs</DependentUpon>
159159
</Compile>
160160
<Compile Include="MediaItem.cs" />
161+
<Compile Include="Models\ColorRGB.cs" />
161162
<Compile Include="Models\ComparerMode.cs" />
162163
<Compile Include="Models\DirectMessagesTabModel.cs" />
163164
<Compile Include="Models\FavoritesTabModel.cs" />
@@ -168,6 +169,7 @@
168169
<Compile Include="Models\ListTimelineTabModel.cs" />
169170
<Compile Include="Models\LocalSearchTabModel.cs" />
170171
<Compile Include="Models\MediaInfo.cs" />
172+
<Compile Include="Models\MediaViewerWebBrowser.cs" />
171173
<Compile Include="Models\MentionsTabModel.cs" />
172174
<Compile Include="Models\MuteTabModel.cs" />
173175
<Compile Include="Models\PostClass.cs" />

0 commit comments

Comments
 (0)