Skip to content

Commit

Permalink
Use Rx to select screen position.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyone committed May 12, 2015
1 parent 33c623f commit 7f02b2e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 152 deletions.
20 changes: 13 additions & 7 deletions ScreenCaptureWrapper/ScreenCaptureWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Reactive.Core">
<HintPath>..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Interfaces">
<HintPath>..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Linq">
<HintPath>..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.PlatformServices">
<HintPath>..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml" />
Expand All @@ -71,10 +83,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="SelectPositionWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="ShellView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand All @@ -90,9 +98,7 @@
<Compile Include="Preset.cs" />
<Compile Include="Recorder.cs" />
<Compile Include="ScreenCaptureConfig.cs" />
<Compile Include="SelectPositionWindow.xaml.cs">
<DependentUpon>SelectPositionWindow.xaml</DependentUpon>
</Compile>
<Compile Include="SelectScreenPositionUtil.cs" />
<Compile Include="ShellView.xaml.cs">
<DependentUpon>ShellView.xaml</DependentUpon>
</Compile>
Expand Down
18 changes: 0 additions & 18 deletions ScreenCaptureWrapper/SelectPositionWindow.xaml

This file was deleted.

122 changes: 0 additions & 122 deletions ScreenCaptureWrapper/SelectPositionWindow.xaml.cs

This file was deleted.

78 changes: 78 additions & 0 deletions ScreenCaptureWrapper/SelectScreenPositionUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Reactive.Threading.Tasks;

namespace ScreenCaptureWrapper
{
public class SelectScreenPositionUtil
{
public static Task<Rect> SelectScreenPositionAsync()
{
var textBlock = new TextBlock()
{
Foreground = Brushes.White,
FontSize = 12,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left
};

var border = new Border()
{
Background = new SolidColorBrush(Color.FromArgb(0x44, 0x99, 0, 0)),
BorderBrush = Brushes.Red,
BorderThickness = new Thickness(1),
Child = textBlock,
Visibility = Visibility.Hidden
};

var canvas = new Canvas();
canvas.Children.Add(border);

var window = new Window()
{
Title = "Select Screen Position", Top = 0, Left = 0,
Width = SystemParameters.VirtualScreenWidth, Height = SystemParameters.VirtualScreenHeight,
WindowStyle = WindowStyle.None, Topmost = true, ShowInTaskbar = false, AllowsTransparency = true,
Background = new SolidColorBrush(Color.FromArgb(1, 0xff, 0xff, 0xff)),
Cursor = Cursors.Cross, Content = canvas
};

var rectObservable = Observable.FromEventPattern<MouseEventArgs>(window, "MouseLeftButtonDown")
.Select(ev => ev.EventArgs.GetPosition(window))
.CombineLatest(Observable.FromEventPattern<MouseEventArgs>(window, "MouseMove"),
(downPos, ev) => {
var movePos = ev.EventArgs.GetPosition(window);
return new Rect(Math.Min(downPos.X, movePos.X), Math.Min(downPos.Y, movePos.Y),
Math.Abs(downPos.X - movePos.X), Math.Abs(downPos.Y - movePos.Y));
})
.TakeUntil(Observable.FromEventPattern<MouseEventArgs>(window, "MouseLeftButtonUp"));

rectObservable.Subscribe(r =>
{
Canvas.SetLeft(border, r.X);
Canvas.SetTop(border, r.Y);
border.Width = r.Width;
border.Height = r.Height;
border.Visibility = Visibility.Visible;

textBlock.Text = string.Format("{0}x{1} ({2:0.0###}, /8={3}x{4})",
r.Width, r.Height,
r.Height != 0 ? ((double)r.Width) / r.Height : 0,
r.Width / 8.0, r.Height / 8.0);
}, window.Close);

window.Show();

return rectObservable.ToTask(); // return a last value
}
}
}
10 changes: 5 additions & 5 deletions ScreenCaptureWrapper/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ public void SelectFile()

public void PickPosition()
{
SelectPositionWindow.SelectScreenPositionAsync()
SelectScreenPositionUtil.SelectScreenPositionAsync()
.ContinueWith(t =>
{
var exception = t.Exception; // HACK: ignore exception

if (t.Status == TaskStatus.RanToCompletion)
{
var result = t.Result;
this.VideoX = result.X;
this.VideoY = result.Y;
this.VideoWidth = result.Width;
this.VideoHeight = result.Height;
this.VideoX = (int) result.X;
this.VideoY = (int) result.Y;
this.VideoWidth = (int) result.Width;
this.VideoHeight = (int) result.Height;
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions ScreenCaptureWrapper/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
<package id="Caliburn.Micro.Core" version="2.0.2" targetFramework="net45" />
<package id="Cottle" version="1.3.1.2" targetFramework="net45" />
<package id="MSBuildTasks" version="1.4.0.88" targetFramework="net45" />
<package id="Rx-Core" version="2.2.5" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="net45" />
<package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
<package id="Rx-Main" version="2.2.5" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
<package id="YamlDotNet" version="3.5.1" targetFramework="net45" />
</packages>

0 comments on commit 7f02b2e

Please sign in to comment.