Skip to content

Commit

Permalink
Add cancel button to unsaved changes dialogs, fixes #189
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed May 31, 2024
1 parent 0ae4866 commit 62605f7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 68 deletions.
52 changes: 26 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions i18n/en/cosmic_edit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ project-search = Project search
## Prompt save changes
prompt-save-changes-title = Unsaved changes
prompt-unsaved-changes = You have unsaved changes. Save?
cancel = Cancel
discard = Discard changes
save-all = Save all
Expand Down
50 changes: 17 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ pub enum Message {
Cut,
DefaultFont(usize),
DefaultFontSize(usize),
DialogCancel,
DialogMessage(DialogMessage),
Find(Option<bool>),
FindCaseSensitive(bool),
Expand Down Expand Up @@ -1459,41 +1460,18 @@ impl Application for App {

match dialog {
DialogPage::PromptSaveClose(entity) => {
// "Save" is displayed regardless if the file was already saved because the message handles
// "Save As" if necessary
let save = fl!("save");
let save_button =
widget::button::suggested(save).on_press(Message::Save(Some(*entity)));

// "Save As" is only shown if the file has been saved previously
// Rationale: The user may want to save the modified buffer as a new file
let save_as_button = match self.tab_model.data(*entity) {
Some(Tab::Editor(tab)) if tab.path_opt.is_some() => {
let save_as = fl!("save-as");
let save_as_button = widget::button::suggested(save_as)
.on_press(Message::SaveAsDialog(Some(*entity)));
Some(save_as_button)
}
_ => None,
};

// Discards unsaved changes
let discard = fl!("discard");
let discard_button =
widget::button::destructive(discard).on_press(Message::TabCloseForce(*entity));

let mut dialog = widget::dialog(fl!("prompt-save-changes-title"))
widget::button::suggested(fl!("save")).on_press(Message::Save(Some(*entity)));
let discard_button = widget::button::destructive(fl!("discard"))
.on_press(Message::TabCloseForce(*entity));
let cancel_button =
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel);
let dialog = widget::dialog(fl!("prompt-save-changes-title"))
.body(fl!("prompt-unsaved-changes"))
.icon(icon::from_name("dialog-warning-symbolic").size(64))
.primary_action(save_button);
dialog = if let Some(save_as_button) = save_as_button {
dialog
.secondary_action(save_as_button)
.tertiary_action(discard_button)
} else {
dialog.secondary_action(discard_button)
};

.primary_action(save_button)
.secondary_action(discard_button)
.tertiary_action(cancel_button);
Some(dialog.into())
}
DialogPage::PromptSaveQuit(entities) => {
Expand Down Expand Up @@ -1528,12 +1506,15 @@ impl Application for App {
}
let discard_button =
widget::button::destructive(fl!("discard")).on_press(Message::QuitForce);
let cancel_button =
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel);
let dialog = widget::dialog(fl!("prompt-save-changes-title"))
.body(fl!("prompt-unsaved-changes"))
.icon(icon::from_name("dialog-warning-symbolic").size(64))
.control(column)
.primary_action(save_button)
.secondary_action(discard_button);
.secondary_action(discard_button)
.tertiary_action(cancel_button);

Some(dialog.into())
}
Expand Down Expand Up @@ -1668,6 +1649,9 @@ impl Application for App {
log::warn!("failed to find font with index {}", index);
}
},
Message::DialogCancel => {
self.dialog_page_opt = None;
}
Message::DialogMessage(dialog_message) => {
if let Some(dialog) = &mut self.dialog_opt {
return dialog.update(dialog_message);
Expand Down
12 changes: 3 additions & 9 deletions src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,7 @@ where
// Do this first as the horizontal scrollbar is on top of the buffer
if let Some(scrollbar_h_rect) = state.scrollbar_h_rect.get() {
if scrollbar_h_rect.contains(Point::new(x_logical, y_logical)) {
state.dragging = Some(Dragging::ScrollbarH {
start_x: x,
start_scroll: editor.with_buffer(|buffer| buffer.scroll()),
});
state.dragging = Some(Dragging::ScrollbarH { start_x: x });
}
}

Expand Down Expand Up @@ -1120,10 +1117,7 @@ where
buffer.set_scroll(scroll);
});
}
Dragging::ScrollbarH {
start_x,
start_scroll,
} => {
Dragging::ScrollbarH { start_x } => {
editor.with_buffer_mut(|buffer| {
//TODO: store this in state?
let mut max_line_width = 0.0;
Expand Down Expand Up @@ -1211,7 +1205,7 @@ enum ClickKind {
enum Dragging {
Buffer,
ScrollbarV { start_y: f32, start_scroll: Scroll },
ScrollbarH { start_x: f32, start_scroll: Scroll },
ScrollbarH { start_x: f32 },
}

pub struct State {
Expand Down

0 comments on commit 62605f7

Please sign in to comment.