aboutsummaryrefslogtreecommitdiff
path: root/src/GetWindows.cs
blob: 3eeb81ce0881b14b01fe05be7dc4a5d794453c98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
namespace hyprwatch.Window 
{
  using System;
  using System.Diagnostics;
  using System.Text.RegularExpressions;

  public class GetWindows
  {
    public static string ActiveWindow()
    {
      string? activeWindow = null;

      try {
        Process process = new Process
        {
          StartInfo = new ProcessStartInfo
          {
            FileName = "hyprctl",
            Arguments = "activewindow",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
          }
        };

        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

        var classMatch = Regex.Match(output, @"class:(.+)");

        if(classMatch.Success)
        {
          activeWindow = classMatch.Groups[1].Value.Trim();
        }
      }
      catch(Exception ex)
      {
        Console.WriteLine(ex.Message);
      }

      if(activeWindow == null)
      {
        activeWindow = "Home-Screen";
      }

      return activeWindow ?? string.Empty;
    }
  }
}