Skip to content

Commit

Permalink
feat: 避免重复创建截图页面 && 添加ci配置
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jul 30, 2024
1 parent d9e7304 commit deef265
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 11 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: .NET
on:
release:
types: [ published ]
push:
tags:
- "*" # Push events to matching *, i.e. 1.0, 20.15.10

jobs:
build:
strategy:
matrix:
dotnet: [ '8.0.x' ]
os: [ windows-latest ]

runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} - ${{ matrix.dotnet }}
env:
DOTNET_NOLOGO: true

steps:
- name: Getting code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ matrix.dotnet }}

- name: Clean
run: dotnet clean --configuration Release && dotnet nuget locals all --clear

- name: Build
run: dotnet build --configuration Release --nologo

# - name: Run tests
# run: dotnet test --configuration Release --nologo

- name: Pack
run: dotnet pack

- name: Push
run: dotnet nuget push -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_KEY }} artifacts\**.nupkg
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Install-Package ScreenGrab

- [ScreenGrab](./src/ScreenGrab/README.md)

### Notes

This feature is extracted from[Text-Grab](https://github.com/TheJoeFin/Text-Grab)

### Contacts

* [mail](mailto:zggsong@foxmail.com)
1 change: 1 addition & 0 deletions ScreenGrab.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
LICENSE = LICENSE
README.md = README.md
Directory.Packages.props = Directory.Packages.props
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScreenGrab.Sample", "tests\ScreenGrab.Sample\ScreenGrab.Sample.csproj", "{94390E18-11B6-413E-8571-BCE129F7B3D8}"
Expand Down
5 changes: 5 additions & 0 deletions src/ScreenGrab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

## Usage

```csharp
BitmapSource bs;
ScreenGrabber.OnCaptured = bitmap => bs = bitmap.ToImageSource();
ScreenGrabber.Capture();
```
5 changes: 5 additions & 0 deletions src/ScreenGrab/ScreenGrabView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace ScreenGrab;

/// <summary>
/// Extracted from the project <see href="https://github.com/TheJoeFin/Text-Grab"/>
/// </summary>
public partial class ScreenGrabView
{
#region Constructors
Expand All @@ -26,6 +29,7 @@ public ScreenGrabView(Action<Bitmap>? action)

#region Properties

public Action? OnGrabClose { get; set; }
private DisplayInfo? CurrentScreen { get; set; }

#endregion Properties
Expand Down Expand Up @@ -75,6 +79,7 @@ private void CloseAllFullscreenGrabs()
foreach (var window in Application.Current.Windows)
if (window is ScreenGrabView sgv)
sgv.Close();
OnGrabClose?.Invoke();
}

private void FreezeUnfreezeAllScreenGrabs()
Expand Down
19 changes: 15 additions & 4 deletions src/ScreenGrab/Grab.cs → src/ScreenGrab/ScreenGrabber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@

namespace ScreenGrab;

public class Grab
public abstract class ScreenGrabber
{
public Action<Bitmap>? OnImageCaptured { get; set; }
private static bool _isCapturing;
public static Action<Bitmap>? OnCaptured { get; set; }

public void Capture()
public static void Capture()
{
if (_isCapturing) return;

_isCapturing = true;

var allDisplayInfos = DisplayInfo.AllDisplayInfos;
var allScreenGrab = Application.Current.Windows.OfType<ScreenGrabView>().ToList();
var numberOfScreenGrabWindowsToCreate = allDisplayInfos.Length - allScreenGrab.Count;

for (var i = 0; i < numberOfScreenGrabWindowsToCreate; i++)
allScreenGrab.Add(new ScreenGrabView(OnImageCaptured));
{
var view = new ScreenGrabView(OnCaptured)
{
OnGrabClose = () => _isCapturing = false
};
allScreenGrab.Add(view);
}

const double sideLength = 40;

Expand Down
4 changes: 2 additions & 2 deletions tests/ScreenGrab.Sample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<ui:TitleBar Title="ScreenGrab Sample" />
<ui:Card Grid.Row="1" Margin="8">
<StackPanel Orientation="Horizontal">
<Button Content="Capture" Margin="2 0" Click="Capture_Click" />
<Button Content="Clean" Margin="2 0" Click="Clean_Click" />
<Button Margin="2 0" Content="Capture(Win+Shift+A)" Click="Capture_Click" />
<Button Margin="2 0" Content="Clean" Click="Clean_Click" />
</StackPanel>
</ui:Card>
<ui:DynamicScrollViewer Grid.Row="2" Margin="8">
Expand Down
7 changes: 2 additions & 5 deletions tests/ScreenGrab.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ private void Capture()
{
Clean();

var grab = new Grab
{
OnImageCaptured = bitmap => Img.Source = bitmap.ToImageSource()
};
grab.Capture();
ScreenGrabber.OnCaptured = bitmap => Img.Source = bitmap.ToImageSource();
ScreenGrabber.Capture();
}

private void Capture(object? sender, HotkeyEventArgs e)
Expand Down

0 comments on commit deef265

Please sign in to comment.