# Workspace

> The layout of an editor-style window: title bar, docks, center, status bar. A Pyzed component for Python desktop GUIs, with examples and its API.

Source: https://albakore.github.io/pyzed/library/workspace/

:::tip[In plain words]
The whole layout of an editor-like window: bar on top, side panels, the main area and a bar at the bottom.
:::

Builds the frame of an editor-style window: the title bar on top, docks on the left, right and bottom, the center and the status bar. Pass `None` to hide a dock (that is how Ctrl+B works); `left_width`, `right_width` and `bottom_height` set their size and `on_resize` reports drags.

`ui.workspace` · Category: [Workspace](https://albakore.github.io/pyzed/library/#workspace) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#workspace-1)

**Code:**

```python
files = ui.TreeNode.from_paths(["src/main.py", "src/ui.py", "README.md"], status={"src/ui.py": "modified"})
ui.workspace(
    pz.column([
        ui.tab_bar([ui.Tab.for_file("src/main.py")], "src/main.py", on_close=on_close),
        ui.editor("ws-main", "import pyzed as pz\n\n\ndef main():\n    pz.run(App)\n", path="main.py"),
    ]),
    title_bar=ui.title_bar([ui.title_button("pyzed", chevron=True),
                            ui.title_button("main", icon="git-branch", muted=True)],
                           [ui.icon_button("bell", on_bell, size="sm"), ui.avatar("KV")]),
    left=pz.column([ui.panel_header("Project", [ui.icon_button("plus", on_new, size="xs")]),
                    ui.tree(files, expanded={"src"}, selected="src/main.py")]),
    bottom=pz.column([ui.panel_header("Terminal", icon="terminal"),
                      ui.terminal("$ python main.py\n\x1b[32m✓\x1b[0m done")]),
    status_bar=ui.status_bar([ui.status_item("0", icon="circle-x"), ui.status_item("1", icon="triangle-alert")],
                             [ui.status_item("Ln 4, Col 8"), ui.status_item("Python")]),
    left_width=200, bottom_height=110,
)
```

## When to use

- As the root of editor-like apps: IDEs, note apps, database clients, file managers.

## Usage

```python
import pyzed as pz
from pyzed import ui

class Ide(pz.App):
    def __init__(self):
        self.show_left = True

    def on_key(self, combo):
        if combo == "ctrl-b":
            self.show_left = not self.show_left

    def view(self):
        return ui.workspace(
            self.editor_area(),
            title_bar=ui.title_bar([ui.title_button("my-project", chevron=True)]),
            left=self.project_panel() if self.show_left else None,
            status_bar=ui.status_bar([], [ui.status_item("Python")]),
        )
```

## 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.
:::

### Docks

```python
ui.workspace(main_view(), status_bar=ui.status_bar([], [ui.status_item("Python")]))
```

```python
ui.workspace(main_view(), left=pz.column([ui.panel_header("Project"),
             ui.list_item("main.py", icon="file-code", selected=True, inset=True)]), left_width=150)
```

```python
ui.workspace(main_view(), bottom=pz.column([ui.panel_header("Terminal", icon="terminal"),
             ui.terminal("$ python main.py")]), bottom_height=90)
```

```python
ui.workspace(main_view(), right=pz.column([ui.panel_header("Outline"), ui.list_item("class Notes", icon="box")]),
             right_width=150)
```

```python
ui.workspace(main_view(), title_bar=ui.title_bar([ui.title_button("pyzed", chevron=True)],
                                                  [ui.avatar("KV")]))
```

## Events

Workspace has **1 event 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_resize` | `(side, size)` | Called with `(side, size)` when the user finishes resizing a dock by dragging its inner border. |

**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 Ide(pz.App):
    def __init__(self):
        self.left_width, self.bottom_height = 220, 160

    def resized(self, side, size):       # on_resize: which dock and its new size
        if side == "left":
            self.left_width = size
        elif side == "bottom":
            self.bottom_height = size

    def view(self):
        return ui.workspace(main_view(), left=ui.panel_header("Project"), bottom=ui.panel_header("Terminal"),
                            left_width=self.left_width, bottom_height=self.bottom_height,
                            on_resize=self.resized)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Resized:
    side: str
    size: float

class Ide(pz.App):
    sizes = {"left": 220, "bottom": 160}

    def update(self, message):
        match message:
            case Resized(side, size):
                self.sizes = {**self.sizes, side: size}

    def view(self):
        return ui.workspace(main_view(), left=ui.panel_header("Project"), bottom=ui.panel_header("Terminal"),
                            left_width=self.sizes["left"], bottom_height=self.sizes["bottom"],
                            on_resize=Resized)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    left: float = 220
    bottom: float = 160

def view(state, set_state):
    return ui.workspace(main_view(), left=ui.panel_header("Project"), bottom=ui.panel_header("Terminal"),
                        left_width=state.left, bottom_height=state.bottom,
                        on_resize=lambda side, size: set_state(**{side: size}))
```

## Properties and API

### `ui.workspace`

```python
workspace(
    center,
    *,
    title_bar=None,
    left=None,
    right=None,
    bottom=None,
    status_bar=None,
    left_width=240,
    right_width=320,
    bottom_height=220,
    on_resize=None,
    theme=None,
)
```

The layout of an editor-style window: title bar, left, right and bottom docks, center and status bar. Pass `None` to hide a dock.

| Property | Default | Description |
| --- | --- | --- |
| `center` | *required* | Central content (usually tabs + editor). |
| `title_bar` | `None` | Title bar (`ui.title_bar`). |
| `left` | `None` | Left dock. |
| `right` | `None` | Right dock. |
| `bottom` | `None` | Bottom dock. |
| `status_bar` | `None` | Status bar (`ui.status_bar`). |
| `left_width` | `240` | Width of the left dock. |
| `right_width` | `320` | Width of the right dock. |
| `bottom_height` | `220` | Height of the bottom dock. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_resize` ([events](#events)).

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

## Related

- [Title bar](https://albakore.github.io/pyzed/library/title-bar/): The top bar with project and branch pickers.
- [Status bar](https://albakore.github.io/pyzed/library/status-bar/): The bottom bar: diagnostics, cursor position, language.
- [Panel](https://albakore.github.io/pyzed/library/panel/): A dockable, resizable side panel.
- [Split](https://albakore.github.io/pyzed/library/split/): Two panes with a draggable divider.
