Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

refactor(domain): removed WindowsApiFacade, moved methods to WindowService

Larsc2eb79169985
diff --git a/LarsWM.Domain/Common/Services/WindowsApiFacade.cs b/LarsWM.Domain/Common/Services/WindowsApiFacade.cs
deleted file mode 100644
index 3dc1ca3a..00000000
--- a/LarsWM.Domain/Common/Services/WindowsApiFacade.cs
+++ /dev/null
@@ -1,122 +0,0 @@
-using LarsWM.Domain.Windows;
-using LarsWM.Infrastructure.WindowsApi;
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Runtime.InteropServices;
-using System.Text;
-using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
-using static LarsWM.Domain.UserConfigs.UserConfigService;
-
-namespace LarsWM.Domain.Common.Services
-{
- class WindowsApiFacade
- {
- /// <summary>
- /// Returns all top-level windows that match the given predicate.
- /// </summary>
- /// <param name="predicate">The predicate to filter by.</param>
- public static Window[] FilterToplevelWindows(Predicate<Window> predicate)
- {
- List<Window> matchedWindows = new List<Window>();
-
- EnumWindows(new EnumWindowsDelegate(delegate (IntPtr hwnd, int lParam)
- {
- Window window = new Window(hwnd);
-
- if (predicate(window))
- {
- matchedWindows.Add(window);
- }
- return true;
- }), new IntPtr(0));
-
- return matchedWindows.ToArray();
- }
-
- /// <summary>
- /// Get the id of the process that created the window.
- /// </summary>
- public static Process GetProcessOfWindow(Window window)
- {
- uint processId;
- GetWindowThreadProcessId(window.Hwnd, out processId);
-
- try
- {
- return Process.GetProcesses().First(process => process.Id == (int)processId);
- } catch(InvalidOperationException)
- {
- return null;
- }
- }
-
- /// <summary>
- /// Get the name of the class of the window.
- /// </summary>
- public static string GetClassNameOfWindow(Window window)
- {
- var buffer = new StringBuilder(255);
- GetClassName(window.Hwnd, buffer, buffer.Capacity + 1);
- return buffer.ToString();
- }
-
-
- /// <summary>
- /// Get dimensions of the bounding rectangle of the specified window.
- /// </summary>
- public static WindowRect GetLocationOfWindow(Window window)
- {
- WindowRect rect = new WindowRect();
- GetWindowRect(window.Hwnd, ref rect);
- return rect;
- }
-
- public static bool HasWindowStyle(IntPtr hwnd, WS style)
- {
- var styles = unchecked((WS)GetWindowLongPtr(hwnd, (int)(GWL_STYLE)).ToInt64());
-
- return (styles & style) != 0;
- }
-
- public static bool HasWindowExStyle(IntPtr hwnd, WS_EX style)
- {
- var styles = unchecked((WS_EX)GetWindowLongPtr(hwnd, (int)(GWL_EXSTYLE)).ToInt64());
-
- return (styles & style) != 0;
- }
-
- private static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
- {
- if (Environment.Is64BitProcess)
- {
- return GetWindowLongPtr64(hWnd, nIndex);
- }
- else
- {
- return new IntPtr(GetWindowLong32(hWnd, nIndex));
- }
- }
-
- public static bool IsWindowCloaked(Window window)
- {
-
- bool isCloaked;
- DwmGetWindowAttribute(window.Hwnd, DwmWindowAttribute.DWMWA_CLOAKED, out isCloaked, Marshal.SizeOf(typeof(bool)));
- return isCloaked;
- }
-
- public static bool IsWindowManageable(Window window)
- {
-
- if (HasWindowExStyle(window.Hwnd, WS_EX.WS_EX_TOOLWINDOW) ||
- GetWindow(window.Hwnd, GW.GW_OWNER) != IntPtr.Zero)
- {
- return false;
- }
-
- return true;
- }
- }
-}
diff --git a/LarsWM.Domain/Windows/WindowService.cs b/LarsWM.Domain/Windows/WindowService.cs
index 9de02d12..b014ed77 100644
--- a/LarsWM.Domain/Windows/WindowService.cs
+++ b/LarsWM.Domain/Windows/WindowService.cs
@@ -1,17 +1,99 @@
using System;
-using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using LarsWM.Infrastructure.WindowsApi;
+using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
namespace LarsWM.Domain.Windows
{
public class WindowService
{
- public List<Window> Windows { get; set; } = new List<Window>();
public Window FocusedWindow { get; set; } = null;
- public Window GetWindowByHandle(IntPtr handle)
+ /// <summary>
+ /// Get the id of the process that created the window.
+ /// </summary>
+ public Process GetProcessOfHandle(IntPtr handle)
{
- return Windows.FirstOrDefault(w => w.Hwnd == handle);
+ uint processId;
+ GetWindowThreadProcessId(handle, out processId);
+
+ try
+ {
+ return Process.GetProcesses().First(process => process.Id == (int)processId);
+ } catch(InvalidOperationException)
+ {
+ return null;
+ }
+ }
+
+ /// <summary>
+ /// Get the name of the class of the window.
+ /// </summary>
+ public string GetClassNameOfHandle(IntPtr handle)
+ {
+ var buffer = new StringBuilder(255);
+ GetClassName(handle, buffer, buffer.Capacity + 1);
+ return buffer.ToString();
+ }
+
+
+ /// <summary>
+ /// Get dimensions of the bounding rectangle of the specified window.
+ /// </summary>
+ public WindowRect GetLocationOfHandle(IntPtr handle)
+ {
+ WindowRect rect = new WindowRect();
+ GetWindowRect(handle, ref rect);
+ return rect;
+ }
+
+ public bool HandleHasWindowStyle(IntPtr handle, WS style)
+ {
+ var styles = unchecked((WS)GetWindowLongPtr(handle, (int)(GWL_STYLE)).ToInt64());
+
+ return (styles & style) != 0;
+ }
+
+ public bool HandleHasWindowExStyle(IntPtr handle, WS_EX style)
+ {
+ var styles = unchecked((WS_EX)GetWindowLongPtr(handle, (int)(GWL_EXSTYLE)).ToInt64());
+
+ return (styles & style) != 0;
+ }
+
+ private IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
+ {
+ if (Environment.Is64BitProcess)
+ {
+ return GetWindowLongPtr64(hWnd, nIndex);
+ }
+ else
+ {
+ return new IntPtr(GetWindowLong32(hWnd, nIndex));
+ }
+ }
+
+ public bool IsHandleCloaked(IntPtr handle)
+ {
+
+ bool isCloaked;
+ DwmGetWindowAttribute(handle, DwmWindowAttribute.DWMWA_CLOAKED, out isCloaked, Marshal.SizeOf(typeof(bool)));
+ return isCloaked;
+ }
+
+ public bool IsHandleManageable(IntPtr handle)
+ {
+
+ if (HandleHasWindowExStyle(handle, WS_EX.WS_EX_TOOLWINDOW) ||
+ GetWindow(handle, GW.GW_OWNER) != IntPtr.Zero)
+ {
+ return false;
+ }
+
+ return true;
}
}
}