mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-04-23 12:15:48 +02:00
Move clipboard utils into ClipboardManager & add SetImage
This commit is contained in:
parent
ae8b740600
commit
5888d540a6
@ -5,6 +5,7 @@
|
||||
using TweetDuck.Browser.Notification;
|
||||
using TweetDuck.Controls;
|
||||
using TweetDuck.Dialogs;
|
||||
using TweetDuck.Management;
|
||||
using TweetDuck.Utils;
|
||||
using TweetLib.Core.Features.Notifications;
|
||||
|
||||
@ -108,7 +109,7 @@ public void StopVideo(){
|
||||
}
|
||||
|
||||
public void FixClipboard(){
|
||||
form.InvokeAsyncSafe(WindowsUtils.ClipboardStripHtmlStyles);
|
||||
form.InvokeAsyncSafe(ClipboardManager.StripHtmlStyles);
|
||||
}
|
||||
|
||||
public void OpenBrowser(string url){
|
||||
|
@ -9,6 +9,7 @@
|
||||
using TweetDuck.Configuration;
|
||||
using TweetDuck.Controls;
|
||||
using TweetDuck.Dialogs;
|
||||
using TweetDuck.Management;
|
||||
using TweetDuck.Management.Analytics;
|
||||
using TweetDuck.Utils;
|
||||
using TweetLib.Core.Features.Twitter;
|
||||
@ -202,7 +203,7 @@ protected static void OpenBrowser(Control control, string url){
|
||||
}
|
||||
|
||||
protected static void SetClipboardText(Control control, string text){
|
||||
control.InvokeAsyncSafe(() => WindowsUtils.SetClipboard(text, TextDataFormat.UnicodeText));
|
||||
control.InvokeAsyncSafe(() => ClipboardManager.SetText(text, TextDataFormat.UnicodeText));
|
||||
}
|
||||
|
||||
protected static void InsertSelectionSearchItem(IMenuModel model, CefMenuCommand insertCommand, string insertLabel){
|
||||
|
55
Management/ClipboardManager.cs
Normal file
55
Management/ClipboardManager.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace TweetDuck.Management{
|
||||
static class ClipboardManager{
|
||||
private static readonly Lazy<Regex> RegexStripHtmlStyles = new Lazy<Regex>(() => new Regex(@"\s?(?:style|class)="".*?"""), false);
|
||||
private static readonly Lazy<Regex> RegexOffsetClipboardHtml = new Lazy<Regex>(() => new Regex(@"(?<=EndHTML:|EndFragment:)(\d+)"), false);
|
||||
|
||||
public static void SetText(string text, TextDataFormat format){
|
||||
if (string.IsNullOrEmpty(text)){
|
||||
return;
|
||||
}
|
||||
|
||||
DataObject obj = new DataObject();
|
||||
obj.SetText(text, format);
|
||||
SetClipboardData(obj);
|
||||
}
|
||||
|
||||
public static void SetImage(Image image){
|
||||
DataObject obj = new DataObject();
|
||||
obj.SetImage(image);
|
||||
SetClipboardData(obj);
|
||||
}
|
||||
|
||||
private static void SetClipboardData(DataObject obj){
|
||||
try{
|
||||
Clipboard.SetDataObject(obj);
|
||||
}catch(ExternalException e){
|
||||
Program.Reporter.HandleException("Clipboard Error", "TweetDuck could not access the clipboard as it is currently used by another process.", true, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void StripHtmlStyles(){
|
||||
if (!Clipboard.ContainsText(TextDataFormat.Html) || !Clipboard.ContainsText(TextDataFormat.UnicodeText)){
|
||||
return;
|
||||
}
|
||||
|
||||
string originalText = Clipboard.GetText(TextDataFormat.UnicodeText);
|
||||
string originalHtml = Clipboard.GetText(TextDataFormat.Html);
|
||||
|
||||
string updatedHtml = RegexStripHtmlStyles.Value.Replace(originalHtml, string.Empty);
|
||||
|
||||
int removed = originalHtml.Length - updatedHtml.Length;
|
||||
updatedHtml = RegexOffsetClipboardHtml.Value.Replace(updatedHtml, match => (int.Parse(match.Value) - removed).ToString().PadLeft(match.Value.Length, '0'));
|
||||
|
||||
DataObject obj = new DataObject();
|
||||
obj.SetText(originalText, TextDataFormat.UnicodeText);
|
||||
obj.SetText(updatedHtml, TextDataFormat.Html);
|
||||
SetClipboardData(obj);
|
||||
}
|
||||
}
|
||||
}
|
@ -73,6 +73,7 @@
|
||||
<Compile Include="Controls\NumericUpDownEx.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Management\ClipboardManager.cs" />
|
||||
<Compile Include="Management\FormManager.cs" />
|
||||
<Compile Include="Browser\Handling\ContextMenuGuide.cs" />
|
||||
<Compile Include="Browser\Handling\DragHandlerBrowser.cs" />
|
||||
|
@ -3,10 +3,7 @@
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace TweetDuck.Utils{
|
||||
@ -16,9 +13,6 @@ static class WindowsUtils{
|
||||
public static bool ShouldAvoidToolWindow { get; } = IsWindows8OrNewer;
|
||||
public static bool IsAeroEnabled => IsWindows8OrNewer || (NativeMethods.DwmIsCompositionEnabled(out bool isCompositionEnabled) == 0 && isCompositionEnabled);
|
||||
|
||||
private static readonly Lazy<Regex> RegexStripHtmlStyles = new Lazy<Regex>(() => new Regex(@"\s?(?:style|class)="".*?"""), false);
|
||||
private static readonly Lazy<Regex> RegexOffsetClipboardHtml = new Lazy<Regex>(() => new Regex(@"(?<=EndHTML:|EndFragment:)(\d+)"), false);
|
||||
|
||||
private static bool OSVersionEquals(int major, int minor){
|
||||
Version ver = Environment.OSVersion.Version;
|
||||
return ver.Major == major && ver.Minor == minor;
|
||||
@ -69,43 +63,6 @@ public static void TryDeleteFolderWhenAble(string path, int timeout){
|
||||
}).Start();
|
||||
}
|
||||
|
||||
public static void ClipboardStripHtmlStyles(){
|
||||
if (!Clipboard.ContainsText(TextDataFormat.Html) || !Clipboard.ContainsText(TextDataFormat.UnicodeText)){
|
||||
return;
|
||||
}
|
||||
|
||||
string originalText = Clipboard.GetText(TextDataFormat.UnicodeText);
|
||||
string originalHtml = Clipboard.GetText(TextDataFormat.Html);
|
||||
|
||||
string updatedHtml = RegexStripHtmlStyles.Value.Replace(originalHtml, string.Empty);
|
||||
|
||||
int removed = originalHtml.Length - updatedHtml.Length;
|
||||
updatedHtml = RegexOffsetClipboardHtml.Value.Replace(updatedHtml, match => (int.Parse(match.Value) - removed).ToString().PadLeft(match.Value.Length, '0'));
|
||||
|
||||
DataObject obj = new DataObject();
|
||||
obj.SetText(originalText, TextDataFormat.UnicodeText);
|
||||
obj.SetText(updatedHtml, TextDataFormat.Html);
|
||||
SetClipboardData(obj);
|
||||
}
|
||||
|
||||
public static void SetClipboard(string text, TextDataFormat format){
|
||||
if (string.IsNullOrEmpty(text)){
|
||||
return;
|
||||
}
|
||||
|
||||
DataObject obj = new DataObject();
|
||||
obj.SetText(text, format);
|
||||
SetClipboardData(obj);
|
||||
}
|
||||
|
||||
private static void SetClipboardData(DataObject obj){
|
||||
try{
|
||||
Clipboard.SetDataObject(obj);
|
||||
}catch(ExternalException e){
|
||||
Program.Reporter.HandleException("Clipboard Error", "TweetDuck could not access the clipboard as it is currently used by another process.", true, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Browser> FindInstalledBrowsers(){
|
||||
static IEnumerable<Browser> ReadBrowsersFromKey(RegistryHive hive){
|
||||
using RegistryKey root = RegistryKey.OpenBaseKey(hive, RegistryView.Default);
|
||||
|
Loading…
Reference in New Issue
Block a user