Add context page for unsaved changes

Aesthetics need tuning but it works.
This commit is contained in:
Josh Megnauth 2024-02-25 01:31:54 -05:00 committed by Jeremy Soller
parent 513d5f9884
commit 5b2f15df37
2 changed files with 48 additions and 7 deletions

View file

@ -22,7 +22,9 @@ staged-changes = Staged changes
project-search = Project search
## Prompt save changes
prompt-save-change = Unsaved changes
prompt-save-changes-title = Unsaved changes
prompt-unsaved-changes = You have unsaved changes. Save?
cancel = Cancel
## Settings
settings = Settings

View file

@ -373,7 +373,7 @@ pub enum ContextPage {
GitManagement,
//TODO: Move search to pop-up
ProjectSearch,
PromptSaveChange,
PromptSaveChanges,
Settings,
}
@ -383,7 +383,7 @@ impl ContextPage {
Self::DocumentStatistics => fl!("document-statistics"),
Self::GitManagement => fl!("git-management"),
Self::ProjectSearch => fl!("project-search"),
Self::PromptSaveChange => fl!("prompt-save-change"),
Self::PromptSaveChanges => fl!("prompt-save-changes-title"),
Self::Settings => fl!("settings"),
}
}
@ -1011,6 +1011,40 @@ impl App {
.into()
}
fn prompt_save_changes(&self) -> Element<Message> {
let (spacing, warning_color) = {
let theme = self.core().system_theme().cosmic();
(theme.spacing, theme.warning_text_color())
};
// Save button is only valid if the file was previously saved
let save = widget::text(fl!("save"));
let save_button = widget::button(save)
.on_press(Message::Save)
.style(theme::Button::Standard)
.width(Length::Fill)
.into();
// Save As is always valid
let save_as = widget::text(fl!("save-as"));
let save_as_button = widget::button(save_as)
.on_press(Message::SaveAsDialog)
.style(theme::Button::Standard)
.width(Length::Fill);
// TODO: Maybe a button to show diffs in a split?
// let diff = widget::text(fl!("diff"));
// let diff_button = widget::button(diff.into());
widget::column::with_children(vec![
widget::text(fl!("prompt-unsaved-changes"))
.style(theme::Text::Color(warning_color.into()))
.into(),
widget::row::with_children(vec![save_button, save_as_button.into()]).into(),
])
.into()
}
fn settings(&self) -> Element<Message> {
let app_theme_selected = match self.config.app_theme {
AppTheme::Dark => 1,
@ -1868,6 +1902,10 @@ impl Application for App {
if let Some(title) = title_opt {
self.tab_model.text_set(entity, title);
}
// Close save changes prompt only if the dialog succeeded
if self.context_page == ContextPage::PromptSaveChanges {
self.core_mut().window.show_context = false;
}
}
}
}
@ -1937,11 +1975,12 @@ impl Application for App {
Some(Tab::Editor(tab)) if tab.changed() => {
// Don't close the save prompt if `TabClose` is emitted again; only
// toggle if no pages or a different page is open
if self.context_page != ContextPage::PromptSaveChange
if self.context_page != ContextPage::PromptSaveChanges
|| !self.core.window.show_context
{
return self
.update(Message::ToggleContextPage(ContextPage::PromptSaveChange));
return self.update(Message::ToggleContextPage(
ContextPage::PromptSaveChanges,
));
}
}
// ...or else just close it
@ -2145,7 +2184,7 @@ impl Application for App {
ContextPage::DocumentStatistics => self.document_statistics(),
ContextPage::GitManagement => self.git_management(),
ContextPage::ProjectSearch => self.project_search(),
ContextPage::PromptSaveChange => return None,
ContextPage::PromptSaveChanges => self.prompt_save_changes(),
ContextPage::Settings => self.settings(),
})
}