-
Notifications
You must be signed in to change notification settings - Fork 4
Window System
Normally, a RetUI application shows one screen at a time.
Sometimes you need something to appear on top of the current screen without leaving it.
For example:
- Confirmation dialogs
- Alert messages
- Login windows
- Popup menus
- Settings dialogs
This is what the Window System is for.
A window floats above your application and can receive its own keyboard input and focus.
The Window System is available in a separate package.
import "github.com/subhasundardass/retui/retui/window"Screens and windows are different.
| Screens | Windows |
|---|---|
| Replace the current screen | Appear on top of the current screen |
| Fill the entire terminal | Have their own size and position |
| Used for navigation | Used for dialogs and popups |
| Only one screen is active | Multiple windows can be open |
Use a screen when moving to another page.
Use a window when you want to display something without leaving the current screen.
Create a new window.
win := window.NewWindow()A new window is hidden by default.
Nothing appears until you call:
win.Show()Usually you'll store the window in a variable so you can show, hide or close it later.
Add a title to the window.
win.SetTitle("Confirmation")Example
┌─ Confirmation ──────────────┐
│ │
│ │
└─────────────────────────────┘
Every window has a width and height.
win.SetSize(40, 10)- First value = width
- Second value = height
Move the window to any position.
win.SetPosition(10, 5)Move it later.
win.MoveTo(20, 8)Move it relative to its current position.
win.MoveBy(2, 1)Most dialogs are simply centered.
win.Center()You can also center only one axis.
win.CenterHorizontally(screenWidth)
win.CenterVertically(screenHeight)Resize a window after it has been created.
win.ResizeTo(60, 20)Get the current size.
width, height := win.GetSize()For simple windows, use SetContent().
win.SetContent(
retui.Text(
"File deleted successfully.",
retui.NewStyle(),
),
)This is perfect for messages that never change.
If your window contains buttons, inputs or state, use SetRenderFn().
win.SetRenderFn(func() retui.Element {
count, setCount := retui.UseState(0)
// Build the window UI
})Because the function runs every render, you can use all RetUI hooks inside it.
Display the window.
win.Show()Hide it temporarily.
win.Hide()Show it again later.
win.Show()Close it permanently.
win.Close()You can also toggle visibility.
win.ToggleVisibility()Check whether the window is currently visible.
win.IsVisible()A modal window blocks everything behind it.
win.SetModal(true)Example
+--------------------------------------+
| Dashboard |
| |
| +----------------------+ |
| | Confirm Delete | |
| | | |
| | Yes Cancel | |
| +----------------------+ |
| |
+--------------------------------------+
While a modal window is open:
- Other windows cannot receive focus.
- Keyboard input goes to the modal.
- The modal always stays on top.
Check if any modal is open.
window.IsAnyModalOpen()Get the active modal.
window.GetActiveModal()Only one window can receive keyboard input.
Bring a window to the front.
win.Focus()Check if it currently has focus.
win.IsFocused()Get the currently focused window.
window.GetFocused()A window can listen for keyboard input.
win.OnKeyPress(func(key retui.Key) {
if key.Code == retui.KeyEscape {
win.Close()
}
})A common pattern is closing dialogs when the user presses Escape.
Get the current position.
x, y := win.GetPosition()Get the size.
width, height := win.GetSize()Get the complete bounds.
x, y, width, height := win.GetBounds()win := window.NewWindow().
SetTitle("Confirm Delete").
SetSize(40, 8).
SetModal(true).
SetContent(
retui.Text(
"Delete this file?",
retui.NewStyle(),
),
).
Center()
win.Show()Result
Confirm Delete
Delete this file?
Yes Cancel
You don't need to manually draw windows.
Simply import the window package.
Whenever you call:
win.Show()RetUI automatically renders the window above your current screen.
In most applications, that's all you need.
✅ Use windows for dialogs and popups.
✅ Use screens for full-page navigation.
✅ Center modal dialogs.
✅ Use SetRenderFn() for interactive windows.
✅ Use SetContent() for simple messages.
✅ Hide a window if you plan to show it again.
✅ Close a window when it is no longer needed.
The Window System lets you display floating interfaces without leaving the current screen.
It supports:
- Floating windows
- Dialogs
- Modal windows
- Keyboard events
- Focus management
- Dynamic content with hooks
- Automatic rendering
With just a few methods, you can build confirmation dialogs, popups, login windows, settings panels and many other overlay interfaces.