# Unsaved changes

> “Save your changes before closing?”. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/unsaved-changes/

:::tip[In plain words]
The “Save your changes before closing?” question.
:::

“Save your changes to *name* before closing?” with Don't save, Cancel and Save.

`ui.unsaved_changes` · Category: [Dialogs](https://albakore.github.io/pyzed/library/#dialogs) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#unsaved_changes)

**Code:**

```python
ui.dialog_layer(main_view(), ui.unsaved_changes("notes.py", on_save, on_discard, on_cancel))
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- When closing a tab or a window with unsaved edits.

## Usage

```python
from pyzed import ui

ui.dialog_layer(main_view(), ui.unsaved_changes("notes.py", on_save, on_discard, on_cancel))
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Options

```python
ui.unsaved_changes("notes.py", on_save, on_discard, on_cancel)
```

```python
ui.unsaved_changes("report.pzform", on_save, on_discard, on_cancel, width=360)
```

## Events

Unsaved changes has **3 events of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| Event | Handler gets | When |
| --- | --- | --- |
| `on_save` | `()` | Save. |
| `on_discard` | `()` | Don't save. |
| `on_cancel` | `()` | Cancel. |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Notes(pz.App):
    def __init__(self):
        self.closing = True

    def save(self):
        self.closing = False             # write the file, then close

    def view(self):
        dialog = ui.unsaved_changes("notes.py", self.save,
                                    lambda: setattr(self, "closing", False),   # on_discard
                                    lambda: setattr(self, "closing", False))   # on_cancel
        return ui.dialog_layer(main_view(), dialog if self.closing else None)
```

**Declarative:**

```python
class Notes(pz.App):
    closing = True

    def update(self, message):
        match message:
            case "save" | "discard":
                self.closing = False
            case "cancel":
                self.closing = False

    def view(self):
        dialog = ui.unsaved_changes("notes.py", "save", "discard", "cancel")
        return ui.dialog_layer(main_view(), dialog if self.closing else None)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    closing: bool = True
    saved: bool = False

def view(state, set_state):
    dialog = ui.unsaved_changes("notes.py", lambda: set_state(saved=True, closing=False),
                                lambda: set_state(closing=False), lambda: set_state(closing=False))
    return ui.dialog_layer(main_view(), dialog if state.closing else None)
```

## Properties and API

### `ui.unsaved_changes`

```python
unsaved_changes(name, on_save, on_discard, on_cancel, *, width=440, theme=None)
```

"Save your changes to *name* before closing?" with Don't save, Cancel and Save.

| Property | Default | Description |
| --- | --- | --- |
| `name` | *required* | File or document name. |
| `width` | `440` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_save`, `on_discard`, `on_cancel` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#unsaved_changes)

## Related

- [Confirm](https://albakore.github.io/pyzed/library/confirm/): A yes/no question, red for destructive actions.
- [Choice](https://albakore.github.io/pyzed/library/choice/): A message with your own buttons.
- [Tab bar](https://albakore.github.io/pyzed/library/tab-bar/): Editor tabs with icons, dirty dots and close buttons.
