forked from akanyan/STARTLINER
feat: display witchcraft
This commit is contained in:
93
rust/src/display.rs
Normal file
93
rust/src/display.rs
Normal file
@ -0,0 +1,93 @@
|
||||
use crate::profile::Profile;
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub struct DisplayInfo {
|
||||
primary: String,
|
||||
target: String,
|
||||
target_rotation: displayz::Orientation
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
pub async fn prepare_display(p: &Profile) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub async fn prepare_display(p: &Profile) -> Result<Option<DisplayInfo>> {
|
||||
use displayz::{query_displays, Orientation};
|
||||
|
||||
let display_name = p.get_str("display", "default");
|
||||
let rotation = p.get_int("display-rotation", 0);
|
||||
if display_name == "default" {
|
||||
log::debug!("prepare display: skip");
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let display_set = query_displays()?;
|
||||
|
||||
let primary = display_set
|
||||
.displays()
|
||||
.find(|display| display.is_primary())
|
||||
.ok_or_else(|| anyhow!("Primary display not found"))?;
|
||||
|
||||
let target = display_set
|
||||
.displays()
|
||||
.find(|display| display.name() == display_name)
|
||||
.ok_or_else(|| anyhow!("Display {} not found", display_name))?;
|
||||
|
||||
target.set_primary()?;
|
||||
let settings = target.settings()
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("Unable to query display settings"))?;
|
||||
|
||||
let res = DisplayInfo {
|
||||
primary: primary.name().to_owned(),
|
||||
target: target.name().to_owned(),
|
||||
target_rotation: settings.borrow().orientation
|
||||
};
|
||||
|
||||
match rotation {
|
||||
90 => settings.borrow_mut().orientation = Orientation::Portrait,
|
||||
270 => settings.borrow_mut().orientation = Orientation::PortraitFlipped,
|
||||
_ => ()
|
||||
};
|
||||
|
||||
display_set.apply()?;
|
||||
displayz::refresh()?;
|
||||
|
||||
log::debug!("prepare display: done");
|
||||
|
||||
Ok(Some(res))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub async fn undo_display(info: DisplayInfo) -> Result<()> {
|
||||
use displayz::query_displays;
|
||||
|
||||
let display_set = query_displays()?;
|
||||
|
||||
let primary = display_set
|
||||
.displays()
|
||||
.find(|display| display.name() == info.primary)
|
||||
.ok_or_else(|| anyhow!("Display {} not found", info.primary))?;
|
||||
|
||||
let target = display_set
|
||||
.displays()
|
||||
.find(|display| display.name() == info.target)
|
||||
.ok_or_else(|| anyhow!("Display {} not found", info.target))?;
|
||||
|
||||
primary.set_primary()?;
|
||||
|
||||
let settings = target.settings()
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("Unable to query display settings"))?;
|
||||
settings.borrow_mut().orientation = info.target_rotation;
|
||||
|
||||
display_set.apply()?;
|
||||
displayz::refresh()?;
|
||||
|
||||
log::debug!("undo display: done");
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user