For example, when I use the Messages app in which I use one keyboard, and in Xcode, I use only English keyboard. Can macOS switch my keyboard to English when I make the Xcode window active?
Follow these steps to changing your keyboard language dynamically:
I have developed a app for this functionality.
It will help you to setting default input source for almost every app. By using it, when your app become active, the input source will be automatically changed to the default input source which you setted in GhostSKB.
You may also want to try Punto Switcher. Used it for a long time, worked perfectly. They have versions for Mac and Windows. It automagically recognizes the language you typing in and switches keyboard layout to that language, converting everything you have already typed before into correct layout! And it's free.
UPD. You may also have a look to Caramba Switcher. This is newer version of Punto from the same developer. Punto was bought by Yandex and frozen. Caramba is alive.