From 793438cf780c78cd61faa73e6628e67adc07918e Mon Sep 17 00:00:00 2001 From: erri120 Date: Tue, 18 Aug 2020 10:15:19 +0200 Subject: [PATCH] Cleanup --- .../Hotkey/GlobalHotkeyService.cs | 84 ----------------- ScreenshotPlugin/Hotkey/Hotkey.cs | 94 +++++++++++++++++++ .../HotkeySelectionControl.xaml.cs | 1 - ScreenshotPlugin/NativeFunctions.cs | 46 +++++++++ ScreenshotPlugin/NativeUtils.cs | 25 +---- ScreenshotPlugin/ScreenshotExtensionPlugin.cs | 14 +-- ScreenshotPlugin/ScreenshotPlugin.csproj | 2 + ScreenshotPlugin/ScreenshotPluginSettings.cs | 9 +- ScreenshotPlugin/ScreenshotSettingsView.xaml | 2 + ScreenshotPlugin/ShareX/CaptureHelpers.cs | 51 +++++----- ScreenshotPlugin/ShareX/Screenshot.cs | 24 ++--- 11 files changed, 189 insertions(+), 163 deletions(-) create mode 100644 ScreenshotPlugin/Hotkey/Hotkey.cs create mode 100644 ScreenshotPlugin/NativeFunctions.cs diff --git a/ScreenshotPlugin/Hotkey/GlobalHotkeyService.cs b/ScreenshotPlugin/Hotkey/GlobalHotkeyService.cs index fe25e11..b62ce83 100644 --- a/ScreenshotPlugin/Hotkey/GlobalHotkeyService.cs +++ b/ScreenshotPlugin/Hotkey/GlobalHotkeyService.cs @@ -17,102 +17,18 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; -using System.Text; using System.Windows.Forms; using System.Windows.Input; using Extensions.Common; using JetBrains.Annotations; -using Newtonsoft.Json; using Playnite.SDK; namespace ScreenshotPlugin { //partially from https://tyrrrz.me/blog/wndproc-in-wpf - public static partial class NativeFunctions - { - [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)] - public static extern ushort GlobalAddAtom(string lpString); - [DllImport("kernel32.dll", CharSet = CharSet.Auto)] - public static extern ushort GlobalDeleteAtom(ushort nAtom); - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - public static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKeys fsModifiers, int vk); - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool UnregisterHotKey(IntPtr hWnd, int id); - } - - public enum HotkeyStatus - { - [Description("Registered")] - Registered, - [Description("Failed")] - Failed, - [Description("Not Configured")] - NotConfigured - } - - public class Hotkey - { - [JsonIgnore] - public ushort ID { get; set; } - [JsonProperty] - public Key KeyCode { get; set; } = Key.None; - [JsonProperty] - public HotkeyStatus Status { get; set; } = HotkeyStatus.NotConfigured; - [JsonProperty] - public ModifierKeys KeyModifiers { get; set; } = ModifierKeys.None; - [JsonIgnore] - public bool IsValidHotkey => KeyCode != Key.None; - - public string DebugString() - { - return $"{(ID == 0 ? "" : $"ID: {ID} ")}Keys: \"{ToString()}\" Status: {Enum.GetName(typeof(HotkeyStatus), Status)}"; - } - - public override string ToString() - { - var str = new StringBuilder(); - - if (KeyModifiers.HasFlag(ModifierKeys.Control)) - str.Append("Ctrl + "); - if (KeyModifiers.HasFlag(ModifierKeys.Shift)) - str.Append("Shift + "); - if (KeyModifiers.HasFlag(ModifierKeys.Alt)) - str.Append("Alt + "); - if (KeyModifiers.HasFlag(ModifierKeys.Windows)) - str.Append("Win + "); - - str.Append(KeyCode); - - return str.ToString(); - } - } - - public class HotkeyComparer : IEqualityComparer - { - public bool Equals(Hotkey x, Hotkey y) - { - if (x == null) return false; - if (y == null) return false; - return x.ID == y.ID && x.KeyCode == y.KeyCode && x.Status == y.Status; - } - - public int GetHashCode(Hotkey obj) - { - unchecked - { - var hashCode = (int) obj.ID; - hashCode = (hashCode * 397) ^ (int) obj.KeyCode; - hashCode = (hashCode * 397) ^ (int) obj.Status; - return hashCode; - } - } - } - public class GlobalHotkeyService : IDisposable { private readonly SpongeWindow _sponge; diff --git a/ScreenshotPlugin/Hotkey/Hotkey.cs b/ScreenshotPlugin/Hotkey/Hotkey.cs new file mode 100644 index 0000000..ad2f35b --- /dev/null +++ b/ScreenshotPlugin/Hotkey/Hotkey.cs @@ -0,0 +1,94 @@ +// /* +// Copyright (C) 2020 erri120 +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// */ + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; +using System.Windows.Input; +using Newtonsoft.Json; + +namespace ScreenshotPlugin +{ + public enum HotkeyStatus + { + [Description("Registered")] + Registered, + [Description("Failed")] + Failed, + [Description("Not Configured")] + NotConfigured + } + + public class Hotkey + { + [JsonIgnore] + public ushort ID { get; set; } + [JsonProperty] + public Key KeyCode { get; set; } = Key.None; + [JsonProperty] + public HotkeyStatus Status { get; set; } = HotkeyStatus.NotConfigured; + [JsonProperty] + public ModifierKeys KeyModifiers { get; set; } = ModifierKeys.None; + [JsonIgnore] + public bool IsValidHotkey => KeyCode != Key.None; + + public string DebugString() + { + return $"{(ID == 0 ? "" : $"ID: {ID} ")}Keys: \"{ToString()}\" Status: {Enum.GetName(typeof(HotkeyStatus), Status)}"; + } + + public override string ToString() + { + var str = new StringBuilder(); + + if (KeyModifiers.HasFlag(ModifierKeys.Control)) + str.Append("Ctrl + "); + if (KeyModifiers.HasFlag(ModifierKeys.Shift)) + str.Append("Shift + "); + if (KeyModifiers.HasFlag(ModifierKeys.Alt)) + str.Append("Alt + "); + if (KeyModifiers.HasFlag(ModifierKeys.Windows)) + str.Append("Win + "); + + str.Append(KeyCode); + + return str.ToString(); + } + } + + public class HotkeyComparer : IEqualityComparer + { + public bool Equals(Hotkey x, Hotkey y) + { + if (x == null) return false; + if (y == null) return false; + return x.ID == y.ID && x.KeyCode == y.KeyCode && x.Status == y.Status; + } + + public int GetHashCode(Hotkey obj) + { + unchecked + { + var hashCode = (int) obj.ID; + hashCode = (hashCode * 397) ^ (int) obj.KeyCode; + hashCode = (hashCode * 397) ^ (int) obj.Status; + return hashCode; + } + } + } +} \ No newline at end of file diff --git a/ScreenshotPlugin/HotkeySelectionControl.xaml.cs b/ScreenshotPlugin/HotkeySelectionControl.xaml.cs index 9f24d04..d08e11e 100644 --- a/ScreenshotPlugin/HotkeySelectionControl.xaml.cs +++ b/ScreenshotPlugin/HotkeySelectionControl.xaml.cs @@ -20,7 +20,6 @@ public Hotkey Hotkey public HotkeySelectionControl() { InitializeComponent(); - //DataContext = this; } private void HotkeyTextBox_PreviewKeyDown(object sender, KeyEventArgs e) diff --git a/ScreenshotPlugin/NativeFunctions.cs b/ScreenshotPlugin/NativeFunctions.cs new file mode 100644 index 0000000..fe27815 --- /dev/null +++ b/ScreenshotPlugin/NativeFunctions.cs @@ -0,0 +1,46 @@ +// /* +// Copyright (C) 2020 erri120 +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// */ + +using System; +using System.Runtime.InteropServices; +using System.Windows.Input; +using PInvoke; + +namespace ScreenshotPlugin +{ + public static class NativeFunctions + { + [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)] + public static extern ushort GlobalAddAtom(string lpString); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + public static extern ushort GlobalDeleteAtom(ushort nAtom); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKeys fsModifiers, int vk); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + [DllImport("dwmapi.dll", PreserveSig = false)] + public static extern bool DwmIsCompositionEnabled(); + + [DllImport("dwmapi.dll")] + public static extern int DwmGetWindowAttribute(IntPtr hwnd, DwmApi.DWMWINDOWATTRIBUTE dwAttribute, out RECT pvAttribute, int cbAttribute); + } +} \ No newline at end of file diff --git a/ScreenshotPlugin/NativeUtils.cs b/ScreenshotPlugin/NativeUtils.cs index 3a56bff..0bc0933 100644 --- a/ScreenshotPlugin/NativeUtils.cs +++ b/ScreenshotPlugin/NativeUtils.cs @@ -22,22 +22,12 @@ namespace ScreenshotPlugin { public static class NativeUtils { - public static int GetX(this RECT r) - { - return r.left; - } - - public static int GetY(this RECT r) - { - return r.top; - } - - public static int GetWidth(this RECT r) + private static int GetWidth(this RECT r) { return r.right - r.left; } - public static int GetHeight(this RECT r) + private static int GetHeight(this RECT r) { return r.bottom - r.top; } @@ -46,16 +36,5 @@ public static Rectangle ToRectangle(this RECT r) { return new Rectangle(r.left, r.top, r.GetWidth(), r.GetHeight()); } - - public static RECT ToRECT(this Rectangle r) - { - return new RECT - { - left = r.Left, - top = r.Top, - bottom = r.Bottom, - right = r.Right - }; - } } } \ No newline at end of file diff --git a/ScreenshotPlugin/ScreenshotExtensionPlugin.cs b/ScreenshotPlugin/ScreenshotExtensionPlugin.cs index a1221c6..bb5d258 100644 --- a/ScreenshotPlugin/ScreenshotExtensionPlugin.cs +++ b/ScreenshotPlugin/ScreenshotExtensionPlugin.cs @@ -56,21 +56,23 @@ private void GlobalHotkeyServiceOnHotkeyPress(Hotkey hotkey) { try { + if (_currentlyRunningGame == null && _settings.OnlyGameScreenshots) + return; + Bitmap bitmap; - var screenshot = new Screenshot(); string region; if (hotkey == _settings.CaptureFullscreenHotkey) { - bitmap = screenshot.CaptureFullscreen(); + bitmap = Screenshot.CaptureFullscreen(); region = "fullscreen"; } else if (hotkey == _settings.CaptureActiveMonitorHotkey) { - bitmap = screenshot.CaptureActiveMonitor(); + bitmap = Screenshot.CaptureActiveMonitor(); region = "active monitor"; } else if (hotkey == _settings.CaptureActiveWindowHotkey) { - bitmap = screenshot.CaptureActiveWindow(); + bitmap = Screenshot.CaptureActiveWindow(); region = "active window"; } else @@ -105,9 +107,9 @@ private void GlobalHotkeyServiceOnHotkeyPress(Hotkey hotkey) { Process.Start(path); } - catch (Exception) + catch (Exception inner) { - //ignored + _logger.Error(inner, $"Exception while opening {path}"); } })); } diff --git a/ScreenshotPlugin/ScreenshotPlugin.csproj b/ScreenshotPlugin/ScreenshotPlugin.csproj index c1dcade..c0938ba 100644 --- a/ScreenshotPlugin/ScreenshotPlugin.csproj +++ b/ScreenshotPlugin/ScreenshotPlugin.csproj @@ -48,7 +48,9 @@ HotkeySelectionControl.xaml + + diff --git a/ScreenshotPlugin/ScreenshotPluginSettings.cs b/ScreenshotPlugin/ScreenshotPluginSettings.cs index dc3ea32..1622acb 100644 --- a/ScreenshotPlugin/ScreenshotPluginSettings.cs +++ b/ScreenshotPlugin/ScreenshotPluginSettings.cs @@ -16,11 +16,9 @@ // */ using System.Collections.Generic; -using System.Drawing.Imaging; using System.IO; using Extensions.Common; using JetBrains.Annotations; -using Newtonsoft.Json; using Playnite.SDK; namespace ScreenshotPlugin @@ -31,6 +29,7 @@ public class ScreenshotPluginSettings : ISettings //public bool SaveToGame { get; set; } //public bool SaveToFolder { get; set; } + public bool OnlyGameScreenshots { get; set; } public string ScreenshotsPath { get; set; } public Hotkey CaptureActiveMonitorHotkey { get; set; } public Hotkey CaptureActiveWindowHotkey { get; set; } @@ -49,6 +48,7 @@ public ScreenshotPluginSettings(ScreenshotExtensionPlugin plugin) { //SaveToGame = savedSettings.SaveToGame; //SaveToFolder = savedSettings.SaveToFolder; + OnlyGameScreenshots = savedSettings.OnlyGameScreenshots; ScreenshotsPath = savedSettings.ScreenshotsPath; CaptureActiveMonitorHotkey = savedSettings.CaptureActiveMonitorHotkey; CaptureActiveWindowHotkey = savedSettings.CaptureActiveWindowHotkey; @@ -90,11 +90,6 @@ public bool VerifySettings(out List errors) { errors = new List(); - /*if (SaveToFolder && ScreenshotsPath.IsEmpty()) - { - errors.Add("Screenshots Folder Path must not be empty if you want to save the screenshots to a folder!"); - }*/ - if (ScreenshotsPath.IsEmpty()) { errors.Add("Screenshots Folder Path must not be empty!"); diff --git a/ScreenshotPlugin/ScreenshotSettingsView.xaml b/ScreenshotPlugin/ScreenshotSettingsView.xaml index d2ee5b0..c199588 100644 --- a/ScreenshotPlugin/ScreenshotSettingsView.xaml +++ b/ScreenshotPlugin/ScreenshotSettingsView.xaml @@ -8,6 +8,8 @@ d:DataContext="{d:DesignInstance local:ScreenshotPluginSettings}" d:DesignHeight="450" d:DesignWidth="800"> + Only take Screenshots when in a game + diff --git a/ScreenshotPlugin/ShareX/CaptureHelpers.cs b/ScreenshotPlugin/ShareX/CaptureHelpers.cs index c854051..2d3000b 100644 --- a/ScreenshotPlugin/ShareX/CaptureHelpers.cs +++ b/ScreenshotPlugin/ShareX/CaptureHelpers.cs @@ -29,27 +29,29 @@ namespace ScreenshotPlugin.ShareX { public static class CaptureHelpers { - public static readonly Version OSVersion = Environment.OSVersion.Version; + private static readonly Version OSVersion = Environment.OSVersion.Version; - public static Rectangle GetScreenBounds() + private static bool IsWindowsVistaOrGreater() { - return SystemInformation.VirtualScreen; + return OSVersion.Major >= 6; } - public static bool IsWindowsVistaOrGreater() + private static bool IsWindows10OrGreater(int build = -1) { - return OSVersion.Major >= 6; + return OSVersion.Major >= 10 && OSVersion.Build >= build; } - [DllImport("dwmapi.dll", PreserveSig = false)] - public static extern bool DwmIsCompositionEnabled(); - - public static bool IsDWMEnabled() + public static Rectangle GetScreenBounds() { - return IsWindowsVistaOrGreater() && DwmIsCompositionEnabled(); + return SystemInformation.VirtualScreen; } - - public static Point GetCursorPosition() + + private static bool IsDWMEnabled() + { + return IsWindowsVistaOrGreater() && NativeFunctions.DwmIsCompositionEnabled(); + } + + private static Point GetCursorPosition() { if (User32.GetCursorPos(out var point)) { @@ -64,25 +66,16 @@ public static Rectangle GetActiveScreenBounds() return Screen.FromPoint(GetCursorPosition()).Bounds; } - [DllImport("dwmapi.dll")] - public static extern int DwmGetWindowAttribute(IntPtr hwnd, DwmApi.DWMWINDOWATTRIBUTE dwAttribute, out RECT pvAttribute, int cbAttribute); - - public static bool GetExtendedFrameBounds(IntPtr handle, out Rectangle rectangle) + private static bool GetExtendedFrameBounds(IntPtr handle, out Rectangle rectangle) { - //int result = PInvoke.DwmApi.DwmGetWindowAttribute(handle, (int)PInvoke.DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, Marshal.SizeOf(typeof(RECT))); - var result = DwmGetWindowAttribute(handle, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, + var result = NativeFunctions.DwmGetWindowAttribute(handle, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out var rect, Marshal.SizeOf(typeof(RECT))); rectangle = rect.ToRectangle(); return result == 0; } - - public static bool IsWindows10OrGreater(int build = -1) - { - return OSVersion.Major >= 10 && OSVersion.Build >= build; - } - - public static bool GetBorderSize(IntPtr handle, out Size size) + + private static bool GetBorderSize(IntPtr handle, out Size size) { var wi = new User32.WINDOWINFO(); @@ -92,8 +85,8 @@ public static bool GetBorderSize(IntPtr handle, out Size size) return result; } - - public static Rectangle MaximizedWindowFix(IntPtr handle, Rectangle windowRect) + + private static Rectangle MaximizedWindowFix(IntPtr handle, Rectangle windowRect) { if (GetBorderSize(handle, out var size)) { @@ -109,9 +102,7 @@ public static Rectangle GetWindowRectangle(IntPtr handle) if (IsDWMEnabled()) { - Rectangle tempRect; - - if (GetExtendedFrameBounds(handle, out tempRect)) + if (GetExtendedFrameBounds(handle, out var tempRect)) { rect = tempRect; } diff --git a/ScreenshotPlugin/ShareX/Screenshot.cs b/ScreenshotPlugin/ShareX/Screenshot.cs index e8d10e5..eae2368 100644 --- a/ScreenshotPlugin/ShareX/Screenshot.cs +++ b/ScreenshotPlugin/ShareX/Screenshot.cs @@ -25,17 +25,17 @@ You should have received a copy of the GNU General Public License namespace ScreenshotPlugin.ShareX { - public class Screenshot + public static class Screenshot { - public bool CaptureClientArea { get; set; } = false; + /*public bool CaptureClientArea { get; set; } = false; public bool RemoveOutsideScreenArea { get; set; } = true; public bool CaptureShadow { get; set; } = false; - public int ShadowOffset { get; set; } = 20; + public int ShadowOffset { get; set; } = 20;*/ [CanBeNull] - public Bitmap CaptureRectangle(Rectangle rect) + private static Bitmap CaptureRectangle(Rectangle rect) { - if (!RemoveOutsideScreenArea) return CaptureRectangleNative(rect); + //if (!RemoveOutsideScreenArea) return CaptureRectangleNative(rect); var bounds = CaptureHelpers.GetScreenBounds(); rect = Rectangle.Intersect(bounds, rect); @@ -44,7 +44,7 @@ public Bitmap CaptureRectangle(Rectangle rect) } [CanBeNull] - public Bitmap CaptureFullscreen() + public static Bitmap CaptureFullscreen() { var bounds = CaptureHelpers.GetScreenBounds(); @@ -52,12 +52,12 @@ public Bitmap CaptureFullscreen() } [CanBeNull] - private Bitmap CaptureWindow(IntPtr handle) + private static Bitmap CaptureWindow(IntPtr handle) { if (handle.ToInt32() <= 0) return null; - Rectangle rect = default; + var rect = CaptureHelpers.GetWindowRectangle(handle); - if (CaptureClientArea) + /*if (CaptureClientArea) { if (PInvoke.User32.GetClientRect(handle, out var r)) rect = r.ToRectangle(); @@ -65,14 +65,14 @@ private Bitmap CaptureWindow(IntPtr handle) else { rect = CaptureHelpers.GetWindowRectangle(handle); - } + }*/ return CaptureRectangle(rect); } [CanBeNull] - public Bitmap CaptureActiveWindow() + public static Bitmap CaptureActiveWindow() { var handle = PInvoke.User32.GetForegroundWindow(); @@ -80,7 +80,7 @@ public Bitmap CaptureActiveWindow() } [CanBeNull] - public Bitmap CaptureActiveMonitor() + public static Bitmap CaptureActiveMonitor() { var bounds = CaptureHelpers.GetActiveScreenBounds();