probonopd / Filer

A clean rewrite of the Filer file manager for helloSystem, inspired by John Siracusa's descriptions of "Spatial Orientation"
BSD 2-Clause "Simplified" License
12 stars 1 forks source link

Test persisting window position under Wayland #21

Open probonopd opened 2 months ago

probonopd commented 2 months ago

One key concept of a spatial file manger like Filer is that every window has its own size and position that persists when the window is closed and opened again, also across reboots of the machine. This helps spatial orientation. (If you are not aware with the concept, some details are below.)

Should we ever consider to make Filer work under Wayland, we'd need to find a way for Filer to set the pixel-precise window size and position.

As per Wayland philosophy, applications (such as Filer) have no say about pixel-precise window position. Compositors might (or might not) persist window position, who knows? According to some sources (to be verified), some Wayland compositors will ignore the position set by the application and place the window according to their own policies. For example, GNOME's Mutter and KDE's KWin are said to manage window positioning themselves, effectively making it impossible to write a spatial file manager.

The script below has been tested and is working on Windows 11 with no issues. Does it work under Wayland?

#!/usr/bin/env python3

import sys
import json
import os
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtCore import QSize, QPoint

# File to save window states
STATE_FILE = "window_states.json"

class WindowManager:
    def __init__(self):
        self.states = self.load_states()

    def load_states(self):
        if os.path.exists(STATE_FILE):
            with open(STATE_FILE, 'r') as file:
                return json.load(file)
        return {}

    def save_states(self):
        with open(STATE_FILE, 'w') as file:
            json.dump(self.states, file)

    def save_window_state(self, window_id, size, position):
        self.states[window_id] = {
            'size': {'width': size.width(), 'height': size.height()},
            'position': {'x': position.x(), 'y': position.y()}
        }
        self.save_states()

    def get_window_state(self, window_id):
        return self.states.get(window_id, None)

class MainWindow(QMainWindow):
    def __init__(self, window_id, manager):
        super().__init__()
        self.window_id = window_id
        self.manager = manager

        # Restore window state
        state = self.manager.get_window_state(self.window_id)
        if state:
            self.resize(QSize(state['size']['width'], state['size']['height']))
            self.move(QPoint(state['position']['x'], state['position']['y']))
        else:
            self.resize(800, 600)

    def closeEvent(self, event):
        size = self.size()
        position = self.pos()
        self.manager.save_window_state(self.window_id, size, position)
        event.accept()

def main():
    app = QApplication(sys.argv)
    manager = WindowManager()

    main_window = MainWindow("main_window", manager)
    main_window.show()

    sys.exit(app.exec())

if __name__ == "__main__":
    main()

Spatial File Manager

A spatial file manager is a type of file manager that emphasizes the spatial representation of files and directories. The key characteristics include:

John Siracusa: Technology writer and critic, John Siracusa, has extensively discussed the concept of spatial file managers, especially in the context of Apple's Finder in Mac OS X. In his Mac OS X reviews, Siracusa highlighted how the spatial model supports a more intuitive and user-friendly interface, contrasting it with the navigational file managers that became more common later (Windows, KDE, today's Gnome).