Make missing scale = automatic selection

That was the intention, but I missed it before.
This commit is contained in:
Ivan Molodetskikh
2024-05-05 12:39:20 +04:00
parent df0ee996ee
commit 2e4a2e13b1
3 changed files with 36 additions and 9 deletions
+4 -4
View File
@@ -243,8 +243,8 @@ pub struct Output {
pub off: bool,
#[knuffel(argument)]
pub name: String,
#[knuffel(child, unwrap(argument), default = 1.)]
pub scale: f64,
#[knuffel(child, unwrap(argument))]
pub scale: Option<f64>,
#[knuffel(child, unwrap(argument, str), default = Transform::Normal)]
pub transform: Transform,
#[knuffel(child)]
@@ -260,7 +260,7 @@ impl Default for Output {
Self {
off: false,
name: String::new(),
scale: 1.,
scale: None,
transform: Transform::Normal,
position: None,
mode: None,
@@ -2291,7 +2291,7 @@ mod tests {
outputs: vec![Output {
off: false,
name: "eDP-1".to_owned(),
scale: 2.,
scale: Some(2.),
transform: Transform::Flipped90,
position: Some(Position { x: 10, y: 20 }),
mode: Some(ConfiguredMode {
+24 -2
View File
@@ -278,9 +278,9 @@ pub enum OutputAction {
},
/// Set the output scale.
Scale {
/// Scale factor to set.
/// Scale factor to set, or "auto" for automatic selection.
#[cfg_attr(feature = "clap", arg())]
scale: f64,
scale: ScaleToSet,
},
/// Set the output transform.
Transform {
@@ -329,6 +329,15 @@ pub struct ConfiguredMode {
pub refresh: Option<f64>,
}
/// Output scale to set.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum ScaleToSet {
/// Niri will pick the scale automatically.
Automatic,
/// Specific scale.
Specific(f64),
}
/// Output position to set.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "clap", derive(clap::Subcommand))]
@@ -560,3 +569,16 @@ impl FromStr for ConfiguredMode {
})
}
}
impl FromStr for ScaleToSet {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("auto") {
return Ok(Self::Automatic);
}
let scale = s.parse().map_err(|_| "error parsing scale")?;
Ok(Self::Specific(scale))
}
}
+8 -3
View File
@@ -1014,7 +1014,7 @@ impl State {
let config = self.niri.config.borrow_mut();
let config = config.outputs.iter().find(|o| o.name == name);
let scale = config.map(|c| c.scale).unwrap_or_else(|| {
let scale = config.and_then(|c| c.scale).unwrap_or_else(|| {
let size_mm = output.physical_properties().size;
let resolution = output.current_mode().unwrap().size;
guess_monitor_scale(size_mm, resolution)
@@ -1077,7 +1077,12 @@ impl State {
niri_ipc::ModeToSet::Specific(mode) => Some(mode),
}
}
niri_ipc::OutputAction::Scale { scale } => config.scale = scale,
niri_ipc::OutputAction::Scale { scale } => {
config.scale = match scale {
niri_ipc::ScaleToSet::Automatic => None,
niri_ipc::ScaleToSet::Specific(scale) => Some(scale),
}
}
niri_ipc::OutputAction::Transform { transform } => config.transform = transform,
niri_ipc::OutputAction::Position { position } => {
config.position = match position {
@@ -1666,7 +1671,7 @@ impl Niri {
let config = self.config.borrow();
let c = config.outputs.iter().find(|o| o.name == name);
let scale = c.map(|c| c.scale).unwrap_or_else(|| {
let scale = c.and_then(|c| c.scale).unwrap_or_else(|| {
let size_mm = output.physical_properties().size;
let resolution = output.current_mode().unwrap().size;
guess_monitor_scale(size_mm, resolution)