|
| 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 | +} |
0 commit comments