niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use knuffel::errors::DecodeError; |
| 2 | use niri_ipc::{ColumnDisplay, SizeChange}; |
| 3 | |
| 4 | use crate::appearance::{ |
| 5 | Border, FocusRing, InsertHint, Shadow, TabIndicator, DEFAULT_BACKGROUND_COLOR, |
| 6 | }; |
| 7 | use crate::utils::{expect_only_children, Flag, MergeWith}; |
| 8 | use crate::{BorderRule, Color, FloatOrInt, InsertHintPart, ShadowRule, TabIndicatorPart}; |
| 9 | |
| 10 | #[derive(Debug, Clone, PartialEq)] |
| 11 | pub struct Layout { |
| 12 | pub focus_ring: FocusRing, |
| 13 | pub border: Border, |
| 14 | pub shadow: Shadow, |
| 15 | pub tab_indicator: TabIndicator, |
| 16 | pub insert_hint: InsertHint, |
| 17 | pub preset_column_widths: Vec<PresetSize>, |
| 18 | pub default_column_width: Option<PresetSize>, |
| 19 | pub preset_window_heights: Vec<PresetSize>, |
| 20 | pub center_focused_column: CenterFocusedColumn, |
| 21 | pub always_center_single_column: bool, |
| 22 | pub empty_workspace_above_first: bool, |
| 23 | pub default_column_display: ColumnDisplay, |
| 24 | pub gaps: f64, |
| 25 | pub struts: Struts, |
| 26 | pub background_color: Color, |
| 27 | pub fullscreen_backdrop_color: Color, |
| 28 | } |
| 29 | |
| 30 | impl Default for Layout { |
| 31 | fn default() -> Self { |
| 32 | Self { |
| 33 | focus_ring: FocusRing::default(), |
| 34 | border: Border::default(), |
| 35 | shadow: Shadow::default(), |
| 36 | tab_indicator: TabIndicator::default(), |
| 37 | insert_hint: InsertHint::default(), |
| 38 | preset_column_widths: vec![ |
| 39 | PresetSize::Proportion(1. / 3.), |
| 40 | PresetSize::Proportion(0.5), |
| 41 | PresetSize::Proportion(2. / 3.), |
| 42 | ], |
| 43 | default_column_width: Some(PresetSize::Proportion(0.5)), |
| 44 | center_focused_column: CenterFocusedColumn::Never, |
| 45 | always_center_single_column: false, |
| 46 | empty_workspace_above_first: false, |
| 47 | default_column_display: ColumnDisplay::Normal, |
| 48 | gaps: 16., |
| 49 | struts: Struts::default(), |
| 50 | preset_window_heights: vec![ |
| 51 | PresetSize::Proportion(1. / 3.), |
| 52 | PresetSize::Proportion(0.5), |
| 53 | PresetSize::Proportion(2. / 3.), |
| 54 | ], |
| 55 | background_color: DEFAULT_BACKGROUND_COLOR, |
| 56 | fullscreen_backdrop_color: Color::from_array_unpremul([0., 0., 0., 1.]), |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl MergeWith<LayoutPart> for Layout { |
| 62 | fn merge_with(&mut self, part: &LayoutPart) { |
| 63 | merge!( |
| 64 | (self, part), |
| 65 | focus_ring, |
| 66 | border, |
| 67 | shadow, |
| 68 | tab_indicator, |
| 69 | insert_hint, |
| 70 | always_center_single_column, |
| 71 | empty_workspace_above_first, |
| 72 | gaps, |
| 73 | ); |
| 74 | |
| 75 | merge_clone!( |
| 76 | (self, part), |
| 77 | preset_column_widths, |
| 78 | preset_window_heights, |
| 79 | center_focused_column, |
| 80 | default_column_display, |
| 81 | struts, |
| 82 | background_color, |
| 83 | fullscreen_backdrop_color, |
| 84 | ); |
| 85 | |
| 86 | if let Some(x) = part.default_column_width { |
| 87 | self.default_column_width = x.0; |
| 88 | } |
| 89 | |
| 90 | if self.preset_column_widths.is_empty() { |
| 91 | self.preset_column_widths = Layout::default().preset_column_widths; |
| 92 | } |
| 93 | |
| 94 | if self.preset_window_heights.is_empty() { |
| 95 | self.preset_window_heights = Layout::default().preset_window_heights; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| 101 | pub struct LayoutPart { |
| 102 | #[knuffel(child)] |
| 103 | pub focus_ring: Option<BorderRule>, |
| 104 | #[knuffel(child)] |
| 105 | pub border: Option<BorderRule>, |
| 106 | #[knuffel(child)] |
| 107 | pub shadow: Option<ShadowRule>, |
| 108 | #[knuffel(child)] |
| 109 | pub tab_indicator: Option<TabIndicatorPart>, |
| 110 | #[knuffel(child)] |
| 111 | pub insert_hint: Option<InsertHintPart>, |
| 112 | #[knuffel(child, unwrap(children))] |
| 113 | pub preset_column_widths: Option<Vec<PresetSize>>, |
| 114 | #[knuffel(child)] |
| 115 | pub default_column_width: Option<DefaultPresetSize>, |
| 116 | #[knuffel(child, unwrap(children))] |
| 117 | pub preset_window_heights: Option<Vec<PresetSize>>, |
| 118 | #[knuffel(child, unwrap(argument))] |
| 119 | pub center_focused_column: Option<CenterFocusedColumn>, |
| 120 | #[knuffel(child)] |
| 121 | pub always_center_single_column: Option<Flag>, |
| 122 | #[knuffel(child)] |
| 123 | pub empty_workspace_above_first: Option<Flag>, |
| 124 | #[knuffel(child, unwrap(argument, str))] |
| 125 | pub default_column_display: Option<ColumnDisplay>, |
| 126 | #[knuffel(child, unwrap(argument))] |
| 127 | pub gaps: Option<FloatOrInt<0, 65535>>, |
| 128 | #[knuffel(child)] |
| 129 | pub struts: Option<Struts>, |
| 130 | #[knuffel(child)] |
| 131 | pub background_color: Option<Color>, |
| 132 | #[knuffel(child)] |
| 133 | pub fullscreen_backdrop_color: Option<Color>, |
| 134 | } |
| 135 | |
| 136 | #[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)] |
| 137 | pub enum PresetSize { |
| 138 | Proportion(#[knuffel(argument)] f64), |
| 139 | Fixed(#[knuffel(argument)] i32), |
| 140 | } |
| 141 | |
| 142 | impl From<PresetSize> for SizeChange { |
| 143 | fn from(value: PresetSize) -> Self { |
| 144 | match value { |
| 145 | PresetSize::Proportion(prop) => SizeChange::SetProportion(prop * 100.), |
| 146 | PresetSize::Fixed(fixed) => SizeChange::SetFixed(fixed), |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 152 | pub struct DefaultPresetSize(pub Option<PresetSize>); |
| 153 | |
| 154 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] |
| 155 | pub struct Struts { |
| 156 | #[knuffel(child, unwrap(argument), default)] |
| 157 | pub left: FloatOrInt<-65535, 65535>, |
| 158 | #[knuffel(child, unwrap(argument), default)] |
| 159 | pub right: FloatOrInt<-65535, 65535>, |
| 160 | #[knuffel(child, unwrap(argument), default)] |
| 161 | pub top: FloatOrInt<-65535, 65535>, |
| 162 | #[knuffel(child, unwrap(argument), default)] |
| 163 | pub bottom: FloatOrInt<-65535, 65535>, |
| 164 | } |
| 165 | |
| 166 | #[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq, Clone, Copy)] |
| 167 | pub enum CenterFocusedColumn { |
| 168 | /// Focusing a column will not center the column. |
| 169 | #[default] |
| 170 | Never, |
| 171 | /// The focused column will always be centered. |
| 172 | Always, |
| 173 | /// Focusing a column will center it if it doesn't fit on the screen together with the |
| 174 | /// previously focused column. |
| 175 | OnOverflow, |
| 176 | } |
| 177 | |
| 178 | impl<S> knuffel::Decode<S> for DefaultPresetSize |
| 179 | where |
| 180 | S: knuffel::traits::ErrorSpan, |
| 181 | { |
| 182 | fn decode_node( |
| 183 | node: &knuffel::ast::SpannedNode<S>, |
| 184 | ctx: &mut knuffel::decode::Context<S>, |
| 185 | ) -> Result<Self, DecodeError<S>> { |
| 186 | expect_only_children(node, ctx); |
| 187 | |
| 188 | let mut children = node.children(); |
| 189 | |
| 190 | if let Some(child) = children.next() { |
| 191 | if let Some(unwanted_child) = children.next() { |
| 192 | ctx.emit_error(DecodeError::unexpected( |
| 193 | unwanted_child, |
| 194 | "node", |
| 195 | "expected no more than one child", |
| 196 | )); |
| 197 | } |
| 198 | PresetSize::decode_node(child, ctx).map(Some).map(Self) |
| 199 | } else { |
| 200 | Ok(Self(None)) |
| 201 | } |
| 202 | } |
| 203 | } |