Move fallibility inside ExitConfirmDialog

Makes it less annoying on the outside.
This commit is contained in:
Ivan Molodetskikh
2025-08-21 08:57:16 +03:00
parent 70f9ac4af8
commit 0a8b4e036d
3 changed files with 46 additions and 50 deletions
+14 -25
View File
@@ -116,12 +116,8 @@ impl State {
let hide_hotkey_overlay =
self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event);
let hide_exit_confirm_dialog = self
.niri
.exit_confirm_dialog
.as_ref()
.is_some_and(|d| d.is_open())
&& should_hide_exit_confirm_dialog(&event);
let hide_exit_confirm_dialog =
self.niri.exit_confirm_dialog.is_open() && should_hide_exit_confirm_dialog(&event);
use InputEvent::*;
match event {
@@ -159,10 +155,8 @@ impl State {
self.niri.queue_redraw_all();
}
if let Some(dialog) = &mut self.niri.exit_confirm_dialog {
if hide_exit_confirm_dialog && dialog.hide() {
self.niri.queue_redraw_all();
}
if hide_exit_confirm_dialog && self.niri.exit_confirm_dialog.hide() {
self.niri.queue_redraw_all();
}
}
@@ -381,15 +375,14 @@ impl State {
let modified = keysym.modified_sym();
let raw = keysym.raw_latin_sym_or_raw_current_sym();
if let Some(dialog) = &this.niri.exit_confirm_dialog {
if dialog.is_open() && pressed && raw == Some(Keysym::Return) {
info!("quitting after confirming exit dialog");
this.niri.stop_signal.stop();
if this.niri.exit_confirm_dialog.is_open() && pressed && raw == Some(Keysym::Return)
{
info!("quitting after confirming exit dialog");
this.niri.stop_signal.stop();
// Don't send this Enter press to any clients.
this.niri.suppressed_keys.insert(key_code);
return FilterResult::Intercept(None);
}
// Don't send this Enter press to any clients.
this.niri.suppressed_keys.insert(key_code);
return FilterResult::Intercept(None);
}
if pressed
@@ -552,13 +545,9 @@ impl State {
match action {
Action::Quit(skip_confirmation) => {
if !skip_confirmation {
if let Some(dialog) = &mut self.niri.exit_confirm_dialog {
if dialog.show() {
self.niri.queue_redraw_all();
}
return;
}
if !skip_confirmation && self.niri.exit_confirm_dialog.show() {
self.niri.queue_redraw_all();
return;
}
info!("quitting as requested");
+4 -12
View File
@@ -377,7 +377,7 @@ pub struct Niri {
pub screenshot_ui: ScreenshotUi,
pub config_error_notification: ConfigErrorNotification,
pub hotkey_overlay: HotkeyOverlay,
pub exit_confirm_dialog: Option<ExitConfirmDialog>,
pub exit_confirm_dialog: ExitConfirmDialog,
pub pick_window: Option<async_channel::Sender<Option<MappedId>>>,
pub pick_color: Option<async_channel::Sender<Option<niri_ipc::PickedColor>>>,
@@ -2450,13 +2450,7 @@ impl Niri {
hotkey_overlay.show();
}
let exit_confirm_dialog = match ExitConfirmDialog::new() {
Ok(x) => Some(x),
Err(err) => {
warn!("error creating the exit confirm dialog: {err:?}");
None
}
};
let exit_confirm_dialog = ExitConfirmDialog::new();
event_loop
.insert_source(
@@ -4123,10 +4117,8 @@ impl Niri {
}
// Next, the exit confirm dialog.
if let Some(dialog) = &self.exit_confirm_dialog {
if let Some(element) = dialog.render(renderer, output) {
elements.push(element.into());
}
if let Some(element) = self.exit_confirm_dialog.render(renderer, output) {
elements.push(element.into());
}
// Next, the config error notification too.
+28 -13
View File
@@ -27,23 +27,34 @@ pub struct ExitConfirmDialog {
}
impl ExitConfirmDialog {
pub fn new() -> anyhow::Result<Self> {
Ok(Self {
pub fn new() -> Self {
let buffer = match render(1.) {
Ok(x) => Some(x),
Err(err) => {
warn!("error creating the exit confirm dialog: {err:?}");
None
}
};
Self {
is_open: false,
buffers: RefCell::new(HashMap::from([(
NotNan::new(1.).unwrap(),
Some(render(1.)?),
)])),
})
buffers: RefCell::new(HashMap::from([(NotNan::new(1.).unwrap(), buffer)])),
}
}
pub fn can_show(&self) -> bool {
let buffers = self.buffers.borrow();
let fallback = &buffers[&NotNan::new(1.).unwrap()];
fallback.is_some()
}
pub fn show(&mut self) -> bool {
if !self.is_open {
self.is_open = true;
true
} else {
false
if !self.can_show() {
return false;
}
self.is_open = true;
true
}
pub fn hide(&mut self) -> bool {
@@ -72,7 +83,11 @@ impl ExitConfirmDialog {
let output_size = output_size(output);
let mut buffers = self.buffers.borrow_mut();
let fallback = buffers[&NotNan::new(1.).unwrap()].clone().unwrap();
let Some(fallback) = buffers[&NotNan::new(1.).unwrap()].clone() else {
error!("exit confirm dialog opened without fallback buffer");
return None;
};
let buffer = buffers
.entry(NotNan::new(scale).unwrap())
.or_insert_with(|| render(scale).ok());