Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Sep 14, 2023
1 parent e99b77b commit 18d7b71
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,11 @@ private void SetWindowPosition(Window window)

// Show or hide the window depending on whether the workspace is displayed.
if (isWorkspaceDisplayed)
{
defaultFlags |= SetWindowPosFlags.ShowWindow;
// if (window.DisplayState is DisplayState.Hidden or DisplayState.Hiding)
// window.DisplayState = DisplayState.Showing;
}
else
{
defaultFlags |= SetWindowPosFlags.HideWindow;
// if (window.DisplayState is DisplayState.Shown or DisplayState.Showing)
// window.DisplayState = DisplayState.Hiding;
}

// Transition display state depending on whether window will be shown/hidden.
window.DisplayState = window.DisplayState switch
{
DisplayState.Hidden or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public CommandResponse Handle(UnmanageWindowCommand command)
if (focusTarget is null)
return CommandResponse.Ok;

// The OS automatically switches focus to a different window after closing. If
// there are focusable windows, then set focus *after* the OS sets focus. This will
// cause focus to briefly flicker to the OS focus target and then to the WM's focus
// target.
_bus.Invoke(new SetFocusedDescendantCommand(focusTarget));
_containerService.HasPendingFocusSync = true;
_windowService.UnmanagedOrMinimizedStopwatch.Restart();
Expand Down
14 changes: 10 additions & 4 deletions GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void Handle(WindowFocusedEvent @event)
var window = _windowService.GetWindows()
.FirstOrDefault(window => window.Handle == @event.WindowHandle);

// Ignore event if window is unmanaged or being hidden by the WM.
if (window is null || window.DisplayState is DisplayState.Hiding)
// if (window is null || window.DisplayState is DisplayState.Hidden)
return;

_logger.LogWindowEvent("Native focus event", window);
Expand All @@ -52,19 +52,25 @@ public void Handle(WindowFocusedEvent @event)

var unmanagedStopwatch = _windowService.UnmanagedOrMinimizedStopwatch;

// Handle overriding focus on close/minimize. After a window is closed or minimized,
// the OS or the closed application might automatically switch focus to a different
// window. To force focus to go to the WM's target focus container, we reassign any
// focus events 100ms after close/minimize. This will cause focus to briefly flicker
// to the OS focus target and then to the WM's focus target.
if (unmanagedStopwatch.IsRunning && unmanagedStopwatch.ElapsedMilliseconds < 100)
{
_logger.LogDebug("Overriding native focus.");
_bus.Invoke(new SyncNativeFocusCommand());
return;
}

// TODO: Need to return early for other display states.
// TODO: Should this be moved to `WindowShownHandler`. Is show event
// emitted first, or foreground?
// Handle focus events from windows on hidden workspaces. For example, if Discord
// is forcefully shown by the OS when it's on a hidden workspace, switch focus to
// Discord's workspace.
if (window.DisplayState is DisplayState.Hidden)
{
_logger.LogWindowEvent("Focusing off-screen window", window);

var workspace = WorkspaceService.GetWorkspaceFromChildContainer(window);
_bus.Invoke(new FocusWorkspaceCommand(workspace.Name));
_bus.Invoke(new SetFocusedDescendantCommand(window));
Expand Down
26 changes: 4 additions & 22 deletions GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ public void Handle(WindowHiddenEvent @event)

_logger.LogWindowEvent("Window hidden", window);

// Update display state.
// Update the display state.
if (window.DisplayState is DisplayState.Hiding)
{
window.DisplayState = DisplayState.Hidden;
return;
}

// Detach the hidden window from its parent.
// Unmanage the window if it's not in a display state transition. Also, since window
// events are not 100% guaranteed to be in correct order, we need to ignore events
// where the window is not actually hidden.
if (window.DisplayState is DisplayState.Shown && !WindowService.IsHandleVisible(window.Handle))
{
_bus.Invoke(new UnmanageWindowCommand(window));
Expand All @@ -63,23 +65,3 @@ public void Handle(WindowHiddenEvent @event)
}
}
}

// Scenario 1:
// window hiding (due to focus defocus)
// window showing (due to focus refocus)
// window hidden event
// window shown event

// Scenario 2:
// window hiding (due to focus defocus)
// window hidden event
// window showing (due to focus refocus)
// window shown event

// Scenario 3:
// window hiding (due to focus defocus)
// window showing (due to focus refocus)
// window hiding (due to focus defocus)
// window hidden event
// window shown event
// window hidden event
3 changes: 1 addition & 2 deletions GlazeWM.Domain/Windows/EventHandlers/WindowShownHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Diagnostics;
using System.Linq;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Common.Enums;
using GlazeWM.Domain.Common.Utils;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Monitors.Commands;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Infrastructure.Bussing;
Expand Down
12 changes: 1 addition & 11 deletions GlazeWM.Domain/Windows/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@ public abstract class Window : Container
/// <summary>
/// Whether window is shown, hidden, or in an intermediary state.
/// </summary>
private DisplayState _displayState = DisplayState.Shown;
public DisplayState DisplayState
{
get => _displayState;
set
{
var Asdf = Enum.GetName(value.GetType(), value);
Debug.WriteLine($"Display state changed {ProcessName} {Title}: {Asdf}");
_displayState = value;
}
}
public DisplayState DisplayState { get; set; } = DisplayState.Shown;

/// <summary>
/// The placement of the window when floating. Initialized with window's placement on launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public CommandResponse Handle(FocusWorkspaceCommand command)
// Save currently focused workspace as recent for command "recent"
_workspaceService.MostRecentWorkspace = focusedWorkspace;

_logger.LogDebug("WorkspaceToFocus: {WorkspaceToFocusName}", workspaceToFocus.Name);

// Set focus to the last focused window in workspace. If the workspace has no descendant
// windows, then set focus to the workspace itself.
_logger.LogDebug("WorkspaceToFocus: {WorkspaceToFocusName}", workspaceToFocus.Name);
var containerToFocus = workspaceToFocus.HasChildren()
? workspaceToFocus.LastFocusedDescendant
: workspaceToFocus;
Expand Down

0 comments on commit 18d7b71

Please sign in to comment.