Skip to content

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

feat: created method InitialiseState for populating initial state

Lars44906c41e66f
diff --git a/LarsWM.Core/AppState.cs b/LarsWM.Core/AppState.cs
index ea5f427a..7fc17114 100644
--- a/LarsWM.Core/AppState.cs
+++ b/LarsWM.Core/AppState.cs
@@ -1,21 +1,57 @@
+using LarsWM.Core.Common.Services;
using LarsWM.Core.Monitors;
+using LarsWM.Core.Monitors.Commands;
using LarsWM.Core.UserConfigs;
+using LarsWM.Core.UserConfigs.Commands;
using LarsWM.Core.Windows;
using LarsWM.Core.Workspaces;
+using static LarsWM.Core.WindowsApi.WindowsApiFacade;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.Windows.Forms;
namespace LarsWM.Core
{
public class AppState
{
- // Not sure whether these should be BehaviorSubjects.
+ // TODO: Change List types to BehaviorSubjects?
public List<Monitor> Monitors { get; set; } = new List<Monitor>();
public List<Workspace> Workspaces { get; set; } = new List<Workspace>();
public List<Window> Windows { get; set; } = new List<Window>();
+ public UserConfig UserConfig { get; set; } = null;
- // TODO: Create method InitialiseState that invokes AddMonitorCommand etc?
+ private IBus _bus;
+ private MonitorService _monitorService;
+
+ public AppState(IBus bus, MonitorService monitorService)
+ {
+ _bus = bus;
+ _monitorService = monitorService;
+ }
+
+ public void InitialiseState()
+ {
+ // Read user config file and set its values in state.
+ // TODO: Rename to ReadUserConfigCommand
+ _bus.Invoke(new GetUserConfigCommand());
+
+ // Create a Monitor and consequently a Workspace for each detected Screen.
+ foreach (var screen in Screen.AllScreens)
+ {
+ _bus.Invoke(new AddMonitorCommand(screen));
+ }
+
+ // TODO: move the below code to its own command
+ var windows = GetOpenWindows();
+
+ foreach (var window in windows)
+ {
+ // Add window to its nearest workspace
+ var targetMonitor = _monitorService.GetMonitorFromWindowHandle(window);
+ targetMonitor.DisplayedWorkspace.WindowsInWorkspace.Add(window);
+ }
+ }
}
}
diff --git a/LarsWM.Core/Startup.cs b/LarsWM.Core/Startup.cs
index b623287f..2b147783 100644
--- a/LarsWM.Core/Startup.cs
+++ b/LarsWM.Core/Startup.cs
@@ -19,35 +19,23 @@ namespace LarsWM.Core
class Startup
{
private IBus _bus;
+ private AppState _appState;
private List<Monitor> _monitors = new List<Monitor>();
- public Startup(IBus bus)
+ public Startup(IBus bus, AppState appState)
{
_bus = bus;
+ _appState = appState;
}
public void Init()
{
- _bus.Invoke(new GetUserConfigCommand());
+ // Populate initial monitors, windows, workspaces and user config in AppState.
+ _appState.InitialiseState();
- // Create a Monitor instance for each Screen
- foreach (var screen in Screen.AllScreens)
- {
- _bus.Invoke(new AddMonitorCommand(screen));
- }
-
-
- var windows = GetOpenWindows();
-
- foreach (var window in windows)
- {
- // Debug log
- DumpManagedWindows(window);
+ // Subscribe to windows hooks
- // Add window to its nearest workspace
- var targetMonitor = GetMonitorFromWindowHandle(window);
- targetMonitor.DisplayedWorkspace.WindowsInWorkspace.Add(window);
- }
+ // Create a command for forcing the initial layout
foreach (var monitor in _monitors)
{
@@ -95,12 +83,6 @@ namespace LarsWM.Core
Debug.WriteLine(window.CanLayout);
}
- public Monitor GetMonitorFromWindowHandle(Window window)
- {
- var screen = Screen.FromHandle(window.Hwnd);
- return _monitors.FirstOrDefault(m => m.Screen.DeviceName == screen.DeviceName) ?? _monitors[0];
- }
-
public void ShiftFocusInDirection(MovementDirection direction)
{ }