Don't pass the large DialogMessage on the stack

Instead, since the size of DialogMessage is allocated in the Message
enum, pass it as a box, so that only the size of a pointer is allocated
on the stack instead.
This commit is contained in:
Chris Glass 2026-04-10 18:32:09 +02:00
parent f50ece41eb
commit 72a27129e3

View file

@ -375,7 +375,7 @@ pub enum Message {
DefaultFontStretch(usize), DefaultFontStretch(usize),
DefaultFontWeight(usize), DefaultFontWeight(usize),
DefaultZoomStep(usize), DefaultZoomStep(usize),
DialogMessage(DialogMessage), DialogMessage(Box<DialogMessage>), // DialogMessage is huge, so we use a box to make the size of this enum smaller on the stack
Drop(Option<(pane_grid::Pane, segmented_button::Entity, DndDrop)>), Drop(Option<(pane_grid::Pane, segmented_button::Entity, DndDrop)>),
Find(bool), Find(bool),
FindNext, FindNext,
@ -557,13 +557,13 @@ impl App {
color_scheme.into(), color_scheme.into(),
) )
.is_some() .is_some()
{ {
log::warn!( log::warn!(
"custom {:?} color scheme {:?} replaces builtin one", "custom {:?} color scheme {:?} replaces builtin one",
color_scheme_kind, color_scheme_kind,
color_scheme_name color_scheme_name
); );
} }
} }
} }
@ -694,20 +694,21 @@ impl App {
if let Some(tab_model) = self.pane_model.active() { if let Some(tab_model) = self.pane_model.active() {
for entity in tab_model.iter() { for entity in tab_model.iter() {
if tab_model.is_active(entity) if tab_model.is_active(entity)
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity)
let mut terminal = terminal.lock().unwrap(); {
let current_zoom_adj = terminal.zoom_adj(); let mut terminal = terminal.lock().unwrap();
match zoom_message { let current_zoom_adj = terminal.zoom_adj();
Message::ZoomIn => { match zoom_message {
terminal.set_zoom_adj(current_zoom_adj.saturating_add(1)) Message::ZoomIn => {
} terminal.set_zoom_adj(current_zoom_adj.saturating_add(1))
Message::ZoomOut => {
terminal.set_zoom_adj(current_zoom_adj.saturating_sub(1))
}
_ => {}
} }
terminal.set_config(&self.config, &self.themes); Message::ZoomOut => {
terminal.set_zoom_adj(current_zoom_adj.saturating_sub(1))
}
_ => {}
} }
terminal.set_config(&self.config, &self.themes);
}
} }
} }
Task::none() Task::none()
@ -722,9 +723,10 @@ impl App {
ColorSchemeKind::Light => "color_schemes_light", ColorSchemeKind::Light => "color_schemes_light",
}, },
self.config.color_schemes(color_scheme_kind), self.config.color_schemes(color_scheme_kind),
) { )
log::error!("failed to save config: {}", err); {
} log::error!("failed to save config: {}", err);
}
self.update_color_schemes(); self.update_color_schemes();
Task::none() Task::none()
} }
@ -1019,9 +1021,10 @@ impl App {
for action in group.actions { for action in group.actions {
let action_label = shortcuts::action_label(action); let action_label = shortcuts::action_label(action);
if let Some(regex) = &self.shortcut_search_regex if let Some(regex) = &self.shortcut_search_regex
&& regex.find(&action_label).is_none() { && regex.find(&action_label).is_none()
continue; {
} continue;
}
found_actions = true; found_actions = true;
let (bindings, changed) = self.shortcuts_config.bindings_for_action(action); let (bindings, changed) = self.shortcuts_config.bindings_for_action(action);
@ -1535,10 +1538,11 @@ impl App {
let options = tty::Options { let options = tty::Options {
shell: startup_options.shell.or_else(|| { shell: startup_options.shell.or_else(|| {
if let Some(mut args) = shlex::split(&profile.command) if let Some(mut args) = shlex::split(&profile.command)
&& !args.is_empty() { && !args.is_empty()
let command = args.remove(0); {
return Some(tty::Shell::new(command, args)); let command = args.remove(0);
} return Some(tty::Shell::new(command, args));
}
None None
}), }),
working_directory: startup_options.working_directory.or_else( working_directory: startup_options.working_directory.or_else(
@ -1946,24 +1950,24 @@ impl Application for App {
.get(&color_scheme_id) .get(&color_scheme_id)
.map(|color_scheme| color_scheme.name.clone()), .map(|color_scheme| color_scheme.name.clone()),
None => Some(format!("COSMIC {:?}", color_scheme_kind)), None => Some(format!("COSMIC {:?}", color_scheme_kind)),
} && self.dialog_opt.is_none()
{
let (dialog, command) = Dialog::new(
DialogSettings::new().kind(DialogKind::SaveFile {
filename: format!("{}.ron", color_scheme_name),
}),
|msg| Message::DialogMessage(Box::new(msg)),
move |result| {
Message::ColorSchemeExportResult(
color_scheme_kind,
color_scheme_id_opt,
result,
)
},
);
self.dialog_opt = Some(dialog);
return command;
} }
&& self.dialog_opt.is_none() {
let (dialog, command) = Dialog::new(
DialogSettings::new().kind(DialogKind::SaveFile {
filename: format!("{}.ron", color_scheme_name),
}),
Message::DialogMessage,
move |result| {
Message::ColorSchemeExportResult(
color_scheme_kind,
color_scheme_id_opt,
result,
)
},
);
self.dialog_opt = Some(dialog);
return command;
}
} }
Message::ColorSchemeExportResult(color_scheme_kind, color_scheme_id_opt, result) => { Message::ColorSchemeExportResult(color_scheme_kind, color_scheme_id_opt, result) => {
//TODO: show errors in UI //TODO: show errors in UI
@ -2050,7 +2054,7 @@ impl Application for App {
self.color_scheme_errors.clear(); self.color_scheme_errors.clear();
let (dialog, command) = Dialog::new( let (dialog, command) = Dialog::new(
DialogSettings::new().kind(DialogKind::OpenMultipleFiles), DialogSettings::new().kind(DialogKind::OpenMultipleFiles),
Message::DialogMessage, |msg| Message::DialogMessage(Box::new(msg)),
move |result| Message::ColorSchemeImportResult(color_scheme_kind, result), move |result| Message::ColorSchemeImportResult(color_scheme_kind, result),
); );
self.dialog_opt = Some(dialog); self.dialog_opt = Some(dialog);
@ -2108,10 +2112,10 @@ impl Application for App {
.config .config
.color_schemes_mut(color_scheme_kind) .color_schemes_mut(color_scheme_kind)
.get_mut(&color_scheme_id) .get_mut(&color_scheme_id)
{ {
color_scheme.name = color_scheme_name; color_scheme.name = color_scheme_name;
return self.save_color_schemes(color_scheme_kind); return self.save_color_schemes(color_scheme_kind);
} }
} }
Message::ColorSchemeTabActivate(entity) => { Message::ColorSchemeTabActivate(entity) => {
if let Some(color_scheme_kind) = if let Some(color_scheme_kind) =
@ -2292,7 +2296,8 @@ impl Application for App {
}, },
Message::DialogMessage(dialog_message) => { Message::DialogMessage(dialog_message) => {
if let Some(dialog) = &mut self.dialog_opt { if let Some(dialog) = &mut self.dialog_opt {
return dialog.update(dialog_message); // DialogMessage is boxed, so we need to dereference it before updating
return dialog.update(*dialog_message);
} }
} }
Message::Drop(Some((pane, entity, data))) => { Message::Drop(Some((pane, entity, data))) => {
@ -2332,26 +2337,28 @@ impl Application for App {
} }
Message::FindNext => { Message::FindNext => {
if !self.find_search_value.is_empty() if !self.find_search_value.is_empty()
&& let Some(tab_model) = self.pane_model.active() { && let Some(tab_model) = self.pane_model.active()
let entity = tab_model.active(); {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { let entity = tab_model.active();
let mut terminal = terminal.lock().unwrap(); if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
terminal.search(&self.find_search_value, true); let mut terminal = terminal.lock().unwrap();
} terminal.search(&self.find_search_value, true);
} }
}
// Focus correct input // Focus correct input
return self.update_focus(); return self.update_focus();
} }
Message::FindPrevious => { Message::FindPrevious => {
if !self.find_search_value.is_empty() if !self.find_search_value.is_empty()
&& let Some(tab_model) = self.pane_model.active() { && let Some(tab_model) = self.pane_model.active()
let entity = tab_model.active(); {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { let entity = tab_model.active();
let mut terminal = terminal.lock().unwrap(); if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
terminal.search(&self.find_search_value, false); let mut terminal = terminal.lock().unwrap();
} terminal.search(&self.find_search_value, false);
} }
}
// Focus correct input // Focus correct input
return self.update_focus(); return self.update_focus();
@ -2447,9 +2454,10 @@ impl Application for App {
let mut terminal = terminal.lock().unwrap(); let mut terminal = terminal.lock().unwrap();
if let Some(url) = if let Some(url) =
terminal.context_menu.as_ref().and_then(|m| m.link.as_ref()) terminal.context_menu.as_ref().and_then(|m| m.link.as_ref())
&& let Err(err) = open::that_detached(url) { && let Err(err) = open::that_detached(url)
log::warn!("failed to open {:?}: {}", url, err); {
} log::warn!("failed to open {:?}: {}", url, err);
}
terminal.context_menu = None; terminal.context_menu = None;
terminal.active_regex_match = None; terminal.active_regex_match = None;
terminal.needs_update = true; terminal.needs_update = true;
@ -2813,21 +2821,22 @@ impl Application for App {
} }
// Close terminal context menu state // Close terminal context menu state
if let Some(tab_model) = self.pane_model.active() if let Some(tab_model) = self.pane_model.active()
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity)
let mut terminal = terminal.lock().unwrap(); {
//Some actions need the menu_state, let mut terminal = terminal.lock().unwrap();
//so only clear the position for them. //Some actions need the menu_state,
match action { //so only clear the position for them.
Action::LaunchUrlByMenu | Action::CopyUrlByMenu => { match action {
if let Some(context_menu) = terminal.context_menu.as_mut() { Action::LaunchUrlByMenu | Action::CopyUrlByMenu => {
context_menu.position = None; if let Some(context_menu) = terminal.context_menu.as_mut() {
} context_menu.position = None;
}
_ => {
terminal.context_menu = None;
} }
} }
_ => {
terminal.context_menu = None;
}
} }
}
tasks.push(self.update(action.message(Some(entity)))); tasks.push(self.update(action.message(Some(entity))));
return cosmic::Task::batch(tasks); return cosmic::Task::batch(tasks);
} }
@ -2948,9 +2957,7 @@ impl Application for App {
let pos = tab_model let pos = tab_model
.position(tab_model.active()) .position(tab_model.active())
.and_then(|i| (i as usize).checked_sub(1)) .and_then(|i| (i as usize).checked_sub(1))
.unwrap_or_else(|| { .unwrap_or_else(|| tab_model.iter().count().saturating_sub(1));
tab_model.iter().count().saturating_sub(1)
});
let entity = tab_model.iter().nth(pos); let entity = tab_model.iter().nth(pos);
if let Some(entity) = entity { if let Some(entity) = entity {
@ -2990,12 +2997,13 @@ impl Application for App {
}, },
TermEvent::ColorRequest(index, f) => { TermEvent::ColorRequest(index, f) => {
if let Some(tab_model) = self.pane_model.panes.get(pane) if let Some(tab_model) = self.pane_model.panes.get(pane)
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity)
let terminal = terminal.lock().unwrap(); {
let rgb = terminal.colors()[index].unwrap_or_default(); let terminal = terminal.lock().unwrap();
let text = f(rgb); let rgb = terminal.colors()[index].unwrap_or_default();
terminal.input_no_scroll(text.into_bytes()); let text = f(rgb);
} terminal.input_no_scroll(text.into_bytes());
}
} }
TermEvent::CursorBlinkingChange => { TermEvent::CursorBlinkingChange => {
//TODO: should we blink the cursor? //TODO: should we blink the cursor?
@ -3005,10 +3013,11 @@ impl Application for App {
} }
TermEvent::PtyWrite(text) => { TermEvent::PtyWrite(text) => {
if let Some(tab_model) = self.pane_model.panes.get(pane) if let Some(tab_model) = self.pane_model.panes.get(pane)
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity)
let terminal = terminal.lock().unwrap(); {
terminal.input_no_scroll(text.into_bytes()); let terminal = terminal.lock().unwrap();
} terminal.input_no_scroll(text.into_bytes());
}
} }
TermEvent::ResetTitle => { TermEvent::ResetTitle => {
if let Some(tab_model) = self.pane_model.panes.get_mut(pane) { if let Some(tab_model) = self.pane_model.panes.get_mut(pane) {
@ -3028,11 +3037,12 @@ impl Application for App {
} }
TermEvent::TextAreaSizeRequest(f) => { TermEvent::TextAreaSizeRequest(f) => {
if let Some(tab_model) = self.pane_model.panes.get(pane) if let Some(tab_model) = self.pane_model.panes.get(pane)
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity)
let terminal = terminal.lock().unwrap(); {
let text = f(terminal.size().into()); let terminal = terminal.lock().unwrap();
terminal.input_no_scroll(text.into_bytes()); let text = f(terminal.size().into());
} terminal.input_no_scroll(text.into_bytes());
}
} }
TermEvent::Title(title) => { TermEvent::Title(title) => {
if let Some(tab_model) = self.pane_model.panes.get_mut(pane) { if let Some(tab_model) = self.pane_model.panes.get_mut(pane) {
@ -3051,10 +3061,11 @@ impl Application for App {
} }
TermEvent::MouseCursorDirty | TermEvent::Wakeup => { TermEvent::MouseCursorDirty | TermEvent::Wakeup => {
if let Some(tab_model) = self.pane_model.panes.get(pane) if let Some(tab_model) = self.pane_model.panes.get(pane)
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity)
let mut terminal = terminal.lock().unwrap(); {
terminal.needs_update = true; let mut terminal = terminal.lock().unwrap();
} terminal.needs_update = true;
}
} }
TermEvent::ChildExit(_error_code) => { TermEvent::ChildExit(_error_code) => {
//Ignore this for now //Ignore this for now
@ -3191,17 +3202,19 @@ impl Application for App {
} }
Message::ContextMenuPopupClosed(id) => { Message::ContextMenuPopupClosed(id) => {
if let Some((popup_id, pane, entity, _, _, _)) = &self.context_menu_popup if let Some((popup_id, pane, entity, _, _, _)) = &self.context_menu_popup
&& id == *popup_id { && id == *popup_id
// Clear link underline on the terminal {
if let Some(tab_model) = self.pane_model.panes.get(*pane) // Clear link underline on the terminal
&& let Some(terminal) = tab_model.data::<Mutex<Terminal>>(*entity) { if let Some(tab_model) = self.pane_model.panes.get(*pane)
let mut terminal = terminal.lock().unwrap(); && let Some(terminal) = tab_model.data::<Mutex<Terminal>>(*entity)
terminal.context_menu = None; {
terminal.active_regex_match = None; let mut terminal = terminal.lock().unwrap();
terminal.needs_update = true; terminal.context_menu = None;
} terminal.active_regex_match = None;
self.context_menu_popup = None; terminal.needs_update = true;
} }
self.context_menu_popup = None;
}
} }
Message::Surface(a) => { Message::Surface(a) => {
return cosmic::task::message(cosmic::Action::Cosmic( return cosmic::task::message(cosmic::Action::Cosmic(
@ -3311,22 +3324,24 @@ impl Application for App {
fn on_close_requested(&self, id: window::Id) -> Option<Self::Message> { fn on_close_requested(&self, id: window::Id) -> Option<Self::Message> {
if let Some((popup_id, _, _, _, _, _)) = &self.context_menu_popup if let Some((popup_id, _, _, _, _, _)) = &self.context_menu_popup
&& id == *popup_id { && id == *popup_id
return Some(Message::ContextMenuPopupClosed(id)); {
} return Some(Message::ContextMenuPopupClosed(id));
}
None None
} }
fn view_window(&self, window_id: window::Id) -> Element<'_, Message> { fn view_window(&self, window_id: window::Id) -> Element<'_, Message> {
if let Some((popup_id, _pane, entity, ref link, ref autosize_id, _)) = if let Some((popup_id, _pane, entity, ref link, ref autosize_id, _)) =
self.context_menu_popup self.context_menu_popup
&& window_id == popup_id { && window_id == popup_id
return widget::autosize::autosize( {
menu::context_menu(&self.config, &self.key_binds, entity, link.clone()), return widget::autosize::autosize(
autosize_id.clone(), menu::context_menu(&self.config, &self.key_binds, entity, link.clone()),
) autosize_id.clone(),
.into(); )
} .into();
}
match &self.dialog_opt { match &self.dialog_opt {
Some(dialog) => dialog.view(window_id), Some(dialog) => dialog.view(window_id),
None => widget::text("Unknown window ID").into(), None => widget::text("Unknown window ID").into(),