|
| 1 | +# The Renoise Application |
| 2 | + |
| 3 | +The `renoise.app()` function is the entry point for interacting with the Renoise application itself. It returns a [`renoise.Application`](../API/renoise/renoise.Application.md) object, which provides access to global application states, functions for user interaction like dialogs, and control over the main window. |
| 4 | + |
| 5 | +## Controlling the Application Window |
| 6 | + |
| 7 | +The [`renoise.ApplicationWindow`](../API/renoise/renoise.ApplicationWindow.md) object, accessed via `renoise.app().window`, allows you to query and control the state of the main Renoise UI. |
| 8 | + |
| 9 | +```lua |
| 10 | +local window = renoise.app().window |
| 11 | + |
| 12 | +-- Toggle fullscreen mode |
| 13 | +window.fullscreen = not window.fullscreen |
| 14 | + |
| 15 | +-- Check if the disk browser is visible |
| 16 | +if window.disk_browser_is_visible then |
| 17 | + print("Disk browser is currently open.") |
| 18 | +end |
| 19 | + |
| 20 | +-- Switch the middle frame to the Mixer view |
| 21 | +window.active_middle_frame = |
| 22 | + renoise.ApplicationWindow.MIDDLE_FRAME_MIXER |
| 23 | +``` |
| 24 | + |
| 25 | +You can also attach notifiers to many window properties to react to UI changes made by the user. |
| 26 | + |
| 27 | +```lua |
| 28 | +local window = renoise.app().window |
| 29 | + |
| 30 | +-- Be notified when the disk browser visibility changes |
| 31 | +function disk_browser_visibility_changed() |
| 32 | + if window.disk_browser_is_visible then |
| 33 | + renoise.app():show_status("Disk browser was opened.") |
| 34 | + else |
| 35 | + renoise.app():show_status("Disk browser was closed.") |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +window.disk_browser_is_visible_observable:add_notifier( |
| 40 | + disk_browser_visibility_changed |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +## Song and File Handling |
| 45 | + |
| 46 | +The application object provides functions to load, and save songs, instrument and other kinds of components. Note that these operations are not always instantaneous, as Renoise may need to prompt the user to save changes. |
| 47 | + |
| 48 | +To reliably execute code after a new song is created or loaded, you should use notifiers. |
| 49 | + |
| 50 | +```lua |
| 51 | +local app = renoise.app() |
| 52 | + |
| 53 | +-- This will show a file dialog to the user. The song is not loaded immediately. |
| 54 | +app:load_song("/path/to/some/song_file") |
| 55 | + |
| 56 | +-- To run code after a new song is ready, use a notifier. |
| 57 | +-- This is typically done in your tool's initialization code. |
| 58 | +function handle_new_document() |
| 59 | + print("A new song was created or loaded. The new song name is: " .. |
| 60 | + renoise.song().name) |
| 61 | +end |
| 62 | + |
| 63 | +-- The 'new_document_observable' is fired after a new song is |
| 64 | +--- successfully created or loaded. |
| 65 | +renoise.tool().app_new_document_observable:add_notifier(handle_new_document) |
| 66 | + |
| 67 | +-- To save a song to a specific file: |
| 68 | +app:save_song_as("/path/to/MyNewSong.xrns") |
| 69 | +``` |
| 70 | + |
| 71 | +## Showing Dialogs and Messages |
| 72 | + |
| 73 | +A common task for tools is to communicate with the user. The application object provides several functions to show different kinds of dialogs and messages. |
| 74 | + |
| 75 | +### Simple Messages |
| 76 | + |
| 77 | +For simple notifications, you can use `show_message`, `show_error`, `show_warning`, or `show_status`. |
| 78 | + |
| 79 | +```lua |
| 80 | +local app = renoise.app() |
| 81 | + |
| 82 | +-- Show a message in the status bar |
| 83 | +app:show_status("This is a status message.") |
| 84 | + |
| 85 | +-- Show a modal info dialog |
| 86 | +app:show_message("This is an informational message.") |
| 87 | + |
| 88 | +-- Show a modal warning dialog |
| 89 | +app:show_warning("This is a warning message.") |
| 90 | + |
| 91 | +-- Show a modal error dialog |
| 92 | +app:show_error("This is an error message.") |
| 93 | +``` |
| 94 | + |
| 95 | +### User Prompts |
| 96 | + |
| 97 | +To ask the user for a choice, you can use `show_prompt`. It displays a dialog with custom buttons and returns the label of the button that was pressed. |
| 98 | + |
| 99 | +```lua |
| 100 | +local app = renoise.app() |
| 101 | + |
| 102 | +local pressed_button = app:show_prompt( |
| 103 | + "Confirm Action", |
| 104 | + "Do you want to proceed?", |
| 105 | + { "Yes", "No", "Cancel" } |
| 106 | +) |
| 107 | + |
| 108 | +if pressed_button == "Yes" then |
| 109 | + app:show_message("You chose to proceed.") |
| 110 | +elseif pressed_button == "No" then |
| 111 | + app:show_message("You chose not to proceed.") |
| 112 | +else |
| 113 | + app:show_message("You canceled the operation.") |
| 114 | +end |
| 115 | +``` |
| 116 | + |
| 117 | +### Custom Dialogs |
| 118 | + |
| 119 | +For more complex user interfaces, you can create custom non-modal dialogs (tool windows) with `show_custom_dialog`. This requires using the `renoise.ViewBuilder` API to construct the UI. |
| 120 | + |
| 121 | +```lua |
| 122 | +-- A simple custom dialog example |
| 123 | +local vb = renoise.ViewBuilder() |
| 124 | + |
| 125 | +local my_dialog = nil |
| 126 | +local my_dialog_content = vb:column { |
| 127 | + margin = renoise.ViewBuilder.DEFAULT_DIALOG_MARGIN, |
| 128 | + spacing = renoise.ViewBuilder.DEFAULT_DIALOG_SPACING, |
| 129 | + views = { |
| 130 | + vb:text { |
| 131 | + text = "This is a custom dialog." |
| 132 | + }, |
| 133 | + vb:button { |
| 134 | + text = "Close Me", |
| 135 | + notifier = function() |
| 136 | + -- my_dialog is defined when calling show_custom_dialog |
| 137 | + my_dialog:close() |
| 138 | + end |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +my_dialog = renoise.app():show_custom_dialog( |
| 144 | + "My Tool Window", my_dialog_content |
| 145 | +) |
| 146 | +``` |
| 147 | + |
| 148 | +## File and Path Prompts |
| 149 | + |
| 150 | +Your tool might need to read from or write to files. The API provides functions to open native file browser dialogs. |
| 151 | + |
| 152 | +```lua |
| 153 | +local app = renoise.app() |
| 154 | + |
| 155 | +-- Prompt the user to select a directory |
| 156 | +local dir_path = app:prompt_for_path("Select a Project Folder") |
| 157 | +if dir_path and #dir_path > 0 then |
| 158 | + app:show_message("You selected the directory: " .. dir_path) |
| 159 | +end |
| 160 | + |
| 161 | +-- Prompt the user to select a file to open |
| 162 | +local file_to_read = app:prompt_for_filename_to_read( |
| 163 | + { "txt", "lua" }, |
| 164 | + "Open a Text File" |
| 165 | +) |
| 166 | +if file_to_read and #file_to_read > 0 then |
| 167 | + app:show_message("You selected the file: " .. file_to_read) |
| 168 | +end |
| 169 | + |
| 170 | +-- Prompt the user for a filename to save |
| 171 | +local file_to_write = app:prompt_for_filename_to_write( |
| 172 | + "xml", |
| 173 | + "Save Configuration" |
| 174 | +) |
| 175 | +if file_to_write and #file_to_write > 0 then |
| 176 | + app:show_message("File will be saved to: " .. file_to_write) |
| 177 | +end |
| 178 | +``` |
0 commit comments