Make missing scale = automatic selection
Browse files at this commit2e4a2e13b175fcbb0fef58214f7de5b60775214aParent: df0ee993 changed files +36 -9
- niri-config/src/lib.rs +4 -4
- niri-ipc/src/lib.rs +24 -2
- src/niri.rs +8 -3
niri-config/src/lib.rsmodified+4 -4
| @@ -243,8 +243,8 @@ pub struct Output { | |||
| 243 | pub off: bool, | 243 | pub off: bool, |
| 244 | #[knuffel(argument)] | 244 | #[knuffel(argument)] |
| 245 | pub name: String, | 245 | pub name: String, |
| 246 | - #[knuffel(child, unwrap(argument), default = 1.)] | 246 | + #[knuffel(child, unwrap(argument))] |
| 247 | - pub scale: f64, | 247 | + pub scale: Option<f64>, |
| 248 | #[knuffel(child, unwrap(argument, str), default = Transform::Normal)] | 248 | #[knuffel(child, unwrap(argument, str), default = Transform::Normal)] |
| 249 | pub transform: Transform, | 249 | pub transform: Transform, |
| 250 | #[knuffel(child)] | 250 | #[knuffel(child)] |
| @@ -260,7 +260,7 @@ impl Default for Output { | |||
| 260 | Self { | 260 | Self { |
| 261 | off: false, | 261 | off: false, |
| 262 | name: String::new(), | 262 | name: String::new(), |
| 263 | - scale: 1., | 263 | + scale: None, |
| 264 | transform: Transform::Normal, | 264 | transform: Transform::Normal, |
| 265 | position: None, | 265 | position: None, |
| 266 | mode: None, | 266 | mode: None, |
| @@ -2291,7 +2291,7 @@ mod tests { | |||
| 2291 | outputs: vec![Output { | 2291 | outputs: vec![Output { |
| 2292 | off: false, | 2292 | off: false, |
| 2293 | name: "eDP-1".to_owned(), | 2293 | name: "eDP-1".to_owned(), |
| 2294 | - scale: 2., | 2294 | + scale: Some(2.), |
| 2295 | transform: Transform::Flipped90, | 2295 | transform: Transform::Flipped90, |
| 2296 | position: Some(Position { x: 10, y: 20 }), | 2296 | position: Some(Position { x: 10, y: 20 }), |
| 2297 | mode: Some(ConfiguredMode { | 2297 | mode: Some(ConfiguredMode { |
niri-ipc/src/lib.rsmodified+24 -2
| @@ -278,9 +278,9 @@ pub enum OutputAction { | |||
| 278 | }, | 278 | }, |
| 279 | /// Set the output scale. | 279 | /// Set the output scale. |
| 280 | Scale { | 280 | Scale { |
| 281 | - /// Scale factor to set. | 281 | + /// Scale factor to set, or "auto" for automatic selection. |
| 282 | #[cfg_attr(feature = "clap", arg())] | 282 | #[cfg_attr(feature = "clap", arg())] |
| 283 | - scale: f64, | 283 | + scale: ScaleToSet, |
| 284 | }, | 284 | }, |
| 285 | /// Set the output transform. | 285 | /// Set the output transform. |
| 286 | Transform { | 286 | Transform { |
| @@ -329,6 +329,15 @@ pub struct ConfiguredMode { | |||
| 329 | pub refresh: Option<f64>, | 329 | pub refresh: Option<f64>, |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | +/// Output scale to set. | ||
| 333 | +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)] | ||
| 334 | +pub enum ScaleToSet { | ||
| 335 | + /// Niri will pick the scale automatically. | ||
| 336 | + Automatic, | ||
| 337 | + /// Specific scale. | ||
| 338 | + Specific(f64), | ||
| 339 | +} | ||
| 340 | + | ||
| 332 | /// Output position to set. | 341 | /// Output position to set. |
| 333 | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)] | 342 | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)] |
| 334 | #[cfg_attr(feature = "clap", derive(clap::Subcommand))] | 343 | #[cfg_attr(feature = "clap", derive(clap::Subcommand))] |
| @@ -560,3 +569,16 @@ impl FromStr for ConfiguredMode { | |||
| 560 | }) | 569 | }) |
| 561 | } | 570 | } |
| 562 | } | 571 | } |
| 572 | + | ||
| 573 | +impl FromStr for ScaleToSet { | ||
| 574 | + type Err = &'static str; | ||
| 575 | + | ||
| 576 | + fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 577 | + if s.eq_ignore_ascii_case("auto") { | ||
| 578 | + return Ok(Self::Automatic); | ||
| 579 | + } | ||
| 580 | + | ||
| 581 | + let scale = s.parse().map_err(|_| "error parsing scale")?; | ||
| 582 | + Ok(Self::Specific(scale)) | ||
| 583 | + } | ||
| 584 | +} | ||
src/niri.rsmodified+8 -3
| @@ -1014,7 +1014,7 @@ impl State { | |||
| 1014 | let config = self.niri.config.borrow_mut(); | 1014 | let config = self.niri.config.borrow_mut(); |
| 1015 | let config = config.outputs.iter().find(|o| o.name == name); | 1015 | let config = config.outputs.iter().find(|o| o.name == name); |
| 1016 | 1016 | ||
| 1017 | - let scale = config.map(|c| c.scale).unwrap_or_else(|| { | 1017 | + let scale = config.and_then(|c| c.scale).unwrap_or_else(|| { |
| 1018 | let size_mm = output.physical_properties().size; | 1018 | let size_mm = output.physical_properties().size; |
| 1019 | let resolution = output.current_mode().unwrap().size; | 1019 | let resolution = output.current_mode().unwrap().size; |
| 1020 | guess_monitor_scale(size_mm, resolution) | 1020 | guess_monitor_scale(size_mm, resolution) |
| @@ -1077,7 +1077,12 @@ impl State { | |||
| 1077 | niri_ipc::ModeToSet::Specific(mode) => Some(mode), | 1077 | niri_ipc::ModeToSet::Specific(mode) => Some(mode), |
| 1078 | } | 1078 | } |
| 1079 | } | 1079 | } |
| 1080 | - niri_ipc::OutputAction::Scale { scale } => config.scale = scale, | 1080 | + niri_ipc::OutputAction::Scale { scale } => { |
| 1081 | + config.scale = match scale { | ||
| 1082 | + niri_ipc::ScaleToSet::Automatic => None, | ||
| 1083 | + niri_ipc::ScaleToSet::Specific(scale) => Some(scale), | ||
| 1084 | + } | ||
| 1085 | + } | ||
| 1081 | niri_ipc::OutputAction::Transform { transform } => config.transform = transform, | 1086 | niri_ipc::OutputAction::Transform { transform } => config.transform = transform, |
| 1082 | niri_ipc::OutputAction::Position { position } => { | 1087 | niri_ipc::OutputAction::Position { position } => { |
| 1083 | config.position = match position { | 1088 | config.position = match position { |
| @@ -1666,7 +1671,7 @@ impl Niri { | |||
| 1666 | 1671 | ||
| 1667 | let config = self.config.borrow(); | 1672 | let config = self.config.borrow(); |
| 1668 | let c = config.outputs.iter().find(|o| o.name == name); | 1673 | let c = config.outputs.iter().find(|o| o.name == name); |
| 1669 | - let scale = c.map(|c| c.scale).unwrap_or_else(|| { | 1674 | + let scale = c.and_then(|c| c.scale).unwrap_or_else(|| { |
| 1670 | let size_mm = output.physical_properties().size; | 1675 | let size_mm = output.physical_properties().size; |
| 1671 | let resolution = output.current_mode().unwrap().size; | 1676 | let resolution = output.current_mode().unwrap().size; |
| 1672 | guess_monitor_scale(size_mm, resolution) | 1677 | guess_monitor_scale(size_mm, resolution) |