Skip to content

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

feat(domain): add method for getting the title of a window

Lars Berger5a8f6fab7a01
diff --git a/LarsWM.Domain/Windows/WindowService.cs b/LarsWM.Domain/Windows/WindowService.cs
index 11c45670..d38af076 100644
--- a/LarsWM.Domain/Windows/WindowService.cs
+++ b/LarsWM.Domain/Windows/WindowService.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@@ -28,7 +28,7 @@ namespace LarsWM.Domain.Windows
public IEnumerable<Window> GetWindows()
{
return _containerService.ContainerTree.TraverseDownEnumeration()
- .OfType<Window>();
+ .OfType<Window>();
}
/// <summary>
@@ -37,7 +37,7 @@ namespace LarsWM.Domain.Windows
public IEnumerable<Window> GetWindowsOfParentContainer(Container parent)
{
return parent.TraverseDownEnumeration()
- .OfType<Window>();
+ .OfType<Window>();
}
/// <summary>
@@ -47,28 +47,20 @@ namespace LarsWM.Domain.Windows
{
uint processId;
GetWindowThreadProcessId(handle, out processId);
-
- try
- {
- return Process.GetProcesses().First(process => process.Id == (int)processId);
- }
- catch (InvalidOperationException)
- {
- return null;
- }
+ return Process.GetProcesses().FirstOrDefault(process => process.Id == (int)processId);
}
/// <summary>
- /// Get the name of the class of the window.
+ /// Get the class name of the specified window.
/// </summary>
public string GetClassNameOfHandle(IntPtr handle)
{
- var buffer = new StringBuilder(255);
- GetClassName(handle, buffer, buffer.Capacity + 1);
+ // Class name is limited to 256 characters, so it's fine to use a fixed size buffer.
+ var buffer = new StringBuilder(256);
+ GetClassName(handle, buffer, buffer.Capacity);
return buffer.ToString();
}
-
/// <summary>
/// Get dimensions of the bounding rectangle of the specified window.
/// </summary>
@@ -79,6 +71,21 @@ namespace LarsWM.Domain.Windows
return rect;
}
+ /// <summary>
+ /// Get title bar text of the specified window.
+ /// </summary>
+ public string GetTitleOfHandle(IntPtr handle)
+ {
+ int titleLength = GetWindowTextLength(handle);
+
+ if (titleLength == 0)
+ return String.Empty;
+
+ var buffer = new StringBuilder(titleLength + 1);
+ GetWindowText(handle, buffer, buffer.Capacity);
+ return buffer.ToString();
+ }
+
public WS_EX GetWindowStylesEx(IntPtr handle)
{
return unchecked((WS_EX)GetWindowLongPtr(handle, (int)(GWL_EXSTYLE)).ToInt64());