Skip to content

Commit bce5673

Browse files
committed
Use interface for monitors & Add clean clipboard support
1 parent 6263d71 commit bce5673

File tree

6 files changed

+190
-138
lines changed

6 files changed

+190
-138
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2024 Jack251970
2+
// Licensed under the Apache License. See the LICENSE.
3+
4+
namespace Flow.Launcher.Plugin.ClipboardPlus.Core.Data.Models;
5+
6+
/// <summary>
7+
/// Provides data for the <see cref="ClipboardChanged"/> event.
8+
/// </summary>
9+
public class ClipboardChangedEventArgs : EventArgs
10+
{
11+
public ClipboardChangedEventArgs(
12+
object? content,
13+
DataType dataType,
14+
SourceApplication source
15+
)
16+
{
17+
Content = content;
18+
DataType = dataType;
19+
20+
SourceApplication = new SourceApplication(
21+
source.Handle,
22+
source.Name,
23+
source.Title,
24+
source.Path
25+
);
26+
}
27+
28+
#region Properties
29+
30+
/// <summary>
31+
/// Gets the currently copied clipboard content.
32+
/// </summary>
33+
public object? Content { get; }
34+
35+
/// <summary>
36+
/// Gets the currently copied clipboard content-type.
37+
/// </summary>
38+
public DataType DataType { get; }
39+
40+
/// <summary>
41+
/// Gets the application from where the
42+
/// clipboard's content were copied.
43+
/// </summary>
44+
public SourceApplication SourceApplication { get; }
45+
46+
#endregion
47+
}

src/Flow.Launcher.Plugin.ClipboardPlus.Core/Data/Models/Clipboard/ClipboardMonitorW.cs

+15-50
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Flow.Launcher.Plugin.ClipboardPlus.Core.Data.Models;
1111
/// ClipboardMonitorW is a class that monitors the clipboard
1212
/// </summary>
1313
[SupportedOSPlatform("windows6.0.6000")]
14-
public class ClipboardMonitorW : IDisposable
14+
public class ClipboardMonitorW : IClipboardMonitor
1515
{
1616
#region Fields
1717

@@ -143,6 +143,19 @@ public void StopMonitoring()
143143
}
144144
}
145145

146+
/// <summary>
147+
/// Clears the clipboard of all data.
148+
/// </summary>
149+
public void CleanClipboard()
150+
{
151+
ClipboardText = string.Empty;
152+
ClipboardRtfText = string.Empty;
153+
ClipboardObject = null;
154+
ClipboardImage = null;
155+
ClipboardFile = string.Empty;
156+
ClipboardFiles.Clear();
157+
}
158+
146159
#endregion
147160

148161
#region Private
@@ -177,53 +190,6 @@ internal void Invoke(object? content, DataType type, SourceApplication source)
177190

178191
#endregion
179192

180-
#region Event Arguments
181-
182-
/// <summary>
183-
/// Provides data for the <see cref="ClipboardChanged"/> event.
184-
/// </summary>
185-
public class ClipboardChangedEventArgs : EventArgs
186-
{
187-
public ClipboardChangedEventArgs(
188-
object? content,
189-
DataType dataType,
190-
SourceApplication source
191-
)
192-
{
193-
Content = content;
194-
DataType = dataType;
195-
196-
SourceApplication = new SourceApplication(
197-
source.Handle,
198-
source.Name,
199-
source.Title,
200-
source.Path
201-
);
202-
}
203-
204-
#region Properties
205-
206-
/// <summary>
207-
/// Gets the currently copied clipboard content.
208-
/// </summary>
209-
public object? Content { get; }
210-
211-
/// <summary>
212-
/// Gets the currently copied clipboard content-type.
213-
/// </summary>
214-
public DataType DataType { get; }
215-
216-
/// <summary>
217-
/// Gets the application from where the
218-
/// clipboard's content were copied.
219-
/// </summary>
220-
public SourceApplication SourceApplication { get; }
221-
222-
#endregion
223-
}
224-
225-
#endregion
226-
227193
#endregion
228194

229195
#region Private
@@ -284,8 +250,7 @@ protected virtual void Dispose(bool disposing)
284250
_timer = null!;
285251
_clipboardHandle = null!;
286252
_observableFormats = null!;
287-
ClipboardFiles = null!;
288-
ClipboardImage = null!;
253+
CleanClipboard();
289254
_disposed = true;
290255
}
291256
}

src/Flow.Launcher.Plugin.ClipboardPlus.Core/Data/Models/Clipboard/ClipboardMonitorWin.cs

+15-50
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Flow.Launcher.Plugin.ClipboardPlus.Core.Data.Models;
1010
/// ClipboardMonitorWin is a class that monitors the clipboard
1111
/// </summary>
1212
[SupportedOSPlatform("windows10.0.10240.0")]
13-
public class ClipboardMonitorWin : IDisposable
13+
public class ClipboardMonitorWin : IClipboardMonitor
1414
{
1515
#region Fields
1616

@@ -134,6 +134,19 @@ public void StopMonitoring()
134134
}
135135
}
136136

137+
/// <summary>
138+
/// Clears the clipboard of all data.
139+
/// </summary>
140+
public void CleanClipboard()
141+
{
142+
ClipboardText = string.Empty;
143+
ClipboardRtfText = string.Empty;
144+
ClipboardObject = null;
145+
ClipboardImage = null;
146+
ClipboardFile = string.Empty;
147+
ClipboardFiles.Clear();
148+
}
149+
137150
#endregion
138151

139152
#region Private
@@ -170,53 +183,6 @@ internal void Invoke(object? content, DataType type, SourceApplication source)
170183

171184
#endregion
172185

173-
#region Event Arguments
174-
175-
/// <summary>
176-
/// Provides data for the <see cref="ClipboardChanged"/> event.
177-
/// </summary>
178-
public class ClipboardChangedEventArgs : EventArgs
179-
{
180-
public ClipboardChangedEventArgs(
181-
object? content,
182-
DataType dataType,
183-
SourceApplication source
184-
)
185-
{
186-
Content = content;
187-
DataType = dataType;
188-
189-
SourceApplication = new SourceApplication(
190-
source.Handle,
191-
source.Name,
192-
source.Title,
193-
source.Path
194-
);
195-
}
196-
197-
#region Properties
198-
199-
/// <summary>
200-
/// Gets the currently copied clipboard content.
201-
/// </summary>
202-
public object? Content { get; }
203-
204-
/// <summary>
205-
/// Gets the currently copied clipboard content-type.
206-
/// </summary>
207-
public DataType DataType { get; }
208-
209-
/// <summary>
210-
/// Gets the application from where the
211-
/// clipboard's content were copied.
212-
/// </summary>
213-
public SourceApplication SourceApplication { get; }
214-
215-
#endregion
216-
}
217-
218-
#endregion
219-
220186
#endregion
221187

222188
#endregion
@@ -248,8 +214,7 @@ protected virtual void Dispose(bool disposing)
248214
_clipboardHandle.Dispose();
249215
_clipboardHandle = null!;
250216
_observableFormats = null!;
251-
ClipboardFiles = null!;
252-
ClipboardImage = null!;
217+
CleanClipboard();
253218
_disposed = true;
254219
}
255220
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2024 Jack251970
2+
// Licensed under the Apache License. See the LICENSE.
3+
4+
using System.Windows.Media.Imaging;
5+
6+
namespace Flow.Launcher.Plugin.ClipboardPlus.Core.Data.Models;
7+
8+
public interface IClipboardMonitor : IDisposable
9+
{
10+
public bool MonitorClipboard { get; set; }
11+
12+
public bool ObserveLastEntry { get; set; }
13+
14+
public ObservableDataFormats ObservableFormats { get; set; }
15+
16+
public string ClipboardText { get; }
17+
18+
public string ClipboardRtfText { get; }
19+
20+
public object? ClipboardObject { get; }
21+
22+
public BitmapSource? ClipboardImage { get; }
23+
24+
public string ClipboardFile { get; }
25+
26+
public List<string> ClipboardFiles { get; }
27+
28+
public void SetContext(PluginInitContext context);
29+
30+
public void StartMonitoring();
31+
32+
public void PauseMonitoring();
33+
34+
public void ResumeMonitoring();
35+
36+
public void StopMonitoring();
37+
38+
public void CleanClipboard();
39+
40+
public event EventHandler<ClipboardChangedEventArgs>? ClipboardChanged;
41+
}

0 commit comments

Comments
 (0)