Skip to content

Commit

Permalink
Implemented clipboard formats enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
Stadub committed Feb 1, 2015
1 parent 6d75414 commit fd2b983
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 83 deletions.
35 changes: 3 additions & 32 deletions ClipboardHelper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ClipboardWatcher", "ClipboardWatcher\ClipboardWatcher.vcxproj", "{9B6984AF-177E-402F-A6E4-ECC0879684F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClipboardViewer", "ClipboardViewer\ClipboardViewer.csproj", "{E7B6D4C3-94E3-45C9-836D-464661C06856}"
ProjectSection(ProjectDependencies) = postProject
{9B6984AF-177E-402F-A6E4-ECC0879684F7} = {9B6984AF-177E-402F-A6E4-ECC0879684F7}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{9E8BC6A9-F053-4702-B7D9-07202907F2DC}"
EndProject
Expand All @@ -30,10 +33,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClipboardViewer.Test", "Cli
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils.Wpf", "Utils.Wpf\Utils.Wpf.csproj", "{E443A1C6-6077-4C03-A2C7-BA93C748011B}"
EndProject
#Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClipboardViewer.ViewModel.DesignTime", "ClipboardViewer.ViewModel.DesignTime\ClipboardViewer.ViewModel.DesignTime.csproj", "{73988B25-6E61-4BB5-BC4D-A8A568EF8A85}"
#EndProject
#Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClipboardViewer.ViewModel", "ClipboardViewer.ViewModel\ClipboardViewer.ViewModel.csproj", "{A53E25D3-8990-434A-8D79-8F928646597A}"
#EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -174,34 +173,6 @@ Global
{E443A1C6-6077-4C03-A2C7-BA93C748011B}.Release|Win32.ActiveCfg = Release|Any CPU
{E443A1C6-6077-4C03-A2C7-BA93C748011B}.Release|x64.ActiveCfg = Release|Any CPU
{E443A1C6-6077-4C03-A2C7-BA93C748011B}.Release|x86.ActiveCfg = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|Any CPU.Build.0 = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|Win32.ActiveCfg = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|x64.ActiveCfg = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Debug|x86.ActiveCfg = Debug|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|Any CPU.ActiveCfg = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|Any CPU.Build.0 = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|Mixed Platforms.Build.0 = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|Win32.ActiveCfg = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|x64.ActiveCfg = Release|Any CPU
# {73988B25-6E61-4BB5-BC4D-A8A568EF8A85}.Release|x86.ActiveCfg = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|Any CPU.Build.0 = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|Win32.ActiveCfg = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|x64.ActiveCfg = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Debug|x86.ActiveCfg = Debug|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|Any CPU.ActiveCfg = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|Any CPU.Build.0 = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|Win32.ActiveCfg = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|x64.ActiveCfg = Release|Any CPU
# {A53E25D3-8990-434A-8D79-8F928646597A}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
37 changes: 19 additions & 18 deletions ClipboardHelper/Watcher/ClipbordWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static bool WatcherAllreadyRan()

public void StartListen(bool throwIfAnotherListenerStarted = false)
{
if(watcherThread!=null)return;
var startResult = new AsyncResult();
if (proc != null) throw new ClipbordWatcherException("Cannot start second listener istance");

Expand Down Expand Up @@ -117,10 +118,10 @@ private void StartMessageWindow()
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
Arguments = CreateClassName()
};
//procStartInfo.CreateNoWindow = true;

proc = Process.Start(procStartInfo);
}

Expand Down Expand Up @@ -258,7 +259,21 @@ public static uint SequenceNumber
#region Distructor
public void Stop()
{
Dispose();

if (timer != null) timer.Dispose();
if (watcherThread != null)
{
SendMessage(windowHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
proc.Refresh();
if (!proc.HasExited)
{
proc.WaitForExit(10);
proc.Refresh();
if (!proc.HasExited)
proc.Kill();
}
watcherThread.Abort();
}
}

public void Dispose()
Expand All @@ -279,21 +294,7 @@ protected virtual void Dispose(bool disposing)
disposed = true;
waitHandle.Set();
waitHandle.Dispose();
if(timer!=null) timer.Dispose();
if (watcherThread != null)
{
SendMessage(windowHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
proc.Refresh();
if (!proc.HasExited)
{
proc.WaitForExit(10);
proc.Refresh();
if(!proc.HasExited)
proc.Kill();
}
watcherThread.Abort();
}

Stop();
}
#endregion
protected virtual string CreateClassName()
Expand Down
2 changes: 1 addition & 1 deletion ClipboardViewer/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:clipboardViewer="clr-namespace:ClipboardViewer"
xmlns:utils="clr-namespace:Utils;assembly=Utils"
xmlns:designTime="clr-namespace:ClipboardViewer.ViewModel.DesignTime"
StartupUri="MainWindow.xaml">
StartupUri="View/MainWindow.xaml">
<Application.Resources>
<utils:ServiceLocator x:Key="ServiceLocator"/>

Expand Down
6 changes: 1 addition & 5 deletions ClipboardViewer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public partial class App : Application
ServiceLocator container;
protected override void OnStartup(StartupEventArgs e)
{
//ClipbordWatcher watcher = new ClipbordWatcher();
//watcher.StartListen();
//var acessibleFromats = Providers.GetCurrentProviders().ToList();

//container.RegisterInstance<IClipbordWatcher, ClipbordWatcher>(watcher);

//container.RegisterInitializer<IClipboard>(() => Clipboard.CreateReadWrite(watcher));
//container.RegisterType<IClipboard, Clipboard>();

Expand Down
2 changes: 2 additions & 0 deletions ClipboardViewer/Bootstraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using ClipboardHelper;
using ClipboardHelper.FormatProviders;
using ClipboardHelper.Watcher;
using Utils;

namespace ClipboardViewer
Expand All @@ -15,6 +16,7 @@ public Bootstraper(ServiceLocator container)
container.RegisterInitializer<IClipboard>(Clipboard.CreateReadOnly);
container.RegisterInstance<IEnumerable<Func<IClipbordFormatProvider>>, List<Func<IClipbordFormatProvider>>>(accessibleFromats);
container.RegisterInstance<TypeMapper, TypeMapper>(new TypeMapper());
container.RegisterInstance<IClipbordWatcher, ClipbordWatcher>(new ClipbordWatcher());
}
}
}
6 changes: 3 additions & 3 deletions ClipboardViewer/ClipboardViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@
<Compile Include="View\ClipboardContent\TextView.xaml.cs">
<DependentUpon>TextView.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml">
<Page Include="View\MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<Compile Include="View\MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="ClipboardViewStyles.xaml">
<Page Include="View\ClipboardViewStyles.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ContentControl Name="ViewContainer"/>
<ListView Grid.Row="0" Grid.Column="1" Name="LwAvailableFormats" ItemsSource="{Binding }">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<CheckBox Name="CbSubscribe" Grid.Column="1" Grid.Row="1" IsChecked="{Binding AutoUpdate}">Subscribe clipboard changes</CheckBox>
<Border Grid.Row="0" Grid.Column="1" BorderBrush="Teal" BorderThickness="1" Padding="10" >
<DockPanel VerticalAlignment="Stretch">
<ListView Name="LwAvailableFormats" DockPanel.Dock="Top" ItemsSource="{Binding Providers}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<CheckBox VerticalAlignment="Bottom" DockPanel.Dock="Bottom" Name="CbShowUnknown" IsChecked="{Binding ShowUnknownFormats}">Show unknown formats</CheckBox>

</DockPanel>
</Border>
<CheckBox Margin="10" Name="CbSubscribe" Grid.Column="1" Grid.Row="1" IsChecked="{Binding AutoUpdate}">Subscribe clipboard changes</CheckBox>
<Button Name="BtReload" Grid.Row="1" Grid.Column="0" Margin="10" Width="150">Reload clipboard data
<Button.Visibility>
<Binding
Converter="{StaticResource VisibilityConverter }" ConverterParameter="true"
Path="IsChecked" ElementName="CbSubscribe"/>
</Button.Visibility>
<Button.Command>
<Binding>

</Binding>
<Button.Command >
<Binding Path="ReloadClipboardContent" />
</Button.Command>
</Button>
<!--Visibility="{Binding Converter=BooleanToVisibilityConverter, Path=IsChecked, ElementName=CbSubscribe }"-->
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions ClipboardViewer/ViewModel/FormatProviderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ClipboardViewer.ViewModel
{
class FormatProviderViewModel
public class FormatProviderViewModel
{
private readonly IClipbordFormatProvider provider;

Expand Down Expand Up @@ -33,7 +33,7 @@ public override string Name
{
get
{
return string.Format("{{[Unknown]{0}:{1}}}", provider.Id, provider.FormatId);
return string.Format("[Unknown]{0}:{1}", provider.Id, provider.FormatId);
}
}
}
Expand Down
79 changes: 74 additions & 5 deletions ClipboardViewer/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using System.Windows.Threading;
using ClipboardHelper;
using ClipboardHelper.FormatProviders;
using ClipboardHelper.Watcher;
using Utils;
using Utils.Wpf.MvvmBase;

namespace ClipboardViewer.ViewModel
{

public class MainWindowViewModel : ViewModelBase
public class MainWindowViewModel : ViewModelBase,IDisposable
{
private readonly IClipboard clipboard;
private readonly TypeMapper mapper;
private readonly Func<IClipbordFormatProvider>[] clipboardFormats;
private bool autoUpdate;
private ReadOnlyCollection<FormatProviderViewModel> providers;
private IClipbordWatcher clipbordWatcher;
private bool showUnknownFormats;

public MainWindowViewModel(IClipboard clipboard, IEnumerable<Func<IClipbordFormatProvider>> accessibleFromats,TypeMapper mapper)
{
this.clipboard = clipboard;

this.mapper = mapper;
this.clipboardFormats = accessibleFromats as Func<IClipbordFormatProvider>[] ?? accessibleFromats.ToArray();
clipboardFormats.ForEach(clipboard.RegisterFormatProvider);
ReloadClipboardContent= new RelayCommand(UpdateFormats);

ReloadClipboardContent = new RelayCommand(UpdateFormats, () => !AutoUpdate);

Dispatcher.CurrentDispatcher.InvokeAsync(UpdateFormats, DispatcherPriority.Background);
}

[InjectValue]
public IClipbordWatcher ClipbordWatcher
{
get { return clipbordWatcher; }
set
{
if (clipbordWatcher != null)
ClipbordWatcher.OnClipboardContentChanged -= OnClipboardContentChanged;

clipbordWatcher = value;
if (clipbordWatcher != null)
ClipbordWatcher.OnClipboardContentChanged += OnClipboardContentChanged;
}
}

private void OnClipboardContentChanged(object sender, EventArgs<uint> eventArgs)
{
if (AutoUpdate)
UpdateFormats();
}

public bool AutoUpdate
Expand All @@ -33,21 +64,59 @@ public bool AutoUpdate
{
if (autoUpdate == value)return;
autoUpdate = value;
base.OnPropertyChanged();

if (value)
ClipbordWatcher.StartListen();
OnPropertyChanged();
ReloadClipboardContent.CanExecute(null);
}
}

public bool ShowUnknownFormats
{
get { return showUnknownFormats; }
set
{
if (showUnknownFormats == value) return;
showUnknownFormats = value;
OnPropertyChanged();
}
}

public ICommand ReloadClipboardContent { get; set; }

private IEnumerable<IClipbordFormatProvider> GetAvalibleFromats()
{
return clipboard.GetAvalibleFromats();
clipboard.OpenReadOnly();
var formats = clipboard.GetAvalibleFromats(showUnknownFormats).ToList();
clipboard.Close();
return formats;
}

public ReadOnlyCollection<FormatProviderViewModel> Providers
{
get { return providers; }
}

private void UpdateFormats()
{
var formats = GetAvalibleFromats();
var porviderViewModels = new List<FormatProviderViewModel>();
foreach (var provider in GetAvalibleFromats())
{
FormatProviderViewModel providerVM;
if (provider is UnknownFormatProvider)
providerVM = new UnknownFormatProviderViewModel((UnknownFormatProvider) provider);
else
providerVM= new FormatProviderViewModel(provider);
porviderViewModels.Add(providerVM);
}
providers = new ReadOnlyCollection<FormatProviderViewModel>(porviderViewModels);
base.OnPropertyChanged("Providers");
}

public void Dispose()
{
ClipbordWatcher.Stop();
}
}
}

0 comments on commit fd2b983

Please sign in to comment.