You can do that using a combination of TabFocused
event and getEnclosingTabId
method like this:
CMP :
<lightning:workspaceAPI aura:id="workspace" />
<aura:handler event="lightning:tabFocused" action="{! c.onTabFocused }"/>
Controller:
onTabFocused : function(component, event, helper) {
var focusedTabId = event.getParam('currentTabId');
var workspaceAPI = component.find("workspace");
workspaceAPI.getEnclosingTabId().then(function(tabId) {
if(tabId === focusedTabId){
// This means you have focused this component
}
console.log(tabId);
}).catch(function(error) {
console.log(error);
});
}
Focused tab event gets triggered on all component instances, but the code inside your if
will only execute if the current focused tab id is equal to your enclosed tab id. Which means you are focused on a component which is visble in current tab and not on other tabs.