in sample/src/main/kotlin/org/jetbrains/desktop/sample/macos/SkikoSampleMac.kt [588:1104]
fun buildMenu(): AppMenuStructure {
return AppMenuStructure(
AppMenuItem.SubMenu(
// Ignored
title = "App",
AppMenuItem.Action(
"New Window",
keystroke = Keystroke(key = "n", modifiers = KeyModifiersSet.create(command = true)),
perform = { createWindow(useCustomTitlebar = true) },
),
AppMenuItem.Action(
"Open...",
keystroke = Keystroke(key = "o", modifiers = KeyModifiersSet.create(command = true, shift = true)),
perform = {
val path = FileDialog.showOpenFileDialog(
params = FileDialog.CommonDialogParams(
title = "Hello Dialog",
prompt = "Acceptable",
directoryUrl = "/Users/pavel.sergeev/AndroidStudioProjects",
),
openDialogParams = FileDialog.OpenDialogParams(allowsMultipleSelections = true),
)
println("openFileDialog callback received $path")
},
),
AppMenuItem.Action(
"New Titled Window",
keystroke = Keystroke(key = "n", modifiers = KeyModifiersSet.create(command = true, shift = true)),
perform = { createWindow(useCustomTitlebar = false) },
),
AppMenuItem.Separator,
AppMenuItem.Action(
"Hide ${Application.name}",
keystroke = Keystroke(key = "h", modifiers = KeyModifiersSet.create(command = true)),
perform = {
Application.hide()
},
),
AppMenuItem.Action(
"Hide Others",
keystroke = Keystroke(key = "h", modifiers = KeyModifiersSet.create(command = true, option = true)),
perform = {
Application.hideOtherApplications()
},
),
AppMenuItem.Action(
"Show All",
perform = {
Application.unhideAllApplications()
},
),
AppMenuItem.Action(
"Quit",
keystroke = Keystroke(key = "q", modifiers = KeyModifiersSet.create(command = true)),
perform = {
Application.stopEventLoop()
},
),
specialTag = AppMenuItem.SubMenu.SpecialTag.AppNameMenu,
),
AppMenuItem.SubMenu(
title = "Edit",
AppMenuItem.Action(
title = "Log input source",
keystroke = Keystroke(key = "x", modifiers = KeyModifiersSet.create(command = true)),
specialTag = AppMenuItem.Action.SpecialTag.Cut,
perform = {
Logger.info { "Current input source: ${Application.currentInputSource()}" }
},
),
AppMenuItem.Action(
title = "Cut",
keystroke = Keystroke(key = "x", modifiers = KeyModifiersSet.create(command = true)),
specialTag = AppMenuItem.Action.SpecialTag.Cut,
perform = {
},
),
AppMenuItem.Action(
title = "Copy",
keystroke = Keystroke(key = "c", modifiers = KeyModifiersSet.create(command = true)),
specialTag = AppMenuItem.Action.SpecialTag.Copy,
perform = {
Pasteboard.clear()
val result = Pasteboard.writeObjects(Pasteboard.Item.of(Pasteboard.PNG_IMAGE_TYPE, jbIconBytes()))
Logger.info { "Pasteboard.writeObjects result: $result" }
},
),
AppMenuItem.Action(
title = "Paste",
keystroke = Keystroke(key = "v", modifiers = KeyModifiersSet.create(command = true)),
specialTag = AppMenuItem.Action.SpecialTag.Paste,
perform = {
Logger.info {
Pasteboard.readItemsOfType(Pasteboard.STRING_TYPE).toString()
.replace("\n", "\\n")
.replace("\t", "\\t")
.replace("\r", "\\r")
.replace("\"", "\\\"")
}
},
),
AppMenuItem.Action(
title = "Paste Files",
keystroke = Keystroke(key = "v", modifiers = KeyModifiersSet.create(command = true, shift = true)),
perform = {
Pasteboard.readFileItemPaths()
},
),
),
AppMenuItem.SubMenu(
title = "View",
AppMenuItem.Action(
title = "Set Title",
keystroke = Keystroke(key = "s", modifiers = KeyModifiersSet.create(command = true)),
perform = {
mainWindow()?.window?.let { window ->
val previousTitle = window.title
Logger.info { "Title was: $previousTitle" }
window.title = "$previousTitle[x]"
}
},
),
AppMenuItem.Action(
title = "Toggle Full Screen",
state = when (mainWindow()?.window?.isFullScreen) {
true -> AppMenuItem.ActionItemState.On
else -> AppMenuItem.ActionItemState.Off
},
keystroke = Keystroke(key = "f", modifiers = KeyModifiersSet.create(command = true, control = true)),
perform = { mainWindow()?.window?.toggleFullScreen() },
),
// specialTag = "View",
),
AppMenuItem.SubMenu(
title = "Animation",
AppMenuItem.Action(
title = "Pause",
keystroke = Keystroke(key = "p", modifiers = KeyModifiersSet.create(command = true)),
perform = { setPaused(true) },
),
AppMenuItem.Action(
title = "Run",
keystroke = Keystroke(key = "r", modifiers = KeyModifiersSet.create(command = true)),
perform = { setPaused(false) },
),
),
AppMenuItem.SubMenu(
title = "Displays",
AppMenuItem.Action(
title = "List Displays",
keystroke = Keystroke(key = "d", modifiers = KeyModifiersSet.create(command = true)),
perform = { Logger.info { Screen.allScreens().toString() } },
),
),
AppMenuItem.SubMenu(
title = "Window",
AppMenuItem.Action(
title = "Increase Size",
keystroke = Keystroke(key = "+", modifiers = KeyModifiersSet.create(command = true)),
perform = { changeCurrentWindowSize(50.0) },
),
AppMenuItem.Action(
title = "Decrease Size",
keystroke = Keystroke(key = "-", modifiers = KeyModifiersSet.create(command = true)),
perform = { changeCurrentWindowSize(-50.0) },
),
AppMenuItem.SubMenu(
title = "Titlebar",
AppMenuItem.Action(
title = "Increate Titlebar Height",
keystroke = Keystroke(key = "+", modifiers = KeyModifiersSet.create(option = true, command = true)),
perform = {
changeTitlebarHeight(2.0)
},
),
AppMenuItem.Action(
title = "Decrease Titlebar Height",
keystroke = Keystroke(key = "-", modifiers = KeyModifiersSet.create(option = true, command = true)),
perform = {
changeTitlebarHeight(-2.0)
},
),
AppMenuItem.Action(
title = "Switch to Custom Titlebar",
perform = { setCustomTitlebarEnabled(true) },
),
AppMenuItem.Action(
title = "Switch to Regular Titlebar",
perform = { setCustomTitlebarEnabled(false) },
),
),
AppMenuItem.Action(
title = "Toggle Miniaturize first Window",
keystroke = Keystroke(key = "m", modifiers = KeyModifiersSet.create(command = true, option = true)),
perform = { toggleMiniaturizationOfFirstWindow() },
),
AppMenuItem.Action(
title = "Make Window Transparent",
keystroke = Keystroke(key = "t", modifiers = KeyModifiersSet.create(command = true)),
perform = { makeWindowTransparent() },
),
AppMenuItem.Action(
title = "Make Window Opaque",
keystroke = Keystroke(key = "o", modifiers = KeyModifiersSet.create(command = true)),
perform = { makeWindowOpaque() },
),
AppMenuItem.Action(
title = "Cycle Window Effects",
keystroke = Keystroke(key = "e", modifiers = KeyModifiersSet.create(command = true)),
perform = { cycleWindowEffects() },
),
AppMenuItem.Action(
title = "Log Window Position",
keystroke = Keystroke(key = "l", modifiers = KeyModifiersSet.create(command = true)),
perform = {
mainWindow()?.window?.let { window ->
Logger.info {
"""
WindowId: ${window.windowId()}
origin: ${window.origin}
size: ${window.size}
contentOrigin: ${window.contentOrigin}
contentSize: ${window.contentSize}
""".trimIndent()
}
}
},
),
AppMenuItem.Action(
title = "Lock Window",
keystroke = Keystroke(key = "L", modifiers = KeyModifiersSet.create(command = true, shift = true)),
perform = {
if (lockedWindowId == null) {
lockedWindowId = mainWindow()?.window?.windowId()
} else {
lockedWindowId = null
}
},
),
AppMenuItem.Action(
title = "Close Window",
keystroke = Keystroke(key = "w", modifiers = KeyModifiersSet.create(command = true)),
perform = {
mainWindow()?.let {
killWindow(it)
}
},
),
specialTag = AppMenuItem.SubMenu.SpecialTag.Window,
),
AppMenuItem.SubMenu(
title = "Mouse",
AppMenuItem.Action(
title = "Cycle Mouse Cursor",
keystroke = Keystroke(key = "m", modifiers = KeyModifiersSet.create(command = true)),
perform = { cycleCursor() },
),
AppMenuItem.Action(
title = "Mouse Toggle Cursor",
keystroke = Keystroke(key = "h", modifiers = KeyModifiersSet.create(command = true)),
perform = {
Cursor.hidden = !Cursor.hidden
},
),
),
AppMenuItem.SubMenu(
title = "Notifications",
AppMenuItem.Action(
title = "Request Permission",
perform = {
NotificationCenter.requestAuthorization { granted ->
Logger.info { "Notification permission ${if (granted) "granted" else "denied"}" }
}
},
),
AppMenuItem.Action(
title = "Log Notification Capabilities",
perform = {
NotificationCenter.getAuthorizationStatus { status ->
Logger.info { "Notification authorization status: $status" }
}
},
),
AppMenuItem.Action(
title = "Show Test Notification",
keystroke = Keystroke(key = "n", modifiers = KeyModifiersSet.create(command = true, option = true)),
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Test Notification",
body = "This is a test notification from Kotlin Desktop Toolkit!",
notificationId = NotificationCenter.NotificationId("TestNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show notification: $error" }
} else {
Logger.info { "Notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Notification with Default Sound",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Default Sound Notification",
body = "This notification uses the default system sound.",
sound = NotificationSound.Default,
notificationId = NotificationCenter.NotificationId("DefaultSoundNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show notification with default sound: $error" }
} else {
Logger.info { "Default sound notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Silent Notification",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Silent Notification",
body = "This notification has no sound.",
sound = NotificationSound.None,
notificationId = NotificationCenter.NotificationId("SilentNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show silent notification: $error" }
} else {
Logger.info { "Silent notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Critical Alert Notification",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Critical Alert",
body = "This is a critical alert that bypasses Do Not Disturb and mute switch.",
sound = NotificationSound.Critical,
notificationId = NotificationCenter.NotificationId("CriticalAlertNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show critical notification: $error" }
} else {
Logger.info { "Critical notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Ringtone Notification",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Ringtone Notification",
body = "This notification uses the default ringtone sound.",
sound = NotificationSound.Ringtone,
notificationId = NotificationCenter.NotificationId("RingtoneNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show ringtone notification: $error" }
} else {
Logger.info { "Ringtone notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Named Sound Notification (Basso)",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Named Sound Notification",
body = "This notification uses the Basso system sound.",
sound = NotificationSound.Named("Basso.aiff"),
notificationId = NotificationCenter.NotificationId("NamedSoundNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show named sound notification: $error" }
} else {
Logger.info { "Named sound notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Critical Named Sound Notification (Funk)",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Critical Named Sound",
body = "This notification uses the Funk system sound as a critical alert.",
sound = NotificationSound.CriticalNamed("Funk.aiff"),
notificationId = NotificationCenter.NotificationId(
"CriticalNamedSoundNotification${nextNotificationId++}",
),
) { error ->
if (error != null) {
Logger.error { "Failed to show critical named sound notification: $error" }
} else {
Logger.info { "Critical named sound notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Separator,
AppMenuItem.Action(
title = "Show Notification with Action Buttons",
keystroke = Keystroke(key = "a", modifiers = KeyModifiersSet.create(command = true, option = true)),
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Action Required",
body = "Please choose an action:",
sound = NotificationSound.Default,
categoryId = NotificationCenter.CategoryId("action_category"),
notificationId = NotificationCenter.NotificationId("TestNotification${nextNotificationId++}"),
) { error ->
if (error != null) {
Logger.error { "Failed to show action notification: $error" }
} else {
Logger.info { "Action notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Show Evil Notification with Action Buttons",
perform = {
thread {
sleep(5000)
GrandCentralDispatch.dispatchOnMain {
NotificationCenter.showNotification(
title = "Evil Action Required",
body = "Please choose an action:",
sound = NotificationSound.Default,
notificationId = NotificationCenter.NotificationId("EvilTestNotification${nextNotificationId++}"),
categoryId = NotificationCenter.CategoryId("evil_action_category"),
) { error ->
if (error != null) {
Logger.error { "Failed to show action notification: $error" }
} else {
Logger.info { "Action notification delivered successfully" }
}
}
}
}
},
),
AppMenuItem.Action(
title = "Request User Attention",
perform = {
thread {
sleep(3000)
for (i in 1..3) {
GrandCentralDispatch.dispatchOnMain {
val requestId = Application.requestUserAttention()
println("RequestId: $requestId")
}
}
}
},
),
AppMenuItem.Action(
title = "Request User Attention Critical",
perform = {
thread {
sleep(3000)
GrandCentralDispatch.dispatchOnMain {
val requestId = Application.requestUserAttention(isCritical = true)
println("RequestId: $requestId")
}
}
},
),
),
AppMenuItem.SubMenu(title = "Help"),
)
}