# Dialog App Example

This example demonstrates how to create and display a modal dialog using the NodeActa UI framework. It covers dialog initialization, window positioning and sizing, adding controls, saving and restoring window position, and displaying the dialog with OK and Cancel buttons.

## Dialog Class

The `Dialog` class extends built-in `DialogView`, providing a dedicated window for the application's dialog interface.

### Window Properties

The dialog's appearance and initial position are configured through the following properties:

- `caption` – Sets the dialog window title using the application label.
- `left`, `top` – Defines the dialog's initial position relative to its parent.
- `width`, `height` – Defines the dialog dimensions.
- `borderStyle` – Sets the window border style to `DialogViewBorderStyle.Sizeable`, allowing the user to resize the dialog.

Position and dimensions can be specified using percentages, allowing the dialog to adapt to the screen size.

### Adding Controls

The example creates a `TextView` control and adds it to the dialog:

```
this.textView = new TextView(this);
this.textView.left = 50;
this.textView.top = 50;
this.textView.width = '50%';
this.textView.height = '1ch';
this.textView.textAlignment = Alignment.Center;
this.textView.textVerticalAlignment = VerticalAlignment.Center;
this.textView.text = '<Add Dialog Controls Here>';
```

The `TextView` is positioned and sized relative to the dialog. Its text is centered both horizontally and vertically.

Additional controls can be added in the same way, using the dialog as their parent.

The `LAYOUT DESIGN` markers identify code managed by the Page Designer. Manually modifying this section may interfere with visual layout editing.

## Displaying the Modal Dialog

The `DialogApp` constructor creates an instance of the dialog:

```
this.dialog = new Dialog(this);
```

The application displays it from the asynchronous `run()` method:

```
async run() {
    await this.dialog.showModal(L`OK`, L`Cancel`);
}
```

`showModal()` displays the dialog as a modal window (blocks the rest of the user interface) and provides OK and Cancel buttons. If you need a modeless dialog, instead of `showModal()`, you can call `show()`. Dialogs support up to 5 buttons at the bottom. Or if you want to create the buttons yourself, then just call `showModal()` without parameters. 

`showModal()` returns the name of the pressed button, or `undefined` if closed using the close button in the title bar. 

The `L` template tag is used for localized button captions. You can find more about localization [here](https://www.nodeacta.com/development/localization/).


### Saving and Restoring Window Position

The dialog automatically restores its previous window position and size:

```
this.restoreWindowPos(`${this.app.name}.windowPos`);
```

When the dialog closes, its current window position and size are saved:

```
this.close(e=> {
    this.storeWindowPos(`${this.app.name}.windowPos`);
})
```

Using the application name as part of the storage key allows you using exactly the same code snippet in all apps.

### Preventing Dialog Closing

Also, in the close event you can prevent the Dialog from closing. For example, ask for close confirmation on `OK`, but be silent on `Cancel`:


```
this.close(e=> {
    ...
    if(e.button == L`OK`)
        if (!confirm('Close the Window?'))
            e.cancel = true;
})
```

### Closing Dialog on Escape Button

In `close(handler)` event you can listen keys pressed and respond on `Escape`:


```
this.keyDown(e=>{
    if (e.key == 'Escape') 
        this.close(L`Cancel`);
});
```

### Enabling/Disabling Dialog Buttons

You can access Dialog buttons, but only after dialog is already displayed. This functionality therefore is not available in the constructor, because the buttons are initialized  only after calling `show()` or `showModal()`. 

```
this.buttons[0].enabled = false;
```                
			  	
Alternatively, you can create an interval in the constructor and check is button is available:  

```
setInterval(()=> {
    if(this.buttons[0])
        this.buttons[0] = true or false logic;
}, 1000);
```            

## Summary

This example demonstrates the basic dialog functionality in NodeActa. You can find more about apps [here](https://www.nodeacta.com/development/apps/).
