Skip to content

Commit

Permalink
Add cloned shadow behind the window when swiching workspace on Desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
yilozt committed Oct 16, 2023
1 parent 1ade47f commit c8538c9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
11 changes: 8 additions & 3 deletions @imports/ui/workspaceAnimation.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Window, WindowActor } from 'gi://Meta'
import { Actor, Clone } from 'gi://Clutter'
import * as St from 'gi://St'

export class WorkspaceAnimationController {
_movingWindow: Window;
_switchData: {
monitors: any[];
gestureActivated: boolean;
inProgress: boolean;
monitors: MonitorGroup[];
};
_swipeTracker: any;
_prepareWorkspaceSwitch(workspaceIndices: Array<number>): void;
_finishWorkspaceSwitch(switchData: typeof this._switchData): void;

}

export class WorkspaceGroup extends Actor {
Expand All @@ -22,3 +23,7 @@ export class WorkspaceGroup extends Actor {
_syncStacking(): void;
_shouldShowWindow(win: Window): boolean;
}

export class MonitorGroup extends St.Widget {
_workspaceGroups: WorkspaceGroup []
}
90 changes: 90 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as GObject from 'gi://GObject'
import { WindowPreview } from 'resource:///org/gnome/shell/ui/windowPreview.js'
import { overview, layoutManager } from 'resource:///org/gnome/shell/ui/main.js'
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js'
import { WorkspaceAnimationController } from 'resource:///org/gnome/shell/ui/workspaceAnimation.js'

// local modules
import { constants } from './utils/constants.js'
Expand All @@ -31,6 +32,8 @@ import { global } from '@global'
export default class RoundedWindowCorners extends Extension {
// The methods of gnome-shell to monkey patch
private _orig_add_window!: (_: Meta.Window) => void
private _orig_prep_worksapce_swt!: (workspaceIndices: number[]) => void
private _orig_finish_workspace_swt!: typeof WorkspaceAnimationController.prototype._finishWorkspaceSwitch

private _services: Services | null = null
private _window_actor_tracker: WindowActorTracker | null = null
Expand All @@ -41,6 +44,10 @@ export default class RoundedWindowCorners extends Extension {
// Restore original methods, those methods will be restore when
// extensions is disabled
this._orig_add_window = WindowPreview.prototype._addWindow
this._orig_prep_worksapce_swt =
WorkspaceAnimationController.prototype._prepareWorkspaceSwitch
this._orig_finish_workspace_swt =
WorkspaceAnimationController.prototype._finishWorkspaceSwitch

this._services = new Services ()
this._window_actor_tracker = new WindowActorTracker ()
Expand Down Expand Up @@ -210,6 +217,83 @@ export default class RoundedWindowCorners extends Extension {
})
}

// Just Like the monkey patch when enter overview, need to add cloned shadow
// actor when switching workspaces on Desktop
WorkspaceAnimationController.prototype._prepareWorkspaceSwitch = function (
workspaceIndices
) {
self._orig_prep_worksapce_swt.apply (this, [workspaceIndices])
for (const monitor of this._switchData.monitors) {
for (const workspace of monitor._workspaceGroups) {
// Let shadow actor always behind the window clone actor when we
// switch workspace by Ctrl+Alt+Left/Right
//
// Fix #55
const restacked_id = global.display.connect ('restacked', () => {
workspace._windowRecords.forEach (({ clone }) => {
const shadow = (clone as WsAnimationActor)._shadow_clone
if (shadow) {
workspace.set_child_below_sibling (shadow, clone)
}
})
})
const destroy_id = workspace.connect ('destroy', () => {
global.display.disconnect (restacked_id)
workspace.disconnect (destroy_id)
})

workspace._windowRecords.forEach (({ windowActor: actor, clone }) => {
const win = actor.meta_window
const frame_rect = win.get_frame_rect ()
const shadow = (actor as ExtensionsWindowActor)
.__rwc_rounded_window_info?.shadow
const enabled = UI.get_rounded_corners_effect (actor)?.enabled
if (shadow && enabled) {
// Only create shadow actor when window should have rounded
// corners when switching workspace

// Copy shadow actor to workspace group, so that to see
// shadow when switching workspace
const shadow_clone = new Clutter.Clone ({ source: shadow })
const paddings =
constants.SHADOW_PADDING * UI.WindowScaleFactor (win)

shadow_clone.width = frame_rect.width + paddings * 2
shadow_clone.height = frame_rect.height + paddings * 2
shadow_clone.x = clone.x + frame_rect.x - actor.x - paddings
shadow_clone.y = clone.y + frame_rect.y - actor.y - paddings

// Should works well work Desktop Cube extensions
clone.connect (
'notify::translation-z',
() => (shadow_clone.translation_z = clone.translation_z - 0.05)
)

// Add reference shadow clone for clone actor, so that we
// can restack position of shadow when we need
;(clone as WsAnimationActor)._shadow_clone = shadow_clone
clone.bind_property ('visible', shadow_clone, 'visible', 0)
workspace.insert_child_below (shadow_clone, clone)
}
})
}
}
}

WorkspaceAnimationController.prototype._finishWorkspaceSwitch = function (
switchData
) {
for (const monitor of this._switchData.monitors) {
for (const workspace of monitor._workspaceGroups) {
workspace._windowRecords.forEach (({ clone }) => {
(clone as WsAnimationActor)._shadow_clone?.destroy ()
delete (clone as WsAnimationActor)._shadow_clone
})
}
}
self._orig_finish_workspace_swt.apply (this, [switchData])
}

const c = connections.get ()

// Gnome-shell will not disable extensions when _logout/shutdown/restart
Expand Down Expand Up @@ -243,6 +327,10 @@ export default class RoundedWindowCorners extends Extension {
disable () {
// Restore patched methods
WindowPreview.prototype._addWindow = this._orig_add_window
WorkspaceAnimationController.prototype._prepareWorkspaceSwitch =
this._orig_prep_worksapce_swt
WorkspaceAnimationController.prototype._finishWorkspaceSwitch =
this._orig_finish_workspace_swt

// Remove the item to open preferences page in background menu
UI.RestoreBackgroundMenu ()
Expand Down Expand Up @@ -330,3 +418,5 @@ const OverviewShadowActor = GObject.registerClass (
}
}
)

type WsAnimationActor = Clutter.Actor & { _shadow_clone?: Clutter.Actor }

0 comments on commit c8538c9

Please sign in to comment.