From 182ee8f23327ad0e2517b8b06386d571e9233cdc Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 14 Nov 2023 22:04:17 +0800 Subject: [PATCH] fix, order displays from dxgi Signed-off-by: fufesou --- libs/scrap/src/common/dxgi.rs | 47 +++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/libs/scrap/src/common/dxgi.rs b/libs/scrap/src/common/dxgi.rs index 3bf1783a8..403a7837a 100644 --- a/libs/scrap/src/common/dxgi.rs +++ b/libs/scrap/src/common/dxgi.rs @@ -108,15 +108,48 @@ impl Display { } pub fn all() -> io::Result> { - let tmp = Self::all_().unwrap_or(Default::default()); - if tmp.is_empty() { + let displays_gdi = dxgi::Displays::get_from_gdi() + .drain(..) + .map(Display) + .collect::>(); + + let displays_dxgi = Self::all_().unwrap_or(Default::default()); + + // Return gdi displays if dxgi is not supported + if displays_dxgi.is_empty() { println!("Display got from gdi"); - return Ok(dxgi::Displays::get_from_gdi() - .drain(..) - .map(Display) - .collect::>()); + return Ok(displays_gdi); } - Ok(tmp) + + // Return dxgi displays if length is not equal + if displays_dxgi.len() != displays_gdi.len() { + return Ok(displays_dxgi); + } + + // Check if names are equal + let names_gdi = displays_gdi.iter().map(|d| d.name()).collect::>(); + let names_dxgi = displays_dxgi.iter().map(|d| d.name()).collect::>(); + for name in names_gdi.iter() { + if !names_dxgi.contains(name) { + return Ok(displays_dxgi); + } + } + + // Reorder displays from dxgi + let mut displays_dxgi = displays_dxgi; + let mut displays_dxgi_ordered = Vec::new(); + for name in names_gdi.iter() { + let pos = match displays_dxgi.iter().position(|d| d.name() == *name) { + Some(pos) => pos, + None => { + // unreachable! + 0 + } + }; + displays_dxgi_ordered.push(displays_dxgi.remove(pos)); + } + + Ok(displays_dxgi_ordered) } fn all_() -> io::Result> {