Add exit confirmation dialog
Browse files at this commit4b9cb2f0d3d9b1b2e730844be7d9fe3fb44a5ee1Parent: 3461c664 changed files +224 -3
- src/exit_confirm_dialog.rs +162 -0
- src/input.rs +43 -2
- src/main.rs +1 -0
- src/niri.rs +18 -1
src/exit_confirm_dialog.rsadded+162 -0
| @@ -0,0 +1,162 @@ | |||
| 1 | +use std::cell::RefCell; | ||
| 2 | +use std::collections::HashMap; | ||
| 3 | + | ||
| 4 | +use pangocairo::cairo::{self, ImageSurface}; | ||
| 5 | +use pangocairo::pango::{Alignment, FontDescription}; | ||
| 6 | +use smithay::backend::renderer::element::memory::{ | ||
| 7 | + MemoryRenderBuffer, MemoryRenderBufferRenderElement, | ||
| 8 | +}; | ||
| 9 | +use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | ||
| 10 | +use smithay::backend::renderer::element::{Element, Kind}; | ||
| 11 | +use smithay::output::Output; | ||
| 12 | +use smithay::reexports::gbm::Format as Fourcc; | ||
| 13 | +use smithay::utils::Transform; | ||
| 14 | + | ||
| 15 | +use crate::render_helpers::NiriRenderer; | ||
| 16 | + | ||
| 17 | +const TEXT: &str = "Are you sure you want to exit niri?\n\n\ | ||
| 18 | + Press <span face='mono' bgcolor='#2C2C2C'> Enter </span> to confirm."; | ||
| 19 | +const PADDING: i32 = 16; | ||
| 20 | +const FONT: &str = "sans 14px"; | ||
| 21 | +const BORDER: i32 = 8; | ||
| 22 | + | ||
| 23 | +pub struct ExitConfirmDialog { | ||
| 24 | + is_open: bool, | ||
| 25 | + buffers: RefCell<HashMap<i32, Option<MemoryRenderBuffer>>>, | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +pub type ExitConfirmDialogRenderElement<R> = | ||
| 29 | + RelocateRenderElement<MemoryRenderBufferRenderElement<R>>; | ||
| 30 | + | ||
| 31 | +impl ExitConfirmDialog { | ||
| 32 | + pub fn new() -> anyhow::Result<Self> { | ||
| 33 | + Ok(Self { | ||
| 34 | + is_open: false, | ||
| 35 | + buffers: RefCell::new(HashMap::from([(1, Some(render(1)?))])), | ||
| 36 | + }) | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + pub fn show(&mut self) -> bool { | ||
| 40 | + if !self.is_open { | ||
| 41 | + self.is_open = true; | ||
| 42 | + true | ||
| 43 | + } else { | ||
| 44 | + false | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + pub fn hide(&mut self) -> bool { | ||
| 49 | + if self.is_open { | ||
| 50 | + self.is_open = false; | ||
| 51 | + true | ||
| 52 | + } else { | ||
| 53 | + false | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + pub fn is_open(&self) -> bool { | ||
| 58 | + self.is_open | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + pub fn render<R: NiriRenderer>( | ||
| 62 | + &self, | ||
| 63 | + renderer: &mut R, | ||
| 64 | + output: &Output, | ||
| 65 | + ) -> Option<ExitConfirmDialogRenderElement<R>> { | ||
| 66 | + if !self.is_open { | ||
| 67 | + return None; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + let scale = output.current_scale().integer_scale(); | ||
| 71 | + | ||
| 72 | + let mut buffers = self.buffers.borrow_mut(); | ||
| 73 | + let fallback = buffers[&1].clone().unwrap(); | ||
| 74 | + let buffer = buffers.entry(scale).or_insert_with(|| render(scale).ok()); | ||
| 75 | + let buffer = buffer.as_ref().unwrap_or(&fallback); | ||
| 76 | + | ||
| 77 | + let elem = MemoryRenderBufferRenderElement::from_buffer( | ||
| 78 | + renderer, | ||
| 79 | + (0., 0.), | ||
| 80 | + buffer, | ||
| 81 | + None, | ||
| 82 | + None, | ||
| 83 | + None, | ||
| 84 | + Kind::Unspecified, | ||
| 85 | + ) | ||
| 86 | + .ok()?; | ||
| 87 | + | ||
| 88 | + let output_transform = output.current_transform(); | ||
| 89 | + let output_mode = output.current_mode().unwrap(); | ||
| 90 | + let output_size = output_transform.transform_size(output_mode.size); | ||
| 91 | + | ||
| 92 | + let buffer_size = elem | ||
| 93 | + .geometry(output.current_scale().fractional_scale().into()) | ||
| 94 | + .size; | ||
| 95 | + | ||
| 96 | + let x = (output_size.w / 2 - buffer_size.w / 2).max(0); | ||
| 97 | + let y = (output_size.h / 2 - buffer_size.h / 2).max(0); | ||
| 98 | + let elem = RelocateRenderElement::from_element(elem, (x, y), Relocate::Absolute); | ||
| 99 | + | ||
| 100 | + Some(elem) | ||
| 101 | + } | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +fn render(scale: i32) -> anyhow::Result<MemoryRenderBuffer> { | ||
| 105 | + let _span = tracy_client::span!("exit_confirm_dialog::render"); | ||
| 106 | + | ||
| 107 | + let padding = PADDING * scale; | ||
| 108 | + | ||
| 109 | + let mut font = FontDescription::from_string(FONT); | ||
| 110 | + font.set_absolute_size((font.size() * scale).into()); | ||
| 111 | + | ||
| 112 | + let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?; | ||
| 113 | + let cr = cairo::Context::new(&surface)?; | ||
| 114 | + let layout = pangocairo::create_layout(&cr); | ||
| 115 | + layout.set_font_description(Some(&font)); | ||
| 116 | + layout.set_alignment(Alignment::Center); | ||
| 117 | + layout.set_markup(TEXT); | ||
| 118 | + | ||
| 119 | + let (mut width, mut height) = layout.pixel_size(); | ||
| 120 | + width += padding * 2; | ||
| 121 | + height += padding * 2; | ||
| 122 | + | ||
| 123 | + // FIXME: fix bug in Smithay that rounds pixel sizes down to scale. | ||
| 124 | + width = (width + scale - 1) / scale * scale; | ||
| 125 | + height = (height + scale - 1) / scale * scale; | ||
| 126 | + | ||
| 127 | + let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; | ||
| 128 | + let cr = cairo::Context::new(&surface)?; | ||
| 129 | + cr.set_source_rgb(0.1, 0.1, 0.1); | ||
| 130 | + cr.paint()?; | ||
| 131 | + | ||
| 132 | + cr.move_to(padding.into(), padding.into()); | ||
| 133 | + let layout = pangocairo::create_layout(&cr); | ||
| 134 | + layout.set_font_description(Some(&font)); | ||
| 135 | + layout.set_alignment(Alignment::Center); | ||
| 136 | + layout.set_markup(TEXT); | ||
| 137 | + | ||
| 138 | + cr.set_source_rgb(1., 1., 1.); | ||
| 139 | + pangocairo::show_layout(&cr, &layout); | ||
| 140 | + | ||
| 141 | + cr.move_to(0., 0.); | ||
| 142 | + cr.line_to(width.into(), 0.); | ||
| 143 | + cr.line_to(width.into(), height.into()); | ||
| 144 | + cr.line_to(0., height.into()); | ||
| 145 | + cr.line_to(0., 0.); | ||
| 146 | + cr.set_source_rgb(1., 0.3, 0.3); | ||
| 147 | + cr.set_line_width((BORDER * scale).into()); | ||
| 148 | + cr.stroke()?; | ||
| 149 | + drop(cr); | ||
| 150 | + | ||
| 151 | + let data = surface.take_data().unwrap(); | ||
| 152 | + let buffer = MemoryRenderBuffer::from_memory( | ||
| 153 | + &data, | ||
| 154 | + Fourcc::Argb8888, | ||
| 155 | + (width, height), | ||
| 156 | + scale, | ||
| 157 | + Transform::Normal, | ||
| 158 | + None, | ||
| 159 | + ); | ||
| 160 | + | ||
| 161 | + Ok(buffer) | ||
| 162 | +} | ||
src/input.rsmodified+43 -2
| @@ -57,6 +57,13 @@ impl State { | |||
| 57 | let hide_hotkey_overlay = | 57 | let hide_hotkey_overlay = |
| 58 | self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event); | 58 | self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event); |
| 59 | 59 | ||
| 60 | + let hide_exit_confirm_dialog = self | ||
| 61 | + .niri | ||
| 62 | + .exit_confirm_dialog | ||
| 63 | + .as_ref() | ||
| 64 | + .map_or(false, |d| d.is_open()) | ||
| 65 | + && should_hide_exit_confirm_dialog(&event); | ||
| 66 | + | ||
| 60 | use InputEvent::*; | 67 | use InputEvent::*; |
| 61 | match event { | 68 | match event { |
| 62 | DeviceAdded { device } => self.on_device_added(device), | 69 | DeviceAdded { device } => self.on_device_added(device), |
| @@ -91,6 +98,12 @@ impl State { | |||
| 91 | if hide_hotkey_overlay && self.niri.hotkey_overlay.hide() { | 98 | if hide_hotkey_overlay && self.niri.hotkey_overlay.hide() { |
| 92 | self.niri.queue_redraw_all(); | 99 | self.niri.queue_redraw_all(); |
| 93 | } | 100 | } |
| 101 | + | ||
| 102 | + if let Some(dialog) = &mut self.niri.exit_confirm_dialog { | ||
| 103 | + if hide_exit_confirm_dialog && dialog.hide() { | ||
| 104 | + self.niri.queue_redraw_all(); | ||
| 105 | + } | ||
| 106 | + } | ||
| 94 | } | 107 | } |
| 95 | 108 | ||
| 96 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { | 109 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| @@ -204,6 +217,13 @@ impl State { | |||
| 204 | let key_code = event.key_code(); | 217 | let key_code = event.key_code(); |
| 205 | let modified = keysym.modified_sym(); | 218 | let modified = keysym.modified_sym(); |
| 206 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); | 219 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); |
| 220 | + | ||
| 221 | + if let Some(dialog) = &this.niri.exit_confirm_dialog { | ||
| 222 | + if dialog.is_open() && pressed && raw == Some(Keysym::Return) { | ||
| 223 | + this.niri.stop_signal.stop(); | ||
| 224 | + } | ||
| 225 | + } | ||
| 226 | + | ||
| 207 | should_intercept_key( | 227 | should_intercept_key( |
| 208 | &mut this.niri.suppressed_keys, | 228 | &mut this.niri.suppressed_keys, |
| 209 | bindings, | 229 | bindings, |
| @@ -232,8 +252,14 @@ impl State { | |||
| 232 | 252 | ||
| 233 | match action { | 253 | match action { |
| 234 | Action::Quit => { | 254 | Action::Quit => { |
| 235 | - info!("quitting because quit bind was pressed"); | 255 | + if let Some(dialog) = &mut self.niri.exit_confirm_dialog { |
| 236 | - self.niri.stop_signal.stop() | 256 | + if dialog.show() { |
| 257 | + self.niri.queue_redraw_all(); | ||
| 258 | + } | ||
| 259 | + } else { | ||
| 260 | + info!("quitting because quit bind was pressed"); | ||
| 261 | + self.niri.stop_signal.stop() | ||
| 262 | + } | ||
| 237 | } | 263 | } |
| 238 | Action::ChangeVt(vt) => { | 264 | Action::ChangeVt(vt) => { |
| 239 | self.backend.change_vt(vt); | 265 | self.backend.change_vt(vt); |
| @@ -1401,6 +1427,21 @@ fn should_hide_hotkey_overlay<I: InputBackend>(event: &InputEvent<I>) -> bool { | |||
| 1401 | } | 1427 | } |
| 1402 | } | 1428 | } |
| 1403 | 1429 | ||
| 1430 | +fn should_hide_exit_confirm_dialog<I: InputBackend>(event: &InputEvent<I>) -> bool { | ||
| 1431 | + match event { | ||
| 1432 | + InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, | ||
| 1433 | + InputEvent::PointerButton { .. } | ||
| 1434 | + | InputEvent::PointerAxis { .. } | ||
| 1435 | + | InputEvent::GestureSwipeBegin { .. } | ||
| 1436 | + | InputEvent::GesturePinchBegin { .. } | ||
| 1437 | + | InputEvent::TouchDown { .. } | ||
| 1438 | + | InputEvent::TouchMotion { .. } | ||
| 1439 | + | InputEvent::TabletToolTip { .. } | ||
| 1440 | + | InputEvent::TabletToolButton { .. } => true, | ||
| 1441 | + _ => false, | ||
| 1442 | + } | ||
| 1443 | +} | ||
| 1444 | + | ||
| 1404 | fn allowed_when_locked(action: &Action) -> bool { | 1445 | fn allowed_when_locked(action: &Action) -> bool { |
| 1405 | matches!( | 1446 | matches!( |
| 1406 | action, | 1447 | action, |
src/main.rsmodified+1 -0
| @@ -7,6 +7,7 @@ mod config_error_notification; | |||
| 7 | mod cursor; | 7 | mod cursor; |
| 8 | #[cfg(feature = "dbus")] | 8 | #[cfg(feature = "dbus")] |
| 9 | mod dbus; | 9 | mod dbus; |
| 10 | +mod exit_confirm_dialog; | ||
| 10 | mod frame_clock; | 11 | mod frame_clock; |
| 11 | mod handlers; | 12 | mod handlers; |
| 12 | mod hotkey_overlay; | 13 | mod hotkey_overlay; |
src/niri.rsmodified+18 -1
| @@ -98,6 +98,7 @@ use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; | |||
| 98 | use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; | 98 | use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; |
| 99 | #[cfg(feature = "xdp-gnome-screencast")] | 99 | #[cfg(feature = "xdp-gnome-screencast")] |
| 100 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; | 100 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; |
| 101 | +use crate::exit_confirm_dialog::ExitConfirmDialog; | ||
| 101 | use crate::frame_clock::FrameClock; | 102 | use crate::frame_clock::FrameClock; |
| 102 | use crate::handlers::configure_lock_surface; | 103 | use crate::handlers::configure_lock_surface; |
| 103 | use crate::hotkey_overlay::HotkeyOverlay; | 104 | use crate::hotkey_overlay::HotkeyOverlay; |
| @@ -191,6 +192,7 @@ pub struct Niri { | |||
| 191 | pub screenshot_ui: ScreenshotUi, | 192 | pub screenshot_ui: ScreenshotUi, |
| 192 | pub config_error_notification: ConfigErrorNotification, | 193 | pub config_error_notification: ConfigErrorNotification, |
| 193 | pub hotkey_overlay: HotkeyOverlay, | 194 | pub hotkey_overlay: HotkeyOverlay, |
| 195 | + pub exit_confirm_dialog: Option<ExitConfirmDialog>, | ||
| 194 | 196 | ||
| 195 | #[cfg(feature = "dbus")] | 197 | #[cfg(feature = "dbus")] |
| 196 | pub dbus: Option<crate::dbus::DBusServers>, | 198 | pub dbus: Option<crate::dbus::DBusServers>, |
| @@ -867,6 +869,13 @@ impl Niri { | |||
| 867 | let screenshot_ui = ScreenshotUi::new(); | 869 | let screenshot_ui = ScreenshotUi::new(); |
| 868 | let config_error_notification = ConfigErrorNotification::new(); | 870 | let config_error_notification = ConfigErrorNotification::new(); |
| 869 | let hotkey_overlay = HotkeyOverlay::new(config.clone(), backend.mod_key()); | 871 | let hotkey_overlay = HotkeyOverlay::new(config.clone(), backend.mod_key()); |
| 872 | + let exit_confirm_dialog = match ExitConfirmDialog::new() { | ||
| 873 | + Ok(x) => Some(x), | ||
| 874 | + Err(err) => { | ||
| 875 | + warn!("error creating the exit confirm dialog: {err:?}"); | ||
| 876 | + None | ||
| 877 | + } | ||
| 878 | + }; | ||
| 870 | 879 | ||
| 871 | let socket_source = ListeningSocketSource::new_auto().unwrap(); | 880 | let socket_source = ListeningSocketSource::new_auto().unwrap(); |
| 872 | let socket_name = socket_source.socket_name().to_os_string(); | 881 | let socket_name = socket_source.socket_name().to_os_string(); |
| @@ -973,6 +982,7 @@ impl Niri { | |||
| 973 | screenshot_ui, | 982 | screenshot_ui, |
| 974 | config_error_notification, | 983 | config_error_notification, |
| 975 | hotkey_overlay, | 984 | hotkey_overlay, |
| 985 | + exit_confirm_dialog, | ||
| 976 | 986 | ||
| 977 | #[cfg(feature = "dbus")] | 987 | #[cfg(feature = "dbus")] |
| 978 | dbus: None, | 988 | dbus: None, |
| @@ -1807,7 +1817,14 @@ impl Niri { | |||
| 1807 | elements = self.pointer_element(renderer, output); | 1817 | elements = self.pointer_element(renderer, output); |
| 1808 | } | 1818 | } |
| 1809 | 1819 | ||
| 1810 | - // The config error notification too. | 1820 | + // Next, the exit confirm dialog. |
| 1821 | + if let Some(dialog) = &self.exit_confirm_dialog { | ||
| 1822 | + if let Some(element) = dialog.render(renderer, output) { | ||
| 1823 | + elements.push(element.into()); | ||
| 1824 | + } | ||
| 1825 | + } | ||
| 1826 | + | ||
| 1827 | + // Next, the config error notification too. | ||
| 1811 | if let Some(element) = self.config_error_notification.render(renderer, output) { | 1828 | if let Some(element) = self.config_error_notification.render(renderer, output) { |
| 1812 | elements.push(element.into()); | 1829 | elements.push(element.into()); |
| 1813 | } | 1830 | } |