foreign_toplevel: Update the focused window last
Browse files at this commitfefb1cccd6c3c7b92f8d4021fe5e38609760d1e4Parent: deef5251 changed file +128 -97
- src/protocols/foreign_toplevel.rs +128 -97
src/protocols/foreign_toplevel.rsmodified+128 -97
| @@ -12,7 +12,9 @@ use smithay::reexports::wayland_server::{ | |||
| 12 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, | 12 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 13 | }; | 13 | }; |
| 14 | use smithay::wayland::compositor::with_states; | 14 | use smithay::wayland::compositor::with_states; |
| 15 | -use smithay::wayland::shell::xdg::{ToplevelStateSet, XdgToplevelSurfaceData}; | 15 | +use smithay::wayland::shell::xdg::{ |
| 16 | + ToplevelStateSet, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, | ||
| 17 | +}; | ||
| 16 | use wayland_protocols_wlr::foreign_toplevel::v1::server::{ | 18 | use wayland_protocols_wlr::foreign_toplevel::v1::server::{ |
| 17 | zwlr_foreign_toplevel_handle_v1, zwlr_foreign_toplevel_manager_v1, | 19 | zwlr_foreign_toplevel_handle_v1, zwlr_foreign_toplevel_manager_v1, |
| 18 | }; | 20 | }; |
| @@ -89,6 +91,10 @@ pub fn refresh(state: &mut State) { | |||
| 89 | }); | 91 | }); |
| 90 | 92 | ||
| 91 | // Handle new and existing windows. | 93 | // Handle new and existing windows. |
| 94 | + // | ||
| 95 | + // Save the focused window for last, this way when the focus changes, we will first deactivate | ||
| 96 | + // the previous window and only then activate the newly focused window. | ||
| 97 | + let mut focused = None; | ||
| 92 | state.niri.layout.with_windows(|window, output| { | 98 | state.niri.layout.with_windows(|window, output| { |
| 93 | let wl_surface = window.toplevel().wl_surface(); | 99 | let wl_surface = window.toplevel().wl_surface(); |
| 94 | 100 | ||
| @@ -100,125 +106,150 @@ pub fn refresh(state: &mut State) { | |||
| 100 | .lock() | 106 | .lock() |
| 101 | .unwrap(); | 107 | .unwrap(); |
| 102 | 108 | ||
| 103 | - let has_focus = state.niri.keyboard_focus.as_ref() == Some(wl_surface); | 109 | + if state.niri.keyboard_focus.as_ref() == Some(wl_surface) { |
| 104 | - let states = to_state_vec(&role.current.states, has_focus); | 110 | + focused = Some((window.clone(), output.cloned())); |
| 105 | - | 111 | + } else { |
| 106 | - match protocol_state.toplevels.entry(wl_surface.clone()) { | 112 | + refresh_toplevel(protocol_state, wl_surface, &role, output, false); |
| 107 | - Entry::Occupied(entry) => { | 113 | + } |
| 108 | - // Existing window, check if anything changed. | 114 | + }); |
| 109 | - let data = entry.into_mut(); | 115 | + }); |
| 110 | - | ||
| 111 | - let mut new_title = None; | ||
| 112 | - if data.title != role.title { | ||
| 113 | - data.title = role.title.clone(); | ||
| 114 | - new_title = role.title.as_deref(); | ||
| 115 | 116 | ||
| 116 | - if new_title.is_none() { | 117 | + // Finally, refresh the focused window. |
| 117 | - error!("toplevel title changed to None"); | 118 | + if let Some((window, output)) = focused { |
| 118 | - } | 119 | + let wl_surface = window.toplevel().wl_surface(); |
| 119 | - } | ||
| 120 | 120 | ||
| 121 | - let mut new_app_id = None; | 121 | + with_states(wl_surface, |states| { |
| 122 | - if data.app_id != role.app_id { | 122 | + let role = states |
| 123 | - data.app_id = role.app_id.clone(); | 123 | + .data_map |
| 124 | - new_app_id = role.app_id.as_deref(); | 124 | + .get::<XdgToplevelSurfaceData>() |
| 125 | + .unwrap() | ||
| 126 | + .lock() | ||
| 127 | + .unwrap(); | ||
| 125 | 128 | ||
| 126 | - if new_app_id.is_none() { | 129 | + refresh_toplevel(protocol_state, wl_surface, &role, output.as_ref(), true); |
| 127 | - error!("toplevel app_id changed to None"); | 130 | + }); |
| 128 | - } | 131 | + } |
| 129 | - } | 132 | +} |
| 130 | 133 | ||
| 131 | - let mut states_changed = false; | 134 | +fn refresh_toplevel( |
| 132 | - if data.states != states { | 135 | + protocol_state: &mut ForeignToplevelManagerState, |
| 133 | - data.states = states; | 136 | + wl_surface: &WlSurface, |
| 134 | - states_changed = true; | 137 | + role: &XdgToplevelSurfaceRoleAttributes, |
| 135 | - } | 138 | + output: Option<&Output>, |
| 139 | + has_focus: bool, | ||
| 140 | +) { | ||
| 141 | + let states = to_state_vec(&role.current.states, has_focus); | ||
| 142 | + | ||
| 143 | + match protocol_state.toplevels.entry(wl_surface.clone()) { | ||
| 144 | + Entry::Occupied(entry) => { | ||
| 145 | + // Existing window, check if anything changed. | ||
| 146 | + let data = entry.into_mut(); | ||
| 147 | + | ||
| 148 | + let mut new_title = None; | ||
| 149 | + if data.title != role.title { | ||
| 150 | + data.title = role.title.clone(); | ||
| 151 | + new_title = role.title.as_deref(); | ||
| 152 | + | ||
| 153 | + if new_title.is_none() { | ||
| 154 | + error!("toplevel title changed to None"); | ||
| 155 | + } | ||
| 156 | + } | ||
| 136 | 157 | ||
| 137 | - let mut output_changed = false; | 158 | + let mut new_app_id = None; |
| 138 | - if data.output.as_ref() != output { | 159 | + if data.app_id != role.app_id { |
| 139 | - data.output = output.cloned(); | 160 | + data.app_id = role.app_id.clone(); |
| 140 | - output_changed = true; | 161 | + new_app_id = role.app_id.as_deref(); |
| 141 | - } | ||
| 142 | 162 | ||
| 143 | - let something_changed = new_title.is_some() | 163 | + if new_app_id.is_none() { |
| 144 | - || new_app_id.is_some() | 164 | + error!("toplevel app_id changed to None"); |
| 145 | - || states_changed | 165 | + } |
| 146 | - || output_changed; | 166 | + } |
| 147 | 167 | ||
| 148 | - if something_changed { | 168 | + let mut states_changed = false; |
| 149 | - for (instance, outputs) in &mut data.instances { | 169 | + if data.states != states { |
| 150 | - if let Some(new_title) = new_title { | 170 | + data.states = states; |
| 151 | - instance.title(new_title.to_owned()); | 171 | + states_changed = true; |
| 152 | - } | 172 | + } |
| 153 | - if let Some(new_app_id) = new_app_id { | ||
| 154 | - instance.app_id(new_app_id.to_owned()); | ||
| 155 | - } | ||
| 156 | - if states_changed { | ||
| 157 | - instance.state( | ||
| 158 | - data.states.iter().flat_map(|x| x.to_ne_bytes()).collect(), | ||
| 159 | - ); | ||
| 160 | - } | ||
| 161 | - if output_changed { | ||
| 162 | - for wl_output in outputs.drain(..) { | ||
| 163 | - instance.output_leave(&wl_output); | ||
| 164 | - } | ||
| 165 | - if let Some(output) = &data.output { | ||
| 166 | - if let Some(client) = instance.client() { | ||
| 167 | - for wl_output in output.client_outputs(&client) { | ||
| 168 | - instance.output_enter(&wl_output); | ||
| 169 | - outputs.push(wl_output); | ||
| 170 | - } | ||
| 171 | - } | ||
| 172 | - } | ||
| 173 | - } | ||
| 174 | - } | ||
| 175 | - } | ||
| 176 | 173 | ||
| 177 | - for (instance, outputs) in &mut data.instances { | 174 | + let mut output_changed = false; |
| 178 | - // Clean up dead wl_outputs. | 175 | + if data.output.as_ref() != output { |
| 179 | - outputs.retain(|x| x.is_alive()); | 176 | + data.output = output.cloned(); |
| 177 | + output_changed = true; | ||
| 178 | + } | ||
| 180 | 179 | ||
| 181 | - let mut send_done = something_changed; | 180 | + let something_changed = |
| 181 | + new_title.is_some() || new_app_id.is_some() || states_changed || output_changed; | ||
| 182 | 182 | ||
| 183 | - // If the client bound any more wl_outputs, send enter for them. | 183 | + if something_changed { |
| 184 | + for (instance, outputs) in &mut data.instances { | ||
| 185 | + if let Some(new_title) = new_title { | ||
| 186 | + instance.title(new_title.to_owned()); | ||
| 187 | + } | ||
| 188 | + if let Some(new_app_id) = new_app_id { | ||
| 189 | + instance.app_id(new_app_id.to_owned()); | ||
| 190 | + } | ||
| 191 | + if states_changed { | ||
| 192 | + instance.state(data.states.iter().flat_map(|x| x.to_ne_bytes()).collect()); | ||
| 193 | + } | ||
| 194 | + if output_changed { | ||
| 195 | + for wl_output in outputs.drain(..) { | ||
| 196 | + instance.output_leave(&wl_output); | ||
| 197 | + } | ||
| 184 | if let Some(output) = &data.output { | 198 | if let Some(output) = &data.output { |
| 185 | if let Some(client) = instance.client() { | 199 | if let Some(client) = instance.client() { |
| 186 | for wl_output in output.client_outputs(&client) { | 200 | for wl_output in output.client_outputs(&client) { |
| 187 | - if !outputs.iter().any(|x| x == &wl_output) { | 201 | + instance.output_enter(&wl_output); |
| 188 | - instance.output_enter(&wl_output); | 202 | + outputs.push(wl_output); |
| 189 | - outputs.push(wl_output); | ||
| 190 | - send_done = true; | ||
| 191 | - } | ||
| 192 | } | 203 | } |
| 193 | } | 204 | } |
| 194 | } | 205 | } |
| 195 | - | ||
| 196 | - if send_done { | ||
| 197 | - instance.done(); | ||
| 198 | - } | ||
| 199 | } | 206 | } |
| 200 | } | 207 | } |
| 201 | - Entry::Vacant(entry) => { | 208 | + } |
| 202 | - // New window, start tracking it. | 209 | + |
| 203 | - let mut data = ToplevelData { | 210 | + for (instance, outputs) in &mut data.instances { |
| 204 | - title: role.title.clone(), | 211 | + // Clean up dead wl_outputs. |
| 205 | - app_id: role.app_id.clone(), | 212 | + outputs.retain(|x| x.is_alive()); |
| 206 | - states, | 213 | + |
| 207 | - output: output.cloned(), | 214 | + let mut send_done = something_changed; |
| 208 | - instances: HashMap::new(), | 215 | + |
| 209 | - }; | 216 | + // If the client bound any more wl_outputs, send enter for them. |
| 210 | - | 217 | + if let Some(output) = &data.output { |
| 211 | - for manager in &protocol_state.instances { | 218 | + if let Some(client) = instance.client() { |
| 212 | - if let Some(client) = manager.client() { | 219 | + for wl_output in output.client_outputs(&client) { |
| 213 | - data.add_instance::<State>(&protocol_state.display, &client, manager); | 220 | + if !outputs.iter().any(|x| x == &wl_output) { |
| 221 | + instance.output_enter(&wl_output); | ||
| 222 | + outputs.push(wl_output); | ||
| 223 | + send_done = true; | ||
| 224 | + } | ||
| 214 | } | 225 | } |
| 215 | } | 226 | } |
| 227 | + } | ||
| 216 | 228 | ||
| 217 | - entry.insert(data); | 229 | + if send_done { |
| 230 | + instance.done(); | ||
| 218 | } | 231 | } |
| 219 | } | 232 | } |
| 220 | - }); | 233 | + } |
| 221 | - }); | 234 | + Entry::Vacant(entry) => { |
| 235 | + // New window, start tracking it. | ||
| 236 | + let mut data = ToplevelData { | ||
| 237 | + title: role.title.clone(), | ||
| 238 | + app_id: role.app_id.clone(), | ||
| 239 | + states, | ||
| 240 | + output: output.cloned(), | ||
| 241 | + instances: HashMap::new(), | ||
| 242 | + }; | ||
| 243 | + | ||
| 244 | + for manager in &protocol_state.instances { | ||
| 245 | + if let Some(client) = manager.client() { | ||
| 246 | + data.add_instance::<State>(&protocol_state.display, &client, manager); | ||
| 247 | + } | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + entry.insert(data); | ||
| 251 | + } | ||
| 252 | + } | ||
| 222 | } | 253 | } |
| 223 | 254 | ||
| 224 | impl ToplevelData { | 255 | impl ToplevelData { |