From 150588628e4922bc8de28dd8f6a89394c249f83f Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 8 Apr 2019 14:15:39 +0200 Subject: [PATCH] Best resolution for only secondary screen --- src/lib.rs | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3e320c3..f20c030 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,22 +143,9 @@ impl Screen { /// Finds the best common resolution between two screens. fn best_common_resolution(&self, other: &Screen) -> Option<(u32, u32)> { - let self_res: HashSet<(u32, u32)> = self.resolutions.clone().into_iter().collect(); let other_res: HashSet<(u32, u32)> = other.resolutions.clone().into_iter().collect(); - let intersection = self_res.intersection(&other_res); - - let mut output = None; - - for element in intersection { - match output { - None => output = Some(*element), - Some(res) if &res < element => output = Some(*element), - _ => (), - } - } - - output + self_res.intersection(&other_res).into_iter().max().cloned() } } @@ -311,18 +298,26 @@ impl MultiScreen { /// Returns the arguments for enabling only the second screen. pub fn only_secondary_args(&self) -> Option> { - if let Some((ref main, ref second)) = self.main_screens() { - Some(vec![ - "--output".to_owned(), - main.name.clone(), - "--off".to_owned(), - "--output".to_owned(), - second.name.clone(), - "--auto".to_owned(), - ]) - } else { - None - } + + let (main, second) = match self.main_screens() { + Some(x) => x, + None => return None, + }; + + let (width, height) = match second.resolutions.iter().max() { + Some(x) => x, + None => return None, + }; + + Some(vec![ + "--output".to_owned(), + main.name.clone(), + "--off".to_owned(), + "--output".to_owned(), + second.name.clone(), + "--mode".to_owned(), + format!("{}x{}", width, height), + ]) } /// Calls the xrandr program to disable all screens except the primary.