diff --git a/hyprland.go b/hyprland.go index 9014bb5..eb91fa4 100644 --- a/hyprland.go +++ b/hyprland.go @@ -39,6 +39,15 @@ type hyprMonitor struct { AvailableModes []string `json:"availableModes"` } +type hyprWorkspace struct { + ID int `json:"id"` + Name string `json:"name"` + Monitor string `json:"monitor"` + MonitorID int `json:"monitorID"` + Windows int `json:"windows"` + Persistent bool `json:"ispersistent"` +} + func readMonitors() ([]Monitor, error) { cmd := exec.Command("hyprctl", "monitors", "all", "-j") output, err := cmd.Output() @@ -241,3 +250,79 @@ func rollback() error { } return applyMonitors(previousMonitors) } + +func readWorkspaces() ([]hyprWorkspace, error) { + cmd := exec.Command("hyprctl", "workspaces", "-j") + output, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("failed to execute hyprctl workspaces: %w", err) + } + + var workspaces []hyprWorkspace + if err := json.Unmarshal(output, &workspaces); err != nil { + return nil, fmt.Errorf("failed to parse workspaces JSON: %w", err) + } + + return workspaces, nil +} + +func getCurrentMonitorNames() ([]string, error) { + cmd := exec.Command("hyprctl", "monitors", "-j") + output, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("failed to execute hyprctl monitors: %w", err) + } + + var hyprMonitors []hyprMonitor + if err := json.Unmarshal(output, &hyprMonitors); err != nil { + return nil, fmt.Errorf("failed to parse monitors JSON: %w", err) + } + + var names []string + for _, m := range hyprMonitors { + if !m.Disabled { + names = append(names, m.Name) + } + } + return names, nil +} + +func migrateOrphanedWorkspaces(previousMonitorNames, currentMonitorNames []string) error { + removedMonitors := findRemovedMonitors(previousMonitorNames, currentMonitorNames) + if len(removedMonitors) == 0 { + return nil + } + + workspaces, err := readWorkspaces() + if err != nil { + return fmt.Errorf("failed to read workspaces: %w", err) + } + + for _, workspace := range workspaces { + for _, removedMonitor := range removedMonitors { + if workspace.Monitor == removedMonitor { + cmd := fmt.Sprintf("hyprctl dispatch moveworkspacetomonitor %d current", workspace.ID) + if err := exec.Command("sh", "-c", cmd).Run(); err != nil { + return fmt.Errorf("failed to migrate workspace %d: %w", workspace.ID, err) + } + } + } + } + + return nil +} + +func findRemovedMonitors(previous, current []string) []string { + currentSet := make(map[string]bool) + for _, name := range current { + currentSet[name] = true + } + + var removed []string + for _, name := range previous { + if !currentSet[name] { + removed = append(removed, name) + } + } + return removed +} diff --git a/models.go b/models.go index 2d7215e..c7e965d 100644 --- a/models.go +++ b/models.go @@ -74,6 +74,9 @@ type model struct { ShowHelp bool HelpScrollOffset int // Scroll position for help screen OpenProfiles bool // Flag to open profiles page + + // Monitor tracking for workspace migration + PreviousMonitorNames []string } type initMsg struct { diff --git a/update.go b/update.go index 23a7001..8c3f54b 100644 --- a/update.go +++ b/update.go @@ -161,6 +161,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.Status = fmt.Sprintf("Loaded %d monitors", len(m.Monitors)) m.updateWorld() + + // Store current monitor names for tracking + var names []string + for _, mon := range m.Monitors { + if mon.Active { + names = append(names, mon.Name) + } + } + m.PreviousMonitorNames = names } // Force a window size refresh after initial load return m, tea.WindowSize() @@ -450,8 +459,25 @@ func reloadMonitorsCmd() tea.Cmd { func applyCmd(monitors []Monitor) tea.Cmd { return func() tea.Msg { + // Get current monitor names before applying changes + previousNames, _ := getCurrentMonitorNames() + + // Apply the monitor configuration err := applyMonitors(monitors) - return applyMsg{success: err == nil, err: err} + if err != nil { + return applyMsg{success: false, err: err} + } + + // Get monitor names after applying changes + currentNames, _ := getCurrentMonitorNames() + + // Migrate orphaned workspaces if monitors were removed + if err := migrateOrphanedWorkspaces(previousNames, currentNames); err != nil { + // Log the error but don't fail the apply operation + fmt.Printf("Warning: Failed to migrate workspaces: %v\n", err) + } + + return applyMsg{success: true, err: nil} } }