Skip to content

Commit

Permalink
use snackbars for saving
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepilledgreat committed Jan 23, 2025
1 parent 4aee0a3 commit ee377d9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,12 @@
Command="{Binding Path=SaveCommand, Mode=OneTime}"
Content="Save" />
</Grid>

<ui:Snackbar
x:Name="Snackbar"
Grid.RowSpan="3"
Margin="200,0,200,20"
Panel.ZIndex="9"
Timeout="3000" />
</Grid>
</base:WpfUiWindow>
9 changes: 9 additions & 0 deletions Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public BootstrapperEditorWindow(string name)
themeContents = ToCRLF(themeContents); // make sure the theme is in CRLF. a function expects CRLF.

var viewModel = new BootstrapperEditorWindowViewModel();
viewModel.ThemeSavedCallback = ThemeSavedCallback;
viewModel.Directory = directory;
viewModel.Name = name;
viewModel.Title = $"Editing \"{name}\"";
Expand All @@ -166,6 +167,14 @@ private void LoadHighlightingTheme()
UIXML.TextArea.TextView.SetResourceReference(ICSharpCode.AvalonEdit.Rendering.TextView.LinkTextForegroundBrushProperty, "NewTextEditorLink");
}

private void ThemeSavedCallback(bool success, string message)
{
if (success)
Snackbar.Show("Settings saved!", message, Wpf.Ui.Common.SymbolRegular.CheckmarkCircle32, Wpf.Ui.Common.ControlAppearance.Success);
else
Snackbar.Show("Error", message, Wpf.Ui.Common.SymbolRegular.ErrorCircle24, Wpf.Ui.Common.ControlAppearance.Danger);
}

private static string ToCRLF(string text)
{
return text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class BootstrapperEditorWindowViewModel : NotifyPropertyChangedViewModel
public ICommand SaveCommand => new RelayCommand(Save);
public ICommand OpenThemeFolderCommand => new RelayCommand(OpenThemeFolder);

public Action<bool, string> ThemeSavedCallback { get; set; } = null!;

public string Directory { get; set; } = "";

public string Name { get; set; } = "";
Expand All @@ -34,8 +36,7 @@ private void Preview()

dialog.ApplyCustomTheme(Name, Code);

if (_dialog != null)
_dialog.CloseBootstrapper();
_dialog?.CloseBootstrapper();
_dialog = dialog;

dialog.Message = Strings.Bootstrapper_StylePreview_TextCancel;
Expand All @@ -55,18 +56,20 @@ private void Save()
{
const string LOG_IDENT = "BootstrapperEditorWindowViewModel::Save";

string path = Path.Combine(Paths.CustomThemes, Name, "Theme.xml");
string path = Path.Combine(Directory, "Theme.xml");

try
{
File.WriteAllText(path, Code);
ThemeSavedCallback.Invoke(true, "Your theme has been saved!");
}
catch (Exception ex)
{
App.Logger.WriteLine(LOG_IDENT, "Failed to save custom theme");
App.Logger.WriteException(LOG_IDENT, ex);

Frontend.ShowMessageBox($"Failed to save theme: {ex.Message}", MessageBoxImage.Error, MessageBoxButton.OK);
//Frontend.ShowMessageBox($"Failed to save theme: {ex.Message}", MessageBoxImage.Error, MessageBoxButton.OK);
ThemeSavedCallback.Invoke(false, ex.Message);
}
}

Expand Down

0 comments on commit ee377d9

Please sign in to comment.