Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ hyprmon profiles
| `Enter` or `Space` | Toggle monitor active/inactive |
| `C` or `D` | Open advanced display settings dialog |
| `M` | Open monitor mirroring configuration |
| `V` | Toggle layout between side-by-side and stacked |
| `X` | Swap monitor order (left ↔ right, or top ↔ bottom) |
| `A` | Apply changes live to Hyprland |
| `S` | Save changes to configuration file |
| `P` | Save current layout as named profile |
Expand Down
110 changes: 110 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"slices"
"sort"

tea "github.com/charmbracelet/bubbletea"
)

Expand Down Expand Up @@ -386,3 +389,110 @@ func (m *model) canDisableMonitor(index int) bool {

return m.countActiveMonitors() > 1
}

// toggleOrientation switches the active monitors between a side-by-side
// (horizontal) row and a stacked (vertical) column. Monitor order is kept
// (left→right becomes top→bottom and vice versa), each monitor is centered on
// the cross axis, and the arrangement stays anchored at the original top-left
// corner. Inactive and mirrored monitors are left untouched. It returns the
// new orientation ("vertical" or "horizontal"), or "" if fewer than two
// monitors take part.
func (m *model) toggleOrientation() string {
return m.arrangeMonitors(true, false)
}

// swapMonitorOrder reverses the order of the active monitors along the current
// layout axis (left↔right in a row, top↔bottom in a column), keeping the
// orientation. It returns the orientation, or "" if fewer than two monitors
// take part.
func (m *model) swapMonitorOrder() string {
return m.arrangeMonitors(false, true)
}

// arrangeMonitors lays the active, non-mirrored monitors out in a single row or
// column. The current orientation is detected from the spread of monitor
// centers; flip switches it, reverse reverses the monitor order.
func (m *model) arrangeMonitors(flip, reverse bool) string {
var idx []int
for i, mon := range m.Monitors {
if mon.Active && !mon.IsMirrored {
idx = append(idx, i)
}
}
if len(idx) < 2 {
return ""
}

// Measure the spread of monitor centers on each axis to decide which way
// they are laid out now.
var minCX, maxCX, minCY, maxCY, minX, minY int32
for n, i := range idx {
mon := m.Monitors[i]
w, h := m.getEffectiveDimensions(mon)
cx, cy := mon.X+w/2, mon.Y+h/2
if n == 0 {
minCX, maxCX, minCY, maxCY = cx, cx, cy, cy
minX, minY = mon.X, mon.Y
continue
}
minCX, maxCX = min32(minCX, cx), max(maxCX, cx)
minCY, maxCY = min32(minCY, cy), max(maxCY, cy)
minX, minY = min32(minX, mon.X), min32(minY, mon.Y)
}
wasHorizontal := maxCX-minCX >= maxCY-minCY
toVertical := wasHorizontal == flip

// Keep (or reverse) the existing order along the current main axis.
sort.SliceStable(idx, func(a, b int) bool {
ma, mb := m.Monitors[idx[a]], m.Monitors[idx[b]]
if wasHorizontal {
return ma.X < mb.X
}
return ma.Y < mb.Y
})
if reverse {
slices.Reverse(idx)
}

// The widest (for a column) or tallest (for a row) monitor sets the
// cross-axis span that the others are centered within.
var span int32
for _, i := range idx {
w, h := m.getEffectiveDimensions(m.Monitors[i])
if toVertical {
span = max(span, w)
} else {
span = max(span, h)
}
}

var offset int32
for _, i := range idx {
mon := &m.Monitors[i]
w, h := m.getEffectiveDimensions(*mon)
if toVertical {
mon.X = minX + (span-w)/2
mon.Y = minY + offset
offset += h
} else {
mon.X = minX + offset
mon.Y = minY + (span-h)/2
offset += w
}
}

m.Guides = nil
m.updateWorld()
if toVertical {
return "vertical"
}
return "horizontal"
}

// min32 exists because view.go declares an int-only min that shadows the builtin.
func min32(a, b int32) int32 {
if a < b {
return a
}
return b
}
133 changes: 133 additions & 0 deletions orientation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"strings"
"testing"

tea "github.com/charmbracelet/bubbletea"
)

func TestToggleOrientationRoundTrip(t *testing.T) {
m := model{
Monitors: []Monitor{
// Listed out of order on purpose: the laptop is on the right.
{Name: "eDP-1", X: 2560, Y: 360, PxW: 2880, PxH: 1800, Scale: 2, Active: true},
{Name: "DP-1", X: 0, Y: 0, PxW: 2560, PxH: 1440, Scale: 1, Active: true},
{Name: "HDMI-1", X: 9000, Y: 9000, PxW: 1920, PxH: 1080, Scale: 1, Active: false},
},
}

if got := m.toggleOrientation(); got != "vertical" {
t.Fatalf("first toggle = %q, want vertical", got)
}
dp, edp := m.Monitors[1], m.Monitors[0]
// DP-1 was on the left, so it should now be on top, centered over the laptop.
if dp.X != 0 || dp.Y != 0 {
t.Fatalf("DP-1 at %d,%d, want 0,0", dp.X, dp.Y)
}
if edp.X != (2560-1440)/2 || edp.Y != 1440 {
t.Fatalf("eDP-1 at %d,%d, want %d,1440", edp.X, edp.Y, (2560-1440)/2)
}
if m.Monitors[2].X != 9000 || m.Monitors[2].Y != 9000 {
t.Fatalf("inactive monitor moved")
}

if got := m.toggleOrientation(); got != "horizontal" {
t.Fatalf("second toggle = %q, want horizontal", got)
}
dp, edp = m.Monitors[1], m.Monitors[0]
if dp.X != 0 || dp.Y != 0 {
t.Fatalf("DP-1 at %d,%d, want 0,0", dp.X, dp.Y)
}
if edp.X != 2560 || edp.Y != (1440-900)/2 {
t.Fatalf("eDP-1 at %d,%d, want 2560,%d", edp.X, edp.Y, (1440-900)/2)
}
}

func TestToggleOrientationRespectsRotation(t *testing.T) {
m := model{
Monitors: []Monitor{
{Name: "A", X: 0, Y: 0, PxW: 1920, PxH: 1080, Scale: 1, Active: true},
{Name: "B", X: 1920, Y: 0, PxW: 1920, PxH: 1080, Scale: 1, Active: true, Transform: 1},
},
}
m.toggleOrientation()
// B is rotated (1080 wide, 1920 tall) and sits below A, centered.
if b := m.Monitors[1]; b.X != (1920-1080)/2 || b.Y != 1080 {
t.Fatalf("B at %d,%d, want %d,1080", b.X, b.Y, (1920-1080)/2)
}
}

func TestToggleOrientationNeedsTwoMonitors(t *testing.T) {
m := model{Monitors: []Monitor{{Name: "A", PxW: 1920, PxH: 1080, Scale: 1, Active: true}}}
if got := m.toggleOrientation(); got != "" {
t.Fatalf("got %q, want empty", got)
}
}

func TestSwapMonitorOrderVertical(t *testing.T) {
m := model{
Monitors: []Monitor{
// B on top, A (laptop) below it.
{Name: "A", X: 560, Y: 1440, PxW: 2880, PxH: 1800, Scale: 2, Active: true},
{Name: "B", X: 0, Y: 0, PxW: 2560, PxH: 1440, Scale: 1, Active: true},
},
}
if got := m.swapMonitorOrder(); got != "vertical" {
t.Fatalf("swap = %q, want vertical", got)
}
a, b := m.Monitors[0], m.Monitors[1]
// A should now be on top, centered over the wider B.
if a.X != (2560-1440)/2 || a.Y != 0 {
t.Fatalf("A at %d,%d, want %d,0", a.X, a.Y, (2560-1440)/2)
}
if b.X != 0 || b.Y != 900 {
t.Fatalf("B at %d,%d, want 0,900", b.X, b.Y)
}

// Swapping twice restores the original order.
m.swapMonitorOrder()
if m.Monitors[1].Y != 0 || m.Monitors[0].Y != 1440 {
t.Fatalf("double swap: A.Y=%d B.Y=%d, want 1440 and 0", m.Monitors[0].Y, m.Monitors[1].Y)
}
}

func TestSwapMonitorOrderHorizontal(t *testing.T) {
m := model{
Monitors: []Monitor{
{Name: "A", X: 0, Y: 0, PxW: 1920, PxH: 1080, Scale: 1, Active: true},
{Name: "B", X: 1920, Y: 0, PxW: 1920, PxH: 1080, Scale: 1, Active: true},
},
}
if got := m.swapMonitorOrder(); got != "horizontal" {
t.Fatalf("swap = %q, want horizontal", got)
}
if m.Monitors[1].X != 0 || m.Monitors[0].X != 1920 {
t.Fatalf("A.X=%d B.X=%d, want 1920 and 0", m.Monitors[0].X, m.Monitors[1].X)
}
}

func TestSwapMonitorOrderNeedsTwoMonitors(t *testing.T) {
m := model{Monitors: []Monitor{{Name: "A", PxW: 1920, PxH: 1080, Scale: 1, Active: true}}}
if got := m.swapMonitorOrder(); got != "" {
t.Fatalf("swap = %q, want empty", got)
}
}

func TestOrientationKeysDoNotApply(t *testing.T) {
for _, key := range []string{"v", "x"} {
m := model{
Monitors: []Monitor{
{Name: "A", X: 0, Y: 0, PxW: 1920, PxH: 1080, Scale: 1, Active: true},
{Name: "B", X: 1920, Y: 0, PxW: 1920, PxH: 1080, Scale: 1, Active: true},
},
}
next, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key)})
if st := next.(model).Status; !strings.Contains(st, "A to apply") {
t.Fatalf("%q status = %q, key handler not reached", key, st)
}
if cmd != nil {
t.Fatalf("%q returned a command; layout changes should wait for A", key)
}
}
}
20 changes: 20 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,26 @@ func (m model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.ShowModePicker = true
}

case "v", "V":
// Toggle between side-by-side and stacked layouts (applied with A)
orientation := m.toggleOrientation()
if orientation == "" {
m.Status = "Need at least two active monitors to change orientation"
return m, nil
}
m.Status = fmt.Sprintf("Layout: %s (A to apply, S to save)", orientation)
return m, nil

case "x", "X":
// Swap monitor order (top↔bottom or left↔right) (applied with A)
orientation := m.swapMonitorOrder()
if orientation == "" {
m.Status = "Need at least two active monitors to swap"
return m, nil
}
m.Status = fmt.Sprintf("Swapped monitors, %s layout (A to apply, S to save)", orientation)
return m, nil

case "m", "M":
// Open mirror picker for selected monitor
if m.Selected >= 0 && m.Selected < len(m.Monitors) {
Expand Down
4 changes: 4 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (m model) renderHelp() string {
{"R", "Open scale adjustment dialog"},
{"F", "Open mode selection dialog"},
{"M", "Open mirror configuration dialog"},
{"V", "Toggle layout horizontal ↔ vertical"},
{"X", "Swap monitor order (left ↔ right / top ↔ bottom)"},
{"C/D", "Open advanced display settings"},
{"A", "Apply the changes right now (doesn't persist)"},
{"S", "Save current configuration to Hyprland. Will persist restarts"},
Expand Down Expand Up @@ -630,6 +632,8 @@ func (m model) renderFooter() string {
{"R scale", "R scale", "R", 1},
{"F mode", "F mode", "F", 2},
{"M mirror", "M mirror", "M", 2},
{"V orientation", "V orient", "V", 2},
{"X swap", "X swap", "X", 2},
{"C advanced", "C adv", "C", 1},
{"A apply", "A apply", "A", 2},
{"S save", "S save", "S", 2},
Expand Down