balta2ar / brotab

Control your browser's tabs from the command line
MIT License
389 stars 27 forks source link

Changes on Background.js : Improvements for activate --focused #26

Open naeloob opened 4 years ago

naeloob commented 4 years ago

Firefox has another option on windows.update

From Firefox Docs : https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/update

drawAttention : boolean. If true, causes the window to be displayed in a manner that draws the user's attention to the window, without changing the focused window. The effect lasts until the user changes focus to the window.

Don't know why but i have strange behaviour with these options :

 Firefox : 
       focused : executing from the extension doesn't seems to have any effect. Execution from Console it really puts the browser on the front.
       drawAttention : Executing from the console draws attention to the window. Hope it will still have this effect from extension execution.
Chrome : 
      focused : it have the same behaviour as drawAttention on firefox, it don't bring the browser to the front, but it draws your attention to it.
     drawAttention : i have not test it. It only appears on firefox Documentation.

So, until we full understand it, i think the best is add drawAttention on Firefox.

Changes on Class FirefoxTabs :

  activate(tab_id, focused) {
    this._browser.tabs.update(tab_id, {'active': true});
    if (focused) {
         this._browser.tabs.get(tab_id, function(tab) {
           this._browser.windows.update(tab.windowId, {drawAttention: true});
         });

         this._browser.tabs.get(tab_id, function(tab) {
           this._browser.windows.update(tab.windowId, {focused: true});
         });
       }
  }

Changes on Class ChromeTabs :

  activate(tab_id, focused) {
    this._browser.tabs.update(tab_id, {'active': true});
    if (focused) {
         this._browser.tabs.get(tab_id, function(tab) {
           this._browser.windows.update(tab.windowId, {focused: true});
         });
       }
  }

Edit : It seems Chrome also has the drawAttention option, so both classes (FF and Chrome versions) could have the same method.

Edit2 : No, we cannot use the same method neither unify with this._browser... so : Chrome Version

  activate(tab_id, focused) {
    this._browser.tabs.update(tab_id, {'active': true});
    if (focused) {
         this._browser.tabs.get(tab_id, function(tab) {
           chrome.windows.update(tab.windowId, {drawAttention: true});
           chrome.windows.update(tab.windowId, {focused: true});
         });
       }
  }

FF Version

  activate(tab_id, focused) {
    this._browser.tabs.update(tab_id, {'active': true});
    if (focused) {
         this._browser.tabs.get(tab_id, function(tab) {
           browser.windows.update(tab.windowId, {drawAttention: true});
           browser.windows.update(tab.windowId, {focused: true});
         });
       }
  }
naeloob commented 4 years ago

Another implementation to try to get the browser to the front. This time is changing its state : Minimized, Maximized, normal o fullscreen. In general it minimizes the window and then restore its original state.

  activate(tab_id, focused) {
    this._browser.tabs.update(tab_id, {'active': true});
    if (focused) {
        this._browser.tabs.get(tab_id,function (tab){
            this._browser.windows.get(tab.windowId,function (win){
                if (!win.focused) {
                    if (win.state=="normal"){
                        this._browser.windows.update(win.id, {state: "minimized"});
                        this._browser.windows.update(win.id, {state: "normal"});
                    }
                    if (win.state=="maximized"){
                        this._browser.windows.update(win.id, {state: "minimized"});
                        this._browser.windows.update(win.id, {state: "maximized"});
                    }
                    if (win.state=="minimized"){
                        this._browser.windows.update(win.id, {state: "maximized"});
                    }
                    if (win.state=="fullscreen"){
                        this._browser.windows.update(win.id, {state: "minimized"});
                        this._browser.windows.update(win.id, {state: "fullscreen"});
                    }
                }
            });
        });
    }
  }

Edit : forget about this comment. Still Testing.