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