GUIDE / BUILDING APPLICATIONS

Windows and screens

Open more windows, keep one of each kind, choose the screen a window uses, go full screen and ask before a window closes.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 18 / 29~4 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What you will learn

  • How open_window opens a component in its own window, with props and events.
  • How single=True brings an open window to the front instead of opening another.
  • How to choose a screen and go full screen with <window screen> and fullscreen.
  • How before_leave asks before the user closes a window.

Open another window

open_window(Component, **props) opens an imported component in its own window. Keyword arguments are its props and on_name arguments its events, as in a template: on_saved=accept listens to saved = event(). A state passed as a prop stays reactive in the new window, and an unknown prop is a TypeError that lists the ones available.

modal=True blocks the window that opened it while it is open, and theme="dark" gives it its own theme without changing the application’s. The new window inherits context (inject) from the component that opens it, stays in front of its window, and closes on its own when that component is destroyed. Closing the main window closes the windows it opened.

src/components/Settings.janel
<window title="Settings" width={360} height={220}>
  <column padding={24}>
    <text>Settings window</text>
  </column>
</window>
Copy
src/App.janel
<script lang="python">
from janelkit import open_window, state
from components.Settings import Settings

window = state(None)

def open_settings():
    window.value = open_window(Settings, modal=True, theme="dark")
</script>

<window title="Main" width={380} height={200}>
  <column padding={24} gap={12}>
    <button on:click={open_settings}>Open settings</button>
  </column>
</window>
Copy

Complete example in 2 files · Save each one at the path shown above it.

How it works

  1. Settings is an ordinary component. Its <window> root sets the title and size of its own window.
  2. open_window returns a handle with a reactive is_open, close(), focus() and instance (the component’s namespace).
  3. Each call opens a new window unless you ask for single=True, described next.

What you should see

Open settings shows a separate dark window; while it is open, the main window does not respond. Closing it returns control to the main window.

One window of each kind

Settings, About or a log viewer should open once. open_window(Settings, single=True) checks whether a window of that component is already open: if it is, it brings it to the front and returns it instead of opening another. The props of the second call are not applied. This is what a menu item or a shortcut needs, because the user may press it twice.

python
# In the main component's script.
from janelkit import open_window
from components.About import About

def show_about():
    open_window(About, single=True)   # a second call focuses the open window
Copy

Common mistakes

  • single=True compares components, not props: opening Document for two different files with single=True shows the first one twice. Keep it for windows that exist once.

Choose the screen, and go full screen

screens is the reactive list of connected screens. Each Screen has index, name, width, height, x and y (logical pixels of the virtual desktop), primary and scale (physical pixels per logical pixel). It changes by itself when a screen is connected or disconnected, when the primary one changes or when a resolution changes, so a template that reads it stays up to date.

<window screen={i}> opens the window on that screen, by index or by name, and fullscreen={True} shows it full screen. Both follow state: changing screen while full screen leaves full screen, moves the window and enters again. If the screen does not exist, the window stays where it is.

janel
<script lang="python">
from janelkit import state, screens

target = state(0)
presenting = state(False)
</script>

<window title="Presenter" width={480} height={320} screen={target} fullscreen={presenting}>
  <column padding={24} gap={12}>
    <text>{len(screens)} screen(s) connected</text>
    {#each screens as s (s.index)}
      <button on:click={target = s.index}>{s.name}: {s.width} × {s.height}{" (primary)" if s.primary else ""}</button>
    {/each}
    <switch bind:checked={presenting}>Full screen</switch>
  </column>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. screens is unwrapped in the template like a state, so {#each screens} lists the Screen objects.
  2. Each button moves the window to that screen by setting target.
  3. The switch writes presenting, and fullscreen follows it.

What you should see

The window lists every connected screen with its size. Clicking one moves the window there; turning on Full screen fills that screen, and turning it off restores the window.

Common mistakes

  • screens is empty until the application starts, so read it in the template or after mounting, not at import time.
  • Index 0 is not always the primary screen. Use s.primary, or a screen’s name, when it matters which one you get.

Ask before a window closes

before_leave(fn) registers a question in the script of a component. When the user closes the window that shows it with the X, fn is called; if it returns False, the window stays open. fn can be async, for example to ask with a confirmation dialog. Closing the main window also asks the windows it opened.

Closing from code with handle.close() does not ask: your code already decided. The same before_leave is also asked by a host that is about to discard the component, such as a page of janelkit-router when the user goes back.

python
# In the script of an editor component.
from janelkit import state, before_leave

unsaved = state(False)

@before_leave
def may_close():
    return not unsaved.value     # False keeps the window open
Copy

Common mistakes

  • A guard is not a cleanup: when it refuses, the window must keep working. Release resources in on_destroy.
  • Keep the question quick and honest. A guard that always returns False traps the user.

Learn moreAsk before leaving ↗Lifecycle and effects ↗

Key points

  • open_window opens a component in its own window; props and on_ events work as in templates.
  • single=True reuses an open window; modal=True blocks its opener.
  • screens lists the screens; <window screen fullscreen> places the window and follows state.
  • before_leave asks before the user closes a window with the X.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.