Skip to content

Commit 9a3ffe1

Browse files
Add resizing functionality (#10)
* Add functionality to get the current state of windows * Add screenshot to readme * Add resizing width and height * Add window size query script * Add example with 25-50-25 split
1 parent 767cf71 commit 9a3ffe1

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Sure, I could just use `tmux`, but that came with some issues:
4949
* allow different IDEs to be defined through a readable file (like TOML)
5050
* run arbitrary commands (not just shells and TUIs!)
5151

52+
[screenshot](screenshots/early_magic_tile.png)
5253

5354
## Alternatives
5455
* [tmux](https://github.com/tmux/tmux)

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ omit = [
3939
"src/magic_tiler/subprocess_runner.py",
4040
"src/magic_tiler/sway.py",
4141
"src/magic_tiler/main.py",
42+
"src/magic_tiler/get_window_sizes.py",
4243
]
4344

4445
[tool.coverage.report]
@@ -50,3 +51,4 @@ profile = "google"
5051

5152
[tool.poetry.scripts]
5253
magic-tiler = "magic_tiler.main:main"
54+
get-window-sizes = "magic_tiler.get_window_sizes:main"

screenshots/early_magic_tile.png

112 KB
Loading

src/magic_tiler/get_window_sizes.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pprint
2+
3+
import click
4+
5+
from magic_tiler import subprocess_runner
6+
from magic_tiler import sway
7+
8+
9+
@click.command()
10+
def main() -> None:
11+
runner = subprocess_runner.SubprocessRunner()
12+
window_manager = sway.Sway(runner)
13+
pprint.pprint(window_manager.get_window_sizes())

src/magic_tiler/main.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import pprint
23

34
import click
45

@@ -21,13 +22,12 @@ def main() -> None:
2122
if swaywm.num_workspace_windows > 1:
2223
raise RuntimeError("There are multiple windows open in the current workspace.")
2324
swaywm.make_window('alacritty --title "first window"')
24-
swaywm.make_horizontal_sibling(
25-
"^first window$", 'alacritty --title "second window"'
26-
)
27-
swaywm.make_vertical_sibling("^second window$", 'alacritty --title "small window"')
28-
swaywm.make_vertical_sibling(
29-
"^first window$", 'alacritty --title "another small window"'
30-
)
25+
swaywm.make_horizontal_sibling("^first window$", 'alacritty --title "big boy"')
26+
swaywm.resize_width("first window", 25)
27+
swaywm.make_horizontal_sibling("^big boy$", 'alacritty --title "side window"')
28+
swaywm.resize_width("side window", 33)
29+
window_sizes = swaywm.get_window_sizes()
30+
logging.info(pprint.pformat(window_sizes))
3131

3232

3333
def woops() -> None:

src/magic_tiler/sway.py

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import time
3+
from typing import Dict, List
34

45
import i3ipc
56

@@ -35,10 +36,23 @@ def make_window(self, command: str) -> None:
3536
time.sleep(0.25)
3637

3738
def resize_width(self, window_title_regex: str, container_percentage: int) -> None:
38-
pass
39+
window = self._get_window(window_title_regex)
40+
window.command("focus")
41+
window.command(f"resize set width {container_percentage} ppt")
3942

4043
def resize_height(self, window_title_regex: str, container_percentage: int) -> None:
41-
pass
44+
window = self._get_window(window_title_regex)
45+
window.command("focus")
46+
window.command(f"resize set height {container_percentage} ppt")
47+
48+
def get_window_sizes(self) -> Dict[str, Dict[str, float]]:
49+
return {
50+
window.name: {
51+
"width": window.window_rect.width,
52+
"height": window.window_rect.height,
53+
}
54+
for window in self._get_windows_in_current_workspace()
55+
}
4256

4357
def _get_window(self, window_title_regex: str) -> i3ipc.Con:
4458
tree = self._sway.get_tree()
@@ -54,13 +68,7 @@ def _get_window(self, window_title_regex: str) -> i3ipc.Con:
5468
)
5569
return windows[0]
5670

57-
@property
58-
def num_workspace_windows(self) -> int:
59-
"""Get the number of windows open on the current workspace
60-
61-
The current workspace must not be a "named workspace"
62-
https://i3ipc-python.readthedocs.io/en/latest/replies.html#i3ipc.WorkspaceReply
63-
"""
71+
def _get_windows_in_current_workspace(self) -> List[i3ipc.Con]:
6472
workspaces = self._sway.get_workspaces()
6573
for workspace in workspaces:
6674
if workspace.focused:
@@ -70,11 +78,20 @@ def num_workspace_windows(self) -> int:
7078
break
7179
else:
7280
raise RuntimeError("There is no current workspace")
73-
num_windows = 0
81+
windows_in_current_workspace = []
7482
for container in self._sway.get_tree().leaves():
7583
logging.debug(
7684
f'"{container.name}" is in workspace {container.workspace().num}'
7785
)
7886
if container.workspace().num == current_workspace_num:
79-
num_windows += 1
80-
return num_windows
87+
windows_in_current_workspace.append(container)
88+
return windows_in_current_workspace
89+
90+
@property
91+
def num_workspace_windows(self) -> int:
92+
"""Get the number of windows open on the current workspace
93+
94+
The current workspace must not be a "named workspace"
95+
https://i3ipc-python.readthedocs.io/en/latest/replies.html#i3ipc.WorkspaceReply
96+
"""
97+
return len(self._get_windows_in_current_workspace())

0 commit comments

Comments
 (0)