Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce redraws by redrawing only at the end of keybindings + ce…
Browse files Browse the repository at this point in the history
…rtain events (glzr-io#388)
HolbyFPV authored and AryanSolanki637 committed Sep 18, 2023
1 parent fd37dc0 commit 8882e8d
Showing 22 changed files with 30 additions and 50 deletions.
1 change: 1 addition & 0 deletions GlazeWM.App.WindowManager/WmStartup.cs
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ public ExitCode Run()

// Populate initial monitors, windows, workspaces and user config.
_bus.Invoke(new PopulateInitialStateCommand());
_bus.Invoke(new RedrawContainersCommand());

// Listen on registered keybindings.
_keybindingService.Start();
Original file line number Diff line number Diff line change
@@ -45,8 +45,6 @@ public CommandResponse Handle(PopulateInitialStateCommand command)
if (_windowService.IsHandleAppBar(windowHandle))
_windowService.AppBarHandles.Add(windowHandle);

_bus.Invoke(new RedrawContainersCommand());

// Get the originally focused window when the WM is started.
var focusedWindow =
_windowService.GetWindows().FirstOrDefault(window => window.Handle == focusedHandle);
@@ -85,7 +83,7 @@ private void PopulateContainerTree()
var targetMonitor = _monitorService.GetMonitorFromHandleLocation(windowHandle);
var targetWorkspace = targetMonitor.DisplayedWorkspace;

_bus.Invoke(new ManageWindowCommand(windowHandle, targetWorkspace, false));
_bus.Invoke(new ManageWindowCommand(windowHandle, targetWorkspace));
}
}
}
Original file line number Diff line number Diff line change
@@ -78,7 +78,6 @@ public CommandResponse Handle(RefreshMonitorStateCommand command)

// Redraw full container tree.
_containerService.ContainersToRedraw.Add(_containerService.ContainerTree);
_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.UserConfigs.Commands;
using GlazeWM.Domain.Windows;
using GlazeWM.Infrastructure.Bussing;
@@ -45,11 +46,15 @@ public CommandResponse Handle(RegisterKeybindingsCommand command)
{
try
{
// Avoid invoking keybinding if an ignored window currently has focus.
if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))
return;
lock (_bus.LockObj)
{
// Avoid invoking keybinding if an ignored window currently has focus.
if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))
return;

_bus.Invoke(new RunWithSubjectContainerCommand(commandStrings));
_bus.Invoke(new RunWithSubjectContainerCommand(commandStrings));
_bus.Invoke(new RedrawContainersCommand());
}
}
catch (Exception e)
{
Original file line number Diff line number Diff line change
@@ -49,7 +49,6 @@ public CommandResponse Handle(ReloadUserConfigCommand command)

// Redraw full container tree.
_containerService.ContainersToRedraw.Add(_containerService.ContainerTree);
_bus.Invoke(new RedrawContainersCommand());

_bus.Emit(new UserConfigReloadedEvent());

2 changes: 0 additions & 2 deletions GlazeWM.Domain/Windows/CommandHandlers/IgnoreWindowHandler.cs
Original file line number Diff line number Diff line change
@@ -28,8 +28,6 @@ public CommandResponse Handle(IgnoreWindowCommand command)
else
_bus.Invoke(new DetachContainerCommand(windowToIgnore));

_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
}
4 changes: 0 additions & 4 deletions GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs
Original file line number Diff line number Diff line change
@@ -44,7 +44,6 @@ public ManageWindowHandler(
public CommandResponse Handle(ManageWindowCommand command)
{
var windowHandle = command.WindowHandle;
var shouldRedraw = command.ShouldRedraw;

// Attach the new window as first child of the target parent (if provided), otherwise, add as
// a sibling of the focused container.
@@ -83,9 +82,6 @@ public CommandResponse Handle(ManageWindowCommand command)
if (window?.IsDetached() != false)
return CommandResponse.Ok;

if (shouldRedraw)
_bus.Invoke(new RedrawContainersCommand());

_logger.LogWindowEvent("New window managed", window);
_bus.Emit(new WindowManagedEvent(window));

10 changes: 0 additions & 10 deletions GlazeWM.Domain/Windows/CommandHandlers/MoveWindowHandler.cs
Original file line number Diff line number Diff line change
@@ -85,7 +85,6 @@ private void SwapSiblingContainers(Window windowToMove, Direction direction)
)
);

_bus.Invoke(new RedrawContainersCommand());
return;
}

@@ -105,8 +104,6 @@ private void SwapSiblingContainers(Window windowToMove, Direction direction)
_bus.Invoke(
new MoveContainerWithinTreeCommand(windowToMove, targetParent, insertionIndex, true)
);

_bus.Invoke(new RedrawContainersCommand());
}

private void MoveToWorkspaceInDirection(Window windowToMove, Direction direction)
@@ -132,8 +129,6 @@ private void MoveToWorkspaceInDirection(Window windowToMove, Direction direction
else
_bus.Invoke(new MoveContainerWithinTreeCommand(windowToMove, workspaceInDirection, 0, true));

_bus.Invoke(new RedrawContainersCommand());

// Refresh state in bar of which workspace has focus.
_bus.Emit(new FocusChangedEvent(windowToMove));
}
@@ -147,8 +142,6 @@ private void ChangeWorkspaceTilingDirection(Window windowToMove, Direction direc
// TODO: Should probably descend into sibling if possible.
if (HasSiblingInDirection(windowToMove, direction))
SwapSiblingContainers(windowToMove, direction);

_bus.Invoke(new RedrawContainersCommand());
}

private void InsertIntoAncestor(
@@ -201,8 +194,6 @@ private void InsertIntoAncestor(
)
);
}

_bus.Invoke(new RedrawContainersCommand());
}

private void MoveTilingWindow(TilingWindow windowToMove, Direction direction)
@@ -307,7 +298,6 @@ private void MoveFloatingWindow(Window windowToMove, Direction direction)
windowToMove.FloatingPlacement = newPlacement;

_containerService.ContainersToRedraw.Add(windowToMove);
_bus.Invoke(new RedrawContainersCommand());
}
}
}
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@ public CommandResponse Handle(ResizeWindowBordersCommand command)
return CommandResponse.Ok;

_containerService.ContainersToRedraw.Add(windowToResize);
_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
Original file line number Diff line number Diff line change
@@ -59,7 +59,6 @@ public CommandResponse Handle(ResizeWindowCommand command)

// TODO: Return early if `clampedResizePercentage` is 0 to avoid unnecessary redraws.
_containerService.ContainersToRedraw.Add(containerToResize.Parent);
_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
@@ -116,7 +115,6 @@ private void ResizeFloatingWindow(Window windowToResize, ResizeDimension dimensi
windowToResize.FloatingPlacement = Rect.FromXYCoordinates(windowToResize.FloatingPlacement.X, windowToResize.FloatingPlacement.Y, width, height);

_containerService.ContainersToRedraw.Add(windowToResize);
_bus.Invoke(new RedrawContainersCommand());

// Check if window now takes up more of another screen after moving
var currentWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(windowToResize);
@@ -135,9 +133,7 @@ private void ResizeFloatingWindow(Window windowToResize, ResizeDimension dimensi
_bus.Invoke(new MoveContainerWithinTreeCommand(windowToResize, targetWorkspace, false));
_bus.Emit(new FocusChangedEvent(windowToResize));

// Redrawing again to fix weird WindowsOS dpi change behaviour
_containerService.ContainersToRedraw.Add(windowToResize);
_bus.Invoke(new RedrawContainersCommand());
windowToResize.HasPendingDpiAdjustment = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -41,7 +41,6 @@ public CommandResponse Handle(SetFloatingCommand command)
};

_bus.Invoke(new ReplaceContainerCommand(floatingWindow, window.Parent, window.Index));
_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
2 changes: 0 additions & 2 deletions GlazeWM.Domain/Windows/CommandHandlers/SetTilingHandler.cs
Original file line number Diff line number Diff line change
@@ -53,8 +53,6 @@ public CommandResponse Handle(SetTilingCommand command)
)
);

_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
}
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@ public CommandResponse Handle(UnmanageWindowCommand command)
// cause focus to briefly flicker to the OS focus target and then to the WM's focus
// target.

_bus.Invoke(new RedrawContainersCommand());
_containerService.PendingFocusContainer = focusTarget;
_bus.InvokeAsync(new SetNativeFocusCommand(focusTarget));

5 changes: 1 addition & 4 deletions GlazeWM.Domain/Windows/Commands/ManageWindowCommand.cs
Original file line number Diff line number Diff line change
@@ -8,16 +8,13 @@ public class ManageWindowCommand : Command
{
public IntPtr WindowHandle { get; }
public SplitContainer TargetParent { get; }
public bool ShouldRedraw { get; }

public ManageWindowCommand(
IntPtr windowHandle,
SplitContainer targetParent = null,
bool shouldRedraw = true)
SplitContainer targetParent = null)
{
WindowHandle = windowHandle;
TargetParent = targetParent;
ShouldRedraw = shouldRedraw;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using GlazeWM.Domain.Common.Utils;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Monitors.Commands;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Infrastructure.Bussing;
@@ -45,6 +46,7 @@ public void Handle(WindowDestroyedEvent @event)

// If window is in tree, detach the removed window from its parent.
_bus.Invoke(new UnmanageWindowCommand(window));
_bus.Invoke(new RedrawContainersCommand());
}
}
}
2 changes: 2 additions & 0 deletions GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using GlazeWM.Domain.Common.Utils;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Monitors.Commands;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Infrastructure.Bussing;
@@ -48,6 +49,7 @@ public void Handle(WindowHiddenEvent @event)

// Detach the hidden window from its parent.
_bus.Invoke(new UnmanageWindowCommand(window));
_bus.Invoke(new RedrawContainersCommand());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Monitors.Commands;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Common.Events;
@@ -23,6 +24,7 @@ public void Handle(WindowLocationChangedEvent @event)
return;

_bus.Invoke(new RefreshMonitorStateCommand());
_bus.Invoke(new RedrawContainersCommand());
}
}
}
Original file line number Diff line number Diff line change
@@ -38,8 +38,7 @@ public WindowMovedOrResizedHandler(

public void Handle(WindowMovedOrResizedEvent @event)
{
var window = _windowService.GetWindows()
.FirstOrDefault(window => window.Handle == @event.WindowHandle);
var window = _windowService.GetWindows().FirstOrDefault(window => window.Handle == @event.WindowHandle);

if (window is null)
return;
@@ -60,12 +59,11 @@ private void UpdateTilingWindow(TilingWindow window)
{
// Snap window to its original position even if it's not being resized.
var hasNoResizableSiblings = window.Parent is Workspace
&& !window.SiblingsOfType<IResizable>().Any();
&& !window.SiblingsOfType<IResizable>().Any();

if (hasNoResizableSiblings)
{
_containerService.ContainersToRedraw.Add(window);
_bus.Invoke(new RedrawContainersCommand());
return;
}

@@ -84,6 +82,7 @@ private void UpdateTilingWindow(TilingWindow window)

_bus.Invoke(new ResizeWindowCommand(window, ResizeDimension.Width, $"{deltaWidth}px"));
_bus.Invoke(new ResizeWindowCommand(window, ResizeDimension.Height, $"{deltaHeight}px"));
_bus.Invoke(new RedrawContainersCommand());
}

private void UpdateFloatingWindow(FloatingWindow window)
2 changes: 2 additions & 0 deletions GlazeWM.Domain/Windows/EventHandlers/WindowShownHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Monitors.Commands;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Infrastructure.Bussing;
@@ -36,6 +37,7 @@ public void Handle(WindowShownEvent @event)
return;

_bus.Invoke(new ManageWindowCommand(@event.WindowHandle));
_bus.Invoke(new RedrawContainersCommand());
}
}
}
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ public CommandResponse Handle(FocusWorkspaceCommand command)

// Get workspace to focus. If it's currently inactive, then activate it.
var workspaceToFocus = _workspaceService.GetActiveWorkspaceByName(workspaceName)
?? ActivateWorkspace(workspaceName);
?? ActivateWorkspace(workspaceName);

var displayedWorkspace = (workspaceToFocus.Parent as Monitor).DisplayedWorkspace;

@@ -70,9 +70,11 @@ public CommandResponse Handle(FocusWorkspaceCommand command)
_bus.Emit(new FocusChangedEvent(containerToFocus));

// Display the workspace to switch focus to.
_containerService.ContainersToRedraw.Add(displayedWorkspace);
_containerService.ContainersToRedraw.Add(workspaceToFocus);
_bus.Invoke(new RedrawContainersCommand());
if (focusedWorkspace.Parent == workspaceToFocus.Parent)
{
_containerService.ContainersToRedraw.Add(displayedWorkspace);
_containerService.ContainersToRedraw.Add(workspaceToFocus);
}

_bus.Invoke(new SetNativeFocusCommand(containerToFocus));

Original file line number Diff line number Diff line change
@@ -68,7 +68,6 @@ public CommandResponse Handle(MoveWindowToWorkspaceCommand command)

_containerService.ContainersToRedraw.Add(currentWorkspace);
_containerService.ContainersToRedraw.Add(windowToMove);
_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}
Original file line number Diff line number Diff line change
@@ -64,8 +64,6 @@ public CommandResponse Handle(MoveWorkspaceInDirectionCommand command)
// TODO: Consider creating separate event `WorkspaceMovedEvent`.
_bus.Emit(new WorkspaceActivatedEvent(focusedWorkspace));

_bus.Invoke(new RedrawContainersCommand());

return CommandResponse.Ok;
}

0 comments on commit 8882e8d

Please sign in to comment.