App
Provides methods to manage application lifecycle.
import { app } from '@mobrowser/api';
Example
import { app } from '@mobrowser/api';
console.log(app.name)
console.log(app.version)
Properties
packaged
readonly packaged: boolean;
Indicates whether the application is packaged (i.e., running in production mode) or running in development mode.
Example
import { app } from '@mobrowser/api';
if (app.packaged) {
console.log('Running in production')
}
url
readonly url: string;
The URL of the application frontend entry point.
It can be different based on whether the application is running in the development or
production mode. The value for each mode is configured in the mobrowser.conf.json file
in the configurations section.
Example
import { app } from '@mobrowser/api';
const win = app.createWindow()
win.browser.loadUrl(app.url)
name
readonly name: string;
The application name defined in the mobrowser.conf.json file.
version
readonly version: string;
The application version defined in the mobrowser.conf.json file.
description
readonly description: string;
The application description defined in the mobrowser.conf.json file.
copyright
readonly copyright: string;
The application copyright information defined in the mobrowser.conf.json file.
launchInfo
readonly launchInfo: LaunchInfo;
Contains information about the application launch, including whether it is the first run and the version that was previously launched.
Example
import { app } from '@mobrowser/api';
if (app.launchInfo.isFirstRun) {
console.log('Welcome!')
}
windows
readonly windows: BrowserWindow[];
An array of all currently open browser windows. This array is updated automatically as windows are created and closed.
Example
import { app } from '@mobrowser/api';
const win = app.createWindow()
win.setTitle('MyApp')
app.windows.forEach(win => console.log(win.title))
theme
readonly theme: Theme;
The current application theme.
Example
import { app } from '@mobrowser/api';
const win = app.createWindow()
if (app.theme === 'dark') {
win.setTitle('App (Dark Mode)')
}
menu
readonly menu: Menu;
The application main menu.
Methods
setMenu()
setMenu(menu: Menu): void;
Sets the application main menu.
| Parameter | Type | Description |
|---|---|---|
menu | Menu | The menu to set as the application main menu. |
Example
import { app } from '@mobrowser/api';
app.setMenu(new Menu({
items: [
new MenuItem({
id: 'quit',
label: 'Quit',
action: (item: MenuItem) => {
app.quit()
}
})
]
}))
setTheme()
setTheme(theme: Theme): void;
Sets the application theme.
| Parameter | Type | Description |
|---|---|---|
theme | Theme | The theme to set. |
Example
import { app } from '@mobrowser/api';
// Set the dark theme
app.setTheme('dark')
// Set the light theme
app.setTheme('light')
// Set the theme of the operating system
app.setTheme('system')
createWindow()
createWindow(options?: BrowserWindowOptions): BrowserWindow;
Creates a new browser window with the given options.
| Parameter | Type | Description |
|---|---|---|
options? | BrowserWindowOptions | The options to use for creating the new browser window. If not provided, default options will be used. |
Return value
The newly created BrowserWindow instance.
Example
import { app } from '@mobrowser/api';
const win = app.createWindow()
showMessageDialog()
showMessageDialog(options: MessageDialogOptions): Promise<MessageDialogResult>;
Opens a message dialog with the given options.
| Parameter | Type | Description |
|---|---|---|
options | MessageDialogOptions | The options to use for opening the message dialog. |
Return value
The result of the message dialog as a promise that resolves to a MessageDialogResult object.
Example
import { app } from '@mobrowser/api';
// Create a new window.
const win = app.createWindow()
win.setTitle('MyApp')
win.show()
// Show a message dialog.
const result = await app.showMessageDialog({
parentWindow: win,
title: 'Would you like to delete this conversation?',
message: 'This conversation will be deleted from all of your devices. ' +
'You cannot undo this action.',
buttons: [
{ label: 'Cancel', type: 'secondary' },
{ label: 'Delete', type: 'primary' }
]
})
if (result.button.type === 'primary') {
// The 'Delete' button was clicked.
}
showOpenDialog()
showOpenDialog(options: OpenDialogOptions): Promise<OpenDialogResult>;
Opens a file open dialog with the given options.
| Parameter | Type | Description |
|---|---|---|
options | OpenDialogOptions | The options to use for opening the file open dialog. |
Return value
The result of the file open dialog as a promise that resolves to a OpenDialogResult object.
Example
import { app } from '@mobrowser/api';
// Create a new window.
const win = app.createWindow()
win.setTitle('MyApp')
win.show()
// Show a file open dialog.
const result = await app.showOpenDialog({
parentWindow: win,
title: 'Open Files',
defaultPath: '/Users/john/Desktop',
selectionPolicy: 'files',
features: {
allowMultiple: true,
canCreateDirectories: true
},
})
if (!result.canceled) {
console.log('Selected paths: ', result.paths[0])
}
showSaveDialog()
showSaveDialog(options: SaveDialogOptions): Promise<SaveDialogResult>;
Opens a file save dialog with the given options.
| Parameter | Type | Description |
|---|---|---|
options | SaveDialogOptions | The options to use for opening the file save dialog. |
Return value
The result of the file save dialog as a promise that resolves to a SaveDialogResult object.
Example
import { app } from '@mobrowser/api';
// Create a new window.
const win = app.createWindow()
win.setTitle('MyApp')
win.show()
// Show a file save dialog.
const result = await app.showSaveDialog({
parentWindow: win,
title: 'Save File',
defaultPath: '/Users/john/Desktop',
filters: [{ name: 'Text Files', extensions: ['txt'] }]
})
if (!result.canceled) {
console.log('Selected path: ', result.path)
}
getPath()
getPath(name: PathName): FilePath;
Gets the absolute path to a directory identified by the given name.
| Parameter | Type | Description |
|---|---|---|
name | PathName | Identifies which path should be retrieved. |
Return value
The absolute path to the directory or an empty string if the path is not found.
Example
import { app } from '@mobrowser/api';
// Get the path to the app resources directory.
const dirPath = app.getPath('appResources')
console.log(dirPath)
quit()
quit(): void;
Quits the application, closing all windows and terminating the process.
Example
import { app } from '@mobrowser/api';
const win = app.createWindow()
win.show()
const result = await app.showMessageDialog({
parentWindow: win,
title: 'Quit',
message: 'Are you sure you want to quit?',
buttons: [
{ label: 'Cancel', type: 'secondary' },
{ label: 'Quit', type: 'primary' }
]
})
if (result.button.type === 'primary') {
app.quit()
}
restart()
restart(): void;
Restarts the application, closing all windows and terminating the process.
The new application instance will use the same working directory and the command line arguments as the current one.
Example
import { app } from '@mobrowser/api';
const win = app.createWindow()
win.show()
const result = await app.showMessageDialog({
parentWindow: win,
title: 'Restart',
message: 'Restart to apply the new settings?',
buttons: [
{ label: 'Later', type: 'secondary' },
{ label: 'Restart', type: 'primary' }
]
})
if (result.button.type === 'primary') {
app.restart()
}
checkForUpdate()
checkForUpdate(source: string): Promise<AppUpdate | undefined | string>;
Checks for an available application update.
Available only on Windows and macOS.
| Parameter | Type | Description |
|---|---|---|
source | string | a web server with files for application updates. |
Return value
A promise that resolves to the application update object if an update is available, or undefined if no update is available, or a string containing the error message if an error occurs.
Example
import { app, AppUpdate } from '@mobrowser/api';
const result = await app.checkForUpdate('https://app.com/updates')
if (!result) {
// No update is available or do nothing.
} else if (typeof result === 'string') {
// An error occurred while checking for updates.
} else {
// An update is available.
const appUpdate: AppUpdate = result
console.log('Update available: ', appUpdate.version)
}