Skip to content

Commit 12b1260

Browse files
committed
Don't allow mutable access to displays outside of Compositor
1 parent 02f3c64 commit 12b1260

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

‎src/compositor.rs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ struct CursorCommand {
2323
}
2424

2525
pub struct Compositor {
26-
// FIXME make these private once possible
27-
pub displays: Vec<Display>,
26+
displays: Vec<Display>,
2827

2928
redraws: Vec<Rect>,
3029

@@ -77,11 +76,29 @@ impl Compositor {
7776
}
7877
}
7978

79+
pub fn displays(&self) -> &[Display] {
80+
&self.displays
81+
}
82+
8083
/// Return the screen rectangle
8184
pub fn screen_rect(&self) -> Rect {
8285
self.displays[0].screen_rect()
8386
}
8487

88+
/// Find the display that a window (`rect`) most overlaps and return it's screen_rect
89+
pub fn get_screen_rect_for_window(&self, rect: &Rect) -> Rect {
90+
let mut screen_rect = self.displays[0].screen_rect();
91+
let mut max_intersection_area = 0;
92+
for display in &self.displays {
93+
let intersect = display.screen_rect().intersection(rect);
94+
if intersect.area() > max_intersection_area {
95+
screen_rect = display.screen_rect();
96+
max_intersection_area = intersect.area();
97+
}
98+
}
99+
screen_rect
100+
}
101+
85102
/// Resize the inner image buffer.
86103
pub fn resize(&mut self, width: i32, height: i32) {
87104
//TODO: should other screens be moved after a resize?

‎src/scheme.rs‎

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -910,20 +910,18 @@ impl OrbitalScheme {
910910
fn tile_window(&mut self, window_id: Option<&usize>, position: TilePosition) {
911911
if let Some(id) = window_id.or(self.order.front()) {
912912
if let Some(window) = self.windows.get_mut(id) {
913-
let display_index =
914-
Self::get_display_index(&self.compositor.displays, &window.rect());
915913
Self::update_window(&mut self.compositor, window, |compositor, window| {
916914
let (x, y, width, height) = match window.restore.take() {
917915
None => {
918916
// we are about to maximize window, so store current size for restore later
919917
window.restore = Some(window.rect());
920918

921-
let top =
922-
compositor.displays[display_index].y + window.title_rect().height();
923-
let left = compositor.displays[display_index].x;
924-
let max_height = compositor.displays[display_index].image.height()
925-
- window.title_rect().height();
926-
let max_width = compositor.displays[display_index].image.width();
919+
let screen_rect =
920+
compositor.get_screen_rect_for_window(&window.rect());
921+
let top = screen_rect.top() + window.title_rect().height();
922+
let left = screen_rect.left();
923+
let max_height = screen_rect.height() - window.title_rect().height();
924+
let max_width = screen_rect.width();
927925
let half_width = (max_width / 2) as u32;
928926
let half_height = (max_height / 2) as u32;
929927

@@ -1274,15 +1272,15 @@ impl OrbitalScheme {
12741272
// This logic assumes horizontal and touching, but not overlapping, screens
12751273
let mut max_x = 0;
12761274
let mut max_y = 0;
1277-
for display in self.compositor.displays.iter() {
1275+
for display in self.compositor.displays() {
12781276
let rect = display.screen_rect();
12791277
max_x = cmp::max(max_x, rect.right() - 1);
12801278
max_y = cmp::max(max_y, rect.bottom() - 1);
12811279
}
12821280

12831281
let x = cmp::max(0, cmp::min(max_x, self.cursor_x + event.dx));
12841282
let mut y = cmp::max(0, cmp::min(max_y, self.cursor_y + event.dy));
1285-
for display in self.compositor.displays.iter() {
1283+
for display in self.compositor.displays() {
12861284
let rect = display.screen_rect();
12871285
if x >= rect.left() && x < rect.right() {
12881286
y = cmp::max(rect.top(), cmp::min(rect.bottom() - 1, y));
@@ -1292,23 +1290,6 @@ impl OrbitalScheme {
12921290
self.mouse_event(MouseEvent { x, y });
12931291
}
12941292

1295-
// Find the display that a window (`rect`) most overlaps and return the index of it
1296-
fn get_display_index(displays: &[Display], rect: &Rect) -> usize {
1297-
// Find the index of the Display this window has the most overlap with
1298-
let mut display_index = 0;
1299-
let mut max_intersection_area = 0;
1300-
for (display_i, display) in displays.iter().enumerate() {
1301-
let intersect = display.screen_rect().intersection(rect);
1302-
let area = intersect.area();
1303-
if area > max_intersection_area {
1304-
display_index = display_i;
1305-
max_intersection_area = area;
1306-
}
1307-
}
1308-
1309-
display_index
1310-
}
1311-
13121293
fn button_event(&mut self, event: ButtonEvent) {
13131294
// Check for focus switch, dragging, and forward mouse events to applications
13141295
match self.dragging {

0 commit comments

Comments
 (0)