Skip to content

Commit

Permalink
Introduce a WindowPositioner (#2087)
Browse files Browse the repository at this point in the history
Co-authored-by: Leo <lenemter@gmail.com>
  • Loading branch information
leolost2605 and lenemter authored Nov 2, 2024
1 parent 1e15604 commit d850444
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 74 deletions.
55 changes: 0 additions & 55 deletions src/ShellClients/CenteredWindow.vala

This file was deleted.

37 changes: 19 additions & 18 deletions src/ShellClients/ShellClientsManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class Gala.ShellClientsManager : Object {
private NotificationsClient notifications_client;
private ManagedClient[] protocol_clients = {};

private GLib.HashTable<Meta.Window, PanelWindow> windows = new GLib.HashTable<Meta.Window, PanelWindow> (null, null);
private GLib.HashTable<Meta.Window, CenteredWindow> centered_windows = new GLib.HashTable<Meta.Window, CenteredWindow> (null, null);
private GLib.HashTable<Meta.Window, PanelWindow> panel_windows = new GLib.HashTable<Meta.Window, PanelWindow> (null, null);
private GLib.HashTable<Meta.Window, WindowPositioner> positioned_windows = new GLib.HashTable<Meta.Window, WindowPositioner> (null, null);

private ShellClientsManager (WindowManager wm) {
Object (wm: wm);
Expand Down Expand Up @@ -144,18 +144,18 @@ public class Gala.ShellClientsManager : Object {
}

public void set_anchor (Meta.Window window, Meta.Side side) {
if (window in windows) {
windows[window].update_anchor (side);
if (window in panel_windows) {
panel_windows[window].update_anchor (side);
return;
}

make_dock (window);
// TODO: Return if requested by window that's not a trusted client?

windows[window] = new PanelWindow (wm, window, side);
panel_windows[window] = new PanelWindow (wm, window, side);

// connect_after so we make sure the PanelWindow can destroy its barriers and struts
window.unmanaging.connect_after (() => windows.remove (window));
window.unmanaging.connect_after ((_window) => panel_windows.remove (_window));
}

/**
Expand All @@ -166,37 +166,38 @@ public class Gala.ShellClientsManager : Object {
* TODO: Maybe use for strut only?
*/
public void set_size (Meta.Window window, int width, int height) {
if (!(window in windows)) {
if (!(window in panel_windows)) {
warning ("Set anchor for window before size.");
return;
}

windows[window].set_size (width, height);
panel_windows[window].set_size (width, height);
}

public void set_hide_mode (Meta.Window window, Pantheon.Desktop.HideMode hide_mode) {
if (!(window in windows)) {
if (!(window in panel_windows)) {
warning ("Set anchor for window before hide mode.");
return;
}

windows[window].set_hide_mode (hide_mode);
panel_windows[window].set_hide_mode (hide_mode);
}

public void make_centered (Meta.Window window) {
if (window in centered_windows) {
return;
}
public void make_centered (Meta.Window window) requires (!is_itself_positioned (window)) {
positioned_windows[window] = new WindowPositioner (wm, window, CENTER);

centered_windows[window] = new CenteredWindow (wm, window);
// connect_after so we make sure that any queued move is unqueued
window.unmanaging.connect_after ((_window) => positioned_windows.remove (_window));
}

window.unmanaging.connect_after (() => centered_windows.remove (window));
private bool is_itself_positioned (Meta.Window window) {
return (window in positioned_windows) || (window in panel_windows);
}

public bool is_positioned_window (Meta.Window window) {
bool positioned = (window in centered_windows) || (window in windows);
bool positioned = is_itself_positioned (window);
window.foreach_ancestor ((ancestor) => {
if (ancestor in centered_windows || ancestor in windows) {
if (is_itself_positioned (ancestor)) {
positioned = true;
}

Expand Down
60 changes: 60 additions & 0 deletions src/ShellClients/WindowPositioner.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Authored by: Leonhard Kargl <leo.kargl@proton.me>
*/

public class Gala.WindowPositioner : Object {
public enum Position {
CENTER
}

public Meta.Window window { get; construct; }
public WindowManager wm { get; construct; }
public Position position { get; private set; }
public Variant? position_data { get; private set; }

public WindowPositioner (WindowManager wm, Meta.Window window, Position position, Variant? position_data = null) {
Object (wm: wm, window: window, position: position, position_data: position_data);
}

construct {
window.stick ();

window.size_changed.connect (position_window);
window.position_changed.connect (position_window);
window.shown.connect (position_window);

unowned var monitor_manager = wm.get_display ().get_context ().get_backend ().get_monitor_manager ();
monitor_manager.monitors_changed.connect (position_window);
monitor_manager.monitors_changed_internal.connect (position_window);
}

/**
* This may only be called after the window was shown.
*/
public void update_position (Position new_position, Variant? new_position_data = null) {
position = new_position;
position_data = new_position_data;

position_window ();
}

private void position_window () {
int x = 0, y = 0;

switch (position) {
case CENTER:
unowned var display = wm.get_display ();
var monitor_geom = display.get_monitor_geometry (display.get_primary_monitor ());
var window_rect = window.get_frame_rect ();

x = monitor_geom.x + (monitor_geom.width - window_rect.width) / 2;
y = monitor_geom.y + (monitor_geom.height - window_rect.height) / 2;
break;
}

window.move_frame (false, x, y);
}
}
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ gala_bin_sources = files(
'HotCorners/Barrier.vala',
'HotCorners/HotCorner.vala',
'HotCorners/HotCornerManager.vala',
'ShellClients/CenteredWindow.vala',
'ShellClients/HideTracker.vala',
'ShellClients/ManagedClient.vala',
'ShellClients/NotificationsClient.vala',
'ShellClients/PanelClone.vala',
'ShellClients/PanelWindow.vala',
'ShellClients/ShellClientsManager.vala',
'ShellClients/WindowPositioner.vala',
'Widgets/DwellClickTimer.vala',
'Widgets/IconGroup.vala',
'Widgets/IconGroupContainer.vala',
Expand Down

0 comments on commit d850444

Please sign in to comment.