If you have created a multi-window app in SwiftUI for macOS, the tabs come for free. The next question is how to add a new system menu command to open a new tab programatically.

the final product

Here is the code that adds a new command to the menu. It opens a new tab when users click on it or presses “Command+T”.

CommandGroup(before: CommandGroupPlacement.newItem, addition: {
Button(action: {
if let currentWindow = NSApp.keyWindow,
let windowController = currentWindow.windowController {
windowController.newWindowForTab(nil)
if let newWindow = NSApp.keyWindow, currentWindow != newWindow {
currentWindow.addTabbedWindow(newWindow, ordered: .above)
}
}
}, label: {
Text("New Tab")
})
.keyboardShortcut("t", modifiers: [.command])
})

Credits, references


Final product

Here is the final product of what is this post about: implement a multi window app on macOS where each window has its own state while the state is accessible from the system menu.

Use case

We have implemented Multi window SwiftUI app using StateObject. That enabled the app to have multiple windows opened at the same time, where each window holds its own state.

Question (aka The problem)

If there are multiple windows of the same app opened, how to programatically access the currently selected window?

For example, the app has Open File system menu item that is supposed to trigger the file selector…


ObservedObject

The @ObservedObject does not bind the ObservableObject to the view where it is defined. That means that when we change a value in one view, it is going to be propagated between the views as well.

Here is the code to make views with the shared state.

import SwiftUIpublic class ContentViewModel: ObservableObject {
@Published var value: String = ""
}
struct ContentView: View {

@ObservedObject var viewModel: ContentViewModel = ContentViewModel()

var body: some View {
TextField("Value", text: $viewModel.value)
.padding()
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}

StateObject

The @StateObject is binded…


These JavaScript articles are written for people who are interested in coding and are meant to provide an introduction to the programming world.

You are going to learn what are arrays in programming in this article.

Array

An array is a special type of variable that can hold multiple values.

Imagine you need to store a list of values but you don’t know how many values exactly it is because those values might be coming from a user or other sources of data. That means you cannot dynamically create multiple variables to hold all the values, like this:

let name1 =…


These JavaScript articles are written for people who are interested in coding and are meant to provide an introduction to the programming world.

You are going to learn how to use another fundamental building block in programming, called conditional statements (a.k.a if/else branches).

Conditional (if/else) Statements

The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript’s “Conditional” Statements, which are used to perform different actions based on different conditions.
W3Schools

Syntax

There are three ways to define if/else branches.

“If” or nothing


These JavaScript articles are written for people who are interested in coding and are meant to provide an introduction to the programming world.

You are going to learn what is a boolean variable and how to work with it in this article.

Boolean variables

Boolean variables can be assigned only one of these two values: true or false.

<script>
let amIAmazing = true
console.log(amIAmazing)
let isThisFailing = false
console.log(isThisFailing)
</script>

Evaluating values

The logic of a program is often driven by a comparison of a value to another value and then deciding what to do in case the result is true or false.

For…


These JavaScript articles are written for people who are interested in coding and are meant to provide an introduction to the programming world.

You are going to learn various ways to define and work with string variables in this article.

Text/String variables

Text variables are called string variables in programming. String and text are used interchangeably from now on because they are the same thing. Learn more about the strings on Wikipedia.

The text variables are used a lot in programming and there are few ways to define them:


These JavaScript articles are written for people who are interested in coding and are meant to provide an introduction to the programming world.

You are going to learn how to define and work with numeric variables.

Numeric variables

The numeric variables can be whole numbers or decimal numbers. Let’s define a whole number and print out the value into the web browser console.

let age = 24
console.log(age)

Here it is, put it into VS Code and displayed in a web browser console.

You can define a variable with a decimal value in the same way. Just add the decimal part to…


These JavaScript articles are written for people who are interested in coding and are meant to provide an introduction to the programming world.

You are going to learn what are the primitive types in Javascript.

Variable Types

There is a couple of primitive types in JavaScript. The main primitive types in JavaScript are:

The primitive types are…

Ondrej Kvasnovsky

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store