aboutsummaryrefslogtreecommitdiff
path: root/src/layouts/smart_placement.cc
blob: b23844c5236bb7ea8c8ee6f591f3ac9d9935969c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "smart_placement.h"
#include <algorithm>
#include <cmath>
#include <iostream>

// Constants
constexpr int SmartPlacement::MIN_WINDOW_WIDTH;
constexpr int SmartPlacement::MIN_WINDOW_HEIGHT;
constexpr int SmartPlacement::GRID_MARGIN;
constexpr int SmartPlacement::CASCADE_OFFSET;

SmartPlacement::PlacementResult SmartPlacement::place_window(
    const SRDWindow* window, const Monitor& monitor, 
    const std::vector<SRDWindow*>& existing_windows) {
    
    // Try grid placement first (SRDWindows 11 style)
    auto grid_result = place_in_grid(window, monitor, existing_windows);
    if (grid_result.success) {
        return grid_result;
    }
    
    // Fall back to cascade placement
    return cascade_place(window, monitor, existing_windows);
}

SmartPlacement::PlacementResult SmartPlacement::place_in_grid(
    const SRDWindow* window, const Monitor& monitor,
    const std::vector<SRDWindow*>& existing_windows) {
    
    PlacementResult result = {0, 0, 0, 0, false, "Grid placement failed"};
    
    // Calculate optimal grid size based on monitor and window count
    int window_count = existing_windows.size() + 1;
    int grid_size = calculate_optimal_grid_size(monitor, window_count);
    
    if (grid_size <= 0) {
        result.reason = "Invalid grid size";
        return result;
    }
    
    // Calculate grid position for this window
    auto [grid_x, grid_y] = calculate_grid_position(window, monitor);
    
    // Calculate cell dimensions
    int cell_width = (monitor.width - (grid_size + 1) * GRID_MARGIN) / grid_size;
    int cell_height = (monitor.height - (grid_size + 1) * GRID_MARGIN) / grid_size;
    
    // Ensure minimum cell size
    cell_width = std::max(cell_width, MIN_WINDOW_WIDTH);
    cell_height = std::max(cell_height, MIN_WINDOW_HEIGHT);
    
    // Calculate window position
    int x = monitor.x + GRID_MARGIN + grid_x * (cell_width + GRID_MARGIN);
    int y = monitor.y + GRID_MARGIN + grid_y * (cell_height + GRID_MARGIN);
    
    // Check if position is valid
    if (is_position_valid(x, y, cell_width, cell_height, monitor)) {
        result.x = x;
        result.y = y;
        result.width = cell_width;
        result.height = cell_height;
        result.success = true;
        result.reason = "Grid placement successful";
    }
    
    return result;
}

SmartPlacement::PlacementResult SmartPlacement::snap_to_edge(
    const SRDWindow* window, const Monitor& monitor,
    const std::vector<SRDWindow*>& existing_windows) {
    
    PlacementResult result = {0, 0, 0, 0, false, "Snap placement failed"};
    
    // For now, implement simple edge snapping
    // In a full implementation, this would detect when windows are dragged near edges
    
    int x = monitor.x + monitor.width / 4;
    int y = monitor.y + monitor.height / 4;
    int width = monitor.width / 2;
    int height = monitor.height / 2;
    
    if (is_position_valid(x, y, width, height, monitor)) {
        result.x = x;
        result.y = y;
        result.width = width;
        result.height = height;
        result.success = true;
        result.reason = "Snap placement successful";
    }
    
    return result;
}

SmartPlacement::PlacementResult SmartPlacement::cascade_place(
    const SRDWindow* window, const Monitor& monitor,
    const std::vector<SRDWindow*>& existing_windows) {
    
    PlacementResult result = {0, 0, 0, 0, false, "Cascade placement failed"};
    
    // Find a free space for cascading
    auto free_spaces = find_free_spaces(monitor, existing_windows);
    
    if (free_spaces.empty()) {
        // No free spaces, use default position
        int x = monitor.x + CASCADE_OFFSET;
        int y = monitor.y + CASCADE_OFFSET;
        int width = std::min(800, monitor.width - 2 * CASCADE_OFFSET);
        int height = std::min(600, monitor.height - 2 * CASCADE_OFFSET);
        
        if (is_position_valid(x, y, width, height, monitor)) {
            result.x = x;
            result.y = y;
            result.width = width;
            result.height = height;
            result.success = true;
            result.reason = "Default cascade placement";
        }
    } else {
        // Use the first free space
        auto [x, y] = free_spaces[0];
        int width = std::min(800, monitor.width - x - CASCADE_OFFSET);
        int height = std::min(600, monitor.height - y - CASCADE_OFFSET);
        
        if (is_position_valid(x, y, width, height, monitor)) {
            result.x = x;
            result.y = y;
            result.width = width;
            result.height = height;
            result.success = true;
            result.reason = "Cascade placement in free space";
        }
    }
    
    return result;
}

SmartPlacement::PlacementResult SmartPlacement::smart_tile(
    const SRDWindow* window, const Monitor& monitor,
    const std::vector<SRDWindow*>& existing_windows) {
    
    PlacementResult result = {0, 0, 0, 0, false, "Smart tile placement failed"};
    
    // Calculate overlap score to find best position
    int best_score = -1;
    int best_x = monitor.x;
    int best_y = monitor.y;
    int best_width = monitor.width / 2;
    int best_height = monitor.height / 2;
    
    // Try different positions and find the one with least overlap
    for (int x = monitor.x; x < monitor.x + monitor.width - MIN_WINDOW_WIDTH; x += 50) {
        for (int y = monitor.y; y < monitor.y + monitor.height - MIN_WINDOW_HEIGHT; y += 50) {
            int width = std::min(800, monitor.width - x);
            int height = std::min(600, monitor.height - y);
            
            if (is_position_valid(x, y, width, height, monitor)) {
                int score = calculate_overlap_score(window, monitor, existing_windows);
                if (score > best_score) {
                    best_score = score;
                    best_x = x;
                    best_y = y;
                    best_width = width;
                    best_height = height;
                }
            }
        }
    }
    
    if (best_score >= 0) {
        result.x = best_x;
        result.y = best_y;
        result.width = best_width;
        result.height = best_height;
        result.success = true;
        result.reason = "Smart tile placement successful";
    }
    
    return result;
}

bool SmartPlacement::windows_overlap(const SRDWindow* w1, const SRDWindow* w2) {
    // Simple AABB overlap detection
    int x1 = w1->getX();
    int y1 = w1->getY();
    int w1_width = w1->getWidth();
    int w1_height = w1->getHeight();
    
    int x2 = w2->getX();
    int y2 = w2->getY();
    int w2_width = w2->getWidth();
    int w2_height = w2->getHeight();
    
    return !(x1 + w1_width <= x2 || x2 + w2_width <= x1 ||
             y1 + w1_height <= y2 || y2 + w2_height <= y1);
}

int SmartPlacement::calculate_overlap_score(const SRDWindow* window, const Monitor& monitor,
                                           const std::vector<SRDWindow*>& existing_windows) {
    int score = 0;
    
    // Calculate how much this position overlaps with existing windows
    for (const auto* existing : existing_windows) {
        if (windows_overlap(window, existing)) {
            score -= 10; // Penalty for overlap
        } else {
            score += 1;  // Bonus for no overlap
        }
    }
    
    return score;
}

std::vector<std::pair<int, int>> SmartPlacement::find_free_spaces(
    const Monitor& monitor, const std::vector<SRDWindow*>& existing_windows) {
    
    std::vector<std::pair<int, int>> free_spaces;
    
    // Simple algorithm: try positions in a grid pattern
    for (int x = monitor.x; x < monitor.x + monitor.width - MIN_WINDOW_WIDTH; x += 100) {
        for (int y = monitor.y; y < monitor.y + monitor.height - MIN_WINDOW_HEIGHT; y += 100) {
            bool is_free = true;
            
            // Check if this position overlaps with any existing window
            for (const auto* existing : existing_windows) {
                int ex = existing->getX();
                int ey = existing->getY();
                int ew = existing->getWidth();
                int eh = existing->getHeight();
                
                if (x < ex + ew && x + MIN_WINDOW_WIDTH > ex &&
                    y < ey + eh && y + MIN_WINDOW_HEIGHT > ey) {
                    is_free = false;
                    break;
                }
            }
            
            if (is_free) {
                free_spaces.emplace_back(x, y);
            }
        }
    }
    
    return free_spaces;
}

bool SmartPlacement::is_position_valid(int x, int y, int width, int height, const Monitor& monitor) {
    return x >= monitor.x && y >= monitor.y &&
           x + width <= monitor.x + monitor.width &&
           y + height <= monitor.y + monitor.height &&
           width >= MIN_WINDOW_WIDTH && height >= MIN_WINDOW_HEIGHT;
}

std::pair<int, int> SmartPlacement::calculate_grid_position(const SRDWindow* window, const Monitor& monitor) {
    // Simple grid position calculation
    // In a real implementation, this might consider window properties or user preferences
    
    // For now, use a simple pattern: first window top-left, second top-right, etc.
    static int window_counter = 0;
    int grid_x = window_counter % 2;
    int grid_y = window_counter / 2;
    window_counter++;
    
    return {grid_x, grid_y};
}

int SmartPlacement::calculate_optimal_grid_size(const Monitor& monitor, int window_count) {
    // Calculate optimal grid size based on monitor dimensions and window count
    if (window_count <= 0) return 1;
    
    // Simple heuristic: try to create a roughly square grid
    int grid_size = static_cast<int>(std::ceil(std::sqrt(window_count)));
    
    // Ensure grid size is reasonable
    grid_size = std::max(1, std::min(grid_size, 4));
    
    return grid_size;
}